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