aboutsummaryrefslogtreecommitdiff
path: root/libxml2-2.9.10/os400/transcode.c
blob: bae61872122edea94317b9b34be484db9add03e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
***     Transcoding support and wrappers.
***
***     See Copyright for the status of this software.
***
***     Author: Patrick Monnerat <pm@datasphere.ch>, DATASPHERE S.A.
**/

#define IN_LIBXML
#include "libxml.h"

#include <sys/types.h>
#include <iconv.h>
#include "libxml/xmlmemory.h"
#include "libxml/dict.h"
#include "transcode.h"


/**
***     Destroy a dictionary and mark as destroyed.
**/

void
xmlZapDict(xmlDictPtr * dict)

{
        if (dict && *dict) {
                xmlDictFree(*dict);
                *dict = (xmlDictPtr) NULL;
                }
}


/**
***     Support for inline conversion from/to UTF-8.
***     This is targetted to function parameter encoding conversion.
***     Method is:
***     -       Convert string from/to UTF-8.
***     -       Keep it in a dictionary.
***     -       Free original string if a release procedure is provided.
***     Can also be called without dictionary to convert a string from/to UTF-8
***             into xmlMalloc'ed dynamic storage.
**/

const char *
xmlTranscodeResult(const xmlChar * s, const char * encoding,
                        xmlDictPtr * dict, void (*freeproc)(const void *))

{
        size_t l;
        iconv_t cd;
        char * srcp;
        char * dstp;
        size_t srcc;
        size_t dstc;
        char * ts;
        const char * ret;
        int err;
        static const int nullstring[] = { 0 };

        /* Convert from UTF-8. */

        if (!s)
                return (const char *) NULL;

        ret = (const char *) NULL;
        ts = (char *) NULL;
        err = 0;
        l = xmlStrlen(s);

        if (!l && dict)
                ret = (const char *) nullstring;
        else {
                if (dict && !*dict)
                        err = !(*dict = xmlDictCreate());

                if (!err)
                        err = !(ts = xmlMalloc(4 * l + 4));

                dstp = ts;
                dstc = 4 * l;

                if (!err && l) {
                        if (!encoding)
                                encoding = "ibm-0";     /* Job's encoding. */

                        cd = iconv_open(encoding, "UTF-8");

                        if (cd == (iconv_t) -1)
                                err = 1;
                        else {
                                srcp = (char *) s;
                                srcc = l;
                                srcc = iconv(cd, &srcp, &srcc, &dstp, &dstc);
                                iconv_close(cd);
                                err = srcc == (size_t) -1;
                                }
                        }

                if (!err) {
                        dstp[0] = dstp[1] = dstp[2] = dstp[3] = '\0';

                        if (!dict) {
                                if (dstc)
                                        ts = xmlRealloc(ts, (dstp - ts) + 4);

                                ret = (const char *) ts;
                                ts = (char *) NULL;
                                }
                        else
                                ret = (char *) xmlDictLookup(*dict,
                                    (xmlChar *) ts, dstp - ts + 1);
                        }
                }

        if (ts)
                xmlFree(ts);

        if (freeproc)
                (*freeproc)(s);

        return ret;
}


/**
***     Support for inline conversion to UTF-8.
***     Method is:
***     -       Convert string to UTF-8.
***     -       Keep it in a dictionary.
***     Can also be called without dictionary to convert a string to UTF-8 into
***             xmlMalloc'ed dynamic storage.
**/

static const xmlChar *
inTranscode(const char * s, size_t l, const char * encoding, xmlDictPtr * dict)

{
        iconv_t cd;
        char * srcp;
        char * dstp;
        size_t srcc;
        size_t dstc;
        xmlChar * ts;
        const xmlChar * ret;
        static const xmlChar nullstring[] = { 0 };

        if (!l && dict)
                return nullstring;

        if (dict && !*dict)
                if (!(*dict = xmlDictCreate()))
                        return (const xmlChar *) NULL;

        ts = (xmlChar *) xmlMalloc(6 * l + 1);

        if (!ts)
                return (const xmlChar *) NULL;

        dstp = (char *) ts;
        dstc = 6 * l;

        if (l) {
                if (!encoding)
                        encoding = "ibm-0";     /* Use job's encoding. */

                cd = iconv_open("UTF-8", encoding);

                if (cd == (iconv_t) -1) {
                        xmlFree((char *) ts);
                        return (const xmlChar *) NULL;
                        }

                srcp = (char *) s;
                srcc = l;
                srcc = iconv(cd, &srcp, &srcc, &dstp, &dstc);
                iconv_close(cd);

                if (srcc == (size_t) -1) {
                        xmlFree((char *) ts);
                        return (const xmlChar *) NULL;
                        }
                }

        *dstp = '\0';

        if (!dict) {
                if (dstc)
                        ts = xmlRealloc(ts, (dstp - ts) + 1);

                return ts;
                }

        ret = xmlDictLookup(*dict, ts, dstp - ts + 1);
        xmlFree((char *) ts);
        return ret;
}


/**
***     Input 8-bit character string parameter.
**/

const xmlChar *
xmlTranscodeString(const char * s, const char * encoding, xmlDictPtr * dict)

{
        if (!s)
                return (const xmlChar *) NULL;

        return inTranscode(s, xmlStrlen(s), encoding, dict);
}


/**
***     Input 16-bit character string parameter.
**/

const xmlChar *
xmlTranscodeWString(const char * s, const char * encoding, xmlDictPtr * dict)

{
        size_t i;

        if (!s)
                return (const xmlChar *) NULL;

        for (i = 0; s[i] && s[i + 1]; i += 2)
                ;

        return inTranscode(s, i, encoding, dict);
}


/**
***     Input 32-bit character string parameter.
**/

const xmlChar *
xmlTranscodeHString(const char * s, const char * encoding, xmlDictPtr * dict)

{
        size_t i;

        if (!s)
                return (const xmlChar *) NULL;

        for (i = 0; s[i] && s[i + 1] && s[i + 2] && s[i + 3]; i += 4)
                ;

        return inTranscode(s, i, encoding, dict);
}


/**
***     vasprintf() implementation with result transcoding.
**/

const char *
xmlVasprintf(xmlDictPtr * dict, const char * encoding,
                                        const xmlChar * fmt, va_list args)

{
        char * s = NULL;

        vasprintf(&s, fmt, args);
        return xmlTranscodeResult((const xmlChar *) s, encoding, dict, free);
}