2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: charneg.c,v 1.9 2007-04-16 21:53:09 adam Exp $
10 * \brief Implements Z39.50 Charset negotiation utilities
12 * Helper functions for Character Set and Language Negotiation - 3
15 #include <yaz/otherinfo.h>
16 #include <yaz/z-charneg.h>
17 #include <yaz/charneg.h>
18 #include <yaz/yaz-util.h>
19 #include <yaz/oid_db.h>
21 static Z_External* z_ext_record2(ODR o, const char *buf)
24 int len = strlen(buf);
26 if (!(p = (Z_External *)odr_malloc(o, sizeof(*p)))) return 0;
29 p->indirect_reference = 0;
31 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_id);
33 p->which = Z_External_octet;
34 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))) {
40 p->u.octet_aligned->len = p->u.octet_aligned->size = len;
41 memcpy(p->u.octet_aligned->buf, buf, len);
46 static int get_form(const char *charset)
51 if (!yaz_matchstr(charset, "UCS-2"))
53 if (!yaz_matchstr(charset, "UCS-4"))
55 if (!yaz_matchstr(charset, "UTF-16"))
57 if (!yaz_matchstr(charset, "UTF-8"))
63 static char *set_form (Odr_oid *encoding)
65 static char *charset = 0;
66 if ( oid_oidlen(encoding) != 6)
79 static Z_OriginProposal_0 *z_get_OriginProposal_0(ODR o, const char *charset)
81 int form = get_form (charset);
82 Z_OriginProposal_0 *p0 =
83 (Z_OriginProposal_0*)odr_malloc(o, sizeof(*p0));
85 memset(p0, 0, sizeof(*p0));
88 { /* ISO 10646 (UNICODE) */
91 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
92 p0->which = Z_OriginProposal_0_iso10646;
95 sprintf (oidname, "1.0.10646.1.0.%d", form);
96 is->encodingLevel = odr_getoidbystr (o, oidname);
100 Z_PrivateCharacterSet *pc =
101 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
103 memset(pc, 0, sizeof(*pc));
105 p0->which = Z_OriginProposal_0_private;
108 pc->which = Z_PrivateCharacterSet_externallySpecified;
109 pc->u.externallySpecified = z_ext_record2(o, charset);
114 static Z_OriginProposal *z_get_OriginProposal(
115 ODR o, const char **charsets, int num_charsets,
116 const char **langs, int num_langs, int selected)
119 Z_OriginProposal *p = (Z_OriginProposal *) odr_malloc(o, sizeof(*p));
121 memset(p, 0, sizeof(*p));
123 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
124 *p->recordsInSelectedCharSets = (selected) ? 1:0;
126 if (charsets && num_charsets) {
128 p->num_proposedCharSets = num_charsets;
129 p->proposedCharSets =
130 (Z_OriginProposal_0**)
131 odr_malloc(o, num_charsets*sizeof(Z_OriginProposal_0*));
133 for (i = 0; i<num_charsets; i++)
134 p->proposedCharSets[i] =
135 z_get_OriginProposal_0(o, charsets[i]);
137 if (langs && num_langs) {
139 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++) {
146 p->proposedlanguages[i] = (char *)langs[i];
153 static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(
156 Z_CharSetandLanguageNegotiation *p =
157 (Z_CharSetandLanguageNegotiation *) odr_malloc(o, sizeof(*p));
159 memset(p, 0, sizeof(*p));
164 /* Create EXTERNAL for negotation proposal. Client side */
165 Z_External *yaz_set_proposal_charneg(ODR o,
166 const char **charsets, int num_charsets,
167 const char **langs, int num_langs,
170 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
173 p->indirect_reference = 0;
175 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_3);
177 p->which = Z_External_charSetandLanguageNegotiation;
178 p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
179 p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_proposal;
180 p->u.charNeg3->u.proposal =
181 z_get_OriginProposal(o, charsets, num_charsets,
182 langs, num_langs, selected);
187 Z_External *yaz_set_proposal_charneg_list(ODR o,
189 const char *charset_list,
190 const char *lang_list,
193 char **charsets_addresses = 0;
194 char **langs_addresses = 0;
195 int charsets_count = 0;
199 nmem_strsplit(odr_getmem(o), delim, charset_list,
200 &charsets_addresses, &charsets_count);
202 nmem_strsplit(odr_getmem(o), delim, lang_list,
203 &langs_addresses, &langs_count);
204 return yaz_set_proposal_charneg(o,
205 (const char **) charsets_addresses,
207 (const char **) langs_addresses,
213 /* used by yaz_set_response_charneg */
214 static Z_TargetResponse *z_get_TargetResponse(ODR o, const char *charset,
215 const char *lang, int selected)
217 Z_TargetResponse *p = (Z_TargetResponse *) odr_malloc(o, sizeof(*p));
218 int form = get_form(charset);
220 memset(p, 0, sizeof(*p));
226 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
227 p->which = Z_TargetResponse_iso10646;
230 sprintf (oidname, "1.0.10646.1.0.%d", form);
231 is->encodingLevel = odr_getoidbystr (o, oidname);
235 Z_PrivateCharacterSet *pc =
236 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
238 memset(pc, 0, sizeof(*pc));
240 p->which = Z_TargetResponse_private;
243 pc->which = Z_PrivateCharacterSet_externallySpecified;
244 pc->u.externallySpecified =
245 z_ext_record2(o, charset);
247 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
248 *p->recordsInSelectedCharSets = (selected) ? 1:0;
250 p->selectedLanguage = lang ? (char *)odr_strdup(o, lang) : 0;
254 /* Create charset response. Server side */
255 Z_External *yaz_set_response_charneg(ODR o, const char *charset,
256 const char *lang, int selected)
258 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
261 p->indirect_reference = 0;
263 p->direct_reference = odr_oiddup(o, yaz_oid_negot_charset_3);
265 p->which = Z_External_charSetandLanguageNegotiation;
266 p->u.charNeg3 = z_get_CharSetandLanguageNegotiation(o);
267 p->u.charNeg3->which = Z_CharSetandLanguageNegotiation_response;
268 p->u.charNeg3->u.response = z_get_TargetResponse(o, charset, lang, selected);
273 /* Get negotiation from OtherInformation. Client&Server side */
274 Z_CharSetandLanguageNegotiation *yaz_get_charneg_record(Z_OtherInformation *p)
281 for (i = 0; i < p->num_elements; i++) {
283 if ((p->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
284 (pext = p->list[i]->information.externallyDefinedInfo)) {
286 if (!oid_oidcmp(pext->direct_reference, yaz_oid_negot_charset_3)
287 && pext->which == Z_External_charSetandLanguageNegotiation)
289 return pext->u.charNeg3;
296 /* Delete negotiation from OtherInformation. Client&Server side */
297 int yaz_del_charneg_record(Z_OtherInformation **p)
304 for (i = 0; i < (*p)->num_elements; i++) {
306 if (((*p)->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
307 (pext = (*p)->list[i]->information.externallyDefinedInfo))
309 if (!oid_oidcmp(pext->direct_reference, yaz_oid_negot_charset_3)
310 && pext->which == Z_External_charSetandLanguageNegotiation)
312 if ((*p)->num_elements <= 1)
316 --((*p)->num_elements);
317 for(; i < (*p)->num_elements; i++)
318 (*p)->list[i] = (*p)->list[i+1];
328 /* Get charsets, langs, selected from negotiation.. Server side */
329 void yaz_get_proposal_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
330 char ***charsets, int *num_charsets,
331 char ***langs, int *num_langs, int *selected)
334 Z_OriginProposal *pro = p->u.proposal;
336 if (num_charsets && charsets)
338 if (pro->num_proposedCharSets)
340 *num_charsets = pro->num_proposedCharSets;
342 (*charsets) = (char **)
343 nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *));
345 for (i=0; i<pro->num_proposedCharSets; i++)
349 if (pro->proposedCharSets[i]->which ==
350 Z_OriginProposal_0_private &&
351 pro->proposedCharSets[i]->u.zprivate->which ==
352 Z_PrivateCharacterSet_externallySpecified) {
355 pro->proposedCharSets[i]->u.zprivate->u.externallySpecified;
357 if (pext->which == Z_External_octet) {
359 (*charsets)[i] = (char *)
360 nmem_malloc(mem, (1+pext->u.octet_aligned->len) *
363 memcpy ((*charsets)[i], pext->u.octet_aligned->buf,
364 pext->u.octet_aligned->len);
365 (*charsets)[i][pext->u.octet_aligned->len] = 0;
369 else if (pro->proposedCharSets[i]->which ==
370 Z_OriginProposal_0_iso10646)
371 (*charsets)[i] = set_form (
372 pro->proposedCharSets[i]->u.iso10646->encodingLevel);
379 if (langs && num_langs)
381 if (pro->num_proposedlanguages)
383 *num_langs = pro->num_proposedlanguages;
386 nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *));
388 for (i=0; i<pro->num_proposedlanguages; i++)
389 (*langs)[i] = nmem_strdup(mem, pro->proposedlanguages[i]);
395 if(pro->recordsInSelectedCharSets && selected)
396 *selected = *pro->recordsInSelectedCharSets;
399 /* Return charset, lang, selected from negotiation.. Client side */
400 void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p,
401 char **charset, char **lang, int *selected)
403 Z_TargetResponse *res = p->u.response;
405 if (charset && res->which == Z_TargetResponse_private &&
406 res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) {
408 Z_External *pext = res->u.zprivate->u.externallySpecified;
410 if (pext->which == Z_External_octet) {
413 nmem_malloc(mem, (1+pext->u.octet_aligned->len)*sizeof(char));
414 memcpy (*charset, pext->u.octet_aligned->buf,
415 pext->u.octet_aligned->len);
416 (*charset)[pext->u.octet_aligned->len] = 0;
419 if (charset && res->which == Z_TargetResponse_iso10646)
420 *charset = set_form (res->u.iso10646->encodingLevel);
421 if (lang && res->selectedLanguage)
422 *lang = nmem_strdup (mem, res->selectedLanguage);
424 if(selected && res->recordsInSelectedCharSets)
425 *selected = *res->recordsInSelectedCharSets;
430 * indent-tabs-mode: nil
432 * vim: shiftwidth=4 tabstop=8 expandtab