Modified function heads & prototypes.
[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.4  1995-09-27 15:03:03  quinn
8  * Modified function heads & prototypes.
9  *
10  * Revision 1.3  1995/09/12  11:32:06  quinn
11  * Added a looker-upper by name.
12  *
13  * Revision 1.2  1995/08/21  09:11:16  quinn
14  * Smallish fixes to suppport new formats.
15  *
16  * Revision 1.1  1995/05/29  08:17:13  quinn
17  * iMoved oid to util to support comstack.
18  *
19  * Revision 1.5  1995/05/22  11:30:16  quinn
20  * Adding Z39.50-1992 stuff to proto.c. Adding zget.c
21  *
22  * Revision 1.4  1995/05/16  08:50:22  quinn
23  * License, documentation, and memory fixes
24  *
25  * Revision 1.3  1995/04/11  11:52:02  quinn
26  * Fixed possible buf in proto.c
27  *
28  * Revision 1.2  1995/03/29  15:39:38  quinn
29  * Adding some resource control elements, and a null-check to getentbyoid
30  *
31  * Revision 1.1  1995/03/27  08:32:12  quinn
32  * Added OID database
33  *
34  *
35  */
36
37 /*
38  * More or less protocol-transparent OID database.
39  * We could (and should?) extend this so that the user app can add new
40  * entries to the list at initialization.
41  */
42
43 #include <oid.h>
44
45 static int z3950_prefix[] = { 1, 2, 840, 10003, -1 };
46 static int sr_prefix[]    = { 1, 0, 10163, -1 };
47
48 /*
49  * OID database
50  */
51 static oident oids[] =
52 {
53     /* General definitions */
54     {PROTO_GENERAL, CLASS_TRANSYN, VAL_BER,       {2,1,1,-1},  "BER"         },
55     {PROTO_GENERAL, CLASS_TRANSYN, VAL_ISO2709,   {1,0,2709,1,1,-1},"ISO2709"},
56
57     /* Z39.50v3 definitions */
58     {PROTO_Z3950,   CLASS_ABSYN,   VAL_APDU,      {2,1,-1},    "Z-APDU"      },
59     {PROTO_Z3950,   CLASS_APPCTX,  VAL_BASIC_CTX, {1,1,-1},    "Z-BASIC"     },
60     {PROTO_Z3950,   CLASS_ATTSET,  VAL_BIB1,      {3,1,-1},    "Bib-1"       },
61     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXP1,      {3,2,-1},    "Exp-1"       },
62     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXT1,      {3,3,-1},    "Ext-1"       },
63     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CCL1,      {3,4,-1},    "CCL-1"       },
64     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GILS,      {3,5,-1},    "GILS"        },
65     {PROTO_Z3950,   CLASS_ATTSET,  VAL_STAS,      {3,6,-1},    "STAS",       },
66     {PROTO_Z3950,   CLASS_DIAGSET, VAL_BIB1,      {4,1,-1},    "Bib-1"       },
67     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG1,     {4,2,-1},    "Diag-1"      },
68     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UNIMARC,   {5,1,-1},    "Unimarc"     },
69     {PROTO_Z3950,   CLASS_RECSYN,  VAL_INTERMARC, {5,2,-1},    "Intermarc"   },
70     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CCF,       {5,3,-1},    "CCF"         },
71     {PROTO_Z3950,   CLASS_RECSYN,  VAL_USMARC,    {5,10,-1},   "USmarc"      },
72     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UKMARC,    {5,11,-1},   "UKmarc"      },
73     {PROTO_Z3950,   CLASS_RECSYN,  VAL_NORMARC,   {5,12,-1},   "Normarc"     },
74     {PROTO_Z3950,   CLASS_RECSYN,  VAL_LIBRISMARC,{5,13,-1},   "Librismarc"  },
75     {PROTO_Z3950,   CLASS_RECSYN,  VAL_DANMARC,   {5,14,-1},   "Danmarc"     },
76     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FINMARC,   {5,15,-1},   "Finmarc"     },
77     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
78     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
79     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SBN,       {5,18,-1},   "SBN"         },
80     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PICAMARC,  {5,19,-1},   "Picamarc"    },
81     {PROTO_Z3950,   CLASS_RECSYN,  VAL_AUSMARC,   {5,20,-1},   "Ausmarc"     },
82     {PROTO_Z3950,   CLASS_RECSYN,  VAL_IBERMARC,  {5,21,-1},   "Ibermarc"    },
83     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXPLAIN,   {5,100,-1},  "Explain"     },
84     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUTRS,     {5,101,-1},  "SUTRS"       },
85     {PROTO_Z3950,   CLASS_RECSYN,  VAL_OPAC,      {5,102,-1},  "OPAC"        },
86     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUMMARY,   {5,103,-1},  "Summary"     },
87     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS0,      {5,104,-1},  "GRS-0"       },
88     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS1,      {5,105,-1},  "GRS-1"       },
89     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXTENDED,  {5,106,-1},  "Extended"    },
90     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},    "Resource-1"  },
91     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},    "Resource-2"  },
92     {PROTO_Z3950,   CLASS_ACCFORM, VAL_PROMPT1,   {8,1,-1},    "Prompt-1"    },
93     {PROTO_Z3950,   CLASS_ACCFORM, VAL_DES1,      {8,2,-1},    "Des-1"       },
94     {PROTO_Z3950,   CLASS_ACCFORM, VAL_KRB1,      {8,3,-1},    "Krb-1"       },
95     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PRESSET,   {9,1,-1},    "Pers. set"   },
96     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PQUERY,    {9,2,-1},    "Pers. query" },
97     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PCQUERY,   {9,3,-1},    "Per'd query" },
98     {PROTO_Z3950,   CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},    "Item order"  },
99     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE,  {9,5,-1},    "DB. Update"  },
100     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTSPEC,{9,6,-1},    "exp. spec."  },
101     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},    "exp. inv."   },
102
103     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETM,      {14,1,-1},   "TagsetM"     },
104     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETG,      {14,2,-1},   "TagsetG"     },
105     {PROTO_Z3950,   CLASS_TAGSET,  VAL_GILS,      {14,3,-1},   "GILS"        },
106
107     /* SR definitions. Note that some of them aren't defined by the
108         standard (yet), but are borrowed from Z3950v3 */
109     {PROTO_SR,      CLASS_ABSYN,   VAL_APDU,      {2,1,-1},    "SR-APDU"     },
110     {PROTO_SR,      CLASS_APPCTX,  VAL_BASIC_CTX, {1,1,-1},    "SR-BASIC"    },
111     {PROTO_SR,      CLASS_ATTSET,  VAL_BIB1,      {3,1,-1},    "Bib-1"       },
112     {PROTO_SR,      CLASS_ATTSET,  VAL_EXP1,      {3,2,-1},    "Exp-1"       },
113     {PROTO_SR,      CLASS_ATTSET,  VAL_EXT1,      {3,3,-1},    "Ext-1"       },
114     {PROTO_SR,      CLASS_ATTSET,  VAL_CCL1,      {3,4,-1},    "CCL-1"       },
115     {PROTO_SR,      CLASS_ATTSET,  VAL_GILS,      {3,5,-1},    "GILS"        },
116     {PROTO_SR,      CLASS_ATTSET,  VAL_STAS,      {3,6,-1},    "STAS",       },
117     {PROTO_SR,      CLASS_DIAGSET, VAL_BIB1,      {4,1,-1},    "Bib-1"       },
118     {PROTO_SR,      CLASS_DIAGSET, VAL_DIAG1,     {4,2,-1},    "Diag-1"      },
119     {PROTO_SR,      CLASS_RECSYN,  VAL_UNIMARC,   {5,1,-1},    "Unimarc"     },
120     {PROTO_SR,      CLASS_RECSYN,  VAL_INTERMARC, {5,2,-1},    "Intermarc"   },
121     {PROTO_SR,      CLASS_RECSYN,  VAL_CCF,       {5,3,-1},     "CCF"        },
122     {PROTO_SR,      CLASS_RECSYN,  VAL_USMARC,    {5,10,-1},   "USmarc"      },
123     {PROTO_SR,      CLASS_RECSYN,  VAL_UKMARC,    {5,11,-1},   "UKmarc"      },
124     {PROTO_SR,      CLASS_RECSYN,  VAL_NORMARC,   {5,12,-1},   "Normarc"     },
125     {PROTO_SR,      CLASS_RECSYN,  VAL_LIBRISMARC,{5,13,-1},   "Librismarc"  },
126     {PROTO_SR,      CLASS_RECSYN,  VAL_DANMARC,   {5,14,-1},   "Danmarc"     },
127     {PROTO_SR,      CLASS_RECSYN,  VAL_FINMARC,   {5,15,-1},   "Finmarc"     },
128     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
129     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
130     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
131     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
132     {PROTO_SR,      CLASS_RECSYN,  VAL_SBN,       {5,18,-1},   "SBN"         },
133     {PROTO_SR,      CLASS_RECSYN,  VAL_PICAMARC,  {5,19,-1},   "Picamarc"    },
134     {PROTO_SR,      CLASS_RECSYN,  VAL_AUSMARC,   {5,20,-1},   "Ausmarc"     },
135     {PROTO_SR,      CLASS_RECSYN,  VAL_IBERMARC,  {5,21,-1},   "Ibermarc"    },
136     {PROTO_SR,      CLASS_RECSYN,  VAL_EXPLAIN,   {5,100,-1},  "Explain"     },
137     {PROTO_SR,      CLASS_RECSYN,  VAL_SUTRS,     {5,101,-1},  "SUTRS"       },
138     {PROTO_SR,      CLASS_RECSYN,  VAL_OPAC,      {5,102,-1},  "OPAC"        },
139     {PROTO_SR,      CLASS_RECSYN,  VAL_SUMMARY,   {5,103,-1},  "Summary"     },
140     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS0,      {5,104,-1},  "GRS-0"       },
141     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS1,      {5,105,-1},  "GRS-1"       },
142     {PROTO_SR,      CLASS_RECSYN,  VAL_EXTENDED,  {5,106,-1},  "Extended"    },
143     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},    "Resource-1"  },
144     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},    "Resource-2"  },
145     {PROTO_SR,      CLASS_ACCFORM, VAL_PROMPT1,   {8,1,-1},    "Prompt-1"    },
146     {PROTO_SR,      CLASS_ACCFORM, VAL_DES1,      {8,2,-1},    "Des-1"       },
147     {PROTO_SR,      CLASS_ACCFORM, VAL_KRB1,      {8,3,-1},    "Krb-1"       },
148     {PROTO_SR,      CLASS_EXTSERV, VAL_PRESSET,   {9,1,-1},    "Pers. set"   },
149     {PROTO_SR,      CLASS_EXTSERV, VAL_PQUERY,    {9,2,-1},    "Pers. query" },
150     {PROTO_SR,      CLASS_EXTSERV, VAL_PCQUERY,   {9,3,-1},    "Per'd query" },
151     {PROTO_SR,      CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},    "Item order"  },
152     {PROTO_SR,      CLASS_EXTSERV, VAL_DBUPDATE,  {9,5,-1},    "DB. Update"  },
153     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTSPEC,{9,6,-1},    "exp. spec."  },
154     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},    "exp. inv."   },
155
156     {PROTO_SR,      CLASS_TAGSET,  VAL_SETM,      {14,1,-1},   "TagsetM"     },
157     {PROTO_SR,      CLASS_TAGSET,  VAL_SETG,      {14,2,-1},   "TagsetG"     },
158
159     {0,             0,             0,             {-1},        0          }
160 };
161
162 /* OID utilities */
163
164 void MDF oid_oidcpy(int *t, int *s)
165 {
166     while ((*(t++) = *(s++)) > -1);
167 }
168
169 void MDF oid_oidcat(int *t, int *s)
170 {
171     while (*t > -1)
172         t++;
173     while ((*(t++) = *(s++)) > -1);
174 }
175
176 int MDF oid_oidcmp(int *o1, int *o2)
177 {
178     while (*o1 == *o2 && *o1 > -1)
179     {
180         o1++;
181         o2++;
182     }
183     if (*o1 == *o2)
184         return 0;
185     else if (*o1 > *o2)
186         return 1;
187     else
188         return -1;
189 }
190
191 int MDF oid_oidlen(int *o)
192 {
193     int len = 0;
194
195     while (*(o++) >= 0)
196         len++;
197     return len;
198 }
199
200
201 static int match_prefix(int *look, int *prefix)
202 {
203     int len;
204
205     for (len = 0; *look == *prefix; look++, prefix++, len++);
206     if (*prefix < 0) /* did we reach the end of the prefix? */
207         return len;
208     return 0;
209 }
210
211 struct MDF oident *oid_getentbyoid(int *o)
212 {
213     enum oid_proto proto;
214     int prelen;
215     oident *p;
216
217     /* determine protocol type */
218     if (!o)
219         return 0;
220     if ((prelen = match_prefix(o, z3950_prefix)))
221         proto = PROTO_Z3950;
222     else if ((prelen = match_prefix(o, sr_prefix)))
223         proto = PROTO_SR;
224     else
225         proto = PROTO_GENERAL;
226     for (p = oids; *p->oidsuffix >= 0; p++)
227         if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
228             return p;
229     return 0;
230 }
231
232 /*
233  * To query, fill out proto, class, and value of the ent parameter.
234  */
235 int MDF *oid_getoidbyent(struct oident *ent)
236 {
237     struct oident *p;
238     static int ret[OID_SIZE];
239
240     for (p = oids; *p->oidsuffix >= 0; p++)
241         if (ent->proto == p->proto &&
242             ent->class == p->class &&
243             ent->value == p->value)
244         {
245             if (ent->proto == PROTO_Z3950)
246                 oid_oidcpy(ret, z3950_prefix);
247             else if (ent->proto == PROTO_SR)
248                 oid_oidcpy(ret, sr_prefix);
249             else
250                 ret[0] = -1;
251             oid_oidcat(ret, p->oidsuffix);
252             return ret;
253         }
254     return 0;
255 }
256
257 oid_value MDF oid_getvalbyname(char *name)
258 {
259     struct oident *p;
260
261     for (p = oids; *p->oidsuffix >= 0; p++)
262         if (!strcmp(p->desc, name))
263             return p->value;
264     return VAL_NONE;
265 }