OSDN Git Service

* warning
[modchxj/mod_chxj.git] / src / chxj_encoding.c
1 /*
2  * Copyright (C) 2005-2009 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 #include "mod_chxj.h"
18 #include "chxj_encoding.h"
19 #include "chxj_apply_convrule.h"
20 #include "chxj_url_encode.h"
21 #include "chxj_dump_string.h"
22 #include <errno.h>
23 #include <iconv.h>
24
25
26 char *
27 chxj_encoding(request_rec *r, const char *src, apr_size_t *len)
28 {
29   char                *obuf;
30   char                *ibuf;
31   char                *spos;
32   
33   iconv_t             cd;
34   size_t              result;
35   apr_size_t          ilen;
36   apr_size_t          olen;
37   mod_chxj_config     *dconf;
38   chxjconvrule_entry  *entryp;
39   apr_pool_t          *pool;
40
41
42   DBG(r,"REQ[%X] start chxj_encoding()", (unsigned int)(apr_size_t)r);
43
44   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
45
46   if (dconf == NULL) {
47     DBG(r,"none encoding.");
48     DBG(r,"REQ[%X] end   chxj_encoding()", (unsigned int)(apr_size_t)r);
49     return (char*)src;
50   }
51   if ((int)*len < 0) {
52     ERR(r, "runtime exception: chxj_encoding(): invalid string size.[%d]", (int)*len);
53     DBG(r,"REQ[%X] end   chxj_encoding()", (unsigned int)(apr_size_t)r);
54     return (char *)apr_pstrdup(r->pool, "");
55   }
56
57   entryp = chxj_apply_convrule(r, dconf->convrules);
58   if (entryp->encoding == NULL) {
59     DBG(r,"REQ[%X] none encoding.", (unsigned int)(apr_size_t)r);
60     DBG(r,"REQ[%X] end   chxj_encoding()", (unsigned int)(apr_size_t)r);
61     return (char *)src;
62   }
63
64   if (STRCASEEQ('n','N',"none", entryp->encoding)) {
65     DBG(r,"REQ[%X] none encoding.", (unsigned int)(apr_size_t)r);
66     DBG(r,"REQ[%X] end   chxj_encoding()", (unsigned int)(apr_size_t)r);
67     return (char*)src;
68   }
69
70   apr_pool_create(&pool, r->pool);
71   ilen = *len;
72   ibuf = apr_palloc(pool, ilen+1);
73   if (ibuf == NULL) {
74     ERR(r, "runtime exception: chxj_encoding(): Out of memory.");
75     DBG(r,"REQ[%X] end chxj_encoding()", (unsigned int)(apr_size_t)r);
76     return (char *)src;
77   }
78   memset(ibuf, 0, ilen+1);
79   memcpy(ibuf, src, ilen);
80
81   olen = ilen * 4 + 1;
82   spos = obuf = apr_palloc(pool, olen);
83   if (obuf == NULL) {
84     DBG(r,"REQ[%X] end   chxj_encoding()", (unsigned int)(apr_size_t)r);
85     return ibuf;
86   }
87   DBG(r,"REQ[%X] encode convert [%s] -> [%s]", (unsigned int)(apr_size_t)r, entryp->encoding, "CP932");
88
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, "The conversion from %s to %s is not supported by the implementation.", entryp->encoding, "CP932");
94     }
95     else {
96       ERR(r, "iconv open failed. from:[%s] to:[%s] errno:[%d]", entryp->encoding, "CP932", errno);
97     }
98     DBG(r,"REQ[%X] end   chxj_encoding()", (unsigned int)(apr_size_t)r);
99     return ibuf;
100   }
101   while (ilen > 0) {
102     result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
103     if (result == (size_t)(-1)) {
104       if (E2BIG == errno) {
105         ERR(r, "There is not sufficient room at *outbuf.");
106         break;
107       }
108       else if (EILSEQ == errno) {
109         ERR(r, "%s:%d An invalid multibyte sequence has been encountered in the input. input:[%s]", __FILE__,__LINE__,ibuf);
110         chxj_convert_illegal_charactor_sequence(r, entryp, &ibuf, &ilen, &obuf, &olen);
111       }
112       else if (EINVAL == errno) {
113         ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
114         break;
115       }
116     }
117   }
118   *len = strlen(spos);
119   iconv_close(cd);
120
121   chxj_dump_string(r, APLOG_MARK, "RESULT Convert Encoding", spos, *len);
122   DBG(r,"REQ[%X] end   chxj_encoding()", (unsigned int)(apr_size_t)r);
123   return spos;
124 }
125
126
127 void
128 chxj_convert_illegal_charactor_sequence(request_rec *r, chxjconvrule_entry  *entryp, char **ibuf, apr_size_t *ilen, char **obuf, apr_size_t *olen)
129 {
130   if (STRCASEEQ('u','U',"UTF-8", entryp->encoding) || STRCASEEQ('u','U',"UTF8", entryp->encoding)) {
131     if ((0xe0 & **ibuf) == 0xc0) {
132       /* 2byte charactor */
133       **obuf = '?';
134       *obuf += 1;
135       *olen -= 1;
136       *ibuf += 2;
137       DBG(r, "passed 2byte.");
138     }
139     else if ((0xf0 & **ibuf) == 0xe0) {
140       /* 3byte charactor */
141       **obuf = '?';
142       *obuf += 1;
143       *olen -= 1;
144       *ibuf +=3;
145       DBG(r, "passed 3byte.");
146     }
147     else if ((0xf8 & **ibuf) == 0xf0) {
148       /* 4byte charactor */
149       **obuf = '?';
150       *obuf += 1;
151       *olen -= 1;
152       *ibuf +=4;
153       DBG(r, "passed 4byte.");
154     }
155     else if ((0xc0 & **ibuf) == 0x80) {
156       /* 1byte charactor */
157       **obuf = '?';
158       *obuf += 1;
159       *olen -= 1;
160       *ibuf += 1;
161       DBG(r, "passed 1byte.");
162     }
163     else {
164       /* unknown charactor */
165       **obuf = '?';
166       *obuf += 1;
167       *olen -= 1;
168       *ibuf += 1;
169       DBG(r, "passed 1byte.");
170     }
171   }
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 */
183       **obuf = '?';
184       *obuf += 1;
185       *olen -= 1;
186       *ibuf +=3;
187       DBG(r, "passed 3byte.");
188     }
189     else {
190       /* 2byte charactor */
191       **obuf = '?';
192       *obuf += 1;
193       *olen -= 1;
194       *ibuf += 2;
195       DBG(r, "passed 2byte.");
196     }
197   }
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)))
209        &&
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 */
213       **obuf = '?';
214       *obuf += 1;
215       *olen -= 1;
216       *ibuf += 2;
217       DBG(r, "passed 2byte.");
218     }
219     else {
220       /* 1byte charactor */
221       **obuf = '?';
222       *obuf += 1;
223       *olen -= 1;
224       *ibuf += 1;
225       DBG(r, "passed 1byte.");
226     }
227   }
228   else {
229     /* unknown 1byte charactor */
230     **obuf = '?';
231     *obuf += 1;
232     *olen -= 1;
233     *ibuf += 1;
234     DBG(r, "passed 1byte.");
235   }
236   if (ibuf && *ibuf) {
237     *ilen = strlen(*ibuf);
238     DBG(r, "new len = [%" APR_SIZE_T_FMT "].", (apr_size_t)*ilen);
239   }
240 }
241
242
243 char *
244 chxj_rencoding(request_rec *r, const char *src, apr_size_t *len)
245 {
246   char                *obuf;
247   char                *ibuf;
248   char                *spos;
249   
250   iconv_t             cd;
251   size_t              result;
252   apr_size_t          ilen;
253   apr_size_t          olen;
254   mod_chxj_config     *dconf;
255   chxjconvrule_entry  *entryp;
256
257   DBG(r,"REQ[%X] start chxj_rencoding()", (unsigned int)(apr_size_t)r);
258
259   if ((int)*len < 0) {
260     ERR(r, "runtime exception: chxj_rencoding(): invalid string size.[%d]", (int)*len);
261     DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
262     return (char *)apr_pstrdup(r->pool, "");
263   }
264
265   dconf = chxj_get_module_config(r->per_dir_config, &chxj_module);
266   if (! dconf) {
267     DBG(r,"REQ[%X] none encoding.", (unsigned int)(apr_size_t)r);
268     DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
269     return (char*)src;
270   }
271
272   entryp = chxj_apply_convrule(r, dconf->convrules);
273   if (! entryp->encoding) {
274     DBG(r,"REQ[%X] none encoding.", (unsigned int)(apr_size_t)r);
275     DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
276     return (char*)src;
277   }
278
279   if (STRCASEEQ('n','N',"none", entryp->encoding)) {
280     DBG(r,"REQ[%X] none encoding.", (unsigned int)(apr_size_t)r);
281     DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
282     return (char*)src;
283   }
284
285   ilen = *len;
286   ibuf = apr_palloc(r->pool, ilen+1);
287   if (! ibuf) {
288     DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
289     return (char*)src;
290   }
291
292   memset(ibuf, 0,   ilen+1);
293   memcpy(ibuf, src, ilen+0);
294
295   olen = ilen * 4 + 1;
296   spos = obuf = apr_palloc(r->pool, olen);
297   if (! obuf) {
298     DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
299     return ibuf;
300   }
301   DBG(r,"encode convert [%s] -> [%s]", "CP932", entryp->encoding);
302
303   memset(obuf, 0, olen);
304
305   cd = iconv_open(entryp->encoding, "CP932");
306   if (cd == (iconv_t)-1) {
307     if (EINVAL == errno) {
308       ERR(r, "The conversion from %s to %s is not supported by the implementation.", "CP932", entryp->encoding);
309     }
310     DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
311     return ibuf;
312   }
313
314   while (ilen > 0) {
315     result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
316     if (result == (size_t)(-1)) {
317       if (E2BIG == errno) {
318         ERR(r, "There is not sufficient room at *outbuf");
319         break;
320       }
321       else if (EILSEQ == errno) {
322         ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
323         chxj_convert_illegal_charactor_sequence(r, entryp, &ibuf, &ilen, &obuf, &olen);
324       }
325       else if (EINVAL == errno) {
326         ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
327         break;
328       }
329     }
330   }
331
332   *len = strlen(spos);
333   iconv_close(cd);
334
335   chxj_dump_string(r, APLOG_MARK, "RESULT Convert REncoding", spos, *len);
336   DBG(r,"REQ[%X] end   chxj_rencoding()", (unsigned int)(apr_size_t)r);
337
338   return spos;
339 }
340
341
342 char *
343 chxj_encoding_parameter(request_rec *r, const char *value, int xmlflag)
344 {
345   char *src;
346   char *src_sv;
347   char *pstat;
348   char *spos;
349   char *pair;
350   char *key;
351   char *val;
352   char *vstat;
353   char *param;
354   char *anchor_pos;
355   char *anchor = NULL;
356
357   int   use_amp_flag;
358   
359   DBG(r, "REQ[%X] start chxj_encoding_parameter()", TO_ADDR(r));
360
361   src = apr_pstrdup(r->pool, value);
362
363   anchor_pos = strchr(src, '#');
364   if (anchor_pos) {
365     anchor_pos++;
366     anchor = apr_pstrdup(r->pool, anchor_pos);
367     anchor_pos--;
368     *anchor_pos = 0;
369   }
370
371   spos = strchr(src, '?');
372   if (!spos) {
373     DBG(r, "REQ[%X] end   chxj_encoding_parameter()", (unsigned int)(apr_size_t)r);
374     if (anchor_pos) {
375       return apr_pstrcat(r->pool, src, "#", anchor, NULL);
376     } else {
377       return src;
378     }
379   }
380   *spos++ = 0;
381
382   src_sv = apr_pstrdup(r->pool, src);
383   param  = apr_palloc(r->pool, 1);
384   param[0] = 0;
385
386   for (;;) {
387     apr_size_t len;
388     char *sep_pos;
389
390     use_amp_flag = (xmlflag) ? 1 : 0;
391
392     pair = apr_strtok(spos, "&", &pstat);
393     spos = NULL;
394     if (!pair) break;
395     if (strncasecmp(pair, "amp;", 4) == 0) {
396       pair += 4;
397       use_amp_flag = 1;
398     }
399     sep_pos = strchr(pair, '=');
400     if (pair == sep_pos) {
401       key = apr_pstrdup(r->pool, "");
402     }
403     else {
404       key = apr_strtok(pair, "=", &vstat);
405       pair = NULL;
406     }
407     if (key) {
408       apr_size_t klen = (apr_size_t)strlen(key);
409       key = chxj_url_decode(r->pool, key);
410       len = (apr_size_t)strlen(key);
411       if (klen != len) {
412         key = chxj_encoding(r, key, &len);
413         key = chxj_url_encode(r->pool, key);
414       }
415 #if 0  /* XXX:2009/4/10 */
416       key = chxj_url_encode(r->pool, key);
417 #endif
418     }
419     val = apr_strtok(pair, "=", &vstat);
420     if (! val && sep_pos) {
421       val = apr_pstrdup(r->pool, "");
422     }
423     if (val) {
424       apr_size_t vlen = (apr_size_t)strlen(val);
425       val = chxj_url_decode(r->pool, val);
426       len = (apr_size_t)strlen(val);
427       if (vlen != len) {
428         val = chxj_encoding(r, val, &len);
429         val = chxj_url_encode(r->pool, val);
430       }
431 #if 0  /* XXX:2009/4/10 */
432       val = chxj_url_encode(r->pool, val);
433 #endif
434       if (strlen(param) == 0) {
435         param = apr_pstrcat(r->pool, param, key, "=", val, NULL);
436       }
437       else {
438         if (use_amp_flag) {
439           param = apr_pstrcat(r->pool, param, "&amp;", key, "=", val, NULL);
440         }
441         else {
442           param = apr_pstrcat(r->pool, param, "&", key, "=", val, NULL);
443         }
444       }
445     }
446     else {
447       if (strlen(param) == 0) {
448         param = apr_pstrcat(r->pool, param, key,  NULL);
449       }
450       else {
451         if (use_amp_flag) {
452           param = apr_pstrcat(r->pool, param, "&amp;", key, NULL);
453         }
454         else {
455           param = apr_pstrcat(r->pool, param, "&", key, NULL);
456         }
457       }
458     }
459   }
460   DBG(r, "REQ[%X] end   chxj_encoding_parameter()", TO_ADDR(r));
461
462   if (anchor_pos) {
463     return apr_pstrcat(r->pool, src_sv, "?", param, "#", anchor, NULL);
464   } else {
465     return apr_pstrcat(r->pool, src_sv, "?", param, NULL);
466   }
467 }
468
469
470 char *
471 chxj_iconv(request_rec *r, apr_pool_t *pool, const char *src, apr_size_t *len, const char *from, const char *to)
472 {
473   char                *obuf;
474   char                *ibuf;
475   char                *spos;
476   
477   iconv_t             cd;
478   size_t              result;
479   apr_size_t          ilen;
480   apr_size_t          olen;
481
482
483   if ((int)*len < 0) {
484     ERR(r, "runtime exception: chxj_iconv(): invalid string size.[%d]", (int)*len);
485     return (char *)apr_pstrdup(pool, "");
486   }
487
488   ilen = *len;
489   ibuf = apr_palloc(pool, ilen+1);
490   if (ibuf == NULL) {
491     ERR(r, "runtime exception: chxj_iconv(): Out of memory.");
492     return (char *)src;
493   }
494   memset(ibuf, 0, ilen+1);
495   memcpy(ibuf, src, ilen);
496
497   olen = ilen * 4 + 1;
498   spos = obuf = apr_palloc(pool, olen);
499   if (obuf == NULL) {
500     ERR(r, "%s:%d runtime exception: chxj_iconv(): Out of memory", APLOG_MARK);
501     return ibuf;
502   }
503   memset(obuf, 0, olen);
504   cd = iconv_open(to, from);
505   if (cd == (iconv_t)-1) {
506     if (EINVAL == errno) {
507       ERR(r, "The conversion from %s to %s is not supported by the implementation.", from, to);
508     }
509     else {
510       ERR(r, "iconv open failed. from:[%s] to:[%s] errno:[%d]", from, to, errno);
511     }
512     return ibuf;
513   }
514   while (ilen > 0) {
515     result = iconv(cd, &ibuf, &ilen, &obuf, &olen);
516     if (result == (size_t)(-1)) {
517       if (E2BIG == errno) {
518         ERR(r, "There is not sufficient room at *outbuf.");
519       }
520       else if (EILSEQ == errno) {
521         ERR(r, "An invalid multibyte sequence has been encountered in the input. input:[%s]", ibuf);
522       }
523       else if (EINVAL == errno) {
524         ERR(r, "An incomplete multibyte sequence has been encountered in the input. input:[%s]", ibuf);
525       }
526       break;
527     }
528   }
529   *len = strlen(spos);
530   iconv_close(cd);
531
532   return spos;
533 }
534 /*
535  * vim:ts=2 et
536  */