Accept application/soap+xml as content-type for SOAP msg
[yaz-moved-to-github.git] / src / charneg.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: charneg.c,v 1.5 2005-06-25 15:46:03 adam Exp $
6  */
7
8 /** 
9  * \file charneg.c
10  * \brief Implements Z39.50 Charset negotiation utilities
11  *
12  * Helper functions for Character Set and Language Negotiation - 3
13  */
14 #include <stdio.h>
15 #include <yaz/otherinfo.h>
16 #include <yaz/z-charneg.h>
17 #include <yaz/charneg.h>
18 #include <yaz/yaz-util.h>
19
20 static Z_External* z_ext_record2(ODR o, int oid_class, int oid_value,
21                                  const char *buf)
22 {
23     Z_External *p;
24     oident oid;
25     int len = strlen(buf);
26     
27     if (!(p = (Z_External *)odr_malloc(o, sizeof(*p)))) return 0;
28     
29     p->descriptor = 0;
30     p->indirect_reference = 0;
31     
32     oid.proto = PROTO_Z3950;
33     oid.oclass = (enum oid_class) oid_class;
34     oid.value = (enum oid_value) oid_value;
35     p->direct_reference = odr_oiddup(o, oid_getoidbyent(&oid));
36     
37     p->which = Z_External_octet;
38     if (!(p->u.octet_aligned = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)))) {
39         return 0;
40     }
41     if (!(p->u.octet_aligned->buf = (unsigned char *)odr_malloc(o, len))) {
42         return 0;
43     }
44     p->u.octet_aligned->len = p->u.octet_aligned->size = len;
45     memcpy(p->u.octet_aligned->buf, buf, len);
46         
47     return p;
48 }
49
50 static int get_form(const char *charset)
51 {
52     int form = -1;
53
54
55     if (!yaz_matchstr(charset, "UCS-2"))
56         form = 2;
57     if (!yaz_matchstr(charset, "UCS-4"))
58         form = 4;
59     if (!yaz_matchstr(charset, "UTF-16"))
60         form = 5;
61     if (!yaz_matchstr(charset, "UTF-8"))
62         form = 8;
63
64     return form;
65 }
66
67 static char *set_form (Odr_oid *encoding)
68 {
69     static char *charset = 0;
70     if ( oid_oidlen(encoding) != 6)
71         return 0;
72     if (encoding[5] == 2)
73         charset = "UCS-2";
74     if (encoding[5] == 4)
75         charset = "UCS-4";
76     if (encoding[5] == 5)
77         charset = "UTF-16";
78     if (encoding[5] == 8)
79         charset = "UTF-8";
80     return charset;
81 }
82
83 static Z_OriginProposal_0 *z_get_OriginProposal_0(ODR o, const char *charset)
84 {
85     int form = get_form (charset);
86     Z_OriginProposal_0 *p0 =
87         (Z_OriginProposal_0*)odr_malloc(o, sizeof(*p0));
88
89     memset(p0, 0, sizeof(*p0));
90
91     if (form > 0)
92     {   /* ISO 10646 (UNICODE) */
93         char oidname[20];
94
95         Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
96         p0->which = Z_OriginProposal_0_iso10646;
97         p0->u.iso10646 = is;
98         is->collections = 0;
99         sprintf (oidname, "1.0.10646.1.0.%d", form);
100         is->encodingLevel = odr_getoidbystr (o, oidname);
101     }
102     else
103     {   /* private ones */
104         Z_PrivateCharacterSet *pc =
105             (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
106
107         memset(pc, 0, sizeof(*pc));
108         
109         p0->which = Z_OriginProposal_0_private;
110         p0->u.zprivate = pc;
111         
112         pc->which = Z_PrivateCharacterSet_externallySpecified;
113         pc->u.externallySpecified =
114             z_ext_record2(o, CLASS_NEGOT, VAL_ID_CHARSET, charset);
115     }
116     return p0;
117 }
118
119 static Z_OriginProposal *z_get_OriginProposal(
120     ODR o, const char **charsets, int num_charsets,
121     const char **langs, int num_langs, int selected)
122 {       
123     int i;
124     Z_OriginProposal *p = (Z_OriginProposal *) odr_malloc(o, sizeof(*p));
125                 
126     memset(p, 0, sizeof(*p));
127
128     p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
129     *p->recordsInSelectedCharSets = (selected) ? 1:0;
130
131     if (charsets && num_charsets) {             
132         
133         p->num_proposedCharSets = num_charsets;
134         p->proposedCharSets = 
135             (Z_OriginProposal_0**)
136             odr_malloc(o, num_charsets*sizeof(Z_OriginProposal_0*));
137
138         for (i = 0; i<num_charsets; i++)
139             p->proposedCharSets[i] =
140                 z_get_OriginProposal_0(o, charsets[i]);
141     }
142     if (langs && num_langs) {
143         
144         p->num_proposedlanguages = num_langs;
145
146         p->proposedlanguages = 
147             (char **) odr_malloc(o, num_langs*sizeof(char *));
148
149         for (i = 0; i<num_langs; i++) {
150
151             p->proposedlanguages[i] = (char *)langs[i];
152                         
153         }
154     }
155     return p;
156 }
157
158 static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(
159     ODR o)
160 {
161     Z_CharSetandLanguageNegotiation *p =
162         (Z_CharSetandLanguageNegotiation *) odr_malloc(o, sizeof(*p));
163     
164     memset(p, 0, sizeof(*p));
165         
166     return p;
167 }
168
169 /* Create EXTERNAL for negotation proposal. Client side */
170 Z_External *yaz_set_proposal_charneg(ODR o,
171                                      const char **charsets, int num_charsets,
172                                      const char **langs, int num_langs,
173                                      int selected)
174 {
175     Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
176     oident oid;
177         
178     p->descriptor = 0;
179     p->indirect_reference = 0;  
180
181     oid.proto = PROTO_Z3950;
182     oid.oclass = CLASS_NEGOT;
183     oid.value = VAL_CHARNEG3;
184     p->direct_reference = odr_oiddup(o, oid_getoidbyent(&oid));
185
186     p->which = Z_External_charSetandLanguageNegotiation;
187     p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
188     p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_proposal;
189     p->u.charNeg3->u.proposal =
190         z_get_OriginProposal(o, charsets, num_charsets,
191                              langs, num_langs, selected);
192
193     return p;
194 }
195
196 /* used by yaz_set_response_charneg */
197 static Z_TargetResponse *z_get_TargetResponse(ODR o, const char *charset,
198                                               const char *lang, int selected)
199 {       
200     Z_TargetResponse *p = (Z_TargetResponse *) odr_malloc(o, sizeof(*p));
201     int form = get_form(charset);
202
203     memset(p, 0, sizeof(*p));
204
205     if (form > 0)
206     {
207         char oidname[20];
208
209         Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
210         p->which = Z_TargetResponse_iso10646;
211         p->u.iso10646 = is;
212         is->collections = 0;
213         sprintf (oidname, "1.0.10646.1.0.%d", form);
214         is->encodingLevel = odr_getoidbystr (o, oidname);
215     }
216     else
217     {
218         Z_PrivateCharacterSet *pc =
219             (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
220         
221         memset(pc, 0, sizeof(*pc));
222         
223         p->which = Z_TargetResponse_private;
224         p->u.zprivate = pc;
225         
226         pc->which = Z_PrivateCharacterSet_externallySpecified;
227         pc->u.externallySpecified =
228             z_ext_record2(o, CLASS_NEGOT, VAL_ID_CHARSET, charset);
229     }
230     p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
231     *p->recordsInSelectedCharSets = (selected) ? 1:0;
232     
233     p->selectedLanguage = lang ? (char *)odr_strdup(o, lang) : 0;
234     return p;
235 }
236
237 /* Create charset response. Server side */
238 Z_External *yaz_set_response_charneg(ODR o, const char *charset,
239                                      const char *lang, int selected)
240 {
241     Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
242     oident oid;
243         
244     p->descriptor = 0;
245     p->indirect_reference = 0;  
246
247     oid.proto = PROTO_Z3950;
248     oid.oclass = CLASS_NEGOT;
249     oid.value = VAL_CHARNEG3;
250     p->direct_reference = odr_oiddup(o, oid_getoidbyent(&oid));
251
252     p->which = Z_External_charSetandLanguageNegotiation;
253     p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
254     p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_response;
255     p->u.charNeg3->u.response = z_get_TargetResponse(o, charset, lang, selected);
256
257     return p;
258 }
259
260 /* Get negotiation from OtherInformation. Client&Server side */
261 Z_CharSetandLanguageNegotiation *yaz_get_charneg_record(Z_OtherInformation *p)
262 {
263     int i;
264         
265     if (!p)
266         return 0;
267         
268     for (i = 0; i < p->num_elements; i++) {
269         Z_External *pext;
270         if ((p->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
271             (pext = p->list[i]->information.externallyDefinedInfo)) {
272             
273             oident *ent = oid_getentbyoid(pext->direct_reference);
274             
275             if (ent && ent->value == VAL_CHARNEG3 
276                 && ent->oclass == CLASS_NEGOT
277                 &&  pext->which == Z_External_charSetandLanguageNegotiation)
278             {
279                 return pext->u.charNeg3;
280             }
281         }
282     }
283     return 0;
284 }
285
286 /* Delete negotiation from OtherInformation. Client&Server side */
287 int yaz_del_charneg_record(Z_OtherInformation **p)
288 {
289     int i;
290         
291     if (!*p)
292         return 0;
293         
294     for (i = 0; i < (*p)->num_elements; i++) {
295         Z_External *pext;
296         if (((*p)->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
297             (pext = (*p)->list[i]->information.externallyDefinedInfo)) {
298             
299             oident *ent = oid_getentbyoid(pext->direct_reference);
300             
301             if (ent && ent->value == VAL_CHARNEG3 
302                 && ent->oclass == CLASS_NEGOT
303                 && pext->which == Z_External_charSetandLanguageNegotiation)
304             {
305                 --((*p)->num_elements);
306                 if ((*p)->num_elements == 0)
307                     *p = 0;
308                 else
309                 {
310                     for(; i < (*p)->num_elements; i++)
311                         (*p)->list[i] = (*p)->list[i+1];
312                 }
313                 return 1;
314             }
315         }
316     }
317     return 0;
318 }
319
320
321 /* Get charsets, langs, selected from negotiation.. Server side */
322 void yaz_get_proposal_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
323                               char ***charsets, int *num_charsets,
324                               char ***langs, int *num_langs, int *selected)
325 {
326     int i;
327     Z_OriginProposal *pro = p->u.proposal;
328     
329     if (num_charsets && charsets)
330     {
331         if (pro->num_proposedCharSets)
332         {
333             *num_charsets = pro->num_proposedCharSets;
334             
335             (*charsets) = (char **)
336                 nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *));
337             
338             for (i=0; i<pro->num_proposedCharSets; i++) 
339             {
340                 (*charsets)[i] = 0;
341                 
342                 if (pro->proposedCharSets[i]->which ==
343                     Z_OriginProposal_0_private &&
344                     pro->proposedCharSets[i]->u.zprivate->which ==
345                     Z_PrivateCharacterSet_externallySpecified) {
346                     
347                     Z_External *pext =
348                         pro->proposedCharSets[i]->u.zprivate->u.externallySpecified;
349                     
350                     if (pext->which == Z_External_octet) {
351                         
352                         (*charsets)[i] = (char *)
353                             nmem_malloc(mem, (1+pext->u.octet_aligned->len) *
354                                         sizeof(char));
355                         
356                         memcpy ((*charsets)[i], pext->u.octet_aligned->buf,
357                                 pext->u.octet_aligned->len);
358                         (*charsets)[i][pext->u.octet_aligned->len] = 0;
359                         
360                     }
361                 }
362                 else if (pro->proposedCharSets[i]->which ==
363                          Z_OriginProposal_0_iso10646)
364                     (*charsets)[i] = set_form (
365                         pro->proposedCharSets[i]->u.iso10646->encodingLevel);
366             }
367         }
368         else
369             *num_charsets = 0;
370     }
371     
372     if (langs && num_langs)
373     {
374         if (pro->num_proposedlanguages)
375         {
376             *num_langs = pro->num_proposedlanguages;
377             
378             (*langs) = (char **)
379                 nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *));
380             
381             for (i=0; i<pro->num_proposedlanguages; i++)
382                 (*langs)[i] = nmem_strdup(mem, pro->proposedlanguages[i]);
383         }
384         else
385             *num_langs = 0;
386     }
387     
388     if(pro->recordsInSelectedCharSets && selected)
389         *selected = *pro->recordsInSelectedCharSets;
390 }
391
392 /* Return charset, lang, selected from negotiation.. Client side */
393 void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
394                               char **charset, char **lang, int *selected)
395 {
396     Z_TargetResponse *res = p->u.response;
397         
398     if (charset && res->which == Z_TargetResponse_private &&
399         res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) {
400
401         Z_External *pext = res->u.zprivate->u.externallySpecified;
402         
403         if (pext->which == Z_External_octet) {
404             
405             *charset = (char *)
406                 nmem_malloc(mem, (1+pext->u.octet_aligned->len)*sizeof(char));
407             memcpy (*charset, pext->u.octet_aligned->buf,
408                     pext->u.octet_aligned->len);
409             (*charset)[pext->u.octet_aligned->len] = 0;
410         }       
411     }
412     if (charset && res->which == Z_TargetResponse_iso10646)
413         *charset = set_form (res->u.iso10646->encodingLevel);
414     if (lang && res->selectedLanguage)
415         *lang = nmem_strdup (mem, res->selectedLanguage);
416
417     if(selected && res->recordsInSelectedCharSets)
418         *selected = *res->recordsInSelectedCharSets;
419 }
420 /*
421  * Local variables:
422  * c-basic-offset: 4
423  * indent-tabs-mode: nil
424  * End:
425  * vim: shiftwidth=4 tabstop=8 expandtab
426  */
427