1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apu_config.h"
20 #include "apr_strings.h"
21 #include "apr_portable.h"
22 #include "apr_xlate.h"
24 /* If no implementation is available, don't generate code here since
25 * apr_xlate.h emitted macros which return APR_ENOTIMPL.
31 #include <stddef.h> /* for NULL */
36 #if APR_HAVE_STRINGS_H
42 #if APU_HAVE_APR_ICONV
43 #include <apr_iconv.h>
46 #if defined(APU_ICONV_INBUF_CONST) || APU_HAVE_APR_ICONV
47 #define ICONV_INBUF_TYPE const char **
49 #define ICONV_INBUF_TYPE char **
53 #define min(x,y) ((x) <= (y) ? (x) : (y))
63 #elif APU_HAVE_APR_ICONV
69 static const char *handle_special_names(const char *page, apr_pool_t *pool)
71 if (page == APR_DEFAULT_CHARSET) {
72 return apr_os_default_encoding(pool);
74 else if (page == APR_LOCALE_CHARSET) {
75 return apr_os_locale_encoding(pool);
82 static apr_status_t apr_xlate_cleanup(void *convset)
84 apr_xlate_t *old = convset;
86 #if APU_HAVE_APR_ICONV
87 if (old->ich != (apr_iconv_t)-1) {
88 return apr_iconv_close(old->ich, old->pool);
92 if (old->ich != (iconv_t)-1) {
93 if (iconv_close(old->ich)) {
96 /* Sometimes, iconv is not good about setting errno. */
97 return rv ? rv : APR_EINVAL;
106 static void check_sbcs(apr_xlate_t *convset)
108 char inbuf[256], outbuf[256];
109 char *inbufptr = inbuf;
110 char *outbufptr = outbuf;
111 apr_size_t inbytes_left, outbytes_left;
113 apr_size_t translated;
115 for (i = 0; i < sizeof(inbuf); i++) {
119 inbytes_left = outbytes_left = sizeof(inbuf);
120 translated = iconv(convset->ich, (ICONV_INBUF_TYPE)&inbufptr,
121 &inbytes_left, &outbufptr, &outbytes_left);
123 if (translated != (apr_size_t)-1
125 && outbytes_left == 0) {
126 /* hurray... this is simple translation; save the table,
127 * close the iconv descriptor
130 convset->sbcs_table = apr_palloc(convset->pool, sizeof(outbuf));
131 memcpy(convset->sbcs_table, outbuf, sizeof(outbuf));
132 iconv_close(convset->ich);
133 convset->ich = (iconv_t)-1;
135 /* TODO: add the table to the cache */
138 /* reset the iconv descriptor, since it's now in an undefined
140 iconv_close(convset->ich);
141 convset->ich = iconv_open(convset->topage, convset->frompage);
144 #elif APU_HAVE_APR_ICONV
145 static void check_sbcs(apr_xlate_t *convset)
147 char inbuf[256], outbuf[256];
148 char *inbufptr = inbuf;
149 char *outbufptr = outbuf;
150 apr_size_t inbytes_left, outbytes_left;
152 apr_size_t translated;
155 for (i = 0; i < sizeof(inbuf); i++) {
159 inbytes_left = outbytes_left = sizeof(inbuf);
160 rv = apr_iconv(convset->ich, (ICONV_INBUF_TYPE)&inbufptr,
161 &inbytes_left, &outbufptr, &outbytes_left,
164 if ((rv == APR_SUCCESS)
165 && (translated != (apr_size_t)-1)
167 && outbytes_left == 0) {
168 /* hurray... this is simple translation; save the table,
169 * close the iconv descriptor
172 convset->sbcs_table = apr_palloc(convset->pool, sizeof(outbuf));
173 memcpy(convset->sbcs_table, outbuf, sizeof(outbuf));
174 apr_iconv_close(convset->ich, convset->pool);
175 convset->ich = (apr_iconv_t)-1;
177 /* TODO: add the table to the cache */
180 /* reset the iconv descriptor, since it's now in an undefined
182 apr_iconv_close(convset->ich, convset->pool);
183 rv = apr_iconv_open(convset->topage, convset->frompage, convset->pool, &convset->ich);
186 #endif /* APU_HAVE_APR_ICONV */
188 static void make_identity_table(apr_xlate_t *convset)
192 convset->sbcs_table = apr_palloc(convset->pool, 256);
193 for (i = 0; i < 256; i++)
194 convset->sbcs_table[i] = i;
197 APU_DECLARE(apr_status_t) apr_xlate_open(apr_xlate_t **convset,
199 const char *frompage,
208 topage = handle_special_names(topage, pool);
209 frompage = handle_special_names(frompage, pool);
211 new = (apr_xlate_t *)apr_pcalloc(pool, sizeof(apr_xlate_t));
217 new->topage = apr_pstrdup(pool, topage);
218 new->frompage = apr_pstrdup(pool, frompage);
219 if (!new->topage || !new->frompage) {
224 /* search cache of codepage pairs; we may be able to avoid the
225 * expensive iconv_open()
228 set found to non-zero if found in the cache
231 if ((! found) && (strcmp(topage, frompage) == 0)) {
232 /* to and from are the same */
234 make_identity_table(new);
237 #if APU_HAVE_APR_ICONV
239 rv = apr_iconv_open(topage, frompage, pool, &new->ich);
240 if (rv != APR_SUCCESS) {
246 new->ich = (apr_iconv_t)-1;
250 new->ich = iconv_open(topage, frompage);
251 if (new->ich == (iconv_t)-1) {
253 /* Sometimes, iconv is not good about setting errno. */
254 return rv ? rv : APR_EINVAL;
259 new->ich = (iconv_t)-1;
260 #endif /* APU_HAVE_ICONV */
264 apr_pool_cleanup_register(pool, (void *)new, apr_xlate_cleanup,
265 apr_pool_cleanup_null);
269 rv = APR_EINVAL; /* iconv() would return EINVAL if it
270 couldn't handle the pair */
276 APU_DECLARE(apr_status_t) apr_xlate_sb_get(apr_xlate_t *convset, int *onoff)
278 *onoff = convset->sbcs_table != NULL;
282 APU_DECLARE(apr_status_t) apr_xlate_conv_buffer(apr_xlate_t *convset,
284 apr_size_t *inbytes_left,
286 apr_size_t *outbytes_left)
288 apr_status_t status = APR_SUCCESS;
290 #if APU_HAVE_APR_ICONV
291 if (convset->ich != (apr_iconv_t)-1) {
292 const char *inbufptr = inbuf;
293 apr_size_t translated;
294 char *outbufptr = outbuf;
295 status = apr_iconv(convset->ich, &inbufptr, inbytes_left,
296 &outbufptr, outbytes_left, &translated);
298 /* If everything went fine but we ran out of buffer, don't
299 * report it as an error. Caller needs to look at the two
300 * bytes-left values anyway.
302 * There are three expected cases where rc is -1. In each of
303 * these cases, *inbytes_left != 0.
304 * a) the non-error condition where we ran out of output
306 * b) the non-error condition where we ran out of input (i.e.,
307 * the last input character is incomplete)
308 * c) the error condition where the input is invalid
312 case E2BIG: /* out of space on output */
313 status = 0; /* change table lookup code below if you
314 make this an error */
317 case EINVAL: /* input character not complete (yet) */
318 status = APR_INCOMPLETE;
321 case EILSEQ: /* bad input byte */
325 /* Sometimes, iconv is not good about setting errno. */
327 if (inbytes_left && *inbytes_left)
328 status = APR_INCOMPLETE;
338 if (convset->ich != (iconv_t)-1) {
339 const char *inbufptr = inbuf;
340 char *outbufptr = outbuf;
341 apr_size_t translated;
342 translated = iconv(convset->ich, (ICONV_INBUF_TYPE)&inbufptr,
343 inbytes_left, &outbufptr, outbytes_left);
345 /* If everything went fine but we ran out of buffer, don't
346 * report it as an error. Caller needs to look at the two
347 * bytes-left values anyway.
349 * There are three expected cases where rc is -1. In each of
350 * these cases, *inbytes_left != 0.
351 * a) the non-error condition where we ran out of output
353 * b) the non-error condition where we ran out of input (i.e.,
354 * the last input character is incomplete)
355 * c) the error condition where the input is invalid
357 if (translated == (apr_size_t)-1) {
361 case E2BIG: /* out of space on output */
362 status = 0; /* change table lookup code below if you
363 make this an error */
366 case EINVAL: /* input character not complete (yet) */
367 status = APR_INCOMPLETE;
370 case EILSEQ: /* bad input byte */
374 /* Sometimes, iconv is not good about setting errno. */
376 status = APR_INCOMPLETE;
389 int to_convert = min(*inbytes_left, *outbytes_left);
390 int converted = to_convert;
391 char *table = convset->sbcs_table;
394 *outbuf = table[(unsigned char)*inbuf];
399 *inbytes_left -= converted;
400 *outbytes_left -= converted;
406 APU_DECLARE(apr_int32_t) apr_xlate_conv_byte(apr_xlate_t *convset,
407 unsigned char inchar)
409 if (convset->sbcs_table) {
410 return convset->sbcs_table[inchar];
417 APU_DECLARE(apr_status_t) apr_xlate_close(apr_xlate_t *convset)
419 return apr_pool_cleanup_run(convset->pool, convset, apr_xlate_cleanup);
422 #else /* !APR_HAS_XLATE */
424 APU_DECLARE(apr_status_t) apr_xlate_open(apr_xlate_t **convset,
426 const char *frompage,
432 APU_DECLARE(apr_status_t) apr_xlate_sb_get(apr_xlate_t *convset, int *onoff)
437 APU_DECLARE(apr_int32_t) apr_xlate_conv_byte(apr_xlate_t *convset,
438 unsigned char inchar)
443 APU_DECLARE(apr_status_t) apr_xlate_conv_buffer(apr_xlate_t *convset,
445 apr_size_t *inbytes_left,
447 apr_size_t *outbytes_left)
452 APU_DECLARE(apr_status_t) apr_xlate_close(apr_xlate_t *convset)
457 #endif /* APR_HAS_XLATE */
461 APU_DECLARE(apr_status_t) apr_xlate_get_sb(apr_xlate_t *convset, int *onoff)
463 return apr_xlate_sb_get(convset, onoff);