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