Changed ES: Update OID
[yaz-moved-to-github.git] / util / oid.c
1 /*
2  * Copyright (c) 1995-1998, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: oid.c,v $
7  * Revision 1.32  1999-02-18 10:30:46  quinn
8  * Changed ES: Update OID
9  *
10  * Revision 1.31  1998/12/03 11:33:05  adam
11  * Added OID's for XML.
12  *
13  * Revision 1.30  1998/10/18 07:48:56  adam
14  * Fixed oid_getentbyoid so that it returns NULL when parsed oid is NULL.
15  *
16  * Revision 1.29  1998/10/14 13:32:35  adam
17  * Added include of string.h.
18  *
19  * Revision 1.28  1998/10/13 16:01:53  adam
20  * Implemented support for dynamic object identifiers.
21  * Function oid_getvalbyname now accepts raw OID's as well as traditional
22  * names.
23  *
24  * Revision 1.27  1998/05/18 10:10:02  adam
25  * Added Explain-schema and Explain-tagset to OID database.
26  *
27  * Revision 1.26  1998/03/20 14:46:06  adam
28  * Added UNIverse Resource Reports.
29  *
30  * Revision 1.25  1998/02/10 15:32:03  adam
31  * Added new Object Identifiers.
32  *
33  * Revision 1.24  1997/09/29 13:19:00  adam
34  * Added function, oid_ent_to_oid, to replace the function
35  * oid_getoidbyent, which is not thread safe.
36  *
37  * Revision 1.23  1997/09/09 10:10:19  adam
38  * Another MSV5.0 port. Changed projects to include proper
39  * library/include paths.
40  * Server starts server in test-mode when no options are given.
41  *
42  * Revision 1.22  1997/08/29 13:34:58  quinn
43  * Added thesaurus oids
44  *
45  * Revision 1.21  1997/08/19 08:46:05  quinn
46  * Added Thesaurus OID
47  *
48  * Revision 1.20  1997/07/28 12:34:43  adam
49  * Added new OID entries (RVDM).
50  *
51  * Revision 1.19  1997/05/02 08:39:41  quinn
52  * Support for private OID table added. Thanks to Ronald van der Meer
53  *
54  * Revision 1.18  1997/04/30 08:52:12  quinn
55  * Null
56  *
57  * Revision 1.17  1996/10/10  12:35:23  quinn
58  * Added Update extended service.
59  *
60  * Revision 1.16  1996/10/09  15:55:02  quinn
61  * Added SearchInfoReport
62  *
63  * Revision 1.15  1996/10/07  15:29:43  quinn
64  * Added SOIF support
65  *
66  * Revision 1.14  1996/02/20  17:58:28  adam
67  * Added const to oid_getvalbyname.
68  *
69  * Revision 1.13  1996/02/20  16:37:33  quinn
70  * Using yaz_matchstr in oid_getvalbyname
71  *
72  * Revision 1.12  1996/01/02  08:57:53  quinn
73  * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
74  *
75  * Revision 1.11  1995/12/13  16:03:35  quinn
76  * *** empty log message ***
77  *
78  * Revision 1.10  1995/11/28  09:30:44  quinn
79  * Work.
80  *
81  * Revision 1.9  1995/11/13  09:27:53  quinn
82  * Fiddling with the variant stuff.
83  *
84  * Revision 1.8  1995/10/12  10:34:56  quinn
85  * Added Espec-1.
86  *
87  * Revision 1.7  1995/10/10  16:27:12  quinn
88  * *** empty log message ***
89  *
90  * Revision 1.6  1995/09/29  17:12:35  quinn
91  * Smallish
92  *
93  * Revision 1.5  1995/09/29  17:01:51  quinn
94  * More Windows work
95  *
96  * Revision 1.4  1995/09/27  15:03:03  quinn
97  * Modified function heads & prototypes.
98  *
99  * Revision 1.3  1995/09/12  11:32:06  quinn
100  * Added a looker-upper by name.
101  *
102  * Revision 1.2  1995/08/21  09:11:16  quinn
103  * Smallish fixes to suppport new formats.
104  *
105  * Revision 1.1  1995/05/29  08:17:13  quinn
106  * iMoved oid to util to support comstack.
107  *
108  * Revision 1.5  1995/05/22  11:30:16  quinn
109  * Adding Z39.50-1992 stuff to proto.c. Adding zget.c
110  *
111  * Revision 1.4  1995/05/16  08:50:22  quinn
112  * License, documentation, and memory fixes
113  *
114  * Revision 1.3  1995/04/11  11:52:02  quinn
115  * Fixed possible buf in proto.c
116  *
117  * Revision 1.2  1995/03/29  15:39:38  quinn
118  * Adding some resource control elements, and a null-check to getentbyoid
119  *
120  * Revision 1.1  1995/03/27  08:32:12  quinn
121  * Added OID database
122  *
123  *
124  */
125
126 /*
127  * More or less protocol-transparent OID database.
128  * We could (and should?) extend this so that the user app can add new
129  * entries to the list at initialization.
130  */
131
132 #include <stdlib.h>
133 #include <string.h>
134 #include <ctype.h>
135
136 #include <oid.h>
137 #include <yaz-util.h>
138
139 static int z3950_prefix[] = { 1, 2, 840, 10003, -1 };
140 static int sr_prefix[]    = { 1, 0, 10163, -1 };
141
142 struct oident_list {
143     struct oident oident;
144     struct oident_list *next;
145 };
146
147 static struct oident_list *oident_table = NULL;
148 static int oid_value_dynamic = VAL_DYNAMIC;
149
150 /*
151  * OID database
152  */
153 static oident oids[] =
154 {
155     /* General definitions */
156     {PROTO_GENERAL, CLASS_TRANSYN, VAL_BER,       {2,1,1,-1},  "BER"         },
157     {PROTO_GENERAL, CLASS_TRANSYN, VAL_ISO2709,   {1,0,2709,1,1,-1},"ISO2709"},
158
159     /* Z39.50v3 definitions */
160     {PROTO_Z3950,   CLASS_ABSYN,   VAL_APDU,      {2,1,-1},    "Z-APDU"      },
161
162     {PROTO_Z3950,   CLASS_APPCTX,  VAL_BASIC_CTX, {1,1,-1},    "Z-BASIC"     },
163
164     {PROTO_Z3950,   CLASS_ATTSET,  VAL_BIB1,      {3,1,-1},    "Bib-1"       },
165     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXP1,      {3,2,-1},    "Exp-1"       },
166     {PROTO_Z3950,   CLASS_ATTSET,  VAL_EXT1,      {3,3,-1},    "Ext-1"       },
167     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CCL1,      {3,4,-1},    "CCL-1"       },
168     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GILS,      {3,5,-1},    "GILS-attset" },
169     {PROTO_Z3950,   CLASS_ATTSET,  VAL_STAS,      {3,6,-1},    "STAS-attset" },
170     {PROTO_Z3950,   CLASS_ATTSET,  VAL_COLLECT1,  {3,7,-1},    "Collections-attset"},
171     {PROTO_Z3950,   CLASS_ATTSET,  VAL_CIMI1,     {3,8,-1},    "CIMI-attset"},
172     {PROTO_Z3950,   CLASS_ATTSET,  VAL_GEO,       {3,9,-1},    "Geo-attset"},
173     {PROTO_Z3950,   CLASS_ATTSET,  VAL_THESAURUS, {3,1000,81,1,-1},"Thesaurus-attset"},
174     {PROTO_Z3950,   CLASS_DIAGSET, VAL_BIB1,      {4,1,-1},    "Bib-1"       },
175     {PROTO_Z3950,   CLASS_DIAGSET, VAL_DIAG1,     {4,2,-1},    "Diag-1"      },
176
177     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UNIMARC,   {5,1,-1},    "Unimarc"     },
178     {PROTO_Z3950,   CLASS_RECSYN,  VAL_INTERMARC, {5,2,-1},    "Intermarc"   },
179     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CCF,       {5,3,-1},    "CCF"         },
180     {PROTO_Z3950,   CLASS_RECSYN,  VAL_USMARC,    {5,10,-1},   "USmarc"      },
181     {PROTO_Z3950,   CLASS_RECSYN,  VAL_UKMARC,    {5,11,-1},   "UKmarc"      },
182     {PROTO_Z3950,   CLASS_RECSYN,  VAL_NORMARC,   {5,12,-1},   "Normarc"     },
183     {PROTO_Z3950,   CLASS_RECSYN,  VAL_LIBRISMARC,{5,13,-1},   "Librismarc"  },
184     {PROTO_Z3950,   CLASS_RECSYN,  VAL_DANMARC,   {5,14,-1},   "Danmarc"     },
185     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FINMARC,   {5,15,-1},   "Finmarc"     },
186     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
187     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
188     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SBN,       {5,18,-1},   "SBN"         },
189     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PICAMARC,  {5,19,-1},   "Picamarc"    },
190     {PROTO_Z3950,   CLASS_RECSYN,  VAL_AUSMARC,   {5,20,-1},   "Ausmarc"     },
191     {PROTO_Z3950,   CLASS_RECSYN,  VAL_IBERMARC,  {5,21,-1},   "Ibermarc"    },
192     {PROTO_Z3950,   CLASS_RECSYN,  VAL_CATMARC,   {5,22,-1},   "Carmarc"     },
193     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MALMARC,   {5,23,-1},   "Malmarc"     },
194     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXPLAIN,   {5,100,-1},  "Explain"     },
195     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUTRS,     {5,101,-1},  "SUTRS"       },
196     {PROTO_Z3950,   CLASS_RECSYN,  VAL_OPAC,      {5,102,-1},  "OPAC"        },
197     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SUMMARY,   {5,103,-1},  "Summary"     },
198     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS0,      {5,104,-1},  "GRS-0"       },
199     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GRS1,      {5,105,-1},  "GRS-1"       },
200     {PROTO_Z3950,   CLASS_RECSYN,  VAL_EXTENDED,  {5,106,-1},  "Extended"    },
201     {PROTO_Z3950,   CLASS_RECSYN,  VAL_FRAGMENT,  {5,107,-1},  "Fragment"    },
202
203     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PDF,       {5,109,1,-1},"pdf"         },
204     {PROTO_Z3950,   CLASS_RECSYN,  VAL_POSTSCRIPT,{5,109,2,-1},"postscript"  },
205     {PROTO_Z3950,   CLASS_RECSYN,  VAL_HTML,      {5,109,3,-1},"html"        },
206     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TIFF,      {5,109,4,-1},"tiff"        },
207     {PROTO_Z3950,   CLASS_RECSYN,  VAL_GIF,       {5,109,5,-1},"gif"         },
208     {PROTO_Z3950,   CLASS_RECSYN,  VAL_JPEG,      {5,109,6,-1},"jpeg"        },
209     {PROTO_Z3950,   CLASS_RECSYN,  VAL_PNG,       {5,109,7,-1},"png"         },
210     {PROTO_Z3950,   CLASS_RECSYN,  VAL_MPEG,      {5,109,8,-1},"mpeg"        },
211     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SGML,      {5,109,9,-1},"sgml"        },
212
213     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TIFFB,     {5,110,1,-1},"tiff-b"      },
214     {PROTO_Z3950,   CLASS_RECSYN,  VAL_WAV,       {5,110,2,-1},"wav"         },
215
216     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SQLRS,     {5,111,-1},  "SQL-RS"      },
217 #if 0
218     {PROTO_Z3950,   CLASS_RECSYN,  VAL_ID_SGML,   {5,1000,81,1,-1},"ID-SGML" },
219 #endif
220     {PROTO_Z3950,   CLASS_RECSYN,  VAL_SOIF,      {5,1000,81,2,-1},"SOIF" },
221     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TEXT_XML,  {5,109,10,-1}, "text-XML" },
222     {PROTO_Z3950,   CLASS_RECSYN,  VAL_TEXT_XML,  {5,109,10,-1}, "XML" },
223     {PROTO_Z3950,   CLASS_RECSYN,  VAL_APPLICATION_XML,
224           {5,109,11,-1}, "application-XML" },
225     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},    "Resource-1"  },
226     {PROTO_Z3950,   CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},    "Resource-2"  },
227     {PROTO_Z3950,   CLASS_RESFORM, VAL_UNIVERSE_REPORT,  {7,1000,81,1,-1}, "UNIverse-Resource-Report"},
228
229     {PROTO_Z3950,   CLASS_ACCFORM, VAL_PROMPT1,   {8,1,-1},    "Prompt-1"    },
230     {PROTO_Z3950,   CLASS_ACCFORM, VAL_DES1,      {8,2,-1},    "Des-1"       },
231     {PROTO_Z3950,   CLASS_ACCFORM, VAL_KRB1,      {8,3,-1},    "Krb-1"       },
232     
233     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PRESSET,   {9,1,-1},    "Pers. set"   },
234     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PQUERY,    {9,2,-1},    "Pers. query" },
235     {PROTO_Z3950,   CLASS_EXTSERV, VAL_PCQUERY,   {9,3,-1},    "Per'd query" },
236     {PROTO_Z3950,   CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},    "Item order"  },
237     {PROTO_Z3950,   CLASS_EXTSERV, VAL_DBUPDATE,  {9,5,1,1,-1},    "DB. Update"  },
238     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTSPEC,{9,6,-1},    "exp. spec."  },
239     {PROTO_Z3950,   CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},    "exp. inv."   },
240
241     {PROTO_Z3950,   CLASS_USERINFO,VAL_SEARCHRES1,{10,1,-1},   "searchResult-1"},
242     {PROTO_Z3950,   CLASS_USERINFO,VAL_CHARLANG,  {10,2,-1},   "CharSetandLanguageNegotiation"},
243     {PROTO_Z3950,   CLASS_USERINFO,VAL_USERINFO1, {10,3,-1},   "UserInfo-1"},
244     {PROTO_Z3950,   CLASS_USERINFO,VAL_MULTISRCH1,{10,4,-1},   "MultipleSearchTerms-1"},
245     {PROTO_Z3950,   CLASS_USERINFO,VAL_MULTISRCH2,{10,5,-1},   "MultipleSearchTerms-2"},
246     {PROTO_Z3950,   CLASS_USERINFO,VAL_DATETIME,  {10,6,-1},   "DateTime"},
247
248     {PROTO_Z3950,   CLASS_ELEMSPEC,VAL_ESPEC1,    {11,1,-1},   "Espec-1"     },
249
250     {PROTO_Z3950,   CLASS_VARSET,  VAL_VAR1,      {12,1,-1},   "Variant-1"   },
251
252     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_WAIS,      {13,1,-1},   "WAIS-schema" },
253     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_GILS,      {13,2,-1},   "GILS-schema" },
254     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_COLLECT1,  {13,3,-1},   "Collections-schema" },
255     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_GEO,       {13,4,-1},   "Geo-schema" },
256     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_CIMI1,     {13,5,-1},   "CIMI-schema" },
257     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_UPDATEES,  {13,6,-1},   "Update ES" },
258     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_THESAURUS, {13,1000,81,1,-1}, "thesaurus-schema"},
259     {PROTO_Z3950,   CLASS_SCHEMA,  VAL_EXPLAIN,   {13,1000,81,2,-1}, "Explain-schema"},
260     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETM,      {14,1,-1},   "TagsetM"     },
261     {PROTO_Z3950,   CLASS_TAGSET,  VAL_SETG,      {14,2,-1},   "TagsetG"     },
262     {PROTO_Z3950,   CLASS_TAGSET,  VAL_STAS,      {14,3,-1},   "STAS-tagset" },
263     {PROTO_Z3950,   CLASS_TAGSET,  VAL_GILS,      {14,4,-1},   "GILS-tagset" },
264     {PROTO_Z3950,   CLASS_TAGSET,  VAL_COLLECT1,  {14,5,-1},   "Collections-tagset"},
265     {PROTO_Z3950,   CLASS_TAGSET,  VAL_CIMI1,     {14,6,-1},   "CIMI-tagset" },
266     {PROTO_Z3950,   CLASS_TAGSET,  VAL_THESAURUS, {14,1000,81,1,-1}, "thesaurus-tagset"},
267     {PROTO_Z3950,   CLASS_TAGSET,  VAL_EXPLAIN,   {14,1000,81,2,-1}, "Explain-tagset"},
268     
269
270     /* SR definitions. Note that some of them aren't defined by the
271         standard (yet), but are borrowed from Z3950v3 */
272     {PROTO_SR,      CLASS_ABSYN,   VAL_APDU,      {2,1,-1},    "SR-APDU"     },
273
274     {PROTO_SR,      CLASS_APPCTX,  VAL_BASIC_CTX, {1,1,-1},    "SR-BASIC"    },
275
276     {PROTO_SR,      CLASS_ATTSET,  VAL_BIB1,      {3,1,-1},    "Bib-1"       },
277     {PROTO_SR,      CLASS_ATTSET,  VAL_EXP1,      {3,2,-1},    "Exp-1"       },
278     {PROTO_SR,      CLASS_ATTSET,  VAL_EXT1,      {3,3,-1},    "Ext-1"       },
279     {PROTO_SR,      CLASS_ATTSET,  VAL_CCL1,      {3,4,-1},    "CCL-1"       },
280     {PROTO_SR,      CLASS_ATTSET,  VAL_GILS,      {3,5,-1},    "GILS"        },
281     {PROTO_SR,      CLASS_ATTSET,  VAL_STAS,      {3,6,-1},    "STAS",       },
282     {PROTO_SR,      CLASS_ATTSET,  VAL_COLLECT1,  {3,7,-1},    "Collections-attset"},
283     {PROTO_SR,      CLASS_ATTSET,  VAL_CIMI1,     {3,8,-1},    "CIMI-attset"},
284     {PROTO_SR,      CLASS_ATTSET,  VAL_GEO,       {3,9,-1},    "Geo-attset"},
285
286     {PROTO_SR,      CLASS_DIAGSET, VAL_BIB1,      {4,1,-1},    "Bib-1"       },
287     {PROTO_SR,      CLASS_DIAGSET, VAL_DIAG1,     {4,2,-1},    "Diag-1"      },
288
289     {PROTO_SR,      CLASS_RECSYN,  VAL_UNIMARC,   {5,1,-1},    "Unimarc"     },
290     {PROTO_SR,      CLASS_RECSYN,  VAL_INTERMARC, {5,2,-1},    "Intermarc"   },
291     {PROTO_SR,      CLASS_RECSYN,  VAL_CCF,       {5,3,-1},    "CCF"        },
292     {PROTO_SR,      CLASS_RECSYN,  VAL_USMARC,    {5,10,-1},   "USmarc"      },
293     {PROTO_SR,      CLASS_RECSYN,  VAL_UKMARC,    {5,11,-1},   "UKmarc"      },
294     {PROTO_SR,      CLASS_RECSYN,  VAL_NORMARC,   {5,12,-1},   "Normarc"     },
295     {PROTO_SR,      CLASS_RECSYN,  VAL_LIBRISMARC,{5,13,-1},   "Librismarc"  },
296     {PROTO_SR,      CLASS_RECSYN,  VAL_DANMARC,   {5,14,-1},   "Danmarc"     },
297     {PROTO_SR,      CLASS_RECSYN,  VAL_FINMARC,   {5,15,-1},   "Finmarc"     },
298     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
299     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
300     {PROTO_SR,      CLASS_RECSYN,  VAL_MAB,       {5,16,-1},   "MAB"         },
301     {PROTO_SR,      CLASS_RECSYN,  VAL_CANMARC,   {5,17,-1},   "Canmarc"     },
302     {PROTO_SR,      CLASS_RECSYN,  VAL_SBN,       {5,18,-1},   "SBN"         },
303     {PROTO_SR,      CLASS_RECSYN,  VAL_PICAMARC,  {5,19,-1},   "Picamarc"    },
304     {PROTO_SR,      CLASS_RECSYN,  VAL_AUSMARC,   {5,20,-1},   "Ausmarc"     },
305     {PROTO_SR,      CLASS_RECSYN,  VAL_IBERMARC,  {5,21,-1},   "Ibermarc"    },
306     {PROTO_SR,      CLASS_RECSYN,  VAL_CATMARC,   {5,22,-1},   "Catmarc"     },
307     {PROTO_SR,      CLASS_RECSYN,  VAL_MALMARC,   {5,23,-1},   "Malmarc"     },
308     {PROTO_SR,      CLASS_RECSYN,  VAL_EXPLAIN,   {5,100,-1},  "Explain"     },
309     {PROTO_SR,      CLASS_RECSYN,  VAL_SUTRS,     {5,101,-1},  "SUTRS"       },
310     {PROTO_SR,      CLASS_RECSYN,  VAL_OPAC,      {5,102,-1},  "OPAC"        },
311     {PROTO_SR,      CLASS_RECSYN,  VAL_SUMMARY,   {5,103,-1},  "Summary"     },
312     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS0,      {5,104,-1},  "GRS-0"       },
313     {PROTO_SR,      CLASS_RECSYN,  VAL_GRS1,      {5,105,-1},  "GRS-1"       },
314     {PROTO_SR,      CLASS_RECSYN,  VAL_EXTENDED,  {5,106,-1},  "Extended"    },
315     {PROTO_SR,      CLASS_RECSYN,  VAL_FRAGMENT,  {5,107,-1},  "Fragment"    },
316
317     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE1, {7,1,-1},    "Resource-1"  },
318     {PROTO_SR,      CLASS_RESFORM, VAL_RESOURCE2, {7,2,-1},    "Resource-2"  },
319
320     {PROTO_SR,      CLASS_ACCFORM, VAL_PROMPT1,   {8,1,-1},    "Prompt-1"    },
321     {PROTO_SR,      CLASS_ACCFORM, VAL_DES1,      {8,2,-1},    "Des-1"       },
322     {PROTO_SR,      CLASS_ACCFORM, VAL_KRB1,      {8,3,-1},    "Krb-1"       },
323
324     {PROTO_SR,      CLASS_EXTSERV, VAL_PRESSET,   {9,1,-1},    "Pers. set"   },
325     {PROTO_SR,      CLASS_EXTSERV, VAL_PQUERY,    {9,2,-1},    "Pers. query" },
326     {PROTO_SR,      CLASS_EXTSERV, VAL_PCQUERY,   {9,3,-1},    "Per'd query" },
327     {PROTO_SR,      CLASS_EXTSERV, VAL_ITEMORDER, {9,4,-1},    "Item order"  },
328     {PROTO_SR,      CLASS_EXTSERV, VAL_DBUPDATE,  {9,5,-1},    "DB. Update"  },
329     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTSPEC,{9,6,-1},    "exp. spec."  },
330     {PROTO_SR,      CLASS_EXTSERV, VAL_EXPORTINV, {9,7,-1},    "exp. inv."   },
331
332     {PROTO_SR,      CLASS_ELEMSPEC,VAL_ESPEC1,    {11,1,-1},   "Espec-1"     },
333
334     {PROTO_SR,      CLASS_VARSET,  VAL_VAR1,      {12,1,-1},   "Variant-1"   },
335
336     {PROTO_SR,      CLASS_SCHEMA,  VAL_WAIS,      {13,1,-1},   "WAIS-schema" },
337     {PROTO_SR,      CLASS_SCHEMA,  VAL_GILS,      {13,2,-1},   "GILS-schema" },
338     {PROTO_SR,      CLASS_SCHEMA,  VAL_COLLECT1,  {13,3,-1},   "Collections-schema" },
339     {PROTO_SR,      CLASS_SCHEMA,  VAL_GEO,       {13,4,-1},   "Geo-schema" },
340     {PROTO_SR,      CLASS_SCHEMA,  VAL_CIMI1,     {13,5,-1},   "CIMI-schema" },
341
342     {PROTO_SR,      CLASS_TAGSET,  VAL_SETM,      {14,1,-1},   "TagsetM"     },
343     {PROTO_SR,      CLASS_TAGSET,  VAL_SETG,      {14,2,-1},   "TagsetG"     },
344
345     {PROTO_SR,      CLASS_TAGSET,  VAL_STAS,      {14,3,-1},   "STAS-tagset" },
346     {PROTO_SR,      CLASS_TAGSET,  VAL_GILS,      {14,4,-1},   "GILS-tagset" },
347     {PROTO_SR,      CLASS_TAGSET,  VAL_COLLECT1,  {14,5,-1},   "Collections-tagset"},
348     {PROTO_SR,      CLASS_TAGSET,  VAL_CIMI1,     {14,6,-1},   "CIMI-tagset" },
349
350     {PROTO_NOP,     CLASS_NOP,     VAL_NOP,       {-1},        0          }
351 };
352
353 /* OID utilities */
354
355 void oid_oidcpy(int *t, int *s)
356 {
357     while ((*(t++) = *(s++)) > -1);
358 }
359
360 void oid_oidcat(int *t, int *s)
361 {
362     while (*t > -1)
363         t++;
364     while ((*(t++) = *(s++)) > -1);
365 }
366
367 int oid_oidcmp(int *o1, int *o2)
368 {
369     while (*o1 == *o2 && *o1 > -1)
370     {
371         o1++;
372         o2++;
373     }
374     if (*o1 == *o2)
375         return 0;
376     else if (*o1 > *o2)
377         return 1;
378     else
379         return -1;
380 }
381
382 int oid_oidlen(int *o)
383 {
384     int len = 0;
385
386     while (*(o++) >= 0)
387         len++;
388     return len;
389 }
390
391
392 static int match_prefix(int *look, int *prefix)
393 {
394     int len;
395
396     for (len = 0; *look == *prefix; look++, prefix++, len++);
397     if (*prefix < 0) /* did we reach the end of the prefix? */
398         return len;
399     return 0;
400 }
401
402 void oid_transfer (struct oident *oident)
403 {
404     while (*oident->oidsuffix >= 0)
405     {
406         oid_addent (oident->oidsuffix, oident->proto,
407                     oident->oclass,
408                     oident->desc, oident->value);
409         oident++;
410     }
411 }
412
413 static void oid_init (void)
414 {
415     static int checked = 0;
416     
417     if (checked)
418         return;
419     oid_transfer (oids);
420     checked = 1;
421 }
422
423
424 static struct oident *oid_getentbyoid_x(int *o)
425 {
426     enum oid_proto proto;
427     int prelen;
428     struct oident_list *ol;
429     
430     /* determine protocol type */
431     if ((prelen = match_prefix(o, z3950_prefix)) != 0)
432         proto = PROTO_Z3950;
433     else if ((prelen = match_prefix(o, sr_prefix)) != 0)
434         proto = PROTO_SR;
435     else
436         proto = PROTO_GENERAL;
437     for (ol = oident_table; ol; ol = ol->next)
438     {
439         struct oident *p = &ol->oident;
440         if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
441             return p;
442         if (p->proto == PROTO_GENERAL && !oid_oidcmp (o, p->oidsuffix))
443             return p;
444     }
445     return 0;
446 }
447
448 /*
449  * To query, fill out proto, class, and value of the ent parameter.
450  */
451 int *oid_ent_to_oid(struct oident *ent, int *ret)
452 {
453     struct oident_list *ol;
454     
455     oid_init ();
456     for (ol = oident_table; ol; ol = ol->next)
457     {
458         struct oident *p = &ol->oident;
459         if ((ent->proto == p->proto || p->proto == PROTO_GENERAL) &&
460             (ent->oclass == p->oclass || p->oclass == CLASS_GENERAL) &&
461             ent->value == p->value)
462         {
463             if (p->proto == PROTO_Z3950)
464                 oid_oidcpy(ret, z3950_prefix);
465             else if (p->proto == PROTO_SR)
466                 oid_oidcpy(ret, sr_prefix);
467             else
468                 ret[0] = -1;
469             oid_oidcat(ret, p->oidsuffix);
470             ent->desc = p->desc;
471             return ret;
472         }
473     }
474     ret[0] = -1;
475     return 0;
476 }
477
478 /*
479  * To query, fill out proto, class, and value of the ent parameter.
480  */
481 int *oid_getoidbyent(struct oident *ent)
482 {
483     static int ret[OID_SIZE];
484
485     return oid_ent_to_oid (ent, ret);
486 }
487
488 struct oident *oid_addent (int *oid, int proto, int oclass,
489                            const char *desc, int value)
490 {
491     struct oident *oident;
492
493     nmem_critical_enter ();
494     oident = oid_getentbyoid_x (oid);
495     if (!oident)
496     {
497         char desc_str[200];
498         struct oident_list *oident_list;
499         oident_list = (struct oident_list *) malloc (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; oid[i] >= 0; i++)
510                 sprintf (desc_str+strlen(desc_str), ".%d", oid[i]);
511             desc = desc_str;
512         }
513         oident->desc = (char *) malloc (strlen(desc)+1);
514         strcpy (oident->desc, desc);
515         if (value == VAL_DYNAMIC)
516             oident->value = ++oid_value_dynamic;
517         else
518             oident->value = value;
519         oid_oidcpy (oident->oidsuffix, oid);
520         oident_list->next = oident_table;
521         oident_table = oident_list;
522     }
523     nmem_critical_leave ();
524     return oident;
525 }
526
527 struct oident *oid_getentbyoid(int *oid)
528 {
529     struct oident *oident;
530
531     if (!oid)
532         return 0;
533     oid_init ();
534     oident = oid_getentbyoid_x (oid);
535     if (!oident)
536         oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL,
537                              NULL, VAL_DYNAMIC);
538     return oident;
539 }
540
541 static oid_value oid_getval_raw(const char *name)
542 {
543     int val = 0, i = 0, oid[OID_SIZE];
544     struct oident *oident;
545     
546     while (isdigit (*name))
547     {
548         val = val*10 + (*name - '0');
549         name++;
550         if (*name == '.')
551         {
552             if (i < OID_SIZE-1)
553                 oid[i++] = val;
554             val = 0;
555             name++;
556         }
557     }
558     oid[i] = val;
559     oid[i+1] = -1;
560     oident = oid_addent (oid, PROTO_GENERAL, CLASS_GENERAL, NULL,
561                          VAL_DYNAMIC);
562     return oident->value;
563 }
564
565 oid_value oid_getvalbyname(const char *name)
566 {
567     struct oident_list *ol;
568
569     oid_init ();
570     if (isdigit (*name))
571         return oid_getval_raw (name);
572     for (ol = oident_table; ol; ol = ol->next)
573         if (!yaz_matchstr(ol->oident.desc, name))
574         {
575             return ol->oident.value;
576         }
577     return VAL_NONE;
578 }
579
580 void oid_setprivateoids(oident *list)
581 {
582     oid_transfer (list);
583 }