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