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