Support for private OID table added. Thanks to Ronald van der Meer
[yaz-moved-to-github.git] / util / oid.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: oid.c,v $
7  * Revision 1.19  1997-05-02 08:39:41  quinn
8  * Support for private OID table added. Thanks to Ronald van der Meer
9  *
10  * Revision 1.18  1997/04/30 08:52:12  quinn
11  * Null
12  *
13  * Revision 1.17  1996/10/10  12:35:23  quinn
14  * Added Update extended service.
15  *
16  * Revision 1.16  1996/10/09  15:55:02  quinn
17  * Added SearchInfoReport
18  *
19  * Revision 1.15  1996/10/07  15:29:43  quinn
20  * Added SOIF support
21  *
22  * Revision 1.14  1996/02/20  17:58:28  adam
23  * Added const to oid_getvalbyname.
24  *
25  * Revision 1.13  1996/02/20  16:37:33  quinn
26  * Using yaz_matchstr in oid_getvalbyname
27  *
28  * Revision 1.12  1996/01/02  08:57:53  quinn
29  * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
30  *
31  * Revision 1.11  1995/12/13  16:03:35  quinn
32  * *** empty log message ***
33  *
34  * Revision 1.10  1995/11/28  09:30:44  quinn
35  * Work.
36  *
37  * Revision 1.9  1995/11/13  09:27:53  quinn
38  * Fiddling with the variant stuff.
39  *
40  * Revision 1.8  1995/10/12  10:34:56  quinn
41  * Added Espec-1.
42  *
43  * Revision 1.7  1995/10/10  16:27:12  quinn
44  * *** empty log message ***
45  *
46  * Revision 1.6  1995/09/29  17:12:35  quinn
47  * Smallish
48  *
49  * Revision 1.5  1995/09/29  17:01:51  quinn
50  * More Windows work
51  *
52  * Revision 1.4  1995/09/27  15:03:03  quinn
53  * Modified function heads & prototypes.
54  *
55  * Revision 1.3  1995/09/12  11:32:06  quinn
56  * Added a looker-upper by name.
57  *
58  * Revision 1.2  1995/08/21  09:11:16  quinn
59  * Smallish fixes to suppport new formats.
60  *
61  * Revision 1.1  1995/05/29  08:17:13  quinn
62  * iMoved oid to util to support comstack.
63  *
64  * Revision 1.5  1995/05/22  11:30:16  quinn
65  * Adding Z39.50-1992 stuff to proto.c. Adding zget.c
66  *
67  * Revision 1.4  1995/05/16  08:50:22  quinn
68  * License, documentation, and memory fixes
69  *
70  * Revision 1.3  1995/04/11  11:52:02  quinn
71  * Fixed possible buf in proto.c
72  *
73  * Revision 1.2  1995/03/29  15:39:38  quinn
74  * Adding some resource control elements, and a null-check to getentbyoid
75  *
76  * Revision 1.1  1995/03/27  08:32:12  quinn
77  * Added OID database
78  *
79  *
80  */
81
82 /*
83  * More or less protocol-transparent OID database.
84  * We could (and should?) extend this so that the user app can add new
85  * entries to the list at initialization.
86  */
87
88 #include <oid.h>
89 #include <yaz-util.h>
90
91 static int z3950_prefix[] = { 1, 2, 840, 10003, -1 };
92 static int sr_prefix[]    = { 1, 0, 10163, -1 };
93
94 static oident *extoids = NULL;
95
96 /*
97  * OID database
98  */
99 static oident oids[] =
100 {
101     /* General definitions */
102     {PROTO_GENERAL, CLASS_TRANSYN, VAL_BER,       {2,1,1,-1},  "BER"         },
103     {PROTO_GENERAL, CLASS_TRANSYN, VAL_ISO2709,   {1,0,2709,1,1,-1},"ISO2709"},
104
105     /* Z39.50v3 definitions */
106     {PROTO_Z3950,   CLASS_ABSYN,   VAL_APDU,      {2,1,-1},    "Z-APDU"      },
107     {PROTO_Z3950,   CLASS_APPCTX,  VAL_BASIC_CTX, {1,1,-1},    "Z-BASIC"     },
108     {PROTO_Z3950,   CLASS_ATTSET,  VAL_BIB1,      {3,1,-1},    "Bib-1"       },
109     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXP1,      {3,2,-1},    "Exp-1"       },
110     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXT1,      {3,3,-1},    "Ext-1"       },
111     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CCL1,      {3,4,-1},    "CCL-1"       },
112     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GILS,      {3,5,-1},    "GILS-attset" },
113     {PROTO_Z3950,   CLASS_ATTSET,  VAL_STAS,      {3,6,-1},    "STAS-attset" },
114     {PROTO_Z3950,   CLASS_DIAGSET, VAL_BIB1,      {4,1,-1},    "Bib-1"       },
115     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG1,     {4,2,-1},    "Diag-1"      },
116     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UNIMARC,   {5,1,-1},    "Unimarc"     },
117     {PROTO_Z3950,   CLASS_RECSYN,  VAL_INTERMARC, {5,2,-1},    "Intermarc"   },
118     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CCF,       {5,3,-1},    "CCF"         },
119     {PROTO_Z3950,   CLASS_RECSYN,  VAL_USMARC,    {5,10,-1},   "USmarc"      },
120     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UKMARC,    {5,11,-1},   "UKmarc"      },
121     {PROTO_Z3950,   CLASS_RECSYN,  VAL_NORMARC,   {5,12,-1},   "Normarc"     },
122     {PROTO_Z3950,   CLASS_RECSYN,  VAL_LIBRISMARC,{5,13,-1},   "Librismarc"  },
123     {PROTO_Z3950,   CLASS_RECSYN,  VAL_DANMARC,   {5,14,-1},   "Danmarc"     },
124     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FINMARC,   {5,15,-1},   "Finmarc"     },
125     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
126     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
127     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SBN,       {5,18,-1},   "SBN"         },
128     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PICAMARC,  {5,19,-1},   "Picamarc"    },
129     {PROTO_Z3950,   CLASS_RECSYN,  VAL_AUSMARC,   {5,20,-1},   "Ausmarc"     },
130     {PROTO_Z3950,   CLASS_RECSYN,  VAL_IBERMARC,  {5,21,-1},   "Ibermarc"    },
131     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXPLAIN,   {5,100,-1},  "Explain"     },
132     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUTRS,     {5,101,-1},  "SUTRS"       },
133     {PROTO_Z3950,   CLASS_RECSYN,  VAL_OPAC,      {5,102,-1},  "OPAC"        },
134     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUMMARY,   {5,103,-1},  "Summary"     },
135     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS0,      {5,104,-1},  "GRS-0"       },
136     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS1,      {5,105,-1},  "GRS-1"       },
137     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXTENDED,  {5,106,-1},  "Extended"    },
138 #if 0
139     {PROTO_Z3950,   CLASS_RECSYN,  VAL_ID_SGML,   {5,1000,81,1,-1},"ID-SGML" },
140 #endif
141     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SOIF,      {5,1000,81,2,-1},"SOIF" },
142     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},    "Resource-1"  },
143     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},    "Resource-2"  },
144     {PROTO_Z3950,   CLASS_ACCFORM, VAL_PROMPT1,   {8,1,-1},    "Prompt-1"    },
145     {PROTO_Z3950,   CLASS_ACCFORM, VAL_DES1,      {8,2,-1},    "Des-1"       },
146     {PROTO_Z3950,   CLASS_ACCFORM, VAL_KRB1,      {8,3,-1},    "Krb-1"       },
147     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PRESSET,   {9,1,-1},    "Pers. set"   },
148     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PQUERY,    {9,2,-1},    "Pers. query" },
149     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PCQUERY,   {9,3,-1},    "Per'd query" },
150     {PROTO_Z3950,   CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},    "Item order"  },
151     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE,  {9,5,-1},    "DB. Update"  },
152     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTSPEC,{9,6,-1},    "exp. spec."  },
153     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},    "exp. inv."   },
154
155     {PROTO_Z3950,   CLASS_USERINFO,VAL_SEARCHRES1,{10,1,-1},  "searchResult-1"},
156
157     {PROTO_Z3950,   CLASS_ELEMSPEC,VAL_ESPEC1,    {11,1,-1},   "Espec-1"     },
158     {PROTO_Z3950,   CLASS_VARSET,  VAL_VAR1,      {12,1,-1},   "Variant-1"   },
159
160     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_WAIS,      {13,1,-1},   "WAIS-schema" },
161     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_GILS,      {13,2,-1},   "GILS-schema" },
162
163     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETM,      {14,1,-1},   "TagsetM"     },
164     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETG,      {14,2,-1},   "TagsetG"     },
165 #if 0
166     {PROTO_Z3950,   CLASS_TAGSET,  VAL_GILS,      {14,3,-1},   "GILS-tagset" },
167 #endif
168
169     /* SR definitions. Note that some of them aren't defined by the
170         standard (yet), but are borrowed from Z3950v3 */
171     {PROTO_SR,      CLASS_ABSYN,   VAL_APDU,      {2,1,-1},    "SR-APDU"     },
172     {PROTO_SR,      CLASS_APPCTX,  VAL_BASIC_CTX, {1,1,-1},    "SR-BASIC"    },
173     {PROTO_SR,      CLASS_ATTSET,  VAL_BIB1,      {3,1,-1},    "Bib-1"       },
174     {PROTO_SR,      CLASS_ATTSET,  VAL_EXP1,      {3,2,-1},    "Exp-1"       },
175     {PROTO_SR,      CLASS_ATTSET,  VAL_EXT1,      {3,3,-1},    "Ext-1"       },
176     {PROTO_SR,      CLASS_ATTSET,  VAL_CCL1,      {3,4,-1},    "CCL-1"       },
177     {PROTO_SR,      CLASS_ATTSET,  VAL_GILS,      {3,5,-1},    "GILS"        },
178     {PROTO_SR,      CLASS_ATTSET,  VAL_STAS,      {3,6,-1},    "STAS",       },
179     {PROTO_SR,      CLASS_DIAGSET, VAL_BIB1,      {4,1,-1},    "Bib-1"       },
180     {PROTO_SR,      CLASS_DIAGSET, VAL_DIAG1,     {4,2,-1},    "Diag-1"      },
181     {PROTO_SR,      CLASS_RECSYN,  VAL_UNIMARC,   {5,1,-1},    "Unimarc"     },
182     {PROTO_SR,      CLASS_RECSYN,  VAL_INTERMARC, {5,2,-1},    "Intermarc"   },
183     {PROTO_SR,      CLASS_RECSYN,  VAL_CCF,       {5,3,-1},     "CCF"        },
184     {PROTO_SR,      CLASS_RECSYN,  VAL_USMARC,    {5,10,-1},   "USmarc"      },
185     {PROTO_SR,      CLASS_RECSYN,  VAL_UKMARC,    {5,11,-1},   "UKmarc"      },
186     {PROTO_SR,      CLASS_RECSYN,  VAL_NORMARC,   {5,12,-1},   "Normarc"     },
187     {PROTO_SR,      CLASS_RECSYN,  VAL_LIBRISMARC,{5,13,-1},   "Librismarc"  },
188     {PROTO_SR,      CLASS_RECSYN,  VAL_DANMARC,   {5,14,-1},   "Danmarc"     },
189     {PROTO_SR,      CLASS_RECSYN,  VAL_FINMARC,   {5,15,-1},   "Finmarc"     },
190     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
191     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
192     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
193     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
194     {PROTO_SR,      CLASS_RECSYN,  VAL_SBN,       {5,18,-1},   "SBN"         },
195     {PROTO_SR,      CLASS_RECSYN,  VAL_PICAMARC,  {5,19,-1},   "Picamarc"    },
196     {PROTO_SR,      CLASS_RECSYN,  VAL_AUSMARC,   {5,20,-1},   "Ausmarc"     },
197     {PROTO_SR,      CLASS_RECSYN,  VAL_IBERMARC,  {5,21,-1},   "Ibermarc"    },
198     {PROTO_SR,      CLASS_RECSYN,  VAL_EXPLAIN,   {5,100,-1},  "Explain"     },
199     {PROTO_SR,      CLASS_RECSYN,  VAL_SUTRS,     {5,101,-1},  "SUTRS"       },
200     {PROTO_SR,      CLASS_RECSYN,  VAL_OPAC,      {5,102,-1},  "OPAC"        },
201     {PROTO_SR,      CLASS_RECSYN,  VAL_SUMMARY,   {5,103,-1},  "Summary"     },
202     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS0,      {5,104,-1},  "GRS-0"       },
203     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS1,      {5,105,-1},  "GRS-1"       },
204     {PROTO_SR,      CLASS_RECSYN,  VAL_EXTENDED,  {5,106,-1},  "Extended"    },
205     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},    "Resource-1"  },
206     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},    "Resource-2"  },
207     {PROTO_SR,      CLASS_ACCFORM, VAL_PROMPT1,   {8,1,-1},    "Prompt-1"    },
208     {PROTO_SR,      CLASS_ACCFORM, VAL_DES1,      {8,2,-1},    "Des-1"       },
209     {PROTO_SR,      CLASS_ACCFORM, VAL_KRB1,      {8,3,-1},    "Krb-1"       },
210     {PROTO_SR,      CLASS_EXTSERV, VAL_PRESSET,   {9,1,-1},    "Pers. set"   },
211     {PROTO_SR,      CLASS_EXTSERV, VAL_PQUERY,    {9,2,-1},    "Pers. query" },
212     {PROTO_SR,      CLASS_EXTSERV, VAL_PCQUERY,   {9,3,-1},    "Per'd query" },
213     {PROTO_SR,      CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},    "Item order"  },
214     {PROTO_SR,      CLASS_EXTSERV, VAL_DBUPDATE,  {9,5,-1},    "DB. Update"  },
215     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTSPEC,{9,6,-1},    "exp. spec."  },
216     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},    "exp. inv."   },
217     {PROTO_SR,      CLASS_ELEMSPEC,VAL_ESPEC1,    {11,1,-1},   "Espec-1"     },
218     {PROTO_SR,      CLASS_VARSET,  VAL_VAR1,      {12,1,-1},   "Variant-1"   },
219
220     {PROTO_SR,      CLASS_SCHEMA,  VAL_WAIS,      {13,1,-1},   "WAIS-schema" },
221     {PROTO_SR,      CLASS_SCHEMA,  VAL_GILS,      {13,2,-1},   "GILS-schema" },
222
223     {PROTO_SR,      CLASS_TAGSET,  VAL_SETM,      {14,1,-1},   "TagsetM"     },
224     {PROTO_SR,      CLASS_TAGSET,  VAL_SETG,      {14,2,-1},   "TagsetG"     },
225 #if 0
226     {PROTO_SR,      CLASS_TAGSET,  VAL_GILS,      {14,3,-1},   "GILS-tagset" },
227 #endif
228
229     {0,             0,             0,             {-1},        0          }
230 };
231
232 /* OID utilities */
233
234 void oid_oidcpy(int *t, int *s)
235 {
236     while ((*(t++) = *(s++)) > -1);
237 }
238
239 void oid_oidcat(int *t, int *s)
240 {
241     while (*t > -1)
242         t++;
243     while ((*(t++) = *(s++)) > -1);
244 }
245
246 int oid_oidcmp(int *o1, int *o2)
247 {
248     while (*o1 == *o2 && *o1 > -1)
249     {
250         o1++;
251         o2++;
252     }
253     if (*o1 == *o2)
254         return 0;
255     else if (*o1 > *o2)
256         return 1;
257     else
258         return -1;
259 }
260
261 int oid_oidlen(int *o)
262 {
263     int len = 0;
264
265     while (*(o++) >= 0)
266         len++;
267     return len;
268 }
269
270
271 static int match_prefix(int *look, int *prefix)
272 {
273     int len;
274
275     for (len = 0; *look == *prefix; look++, prefix++, len++);
276     if (*prefix < 0) /* did we reach the end of the prefix? */
277         return len;
278     return 0;
279 }
280
281 struct oident *oid_getentbyoid(int *o)
282 {
283     enum oid_proto proto;
284     int prelen;
285     oident *p;
286
287     /* determine protocol type */
288     if (!o)
289         return 0;
290     if ((prelen = match_prefix(o, z3950_prefix)) != 0)
291         proto = PROTO_Z3950;
292     else if ((prelen = match_prefix(o, sr_prefix)) != 0)
293         proto = PROTO_SR;
294     else
295         proto = PROTO_GENERAL;
296     for (p = oids; *p->oidsuffix >= 0; p++)
297         if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
298             return p;
299     if (extoids != NULL)
300         for (p = extoids; *p->oidsuffix >= 0; p++)
301             if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
302                 return p;
303     return 0;
304 }
305
306 /*
307  * To query, fill out proto, class, and value of the ent parameter.
308  */
309 int *oid_getoidbyent(struct oident *ent)
310 {
311     struct oident *p;
312     static int ret[OID_SIZE];
313
314     for (p = oids; *p->oidsuffix >= 0; p++)
315         if (ent->proto == p->proto &&
316             ent->oclass == p->oclass &&
317             ent->value == p->value)
318         {
319             if (ent->proto == PROTO_Z3950)
320                 oid_oidcpy(ret, z3950_prefix);
321             else if (ent->proto == PROTO_SR)
322                 oid_oidcpy(ret, sr_prefix);
323             else
324                 ret[0] = -1;
325             oid_oidcat(ret, p->oidsuffix);
326             return ret;
327         }
328     if (extoids != NULL)
329         for (p = extoids; *p->oidsuffix >= 0; p++)
330            if (ent->proto == p->proto &&
331                 ent->oclass == p->oclass &&
332                 ent->value == p->value)
333             {
334                 if (ent->proto == PROTO_Z3950)
335                     oid_oidcpy(ret, z3950_prefix);
336                 else if (ent->proto == PROTO_SR)
337                     oid_oidcpy(ret, sr_prefix);
338                 else
339                     ret[0] = -1;
340                 oid_oidcat(ret, p->oidsuffix);
341                 return ret;
342             }
343     return 0;
344 }
345
346 oid_value oid_getvalbyname(const char *name)
347 {
348     struct oident *p;
349
350     for (p = oids; *p->oidsuffix >= 0; p++)
351         if (!yaz_matchstr(p->desc, name))
352             return p->value;
353     if (extoids != NULL)
354         for (p = extoids; *p->oidsuffix >= 0; p++)
355             if (!yaz_matchstr(p->desc, name))
356                 return p->value;
357     return VAL_NONE;
358 }
359
360 void oid_setprivateoids(oident *list)
361 {
362     extoids = list;
363 }