1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements Z39.50 Charset negotiation utilities
9 * Helper functions for Character Set and Language Negotiation - 3
16 #include <yaz/otherinfo.h>
17 #include <yaz/z-charneg.h>
18 #include <yaz/charneg.h>
19 #include <yaz/yaz-util.h>
20 #include <yaz/oid_db.h>
22 static Z_External* z_ext_record2(ODR o, const char *buf)
25 int len = strlen(buf);
27 if (!(p = (Z_External *)odr_malloc(o, sizeof(*p))))
30 p->indirect_reference = 0;
32 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_id);
34 p->which = Z_External_octet;
35 if (!(p->u.octet_aligned = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct))))
37 if (!(p->u.octet_aligned->buf = (char *)odr_malloc(o, len)))
39 p->u.octet_aligned->len = len;
41 p->u.octet_aligned->size = len;
43 memcpy(p->u.octet_aligned->buf, buf, len);
48 static int get_form(const char *charset)
52 if (!yaz_matchstr(charset, "UCS-2"))
54 if (!yaz_matchstr(charset, "UCS-4"))
56 if (!yaz_matchstr(charset, "UTF-16"))
58 if (!yaz_matchstr(charset, "UTF-8"))
64 static char *set_form(Odr_oid *encoding)
66 static char *charset = 0;
67 if ( oid_oidlen(encoding) != 6)
80 static Z_OriginProposal_0 *z_get_OriginProposal_0(ODR o, const char *charset)
82 int form = get_form(charset);
83 Z_OriginProposal_0 *p0 =
84 (Z_OriginProposal_0*)odr_malloc(o, sizeof(*p0));
86 memset(p0, 0, sizeof(*p0));
89 { /* ISO 10646 (UNICODE) */
92 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc(o, sizeof(*is));
93 p0->which = Z_OriginProposal_0_iso10646;
96 sprintf(oidname, "1.0.10646.1.0.%d", form);
97 is->encodingLevel = odr_getoidbystr(o, oidname);
101 Z_PrivateCharacterSet *pc =
102 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
104 memset(pc, 0, sizeof(*pc));
106 p0->which = Z_OriginProposal_0_private;
109 pc->which = Z_PrivateCharacterSet_externallySpecified;
110 pc->u.externallySpecified = z_ext_record2(o, charset);
115 static Z_OriginProposal *z_get_OriginProposal(
116 ODR o, const char **charsets, int num_charsets,
117 const char **langs, int num_langs, int selected)
120 Z_OriginProposal *p = (Z_OriginProposal *) odr_malloc(o, sizeof(*p));
122 memset(p, 0, sizeof(*p));
124 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
125 *p->recordsInSelectedCharSets = (selected) ? 1 : 0;
127 if (charsets && num_charsets)
129 p->num_proposedCharSets = num_charsets;
130 p->proposedCharSets =
131 (Z_OriginProposal_0**)
132 odr_malloc(o, num_charsets*sizeof(Z_OriginProposal_0*));
134 for (i = 0; i < num_charsets; i++)
135 p->proposedCharSets[i] =
136 z_get_OriginProposal_0(o, charsets[i]);
138 if (langs && num_langs)
140 p->num_proposedlanguages = num_langs;
141 p->proposedlanguages =
142 (char **) odr_malloc(o, num_langs*sizeof(char *));
144 for (i = 0; i < num_langs; i++)
145 p->proposedlanguages[i] = (char *)langs[i];
150 static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(
153 Z_CharSetandLanguageNegotiation *p =
154 (Z_CharSetandLanguageNegotiation *) odr_malloc(o, sizeof(*p));
156 memset(p, 0, sizeof(*p));
161 /* Create EXTERNAL for negotation proposal. Client side */
162 Z_External *yaz_set_proposal_charneg(ODR o,
163 const char **charsets, int num_charsets,
164 const char **langs, int num_langs,
167 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
170 p->indirect_reference = 0;
172 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_3);
174 p->which = Z_External_charSetandLanguageNegotiation;
175 p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
176 p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_proposal;
177 p->u.charNeg3->u.proposal =
178 z_get_OriginProposal(o, charsets, num_charsets,
179 langs, num_langs, selected);
184 Z_External *yaz_set_proposal_charneg_list(ODR o,
186 const char *charset_list,
187 const char *lang_list,
190 char **charsets_addresses = 0;
191 char **langs_addresses = 0;
192 int charsets_count = 0;
196 nmem_strsplit(odr_getmem(o), delim, charset_list,
197 &charsets_addresses, &charsets_count);
199 nmem_strsplit(odr_getmem(o), delim, lang_list,
200 &langs_addresses, &langs_count);
201 return yaz_set_proposal_charneg(o,
202 (const char **) charsets_addresses,
204 (const char **) langs_addresses,
210 /* used by yaz_set_response_charneg */
211 static Z_TargetResponse *z_get_TargetResponse(ODR o, const char *charset,
212 const char *lang, int selected)
214 Z_TargetResponse *p = (Z_TargetResponse *) odr_malloc(o, sizeof(*p));
215 int form = get_form(charset);
217 memset(p, 0, sizeof(*p));
222 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
223 p->which = Z_TargetResponse_iso10646;
226 sprintf(oidname, "1.0.10646.1.0.%d", form);
227 is->encodingLevel = odr_getoidbystr (o, oidname);
231 Z_PrivateCharacterSet *pc =
232 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
234 memset(pc, 0, sizeof(*pc));
236 p->which = Z_TargetResponse_private;
239 pc->which = Z_PrivateCharacterSet_externallySpecified;
240 pc->u.externallySpecified =
241 z_ext_record2(o, charset);
243 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
244 *p->recordsInSelectedCharSets = (selected) ? 1 : 0;
246 p->selectedLanguage = lang ? (char *) odr_strdup(o, lang) : 0;
250 /* Create charset response. Server side */
251 Z_External *yaz_set_response_charneg(ODR o, const char *charset,
252 const char *lang, int selected)
254 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
257 p->indirect_reference = 0;
259 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_3);
261 p->which = Z_External_charSetandLanguageNegotiation;
262 p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
263 p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_response;
264 p->u.charNeg3->u.response = z_get_TargetResponse(o, charset, lang, selected);
269 /* Get negotiation from OtherInformation. Client&Server side */
270 Z_CharSetandLanguageNegotiation *yaz_get_charneg_record(Z_OtherInformation *p)
277 for (i = 0; i < p->num_elements; i++)
280 if ((p->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
281 (pext = p->list[i]->information.externallyDefinedInfo))
283 if (!oid_oidcmp(pext->direct_reference, yaz_oid_negot_charset_3)
284 && pext->which == Z_External_charSetandLanguageNegotiation)
286 return pext->u.charNeg3;
293 /* Delete negotiation from OtherInformation. Client&Server side */
294 int yaz_del_charneg_record(Z_OtherInformation **p)
301 for (i = 0; i < (*p)->num_elements; i++)
304 if (((*p)->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
305 (pext = (*p)->list[i]->information.externallyDefinedInfo))
307 if (!oid_oidcmp(pext->direct_reference, yaz_oid_negot_charset_3)
308 && pext->which == Z_External_charSetandLanguageNegotiation)
310 if ((*p)->num_elements <= 1)
314 --((*p)->num_elements);
315 for (; i < (*p)->num_elements; i++)
316 (*p)->list[i] = (*p)->list[i+1];
326 /* Get charsets, langs, selected from negotiation.. Server side */
327 void yaz_get_proposal_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
328 char ***charsets, int *num_charsets,
329 char ***langs, int *num_langs, int *selected)
332 Z_OriginProposal *pro = p->u.proposal;
334 if (num_charsets && charsets)
336 if (pro->num_proposedCharSets)
338 *num_charsets = pro->num_proposedCharSets;
340 (*charsets) = (char **)
341 nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *));
343 for (i = 0; i < pro->num_proposedCharSets; i++)
347 if (pro->proposedCharSets[i]->which ==
348 Z_OriginProposal_0_private &&
349 pro->proposedCharSets[i]->u.zprivate->which ==
350 Z_PrivateCharacterSet_externallySpecified)
353 pro->proposedCharSets[i]->u.zprivate->u.externallySpecified;
355 if (pext->which == Z_External_octet)
357 (*charsets)[i] = (char *)
358 nmem_malloc(mem, (1+pext->u.octet_aligned->len) *
361 memcpy((*charsets)[i], pext->u.octet_aligned->buf,
362 pext->u.octet_aligned->len);
363 (*charsets)[i][pext->u.octet_aligned->len] = 0;
366 else if (pro->proposedCharSets[i]->which ==
367 Z_OriginProposal_0_iso10646)
368 (*charsets)[i] = set_form(
369 pro->proposedCharSets[i]->u.iso10646->encodingLevel);
376 if (langs && num_langs)
378 if (pro->num_proposedlanguages)
380 *num_langs = pro->num_proposedlanguages;
383 nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *));
385 for (i = 0; i < pro->num_proposedlanguages; i++)
386 (*langs)[i] = nmem_strdup(mem, pro->proposedlanguages[i]);
392 if (pro->recordsInSelectedCharSets && selected)
393 *selected = *pro->recordsInSelectedCharSets;
396 /* Return charset, lang, selected from negotiation.. Client side */
397 void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
398 char **charset, char **lang, int *selected)
400 Z_TargetResponse *res = p->u.response;
402 if (charset && res->which == Z_TargetResponse_private &&
403 res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified)
405 Z_External *pext = res->u.zprivate->u.externallySpecified;
407 if (pext->which == Z_External_octet)
410 nmem_malloc(mem, (1+pext->u.octet_aligned->len)*sizeof(char));
411 memcpy(*charset, pext->u.octet_aligned->buf,
412 pext->u.octet_aligned->len);
413 (*charset)[pext->u.octet_aligned->len] = 0;
416 if (charset && res->which == Z_TargetResponse_iso10646)
417 *charset = set_form(res->u.iso10646->encodingLevel);
418 if (lang && res->selectedLanguage)
419 *lang = nmem_strdup(mem, res->selectedLanguage);
421 if (selected && res->recordsInSelectedCharSets)
422 *selected = *res->recordsInSelectedCharSets;
427 * c-file-style: "Stroustrup"
428 * indent-tabs-mode: nil
430 * vim: shiftwidth=4 tabstop=8 expandtab