2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: charneg.c,v 1.5 2005-06-25 15:46:03 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>
20 static Z_External* z_ext_record2(ODR o, int oid_class, int oid_value,
25 int len = strlen(buf);
27 if (!(p = (Z_External *)odr_malloc(o, sizeof(*p)))) return 0;
30 p->indirect_reference = 0;
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));
37 p->which = Z_External_octet;
38 if (!(p->u.octet_aligned = (Odr_oct *)odr_malloc(o, sizeof(Odr_oct)))) {
41 if (!(p->u.octet_aligned->buf = (unsigned char *)odr_malloc(o, len))) {
44 p->u.octet_aligned->len = p->u.octet_aligned->size = len;
45 memcpy(p->u.octet_aligned->buf, buf, len);
50 static int get_form(const char *charset)
55 if (!yaz_matchstr(charset, "UCS-2"))
57 if (!yaz_matchstr(charset, "UCS-4"))
59 if (!yaz_matchstr(charset, "UTF-16"))
61 if (!yaz_matchstr(charset, "UTF-8"))
67 static char *set_form (Odr_oid *encoding)
69 static char *charset = 0;
70 if ( oid_oidlen(encoding) != 6)
83 static Z_OriginProposal_0 *z_get_OriginProposal_0(ODR o, const char *charset)
85 int form = get_form (charset);
86 Z_OriginProposal_0 *p0 =
87 (Z_OriginProposal_0*)odr_malloc(o, sizeof(*p0));
89 memset(p0, 0, sizeof(*p0));
92 { /* ISO 10646 (UNICODE) */
95 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
96 p0->which = Z_OriginProposal_0_iso10646;
99 sprintf (oidname, "1.0.10646.1.0.%d", form);
100 is->encodingLevel = odr_getoidbystr (o, oidname);
104 Z_PrivateCharacterSet *pc =
105 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
107 memset(pc, 0, sizeof(*pc));
109 p0->which = Z_OriginProposal_0_private;
112 pc->which = Z_PrivateCharacterSet_externallySpecified;
113 pc->u.externallySpecified =
114 z_ext_record2(o, CLASS_NEGOT, VAL_ID_CHARSET, charset);
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)
124 Z_OriginProposal *p = (Z_OriginProposal *) odr_malloc(o, sizeof(*p));
126 memset(p, 0, sizeof(*p));
128 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
129 *p->recordsInSelectedCharSets = (selected) ? 1:0;
131 if (charsets && num_charsets) {
133 p->num_proposedCharSets = num_charsets;
134 p->proposedCharSets =
135 (Z_OriginProposal_0**)
136 odr_malloc(o, num_charsets*sizeof(Z_OriginProposal_0*));
138 for (i = 0; i<num_charsets; i++)
139 p->proposedCharSets[i] =
140 z_get_OriginProposal_0(o, charsets[i]);
142 if (langs && num_langs) {
144 p->num_proposedlanguages = num_langs;
146 p->proposedlanguages =
147 (char **) odr_malloc(o, num_langs*sizeof(char *));
149 for (i = 0; i<num_langs; i++) {
151 p->proposedlanguages[i] = (char *)langs[i];
158 static Z_CharSetandLanguageNegotiation *z_get_CharSetandLanguageNegotiation(
161 Z_CharSetandLanguageNegotiation *p =
162 (Z_CharSetandLanguageNegotiation *) odr_malloc(o, sizeof(*p));
164 memset(p, 0, sizeof(*p));
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,
175 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
179 p->indirect_reference = 0;
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));
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);
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)
200 Z_TargetResponse *p = (Z_TargetResponse *) odr_malloc(o, sizeof(*p));
201 int form = get_form(charset);
203 memset(p, 0, sizeof(*p));
209 Z_Iso10646 *is = (Z_Iso10646 *) odr_malloc (o, sizeof(*is));
210 p->which = Z_TargetResponse_iso10646;
213 sprintf (oidname, "1.0.10646.1.0.%d", form);
214 is->encodingLevel = odr_getoidbystr (o, oidname);
218 Z_PrivateCharacterSet *pc =
219 (Z_PrivateCharacterSet *)odr_malloc(o, sizeof(*pc));
221 memset(pc, 0, sizeof(*pc));
223 p->which = Z_TargetResponse_private;
226 pc->which = Z_PrivateCharacterSet_externallySpecified;
227 pc->u.externallySpecified =
228 z_ext_record2(o, CLASS_NEGOT, VAL_ID_CHARSET, charset);
230 p->recordsInSelectedCharSets = (bool_t *)odr_malloc(o, sizeof(bool_t));
231 *p->recordsInSelectedCharSets = (selected) ? 1:0;
233 p->selectedLanguage = lang ? (char *)odr_strdup(o, lang) : 0;
237 /* Create charset response. Server side */
238 Z_External *yaz_set_response_charneg(ODR o, const char *charset,
239 const char *lang, int selected)
241 Z_External *p = (Z_External *)odr_malloc(o, sizeof(*p));
245 p->indirect_reference = 0;
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));
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);
260 /* Get negotiation from OtherInformation. Client&Server side */
261 Z_CharSetandLanguageNegotiation *yaz_get_charneg_record(Z_OtherInformation *p)
268 for (i = 0; i < p->num_elements; i++) {
270 if ((p->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
271 (pext = p->list[i]->information.externallyDefinedInfo)) {
273 oident *ent = oid_getentbyoid(pext->direct_reference);
275 if (ent && ent->value == VAL_CHARNEG3
276 && ent->oclass == CLASS_NEGOT
277 && pext->which == Z_External_charSetandLanguageNegotiation)
279 return pext->u.charNeg3;
286 /* Delete negotiation from OtherInformation. Client&Server side */
287 int yaz_del_charneg_record(Z_OtherInformation **p)
294 for (i = 0; i < (*p)->num_elements; i++) {
296 if (((*p)->list[i]->which == Z_OtherInfo_externallyDefinedInfo) &&
297 (pext = (*p)->list[i]->information.externallyDefinedInfo)) {
299 oident *ent = oid_getentbyoid(pext->direct_reference);
301 if (ent && ent->value == VAL_CHARNEG3
302 && ent->oclass == CLASS_NEGOT
303 && pext->which == Z_External_charSetandLanguageNegotiation)
305 --((*p)->num_elements);
306 if ((*p)->num_elements == 0)
310 for(; i < (*p)->num_elements; i++)
311 (*p)->list[i] = (*p)->list[i+1];
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)
327 Z_OriginProposal *pro = p->u.proposal;
329 if (num_charsets && charsets)
331 if (pro->num_proposedCharSets)
333 *num_charsets = pro->num_proposedCharSets;
335 (*charsets) = (char **)
336 nmem_malloc(mem, pro->num_proposedCharSets * sizeof(char *));
338 for (i=0; i<pro->num_proposedCharSets; i++)
342 if (pro->proposedCharSets[i]->which ==
343 Z_OriginProposal_0_private &&
344 pro->proposedCharSets[i]->u.zprivate->which ==
345 Z_PrivateCharacterSet_externallySpecified) {
348 pro->proposedCharSets[i]->u.zprivate->u.externallySpecified;
350 if (pext->which == Z_External_octet) {
352 (*charsets)[i] = (char *)
353 nmem_malloc(mem, (1+pext->u.octet_aligned->len) *
356 memcpy ((*charsets)[i], pext->u.octet_aligned->buf,
357 pext->u.octet_aligned->len);
358 (*charsets)[i][pext->u.octet_aligned->len] = 0;
362 else if (pro->proposedCharSets[i]->which ==
363 Z_OriginProposal_0_iso10646)
364 (*charsets)[i] = set_form (
365 pro->proposedCharSets[i]->u.iso10646->encodingLevel);
372 if (langs && num_langs)
374 if (pro->num_proposedlanguages)
376 *num_langs = pro->num_proposedlanguages;
379 nmem_malloc(mem, pro->num_proposedlanguages * sizeof(char *));
381 for (i=0; i<pro->num_proposedlanguages; i++)
382 (*langs)[i] = nmem_strdup(mem, pro->proposedlanguages[i]);
388 if(pro->recordsInSelectedCharSets && selected)
389 *selected = *pro->recordsInSelectedCharSets;
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)
396 Z_TargetResponse *res = p->u.response;
398 if (charset && res->which == Z_TargetResponse_private &&
399 res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) {
401 Z_External *pext = res->u.zprivate->u.externallySpecified;
403 if (pext->which == Z_External_octet) {
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;
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);
417 if(selected && res->recordsInSelectedCharSets)
418 *selected = *res->recordsInSelectedCharSets;
423 * indent-tabs-mode: nil
425 * vim: shiftwidth=4 tabstop=8 expandtab