2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: oid.c,v 1.11 2005-06-25 15:46:04 adam Exp $
10 * \brief Implements OID database
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.
25 #include <yaz/yaz-util.h>
27 static int z3950_prefix[] = { 1, 2, 840, 10003, -1 };
28 static int sr_prefix[] = { 1, 0, 10163, -1 };
32 struct oident_list *next;
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;
44 static oident standard_oids[] =
46 /* General definitions */
47 {PROTO_GENERAL, CLASS_TRANSYN, VAL_BER, {2,1,1,-1},
49 {PROTO_GENERAL, CLASS_TRANSYN, VAL_ISO2709, {1,0,2709,1,1,-1},
51 {PROTO_GENERAL, CLASS_GENERAL, VAL_ISO_ILL_1, {1,0,10161,2,1,-1},
53 /* Z39.50v3 definitions */
54 {PROTO_Z3950, CLASS_ABSYN, VAL_APDU, {2,1,-1},
56 {PROTO_Z3950, CLASS_APPCTX, VAL_BASIC_CTX, {1,1,-1},
58 {PROTO_Z3950, CLASS_ATTSET, VAL_BIB1, {3,1,-1},
60 {PROTO_Z3950, CLASS_ATTSET, VAL_EXP1, {3,2,-1},
62 {PROTO_Z3950, CLASS_ATTSET, VAL_EXT1, {3,3,-1},
64 {PROTO_Z3950, CLASS_ATTSET, VAL_CCL1, {3,4,-1},
66 {PROTO_Z3950, CLASS_ATTSET, VAL_GILS, {3,5,-1},
68 {PROTO_Z3950, CLASS_ATTSET, VAL_GILS, {3,5,-1},
70 {PROTO_Z3950, CLASS_ATTSET, VAL_STAS, {3,6,-1},
72 {PROTO_Z3950, CLASS_ATTSET, VAL_COLLECT1, {3,7,-1},
73 "Collections-attset"},
74 {PROTO_Z3950, CLASS_ATTSET, VAL_CIMI1, {3,8,-1},
76 {PROTO_Z3950, CLASS_ATTSET, VAL_GEO, {3,9,-1},
79 {PROTO_Z3950, CLASS_ATTSET, VAL_ZBIG, {3,10,-1},
81 {PROTO_Z3950, CLASS_ATTSET, VAL_UTIL, {3,11,-1},
83 {PROTO_Z3950, CLASS_ATTSET, VAL_XD1, {3,12,-1},
85 {PROTO_Z3950, CLASS_ATTSET, VAL_ZTHES, {3,13,-1},
87 {PROTO_Z3950, CLASS_ATTSET, VAL_FIN1, {3,14,-1},
89 {PROTO_Z3950, CLASS_ATTSET, VAL_DAN1, {3,15,-1},
91 {PROTO_Z3950, CLASS_ATTSET, VAL_HOLDINGS, {3,16,-1},
93 {PROTO_Z3950, CLASS_ATTSET, VAL_USMARC, {3,17,-1},
95 {PROTO_Z3950, CLASS_ATTSET, VAL_BIB2, {3,18,-1},
97 {PROTO_Z3950, CLASS_ATTSET, VAL_ZEEREX, {3,19,-1},
99 /* New applications should use Zthes-1 instead of this Satan-spawn */
100 {PROTO_Z3950, CLASS_ATTSET, VAL_THESAURUS, {3,1000,81,1,-1},
102 {PROTO_Z3950, CLASS_ATTSET, VAL_IDXPATH, {3,1000,81,2,-1},
104 {PROTO_Z3950, CLASS_ATTSET, VAL_EXTLITE, {3,1000,81,3,-1},
106 {PROTO_Z3950, CLASS_DIAGSET, VAL_BIB1, {4,1,-1},
108 {PROTO_Z3950, CLASS_DIAGSET, VAL_DIAG1, {4,2,-1},
110 {PROTO_Z3950, CLASS_DIAGSET, VAL_DIAG_ES, {4,3,-1},
112 {PROTO_Z3950, CLASS_DIAGSET, VAL_DIAG_GENERAL, {4,3,-1},
114 {PROTO_Z3950, CLASS_RECSYN, VAL_UNIMARC, {5,1,-1},
116 {PROTO_Z3950, CLASS_RECSYN, VAL_INTERMARC, {5,2,-1},
118 {PROTO_Z3950, CLASS_RECSYN, VAL_CCF, {5,3,-1},
120 /* MARC21 is just an alias for the original USmarc */
121 {PROTO_Z3950, CLASS_RECSYN, VAL_USMARC, {5,10,-1},
123 {PROTO_Z3950, CLASS_RECSYN, VAL_USMARC, {5,10,-1},
125 {PROTO_Z3950, CLASS_RECSYN, VAL_UKMARC, {5,11,-1},
127 {PROTO_Z3950, CLASS_RECSYN, VAL_NORMARC, {5,12,-1},
129 {PROTO_Z3950, CLASS_RECSYN, VAL_LIBRISMARC, {5,13,-1},
131 {PROTO_Z3950, CLASS_RECSYN, VAL_DANMARC, {5,14,-1},
133 {PROTO_Z3950, CLASS_RECSYN, VAL_FINMARC, {5,15,-1},
135 {PROTO_Z3950, CLASS_RECSYN, VAL_MAB, {5,16,-1},
137 {PROTO_Z3950, CLASS_RECSYN, VAL_CANMARC, {5,17,-1},
139 {PROTO_Z3950, CLASS_RECSYN, VAL_SBN, {5,18,-1},
141 {PROTO_Z3950, CLASS_RECSYN, VAL_PICAMARC, {5,19,-1},
143 {PROTO_Z3950, CLASS_RECSYN, VAL_AUSMARC, {5,20,-1},
145 {PROTO_Z3950, CLASS_RECSYN, VAL_IBERMARC, {5,21,-1},
147 {PROTO_Z3950, CLASS_RECSYN, VAL_CATMARC, {5,22,-1},
149 {PROTO_Z3950, CLASS_RECSYN, VAL_MALMARC, {5,23,-1},
151 {PROTO_Z3950, CLASS_RECSYN, VAL_JPMARC, {5,24,-1},
153 {PROTO_Z3950, CLASS_RECSYN, VAL_SWEMARC, {5,25,-1},
155 {PROTO_Z3950, CLASS_RECSYN, VAL_SIGLEMARC, {5,26,-1},
157 {PROTO_Z3950, CLASS_RECSYN, VAL_ISDSMARC, {5,27,-1},
159 {PROTO_Z3950, CLASS_RECSYN, VAL_RUSMARC, {5,28,-1},
161 {PROTO_Z3950, CLASS_RECSYN, VAL_HUNMARC, {5,29,-1},
163 {PROTO_Z3950, CLASS_RECSYN, VAL_NACSISCATP, {5,30,-1},
165 {PROTO_Z3950, CLASS_RECSYN, VAL_FINMARC2000, {5,31,-1},
167 {PROTO_Z3950, CLASS_RECSYN, VAL_MARC21FIN, {5,32,-1},
169 {PROTO_Z3950, CLASS_RECSYN, VAL_EXPLAIN, {5,100,-1},
171 {PROTO_Z3950, CLASS_RECSYN, VAL_SUTRS, {5,101,-1},
173 {PROTO_Z3950, CLASS_RECSYN, VAL_OPAC, {5,102,-1},
175 {PROTO_Z3950, CLASS_RECSYN, VAL_SUMMARY, {5,103,-1},
177 {PROTO_Z3950, CLASS_RECSYN, VAL_GRS0, {5,104,-1},
179 {PROTO_Z3950, CLASS_RECSYN, VAL_GRS1, {5,105,-1},
181 {PROTO_Z3950, CLASS_RECSYN, VAL_EXTENDED, {5,106,-1},
183 {PROTO_Z3950, CLASS_RECSYN, VAL_FRAGMENT, {5,107,-1},
185 {PROTO_Z3950, CLASS_RECSYN, VAL_PDF, {5,109,1,-1},
187 {PROTO_Z3950, CLASS_RECSYN, VAL_POSTSCRIPT, {5,109,2,-1},
189 {PROTO_Z3950, CLASS_RECSYN, VAL_HTML, {5,109,3,-1},
191 {PROTO_Z3950, CLASS_RECSYN, VAL_TIFF, {5,109,4,-1},
193 {PROTO_Z3950, CLASS_RECSYN, VAL_GIF, {5,109,5,-1},
195 {PROTO_Z3950, CLASS_RECSYN, VAL_JPEG, {5,109,6,-1},
197 {PROTO_Z3950, CLASS_RECSYN, VAL_PNG, {5,109,7,-1},
199 {PROTO_Z3950, CLASS_RECSYN, VAL_MPEG, {5,109,8,-1},
201 {PROTO_Z3950, CLASS_RECSYN, VAL_SGML, {5,109,9,-1},
204 {PROTO_Z3950, CLASS_RECSYN, VAL_TIFFB, {5,110,1,-1},
206 {PROTO_Z3950, CLASS_RECSYN, VAL_WAV, {5,110,2,-1},
209 {PROTO_Z3950, CLASS_RECSYN, VAL_SQLRS, {5,111,-1},
211 {PROTO_Z3950, CLASS_RECSYN, VAL_SOIF, {5,1000,81,2,-1},
213 {PROTO_Z3950, CLASS_RECSYN, VAL_TEXT_XML, {5,109,10,-1},
215 {PROTO_Z3950, CLASS_RECSYN, VAL_TEXT_XML, {5,109,10,-1},
217 {PROTO_Z3950, CLASS_RECSYN, VAL_APPLICATION_XML, {5,109,11,-1},
219 {PROTO_Z3950, CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},
221 {PROTO_Z3950, CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},
223 {PROTO_Z3950, CLASS_RESFORM, VAL_UNIVERSE_REPORT, {7,1000,81,1,-1},
224 "UNIverse-Resource-Report"},
226 {PROTO_Z3950, CLASS_ACCFORM, VAL_PROMPT1, {8,1,-1},
228 {PROTO_Z3950, CLASS_ACCFORM, VAL_DES1, {8,2,-1},
230 {PROTO_Z3950, CLASS_ACCFORM, VAL_KRB1, {8,3,-1},
232 {PROTO_Z3950, CLASS_EXTSERV, VAL_PRESSET, {9,1,-1},
234 {PROTO_Z3950, CLASS_EXTSERV, VAL_PQUERY, {9,2,-1},
236 {PROTO_Z3950, CLASS_EXTSERV, VAL_PCQUERY, {9,3,-1},
238 {PROTO_Z3950, CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},
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},
246 {PROTO_Z3950, CLASS_EXTSERV, VAL_EXPORTSPEC, {9,6,-1},
248 {PROTO_Z3950, CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},
250 {PROTO_Z3950, CLASS_EXTSERV, VAL_ADMINSERVICE, {9,1000,81,1,-1},
252 {PROTO_Z3950, CLASS_USERINFO,VAL_SEARCHRES1, {10,1,-1},
254 {PROTO_Z3950, CLASS_USERINFO,VAL_CHARLANG, {10,2,-1},
255 "CharSetandLanguageNegotiation"},
256 {PROTO_Z3950, CLASS_USERINFO,VAL_USERINFO1, {10,3,-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},
264 {PROTO_Z3950, CLASS_USERINFO,VAL_PROXY, {10,1000,81,1,-1},
266 {PROTO_Z3950, CLASS_USERINFO,VAL_COOKIE, {10,1000,81,2,-1},
268 {PROTO_Z3950, CLASS_USERINFO,VAL_CLIENT_IP, {10,1000,81,3,-1},
270 {PROTO_Z3950, CLASS_ELEMSPEC,VAL_ESPEC1, {11,1,-1},
272 {PROTO_Z3950, CLASS_VARSET, VAL_VAR1, {12,1,-1},
274 {PROTO_Z3950, CLASS_SCHEMA, VAL_WAIS, {13,1,-1},
276 {PROTO_Z3950, CLASS_SCHEMA, VAL_GILS, {13,2,-1},
278 {PROTO_Z3950, CLASS_SCHEMA, VAL_COLLECT1, {13,3,-1},
279 "Collections-schema"},
280 {PROTO_Z3950, CLASS_SCHEMA, VAL_GEO, {13,4,-1},
282 {PROTO_Z3950, CLASS_SCHEMA, VAL_CIMI1, {13,5,-1},
284 {PROTO_Z3950, CLASS_SCHEMA, VAL_UPDATEES, {13,6,-1},
286 {PROTO_Z3950, CLASS_SCHEMA, VAL_HOLDINGS, {13,7,-1},
288 {PROTO_Z3950, CLASS_SCHEMA, VAL_ZTHES, {13,8,-1},
290 {PROTO_Z3950, CLASS_SCHEMA, VAL_THESAURUS, {13,1000,81,1,-1},
292 {PROTO_Z3950, CLASS_SCHEMA, VAL_EXPLAIN, {13,1000,81,2,-1},
294 {PROTO_Z3950, CLASS_TAGSET, VAL_SETM, {14,1,-1},
296 {PROTO_Z3950, CLASS_TAGSET, VAL_SETG, {14,2,-1},
298 {PROTO_Z3950, CLASS_TAGSET, VAL_STAS, {14,3,-1},
300 {PROTO_Z3950, CLASS_TAGSET, VAL_GILS, {14,4,-1},
302 {PROTO_Z3950, CLASS_TAGSET, VAL_COLLECT1, {14,5,-1},
303 "Collections-tagset"},
304 {PROTO_Z3950, CLASS_TAGSET, VAL_CIMI1, {14,6,-1},
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},
310 {PROTO_Z3950, CLASS_TAGSET, VAL_ZTHES, {14,8,-1},
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},
318 {PROTO_Z3950, CLASS_USERINFO,VAL_CQL, {16, 2, -1},
320 {PROTO_GENERAL, CLASS_GENERAL, VAL_UCS2, {1,0,10646,1,0,2,-1},
322 {PROTO_GENERAL, CLASS_GENERAL, VAL_UCS4, {1,0,10646,1,0,4,-1},
324 {PROTO_GENERAL, CLASS_GENERAL, VAL_UTF16, {1,0,10646,1,0,5,-1},
326 {PROTO_GENERAL, CLASS_GENERAL, VAL_UTF8, {1,0,10646,1,0,8,-1},
328 {PROTO_Z3950, CLASS_USERINFO,VAL_OCLCUI, {10, 1000, 17, 1, -1},
330 {PROTO_Z3950, CLASS_EXTSERV, VAL_XMLES, {9,1000,105,4,-1},
332 {PROTO_NOP, CLASS_NOP, VAL_NOP, {-1}, 0 }
337 void oid_oidcpy(int *t, int *s)
339 while ((*(t++) = *(s++)) > -1);
342 void oid_oidcat(int *t, int *s)
346 while ((*(t++) = *(s++)) > -1);
349 int oid_oidcmp(int *o1, int *o2)
351 while (*o1 == *o2 && *o1 > -1)
364 int oid_oidlen(int *o)
374 static int match_prefix(int *look, int *prefix)
378 for (len = 0; *look == *prefix; look++, prefix++, len++);
379 if (*prefix < 0) /* did we reach the end of the prefix? */
384 void oid_transfer (struct oident *oidentp)
386 while (*oidentp->oidsuffix >= 0)
388 oid_addent (oidentp->oidsuffix, oidentp->proto,
390 oidentp->desc, oidentp->value);
397 if (oid_init_flag == 0)
399 /* oid_transfer is thread safe, so there's nothing wrong in having
400 two threads calling it simultaniously. On the other hand
401 no thread may exit oid_init before all OID's bave been
402 transferred - which is why checked is set after oid_transfer...
404 nmem_mutex_create (&oid_mutex);
405 nmem_mutex_enter (oid_mutex);
407 oid_nmem = nmem_create ();
408 nmem_mutex_leave (oid_mutex);
409 oid_transfer (standard_oids);
419 nmem_mutex_destroy (&oid_mutex);
420 nmem_destroy (oid_nmem);
425 static struct oident *oid_getentbyoid_x(int *o)
427 enum oid_proto proto;
429 struct oident_list *ol;
431 /* determine protocol type */
432 if ((prelen = match_prefix(o, z3950_prefix)) != 0)
434 else if ((prelen = match_prefix(o, sr_prefix)) != 0)
437 proto = PROTO_GENERAL;
438 for (ol = oident_table; ol; ol = ol->next)
440 struct oident *p = &ol->oident;
441 if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
443 if (p->proto == PROTO_GENERAL && !oid_oidcmp (o, p->oidsuffix))
450 * To query, fill out proto, class, and value of the ent parameter.
452 int *oid_ent_to_oid(struct oident *ent, int *ret)
454 struct oident_list *ol;
457 for (ol = oident_table; ol; ol = ol->next)
459 struct oident *p = &ol->oident;
460 if (ent->value == p->value &&
461 (p->proto == PROTO_GENERAL || (ent->proto == p->proto &&
462 (ent->oclass == p->oclass || ent->oclass == CLASS_GENERAL))))
464 if (p->proto == PROTO_Z3950)
465 oid_oidcpy(ret, z3950_prefix);
466 else if (p->proto == PROTO_SR)
467 oid_oidcpy(ret, sr_prefix);
470 oid_oidcat(ret, p->oidsuffix);
480 * To query, fill out proto, class, and value of the ent parameter.
482 int *oid_getoidbyent(struct oident *ent)
484 static int ret[OID_SIZE];
486 return oid_ent_to_oid (ent, ret);
489 struct oident *oid_addent (int *oid, enum oid_proto proto,
490 enum oid_class oclass,
491 const char *desc, int value)
493 struct oident *oident = 0;
495 nmem_mutex_enter (oid_mutex);
498 struct oident_list *oident_list;
499 oident_list = (struct oident_list *)
500 nmem_malloc (oid_nmem, sizeof(*oident_list));
501 oident = &oident_list->oident;
502 oident->proto = proto;
503 oident->oclass = oclass;
507 char desc_str[OID_SIZE*10];
513 sprintf (desc_str, "%d", *oid);
514 for (i = 1; i < OID_SIZE && oid[i] >= 0; i++)
515 sprintf (desc_str+strlen(desc_str), ".%d", oid[i]);
517 oident->desc = nmem_strdup(oid_nmem, desc_str);
520 oident->desc = nmem_strdup(oid_nmem, desc);
521 if (value == VAL_DYNAMIC)
522 oident->value = (enum oid_value) (++oid_value_dynamic);
524 oident->value = (enum oid_value) value;
525 oid_oidcpy (oident->oidsuffix, oid);
526 oident_list->next = oident_table;
527 oident_table = oident_list;
529 nmem_mutex_leave (oid_mutex);
533 struct oident *oid_getentbyoid(int *oid)
535 struct oident *oident;
540 oident = oid_getentbyoid_x (oid);
542 oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL,
547 static oid_value oid_getval_raw(const char *name)
549 int val = 0, i = 0, oid[OID_SIZE];
550 struct oident *oident;
552 while (isdigit (*(const unsigned char *) name))
554 val = val*10 + (*name - '0');
566 oident = oid_getentbyoid_x (oid);
568 oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL, NULL,
570 return oident->value;
573 oid_value oid_getvalbyname(const char *name)
575 struct oident_list *ol;
578 if (isdigit (*(const unsigned char *) name))
579 return oid_getval_raw (name);
580 for (ol = oident_table; ol; ol = ol->next)
581 if (!yaz_matchstr(ol->oident.desc, name))
583 return ol->oident.value;
588 void oid_setprivateoids(oident *list)
593 void oid_trav (void (*func)(struct oident *oidinfo, void *vp), void *vp)
595 struct oident_list *ol;
598 for (ol = oident_table; ol; ol = ol->next)
599 (*func)(&ol->oident, vp);
602 int *oid_name_to_oid(oid_class oclass, const char *name, int *oid) {
605 /* Translate syntax to oid_val */
606 oid_value value = oid_getvalbyname(name);
608 /* Build it into an oident */
609 ent.proto = PROTO_Z3950;
613 /* Translate to an array of int */
614 return oid_ent_to_oid(&ent, oid);
617 char *oid_to_dotstring(const int *oid, char *oidbuf) {
622 for (i = 0; oid[i] != -1; i++) {
623 sprintf(tmpbuf, "%d", oid[i]);
624 if (i > 0) strcat(oidbuf, ".");
625 strcat(oidbuf, tmpbuf);
631 char *oid_name_to_dotstring(oid_class oclass, const char *name, char *oidbuf) {
634 (void) oid_name_to_oid(oclass, name, oid);
635 return oid_to_dotstring(oid, oidbuf);
641 * indent-tabs-mode: nil
643 * vim: shiftwidth=4 tabstop=8 expandtab