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