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