302e098eee2f8da48b16b6b761113f96e60ed4d5
[yaz-moved-to-github.git] / src / charneg.c
1 /*
2  * Copyright (c) 2002-2004, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: charneg.c,v 1.2 2004-10-15 00:19:00 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     Z_External *pext;
264     int i;
265         
266     if(!p)
267         return 0;
268         
269     for (i=0; i<p->num_elements; i++) {
270         
271         if ((p->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
272             (pext = p->list[i]->information.externallyDefinedInfo)) {
273                                         
274             oident *ent = oid_getentbyoid(pext->direct_reference);
275                         
276             if (ent && ent->value == VAL_CHARNEG3 && 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 /* Get charsets, langs, selected from negotiation.. Server side */
287 void yaz_get_proposal_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
288                               char ***charsets, int *num_charsets,
289                               char ***langs, int *num_langs, int *selected)
290 {
291     int i;
292     Z_OriginProposal *pro = p->u.proposal;
293     
294     if (num_charsets && charsets)
295     {
296         if (pro->num_proposedCharSets)
297         {
298             *num_charsets = pro->num_proposedCharSets;
299             
300             (*charsets) = (char **)
301                 nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *));
302             
303             for (i=0; i<pro->num_proposedCharSets; i++) 
304             {
305                 (*charsets)[i] = 0;
306                 
307                 if (pro->proposedCharSets[i]->which ==
308                     Z_OriginProposal_0_private &&
309                     pro->proposedCharSets[i]->u.zprivate->which ==
310                     Z_PrivateCharacterSet_externallySpecified) {
311                     
312                     Z_External *pext =
313                         pro->proposedCharSets[i]->u.zprivate->u.externallySpecified;
314                     
315                     if (pext->which == Z_External_octet) {
316                         
317                         (*charsets)[i] = (char *)
318                             nmem_malloc(mem, (1+pext->u.octet_aligned->len) *
319                                         sizeof(char));
320                         
321                         memcpy ((*charsets)[i], pext->u.octet_aligned->buf,
322                                 pext->u.octet_aligned->len);
323                         (*charsets)[i][pext->u.octet_aligned->len] = 0;
324                         
325                     }
326                 }
327                 else if (pro->proposedCharSets[i]->which ==
328                          Z_OriginProposal_0_iso10646)
329                     (*charsets)[i] = set_form (
330                         pro->proposedCharSets[i]->u.iso10646->encodingLevel);
331             }
332         }
333         else
334             *num_charsets = 0;
335     }
336     
337     if (langs && num_langs)
338     {
339         if (pro->num_proposedlanguages)
340         {
341             *num_langs = pro->num_proposedlanguages;
342             
343             (*langs) = (char **)
344                 nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *));
345             
346             for (i=0; i<pro->num_proposedlanguages; i++)
347                 (*langs)[i] = nmem_strdup(mem, pro->proposedlanguages[i]);
348         }
349         else
350             *num_langs = 0;
351     }
352     
353     if(pro->recordsInSelectedCharSets && selected)
354         *selected = *pro->recordsInSelectedCharSets;
355 }
356
357 /* Return charset, lang, selected from negotiation.. Client side */
358 void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
359                               char **charset, char **lang, int *selected)
360 {
361     Z_TargetResponse *res = p->u.response;
362         
363     if (charset && res->which == Z_TargetResponse_private &&
364         res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) {
365
366         Z_External *pext = res->u.zprivate->u.externallySpecified;
367         
368         if (pext->which == Z_External_octet) {
369             
370             *charset = (char *)
371                 nmem_malloc(mem, (1+pext->u.octet_aligned->len)*sizeof(char));
372             memcpy (*charset, pext->u.octet_aligned->buf,
373                     pext->u.octet_aligned->len);
374             (*charset)[pext->u.octet_aligned->len] = 0;
375         }       
376     }
377     if (charset && res->which == Z_TargetResponse_iso10646)
378         *charset = set_form (res->u.iso10646->encodingLevel);
379     if (lang && res->selectedLanguage)
380         *lang = nmem_strdup (mem, res->selectedLanguage);
381
382     if(selected && res->recordsInSelectedCharSets)
383         *selected = *res->recordsInSelectedCharSets;
384 }