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