ed33f0246a9bafdecfb56f28cd3569aec6060c40
[yaz-moved-to-github.git] / src / oid.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: oid.c,v 1.3 2003-11-19 22:24:57 adam Exp $
6  */
7
8 /*
9  * More or less protocol-transparent OID database.
10  * We could (and should?) extend this so that the user app can add new
11  * entries to the list at initialization.
12  */
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 #include <yaz/oid.h>
22 #include <yaz/yaz-util.h>
23
24 static int z3950_prefix[] = { 1, 2, 840, 10003, -1 };
25 static int sr_prefix[]    = { 1, 0, 10163, -1 };
26
27 struct oident_list {
28     struct oident oident;
29     struct oident_list *next;
30 };
31
32 static struct oident_list *oident_table = NULL;
33 static int oid_value_dynamic = VAL_DYNAMIC;
34 static int oid_init_flag = 0;
35 static NMEM_MUTEX oid_mutex = 0;
36 static NMEM oid_nmem = 0;
37
38 /*
39  * OID database
40  */
41 static oident standard_oids[] =
42 {
43     /* General definitions */
44     {PROTO_GENERAL, CLASS_TRANSYN, VAL_BER,          {2,1,1,-1},
45      "BER" },
46     {PROTO_GENERAL, CLASS_TRANSYN, VAL_ISO2709,      {1,0,2709,1,1,-1},
47      "ISO2709"},
48     {PROTO_GENERAL, CLASS_GENERAL, VAL_ISO_ILL_1,    {1,0,10161,2,1,-1},
49      "ISOILL-1"},
50     /* Z39.50v3 definitions */
51     {PROTO_Z3950,   CLASS_ABSYN,   VAL_APDU,         {2,1,-1},
52      "Z-APDU"},    
53     {PROTO_Z3950,   CLASS_APPCTX,  VAL_BASIC_CTX,    {1,1,-1},
54      "Z-BASIC"},
55     {PROTO_Z3950,   CLASS_ATTSET,  VAL_BIB1,         {3,1,-1},
56      "Bib-1"},
57     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXP1,         {3,2,-1},
58      "Exp-1"},
59     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXT1,         {3,3,-1},
60      "Ext-1"},
61     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CCL1,         {3,4,-1},
62      "CCL-1"},
63     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GILS,         {3,5,-1},
64      "GILS-attset"},
65     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GILS,         {3,5,-1},
66      "GILS"},
67     {PROTO_Z3950,   CLASS_ATTSET,  VAL_STAS,         {3,6,-1},
68      "STAS-attset"},
69     {PROTO_Z3950,   CLASS_ATTSET,  VAL_COLLECT1,     {3,7,-1},
70      "Collections-attset"},
71     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CIMI1,        {3,8,-1},
72      "CIMI-attset"},
73     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GEO,          {3,9,-1},
74      "Geo-attset"},
75
76     {PROTO_Z3950,   CLASS_ATTSET,  VAL_ZBIG,         {3,10,-1},
77      "ZBIG"},
78     {PROTO_Z3950,   CLASS_ATTSET,  VAL_UTIL,         {3,11,-1},
79      "Util"},
80     {PROTO_Z3950,   CLASS_ATTSET,  VAL_XD1,          {3,12,-1},
81      "XD-1"},
82     {PROTO_Z3950,   CLASS_ATTSET,  VAL_ZTHES,        {3,13,-1},
83      "Zthes"},
84     {PROTO_Z3950,   CLASS_ATTSET,  VAL_FIN1,         {3,14,-1},
85      "Fin-1"},
86     {PROTO_Z3950,   CLASS_ATTSET,  VAL_DAN1,         {3,15,-1},
87      "Dan-1"},
88     {PROTO_Z3950,   CLASS_ATTSET,  VAL_HOLDINGS,     {3,16,-1},
89      "Holdings"},
90     {PROTO_Z3950,   CLASS_ATTSET,  VAL_USMARC,       {3,17,-1},
91      "MARC"},
92     {PROTO_Z3950,   CLASS_ATTSET,  VAL_BIB2,         {3,18,-1},
93      "Bib-2"},
94     {PROTO_Z3950,   CLASS_ATTSET,  VAL_ZEEREX,       {3,19,-1},
95      "ZeeRex"},
96     /* New applications should use Zthes-1 instead of this Satan-spawn */
97     {PROTO_Z3950,   CLASS_ATTSET,  VAL_THESAURUS,    {3,1000,81,1,-1},
98      "Thesaurus-attset"},
99     {PROTO_Z3950,   CLASS_ATTSET,  VAL_IDXPATH,      {3,1000,81,2,-1},
100      "IDXPATH"},
101     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXTLITE,      {3,1000,81,3,-1},
102      "EXTLITE"},
103     {PROTO_Z3950,   CLASS_DIAGSET, VAL_BIB1,         {4,1,-1},
104      "Bib-1"},
105     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG1,        {4,2,-1},
106      "Diag-1"},
107     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG_ES,      {4,3,-1},
108      "Diag-ES"},
109     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG_GENERAL, {4,3,-1},
110      "Diag-General"},
111     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UNIMARC,      {5,1,-1},
112      "Unimarc"},
113     {PROTO_Z3950,   CLASS_RECSYN,  VAL_INTERMARC,    {5,2,-1},
114      "Intermarc"},
115     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CCF,          {5,3,-1},
116      "CCF"},
117     /* MARC21 is just an alias for the original USmarc */
118     {PROTO_Z3950,   CLASS_RECSYN,  VAL_USMARC,       {5,10,-1},
119      "MARC21"},
120     {PROTO_Z3950,   CLASS_RECSYN,  VAL_USMARC,       {5,10,-1},
121      "USmarc"},
122     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UKMARC,       {5,11,-1},
123      "UKmarc"},
124     {PROTO_Z3950,   CLASS_RECSYN,  VAL_NORMARC,      {5,12,-1},
125      "Normarc"},
126     {PROTO_Z3950,   CLASS_RECSYN,  VAL_LIBRISMARC,   {5,13,-1},
127      "Librismarc"},
128     {PROTO_Z3950,   CLASS_RECSYN,  VAL_DANMARC,      {5,14,-1},
129      "Danmarc"},
130     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FINMARC,      {5,15,-1},
131      "Finmarc"},
132     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MAB,          {5,16,-1},
133      "MAB"},
134     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CANMARC,      {5,17,-1},
135      "Canmarc"},
136     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SBN,          {5,18,-1},
137      "SBN"},
138     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PICAMARC,     {5,19,-1},
139      "Picamarc"},
140     {PROTO_Z3950,   CLASS_RECSYN,  VAL_AUSMARC,      {5,20,-1},
141      "Ausmarc"},
142     {PROTO_Z3950,   CLASS_RECSYN,  VAL_IBERMARC,     {5,21,-1},
143      "Ibermarc"},
144     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CATMARC,      {5,22,-1},
145      "Carmarc"},
146     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MALMARC,      {5,23,-1},
147      "Malmarc"},
148     {PROTO_Z3950,   CLASS_RECSYN,  VAL_JPMARC,       {5,24,-1},
149      "JPmarc"},
150     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SWEMARC,      {5,25,-1},
151      "SWEmarc"},
152     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SIGLEMARC,    {5,26,-1},
153      "SIGLEmarc"},
154     {PROTO_Z3950,   CLASS_RECSYN,  VAL_ISDSMARC,     {5,27,-1},
155      "ISDSmarc"},
156     {PROTO_Z3950,   CLASS_RECSYN,  VAL_RUSMARC,      {5,28,-1},
157      "RUSmarc"},
158     {PROTO_Z3950,   CLASS_RECSYN,  VAL_HUNMARC,      {5,29,-1},
159      "Hunmarc"},
160     {PROTO_Z3950,   CLASS_RECSYN,  VAL_NACSISCATP,   {5,30,-1},
161      "NACSIS-CATP"},
162     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FINMARC2000,  {5,31,-1},
163      "FINMARC2000"},
164     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MARC21FIN,    {5,32,-1},
165      "MARC21-fin"},
166     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXPLAIN,      {5,100,-1},
167      "Explain"},
168     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUTRS,        {5,101,-1},
169      "SUTRS"},
170     {PROTO_Z3950,   CLASS_RECSYN,  VAL_OPAC,         {5,102,-1},
171      "OPAC"},
172     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUMMARY,      {5,103,-1},
173      "Summary"},
174     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS0,         {5,104,-1},
175      "GRS-0"},
176     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS1,         {5,105,-1},
177      "GRS-1"},
178     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXTENDED,     {5,106,-1},
179      "Extended"},
180     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FRAGMENT,     {5,107,-1},
181      "Fragment"},
182     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PDF,          {5,109,1,-1},
183      "pdf"},
184     {PROTO_Z3950,   CLASS_RECSYN,  VAL_POSTSCRIPT,   {5,109,2,-1},
185      "postscript"},
186     {PROTO_Z3950,   CLASS_RECSYN,  VAL_HTML,         {5,109,3,-1},
187      "html"},
188     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TIFF,         {5,109,4,-1},
189      "tiff"},
190     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GIF,          {5,109,5,-1},
191      "gif"},
192     {PROTO_Z3950,   CLASS_RECSYN,  VAL_JPEG,         {5,109,6,-1},
193      "jpeg"},
194     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PNG,          {5,109,7,-1},
195      "png"},
196     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MPEG,         {5,109,8,-1},
197      "mpeg"},
198     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SGML,         {5,109,9,-1},
199      "sgml"},
200
201     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TIFFB,        {5,110,1,-1},
202      "tiff-b"},
203     {PROTO_Z3950,   CLASS_RECSYN,  VAL_WAV,          {5,110,2,-1},
204      "wav"},
205
206     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SQLRS,        {5,111,-1},
207      "SQL-RS"},
208     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SOIF,         {5,1000,81,2,-1},
209      "SOIF" },
210     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TEXT_XML,     {5,109,10,-1},
211      "text-XML" },
212     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TEXT_XML,     {5,109,10,-1},
213      "XML" },
214     {PROTO_Z3950,   CLASS_RECSYN,  VAL_APPLICATION_XML, {5,109,11,-1},
215      "application-XML" },
216     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE1,    {7,1,-1},
217      "Resource-1"},
218     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE2,    {7,2,-1},
219      "Resource-2"},
220     {PROTO_Z3950,   CLASS_RESFORM, VAL_UNIVERSE_REPORT, {7,1000,81,1,-1},
221      "UNIverse-Resource-Report"},
222
223     {PROTO_Z3950,   CLASS_ACCFORM, VAL_PROMPT1,      {8,1,-1},
224      "Prompt-1"},
225     {PROTO_Z3950,   CLASS_ACCFORM, VAL_DES1,         {8,2,-1},
226      "Des-1"},
227     {PROTO_Z3950,   CLASS_ACCFORM, VAL_KRB1,         {8,3,-1},
228      "Krb-1"},
229     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PRESSET,      {9,1,-1},
230      "Pers. set"},
231     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PQUERY,       {9,2,-1},
232      "Pers. query"},
233     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PCQUERY,      {9,3,-1},
234      "Per'd query"},
235     {PROTO_Z3950,   CLASS_EXTSERV, VAL_ITEMORDER,    {9,4,-1},
236      "Item order"},
237     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE0,    {9,5,-1},
238      "DB. Update (first version)"},
239     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE1,    {9,5,1,-1},
240      "DB. Update (second version)"},
241     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE,     {9,5,1,1,-1},
242      "DB. Update"},
243     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTSPEC,   {9,6,-1},
244      "exp. spec."},
245     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTINV,    {9,7,-1},
246      "exp. inv."},
247     {PROTO_Z3950,   CLASS_EXTSERV, VAL_ADMINSERVICE, {9,1000,81,1,-1},
248      "Admin"},
249     {PROTO_Z3950,   CLASS_USERINFO,VAL_SEARCHRES1,   {10,1,-1},
250      "searchResult-1"},
251     {PROTO_Z3950,   CLASS_USERINFO,VAL_CHARLANG,     {10,2,-1},
252      "CharSetandLanguageNegotiation"},
253     {PROTO_Z3950,   CLASS_USERINFO,VAL_USERINFO1,    {10,3,-1},
254      "UserInfo-1"},
255     {PROTO_Z3950,   CLASS_USERINFO,VAL_MULTISRCH1,   {10,4,-1},
256      "MultipleSearchTerms-1"},
257     {PROTO_Z3950,   CLASS_USERINFO,VAL_MULTISRCH2,   {10,5,-1},
258      "MultipleSearchTerms-2"},
259     {PROTO_Z3950,   CLASS_USERINFO,VAL_DATETIME,     {10,6,-1},
260      "DateTime"},
261     {PROTO_Z3950,   CLASS_USERINFO,VAL_PROXY,        {10,1000,81,1,-1},
262      "Proxy" },
263     {PROTO_Z3950,   CLASS_USERINFO,VAL_COOKIE,       {10,1000,81,2,-1},
264      "Cookie" },
265     {PROTO_Z3950,   CLASS_USERINFO,VAL_CLIENT_IP,    {10,1000,81,3,-1},
266      "Client-IP" },
267     {PROTO_Z3950,   CLASS_ELEMSPEC,VAL_ESPEC1,       {11,1,-1},
268      "Espec-1"},
269     {PROTO_Z3950,   CLASS_VARSET,  VAL_VAR1,         {12,1,-1},
270      "Variant-1"},
271     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_WAIS,         {13,1,-1},
272      "WAIS-schema"},
273     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_GILS,         {13,2,-1},
274      "GILS-schema"},
275     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_COLLECT1,     {13,3,-1},
276      "Collections-schema"},
277     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_GEO,          {13,4,-1},
278      "Geo-schema"},
279     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_CIMI1,        {13,5,-1},
280      "CIMI-schema"},
281     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_UPDATEES,     {13,6,-1},
282      "Update ES"},
283     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_HOLDINGS,     {13,7,-1},
284      "Holdings"},
285     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_ZTHES,        {13,8,-1},
286      "Zthes"},
287     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_THESAURUS,    {13,1000,81,1,-1},
288      "thesaurus-schema"},
289     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_EXPLAIN,      {13,1000,81,2,-1},
290      "Explain-schema"},
291     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETM,         {14,1,-1},
292      "TagsetM"},
293     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETG,         {14,2,-1},
294      "TagsetG"},
295     {PROTO_Z3950,   CLASS_TAGSET,  VAL_STAS,         {14,3,-1},
296      "STAS-tagset"},
297     {PROTO_Z3950,   CLASS_TAGSET,  VAL_GILS,         {14,4,-1},
298      "GILS-tagset"},
299     {PROTO_Z3950,   CLASS_TAGSET,  VAL_COLLECT1,     {14,5,-1},
300      "Collections-tagset"},
301     {PROTO_Z3950,   CLASS_TAGSET,  VAL_CIMI1,        {14,6,-1},
302      "CIMI-tagset"},
303     {PROTO_Z3950,   CLASS_TAGSET,  VAL_THESAURUS,    {14,1000,81,1,-1},
304      "thesaurus-tagset"},       /* What is this Satan-spawn doing here? */
305     {PROTO_Z3950,   CLASS_TAGSET,  VAL_EXPLAIN,      {14,1000,81,2,-1},
306      "Explain-tagset"},
307     {PROTO_Z3950,   CLASS_TAGSET,  VAL_ZTHES,        {14,8,-1},
308      "Zthes-tagset"},
309     {PROTO_Z3950,   CLASS_NEGOT,   VAL_CHARNEG3,     {15,3,-1},
310      "CharSetandLanguageNegotiation-3"},
311     {PROTO_Z3950,   CLASS_NEGOT,   VAL_ID_CHARSET,   {15,1000,81,1,-1},
312      "ID-Charset" },
313     {PROTO_Z3950,   CLASS_USERINFO,VAL_CQL,          {16, 2, -1},
314      "CQL"},
315     {PROTO_GENERAL, CLASS_GENERAL, VAL_UCS2,    {1,0,10646,1,0,2,-1},
316      "UCS-2"},
317     {PROTO_GENERAL, CLASS_GENERAL, VAL_UCS4,    {1,0,10646,1,0,4,-1},
318      "UCS-4"},
319     {PROTO_GENERAL, CLASS_GENERAL, VAL_UTF16,   {1,0,10646,1,0,5,-1},
320      "UTF-16"},
321     {PROTO_GENERAL, CLASS_GENERAL, VAL_UTF8,    {1,0,10646,1,0,8,-1},
322      "UTF-8"},
323     {PROTO_Z3950,   CLASS_USERINFO,VAL_OCLCUI,  {10, 1000, 17, 1, -1},
324      "OCLC-userInfo"},
325     {PROTO_NOP,     CLASS_NOP,     VAL_NOP,       {-1},        0          }
326 };
327
328 /* OID utilities */
329
330 void oid_oidcpy(int *t, int *s)
331 {
332     while ((*(t++) = *(s++)) > -1);
333 }
334
335 void oid_oidcat(int *t, int *s)
336 {
337     while (*t > -1)
338         t++;
339     while ((*(t++) = *(s++)) > -1);
340 }
341
342 int oid_oidcmp(int *o1, int *o2)
343 {
344     while (*o1 == *o2 && *o1 > -1)
345     {
346         o1++;
347         o2++;
348     }
349     if (*o1 == *o2)
350         return 0;
351     else if (*o1 > *o2)
352         return 1;
353     else
354         return -1;
355 }
356
357 int oid_oidlen(int *o)
358 {
359     int len = 0;
360
361     while (*(o++) >= 0)
362         len++;
363     return len;
364 }
365
366
367 static int match_prefix(int *look, int *prefix)
368 {
369     int len;
370
371     for (len = 0; *look == *prefix; look++, prefix++, len++);
372     if (*prefix < 0) /* did we reach the end of the prefix? */
373         return len;
374     return 0;
375 }
376
377 void oid_transfer (struct oident *oidentp)
378 {
379     while (*oidentp->oidsuffix >= 0)
380     {
381         oid_addent (oidentp->oidsuffix, oidentp->proto,
382                     oidentp->oclass,
383                     oidentp->desc, oidentp->value);
384         oidentp++;
385     }
386 }
387
388 void oid_init (void)
389 {
390     if (oid_init_flag == 0)
391     {
392         /* oid_transfer is thread safe, so there's nothing wrong in having
393            two threads calling it simultaniously. On the other hand
394            no thread may exit oid_init before all OID's bave been
395            transferred - which is why checked is set after oid_transfer... 
396         */
397         nmem_mutex_create (&oid_mutex);
398         nmem_mutex_enter (oid_mutex);
399         if (!oid_nmem)
400             oid_nmem = nmem_create ();
401         nmem_mutex_leave (oid_mutex);
402         oid_transfer (standard_oids);
403         oid_init_flag = 1;
404     }
405 }
406
407 void oid_exit (void)
408 {
409     if (oid_init_flag)
410     {
411         oid_init_flag = 0;
412         nmem_mutex_destroy (&oid_mutex);
413         nmem_destroy (oid_nmem);
414         oid_nmem = 0;
415     }
416 }
417
418 static struct oident *oid_getentbyoid_x(int *o)
419 {
420     enum oid_proto proto;
421     int prelen;
422     struct oident_list *ol;
423     
424     /* determine protocol type */
425     if ((prelen = match_prefix(o, z3950_prefix)) != 0)
426         proto = PROTO_Z3950;
427     else if ((prelen = match_prefix(o, sr_prefix)) != 0)
428         proto = PROTO_SR;
429     else
430         proto = PROTO_GENERAL;
431     for (ol = oident_table; ol; ol = ol->next)
432     {
433         struct oident *p = &ol->oident;
434         if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
435             return p;
436         if (p->proto == PROTO_GENERAL && !oid_oidcmp (o, p->oidsuffix))
437             return p;
438     }
439     return 0;
440 }
441
442 /*
443  * To query, fill out proto, class, and value of the ent parameter.
444  */
445 int *oid_ent_to_oid(struct oident *ent, int *ret)
446 {
447     struct oident_list *ol;
448     
449     oid_init ();
450     for (ol = oident_table; ol; ol = ol->next)
451     {
452         struct oident *p = &ol->oident;
453         if (ent->value == p->value &&
454             (p->proto == PROTO_GENERAL || (ent->proto == p->proto &&  
455             (ent->oclass == p->oclass || ent->oclass == CLASS_GENERAL))))
456         {
457             if (p->proto == PROTO_Z3950)
458                 oid_oidcpy(ret, z3950_prefix);
459             else if (p->proto == PROTO_SR)
460                 oid_oidcpy(ret, sr_prefix);
461             else
462                 ret[0] = -1;
463             oid_oidcat(ret, p->oidsuffix);
464             ent->desc = p->desc;
465             return ret;
466         }
467     }
468     ret[0] = -1;
469     return 0;
470 }
471
472 /*
473  * To query, fill out proto, class, and value of the ent parameter.
474  */
475 int *oid_getoidbyent(struct oident *ent)
476 {
477     static int ret[OID_SIZE];
478
479     return oid_ent_to_oid (ent, ret);
480 }
481
482 struct oident *oid_addent (int *oid, enum oid_proto proto,
483                            enum oid_class oclass,
484                            const char *desc, int value)
485 {
486     struct oident *oident = 0;
487
488     nmem_mutex_enter (oid_mutex);
489     if (!oident)
490     {
491         char desc_str[200];
492         struct oident_list *oident_list;
493         oident_list = (struct oident_list *)
494             nmem_malloc (oid_nmem, sizeof(*oident_list));
495         oident = &oident_list->oident;
496         oident->proto = proto;
497         oident->oclass = oclass;
498
499         if (!desc)
500         {
501             int i;
502
503             sprintf (desc_str, "%d", *oid);
504             for (i = 1; i < 12 && oid[i] >= 0; i++)
505                 sprintf (desc_str+strlen(desc_str), ".%d", oid[i]);
506             desc = desc_str;
507         }
508         oident->desc = nmem_strdup (oid_nmem, desc);
509         if (value == VAL_DYNAMIC)
510             oident->value = (enum oid_value) (++oid_value_dynamic);
511         else
512             oident->value = (enum oid_value) value;
513         oid_oidcpy (oident->oidsuffix, oid);
514         oident_list->next = oident_table;
515         oident_table = oident_list;
516     }
517     nmem_mutex_leave (oid_mutex);
518     return oident;
519 }
520
521 struct oident *oid_getentbyoid(int *oid)
522 {
523     struct oident *oident;
524
525     if (!oid)
526         return 0;
527     oid_init ();
528     oident = oid_getentbyoid_x (oid);
529     if (!oident)
530         oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL,
531                              NULL, VAL_DYNAMIC);
532     return oident;
533 }
534
535 static oid_value oid_getval_raw(const char *name)
536 {
537     int val = 0, i = 0, oid[OID_SIZE];
538     struct oident *oident;
539     
540     while (isdigit (*name))
541     {
542         val = val*10 + (*name - '0');
543         name++;
544         if (*name == '.')
545         {
546             if (i < OID_SIZE-1)
547                 oid[i++] = val;
548             val = 0;
549             name++;
550         }
551     }
552     oid[i] = val;
553     oid[i+1] = -1;
554     oident = oid_getentbyoid_x (oid);
555     if (!oident)
556         oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL, NULL,
557                          VAL_DYNAMIC);
558     return oident->value;
559 }
560
561 oid_value oid_getvalbyname(const char *name)
562 {
563     struct oident_list *ol;
564
565     oid_init ();
566     if (isdigit (*name))
567         return oid_getval_raw (name);
568     for (ol = oident_table; ol; ol = ol->next)
569         if (!yaz_matchstr(ol->oident.desc, name))
570         {
571             return ol->oident.value;
572         }
573     return VAL_NONE;
574 }
575
576 void oid_setprivateoids(oident *list)
577 {
578     oid_transfer (list);
579 }
580
581 void oid_trav (void (*func)(struct oident *oidinfo, void *vp), void *vp)
582 {
583     struct oident_list *ol;
584
585     oid_init ();
586     for (ol = oident_table; ol; ol = ol->next)
587         (*func)(&ol->oident, vp);
588 }
589
590 int *oid_name_to_oid(oid_class oclass, const char *name, int *oid) {
591     struct oident ent;
592
593     /* Translate syntax to oid_val */
594     oid_value value = oid_getvalbyname(name);
595
596     /* Build it into an oident */
597     ent.proto = PROTO_Z3950;
598     ent.oclass = oclass;
599     ent.value = value;
600
601     /* Translate to an array of int */
602     return oid_ent_to_oid(&ent, oid);
603 }
604
605 char *oid_to_dotstring(const int *oid, char *oidbuf) {
606     char tmpbuf[20];
607     int i;
608
609     oidbuf[0] = '\0';
610     for (i = 0; oid[i] != -1; i++) {
611         sprintf(tmpbuf, "%d", oid[i]);
612         if (i > 0) strcat(oidbuf, ".");
613         strcat(oidbuf, tmpbuf);
614     }
615
616     return oidbuf;
617 }
618
619 char *oid_name_to_dotstring(oid_class oclass, const char *name, char *oidbuf) {
620     int oid[OID_SIZE];
621
622     (void) oid_name_to_oid(oclass, name, oid);
623     return oid_to_dotstring(oid, oidbuf);
624 }
625