OSDN Git Service

* merge from 0.8.5
[modchxj/mod_chxj.git] / src / chxj_img_conv_format.c
1 /*
2  * Copyright (C) 2005-2008 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 <libgen.h>
18 #include "mod_chxj.h"
19 #include "chxj_img_conv_format.h"
20 #include "chxj_specified_device.h"
21 #include "chxj_str_util.h"
22 #include "chxj_qr_code.h"
23 #include "chxj_apply_convrule.h"
24 #include "chxj_url_encode.h"
25 #include "qs_parse_string.h"
26 #include "chxj_preg_replace.h"
27
28 #include <limits.h>
29
30 #include "http_core.h"
31
32 #include <wand/magick_wand.h>
33
34
35 #define EXIT_MAGICK_ERROR() \
36   do { \
37     char *description; \
38     ExceptionType severity; \
39     description=MagickGetException(magick_wand,&severity); \
40     ap_log_rerror(APLOG_MARK,APLOG_DEBUG, 0, r,"%s %s %d %s\n",__FILE__,(__func__),__LINE__,description); \
41     description=(char *) MagickRelinquishMemory(description); \
42     DestroyMagickWand(magick_wand); \
43   }while(0) 
44
45 typedef enum img_conv_mode_t {
46   IMG_CONV_MODE_NORMAL = 0,
47   IMG_CONV_MODE_THUMBNAIL,
48   IMG_CONV_MODE_WALLPAPER,
49   IMG_CONV_MODE_EZGET,
50 } img_conv_mode_t;
51
52 /*----------------------------------------------------------------------------*/
53 /* User-Agent use flag                                                        */
54 /*----------------------------------------------------------------------------*/
55 typedef enum _ua_use_flag_t {
56   UA_USE=0,               /* User-Agent is used.                              */
57   UA_IGN,                 /* User-Agent is disregarded.                       */
58 } ua_use_flag_t;
59
60 /*----------------------------------------------------------------------------*/
61 /* Request parameter maintenance structure                                    */
62 /*----------------------------------------------------------------------------*/
63 typedef struct query_string_param_t query_string_param_t;
64
65 struct query_string_param_t {
66   img_conv_mode_t   mode;
67   char              *user_agent;
68   ua_use_flag_t     ua_flag;
69
70   char              *name;      /* for EZGET */
71   long              offset;    /* for EZGET */
72   long              count;     /* for EZGET */
73   int               width;
74   int               height;
75 };
76
77 /*----------------------------------------------------------------------------*/
78 /* Device_spec when User-Agent is disregarded                                 */
79 /*----------------------------------------------------------------------------*/
80 static device_table v_ignore_spec = {
81   NULL,
82   "IGN",
83   "IGN",
84   CHXJ_SPEC_HTML,
85   640,
86   480,
87   640,
88   480,
89   1024*1024,
90   1,
91   1,
92   1,
93   0,
94   0,
95   96,
96   96,
97   65536,
98   NULL,
99   "SJIS",
100 };
101
102 /*----------------------------------------------------------------------------*/
103 /* CRC calculation table for AU                                               */
104 /*----------------------------------------------------------------------------*/
105 static unsigned short  AU_CRC_TBL[256] = {
106   0x0000,0x1021,0x2042,0x3063,0x4084,0x50A5,0x60C6,0x70E7,
107   0x8108,0x9129,0xA14A,0xB16B,0xC18C,0xD1AD,0xE1CE,0xF1EF,
108   0x1231,0x0210,0x3273,0x2252,0x52B5,0x4294,0x72F7,0x62D6,
109   0x9339,0x8318,0xB37B,0xA35A,0xD3BD,0xC39C,0xF3FF,0xE3DE,
110   0x2462,0x3443,0x0420,0x1401,0x64E6,0x74C7,0x44A4,0x5485,
111   0xA56A,0xB54B,0x8528,0x9509,0xE5EE,0xF5CF,0xC5AC,0xD58D,
112   0x3653,0x2672,0x1611,0x0630,0x76D7,0x66F6,0x5695,0x46B4,
113   0xB75B,0xA77A,0x9719,0x8738,0xF7DF,0xE7FE,0xD79D,0xC7BC,
114   0x48C4,0x58E5,0x6886,0x78A7,0x0840,0x1861,0x2802,0x3823,
115   0xC9CC,0xD9ED,0xE98E,0xF9AF,0x8948,0x9969,0xA90A,0xB92B,
116   0x5AF5,0x4AD4,0x7AB7,0x6A96,0x1A71,0x0A50,0x3A33,0x2A12,
117   0xDBFD,0xCBDC,0xFBBF,0xEB9E,0x9B79,0x8B58,0xBB3B,0xAB1A,
118   0x6CA6,0x7C87,0x4CE4,0x5CC5,0x2C22,0x3C03,0x0C60,0x1C41,
119   0xEDAE,0xFD8F,0xCDEC,0xDDCD,0xAD2A,0xBD0B,0x8D68,0x9D49,
120   0x7E97,0x6EB6,0x5ED5,0x4EF4,0x3E13,0x2E32,0x1E51,0x0E70,
121   0xFF9F,0xEFBE,0xDFDD,0xCFFC,0xBF1B,0xAF3A,0x9F59,0x8F78,
122   0x9188,0x81A9,0xB1CA,0xA1EB,0xD10C,0xC12D,0xF14E,0xE16F,
123   0x1080,0x00A1,0x30C2,0x20E3,0x5004,0x4025,0x7046,0x6067,
124   0x83B9,0x9398,0xA3FB,0xB3DA,0xC33D,0xD31C,0xE37F,0xF35E,
125   0x02B1,0x1290,0x22F3,0x32D2,0x4235,0x5214,0x6277,0x7256,
126   0xB5EA,0xA5CB,0x95A8,0x8589,0xF56E,0xE54F,0xD52C,0xC50D,
127   0x34E2,0x24C3,0x14A0,0x0481,0x7466,0x6447,0x5424,0x4405,
128   0xA7DB,0xB7FA,0x8799,0x97B8,0xE75F,0xF77E,0xC71D,0xD73C,
129   0x26D3,0x36F2,0x0691,0x16B0,0x6657,0x7676,0x4615,0x5634,
130   0xD94C,0xC96D,0xF90E,0xE92F,0x99C8,0x89E9,0xB98A,0xA9AB,
131   0x5844,0x4865,0x7806,0x6827,0x18C0,0x08E1,0x3882,0x28A3,
132   0xCB7D,0xDB5C,0xEB3F,0xFB1E,0x8BF9,0x9BD8,0xABBB,0xBB9A,
133   0x4A75,0x5A54,0x6A37,0x7A16,0x0AF1,0x1AD0,0x2AB3,0x3A92,
134   0xFD2E,0xED0F,0xDD6C,0xCD4D,0xBDAA,0xAD8B,0x9DE8,0x8DC9,
135   0x7C26,0x6C07,0x5C64,0x4C45,0x3CA2,0x2C83,0x1CE0,0x0CC1,
136   0xEF1F,0xFF3E,0xCF5D,0xDF7C,0xAF9B,0xBFBA,0x8FD9,0x9FF8,
137   0x6E17,0x7E36,0x4E55,0x5E74,0x2E93,0x3EB2,0x0ED1,0x1EF0 
138 };
139
140 /*----------------------------------------------------------------------------*/
141 /* Download page for AU                                                       */
142 /*----------------------------------------------------------------------------*/
143 static const char* HDML_FIRST_PAGE = 
144   "<HDML VERSION=3.0 TTL=0 PUBLIC=TRUE>\r\n"
145   "  <NODISPLAY>\r\n"
146   "    <ACTION TYPE=ACCEPT TASK=GOSUB DEST=\"device:data/dnld?url=%s&name=%s%s&size=%ld&disposition=%s&title=%s\">\r\n"
147   "  </NODISPLAY>\r\n"
148   "</HDML>\r\n";
149
150 static const char* HDML_SUCCESS_PAGE =
151   "<HDML VERSION=3.0 TTL=0 PUBLIC=TRUE>\r\n"
152   "  <DISPLAY>\r\n"
153   "    <ACTION TYPE=ACCEPT TASK=RETURN>\r\n"
154   "    \x83\x5f\x83\x45\x83\x93\x83\x8d\x81\x5b\x83\x68\x82\xc9\x90\xac\x8c\xf7\x82\xb5\x82\xdc\x82\xb5\x82\xbd\r\n"
155   "  </DISPLAY>\r\n"
156   "<HDML>\r\n";
157
158 static const char* HDML_FAIL_PAGE =
159   "<HDML VERSION=3.0 TTL=0 PUBLIC=TRUE>\r\n"
160   "  <DISPLAY>\r\n"
161   "    <ACTION TYPE=ACCEPT TASK=RETURN>\r\n"
162   "    \x83\x5f\x83\x45\x83\x93\x83\x8d\x81\x5b\x83\x68\x82\xc9\x8e\xb8\x94\x73\x82\xb5\x82\xdc\x82\xb5\x82\xbd\r\n"
163   "  </DISPLAY>\r\n"
164   "<HDML>\r\n";
165
166 static ap_regex_t *v_docomo_serial_pattern1 = NULL;
167 static ap_regex_t *v_docomo_serial_pattern2 = NULL;
168 static ap_regex_t *v_docomo_serial_pattern3 = NULL;
169 static ap_regex_t *v_softbank_serial_pattern1 = NULL;
170
171 /*----------------------------------------------------------------------------*/
172 /* Prototype declaration                                                      */
173 /*----------------------------------------------------------------------------*/
174 static char *s_create_workfile_name(request_rec *, 
175                                mod_chxj_config *, 
176                                const char *,
177                                query_string_param_t *);
178
179 static apr_status_t s_create_cache_file(request_rec  *r, 
180                                         const char   *tmpfile, 
181                                         device_table *spec,
182                                         apr_finfo_t  *st,
183                                         query_string_param_t* qsp,
184                                         mod_chxj_config       *conf);
185
186 static apr_status_t s_send_cache_file(  device_table          *spec,
187                                         query_string_param_t  *query_string,
188                                         request_rec           *r,
189                                         const char            *tmpfile);
190
191 static apr_status_t s_send_original_file(request_rec* r,
192                                          const char* originalfile);
193
194 static apr_status_t s_header_only_cache_file(device_table         *spec, 
195                                              query_string_param_t *query_string, 
196                                              request_rec          *r, 
197                                              const char           *tmpfile);
198
199 static query_string_param_t *s_get_query_string_param(request_rec *r);
200
201 static unsigned short s_add_crc(const char *writedata, 
202                                 apr_size_t witebyte);
203
204 static MagickWand *s_fixup_size(MagickWand *, 
205                                 request_rec *r, 
206                                 device_table *spec, 
207                                 query_string_param_t *qsp);
208
209 static MagickWand *s_fixup_color(MagickWand *magick_wand, 
210                                  request_rec *r, 
211                                  device_table *spec, 
212                                  img_conv_mode_t mode);
213 static MagickWand *s_fixup_depth(MagickWand *magick_wand, 
214                                  request_rec *r, device_table *spec);
215 static MagickWand *s_img_down_sizing(MagickWand *magick_wand, 
216                                 request_rec *r, device_table *spec);
217
218 static MagickWand *s_add_copyright(MagickWand    *magick_wand,
219                                    request_rec   *r,
220                                    device_table  *spec);
221
222 static char *s_create_blob_data(request_rec           *r,
223                                 device_table          *spec,
224                                 query_string_param_t  *qsp,
225                                 char                  *indata,
226                                 apr_size_t            *len);
227
228 static int s_img_conv_format_from_file(request_rec           *r, 
229                                        mod_chxj_config       *conf, 
230                                        const char            *user_agent,
231                                        query_string_param_t  *qsp,
232                                        device_table          *spec);
233
234
235
236 int 
237 chxj_img_conv_format_handler(request_rec *r)
238 {
239   mod_chxj_config       *conf;
240   query_string_param_t  *qsp;
241   char                  *user_agent;
242   device_table          *spec;
243   chxjconvrule_entry    *entryp;
244
245   DBG(r, "start chxj_img_conv_format_handler()");
246   
247   if ((*r->handler != 'c' && *r->handler != 'C') 
248   ||  (strcasecmp(r->handler, "chxj-picture")
249   &&  strcasecmp(r->handler, "chxj-qrcode"))) {
250     DBG(r, "end chxj_img_conv_format_handler()");
251     return DECLINED;
252   }
253
254   qsp = s_get_query_string_param(r);
255   conf = ap_get_module_config(r->per_dir_config, &chxj_module);
256   if (conf == NULL) {
257     DBG(r, "end chxj_img_conv_format_handler() conf is null");
258     return DECLINED;
259   }
260
261   if (strcasecmp(r->handler, "chxj-qrcode") == 0 &&  conf->image == CHXJ_IMG_OFF) {
262     return DECLINED;
263   }
264
265
266   /*------------------------------------------------------------------------*/
267   /* get UserAgent from http header                                         */
268   /*------------------------------------------------------------------------*/
269   /*--------------------------------------------------------------------------*/
270   /* User-Agent to spec                                                       */
271   /*--------------------------------------------------------------------------*/
272   if (qsp->user_agent) {
273     user_agent = apr_pstrdup(r->pool, qsp->user_agent);
274   }
275   else {
276     entryp = chxj_apply_convrule(r, conf->convrules);
277     if (entryp && entryp->user_agent) {
278       user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
279     }
280     else {
281       user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
282     }
283   }
284
285
286
287   if (qsp->ua_flag == UA_IGN)
288     spec = &v_ignore_spec;
289   else
290     spec = chxj_specified_device(r, user_agent);
291
292   DBG(r,"found device_name=[%s]", spec->device_name);
293   DBG(r,"User-Agent=[%s]", user_agent);
294
295 #if 0
296   if (spec->width == 0 || spec->heigh == 0)
297     return DECLINED;
298 #endif
299
300   return s_img_conv_format_from_file(r, conf, user_agent, qsp, spec);
301 }
302
303
304
305 /**
306  * It converts it from ImageData corresponding to each model.
307  *
308  * @param r   [i]
309  * @param src [i]   It is former image binary data.
310  * @param len [i/o] It is length of former image binary data.
311  */
312 char *
313 chxj_convert_image(request_rec *r, const char **src, apr_size_t *len)
314 {
315   mod_chxj_config       *conf;
316   query_string_param_t  *qsp;
317   char                  *user_agent;
318   device_table          *spec;
319   char                  *dst;
320   char                  *conv_check;
321   chxjconvrule_entry    *entryp;
322
323   DBG(r, "start chxj_convert_image()");
324
325   conv_check = (char *)apr_table_get(r->headers_in, "CHXJ_IMG_CONV");
326   if (conv_check) {
327     DBG(r, "end chxj_exchnage_image() already convert.");
328     return NULL;
329   }
330
331
332   qsp = s_get_query_string_param(r);
333   conf = ap_get_module_config(r->per_dir_config, &chxj_module);
334   if (conf == NULL) {
335     DBG(r, "end chxj_convert_image()");
336     return NULL;
337   }
338
339   /*--------------------------------------------------------------------------*/
340   /* User-Agent to spec                                                       */
341   /*--------------------------------------------------------------------------*/
342   if (qsp->user_agent) {
343     user_agent = apr_pstrdup(r->pool, qsp->user_agent);
344   }
345   else {
346     entryp = chxj_apply_convrule(r, conf->convrules);
347     if (entryp && entryp->user_agent) {
348       user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
349     }
350     else {
351       user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
352     }
353   }
354
355   if (qsp->ua_flag == UA_IGN)
356     spec = &v_ignore_spec;
357   else
358     spec = chxj_specified_device(r, user_agent);
359
360   DBG(r,"found device_name=[%s]", spec->device_name);
361   DBG(r, "User-Agent=[%s]", user_agent);
362
363   if (spec->width == 0 || spec->heigh == 0) 
364     return NULL;
365
366   dst = s_create_blob_data(r, spec, qsp, (char*)*src, len);
367   if (dst == NULL) 
368     *len = 0;
369
370   DBG(r, "end chxj_convert_image()");
371
372   return dst;
373 }
374
375 static int
376 s_img_conv_format_from_file(
377                 request_rec           *r, 
378                 mod_chxj_config       *conf, 
379                 const char            *user_agent,
380                 query_string_param_t  *qsp,
381                 device_table          *spec)
382 {
383   apr_status_t   rv;
384   apr_finfo_t    st;
385   apr_finfo_t    cache_st;
386   char           *tmpfile;
387   int            try_count;
388
389   if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
390     /*
391      * If ``ua'' parameter is specified, it must be CHXJ_SPEC_HTML.
392      */
393     return s_send_original_file(r, r->filename);
394   }
395
396   /*--------------------------------------------------------------------------*/
397   /* Create Workfile Name                                                     */
398   /*--------------------------------------------------------------------------*/
399   tmpfile = s_create_workfile_name(r, conf, user_agent, qsp);
400   DBG(r,"workfile=[%s]", tmpfile);
401
402   rv = apr_stat(&st, r->filename, APR_FINFO_MIN, r->pool);
403   if (rv != APR_SUCCESS)
404     return HTTP_NOT_FOUND;
405
406   try_count = CACHE_RETRY_COUNT;
407   do {
408     rv = apr_stat(&cache_st, tmpfile, APR_FINFO_MIN, r->pool);
409
410     if (rv != APR_SUCCESS || cache_st.ctime < st.mtime) {
411       /*------------------------------------------------------------------------*/
412       /* It tries to make the cash file when it doesn't exist or there is       */
413       /* change time later since the making time of the cash file.              */
414       /*------------------------------------------------------------------------*/
415       rv = s_create_cache_file(r,tmpfile, spec, &st, qsp, conf);
416       if (rv != OK)
417         return rv;
418     }
419
420     DBG(r,"color=[%d]", spec->color);
421     if (! r->header_only)  {
422       rv = s_send_cache_file(spec, qsp,r, tmpfile);
423     }
424     else {
425       rv = s_header_only_cache_file(spec, qsp, r, tmpfile);
426     }
427     if (rv == OK) break;
428     if (rv == HTTP_NOT_FOUND) {
429       DBG(r, "recheck wait... try_count[%d]", try_count);
430       apr_sleep(CACHE_RECHECK_WAIT);
431     }
432   } while (try_count--);
433   if (try_count <= 0) {
434     WRN(r, "cache retry failure....");
435     WRN(r, "cache file was deleted...");
436   }
437   apr_table_setn(r->headers_in, "CHXJ_IMG_CONV", "done");
438
439   DBG(r,"end chxj_img_conv_format");
440
441   return rv;
442 }
443
444
445 static apr_status_t
446 s_create_cache_file(request_rec          *r, 
447                     const char           *tmpfile, 
448                     device_table         *spec, 
449                     apr_finfo_t          *st, 
450                     query_string_param_t *qsp,
451                     mod_chxj_config      *conf)
452 {
453   apr_status_t       rv;
454   apr_size_t         readbyte;
455   apr_size_t         writebyte;
456   unsigned short     crc;
457   img_conv_mode_t    mode = qsp->mode;
458
459   char               *writedata = NULL;
460   char               *readdata  = NULL;
461
462   apr_file_t         *fout;
463   apr_file_t         *fin;
464
465   MagickWand         *magick_wand;
466
467   apr_finfo_t        cache_dir_st;
468
469   if ((*r->handler == 'c' || *r->handler == 'C') 
470       &&  strcasecmp(r->handler, "chxj-qrcode") == 0) {
471     /*------------------------------------------------------------------------*/
472     /* QRCODEÍѤΥե¡¥¤¥ë¤Î¾ì¹ç                                               */
473     /*------------------------------------------------------------------------*/
474     Doc       doc;
475     Node      *root;
476     qr_code_t qrcode;
477     int       sts;
478
479     memset(&doc,    0, sizeof(Doc));
480     memset(&qrcode, 0, sizeof(qr_code_t));
481     doc.r = r;
482     doc.parse_mode  = PARSE_MODE_CHTML;
483     qrcode.doc      = &doc;
484     qrcode.r        = r;
485
486     qs_init_malloc(&doc);
487
488     root = qs_parse_file(&doc, r->filename);
489
490     chxj_qrcode_node_to_qrcode(&qrcode, root);
491
492     qs_all_free(&doc,QX_LOGMARK);
493
494     sts = chxj_qrcode_create_image_data(&qrcode, &readdata, &readbyte);
495     if (sts != OK) {
496       ERR(r, "qrcode create failed.");
497       return sts;
498     }
499   }
500   else {
501     /*------------------------------------------------------------------------*/
502     /* Ä̾ï¤Î¥¤¥á¡¼¥¸¥Õ¥¡¥¤¥ë¤Î¾ì¹ç                                           */
503     /*------------------------------------------------------------------------*/
504     rv = apr_file_open(&fin, 
505                     r->filename, 
506                     APR_READ|APR_BINARY ,
507                     APR_OS_DEFAULT, 
508                     r->pool);
509     if (rv != APR_SUCCESS) {
510       DBG(r,"file open failed.[%s]", r->filename);
511       return HTTP_NOT_FOUND;
512     }
513   
514     readdata = apr_palloc(r->pool, st->size);
515     rv = apr_file_read_full(fin, (void*)readdata, st->size, &readbyte);
516     if (rv != APR_SUCCESS || readbyte != st->size) {
517       DBG(r,"file read failed.[%s]", r->filename);
518       apr_file_close(fin);
519   
520       return HTTP_NOT_FOUND;
521     }
522   }
523   DBG(r,"start img convert");
524
525
526   magick_wand = NewMagickWand();
527   if (MagickReadImageBlob(magick_wand,readdata, readbyte) == MagickFalse) {
528     EXIT_MAGICK_ERROR();
529     return HTTP_NOT_FOUND;
530   }
531
532   if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
533     /*
534      * The size of the image is changed.
535      */
536     DBG(r,"call s_fixup_size()");
537   
538     if ((magick_wand = s_fixup_size(magick_wand, r, spec, qsp)) == NULL) 
539       return HTTP_NOT_FOUND;
540   
541     /*
542      * The colors of the image is changed.
543      */
544     DBG(r,"call s_fixup_color()");
545   
546     if ((magick_wand = s_fixup_color(magick_wand, r,spec, mode)) == NULL) 
547       return HTTP_NOT_FOUND;
548   
549     /*
550      * DEPTH of the image is changed.
551      */
552     DBG(r,"call s_fixup_depth()");
553   
554     if ((magick_wand = s_fixup_depth(magick_wand, r, spec)) == NULL) 
555       return HTTP_NOT_FOUND;
556   
557   
558   
559     DBG(r,"start convert and compression");
560   
561     if (spec->available_jpeg) {
562       if (MagickSetImageCompression(magick_wand,JPEGCompression) == MagickFalse) {
563         EXIT_MAGICK_ERROR();
564         return HTTP_NOT_FOUND;
565       }
566   
567       if (MagickSetImageFormat(magick_wand, "jpg") == MagickFalse) {
568         EXIT_MAGICK_ERROR();
569         return HTTP_NOT_FOUND;
570       }
571   
572       if (MagickStripImage(magick_wand) == MagickFalse) {
573         EXIT_MAGICK_ERROR();
574         return HTTP_NOT_FOUND;
575       }
576   
577       if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
578         return HTTP_NOT_FOUND;
579   
580       r->content_type = apr_psprintf(r->pool, "image/jpeg");
581       DBG(r,"convert to jpg");
582     }
583     else
584     if (spec->available_png) {
585   
586       if (MagickSetImageCompression(magick_wand,ZipCompression) == MagickFalse) {
587         EXIT_MAGICK_ERROR();
588         return HTTP_NOT_FOUND;
589       }
590   
591       if (MagickSetImageFormat(magick_wand, "png") == MagickFalse) {
592         EXIT_MAGICK_ERROR();
593         return HTTP_NOT_FOUND;
594       }
595   
596       if (MagickStripImage(magick_wand) == MagickFalse) {
597         EXIT_MAGICK_ERROR();
598         return HTTP_NOT_FOUND;
599       }
600   
601       if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL) 
602         return HTTP_NOT_FOUND;
603   
604       r->content_type = apr_psprintf(r->pool, "image/png");
605       DBG(r, "convert to png");
606     }
607     else
608     if (spec->available_gif) {
609   
610       if (MagickSetImageCompression(magick_wand,LZWCompression) == MagickFalse) {
611         EXIT_MAGICK_ERROR();
612         return HTTP_NOT_FOUND;
613       }
614   
615       if (MagickSetImageFormat(magick_wand, "gif") == MagickFalse) {
616         EXIT_MAGICK_ERROR();
617         return HTTP_NOT_FOUND;
618       }
619   
620       if (MagickStripImage(magick_wand) == MagickFalse) {
621         EXIT_MAGICK_ERROR();
622         return HTTP_NOT_FOUND;
623       }
624   
625       if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL) 
626         return HTTP_NOT_FOUND;
627   
628       r->content_type = apr_psprintf(r->pool, "image/gif");
629   
630       DBG(r,"convert to gif");
631     }
632     else
633     if (spec->available_bmp2 || spec->available_bmp4) {
634   
635       if (MagickSetImageCompression(magick_wand,NoCompression) == MagickFalse) {
636         EXIT_MAGICK_ERROR();
637         return HTTP_NOT_FOUND;
638       }
639   
640       if (MagickSetImageFormat(magick_wand, "bmp") == MagickFalse) {
641         EXIT_MAGICK_ERROR();
642         return HTTP_NOT_FOUND;
643       }
644   
645       if (MagickStripImage(magick_wand) == MagickFalse) {
646         EXIT_MAGICK_ERROR();
647         return HTTP_NOT_FOUND;
648       }
649   
650       if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL) 
651         return HTTP_NOT_FOUND;
652   
653       r->content_type = apr_psprintf(r->pool, "image/bmp");
654   
655       DBG(r, "convert to bmp(unsupported)");
656     }
657   
658     /*
659      * Add Comment (Copyright and so on.)
660      */
661     DBG(r, "call s_add_copyright()");
662   
663     if ((magick_wand = s_add_copyright(magick_wand, r, spec)) == NULL) 
664       return HTTP_NOT_FOUND;
665   }
666   else {
667     char *fmt;
668     fmt = MagickGetImageFormat(magick_wand);
669     if (fmt == NULL) {
670       if (MagickSetImageFormat(magick_wand, "jpg") == MagickFalse) {
671         EXIT_MAGICK_ERROR();
672         return HTTP_NOT_FOUND;
673       }
674   
675       r->content_type = apr_psprintf(r->pool, "image/jpeg");
676     }
677     else {
678       if (strcasecmp(fmt, "jpg") == 0) {
679         r->content_type = apr_psprintf(r->pool, "image/jpeg");
680       }
681       else
682       if (strcasecmp(fmt, "jpeg") == 0) {
683         r->content_type = apr_psprintf(r->pool, "image/jpeg");
684       }
685       else
686       if (strcasecmp(fmt, "gif") == 0) {
687         r->content_type = apr_psprintf(r->pool, "image/gif");
688       }
689       else
690       if (strcasecmp(fmt, "png") == 0) {
691         r->content_type = apr_psprintf(r->pool, "image/png");
692       }
693     }
694   }
695
696   writedata = (char *)MagickGetImageBlob(magick_wand, &writebyte);
697
698   if (! writebyte) {
699     DestroyMagickWand(magick_wand);
700     ERR(r,"convert failure to Jpeg [%s]", tmpfile);
701     return HTTP_INTERNAL_SERVER_ERROR;
702   }
703
704   DBG(r, "end convert and compression");
705
706   /* check limit */
707   /* XXX:START - I will rewrite it when leaving. */
708   rv = apr_stat(&cache_dir_st, conf->image_cache_dir, APR_FINFO_MIN, r->pool);
709   if (rv != APR_SUCCESS) {
710     DestroyMagickWand(magick_wand);
711     ERR(r,"dir stat error.[%s]", conf->image_cache_dir);
712     if (writedata) free(writedata);
713     return HTTP_INTERNAL_SERVER_ERROR;
714   }
715   
716   for (;;) {
717     /* delete candidate */
718     apr_finfo_t dcf;   
719     /* get dir files size */
720     apr_dir_t *dir;
721     unsigned long total_size = 0;
722     int found_file = 0;
723     unsigned long max_size = (! conf->image_cache_limit) ? DEFAULT_IMAGE_CACHE_LIMIT : conf->image_cache_limit;
724     char *delete_file_name;
725     DBG(r, "conf->image_cache_limit:[%lu] max_size:[%lu]", conf->image_cache_limit, max_size);
726
727     rv = apr_dir_open(&dir, conf->image_cache_dir, r->pool);
728     if (rv != APR_SUCCESS) { 
729       DestroyMagickWand(magick_wand);
730       ERR(r,"dir open error.[%s]", conf->image_cache_dir);
731       if (writedata) free(writedata);
732       return HTTP_INTERNAL_SERVER_ERROR;
733     }
734     memset(&dcf, 0, sizeof(apr_finfo_t));
735     dcf.atime = (apr_time_t)LONG_LONG_MAX;
736     for (;;) {
737       apr_finfo_t dirf;
738       rv = apr_dir_read(&dirf, APR_FINFO_SIZE|APR_FINFO_NAME|APR_FINFO_DIRENT|APR_FINFO_ATIME , dir);
739       if (rv != APR_SUCCESS) {
740         break;
741       }
742       if (dirf.name && strcmp(dirf.name, ".") != 0 && strcmp(dirf.name, "..") != 0) {
743         total_size += (unsigned long)dirf.size;
744         DBG(r, "dirf.name=[%s] dirf.size=[%ld] dirf.atime=[%lld]", 
745                dirf.name, (long)dirf.size, (long long int)dirf.atime);
746         if (dcf.atime >= dirf.atime) {
747           memcpy(&dcf, &dirf, sizeof(apr_finfo_t));
748         }
749         found_file++;
750       }
751     }
752     apr_dir_close(dir);
753     if (total_size + writebyte < max_size) {
754       DBG(r, "There is an enough size in cache. total_size:[%lu] max_size:[%lu] found_file=[%d] max_size=[%lu]", total_size, max_size, found_file, max_size);
755       break;
756     }
757     if (found_file == 0 && writebyte >= max_size) {
758       ERR(r, "================================================");
759       ERR(r, "cache space is too small...");
760       ERR(r, "At least the same size as %luByte is necessary for me.", (unsigned long)writebyte);
761       ERR(r, "Please specify the ChxjImageCacheLimit that is larger than now value. ");
762       ERR(r, "================================================");
763       DestroyMagickWand(magick_wand);
764       if (writedata) free(writedata);
765       return HTTP_INTERNAL_SERVER_ERROR;
766     }
767     DBG(r, "Image Cache dir is full. total_size:[%lu] max_size:[%lu]", 
768            total_size + writebyte, max_size);
769     /* search delete candidate */
770     delete_file_name = apr_psprintf(r->pool, "%s/%s", conf->image_cache_dir, dcf.name);
771     DBG(r, "delete image cache target:[%s] atime:[%lld]", delete_file_name, dcf.atime);
772     rv = apr_file_remove(delete_file_name, r->pool);
773     if (rv != APR_SUCCESS) {
774       ERR(r, "cache file delete failure.[%s]", delete_file_name);
775       if (writedata) free(writedata);
776       return HTTP_INTERNAL_SERVER_ERROR;
777     }
778     DBG(r, "deleted image cache target:[%s]", delete_file_name);
779     if (total_size + writebyte - dcf.size < max_size) {
780       DBG(r, "OK, there is an enough size in cache.");
781       break;
782     }
783   }
784   /* XXX:END - I will rewrite it when leaving. */
785   
786   /* to cache */
787   rv = apr_file_open(&fout, tmpfile,
788                   APR_WRITE| APR_CREATE | APR_BINARY | APR_SHARELOCK ,APR_OS_DEFAULT,
789                   r->pool);
790   if (rv != APR_SUCCESS) {
791     DestroyMagickWand(magick_wand);
792     if (writedata) free(writedata);
793     ERR(r,"file open error.[%s]", tmpfile);
794     return HTTP_INTERNAL_SERVER_ERROR;
795   }
796
797   rv = apr_file_write(fout, (void*)writedata, &writebyte);
798   if (rv != APR_SUCCESS) {
799     DestroyMagickWand(magick_wand);
800     apr_file_close(fout);
801     if (writedata) free(writedata);
802     return HTTP_INTERNAL_SERVER_ERROR;
803   }
804
805   /*
806    * CRC is added for AU for EzGET.
807    */
808   if (spec->html_spec_type == CHXJ_SPEC_XHtml_Mobile_1_0
809   ||  spec->html_spec_type == CHXJ_SPEC_Hdml            ) {
810
811     crc = s_add_crc(writedata, writebyte);
812
813     rv = apr_file_putc((crc >> 8)  & 0xff, fout);
814     if (rv != APR_SUCCESS) {
815       DestroyMagickWand(magick_wand);
816       if (writedata) free(writedata);
817       return HTTP_INTERNAL_SERVER_ERROR;
818     }
819
820     rv = apr_file_putc( crc        & 0xff, fout);
821     if (rv != APR_SUCCESS) {
822       DestroyMagickWand(magick_wand);
823       if (writedata) free(writedata);
824       return HTTP_INTERNAL_SERVER_ERROR;
825     }
826   }
827
828   DestroyMagickWand(magick_wand);
829
830   DBG(r,"free writedata area");
831   if (writedata) free(writedata);
832   DBG(r,"free writedata area");
833
834   rv = apr_file_close(fout);
835   if (rv != APR_SUCCESS) {
836     DBG(r,"file write error.[%s]", tmpfile);
837     return HTTP_INTERNAL_SERVER_ERROR;
838   }
839
840   return OK;
841 }
842
843
844 static char *
845 s_create_blob_data(request_rec   *r, 
846                    device_table  *spec, 
847                    query_string_param_t *qsp,
848                    char        *indata,
849                    apr_size_t  *len)
850 {
851   apr_size_t         writebyte;
852   unsigned short     crc;
853   img_conv_mode_t    mode = qsp->mode;
854
855   char               *writedata = NULL;
856   char               *dst       = NULL;
857
858   MagickWand         *magick_wand;
859
860   magick_wand = NewMagickWand();
861
862   if (MagickReadImageBlob(magick_wand,indata, *len) == MagickFalse) {
863     ERR(r,"MagickReadImageBlob failure. indata[%s] len[%d]", indata, *len);
864     EXIT_MAGICK_ERROR();
865     return NULL;
866   }
867
868   /*
869    * The size of the image is changed.
870    */
871   DBG(r, "call s_fixup_size()");
872
873   if ((magick_wand = s_fixup_size(magick_wand, r, spec, qsp)) == NULL)
874     return NULL;
875
876   /*
877    * The colors of the image is changed.
878    */
879   DBG(r, "call s_fixup_color()");
880
881   if ((magick_wand = s_fixup_color(magick_wand, r,spec, mode)) == NULL)
882     return NULL;
883
884   /*
885    * DEPTH of the image is changed.
886    */
887
888   DBG(r,"call s_fixup_depth()");
889
890   if ((magick_wand = s_fixup_depth(magick_wand, r, spec)) == NULL)
891     return NULL;
892
893
894
895   DBG(r,"start convert and compression");
896
897   if (spec->available_jpeg) {
898     if (MagickSetImageCompression(magick_wand,JPEGCompression) == MagickFalse) {
899       EXIT_MAGICK_ERROR();
900       return NULL;
901     }
902
903     if (MagickSetImageFormat(magick_wand, "jpg") == MagickFalse) {
904       EXIT_MAGICK_ERROR();
905       return NULL;
906     }
907
908     if (MagickStripImage(magick_wand) == MagickFalse) {
909       EXIT_MAGICK_ERROR();
910       return NULL;
911     }
912
913     if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
914       return NULL;
915
916     r->content_type = apr_psprintf(r->pool, "image/jpeg");
917
918     DBG(r, "convert to jpg");
919   }
920   else
921   if (spec->available_png) {
922     if (MagickSetImageCompression(magick_wand,ZipCompression) == MagickFalse) {
923       EXIT_MAGICK_ERROR();
924       return NULL;
925     }
926
927     if (MagickSetImageFormat(magick_wand, "png") == MagickFalse) {
928       EXIT_MAGICK_ERROR();
929       return NULL;
930     }
931
932     if (MagickStripImage(magick_wand) == MagickFalse) {
933       EXIT_MAGICK_ERROR();
934       return NULL;
935     }
936
937     if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
938       return NULL;
939
940     r->content_type = apr_psprintf(r->pool, "image/png");
941
942     DBG(r,"convert to png");
943   }
944   else
945   if (spec->available_gif) {
946
947     if (MagickSetImageCompression(magick_wand,LZWCompression) == MagickFalse) {
948       EXIT_MAGICK_ERROR();
949       return NULL;
950     }
951
952     if (MagickSetImageFormat(magick_wand, "gif") == MagickFalse) {
953       EXIT_MAGICK_ERROR();
954       return NULL;
955     }
956
957     if (MagickStripImage(magick_wand) == MagickFalse) {
958       EXIT_MAGICK_ERROR();
959       return NULL;
960     }
961
962     if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
963       return NULL;
964
965     r->content_type = apr_psprintf(r->pool, "image/gif");
966
967     DBG(r,"convert to gif");
968   }
969   else
970   if (spec->available_bmp2 || spec->available_bmp4) {
971     if (MagickSetImageCompression(magick_wand,NoCompression) == MagickFalse) {
972       EXIT_MAGICK_ERROR();
973       return NULL;
974     }
975
976     if (MagickSetImageFormat(magick_wand, "bmp") == MagickFalse) {
977       EXIT_MAGICK_ERROR();
978       return NULL;
979     }
980
981     if (MagickStripImage(magick_wand) == MagickFalse) {
982       EXIT_MAGICK_ERROR();
983       return NULL;
984     }
985
986     if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
987       return NULL;
988
989     r->content_type = apr_psprintf(r->pool, "image/bmp");
990
991     DBG(r,"convert to bmp(unsupported)");
992   }
993   /*--------------------------------------------------------------------------*/
994   /* Add Comment (Copyright and so on.)                                       */
995   /*--------------------------------------------------------------------------*/
996   DBG(r,"call s_add_copyright()");
997
998   if ((magick_wand = s_add_copyright(magick_wand, r, spec)) == NULL)
999     return NULL;
1000
1001   writedata = (char*)MagickGetImageBlob(magick_wand, &writebyte);
1002
1003   if (! writebyte) {
1004     DestroyMagickWand(magick_wand);
1005     DBG(r,"convert failure to Jpeg ");
1006     return NULL;
1007   }
1008
1009   DBG(r,"end convert and compression");
1010
1011
1012   
1013   dst = apr_palloc(r->pool, writebyte+2);
1014
1015   memcpy(dst, writedata, writebyte);
1016   /*--------------------------------------------------------------------------*/
1017   /* CRC is added for AU for EzGET.                                           */
1018   /*--------------------------------------------------------------------------*/
1019   if (spec->html_spec_type == CHXJ_SPEC_XHtml_Mobile_1_0
1020   ||  spec->html_spec_type == CHXJ_SPEC_Hdml) {
1021     crc = s_add_crc(writedata, writebyte);
1022     dst[writebyte + 0] = (crc >> 8) & 0xff;
1023     dst[writebyte + 1] = (crc     ) & 0xff;
1024     writebyte += 2;
1025   }
1026
1027   DestroyMagickWand(magick_wand);
1028
1029   *len = writebyte;
1030   return dst;
1031 }
1032
1033
1034 static MagickWand *
1035 s_fixup_size(MagickWand *magick_wand, 
1036              request_rec *r, 
1037              device_table *spec, 
1038              query_string_param_t *qsp)
1039 {
1040   img_conv_mode_t mode = qsp->mode;
1041   int oldw;
1042   int oldh;
1043   int neww;
1044   int newh;
1045   int c_width;
1046   int c_heigh;
1047
1048   oldw = MagickGetImageWidth(magick_wand);
1049   oldh = MagickGetImageHeight(magick_wand);
1050
1051   DBG(r,"detect width=[%d]", oldw);
1052   DBG(r,"detect heigh=[%d]", oldh);
1053
1054   neww = oldw;
1055   newh = oldh;
1056
1057   DBG(r,"detect spec width=[%d]", spec->width);
1058   DBG(r,"detect spec heigh=[%d]", spec->heigh);
1059
1060   c_width = spec->width;
1061   c_heigh = spec->heigh;
1062
1063   switch(mode) {
1064   case IMG_CONV_MODE_THUMBNAIL:
1065
1066     DBG(r,"**** detect thumbnail mode ****");
1067
1068     if (neww > c_width) {
1069       newh = (int)((double)newh * (double)((double)c_width / (double)neww));
1070       neww = (int)((double)neww * (double)((double)c_width / (double)neww));
1071     }
1072     if (newh > c_heigh) {
1073       neww = (int)((double)neww * (double)((double)c_heigh / (double)newh));
1074       newh = (int)((double)newh * (double)((double)c_heigh / (double)newh));
1075     }
1076
1077     neww = (int)((double)(neww / 3) * 0.8);
1078     newh = (int)((double)(newh / 3) * 0.8);
1079     break;
1080
1081   case IMG_CONV_MODE_WALLPAPER:
1082   case IMG_CONV_MODE_EZGET:
1083
1084     DBG(r,"**** detect wallpaper mode ****");
1085
1086     if (spec->wp_width && spec->wp_heigh) {
1087       c_width = spec->wp_width;
1088       c_heigh = spec->wp_heigh;
1089     }
1090
1091     DBG(r,"calc new width and height");
1092
1093     neww = (int)((double)neww * (double)((double)c_heigh / (double)newh));
1094     newh = (int)((double)newh * (double)((double)c_heigh / (double)newh));
1095
1096     DBG(r,"newh = [%d] neww = [%d]", newh, neww);
1097     break;
1098
1099   default:
1100
1101     DBG(r,"**** detect normal mode ****");
1102
1103     if (qsp->ua_flag != UA_IGN && spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
1104       if (neww > c_width) {
1105         newh = (int)((double)newh * (double)((double)c_width / (double)neww));
1106         neww = (int)((double)neww * (double)((double)c_width / (double)neww));
1107       }
1108
1109       if (newh > c_heigh) {
1110         neww = (int)((double)neww * (double)((double)c_heigh / (double)newh));
1111         newh = (int)((double)newh * (double)((double)c_heigh / (double)newh));
1112       }
1113     }
1114     break;
1115   }
1116
1117   if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
1118     DBG(r,"convert width=[%d --> %d]", oldw, neww);
1119     DBG(r,"convert heigh=[%d --> %d]", oldh, newh);
1120   
1121     MagickResetIterator(magick_wand);
1122   
1123     while (MagickNextImage(magick_wand) != MagickFalse) {
1124       switch (mode) {
1125       case IMG_CONV_MODE_WALLPAPER:
1126       case IMG_CONV_MODE_EZGET:
1127   
1128         if (MagickResizeImage(magick_wand,neww,newh,LanczosFilter,1.0) == MagickFalse) {
1129           EXIT_MAGICK_ERROR();
1130           return NULL;
1131         }
1132   
1133         if (MagickCropImage(magick_wand, 
1134                         (unsigned long)c_width, 
1135                         (unsigned long)c_heigh,
1136                         (long)((neww - c_width) / 2),
1137                         (long)((newh - c_heigh) / 2)) == MagickFalse) {
1138           EXIT_MAGICK_ERROR();
1139           return NULL;
1140         }
1141         break;
1142   
1143       case IMG_CONV_MODE_NORMAL:
1144         if (qsp->width) {
1145           DBG(r,"convert width=[%d --> %d]", neww, qsp->width);
1146           neww = qsp->width;
1147         }
1148         if (qsp->height) {
1149           DBG(r,"convert heigh=[%d --> %d]", newh, qsp->height);
1150           newh = qsp->height;
1151         }
1152   
1153       default:
1154         if (MagickResizeImage(magick_wand,neww,newh,LanczosFilter,1.0) == MagickFalse) {
1155           EXIT_MAGICK_ERROR();
1156           return NULL;
1157         }
1158         break;
1159       }
1160   
1161       if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
1162         if (MagickSetImageUnits(magick_wand, PixelsPerInchResolution) == MagickFalse) {
1163           EXIT_MAGICK_ERROR();
1164           return NULL;
1165         }
1166     
1167         if (MagickSetImageResolution(magick_wand,
1168                                      (double)spec->dpi_width,
1169                                      (double)spec->dpi_heigh) == MagickFalse) {
1170           EXIT_MAGICK_ERROR();
1171           return NULL;
1172         }
1173     
1174         if (MagickSetImageDispose(magick_wand, BackgroundDispose) == MagickFalse) {
1175           EXIT_MAGICK_ERROR();
1176           return NULL;
1177         }
1178       }
1179     }
1180   }
1181   return magick_wand;
1182 }
1183
1184
1185 static MagickWand *
1186 s_fixup_color(MagickWand *magick_wand, request_rec *r, device_table *spec, img_conv_mode_t UNUSED(mode))
1187 {
1188   DBG(r,"start chxj_fixup_clor()");
1189
1190   if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
1191     DBG(r, "Pass s_fixup_color proc");
1192     return magick_wand;
1193   }
1194
1195   if (spec->color >= 256) {
1196
1197     DBG(r,"call MagickQuantizeImage() spec->color=[%d]",spec->color);
1198
1199     if (MagickQuantizeImage(magick_wand,
1200                            spec->color,
1201                            RGBColorspace,
1202                            0,
1203                            1,
1204                            0) == MagickFalse) {
1205       EXIT_MAGICK_ERROR();
1206       return NULL;
1207     }
1208
1209     DBG(r,"call end MagickQuantizeImage() spec->color=[%d]",spec->color);
1210
1211   }
1212   else {
1213     DBG(r,"call MagickQuantizeImage() spec->color=[%d]",spec->color);
1214
1215     if (MagickQuantizeImage(magick_wand,
1216                            spec->color,
1217                            GRAYColorspace,
1218                            0,
1219                            1,
1220                            0) == MagickFalse) {
1221       EXIT_MAGICK_ERROR();
1222       return NULL;
1223     }
1224
1225     DBG(r,"call end MagickQuantizeImage() spec->color=[%d]",spec->color);
1226   }
1227
1228
1229   DBG(r,"end chxj_fixup_clor()");
1230
1231   return magick_wand;
1232 }
1233
1234
1235
1236 static MagickWand *
1237 s_fixup_depth(MagickWand *magick_wand, request_rec *r, device_table *spec)
1238 {
1239   if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
1240     DBG(r, "Pass s_fixup_depth proc");
1241     return magick_wand;
1242   }
1243
1244   if (spec->color == 15680000) {
1245     if (MagickSetImageDepth(magick_wand, 24) == MagickFalse) {
1246       EXIT_MAGICK_ERROR();
1247       return NULL;
1248     }
1249   }
1250   else 
1251   if (spec->color == 262144) {
1252     if (MagickSetImageDepth(magick_wand, 18) == MagickFalse) {
1253       EXIT_MAGICK_ERROR();
1254       return NULL;
1255     }
1256   }
1257   else
1258   if (spec->color == 65536) {
1259     if (MagickSetImageDepth(magick_wand, 16) == MagickFalse) {
1260       EXIT_MAGICK_ERROR();
1261       return NULL;
1262     }
1263   }
1264   else
1265   if (spec->color == 4096) {
1266     if (MagickSetImageDepth(magick_wand, 12) == MagickFalse) {
1267       EXIT_MAGICK_ERROR();
1268       return NULL;
1269     }
1270   }
1271   else
1272   if (spec->color == 256) {
1273     if (MagickSetImageDepth(magick_wand, 8) == MagickFalse) {
1274       EXIT_MAGICK_ERROR();
1275       return NULL;
1276     }
1277   }
1278   else
1279   if (spec->color == 4) {
1280     if (MagickSetImageDepth(magick_wand, 2) == MagickFalse) {
1281       EXIT_MAGICK_ERROR();
1282       return NULL;
1283     }
1284   }
1285   else
1286   if (spec->color == 2) {
1287     if (MagickSetImageDepth(magick_wand, 1) == MagickFalse) {
1288       EXIT_MAGICK_ERROR();
1289       return NULL;
1290     }
1291   }
1292
1293   return magick_wand;
1294 }
1295
1296
1297 static MagickWand *
1298 s_add_copyright(MagickWand *magick_wand, request_rec *r, device_table *spec)
1299 {
1300   mod_chxj_config *conf = ap_get_module_config(r->per_dir_config, &chxj_module);
1301
1302   if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
1303     DBG(r, "Pass add_copiright proc");
1304     return magick_wand;
1305   }
1306
1307   if (conf->image_copyright) {
1308
1309     DBG(r, "Add COPYRIGHT [%s]", conf->image_copyright);
1310
1311     if (spec->html_spec_type == CHXJ_SPEC_Jhtml) {
1312       apr_table_setn(r->headers_out, "x-jphone-copyright", "no-transfer");
1313       if (MagickCommentImage(magick_wand, 
1314                              apr_psprintf(r->pool, 
1315                                           "Copyright(C) %s", 
1316                                           conf->image_copyright)) == MagickFalse) 
1317         goto on_error;
1318     }
1319     else
1320     if (spec->html_spec_type == CHXJ_SPEC_XHtml_Mobile_1_0
1321     ||  spec->html_spec_type == CHXJ_SPEC_Hdml) {
1322       if (MagickCommentImage(magick_wand, 
1323                              apr_psprintf(r->pool, 
1324                                          "kddi_copyright=on,%s", 
1325                                           conf->image_copyright)) == MagickFalse) 
1326         goto on_error;
1327     }
1328     else {
1329       if (MagickCommentImage(magick_wand, 
1330                             apr_psprintf(r->pool, 
1331                                          "copy=\"NO\",%s",
1332                                          conf->image_copyright)) == MagickFalse)
1333         goto on_error;
1334     }
1335   }
1336   else {
1337     if (MagickCommentImage(magick_wand, "mod_chxj") == MagickFalse)
1338       goto on_error;
1339   }
1340   return magick_wand;
1341
1342 on_error:
1343   EXIT_MAGICK_ERROR();
1344   return NULL;
1345 }
1346
1347
1348 static MagickWand *
1349 s_img_down_sizing(MagickWand *magick_wand, request_rec *r, device_table *spec)
1350 {
1351   MagickBooleanType  status;
1352   unsigned long quality = 70;
1353   apr_size_t    writebyte = 0;
1354   char          *writedata;
1355   apr_size_t    prev_size = 0;
1356   int           revers_flag = 0;
1357
1358   writedata = (char *)MagickGetImageBlob(magick_wand, &writebyte);
1359   prev_size = writebyte;
1360
1361   do {
1362     if (MagickSetImageCompressionQuality(magick_wand, quality) == MagickFalse) {
1363       EXIT_MAGICK_ERROR();
1364       return NULL;
1365     }
1366
1367     writedata = (char *)MagickGetImageBlob(magick_wand, &writebyte);
1368     if (writebyte >= prev_size || revers_flag) {
1369       DBG(r, "quality=[%ld] size=[%d]", (long)quality, (int)writebyte);
1370       revers_flag = 1;
1371       quality += 10;
1372       if (quality > 100) {
1373         if (MagickSetImageCompression(magick_wand,NoCompression) == MagickFalse) {
1374           EXIT_MAGICK_ERROR();
1375           return NULL;
1376         }
1377         break;
1378       }
1379       prev_size = writebyte;
1380       continue;
1381     }
1382
1383     DBG(r, "quality=[%ld] size=[%d]", (long)quality, (int)writebyte);
1384
1385     if (spec->cache == 0)
1386       break;
1387
1388     if (writebyte <= (unsigned int)spec->cache)
1389       break;
1390
1391     quality -= 10;
1392
1393     if (quality == 0 || quality > 100)
1394       break;
1395
1396   }
1397   while (1);
1398
1399
1400   if (spec->cache > 0 
1401   &&  writebyte   > (unsigned int)spec->cache) {
1402     unsigned long now_color = spec->color;
1403     unsigned long depth     = 0;
1404     do {
1405       switch(now_color) {
1406       case 2:      depth = 1; break;
1407       case 4:      now_color = 2;        depth = 1;  break;
1408       case 8:      now_color = 4;        depth = 2;  break;
1409       case 16:     now_color = 8;        depth = 3;  break;
1410       case 256:    now_color = 16;       depth = 4;  break;
1411       case 4096:   now_color = 256;      depth = 8;  break;
1412       case 65536:  now_color = 4096;     depth = 12; break;
1413       case 262144: now_color = 65536;    depth = 16; break;
1414       case 15680000: now_color = 262144; depth = 18; break;
1415       default:
1416         now_color = 2;
1417         break;
1418       }
1419
1420       if (now_color <= 2) break;
1421
1422       if (now_color >= 8) {
1423         status = MagickQuantizeImage(magick_wand,
1424                              now_color,
1425                              RGBColorspace,
1426                              0,
1427                              1,
1428                              0);
1429       }
1430       else {
1431         status = MagickQuantizeImage(magick_wand,
1432                              now_color,
1433                              GRAYColorspace,
1434                              0,
1435                              1,
1436                              0);
1437         MagickSetImageType(magick_wand, GrayscaleType);
1438       }
1439       if (status == MagickFalse) {
1440         EXIT_MAGICK_ERROR();
1441         return NULL;
1442       }
1443
1444       if (MagickSetImageDepth(magick_wand, depth) == MagickFalse) {
1445         EXIT_MAGICK_ERROR();
1446         return NULL;
1447       }
1448
1449       writedata = (char*)MagickGetImageBlob(magick_wand, &writebyte);
1450
1451       DBG(r,"now_color=[%ld] size=[%d]", (long)now_color, (int)writebyte);
1452
1453       /* Once per request */
1454       break;
1455     }
1456     while(now_color > 2);
1457   }
1458
1459   return magick_wand;
1460 }
1461
1462 static apr_status_t 
1463 s_send_cache_file(
1464   device_table *spec, 
1465   query_string_param_t *query_string, 
1466   request_rec *r, 
1467   const char *tmpfile)
1468 {
1469   apr_status_t rv;
1470   apr_finfo_t  st;
1471   apr_file_t   *fout;
1472   apr_size_t   sendbyte;
1473   char         *contentLength;
1474
1475   rv = apr_stat(&st, tmpfile, APR_FINFO_MIN, r->pool);
1476   if (rv != APR_SUCCESS)
1477     return HTTP_NOT_FOUND;
1478
1479   DBG(r, "mode:[%d]",    query_string->mode);
1480   DBG(r, "name:[%s]",    query_string->name);
1481   DBG(r, "offset:[%ld]", query_string->offset);
1482   DBG(r, "count:[%ld]",  query_string->count);
1483
1484   if (spec->available_jpeg) {
1485     r->content_type = apr_psprintf(r->pool, "image/jpeg");
1486   }
1487   else
1488   if (spec->available_png) {
1489     r->content_type = apr_psprintf(r->pool, "image/png");
1490   }
1491   else
1492   if (spec->available_gif) {
1493     r->content_type = apr_psprintf(r->pool, "image/gif");
1494   }
1495   else
1496   if (spec->available_bmp2 || spec->available_bmp4) {
1497     r->content_type = apr_psprintf(r->pool, "image/bmp");
1498   }
1499
1500   if (query_string->mode != IMG_CONV_MODE_EZGET && query_string->name == NULL) {
1501     contentLength = apr_psprintf(r->pool, "%d", (int)st.size);
1502     apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1503   
1504     DBG(r,"Content-Length:[%d]", (int)st.size);
1505
1506     rv = apr_file_open(&fout, tmpfile, 
1507       APR_READ | APR_BINARY, APR_OS_DEFAULT, r->pool);
1508     if (rv != APR_SUCCESS) {
1509       DBG(r, "tmpfile open failed[%s]", tmpfile);
1510       return HTTP_NOT_FOUND;
1511     }
1512
1513     ap_send_fd(fout, r, 0, st.size, &sendbyte);
1514     apr_file_close(fout);
1515     ap_rflush(r);
1516     DBG(r, "send file data[%d]byte", (int)sendbyte);
1517   }
1518   else
1519   if (query_string->mode == IMG_CONV_MODE_EZGET) {
1520     char *name = apr_pstrdup(r->pool, basename(r->filename));
1521     name[strlen(name)-4] = 0;
1522     if (strcasecmp(r->content_type, "image/jpeg") == 0) {
1523
1524       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1525       ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".jpg", (long)st.size, "devjaww", name);
1526     }
1527     else
1528     if (strcasecmp(r->content_type, "image/bmp") == 0) {
1529       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1530       ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".bmp", (long)st.size, "devabm", name);
1531     }
1532     else
1533     if (strcasecmp(r->content_type, "image/png") == 0) {
1534       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1535       ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".png", (long)st.size, "dev8aww", name);
1536     }
1537     else
1538     if (strcasecmp(r->content_type, "image/gif") == 0) {
1539       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1540       ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".gif", (long)st.size, "devgi0z", name);
1541     }
1542   }
1543   else
1544   if (query_string->mode == IMG_CONV_MODE_WALLPAPER && query_string->name != NULL) {
1545     if (query_string->count == -1 && query_string->offset == -1) {
1546       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1547       ap_rprintf(r, HDML_SUCCESS_PAGE);
1548       ap_rflush(r);
1549     }
1550     else
1551     if (query_string->count == -2 && query_string->offset == -1) {
1552       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1553       ap_rprintf(r, HDML_FAIL_PAGE);
1554       ap_rflush(r);
1555     }
1556     else { 
1557       ap_set_content_type(r, "application/x-up-download");
1558       contentLength = apr_psprintf(r->pool, "%ld", query_string->count);
1559       apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1560   
1561       DBG(r, "Content-Length:[%d]", (int)st.size);
1562
1563       rv = apr_file_open(&fout, tmpfile, 
1564         APR_READ | APR_BINARY, APR_OS_DEFAULT, r->pool);
1565
1566       if (rv != APR_SUCCESS) {
1567         DBG(r,"tmpfile open failed[%s]", tmpfile);
1568         return HTTP_NOT_FOUND;
1569       }
1570
1571       ap_send_fd(fout, r, query_string->offset, query_string->count, &sendbyte);
1572       apr_file_close(fout);
1573       ap_rflush(r);
1574       DBG(r, "send file data[%d]byte", (int)sendbyte);
1575     }
1576   }
1577   
1578   return OK;
1579 }
1580
1581
1582 static apr_status_t
1583 s_send_original_file(request_rec* r, const char* originalfile)
1584 {
1585   apr_status_t rv;
1586   apr_finfo_t  st;
1587   apr_file_t*  fout;
1588   apr_size_t   sendbyte = 0;
1589
1590   rv = apr_stat(&st, originalfile, APR_FINFO_MIN, r->pool);
1591   if (rv != APR_SUCCESS)
1592     return HTTP_NOT_FOUND;
1593
1594   rv = apr_file_open(&fout, originalfile,
1595     APR_READ | APR_BINARY, APR_OS_DEFAULT, r->pool);
1596   if (rv != APR_SUCCESS) {
1597     DBG(r, "originalfile open failed[%s]", originalfile);
1598     return HTTP_NOT_FOUND;
1599   }
1600
1601   ap_send_fd(fout, r, 0, st.size, &sendbyte);
1602   apr_file_close(fout);
1603   ap_rflush(r);
1604   DBG(r, "send file data[%d]byte", (int)sendbyte);
1605
1606   return OK;
1607 }
1608
1609
1610 static apr_status_t 
1611 s_header_only_cache_file(
1612   device_table *spec,
1613   query_string_param_t *query_string, 
1614   request_rec *r,
1615   const char *tmpfile)
1616 {
1617   apr_status_t rv;
1618   apr_finfo_t  st;
1619   char         *contentLength;
1620
1621   rv = apr_stat(&st, tmpfile, APR_FINFO_MIN, r->pool);
1622   if (rv != APR_SUCCESS)
1623     return HTTP_NOT_FOUND;
1624
1625   DBG(r, "mode:[%d]",    query_string->mode);
1626   DBG(r, "name:[%s]",    query_string->name);
1627   DBG(r, "offset:[%ld]", query_string->offset);
1628   DBG(r, "count:[%ld]",  query_string->count);
1629
1630   if (spec->available_jpeg) {
1631     r->content_type = apr_psprintf(r->pool, "image/jpeg");
1632   }
1633   else
1634   if (spec->available_png) {
1635     r->content_type = apr_psprintf(r->pool, "image/png");
1636   }
1637   else
1638   if (spec->available_gif) {
1639     r->content_type = apr_psprintf(r->pool, "image/gif");
1640   }
1641   else
1642   if (spec->available_bmp2 || spec->available_bmp4) {
1643     r->content_type = apr_psprintf(r->pool, "image/bmp");
1644   }
1645
1646   if (query_string->mode != IMG_CONV_MODE_EZGET && query_string->name == NULL) {
1647     contentLength = apr_psprintf(r->pool, "%d", (int)st.size);
1648     apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1649   
1650     DBG(r,"Content-Length:[%d]", (int)st.size);
1651   }
1652   else
1653   if (query_string->mode == IMG_CONV_MODE_EZGET) {
1654     char* name = apr_pstrdup(r->pool, basename(r->filename));
1655     name[strlen(name)-4] = 0;
1656     if (strcasecmp(r->content_type, "image/jpeg") == 0) {
1657
1658       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1659     }
1660     else
1661     if (strcasecmp(r->content_type, "image/bmp") == 0) {
1662       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1663     }
1664     else
1665     if (strcasecmp(r->content_type, "image/png") == 0) {
1666       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1667     }
1668     else
1669     if (strcasecmp(r->content_type, "image/gif") == 0) {
1670       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1671     }
1672   }
1673   else
1674   if (query_string->mode == IMG_CONV_MODE_WALLPAPER && query_string->name != NULL) {
1675     if (query_string->count == -1 && query_string->offset == -1) {
1676       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1677     }
1678     else
1679     if (query_string->count == -2 && query_string->offset == -1) {
1680       ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1681     }
1682     else { 
1683       ap_set_content_type(r, "application/x-up-download");
1684       contentLength = apr_psprintf(r->pool, "%ld", query_string->count);
1685       apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1686   
1687       DBG(r, "Content-Length:[%d]", (int)st.size);
1688     }
1689   }
1690   
1691   return OK;
1692 }
1693
1694
1695 static void
1696 s_init_serial_pattern(apr_pool_t *p)
1697 {
1698   if (!v_docomo_serial_pattern1) {
1699     v_docomo_serial_pattern1 = chxj_compile_for_preg_replace(p, "/ser[^;\\)]+");
1700   }
1701   if (!v_docomo_serial_pattern2) {
1702     v_docomo_serial_pattern2 = chxj_compile_for_preg_replace(p, ";ser[^;\\)]+");
1703   }
1704   if (!v_docomo_serial_pattern3) {
1705     v_docomo_serial_pattern3 = chxj_compile_for_preg_replace(p, ";icc[^;\\)]+");
1706   }
1707   if (!v_softbank_serial_pattern1) {
1708     v_softbank_serial_pattern1 = chxj_compile_for_preg_replace(p, "/SN[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z] ");
1709   }
1710 }
1711
1712 static char *
1713 s_create_workfile_name(
1714   request_rec *r, 
1715   mod_chxj_config *conf, 
1716   const char *user_agent, 
1717   query_string_param_t *qsp)
1718 {
1719   int ii;
1720   int jj;
1721   int len;
1722   char *w = apr_palloc(r->pool, 256);
1723   char *fname;
1724   char *new_user_agent;
1725
1726   s_init_serial_pattern(r->pool);
1727
1728   /* for DoCoMo */
1729   new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern1, "", user_agent);
1730   new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern2, "", new_user_agent);
1731   new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern3, "", new_user_agent);
1732
1733   /* for SoftBank */
1734   new_user_agent = chxj_preg_replace(r->pool, v_softbank_serial_pattern1, " ", new_user_agent);
1735
1736   memset(w, 0, 256);
1737   switch (qsp->mode) {
1738   case IMG_CONV_MODE_THUMBNAIL:
1739     fname = apr_psprintf(r->pool, "%s.%s.thumbnail", r->filename, user_agent);
1740     DBG(r, "mode=thumbnail [%s]", fname);
1741     break;
1742   case IMG_CONV_MODE_WALLPAPER:
1743   case IMG_CONV_MODE_EZGET:
1744     fname = apr_psprintf(r->pool, "%s.%s.wallpaper", r->filename, user_agent);
1745     DBG(r, "mode=WallPaper [%s]", fname);
1746     break;
1747   case IMG_CONV_MODE_NORMAL:
1748   default:
1749
1750     fname = apr_psprintf(r->pool, "%s.%s", r->filename, user_agent);
1751
1752     if (qsp->width)
1753       fname = apr_psprintf(r->pool, "%s.w%d", fname, qsp->width);
1754
1755     if (qsp->height)
1756       fname = apr_psprintf(r->pool, "%s.h%d", fname, qsp->height);
1757
1758     DBG(r,"mode=normal [%s]", fname);
1759     break;
1760   }
1761   if (qsp->ua_flag == UA_IGN) {
1762     fname = apr_psprintf(r->pool, "%s.IGN", fname);
1763   }
1764
1765   len = strlen(fname);
1766   jj=0;
1767   for  (ii=0; ii<len; ii++) {
1768     if (fname[ii] == '/' 
1769     ||  fname[ii] == ' ' 
1770     ||  fname[ii] == '-' 
1771     ||  fname[ii] == '(' 
1772     ||  fname[ii] == ')') {
1773       w[jj++] = '_';
1774     }
1775     else {
1776       w[jj++] = fname[ii];
1777     }
1778   }
1779
1780   return apr_psprintf(r->pool, "%s/%s", conf->image_cache_dir,w);
1781 }
1782
1783
1784 static unsigned short
1785 s_add_crc(const char *writedata, apr_size_t writebyte)
1786 {
1787   unsigned short crc = 0xffff;
1788   apr_size_t     ii;
1789   unsigned char  ch;
1790
1791   for(ii=0;ii<writebyte;ii++) {
1792     ch  = writedata[ii];
1793     crc = AU_CRC_TBL[(crc>>8^ch)&0xff]^(crc<<8);
1794   }
1795   return crc;
1796 }
1797
1798
1799 int
1800 chxj_trans_name(request_rec *r)
1801 {
1802   const char *ccp;
1803   char *docroot;
1804   int len;
1805   apr_finfo_t st;
1806   apr_status_t rv;
1807   mod_chxj_config *conf;
1808   int ii;
1809   char *ext[] = {
1810           "jpg",
1811           "jpeg",
1812           "png",
1813           "bmp",
1814           "gif",
1815           "qrc",    /* QRCode½ÐÎÏÍÑ¥Õ¥¡¥¤¥ë¤Î³ÈÄ¥»Ò */
1816           "",
1817   };
1818   char     *fname;
1819   char     *idx;
1820   char     *filename_sv;
1821   int      do_ext_check = TRUE;
1822   int      next_ok      = FALSE;
1823
1824   DBG(r, "start chxj_trans_name()");
1825
1826   conf = ap_get_module_config(r->per_dir_config, &chxj_module);
1827
1828   if (conf == NULL) {
1829     DBG(r, "end chxj_trans_name() conf is null[%s]", r->uri);
1830     return DECLINED;
1831   }
1832
1833   if (conf->image != CHXJ_IMG_ON) {
1834     DBG(r, "end chxj_trans_name() conf not found");
1835     return DECLINED;
1836   }
1837
1838
1839   DBG(r,"Match URI[%s]", r->uri);
1840
1841
1842   if (r->filename == NULL) 
1843     r->filename = apr_pstrdup(r->pool, r->uri);
1844
1845   filename_sv = NULL;
1846   if ((idx = strchr(r->filename, ':')) != NULL) 
1847     filename_sv = idx+1;
1848   else 
1849     filename_sv = r->filename;
1850
1851   DBG(r,"r->filename[%s]", filename_sv);
1852
1853   ccp = ap_document_root(r);
1854   if (ccp == NULL)
1855     return HTTP_INTERNAL_SERVER_ERROR;
1856
1857   docroot = apr_pstrdup(r->pool, ccp);
1858   len = strlen(docroot);
1859
1860   if (docroot[len-1] == '/') 
1861     docroot[len-1] = '\0';
1862
1863
1864   if (r->server->path 
1865       &&  *filename_sv == *r->server->path 
1866       &&  strncmp(filename_sv, r->server->path, r->server->pathlen) == 0)
1867     filename_sv = apr_pstrcat(r->pool, docroot, (filename_sv + r->server->pathlen), NULL);
1868   else
1869     filename_sv = apr_pstrcat(r->pool, docroot, filename_sv, NULL);
1870
1871   DBG(r,"URI[%s]", filename_sv);
1872
1873   do_ext_check = TRUE;
1874   for (ii=0; ii<7-1; ii++) {
1875     char *pos = strrchr(filename_sv, '.');
1876     if (pos && pos++) {
1877       if (strcasecmp(pos, ext[ii]) == 0) {
1878         do_ext_check = FALSE;
1879         fname = apr_psprintf(r->pool, "%s", filename_sv);
1880         break;
1881       }
1882     }
1883   }
1884
1885   if (do_ext_check) {
1886     for (ii=0; ii<7; ii++) {
1887       if (strlen(ext[ii]) == 0) {
1888         fname = apr_psprintf(r->pool, "%s", filename_sv);
1889       }
1890       else 
1891         fname = apr_psprintf(r->pool, "%s.%s", filename_sv, ext[ii]);
1892   
1893       DBG(r,"search [%s]", fname);
1894   
1895       rv = apr_stat(&st, fname, APR_FINFO_MIN, r->pool);
1896       if (rv == APR_SUCCESS) {
1897         if (st.filetype != APR_DIR)
1898           break;
1899       }
1900   
1901       fname = NULL;
1902     }
1903   }
1904   if (fname == NULL) {
1905     DBG(r,"NotFound [%s]", r->filename);
1906     return DECLINED;
1907   }
1908   for (ii=0; ii<7-1; ii++) {
1909     char *pos = strrchr(fname, '.');
1910     if (pos && pos++) {
1911       if (strcasecmp(pos, ext[ii]) == 0) {
1912         next_ok = TRUE;
1913         break;
1914       }
1915     }
1916   }
1917
1918   if (! next_ok)  {
1919     DBG(r,"NotFound [%s]", r->filename);
1920     return DECLINED;
1921   }
1922
1923   if (r->handler == NULL || strcasecmp(r->handler, "chxj-qrcode") != 0) {
1924     DBG(r,"Found [%s]", fname);
1925
1926     r->filename = apr_psprintf(r->pool, "%s", fname);
1927   
1928     if (strcasecmp("qrc", ext[ii]) == 0)
1929       r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1930     else
1931       r->handler = apr_psprintf(r->pool, "chxj-picture");
1932   }
1933   DBG(r, "end chxj_trans_name()");
1934   return OK;
1935 }
1936
1937
1938 /**
1939  * It converts it from QUERYSTRING.
1940  *
1941  * @param r   [i]
1942  */
1943 static query_string_param_t *
1944 s_get_query_string_param(request_rec *r)
1945 {
1946   char *pair;
1947   char *name;
1948   char *value;
1949   char *pstate;
1950   char *vstate;
1951   char *s;
1952   query_string_param_t *param;
1953
1954   s = apr_pstrdup(r->pool, r->parsed_uri.query);
1955   param = apr_palloc(r->pool, sizeof(query_string_param_t));
1956   param->mode       = IMG_CONV_MODE_NORMAL;
1957   param->user_agent = NULL;
1958   param->ua_flag    = UA_USE;
1959   param->name       = NULL;
1960   param->offset     = 0;
1961   param->count      = 0;
1962   param->width      = 0;
1963   param->height     = 0;
1964
1965   if (s == NULL) return param;
1966
1967   for (;;) {
1968     if ((pair = apr_strtok(s, "&", &pstate)) == NULL) break;
1969     s = NULL;
1970
1971     name  = apr_strtok(pair, "=", &vstate);
1972     value = apr_strtok(NULL, "=", &vstate);
1973
1974     switch (*name) {
1975     case 'm':
1976     case 'M':
1977       if (value && (strcasecmp(name, "mode") == 0 || strcasecmp(name, "m") == 0)) {
1978
1979         switch (*value) {
1980         case 't':
1981         case 'T':
1982           if (strcasecmp(value, "thumbnail") == 0 || strcasecmp(value, "tb") == 0)
1983             param->mode = IMG_CONV_MODE_THUMBNAIL;
1984           break;
1985   
1986         case 'w':
1987         case 'W':
1988           if (strcasecmp(value, "WP") == 0 || strcasecmp(value, "WallPaper") == 0)
1989             param->mode = IMG_CONV_MODE_WALLPAPER;
1990           break;
1991   
1992         case 'e':
1993         case 'E':
1994           if (strcasecmp(value, "EZGET") == 0)
1995             param->mode = IMG_CONV_MODE_EZGET;
1996           break;
1997         default:
1998           break;
1999         }
2000       }
2001       break;
2002
2003     case 'u':
2004     case 'U':
2005       if (value && (strcasecmp(name, "ua") == 0 || strcasecmp(name, "user-agent") == 0)) {
2006         ap_unescape_url(value);
2007
2008         if ((*value == 'i' || *value == 'I') && strcasecmp(value, "IGN") == 0)
2009           param->ua_flag = UA_IGN;
2010
2011         param->user_agent = apr_pstrdup(r->pool, value);
2012       }
2013       break;
2014
2015     case 'n':
2016     case 'N':
2017       if (value && strcasecmp(name, "name") == 0)
2018         param->name = apr_pstrdup(r->pool, value);
2019       break;
2020
2021     case 'o':
2022     case 'O':
2023       if (value && strcasecmp(name, "offset") == 0 && (! chxj_chk_numeric(value)))
2024         param->offset = chxj_atoi(value);
2025
2026       break;
2027
2028     case 'c':
2029     case 'C':
2030       if (value && strcasecmp(name, "count") == 0 && (! chxj_chk_numeric(value)))
2031         param->count = chxj_atoi(value);
2032       break;
2033
2034     case 'w':
2035     case 'W':
2036       if (value && strcasecmp(name, "w") == 0 && (! chxj_chk_numeric(value)))
2037         param->width = chxj_atoi(value);
2038       break;
2039
2040     case 'h':
2041     case 'H':
2042       if (value && strcasecmp(name, "h") == 0 && (! chxj_chk_numeric(value)))
2043         param->height = chxj_atoi(value);
2044       break;
2045
2046     default:
2047       break;
2048     }
2049   }
2050
2051   if (param->mode == IMG_CONV_MODE_NORMAL && param->name)
2052     param->mode = IMG_CONV_MODE_WALLPAPER;
2053
2054   return param;
2055 }
2056 /*
2057  * vim:ts=2 et
2058  */