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