2 * Copyright (C) 2005-2011 Atsushi Konno All rights reserved.
3 * Copyright (C) 2005 QSDN,Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "chxj_encoding.h"
19 #include "chxj_apply_convrule.h"
20 #include "chxj_url_encode.h"
21 #include "chxj_dump_string.h"
27 chxj_encoding(request_rec *r, const char *src, apr_size_t *len)
37 mod_chxj_config *dconf;
38 chxjconvrule_entry *entryp;
42 DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
44 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
47 DBG(r,"REQ[%X] none encoding.", TO_ADDR(r));
48 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
52 ERR(r,"REQ[%X] runtime exception: chxj_encoding(): invalid string size.[%d]", TO_ADDR(r),(int)*len);
53 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
54 return (char *)apr_pstrdup(r->pool, "");
57 entryp = chxj_apply_convrule(r, dconf->convrules);
58 if (entryp->encoding == NULL) {
59 DBG(r,"REQ[%X] none encoding.", TO_ADDR(r));
60 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
64 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
65 DBG(r,"REQ[%X] none encoding.", TO_ADDR(r));
66 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
70 apr_pool_create(&pool, r->pool);
72 ibuf = apr_palloc(pool, ilen+1);
74 ERR(r,"REQ[%X] runtime exception: chxj_encoding(): Out of memory.",TO_ADDR(r));
75 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
78 memset(ibuf, 0, ilen+1);
79 memcpy(ibuf, src, ilen);
82 spos = obuf = apr_palloc(pool, olen);
84 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
87 DBG(r,"REQ[%X] encode convert [%s] -> [%s]", TO_ADDR(r), entryp->encoding, "CP932");
89 memset(obuf, 0, olen);
90 cd = iconv_open("CP932", entryp->encoding);
91 if (cd == (iconv_t)-1) {
92 if (EINVAL == errno) {
93 ERR(r,"REQ[%X] The conversion from %s to %s is not supported by the implementation.", TO_ADDR(r),entryp->encoding, "CP932");
96 ERR(r,"REQ[%X] iconv open failed. from:[%s] to:[%s] errno:[%d]", TO_ADDR(r),entryp->encoding, "CP932", errno);
98 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
102 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
103 if (result == (size_t)(-1)) {
104 if (E2BIG == errno) {
105 ERR(r, "REQ[%X] There is not sufficient room at *outbuf.",TO_ADDR(r));
108 else if (EILSEQ == errno) {
109 ERR(r,"REQ[%X] %s:%d An invalid multibyte sequence has been encountered in the input. input:[%s]", TO_ADDR(r),__FILE__,__LINE__,ibuf);
110 chxj_convert_illegal_charactor_sequence(r, entryp, &ibuf, &ilen, &obuf, &olen);
112 else if (EINVAL == errno) {
113 ERR(r,"REQ[%X] An incomplete multibyte sequence has been encountered in the input. input:[%s]", TO_ADDR(r),ibuf);
121 chxj_dump_string(r, APLOG_MARK, "RESULT Convert Encoding", spos, *len);
122 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
128 chxj_convert_illegal_charactor_sequence(request_rec *r, chxjconvrule_entry *entryp, char **ibuf, apr_size_t *ilen, char **obuf, apr_size_t *olen)
130 if (STRCASEEQ('u','U',"UTF-8", entryp->encoding) || STRCASEEQ('u','U',"UTF8", entryp->encoding)) {
131 if ((0xe0 & **ibuf) == 0xc0) {
132 /* 2byte charactor */
137 DBG(r,"REQ[%X] passed 2byte.",TO_ADDR(r));
139 else if ((0xf0 & **ibuf) == 0xe0) {
140 /* 3byte charactor */
145 DBG(r,"REQ[%X] passed 3byte.",TO_ADDR(r));
147 else if ((0xf8 & **ibuf) == 0xf0) {
148 /* 4byte charactor */
153 DBG(r,"REQ[%X] passed 4byte.",TO_ADDR(r));
155 else if ((0xc0 & **ibuf) == 0x80) {
156 /* 1byte charactor */
161 DBG(r,"REQ[%X] passed 1byte.",TO_ADDR(r));
164 /* unknown charactor */
169 DBG(r,"REQ[%X] passed 1byte.",TO_ADDR(r));
172 else if (STRCASEEQ('e','E', "EUCJP", entryp->encoding)
173 || STRCASEEQ('c','C', "CSEUCPKDFMTJAPANESE", entryp->encoding)
174 || STRCASEEQ('e','E', "EUC-JISX0213", entryp->encoding)
175 || STRCASEEQ('e','E', "EUC-JP-MS", entryp->encoding)
176 || STRCASEEQ('e','E', "EUC-JP", entryp->encoding)
177 || STRCASEEQ('e','E', "EUCJP-MS", entryp->encoding)
178 || STRCASEEQ('e','E', "EUCJP-OPEN", entryp->encoding)
179 || STRCASEEQ('e','E', "EUCJP-WIN", entryp->encoding)
180 || STRCASEEQ('e','E', "EUCJP", entryp->encoding)) {
181 if ((unsigned char)**ibuf == 0x8F) {
182 /* 3byte charactor */
187 DBG(r,"REQ[%X] passed 3byte.",TO_ADDR(r));
190 /* 2byte charactor */
195 DBG(r,"REQ[%X] passed 2byte.",TO_ADDR(r));
198 else if (STRCASEEQ('c', 'C', "CP932", entryp->encoding)
199 || STRCASEEQ('c', 'C', "CSIBM932", entryp->encoding)
200 || STRCASEEQ('i', 'I', "IBM-932", entryp->encoding)
201 || STRCASEEQ('i', 'I', "IBM932", entryp->encoding)
202 || STRCASEEQ('m', 'M', "MS932", entryp->encoding)
203 || STRCASEEQ('m', 'M', "MS_KANJI", entryp->encoding)
204 || STRCASEEQ('s', 'S', "SJIS-OPEN", entryp->encoding)
205 || STRCASEEQ('s', 'S', "SJIS-WIN", entryp->encoding)
206 || STRCASEEQ('s', 'S', "SJIS", entryp->encoding)) {
207 if ( ( ((0x81 <= (unsigned char)**ibuf) && (0x9f >= (unsigned char)**ibuf))
208 || ((0xe0 <= (unsigned char)**ibuf) && (0xfc >= (unsigned char)**ibuf)))
210 ( ((0x40 <= (unsigned char)*((*ibuf)+1)) && (0x7e >= (unsigned char)*((*ibuf)+1)))
211 ||((0x80 <= (unsigned char)*((*ibuf)+1)) && (0xfc >= (unsigned char)*((*ibuf)+1))))) {
212 /* 2byte charactor */
217 DBG(r,"REQ[%X] passed 2byte.",TO_ADDR(r));
220 /* 1byte charactor */
225 DBG(r,"REQ[%X] passed 1byte.",TO_ADDR(r));
229 /* unknown 1byte charactor */
234 DBG(r,"REQ[%X] passed 1byte.",TO_ADDR(r));
237 *ilen = strlen(*ibuf);
238 DBG(r,"REQ[%X] new len = [%" APR_SIZE_T_FMT "].", TO_ADDR(r),(apr_size_t)*ilen);
244 chxj_rencoding(request_rec *r, const char *src, apr_size_t *len,const char *enc)
254 mod_chxj_config *dconf;
255 chxjconvrule_entry *entryp;
257 DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
260 ERR(r,"REQ[%X] runtime exception: chxj_rencoding(): invalid string size.[%d]",TO_ADDR(r),(int)*len);
261 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
262 return (char *)apr_pstrdup(r->pool, "");
265 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
267 DBG(r,"REQ[%X] none encoding.", TO_ADDR(r));
268 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
272 entryp = chxj_apply_convrule(r, dconf->convrules);
273 if (! entryp->encoding) {
274 DBG(r,"REQ[%X] none encoding.",TO_ADDR(r));
275 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
279 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
280 DBG(r,"REQ[%X] none encoding.",TO_ADDR(r));
281 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
286 ibuf = apr_palloc(r->pool, ilen+1);
288 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
292 memset(ibuf, 0, ilen+1);
293 memcpy(ibuf, src, ilen+0);
296 spos = obuf = apr_palloc(r->pool, olen);
298 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
301 char *from_enc = (char *)enc;
305 if (strcasecmp(enc,"Shift_JIS") == 0){
308 DBG(r,"REQ[%X] encode convert [%s] -> [%s]", TO_ADDR(r),from_enc, entryp->encoding);
309 memset(obuf, 0, olen);
311 cd = iconv_open(entryp->encoding, from_enc);
312 if (cd == (iconv_t)-1) {
313 if (EINVAL == errno) {
314 ERR(r,"REQ[%X] The conversion from %s to %s is not supported by the implementation.", TO_ADDR(r),"CP932", entryp->encoding);
316 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
321 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
322 if (result == (size_t)(-1)) {
323 if (E2BIG == errno) {
324 ERR(r,"REQ[%X] There is not sufficient room at *outbuf",TO_ADDR(r));
327 else if (EILSEQ == errno) {
328 ERR(r,"REQ[%X] An invalid multibyte sequence has been encountered in the input. input:[%s]", TO_ADDR(r),ibuf);
329 chxj_convert_illegal_charactor_sequence(r, entryp, &ibuf, &ilen, &obuf, &olen);
331 else if (EINVAL == errno) {
332 ERR(r,"REQ[%X] An incomplete multibyte sequence has been encountered in the input. input:[%s]", TO_ADDR(r),ibuf);
341 chxj_dump_string(r, APLOG_MARK, "RESULT Convert REncoding", spos, *len);
342 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
349 chxj_encoding_parameter(request_rec *r, const char *value, int xmlflag)
365 DBG(r,"REQ[%X] start %s()",TO_ADDR(r),__func__);
367 src = apr_pstrdup(r->pool, value);
369 anchor_pos = strchr(src, '#');
372 anchor = apr_pstrdup(r->pool, anchor_pos);
377 spos = strchr(src, '?');
379 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
381 return apr_pstrcat(r->pool, src, "#", anchor, NULL);
388 src_sv = apr_pstrdup(r->pool, src);
389 param = apr_palloc(r->pool, 1);
396 use_amp_flag = (xmlflag) ? 1 : 0;
398 pair = apr_strtok(spos, "&", &pstat);
401 if (strncasecmp(pair, "amp;", 4) == 0) {
405 sep_pos = strchr(pair, '=');
406 if (pair == sep_pos) {
407 key = apr_pstrdup(r->pool, "");
410 key = apr_strtok(pair, "=", &vstat);
414 apr_size_t klen = (apr_size_t)strlen(key);
415 key = chxj_url_decode(r->pool, key);
416 len = (apr_size_t)strlen(key);
418 key = chxj_encoding(r, key, &len);
419 key = chxj_url_encode(r->pool, key);
421 #if 0 /* XXX:2009/4/10 */
422 key = chxj_url_encode(r->pool, key);
425 val = apr_strtok(pair, "=", &vstat);
426 if (! val && sep_pos) {
427 val = apr_pstrdup(r->pool, "");
430 apr_size_t vlen = (apr_size_t)strlen(val);
431 val = chxj_url_decode(r->pool, val);
432 len = (apr_size_t)strlen(val);
434 val = chxj_encoding(r, val, &len);
435 val = chxj_url_encode(r->pool, val);
437 #if 0 /* XXX:2009/4/10 */
438 val = chxj_url_encode(r->pool, val);
440 if (strlen(param) == 0) {
441 param = apr_pstrcat(r->pool, param, key, "=", val, NULL);
445 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
448 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
453 if (strlen(param) == 0) {
454 param = apr_pstrcat(r->pool, param, key, NULL);
458 param = apr_pstrcat(r->pool, param, "&", key, NULL);
461 param = apr_pstrcat(r->pool, param, "&", key, NULL);
466 DBG(r,"REQ[%X] end %s()",TO_ADDR(r),__func__);
469 return apr_pstrcat(r->pool, src_sv, "?", param, "#", anchor, NULL);
471 return apr_pstrcat(r->pool, src_sv, "?", param, NULL);
477 chxj_iconv(request_rec *r, apr_pool_t *pool, const char *src, apr_size_t *len, const char *from, const char *to)
490 ERR(r,"REQ[%X] runtime exception: chxj_iconv(): invalid string size.[%d]", TO_ADDR(r),(int)*len);
491 return (char *)apr_pstrdup(pool, "");
495 ibuf = apr_palloc(pool, ilen+1);
497 ERR(r,"REQ[%X] runtime exception: chxj_iconv(): Out of memory.",TO_ADDR(r));
500 memset(ibuf, 0, ilen+1);
501 memcpy(ibuf, src, ilen);
504 spos = obuf = apr_palloc(pool, olen);
506 ERR(r,"REQ[%X] %s:%d runtime exception: chxj_iconv(): Out of memory", TO_ADDR(r),APLOG_MARK);
509 memset(obuf, 0, olen);
510 cd = iconv_open(to, from);
511 if (cd == (iconv_t)-1) {
512 if (EINVAL == errno) {
513 ERR(r,"REQ[%X] The conversion from %s to %s is not supported by the implementation.", TO_ADDR(r),from, to);
516 ERR(r,"REQ[%X] iconv open failed. from:[%s] to:[%s] errno:[%d]", TO_ADDR(r),from, to, errno);
521 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
522 if (result == (size_t)(-1)) {
523 if (E2BIG == errno) {
524 ERR(r,"REQ[%X] There is not sufficient room at *outbuf.",TO_ADDR(r));
526 else if (EILSEQ == errno) {
527 ERR(r,"REQ[%X] An invalid multibyte sequence has been encountered in the input. input:[%s]", TO_ADDR(r),ibuf);
529 else if (EINVAL == errno) {
530 ERR(r,"REQ[%X] An incomplete multibyte sequence has been encountered in the input. input:[%s]", TO_ADDR(r),ibuf);