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