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