New function yaz_strerror.
[yaz-moved-to-github.git] / z39.50 / prt-ext.c
1 /*
2  * Copyright (c) 1995-2002, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Id: prt-ext.c,v 1.4 2002-12-05 12:07:00 adam Exp $
6  */
7
8 #include <yaz/proto.h>
9
10 /*
11  * The table below should be moved to the ODR structure itself and
12  * be an image of the association context: To help
13  * map indirect references when they show up. 
14  */
15 static Z_ext_typeent type_table[] =
16 {
17     {VAL_SUTRS, Z_External_sutrs, (Odr_fun) z_SUTRS},
18     {VAL_EXPLAIN, Z_External_explainRecord, (Odr_fun)z_ExplainRecord},
19     {VAL_RESOURCE1, Z_External_resourceReport1, (Odr_fun)z_ResourceReport1},
20     {VAL_RESOURCE2, Z_External_resourceReport2, (Odr_fun)z_ResourceReport2},
21     {VAL_PROMPT1, Z_External_promptObject1, (Odr_fun)z_PromptObject1 },
22     {VAL_GRS1, Z_External_grs1, (Odr_fun)z_GenericRecord},
23     {VAL_EXTENDED, Z_External_extendedService, (Odr_fun)z_TaskPackage},
24     {VAL_ITEMORDER, Z_External_itemOrder, (Odr_fun)z_IOItemOrder},
25     {VAL_DIAG1, Z_External_diag1, (Odr_fun)z_DiagnosticFormat},
26     {VAL_ESPEC1, Z_External_espec1, (Odr_fun)z_Espec1},
27     {VAL_SUMMARY, Z_External_summary, (Odr_fun)z_BriefBib},
28     {VAL_OPAC, Z_External_OPAC, (Odr_fun)z_OPACRecord},
29     {VAL_SEARCHRES1, Z_External_searchResult1, (Odr_fun)z_SearchInfoReport},
30     {VAL_DBUPDATE, Z_External_update, (Odr_fun)z_IUUpdate},
31     {VAL_DATETIME, Z_External_dateTime, (Odr_fun)z_DateTime},
32     {VAL_UNIVERSE_REPORT, Z_External_universeReport,(Odr_fun)z_UniverseReport},
33     {VAL_ADMINSERVICE, Z_External_ESAdmin, (Odr_fun)z_Admin},
34     {VAL_USERINFO1, Z_External_userInfo1, (Odr_fun) z_OtherInformation},
35     {VAL_CHARNEG3, Z_External_charSetandLanguageNegotiation, (Odr_fun)
36                   z_CharSetandLanguageNegotiation},
37     {VAL_PROMPT1, Z_External_acfPrompt1, (Odr_fun) z_PromptObject1},
38     {VAL_DES1, Z_External_acfDes1, (Odr_fun) z_DES_RN_Object},
39     {VAL_KRB1, Z_External_acfKrb1, (Odr_fun) z_KRBObject},
40     {VAL_MULTISRCH2, Z_External_multisrch2, (Odr_fun) z_MultipleSearchTerms_2},
41     {VAL_NONE, 0, 0}
42 };
43
44 Z_ext_typeent *z_ext_getentbyref(oid_value val)
45 {
46     Z_ext_typeent *i;
47
48     for (i = type_table; i->dref != VAL_NONE; i++)
49         if (i->dref == val)
50             return i;
51     return 0;
52 }
53
54 int z_External(ODR o, Z_External **p, int opt, const char *name)
55 {
56     oident *oid;
57     Z_ext_typeent *type;
58
59     static Odr_arm arm[] =
60     {
61         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_single,
62          (Odr_fun)odr_any, 0},
63         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_External_octet,
64          (Odr_fun)odr_octetstring, 0},
65         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_External_arbitrary,
66          (Odr_fun)odr_bitstring, 0},
67         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_sutrs,
68          (Odr_fun)z_SUTRS, 0},
69         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_explainRecord,
70          (Odr_fun)z_ExplainRecord, 0},
71
72         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport1,
73          (Odr_fun)z_ResourceReport1, 0},
74         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport2,
75          (Odr_fun)z_ResourceReport2, 0},
76         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_promptObject1,
77          (Odr_fun)z_PromptObject1, 0},
78         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_grs1,
79          (Odr_fun)z_GenericRecord, 0},
80         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_extendedService,
81          (Odr_fun)z_TaskPackage, 0},
82
83         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_itemOrder,
84          (Odr_fun)z_IOItemOrder, 0},
85         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_diag1,
86          (Odr_fun)z_DiagnosticFormat, 0},
87         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_espec1,
88          (Odr_fun)z_Espec1, 0},
89         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_summary,
90          (Odr_fun)z_BriefBib, 0},
91         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OPAC,
92          (Odr_fun)z_OPACRecord, 0},
93
94         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_searchResult1,
95          (Odr_fun)z_SearchInfoReport, 0},
96         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update,
97          (Odr_fun)z_IUUpdate, 0},
98         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_dateTime,
99          (Odr_fun)z_DateTime, 0},
100         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_universeReport,
101          (Odr_fun)z_UniverseReport, 0},
102         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_ESAdmin,
103          (Odr_fun)z_Admin, 0},
104
105         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update0,
106          (Odr_fun)z_IU0Update, 0},
107         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_userInfo1,
108          (Odr_fun)z_OtherInformation, 0},
109         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_charSetandLanguageNegotiation,
110          (Odr_fun)z_CharSetandLanguageNegotiation, 0},
111         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfPrompt1,
112          (Odr_fun)z_PromptObject1, 0},
113         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfDes1,
114          (Odr_fun)z_DES_RN_Object, 0},
115
116         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfKrb1,
117          (Odr_fun)z_KRBObject, 0},
118         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_multisrch2,
119          (Odr_fun)z_MultipleSearchTerms_2, 0},
120         {-1, -1, -1, -1, 0, 0}
121     };
122     
123     odr_implicit_settag(o, ODR_UNIVERSAL, ODR_EXTERNAL);
124     if (!odr_sequence_begin(o, p, sizeof(**p), name))
125         return opt && odr_ok(o);
126     if (!(odr_oid(o, &(*p)->direct_reference, 1, 0) &&
127           odr_integer(o, &(*p)->indirect_reference, 1, 0) &&
128           odr_graphicstring(o, &(*p)->descriptor, 1, 0)))
129         return 0;
130     /*
131      * Do we know this beast?
132      */
133     if (o->direction == ODR_DECODE && (*p)->direct_reference &&
134         (oid = oid_getentbyoid((*p)->direct_reference)) &&
135         (type = z_ext_getentbyref(oid->value)))
136     {
137         int zclass, tag, cons;
138         
139         /*
140          * We know it. If it's represented as an ASN.1 type, bias the CHOICE.
141          */
142         if (!odr_peektag(o, &zclass, &tag, &cons))
143             return opt && odr_ok(o);
144         if (zclass == ODR_CONTEXT && tag == 0 && cons == 1)
145             odr_choice_bias(o, type->what);
146     }
147     return
148         odr_choice(o, arm, &(*p)->u, &(*p)->which, name) &&
149         odr_sequence_end(o);
150 }
151
152 Z_External *z_ext_record(ODR o, int format, const char *buf, int len)
153 {
154     Z_External *thisext;
155
156     thisext = (Z_External *) odr_malloc(o, sizeof(*thisext));
157     thisext->descriptor = 0;
158     thisext->indirect_reference = 0;
159
160     thisext->direct_reference = 
161         yaz_oidval_to_z3950oid (o, CLASS_RECSYN, format);    
162     if (!thisext->direct_reference)
163         return 0;
164
165     if (len < 0) /* Structured data */
166     {
167         
168         /*
169          * We cheat on the pointers here. Obviously, the record field
170          * of the backend-fetch structure should have been a union for
171          * correctness, but we're stuck with this for backwards
172          * compatibility.
173          */
174         thisext->u.grs1 = (Z_GenericRecord*) buf;
175
176         switch (format)
177         {
178         case VAL_SUTRS:
179             thisext->which = Z_External_sutrs;
180             break;
181         case VAL_GRS1:
182             thisext->which = Z_External_grs1;
183             break;
184         case VAL_EXPLAIN:
185             thisext->which = Z_External_explainRecord;
186             break;
187         case VAL_SUMMARY:
188             thisext->which = Z_External_summary;
189             break;
190         case VAL_OPAC:
191             thisext->which = Z_External_OPAC;
192             break;
193         case VAL_EXTENDED:
194             thisext->which = Z_External_extendedService;
195             break;
196         default:
197             return 0;
198         }
199     }
200     else if (format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
201     {
202         Odr_oct *sutrs = (Odr_oct *)odr_malloc(o, sizeof(*sutrs));
203         
204         thisext->which = Z_External_sutrs;
205         thisext->u.sutrs = sutrs;
206         sutrs->buf = (unsigned char *)odr_malloc(o, len);
207         sutrs->len = sutrs->size = len;
208         memcpy(sutrs->buf, buf, len);
209     }
210     else
211     {
212         thisext->which = Z_External_octet;
213         if (!(thisext->u.octet_aligned = (Odr_oct *)
214               odr_malloc(o, sizeof(Odr_oct))))
215             return 0;
216         if (!(thisext->u.octet_aligned->buf = (unsigned char *)
217               odr_malloc(o, len)))
218             return 0;
219         memcpy(thisext->u.octet_aligned->buf, buf, len);
220         thisext->u.octet_aligned->len = thisext->u.octet_aligned->size = len;
221     }
222     return thisext;
223 }
224