2 *** Transcoding support and wrappers.
4 *** See Copyright for the status of this software.
6 *** Author: Patrick Monnerat <pm@datasphere.ch>, DATASPHERE S.A.
12 #include <sys/types.h>
14 #include "libxml/xmlmemory.h"
15 #include "libxml/dict.h"
16 #include "transcode.h"
20 *** Destroy a dictionary and mark as destroyed.
24 xmlZapDict(xmlDictPtr * dict)
29 *dict = (xmlDictPtr) NULL;
35 *** Support for inline conversion from/to UTF-8.
36 *** This is targetted to function parameter encoding conversion.
38 *** - Convert string from/to UTF-8.
39 *** - Keep it in a dictionary.
40 *** - Free original string if a release procedure is provided.
41 *** Can also be called without dictionary to convert a string from/to UTF-8
42 *** into xmlMalloc'ed dynamic storage.
46 xmlTranscodeResult(const xmlChar * s, const char * encoding,
47 xmlDictPtr * dict, void (*freeproc)(const void *))
59 static const int nullstring[] = { 0 };
61 /* Convert from UTF-8. */
64 return (const char *) NULL;
66 ret = (const char *) NULL;
72 ret = (const char *) nullstring;
75 err = !(*dict = xmlDictCreate());
78 err = !(ts = xmlMalloc(4 * l + 4));
85 encoding = "ibm-0"; /* Job's encoding. */
87 cd = iconv_open(encoding, "UTF-8");
89 if (cd == (iconv_t) -1)
94 srcc = iconv(cd, &srcp, &srcc, &dstp, &dstc);
96 err = srcc == (size_t) -1;
101 dstp[0] = dstp[1] = dstp[2] = dstp[3] = '\0';
105 ts = xmlRealloc(ts, (dstp - ts) + 4);
107 ret = (const char *) ts;
111 ret = (char *) xmlDictLookup(*dict,
112 (xmlChar *) ts, dstp - ts + 1);
127 *** Support for inline conversion to UTF-8.
129 *** - Convert string to UTF-8.
130 *** - Keep it in a dictionary.
131 *** Can also be called without dictionary to convert a string to UTF-8 into
132 *** xmlMalloc'ed dynamic storage.
135 static const xmlChar *
136 inTranscode(const char * s, size_t l, const char * encoding, xmlDictPtr * dict)
146 static const xmlChar nullstring[] = { 0 };
152 if (!(*dict = xmlDictCreate()))
153 return (const xmlChar *) NULL;
155 ts = (xmlChar *) xmlMalloc(6 * l + 1);
158 return (const xmlChar *) NULL;
165 encoding = "ibm-0"; /* Use job's encoding. */
167 cd = iconv_open("UTF-8", encoding);
169 if (cd == (iconv_t) -1) {
170 xmlFree((char *) ts);
171 return (const xmlChar *) NULL;
176 srcc = iconv(cd, &srcp, &srcc, &dstp, &dstc);
179 if (srcc == (size_t) -1) {
180 xmlFree((char *) ts);
181 return (const xmlChar *) NULL;
189 ts = xmlRealloc(ts, (dstp - ts) + 1);
194 ret = xmlDictLookup(*dict, ts, dstp - ts + 1);
195 xmlFree((char *) ts);
201 *** Input 8-bit character string parameter.
205 xmlTranscodeString(const char * s, const char * encoding, xmlDictPtr * dict)
209 return (const xmlChar *) NULL;
211 return inTranscode(s, xmlStrlen(s), encoding, dict);
216 *** Input 16-bit character string parameter.
220 xmlTranscodeWString(const char * s, const char * encoding, xmlDictPtr * dict)
226 return (const xmlChar *) NULL;
228 for (i = 0; s[i] && s[i + 1]; i += 2)
231 return inTranscode(s, i, encoding, dict);
236 *** Input 32-bit character string parameter.
240 xmlTranscodeHString(const char * s, const char * encoding, xmlDictPtr * dict)
246 return (const xmlChar *) NULL;
248 for (i = 0; s[i] && s[i + 1] && s[i + 2] && s[i + 3]; i += 4)
251 return inTranscode(s, i, encoding, dict);
256 *** vasprintf() implementation with result transcoding.
260 xmlVasprintf(xmlDictPtr * dict, const char * encoding,
261 const xmlChar * fmt, va_list args)
266 vasprintf(&s, fmt, args);
267 return xmlTranscodeResult((const xmlChar *) s, encoding, dict, free);