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