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