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