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