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;
40 DBG(r,"start chxj_encoding()");
42 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
45 DBG(r,"none encoding.");
49 entryp = chxj_apply_convrule(r, dconf->convrules);
50 if (entryp->encoding == NULL) {
51 DBG(r,"none encoding.");
55 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
56 DBG(r,"none encoding.");
60 ibuf = apr_palloc(r->pool, ilen+1);
62 DBG(r,"end chxj_encoding()");
65 memset(ibuf, 0, ilen+1);
66 memcpy(ibuf, src, ilen);
69 spos = obuf = apr_palloc(r->pool, olen);
71 DBG(r,"end chxj_encoding()");
74 DBG(r,"encode convert [%s] -> [%s]", entryp->encoding, "CP932");
76 memset(obuf, 0, olen);
77 cd = iconv_open("CP932", entryp->encoding);
78 if (cd == (iconv_t)-1) {
79 if (EINVAL == errno) {
80 ERR(r, "The conversion from %s to %s is not supported by the implementation.", entryp->encoding, "CP932");
83 ERR(r, "iconv open failed. from:[%s] to:[%s] errno:[%d]", entryp->encoding, "CP932", errno);
85 DBG(r,"end chxj_encoding()");
89 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
90 if (result == (size_t)(-1)) {
92 ERR(r, "There is not sufficient room at *outbuf.");
94 else if (EILSEQ == errno) {
95 ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
97 else if (EINVAL == errno) {
98 ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
106 DBG(r,"end chxj_encoding() len=[%d] obuf=[%.*s]", (int)*len, (int)*len, spos);
112 chxj_rencoding(request_rec *r, const char* src, apr_size_t* len)
122 mod_chxj_config* dconf;
123 chxjconvrule_entry* entryp;
125 DBG(r,"start chxj_rencoding()");
127 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
129 DBG(r,"none encoding.");
130 DBG(r,"end chxj_rencoding()");
134 entryp = chxj_apply_convrule(r, dconf->convrules);
135 if (! entryp->encoding) {
136 DBG(r,"none encoding.");
137 DBG(r,"end chxj_rencoding()");
141 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
142 DBG(r,"none encoding.");
143 DBG(r,"end chxj_rencoding()");
148 ibuf = apr_palloc(r->pool, ilen+1);
150 DBG(r,"end chxj_rencoding()");
154 memset(ibuf, 0, ilen+1);
155 memcpy(ibuf, src, ilen+0);
158 spos = obuf = apr_palloc(r->pool, olen);
160 DBG(r,"end chxj_rencoding()");
163 DBG(r,"encode convert [%s] -> [%s]", "CP932", entryp->encoding);
165 memset(obuf, 0, olen);
167 cd = iconv_open(entryp->encoding, "CP932");
168 if (cd == (iconv_t)-1) {
169 if (EINVAL == errno) {
170 ERR(r, "The conversion from %s to %s is not supported by the implementation.", "CP932", entryp->encoding);
172 DBG(r,"end chxj_rencoding()");
177 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
178 if (result == (size_t)(-1)) {
179 if (E2BIG == errno) {
180 ERR(r, "There is not sufficient room at *outbuf");
182 else if (EILSEQ == errno) {
183 ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
185 else if (EINVAL == errno) {
186 ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
195 DBG(r,"end chxj_rencoding() len=[%d] obuf=[%.*s]", (int)*len, (int)*len, spos);
202 chxj_encoding_parameter(request_rec *r, const char *value)
216 DBG(r, "start chxj_encoding_parameter()");
218 src = apr_pstrdup(r->pool, value);
220 spos = strchr(src, '?');
222 DBG(r, "end chxj_encoding_parameter()");
227 src_sv = apr_pstrdup(r->pool, src);
228 param = apr_palloc(r->pool, 1);
236 pair = apr_strtok(spos, "&", &pstat);
239 if (strncasecmp(pair, "amp;", 4) == 0) {
243 key = apr_strtok(pair, "=", &vstat);
244 val = apr_strtok(NULL, "=", &vstat);
246 val = chxj_url_decode(r, val);
247 len = (apr_size_t)strlen(val);
248 val = chxj_encoding(r, val, &len);
249 val = chxj_url_encode(r, val);
250 if (strlen(param) == 0) {
251 param = apr_pstrcat(r->pool, param, key, "=", val, NULL);
255 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
258 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
263 if (strlen(param) == 0) {
264 param = apr_pstrcat(r->pool, param, key, NULL);
268 param = apr_pstrcat(r->pool, param, "&", key, NULL);
271 param = apr_pstrcat(r->pool, param, "&", key, NULL);
276 DBG(r, "end chxj_encoding_parameter()");
278 return apr_pstrcat(r->pool, src_sv, "?", param, NULL);