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 apr_pool_create(&pool, r->pool);
45 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
48 DBG(r,"none encoding.");
52 entryp = chxj_apply_convrule(r, dconf->convrules);
53 if (entryp->encoding == NULL) {
54 DBG(r,"none encoding.");
58 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
59 DBG(r,"none encoding.");
63 ibuf = apr_palloc(pool, ilen+1);
65 DBG(r,"end chxj_encoding()");
68 memset(ibuf, 0, ilen+1);
69 memcpy(ibuf, src, ilen);
72 spos = obuf = apr_palloc(pool, olen);
74 DBG(r,"end chxj_encoding()");
77 DBG(r,"encode convert [%s] -> [%s]", entryp->encoding, "CP932");
79 memset(obuf, 0, olen);
80 cd = iconv_open("CP932", entryp->encoding);
81 if (cd == (iconv_t)-1) {
82 if (EINVAL == errno) {
83 ERR(r, "The conversion from %s to %s is not supported by the implementation.", entryp->encoding, "CP932");
86 ERR(r, "iconv open failed. from:[%s] to:[%s] errno:[%d]", entryp->encoding, "CP932", errno);
88 DBG(r,"end chxj_encoding()");
92 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
93 if (result == (size_t)(-1)) {
95 ERR(r, "There is not sufficient room at *outbuf.");
97 else if (EILSEQ == errno) {
98 ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
100 else if (EINVAL == errno) {
101 ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
109 DBG(r,"end chxj_encoding() len=[%d] obuf=[%.*s]", (int)*len, (int)*len, spos);
115 chxj_rencoding(request_rec *r, const char* src, apr_size_t* len)
125 mod_chxj_config* dconf;
126 chxjconvrule_entry* entryp;
128 DBG(r,"start chxj_rencoding()");
130 dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
132 DBG(r,"none encoding.");
133 DBG(r,"end chxj_rencoding()");
137 entryp = chxj_apply_convrule(r, dconf->convrules);
138 if (! entryp->encoding) {
139 DBG(r,"none encoding.");
140 DBG(r,"end chxj_rencoding()");
144 if (STRCASEEQ('n','N',"none", entryp->encoding)) {
145 DBG(r,"none encoding.");
146 DBG(r,"end chxj_rencoding()");
151 ibuf = apr_palloc(r->pool, ilen+1);
153 DBG(r,"end chxj_rencoding()");
157 memset(ibuf, 0, ilen+1);
158 memcpy(ibuf, src, ilen+0);
161 spos = obuf = apr_palloc(r->pool, olen);
163 DBG(r,"end chxj_rencoding()");
166 DBG(r,"encode convert [%s] -> [%s]", "CP932", entryp->encoding);
168 memset(obuf, 0, olen);
170 cd = iconv_open(entryp->encoding, "CP932");
171 if (cd == (iconv_t)-1) {
172 if (EINVAL == errno) {
173 ERR(r, "The conversion from %s to %s is not supported by the implementation.", "CP932", entryp->encoding);
175 DBG(r,"end chxj_rencoding()");
180 result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
181 if (result == (size_t)(-1)) {
182 if (E2BIG == errno) {
183 ERR(r, "There is not sufficient room at *outbuf");
185 else if (EILSEQ == errno) {
186 ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
188 else if (EINVAL == errno) {
189 ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
198 DBG(r,"end chxj_rencoding() len=[%d] obuf=[%.*s]", (int)*len, (int)*len, spos);
205 chxj_encoding_parameter(request_rec *r, const char *value)
219 DBG(r, "start chxj_encoding_parameter()");
221 src = apr_pstrdup(r->pool, value);
223 spos = strchr(src, '?');
225 DBG(r, "end chxj_encoding_parameter()");
230 src_sv = apr_pstrdup(r->pool, src);
231 param = apr_palloc(r->pool, 1);
239 pair = apr_strtok(spos, "&", &pstat);
242 if (strncasecmp(pair, "amp;", 4) == 0) {
246 key = apr_strtok(pair, "=", &vstat);
247 val = apr_strtok(NULL, "=", &vstat);
249 val = chxj_url_decode(r, val);
250 len = (apr_size_t)strlen(val);
251 val = chxj_encoding(r, val, &len);
252 val = chxj_url_encode(r, val);
253 if (strlen(param) == 0) {
254 param = apr_pstrcat(r->pool, param, key, "=", val, NULL);
258 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
261 param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
266 if (strlen(param) == 0) {
267 param = apr_pstrcat(r->pool, param, key, NULL);
271 param = apr_pstrcat(r->pool, param, "&", key, NULL);
274 param = apr_pstrcat(r->pool, param, "&", key, NULL);
279 DBG(r, "end chxj_encoding_parameter()");
281 return apr_pstrcat(r->pool, src_sv, "?", param, NULL);