2 * Copyright (C) 2005-2008 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"
26 chxj_encoding(request_rec *r, const char *src, apr_size_t *len)
36 mod_chxj_config *dconf;
37 chxjconvrule_entry *entryp;
41 DBG(r,"start chxj_encoding()");
43 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
46 DBG(r,"none encoding.");
50 ERR(r, "runtime exception: chxj_encoding(): invalid string size.[%d]", (int)*len);
51 return (char *)apr_pstrdup(r->pool, "");
54 entryp = chxj_apply_convrule(r, dconf->convrules);
55 if (entryp->encoding == NULL) {
56 DBG(r,"none encoding.");
60 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
61 DBG(r,"none encoding.");
65 apr_pool_create(&pool, r->pool);
67 ibuf = apr_palloc(pool, ilen+1);
69 ERR(r, "runtime exception: chxj_encoding(): Out of memory.");
72 memset(ibuf, 0, ilen+1);
73 memcpy(ibuf, src, ilen);
76 spos = obuf = apr_palloc(pool, olen);
78 DBG(r,"end chxj_encoding()");
81 DBG(r,"encode convert [%s] -> [%s]", entryp->encoding, "CP932");
83 memset(obuf, 0, olen);
84 cd = iconv_open("CP932", entryp->encoding);
85 if (cd == (iconv_t)-1) {
86 if (EINVAL == errno) {
87 ERR(r, "The conversion from %s to %s is not supported by the implementation.", entryp->encoding, "CP932");
90 ERR(r, "iconv open failed. from:[%s] to:[%s] errno:[%d]", entryp->encoding, "CP932", errno);
92 DBG(r,"end chxj_encoding()");
96 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
97 if (result == (size_t)(-1)) {
99 ERR(r, "There is not sufficient room at *outbuf.");
101 else if (EILSEQ == errno) {
102 ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
104 else if (EINVAL == errno) {
105 ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
113 DBG(r,"end chxj_encoding() len=[%d] obuf=[%.*s]", (int)*len, (int)*len, spos);
119 chxj_rencoding(request_rec *r, const char *src, apr_size_t *len)
129 mod_chxj_config *dconf;
130 chxjconvrule_entry *entryp;
132 DBG(r,"start chxj_rencoding()");
135 ERR(r, "runtime exception: chxj_rencoding(): invalid string size.[%d]", (int)*len);
136 return (char *)apr_pstrdup(r->pool, "");
139 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
141 DBG(r,"none encoding.");
142 DBG(r,"end chxj_rencoding()");
146 entryp = chxj_apply_convrule(r, dconf->convrules);
147 if (! entryp->encoding) {
148 DBG(r,"none encoding.");
149 DBG(r,"end chxj_rencoding()");
153 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
154 DBG(r,"none encoding.");
155 DBG(r,"end chxj_rencoding()");
160 ibuf = apr_palloc(r->pool, ilen+1);
162 DBG(r,"end chxj_rencoding()");
166 memset(ibuf, 0, ilen+1);
167 memcpy(ibuf, src, ilen+0);
170 spos = obuf = apr_palloc(r->pool, olen);
172 DBG(r,"end chxj_rencoding()");
175 DBG(r,"encode convert [%s] -> [%s]", "CP932", entryp->encoding);
177 memset(obuf, 0, olen);
179 cd = iconv_open(entryp->encoding, "CP932");
180 if (cd == (iconv_t)-1) {
181 if (EINVAL == errno) {
182 ERR(r, "The conversion from %s to %s is not supported by the implementation.", "CP932", entryp->encoding);
184 DBG(r,"end chxj_rencoding()");
189 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
190 if (result == (size_t)(-1)) {
191 if (E2BIG == errno) {
192 ERR(r, "There is not sufficient room at *outbuf");
194 else if (EILSEQ == errno) {
195 ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
197 else if (EINVAL == errno) {
198 ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
207 DBG(r,"end chxj_rencoding() len=[%d] obuf=[%.*s]", (int)*len, (int)*len, spos);
214 chxj_encoding_parameter(request_rec *r, const char *value)
228 DBG(r, "start chxj_encoding_parameter()");
230 src = apr_pstrdup(r->pool, value);
232 spos = strchr(src, '?');
234 DBG(r, "end chxj_encoding_parameter()");
239 src_sv = apr_pstrdup(r->pool, src);
240 param = apr_palloc(r->pool, 1);
249 pair = apr_strtok(spos, "&", &pstat);
252 if (strncasecmp(pair, "amp;", 4) == 0) {
256 sep_pos = strchr(pair, '=');
257 if (pair == sep_pos) {
258 key = apr_pstrdup(r->pool, "");
261 key = apr_strtok(pair, "=", &vstat);
265 key = chxj_url_decode(r->pool, key);
266 len = (apr_size_t)strlen(key);
267 key = chxj_encoding(r, key, &len);
268 key = chxj_url_encode(r->pool, key);
270 val = apr_strtok(pair, "=", &vstat);
271 if (! val && sep_pos) {
272 val = apr_pstrdup(r->pool, "");
275 val = chxj_url_decode(r->pool, val);
276 len = (apr_size_t)strlen(val);
277 val = chxj_encoding(r, val, &len);
278 val = chxj_url_encode(r->pool, val);
279 if (strlen(param) == 0) {
280 param = apr_pstrcat(r->pool, param, key, "=", val, NULL);
284 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
287 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
292 if (strlen(param) == 0) {
293 param = apr_pstrcat(r->pool, param, key, NULL);
297 param = apr_pstrcat(r->pool, param, "&", key, NULL);
300 param = apr_pstrcat(r->pool, param, "&", key, NULL);
305 DBG(r, "end chxj_encoding_parameter()");
307 return apr_pstrcat(r->pool, src_sv, "?", param, NULL);
312 chxj_iconv(request_rec *r, apr_pool_t *pool, const char *src, apr_size_t *len, const char *from, const char *to)
325 ERR(r, "runtime exception: chxj_iconv(): invalid string size.[%d]", (int)*len);
326 return (char *)apr_pstrdup(pool, "");
330 ibuf = apr_palloc(pool, ilen+1);
332 ERR(r, "runtime exception: chxj_iconv(): Out of memory.");
335 memset(ibuf, 0, ilen+1);
336 memcpy(ibuf, src, ilen);
339 spos = obuf = apr_palloc(pool, olen);
341 ERR(r, "%s:%d runtime exception: chxj_iconv(): Out of memory", APLOG_MARK);
344 memset(obuf, 0, olen);
345 cd = iconv_open(to, from);
346 if (cd == (iconv_t)-1) {
347 if (EINVAL == errno) {
348 ERR(r, "The conversion from %s to %s is not supported by the implementation.", from, to);
351 ERR(r, "iconv open failed. from:[%s] to:[%s] errno:[%d]", from, to, errno);
356 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
357 if (result == (size_t)(-1)) {
358 if (E2BIG == errno) {
359 ERR(r, "There is not sufficient room at *outbuf.");
361 else if (EILSEQ == errno) {
362 ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
364 else if (EINVAL == errno) {
365 ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);