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