OSDN Git Service

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