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