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