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