a37d65a056e27c69782eb0d1f5ce588b1bec58e3
[yaz-moved-to-github.git] / util / oid.c
1 /*
2  * Copyright (c) 1995-2001, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Log: oid.c,v $
6  * Revision 1.45  2001-05-16 07:25:59  adam
7  * Modified oid_ent_to_oid so that if proto is general, then class
8  * is ignored (only oid value is compared).
9  *
10  * Revision 1.44  2000/10/02 13:58:50  adam
11  * Added some OID's.
12  *
13  * Revision 1.43  2000/03/14 09:21:08  ian
14  * Added Admin Extended Service OID
15  *
16  * Revision 1.42  2000/02/29 13:44:55  adam
17  * Check for config.h (currently not generated).
18  *
19  * Revision 1.41  2000/01/10 15:16:53  adam
20  * Added several OID's.
21  *
22  * Revision 1.40  2000/01/06 14:59:13  adam
23  * Added oid_init/oid_exit. Changed oid_exit.
24  *
25  * Revision 1.39  1999/12/16 23:36:19  adam
26  * Implemented ILL protocol. Minor updates ASN.1 compiler.
27  *
28  * Revision 1.38  1999/11/30 13:47:12  adam
29  * Improved installation. Moved header files to include/yaz.
30  *
31  * Revision 1.37  1999/09/13 12:51:15  adam
32  * Added CLIENT IP OID.
33  *
34  * Revision 1.36  1999/05/27 13:02:20  adam
35  * Assigned OID for old DB Update (VAL_DBUPDATE0).
36  *
37  * Revision 1.35  1999/04/20 09:56:49  adam
38  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
39  * Modified all encoders/decoders to reflect this change.
40  *
41  * Revision 1.34  1999/04/15 09:19:43  adam
42  * Added COOKIE UserInfo OID.
43  *
44  * Revision 1.33  1999/04/09 12:16:11  adam
45  * Added OtherInfo private OID proxy.
46  *
47  * Revision 1.32  1999/02/18 10:30:46  quinn
48  * Changed ES: Update OID
49  *
50  * Revision 1.31  1998/12/03 11:33:05  adam
51  * Added OID's for XML.
52  *
53  * Revision 1.30  1998/10/18 07:48:56  adam
54  * Fixed oid_getentbyoid so that it returns NULL when parsed oid is NULL.
55  *
56  * Revision 1.29  1998/10/14 13:32:35  adam
57  * Added include of string.h.
58  *
59  * Revision 1.28  1998/10/13 16:01:53  adam
60  * Implemented support for dynamic object identifiers.
61  * Function oid_getvalbyname now accepts raw OID's as well as traditional
62  * names.
63  *
64  * Revision 1.27  1998/05/18 10:10:02  adam
65  * Added Explain-schema and Explain-tagset to OID database.
66  *
67  * Revision 1.26  1998/03/20 14:46:06  adam
68  * Added UNIverse Resource Reports.
69  *
70  * Revision 1.25  1998/02/10 15:32:03  adam
71  * Added new Object Identifiers.
72  *
73  * Revision 1.24  1997/09/29 13:19:00  adam
74  * Added function, oid_ent_to_oid, to replace the function
75  * oid_getoidbyent, which is not thread safe.
76  *
77  * Revision 1.23  1997/09/09 10:10:19  adam
78  * Another MSV5.0 port. Changed projects to include proper
79  * library/include paths.
80  * Server starts server in test-mode when no options are given.
81  *
82  * Revision 1.22  1997/08/29 13:34:58  quinn
83  * Added thesaurus oids
84  *
85  * Revision 1.21  1997/08/19 08:46:05  quinn
86  * Added Thesaurus OID
87  *
88  * Revision 1.20  1997/07/28 12:34:43  adam
89  * Added new OID entries (RVDM).
90  *
91  * Revision 1.19  1997/05/02 08:39:41  quinn
92  * Support for private OID table added. Thanks to Ronald van der Meer
93  *
94  * Revision 1.18  1997/04/30 08:52:12  quinn
95  * Null
96  *
97  * Revision 1.17  1996/10/10  12:35:23  quinn
98  * Added Update extended service.
99  *
100  * Revision 1.16  1996/10/09  15:55:02  quinn
101  * Added SearchInfoReport
102  *
103  * Revision 1.15  1996/10/07  15:29:43  quinn
104  * Added SOIF support
105  *
106  * Revision 1.14  1996/02/20  17:58:28  adam
107  * Added const to oid_getvalbyname.
108  *
109  * Revision 1.13  1996/02/20  16:37:33  quinn
110  * Using yaz_matchstr in oid_getvalbyname
111  *
112  * Revision 1.12  1996/01/02  08:57:53  quinn
113  * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
114  *
115  * Revision 1.11  1995/12/13  16:03:35  quinn
116  * *** empty log message ***
117  *
118  * Revision 1.10  1995/11/28  09:30:44  quinn
119  * Work.
120  *
121  * Revision 1.9  1995/11/13  09:27:53  quinn
122  * Fiddling with the variant stuff.
123  *
124  * Revision 1.8  1995/10/12  10:34:56  quinn
125  * Added Espec-1.
126  *
127  * Revision 1.7  1995/10/10  16:27:12  quinn
128  * *** empty log message ***
129  *
130  * Revision 1.6  1995/09/29  17:12:35  quinn
131  * Smallish
132  *
133  * Revision 1.5  1995/09/29  17:01:51  quinn
134  * More Windows work
135  *
136  * Revision 1.4  1995/09/27  15:03:03  quinn
137  * Modified function heads & prototypes.
138  *
139  * Revision 1.3  1995/09/12  11:32:06  quinn
140  * Added a looker-upper by name.
141  *
142  * Revision 1.2  1995/08/21  09:11:16  quinn
143  * Smallish fixes to suppport new formats.
144  *
145  * Revision 1.1  1995/05/29  08:17:13  quinn
146  * iMoved oid to util to support comstack.
147  *
148  * Revision 1.5  1995/05/22  11:30:16  quinn
149  * Adding Z39.50-1992 stuff to proto.c. Adding zget.c
150  *
151  * Revision 1.4  1995/05/16  08:50:22  quinn
152  * License, documentation, and memory fixes
153  *
154  * Revision 1.3  1995/04/11  11:52:02  quinn
155  * Fixed possible buf in proto.c
156  *
157  * Revision 1.2  1995/03/29  15:39:38  quinn
158  * Adding some resource control elements, and a null-check to getentbyoid
159  *
160  * Revision 1.1  1995/03/27  08:32:12  quinn
161  * Added OID database
162  *
163  *
164  */
165
166 /*
167  * More or less protocol-transparent OID database.
168  * We could (and should?) extend this so that the user app can add new
169  * entries to the list at initialization.
170  */
171 #if HAVE_CONFIG_H
172 #include <config.h>
173 #endif
174
175 #include <stdlib.h>
176 #include <string.h>
177 #include <ctype.h>
178
179 #include <yaz/oid.h>
180 #include <yaz/yaz-util.h>
181
182 static int z3950_prefix[] = { 1, 2, 840, 10003, -1 };
183 static int sr_prefix[]    = { 1, 0, 10163, -1 };
184
185 struct oident_list {
186     struct oident oident;
187     struct oident_list *next;
188 };
189
190 static struct oident_list *oident_table = NULL;
191 static int oid_value_dynamic = VAL_DYNAMIC;
192 static int oid_init_flag = 0;
193
194 /*
195  * OID database
196  */
197 static oident oids[] =
198 {
199     /* General definitions */
200     {PROTO_GENERAL, CLASS_TRANSYN, VAL_BER,          {2,1,1,-1},
201      "BER" },
202     {PROTO_GENERAL, CLASS_TRANSYN, VAL_ISO2709,      {1,0,2709,1,1,-1},
203      "ISO2709"},
204     {PROTO_GENERAL, CLASS_GENERAL, VAL_ISO_ILL_1,    {1,2,10161,2,1,-1},
205      "ISOILL-1"},
206     /* Z39.50v3 definitions */
207     {PROTO_Z3950,   CLASS_ABSYN,   VAL_APDU,         {2,1,-1},
208      "Z-APDU"},    
209     {PROTO_Z3950,   CLASS_APPCTX,  VAL_BASIC_CTX,    {1,1,-1},
210      "Z-BASIC"},
211     {PROTO_Z3950,   CLASS_ATTSET,  VAL_BIB1,         {3,1,-1},
212      "Bib-1"},
213     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXP1,         {3,2,-1},
214      "Exp-1"},
215     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXT1,         {3,3,-1},
216      "Ext-1"},
217     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CCL1,         {3,4,-1},
218      "CCL-1"},
219     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GILS,         {3,5,-1},
220      "GILS-attset"},
221     {PROTO_Z3950,   CLASS_ATTSET,  VAL_STAS,         {3,6,-1},
222      "STAS-attset"},
223     {PROTO_Z3950,   CLASS_ATTSET,  VAL_COLLECT1,     {3,7,-1},
224      "Collections-attset"},
225     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CIMI1,        {3,8,-1},
226      "CIMI-attset"},
227     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GEO,          {3,9,-1},
228      "Geo-attset"},
229
230     {PROTO_Z3950,   CLASS_ATTSET,  VAL_ZBIG,         {3,10,-1},
231      "ZBIG"},
232     {PROTO_Z3950,   CLASS_ATTSET,  VAL_UTIL,         {3,11,-1},
233      "Util"},
234     {PROTO_Z3950,   CLASS_ATTSET,  VAL_XD1,          {3,12,-1},
235      "XD-1"},
236     {PROTO_Z3950,   CLASS_ATTSET,  VAL_ZTHES,        {3,13,-1},
237      "Zthes"},
238     {PROTO_Z3950,   CLASS_ATTSET,  VAL_FIN1,         {3,14,-1},
239      "Fin-1"},
240     {PROTO_Z3950,   CLASS_ATTSET,  VAL_DAN1,         {3,15,-1},
241      "Dan-1"},
242     {PROTO_Z3950,   CLASS_ATTSET,  VAL_HOLDINGS,     {3,16,-1},
243      "Holdings"},
244
245     {PROTO_Z3950,   CLASS_ATTSET,  VAL_THESAURUS,    {3,1000,81,1,-1},
246      "Thesaurus-attset"},
247     {PROTO_Z3950,   CLASS_DIAGSET, VAL_BIB1,         {4,1,-1},
248      "Bib-1"},
249     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG1,        {4,2,-1},
250      "Diag-1"},
251     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG_ES,      {4,3,-1},
252      "Diag-ES"},
253     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG_GENERAL, {4,3,-1},
254      "Diag-General"},
255     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UNIMARC,      {5,1,-1},
256      "Unimarc"},
257     {PROTO_Z3950,   CLASS_RECSYN,  VAL_INTERMARC,    {5,2,-1},
258      "Intermarc"},
259     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CCF,          {5,3,-1},
260      "CCF"},
261     {PROTO_Z3950,   CLASS_RECSYN,  VAL_USMARC,       {5,10,-1},
262      "USmarc"},
263     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UKMARC,       {5,11,-1},
264      "UKmarc"},
265     {PROTO_Z3950,   CLASS_RECSYN,  VAL_NORMARC,      {5,12,-1},
266      "Normarc"},
267     {PROTO_Z3950,   CLASS_RECSYN,  VAL_LIBRISMARC,   {5,13,-1},
268      "Librismarc"},
269     {PROTO_Z3950,   CLASS_RECSYN,  VAL_DANMARC,      {5,14,-1},
270      "Danmarc"},
271     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FINMARC,      {5,15,-1},
272      "Finmarc"},
273     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MAB,          {5,16,-1},
274      "MAB"},
275     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CANMARC,      {5,17,-1},
276      "Canmarc"},
277     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SBN,          {5,18,-1},
278      "SBN"},
279     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PICAMARC,     {5,19,-1},
280      "Picamarc"},
281     {PROTO_Z3950,   CLASS_RECSYN,  VAL_AUSMARC,      {5,20,-1},
282      "Ausmarc"},
283     {PROTO_Z3950,   CLASS_RECSYN,  VAL_IBERMARC,     {5,21,-1},
284      "Ibermarc"},
285     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CATMARC,      {5,22,-1},
286      "Carmarc"},
287     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MALMARC,      {5,23,-1},
288      "Malmarc"},
289     {PROTO_Z3950,   CLASS_RECSYN,  VAL_JPMARC,       {5,24,-1},
290      "JPmarc"},
291     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SWEMARC,      {5,25,-1},
292      "SWEmarc"},
293     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SIGLEMARC,    {5,26,-1},
294      "SIGLEmarc"},
295     {PROTO_Z3950,   CLASS_RECSYN,  VAL_ISDSMARC,     {5,27,-1},
296      "ISDSmarc"},
297     {PROTO_Z3950,   CLASS_RECSYN,  VAL_RUSMARC,      {5,28,-1},
298      "RUSmarc"},
299     {PROTO_Z3950,   CLASS_RECSYN,  VAL_HUNMARC,      {5,29,-1},
300      "Hunmarc"},
301     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXPLAIN,      {5,100,-1},
302      "Explain"},
303     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUTRS,        {5,101,-1},
304      "SUTRS"},
305     {PROTO_Z3950,   CLASS_RECSYN,  VAL_OPAC,         {5,102,-1},
306      "OPAC"},
307     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUMMARY,      {5,103,-1},
308      "Summary"},
309     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS0,         {5,104,-1},
310      "GRS-0"},
311     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS1,         {5,105,-1},
312      "GRS-1"},
313     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXTENDED,     {5,106,-1},
314      "Extended"},
315     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FRAGMENT,     {5,107,-1},
316      "Fragment"},
317     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PDF,          {5,109,1,-1},
318      "pdf"},
319     {PROTO_Z3950,   CLASS_RECSYN,  VAL_POSTSCRIPT,   {5,109,2,-1},
320      "postscript"},
321     {PROTO_Z3950,   CLASS_RECSYN,  VAL_HTML,         {5,109,3,-1},
322      "html"},
323     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TIFF,         {5,109,4,-1},
324      "tiff"},
325     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GIF,          {5,109,5,-1},
326      "gif"},
327     {PROTO_Z3950,   CLASS_RECSYN,  VAL_JPEG,         {5,109,6,-1},
328      "jpeg"},
329     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PNG,          {5,109,7,-1},
330      "png"},
331     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MPEG,         {5,109,8,-1},
332      "mpeg"},
333     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SGML,         {5,109,9,-1},
334      "sgml"},
335
336     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TIFFB,        {5,110,1,-1},
337      "tiff-b"},
338     {PROTO_Z3950,   CLASS_RECSYN,  VAL_WAV,          {5,110,2,-1},
339      "wav"},
340
341     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SQLRS,        {5,111,-1},
342      "SQL-RS"},
343     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SOIF,         {5,1000,81,2,-1},
344      "SOIF" },
345     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TEXT_XML,     {5,109,10,-1},
346      "text-XML" },
347     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TEXT_XML,     {5,109,10,-1},
348      "XML" },
349     {PROTO_Z3950,   CLASS_RECSYN,  VAL_APPLICATION_XML, {5,109,11,-1},
350      "application-XML" },
351     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE1,    {7,1,-1},
352      "Resource-1"},
353     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE2,    {7,2,-1},
354      "Resource-2"},
355     {PROTO_Z3950,   CLASS_RESFORM, VAL_UNIVERSE_REPORT, {7,1000,81,1,-1},
356      "UNIverse-Resource-Report"},
357
358     {PROTO_Z3950,   CLASS_ACCFORM, VAL_PROMPT1,      {8,1,-1},
359      "Prompt-1"},
360     {PROTO_Z3950,   CLASS_ACCFORM, VAL_DES1,         {8,2,-1},
361      "Des-1"},
362     {PROTO_Z3950,   CLASS_ACCFORM, VAL_KRB1,         {8,3,-1},
363      "Krb-1"},
364     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PRESSET,      {9,1,-1},
365      "Pers. set"},
366     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PQUERY,       {9,2,-1},
367      "Pers. query"},
368     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PCQUERY,      {9,3,-1},
369      "Per'd query"},
370     {PROTO_Z3950,   CLASS_EXTSERV, VAL_ITEMORDER,    {9,4,-1},
371      "Item order"},
372     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE0,    {9,5,1,-1},
373      "DB. Update (old version)"},
374     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE,     {9,5,1,1,-1},
375      "DB. Update"},
376     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTSPEC,   {9,6,-1},
377      "exp. spec."},
378     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTINV,    {9,7,-1},
379      "exp. inv."},
380     {PROTO_Z3950,   CLASS_EXTSERV, VAL_ADMINSERVICE, {9,81,1,-1},
381      "Admin"},
382     {PROTO_Z3950,   CLASS_USERINFO,VAL_SEARCHRES1,   {10,1,-1},
383      "searchResult-1"},
384     {PROTO_Z3950,   CLASS_USERINFO,VAL_CHARLANG,     {10,2,-1},
385      "CharSetandLanguageNegotiation"},
386     {PROTO_Z3950,   CLASS_USERINFO,VAL_USERINFO1,    {10,3,-1},
387      "UserInfo-1"},
388     {PROTO_Z3950,   CLASS_USERINFO,VAL_MULTISRCH1,   {10,4,-1},
389      "MultipleSearchTerms-1"},
390     {PROTO_Z3950,   CLASS_USERINFO,VAL_MULTISRCH2,   {10,5,-1},
391      "MultipleSearchTerms-2"},
392     {PROTO_Z3950,   CLASS_USERINFO,VAL_DATETIME,     {10,6,-1},
393      "DateTime"},
394     {PROTO_Z3950,   CLASS_USERINFO,VAL_PROXY,        {10,1000,81,1,-1},
395      "Proxy" },
396     {PROTO_Z3950,   CLASS_USERINFO,VAL_COOKIE,       {10,1000,81,2,-1},
397      "Cookie" },
398     {PROTO_Z3950,   CLASS_USERINFO,VAL_CLIENT_IP,    {10,1000,81,3,-1},
399      "Client-IP" },
400     {PROTO_Z3950,   CLASS_ELEMSPEC,VAL_ESPEC1,       {11,1,-1},
401      "Espec-1"},
402     {PROTO_Z3950,   CLASS_VARSET,  VAL_VAR1,         {12,1,-1},
403      "Variant-1"},
404     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_WAIS,         {13,1,-1},
405      "WAIS-schema"},
406     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_GILS,         {13,2,-1},
407      "GILS-schema"},
408     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_COLLECT1,     {13,3,-1},
409      "Collections-schema"},
410     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_GEO,          {13,4,-1},
411      "Geo-schema"},
412     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_CIMI1,        {13,5,-1},
413      "CIMI-schema"},
414     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_UPDATEES,     {13,6,-1},
415      "Update ES"},
416     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_HOLDINGS,     {13,7,-1},
417      "Holdings"},
418     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_ZTHES,        {13,8,-1},
419      "Zthes"},
420     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_THESAURUS,    {13,1000,81,1,-1},
421      "thesaurus-schema"},
422     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_EXPLAIN,      {13,1000,81,2,-1},
423      "Explain-schema"},
424     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETM,         {14,1,-1},
425      "TagsetM"},
426     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETG,         {14,2,-1},
427      "TagsetG"},
428     {PROTO_Z3950,   CLASS_TAGSET,  VAL_STAS,         {14,3,-1},
429      "STAS-tagset"},
430     {PROTO_Z3950,   CLASS_TAGSET,  VAL_GILS,         {14,4,-1},
431      "GILS-tagset"},
432     {PROTO_Z3950,   CLASS_TAGSET,  VAL_COLLECT1,     {14,5,-1},
433      "Collections-tagset"},
434     {PROTO_Z3950,   CLASS_TAGSET,  VAL_CIMI1,        {14,6,-1},
435      "CIMI-tagset"},
436     {PROTO_Z3950,   CLASS_TAGSET,  VAL_THESAURUS,    {14,1000,81,1,-1},
437      "thesaurus-tagset"},
438     {PROTO_Z3950,   CLASS_TAGSET,  VAL_EXPLAIN,      {14,1000,81,2,-1},
439      "Explain-tagset"},
440     
441
442     /* SR definitions. Note that some of them aren't defined by the
443         standard (yet), but are borrowed from Z3950v3 */
444     {PROTO_SR,      CLASS_ABSYN,   VAL_APDU,      {2,1,-1},    "SR-APDU"     },
445
446     {PROTO_SR,      CLASS_APPCTX,  VAL_BASIC_CTX, {1,1,-1},    "SR-BASIC"    },
447
448     {PROTO_SR,      CLASS_ATTSET,  VAL_BIB1,      {3,1,-1},    "Bib-1"       },
449     {PROTO_SR,      CLASS_ATTSET,  VAL_EXP1,      {3,2,-1},    "Exp-1"       },
450     {PROTO_SR,      CLASS_ATTSET,  VAL_EXT1,      {3,3,-1},    "Ext-1"       },
451     {PROTO_SR,      CLASS_ATTSET,  VAL_CCL1,      {3,4,-1},    "CCL-1"       },
452     {PROTO_SR,      CLASS_ATTSET,  VAL_GILS,      {3,5,-1},    "GILS"        },
453     {PROTO_SR,      CLASS_ATTSET,  VAL_STAS,      {3,6,-1},    "STAS",       },
454     {PROTO_SR,      CLASS_ATTSET,  VAL_COLLECT1,  {3,7,-1},    "Collections-attset"},
455     {PROTO_SR,      CLASS_ATTSET,  VAL_CIMI1,     {3,8,-1},    "CIMI-attset"},
456     {PROTO_SR,      CLASS_ATTSET,  VAL_GEO,       {3,9,-1},    "Geo-attset"},
457
458     {PROTO_SR,      CLASS_DIAGSET, VAL_BIB1,      {4,1,-1},    "Bib-1"       },
459     {PROTO_SR,      CLASS_DIAGSET, VAL_DIAG1,     {4,2,-1},    "Diag-1"      },
460
461     {PROTO_SR,      CLASS_RECSYN,  VAL_UNIMARC,   {5,1,-1},    "Unimarc"     },
462     {PROTO_SR,      CLASS_RECSYN,  VAL_INTERMARC, {5,2,-1},    "Intermarc"   },
463     {PROTO_SR,      CLASS_RECSYN,  VAL_CCF,       {5,3,-1},    "CCF"        },
464     {PROTO_SR,      CLASS_RECSYN,  VAL_USMARC,    {5,10,-1},   "USmarc"      },
465     {PROTO_SR,      CLASS_RECSYN,  VAL_UKMARC,    {5,11,-1},   "UKmarc"      },
466     {PROTO_SR,      CLASS_RECSYN,  VAL_NORMARC,   {5,12,-1},   "Normarc"     },
467     {PROTO_SR,      CLASS_RECSYN,  VAL_LIBRISMARC,{5,13,-1},   "Librismarc"  },
468     {PROTO_SR,      CLASS_RECSYN,  VAL_DANMARC,   {5,14,-1},   "Danmarc"     },
469     {PROTO_SR,      CLASS_RECSYN,  VAL_FINMARC,   {5,15,-1},   "Finmarc"     },
470     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
471     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
472     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
473     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
474     {PROTO_SR,      CLASS_RECSYN,  VAL_SBN,       {5,18,-1},   "SBN"         },
475     {PROTO_SR,      CLASS_RECSYN,  VAL_PICAMARC,  {5,19,-1},   "Picamarc"    },
476     {PROTO_SR,      CLASS_RECSYN,  VAL_AUSMARC,   {5,20,-1},   "Ausmarc"     },
477     {PROTO_SR,      CLASS_RECSYN,  VAL_IBERMARC,  {5,21,-1},   "Ibermarc"    },
478     {PROTO_SR,      CLASS_RECSYN,  VAL_CATMARC,   {5,22,-1},   "Catmarc"     },
479     {PROTO_SR,      CLASS_RECSYN,  VAL_MALMARC,   {5,23,-1},   "Malmarc"     },
480     {PROTO_SR,      CLASS_RECSYN,  VAL_EXPLAIN,   {5,100,-1},  "Explain"     },
481     {PROTO_SR,      CLASS_RECSYN,  VAL_SUTRS,     {5,101,-1},  "SUTRS"       },
482     {PROTO_SR,      CLASS_RECSYN,  VAL_OPAC,      {5,102,-1},  "OPAC"        },
483     {PROTO_SR,      CLASS_RECSYN,  VAL_SUMMARY,   {5,103,-1},  "Summary"     },
484     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS0,      {5,104,-1},  "GRS-0"       },
485     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS1,      {5,105,-1},  "GRS-1"       },
486     {PROTO_SR,      CLASS_RECSYN,  VAL_EXTENDED,  {5,106,-1},  "Extended"    },
487     {PROTO_SR,      CLASS_RECSYN,  VAL_FRAGMENT,  {5,107,-1},  "Fragment"    },
488
489     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},    "Resource-1"  },
490     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},    "Resource-2"  },
491
492     {PROTO_SR,      CLASS_ACCFORM, VAL_PROMPT1,   {8,1,-1},    "Prompt-1"    },
493     {PROTO_SR,      CLASS_ACCFORM, VAL_DES1,      {8,2,-1},    "Des-1"       },
494     {PROTO_SR,      CLASS_ACCFORM, VAL_KRB1,      {8,3,-1},    "Krb-1"       },
495
496     {PROTO_SR,      CLASS_EXTSERV, VAL_PRESSET,   {9,1,-1},    "Pers. set"   },
497     {PROTO_SR,      CLASS_EXTSERV, VAL_PQUERY,    {9,2,-1},    "Pers. query" },
498     {PROTO_SR,      CLASS_EXTSERV, VAL_PCQUERY,   {9,3,-1},    "Per'd query" },
499     {PROTO_SR,      CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},    "Item order"  },
500     {PROTO_SR,      CLASS_EXTSERV, VAL_DBUPDATE,  {9,5,-1},    "DB. Update"  },
501     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTSPEC,{9,6,-1},    "exp. spec."  },
502     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},    "exp. inv."   },
503
504     {PROTO_SR,      CLASS_ELEMSPEC,VAL_ESPEC1,    {11,1,-1},   "Espec-1"     },
505
506     {PROTO_SR,      CLASS_VARSET,  VAL_VAR1,      {12,1,-1},   "Variant-1"   },
507
508     {PROTO_SR,      CLASS_SCHEMA,  VAL_WAIS,      {13,1,-1},   "WAIS-schema" },
509     {PROTO_SR,      CLASS_SCHEMA,  VAL_GILS,      {13,2,-1},   "GILS-schema" },
510     {PROTO_SR,      CLASS_SCHEMA,  VAL_COLLECT1,  {13,3,-1},   "Collections-schema" },
511     {PROTO_SR,      CLASS_SCHEMA,  VAL_GEO,       {13,4,-1},   "Geo-schema" },
512     {PROTO_SR,      CLASS_SCHEMA,  VAL_CIMI1,     {13,5,-1},   "CIMI-schema" },
513
514     {PROTO_SR,      CLASS_TAGSET,  VAL_SETM,      {14,1,-1},   "TagsetM"     },
515     {PROTO_SR,      CLASS_TAGSET,  VAL_SETG,      {14,2,-1},   "TagsetG"     },
516
517     {PROTO_SR,      CLASS_TAGSET,  VAL_STAS,      {14,3,-1},   "STAS-tagset" },
518     {PROTO_SR,      CLASS_TAGSET,  VAL_GILS,      {14,4,-1},   "GILS-tagset" },
519     {PROTO_SR,      CLASS_TAGSET,  VAL_COLLECT1,  {14,5,-1},   "Collections-tagset"},
520     {PROTO_SR,      CLASS_TAGSET,  VAL_CIMI1,     {14,6,-1},   "CIMI-tagset" },
521
522     {PROTO_NOP,     CLASS_NOP,     VAL_NOP,       {-1},        0          }
523 };
524
525 /* OID utilities */
526
527 void oid_oidcpy(int *t, int *s)
528 {
529     while ((*(t++) = *(s++)) > -1);
530 }
531
532 void oid_oidcat(int *t, int *s)
533 {
534     while (*t > -1)
535         t++;
536     while ((*(t++) = *(s++)) > -1);
537 }
538
539 int oid_oidcmp(int *o1, int *o2)
540 {
541     while (*o1 == *o2 && *o1 > -1)
542     {
543         o1++;
544         o2++;
545     }
546     if (*o1 == *o2)
547         return 0;
548     else if (*o1 > *o2)
549         return 1;
550     else
551         return -1;
552 }
553
554 int oid_oidlen(int *o)
555 {
556     int len = 0;
557
558     while (*(o++) >= 0)
559         len++;
560     return len;
561 }
562
563
564 static int match_prefix(int *look, int *prefix)
565 {
566     int len;
567
568     for (len = 0; *look == *prefix; look++, prefix++, len++);
569     if (*prefix < 0) /* did we reach the end of the prefix? */
570         return len;
571     return 0;
572 }
573
574 void oid_transfer (struct oident *oident)
575 {
576     while (*oident->oidsuffix >= 0)
577     {
578         oid_addent (oident->oidsuffix, oident->proto,
579                     oident->oclass,
580                     oident->desc, oident->value);
581         oident++;
582     }
583 }
584
585 void oid_init (void)
586 {
587     if (oid_init_flag)
588         return;
589     /* oid_transfer is thread safe, so there's nothing wrong in having
590        two threads calling it simultaniously. On the other hand
591        no thread may exit oid_init before all OID's bave been
592        transferred - which is why checked is set after oid_transfer... 
593     */
594     oid_transfer (oids);
595     oid_init_flag = 1;
596 }
597
598 void oid_exit (void)
599 {
600     while (oident_table)
601     {
602         struct oident_list *this_p = oident_table;
603         oident_table = oident_table->next;
604
605         xfree (this_p->oident.desc);
606         xfree (this_p);
607     }
608     oid_init_flag = 0;
609 }
610
611 static struct oident *oid_getentbyoid_x(int *o)
612 {
613     enum oid_proto proto;
614     int prelen;
615     struct oident_list *ol;
616     
617     /* determine protocol type */
618     if ((prelen = match_prefix(o, z3950_prefix)) != 0)
619         proto = PROTO_Z3950;
620     else if ((prelen = match_prefix(o, sr_prefix)) != 0)
621         proto = PROTO_SR;
622     else
623         proto = PROTO_GENERAL;
624     for (ol = oident_table; ol; ol = ol->next)
625     {
626         struct oident *p = &ol->oident;
627         if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
628             return p;
629         if (p->proto == PROTO_GENERAL && !oid_oidcmp (o, p->oidsuffix))
630             return p;
631     }
632     return 0;
633 }
634
635 /*
636  * To query, fill out proto, class, and value of the ent parameter.
637  */
638 int *oid_ent_to_oid(struct oident *ent, int *ret)
639 {
640     struct oident_list *ol;
641     
642     oid_init ();
643     for (ol = oident_table; ol; ol = ol->next)
644     {
645         struct oident *p = &ol->oident;
646         if (ent->value == p->value &&
647             (p->proto == PROTO_GENERAL || (ent->proto == p->proto &&  
648             (ent->oclass == p->oclass || ent->oclass == CLASS_GENERAL))))
649         {
650             if (p->proto == PROTO_Z3950)
651                 oid_oidcpy(ret, z3950_prefix);
652             else if (p->proto == PROTO_SR)
653                 oid_oidcpy(ret, sr_prefix);
654             else
655                 ret[0] = -1;
656             oid_oidcat(ret, p->oidsuffix);
657             ent->desc = p->desc;
658             return ret;
659         }
660     }
661     ret[0] = -1;
662     return 0;
663 }
664
665 /*
666  * To query, fill out proto, class, and value of the ent parameter.
667  */
668 int *oid_getoidbyent(struct oident *ent)
669 {
670     static int ret[OID_SIZE];
671
672     return oid_ent_to_oid (ent, ret);
673 }
674
675 struct oident *oid_addent (int *oid, enum oid_proto proto,
676                            enum oid_class oclass,
677                            const char *desc, int value)
678 {
679     struct oident *oident;
680
681     nmem_critical_enter ();
682     oident = oid_getentbyoid_x (oid);
683     if (!oident)
684     {
685         char desc_str[200];
686         struct oident_list *oident_list;
687         oident_list = (struct oident_list *) xmalloc (sizeof(*oident_list));
688         oident = &oident_list->oident;
689         oident->proto = proto;
690         oident->oclass = oclass;
691
692         if (!desc)
693         {
694             int i;
695
696             sprintf (desc_str, "%d", *oid);
697             for (i = 1; oid[i] >= 0; i++)
698                 sprintf (desc_str+strlen(desc_str), ".%d", oid[i]);
699             desc = desc_str;
700         }
701         oident->desc = (char *) xmalloc (strlen(desc)+1);
702         strcpy (oident->desc, desc);
703         if (value == VAL_DYNAMIC)
704             oident->value = (enum oid_value) (++oid_value_dynamic);
705         else
706             oident->value = (enum oid_value) value;
707         oid_oidcpy (oident->oidsuffix, oid);
708         oident_list->next = oident_table;
709         oident_table = oident_list;
710     }
711     nmem_critical_leave ();
712     return oident;
713 }
714
715 struct oident *oid_getentbyoid(int *oid)
716 {
717     struct oident *oident;
718
719     if (!oid)
720         return 0;
721     oid_init ();
722     oident = oid_getentbyoid_x (oid);
723     if (!oident)
724         oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL,
725                              NULL, VAL_DYNAMIC);
726     return oident;
727 }
728
729 static oid_value oid_getval_raw(const char *name)
730 {
731     int val = 0, i = 0, oid[OID_SIZE];
732     struct oident *oident;
733     
734     while (isdigit (*name))
735     {
736         val = val*10 + (*name - '0');
737         name++;
738         if (*name == '.')
739         {
740             if (i < OID_SIZE-1)
741                 oid[i++] = val;
742             val = 0;
743             name++;
744         }
745     }
746     oid[i] = val;
747     oid[i+1] = -1;
748     oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL, NULL,
749                          VAL_DYNAMIC);
750     return oident->value;
751 }
752
753 oid_value oid_getvalbyname(const char *name)
754 {
755     struct oident_list *ol;
756
757     oid_init ();
758     if (isdigit (*name))
759         return oid_getval_raw (name);
760     for (ol = oident_table; ol; ol = ol->next)
761         if (!yaz_matchstr(ol->oident.desc, name))
762         {
763             return ol->oident.value;
764         }
765     return VAL_NONE;
766 }
767
768 void oid_setprivateoids(oident *list)
769 {
770     oid_transfer (list);
771 }