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 = (unsigned char *)odr_malloc(o, len)))
39 p->u.octet_aligned->len = p->u.octet_aligned->size = len;
40 memcpy(p->u.octet_aligned->buf, buf, len);
45 static int get_form(const char *charset)
49 if (!yaz_matchstr(charset, "UCS-2"))
51 if (!yaz_matchstr(charset, "UCS-4"))
53 if (!yaz_matchstr(charset, "UTF-16"))
55 if (!yaz_matchstr(charset, "UTF-8"))
61 static char *set_form(Odr_oid *encoding)
63 static char *charset = 0;
64 if ( oid_oidlen(encoding) != 6)
77 static Z_OriginProposal_0 *z_get_OriginProposal_0(ODR o, const char *charset)
79 int form = get_form(charset);
80 Z_OriginProposal_0 *p0 =
81 (Z_OriginProposal_0*)odr_malloc(o, sizeof(*p0));
83 memset(p0, 0, sizeof(*p0));
86 { /* ISO 10646 (UNICODE) */
89 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc(o, sizeof(*is));
90 p0->which = Z_OriginProposal_0_iso10646;
93 sprintf(oidname, "1.0.10646.1.0.%d", form);
94 is->encodingLevel = odr_getoidbystr(o, oidname);
98 Z_PrivateCharacterSet *pc =
99 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
101 memset(pc, 0, sizeof(*pc));
103 p0->which = Z_OriginProposal_0_private;
106 pc->which = Z_PrivateCharacterSet_externallySpecified;
107 pc->u.externallySpecified = z_ext_record2(o, charset);
112 static Z_OriginProposal *z_get_OriginProposal(
113 ODR o, const char **charsets, int num_charsets,
114 const char **langs, int num_langs, int selected)
117 Z_OriginProposal *p = (Z_OriginProposal *) odr_malloc(o, sizeof(*p));
119 memset(p, 0, sizeof(*p));
121 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
122 *p->recordsInSelectedCharSets = (selected) ? 1 : 0;
124 if (charsets && num_charsets)
126 p->num_proposedCharSets = num_charsets;
127 p->proposedCharSets =
128 (Z_OriginProposal_0**)
129 odr_malloc(o, num_charsets*sizeof(Z_OriginProposal_0*));
131 for (i = 0; i < num_charsets; i++)
132 p->proposedCharSets[i] =
133 z_get_OriginProposal_0(o, charsets[i]);
135 if (langs && num_langs)
137 p->num_proposedlanguages = num_langs;
138 p->proposedlanguages =
139 (char **) odr_malloc(o, num_langs*sizeof(char *));
141 for (i = 0; i < num_langs; i++)
142 p->proposedlanguages[i] = (char *)langs[i];
147 static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(
150 Z_CharSetandLanguageNegotiation *p =
151 (Z_CharSetandLanguageNegotiation *) odr_malloc(o, sizeof(*p));
153 memset(p, 0, sizeof(*p));
158 /* Create EXTERNAL for negotation proposal. Client side */
159 Z_External *yaz_set_proposal_charneg(ODR o,
160 const char **charsets, int num_charsets,
161 const char **langs, int num_langs,
164 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
167 p->indirect_reference = 0;
169 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_3);
171 p->which = Z_External_charSetandLanguageNegotiation;
172 p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
173 p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_proposal;
174 p->u.charNeg3->u.proposal =
175 z_get_OriginProposal(o, charsets, num_charsets,
176 langs, num_langs, selected);
181 Z_External *yaz_set_proposal_charneg_list(ODR o,
183 const char *charset_list,
184 const char *lang_list,
187 char **charsets_addresses = 0;
188 char **langs_addresses = 0;
189 int charsets_count = 0;
193 nmem_strsplit(odr_getmem(o), delim, charset_list,
194 &charsets_addresses, &charsets_count);
196 nmem_strsplit(odr_getmem(o), delim, lang_list,
197 &langs_addresses, &langs_count);
198 return yaz_set_proposal_charneg(o,
199 (const char **) charsets_addresses,
201 (const char **) langs_addresses,
207 /* used by yaz_set_response_charneg */
208 static Z_TargetResponse *z_get_TargetResponse(ODR o, const char *charset,
209 const char *lang, int selected)
211 Z_TargetResponse *p = (Z_TargetResponse *) odr_malloc(o, sizeof(*p));
212 int form = get_form(charset);
214 memset(p, 0, sizeof(*p));
219 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
220 p->which = Z_TargetResponse_iso10646;
223 sprintf(oidname, "1.0.10646.1.0.%d", form);
224 is->encodingLevel = odr_getoidbystr (o, oidname);
228 Z_PrivateCharacterSet *pc =
229 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
231 memset(pc, 0, sizeof(*pc));
233 p->which = Z_TargetResponse_private;
236 pc->which = Z_PrivateCharacterSet_externallySpecified;
237 pc->u.externallySpecified =
238 z_ext_record2(o, charset);
240 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
241 *p->recordsInSelectedCharSets = (selected) ? 1 : 0;
243 p->selectedLanguage = lang ? (char *) odr_strdup(o, lang) : 0;
247 /* Create charset response. Server side */
248 Z_External *yaz_set_response_charneg(ODR o, const char *charset,
249 const char *lang, int selected)
251 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
254 p->indirect_reference = 0;
256 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_3);
258 p->which = Z_External_charSetandLanguageNegotiation;
259 p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
260 p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_response;
261 p->u.charNeg3->u.response = z_get_TargetResponse(o, charset, lang, selected);
266 /* Get negotiation from OtherInformation. Client&Server side */
267 Z_CharSetandLanguageNegotiation *yaz_get_charneg_record(Z_OtherInformation *p)
274 for (i = 0; i < p->num_elements; i++)
277 if ((p->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
278 (pext = p->list[i]->information.externallyDefinedInfo))
280 if (!oid_oidcmp(pext->direct_reference, yaz_oid_negot_charset_3)
281 && pext->which == Z_External_charSetandLanguageNegotiation)
283 return pext->u.charNeg3;
290 /* Delete negotiation from OtherInformation. Client&Server side */
291 int yaz_del_charneg_record(Z_OtherInformation **p)
298 for (i = 0; i < (*p)->num_elements; i++)
301 if (((*p)->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
302 (pext = (*p)->list[i]->information.externallyDefinedInfo))
304 if (!oid_oidcmp(pext->direct_reference, yaz_oid_negot_charset_3)
305 && pext->which == Z_External_charSetandLanguageNegotiation)
307 if ((*p)->num_elements <= 1)
311 --((*p)->num_elements);
312 for (; i < (*p)->num_elements; i++)
313 (*p)->list[i] = (*p)->list[i+1];
323 /* Get charsets, langs, selected from negotiation.. Server side */
324 void yaz_get_proposal_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
325 char ***charsets, int *num_charsets,
326 char ***langs, int *num_langs, int *selected)
329 Z_OriginProposal *pro = p->u.proposal;
331 if (num_charsets && charsets)
333 if (pro->num_proposedCharSets)
335 *num_charsets = pro->num_proposedCharSets;
337 (*charsets) = (char **)
338 nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *));
340 for (i = 0; i < pro->num_proposedCharSets; i++)
344 if (pro->proposedCharSets[i]->which ==
345 Z_OriginProposal_0_private &&
346 pro->proposedCharSets[i]->u.zprivate->which ==
347 Z_PrivateCharacterSet_externallySpecified)
350 pro->proposedCharSets[i]->u.zprivate->u.externallySpecified;
352 if (pext->which == Z_External_octet)
354 (*charsets)[i] = (char *)
355 nmem_malloc(mem, (1+pext->u.octet_aligned->len) *
358 memcpy((*charsets)[i], pext->u.octet_aligned->buf,
359 pext->u.octet_aligned->len);
360 (*charsets)[i][pext->u.octet_aligned->len] = 0;
363 else if (pro->proposedCharSets[i]->which ==
364 Z_OriginProposal_0_iso10646)
365 (*charsets)[i] = set_form(
366 pro->proposedCharSets[i]->u.iso10646->encodingLevel);
373 if (langs && num_langs)
375 if (pro->num_proposedlanguages)
377 *num_langs = pro->num_proposedlanguages;
380 nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *));
382 for (i = 0; i < pro->num_proposedlanguages; i++)
383 (*langs)[i] = nmem_strdup(mem, pro->proposedlanguages[i]);
389 if (pro->recordsInSelectedCharSets && selected)
390 *selected = *pro->recordsInSelectedCharSets;
393 /* Return charset, lang, selected from negotiation.. Client side */
394 void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
395 char **charset, char **lang, int *selected)
397 Z_TargetResponse *res = p->u.response;
399 if (charset && res->which == Z_TargetResponse_private &&
400 res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified)
402 Z_External *pext = res->u.zprivate->u.externallySpecified;
404 if (pext->which == Z_External_octet)
407 nmem_malloc(mem, (1+pext->u.octet_aligned->len)*sizeof(char));
408 memcpy(*charset, pext->u.octet_aligned->buf,
409 pext->u.octet_aligned->len);
410 (*charset)[pext->u.octet_aligned->len] = 0;
413 if (charset && res->which == Z_TargetResponse_iso10646)
414 *charset = set_form(res->u.iso10646->encodingLevel);
415 if (lang && res->selectedLanguage)
416 *lang = nmem_strdup(mem, res->selectedLanguage);
418 if (selected && res->recordsInSelectedCharSets)
419 *selected = *res->recordsInSelectedCharSets;
424 * c-file-style: "Stroustrup"
425 * indent-tabs-mode: nil
427 * vim: shiftwidth=4 tabstop=8 expandtab