2 * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3 * Copyright (C) 2005 QSDN,Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "chxj_img_conv_format.h"
21 #include "chxj_specified_device.h"
22 #include "chxj_str_util.h"
23 #include "chxj_qr_code.h"
24 #include "chxj_apply_convrule.h"
25 #include "chxj_url_encode.h"
26 #include "qs_parse_string.h"
27 #include "chxj_preg_replace.h"
29 #include "http_core.h"
31 #include <wand/magick_wand.h>
34 #define EXIT_MAGICK_ERROR() \
36 char *description; ExceptionType severity; \
37 description=MagickGetException(magick_wand,&severity); \
38 ap_log_rerror(APLOG_MARK,APLOG_DEBUG, 0, r,"%s %s %d %s\n",__FILE__,(__func__),__LINE__,description); \
39 description=(char *) MagickRelinquishMemory(description); \
40 DestroyMagickWand(magick_wand); \
43 typedef enum img_conv_mode_t {
44 IMG_CONV_MODE_NORMAL = 0,
45 IMG_CONV_MODE_THUMBNAIL,
46 IMG_CONV_MODE_WALLPAPER,
50 /*----------------------------------------------------------------------------*/
51 /* User-Agent use flag */
52 /*----------------------------------------------------------------------------*/
53 typedef enum _ua_use_flag_t {
54 UA_USE=0, /* User-Agent is used. */
55 UA_IGN, /* User-Agent is disregarded. */
58 /*----------------------------------------------------------------------------*/
59 /* Request parameter maintenance structure */
60 /*----------------------------------------------------------------------------*/
61 typedef struct query_string_param_t query_string_param_t;
63 struct query_string_param_t {
66 ua_use_flag_t ua_flag;
68 char* name; /* for EZGET */
69 long offset; /* for EZGET */
70 long count; /* for EZGET */
75 /*----------------------------------------------------------------------------*/
76 /* Device_spec when User-Agent is disregarded */
77 /*----------------------------------------------------------------------------*/
78 static device_table v_ignore_spec = {
99 /*----------------------------------------------------------------------------*/
100 /* CRC calculation table for AU */
101 /*----------------------------------------------------------------------------*/
102 static unsigned short AU_CRC_TBL[256] = {
103 0x0000,0x1021,0x2042,0x3063,0x4084,0x50A5,0x60C6,0x70E7,
104 0x8108,0x9129,0xA14A,0xB16B,0xC18C,0xD1AD,0xE1CE,0xF1EF,
105 0x1231,0x0210,0x3273,0x2252,0x52B5,0x4294,0x72F7,0x62D6,
106 0x9339,0x8318,0xB37B,0xA35A,0xD3BD,0xC39C,0xF3FF,0xE3DE,
107 0x2462,0x3443,0x0420,0x1401,0x64E6,0x74C7,0x44A4,0x5485,
108 0xA56A,0xB54B,0x8528,0x9509,0xE5EE,0xF5CF,0xC5AC,0xD58D,
109 0x3653,0x2672,0x1611,0x0630,0x76D7,0x66F6,0x5695,0x46B4,
110 0xB75B,0xA77A,0x9719,0x8738,0xF7DF,0xE7FE,0xD79D,0xC7BC,
111 0x48C4,0x58E5,0x6886,0x78A7,0x0840,0x1861,0x2802,0x3823,
112 0xC9CC,0xD9ED,0xE98E,0xF9AF,0x8948,0x9969,0xA90A,0xB92B,
113 0x5AF5,0x4AD4,0x7AB7,0x6A96,0x1A71,0x0A50,0x3A33,0x2A12,
114 0xDBFD,0xCBDC,0xFBBF,0xEB9E,0x9B79,0x8B58,0xBB3B,0xAB1A,
115 0x6CA6,0x7C87,0x4CE4,0x5CC5,0x2C22,0x3C03,0x0C60,0x1C41,
116 0xEDAE,0xFD8F,0xCDEC,0xDDCD,0xAD2A,0xBD0B,0x8D68,0x9D49,
117 0x7E97,0x6EB6,0x5ED5,0x4EF4,0x3E13,0x2E32,0x1E51,0x0E70,
118 0xFF9F,0xEFBE,0xDFDD,0xCFFC,0xBF1B,0xAF3A,0x9F59,0x8F78,
119 0x9188,0x81A9,0xB1CA,0xA1EB,0xD10C,0xC12D,0xF14E,0xE16F,
120 0x1080,0x00A1,0x30C2,0x20E3,0x5004,0x4025,0x7046,0x6067,
121 0x83B9,0x9398,0xA3FB,0xB3DA,0xC33D,0xD31C,0xE37F,0xF35E,
122 0x02B1,0x1290,0x22F3,0x32D2,0x4235,0x5214,0x6277,0x7256,
123 0xB5EA,0xA5CB,0x95A8,0x8589,0xF56E,0xE54F,0xD52C,0xC50D,
124 0x34E2,0x24C3,0x14A0,0x0481,0x7466,0x6447,0x5424,0x4405,
125 0xA7DB,0xB7FA,0x8799,0x97B8,0xE75F,0xF77E,0xC71D,0xD73C,
126 0x26D3,0x36F2,0x0691,0x16B0,0x6657,0x7676,0x4615,0x5634,
127 0xD94C,0xC96D,0xF90E,0xE92F,0x99C8,0x89E9,0xB98A,0xA9AB,
128 0x5844,0x4865,0x7806,0x6827,0x18C0,0x08E1,0x3882,0x28A3,
129 0xCB7D,0xDB5C,0xEB3F,0xFB1E,0x8BF9,0x9BD8,0xABBB,0xBB9A,
130 0x4A75,0x5A54,0x6A37,0x7A16,0x0AF1,0x1AD0,0x2AB3,0x3A92,
131 0xFD2E,0xED0F,0xDD6C,0xCD4D,0xBDAA,0xAD8B,0x9DE8,0x8DC9,
132 0x7C26,0x6C07,0x5C64,0x4C45,0x3CA2,0x2C83,0x1CE0,0x0CC1,
133 0xEF1F,0xFF3E,0xCF5D,0xDF7C,0xAF9B,0xBFBA,0x8FD9,0x9FF8,
134 0x6E17,0x7E36,0x4E55,0x5E74,0x2E93,0x3EB2,0x0ED1,0x1EF0
137 /*----------------------------------------------------------------------------*/
138 /* Download page for AU */
139 /*----------------------------------------------------------------------------*/
140 static const char* HDML_FIRST_PAGE =
141 "<HDML VERSION=3.0 TTL=0 PUBLIC=TRUE>\r\n"
143 " <ACTION TYPE=ACCEPT TASK=GOSUB DEST=\"device:data/dnld?url=%s&name=%s%s&size=%ld&disposition=%s&title=%s\">\r\n"
147 static const char* HDML_SUCCESS_PAGE =
148 "<HDML VERSION=3.0 TTL=0 PUBLIC=TRUE>\r\n"
150 " <ACTION TYPE=ACCEPT TASK=RETURN>\r\n"
151 " \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 static const char* HDML_FAIL_PAGE =
156 "<HDML VERSION=3.0 TTL=0 PUBLIC=TRUE>\r\n"
158 " <ACTION TYPE=ACCEPT TASK=RETURN>\r\n"
159 " \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 static ap_regex_t *v_docomo_serial_pattern1 = NULL;
164 static ap_regex_t *v_docomo_serial_pattern2 = NULL;
165 static ap_regex_t *v_docomo_serial_pattern3 = NULL;
166 static ap_regex_t *v_softbank_serial_pattern1 = NULL;
168 /*----------------------------------------------------------------------------*/
169 /* Prototype declaration */
170 /*----------------------------------------------------------------------------*/
171 static char* s_create_workfile_name( request_rec*,
174 query_string_param_t*);
176 static apr_status_t s_create_cache_file(request_rec* r,
180 query_string_param_t* qsp,
181 mod_chxj_config *conf);
183 static apr_status_t s_send_cache_file( device_table* spec,
184 query_string_param_t* query_string,
186 const char* tmpfile);
188 static apr_status_t s_send_original_file(request_rec* r,
189 const char* originalfile);
191 static apr_status_t s_header_only_cache_file(device_table* spec,
192 query_string_param_t* query_string,
194 const char* tmpfile);
196 static query_string_param_t* s_get_query_string_param(request_rec *r);
198 static unsigned short s_add_crc( const char* writedata,
199 apr_size_t witebyte);
201 static MagickWand* s_fixup_size(MagickWand* ,
204 query_string_param_t *qsp);
206 static MagickWand* s_fixup_color(MagickWand* magick_wand,
209 img_conv_mode_t mode);
210 static MagickWand* s_fixup_depth(MagickWand* magick_wand,
211 request_rec* r, device_table* spec);
212 static MagickWand* s_img_down_sizing(MagickWand* magick_wand,
213 request_rec* r, device_table* spec);
215 static MagickWand* s_add_copyright(MagickWand* magick_wand,
219 static char* s_create_blob_data(request_rec* r,
221 query_string_param_t* qsp,
225 static int s_img_conv_format_from_file(request_rec* r,
226 mod_chxj_config* conf,
227 const char* user_agent,
228 query_string_param_t* qsp,
234 chxj_img_conv_format_handler(request_rec* r)
236 mod_chxj_config* conf;
237 query_string_param_t* qsp;
240 chxjconvrule_entry* entryp;
242 DBG(r, "start chxj_img_conv_format_handler()");
244 if ((*r->handler != 'c' && *r->handler != 'C')
245 || (strcasecmp(r->handler, "chxj-picture")
246 && strcasecmp(r->handler, "chxj-qrcode"))) {
247 DBG(r, "end chxj_img_conv_format_handler()");
251 qsp = s_get_query_string_param(r);
252 conf = ap_get_module_config(r->per_dir_config, &chxj_module);
254 DBG(r, "end chxj_img_conv_format_handler() conf is null");
258 if (strcasecmp(r->handler, "chxj-qrcode") == 0 && conf->image == CHXJ_IMG_OFF) {
263 /*------------------------------------------------------------------------*/
264 /* get UserAgent from http header */
265 /*------------------------------------------------------------------------*/
266 /*--------------------------------------------------------------------------*/
267 /* User-Agent to spec */
268 /*--------------------------------------------------------------------------*/
269 if (qsp->user_agent) {
270 user_agent = apr_pstrdup(r->pool, qsp->user_agent);
273 entryp = chxj_apply_convrule(r, conf->convrules);
274 if (entryp && entryp->user_agent) {
275 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
278 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
284 if (qsp->ua_flag == UA_IGN)
285 spec = &v_ignore_spec;
287 spec = chxj_specified_device(r, user_agent);
289 DBG(r,"found device_name=[%s]", spec->device_name);
290 DBG(r,"User-Agent=[%s]", user_agent);
293 return s_img_conv_format_from_file(r, conf, user_agent, qsp, spec);
299 * It converts it from ImageData corresponding to each model.
302 * @param src [i] It is former image binary data.
303 * @param len [i/o] It is length of former image binary data.
306 chxj_exchange_image(request_rec *r, const char** src, apr_size_t* len)
308 mod_chxj_config* conf;
309 query_string_param_t* qsp;
314 chxjconvrule_entry* entryp;
316 DBG(r, "start chxj_exchange_image()");
318 conv_check = (char*)apr_table_get(r->headers_in, "CHXJ_IMG_CONV");
320 DBG(r, "end chxj_exchnage_image() already convert.");
325 qsp = s_get_query_string_param(r);
326 conf = ap_get_module_config(r->per_dir_config, &chxj_module);
328 DBG(r, "end chxj_exchange_image()");
332 /*--------------------------------------------------------------------------*/
333 /* User-Agent to spec */
334 /*--------------------------------------------------------------------------*/
335 if (qsp->user_agent) {
336 user_agent = apr_pstrdup(r->pool, qsp->user_agent);
339 entryp = chxj_apply_convrule(r, conf->convrules);
340 if (entryp && entryp->user_agent) {
341 user_agent = (char*)apr_table_get(r->headers_in, CHXJ_HTTP_USER_AGENT);
344 user_agent = (char*)apr_table_get(r->headers_in, HTTP_USER_AGENT);
348 if (qsp->ua_flag == UA_IGN)
349 spec = &v_ignore_spec;
351 spec = chxj_specified_device(r, user_agent);
353 DBG(r,"found device_name=[%s]", spec->device_name);
354 DBG(r, "User-Agent=[%s]", user_agent);
356 if (spec->width == 0 || spec->heigh == 0)
359 dst = s_create_blob_data(r, spec, qsp, (char*)*src, len);
363 DBG(r, "end chxj_exchange_image()");
369 s_img_conv_format_from_file(
371 mod_chxj_config* conf,
372 const char* user_agent,
373 query_string_param_t* qsp,
378 apr_finfo_t cache_st;
382 if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
384 * If ``ua'' parameter is specified, it must be CHXJ_SPEC_HTML.
386 return s_send_original_file(r, r->filename);
389 /*--------------------------------------------------------------------------*/
390 /* Create Workfile Name */
391 /*--------------------------------------------------------------------------*/
392 tmpfile = s_create_workfile_name(r, conf, user_agent, qsp);
393 DBG(r,"workfile=[%s]", tmpfile);
395 rv = apr_stat(&st, r->filename, APR_FINFO_MIN, r->pool);
396 if (rv != APR_SUCCESS)
397 return HTTP_NOT_FOUND;
399 try_count = CACHE_RETRY_COUNT;
401 rv = apr_stat(&cache_st, tmpfile, APR_FINFO_MIN, r->pool);
403 if (rv != APR_SUCCESS || cache_st.ctime < st.mtime) {
404 /*------------------------------------------------------------------------*/
405 /* It tries to make the cash file when it doesn't exist or there is */
406 /* change time later since the making time of the cash file. */
407 /*------------------------------------------------------------------------*/
408 rv = s_create_cache_file(r,tmpfile, spec, &st, qsp, conf);
413 DBG(r,"color=[%d]", spec->color);
414 if (! r->header_only) {
415 rv = s_send_cache_file(spec, qsp,r, tmpfile);
418 rv = s_header_only_cache_file(spec, qsp, r, tmpfile);
421 if (rv == HTTP_NOT_FOUND) {
422 DBG(r, "recheck wait... try_count[%d]", try_count);
423 apr_sleep(CACHE_RECHECK_WAIT);
425 } while (try_count--);
426 if (try_count <= 0) {
427 WRN(r, "cache retry failure....");
428 WRN(r, "cache file was deleted...");
430 apr_table_setn(r->headers_in, "CHXJ_IMG_CONV", "done");
432 DBG(r,"end chxj_img_conv_format");
439 s_create_cache_file(request_rec* r,
443 query_string_param_t *qsp,
444 mod_chxj_config *conf)
448 apr_size_t writebyte;
450 img_conv_mode_t mode = qsp->mode;
452 char* writedata = NULL;
453 char* readdata = NULL;
457 apr_finfo_t cache_dir_st;
459 MagickWand* magick_wand;
461 if ((*r->handler == 'c' || *r->handler == 'C')
462 && strcasecmp(r->handler, "chxj-qrcode") == 0) {
463 /*------------------------------------------------------------------------*/
464 /* QRCODEÍѤΥե¡¥¤¥ë¤Î¾ì¹ç */
465 /*------------------------------------------------------------------------*/
471 memset(&doc, 0, sizeof(Doc));
472 memset(&qrcode, 0, sizeof(qr_code_t));
474 doc.parse_mode = PARSE_MODE_CHTML;
478 qs_init_malloc(&doc);
480 root = qs_parse_file(&doc, r->filename);
482 chxj_qrcode_node_to_qrcode(&qrcode, root);
484 qs_all_free(&doc,QX_LOGMARK);
486 sts = chxj_qrcode_create_image_data(&qrcode, &readdata, &readbyte);
488 ERR(r, "qrcode create failed.");
493 /*------------------------------------------------------------------------*/
494 /* Ä̾ï¤Î¥¤¥á¡¼¥¸¥Õ¥¡¥¤¥ë¤Î¾ì¹ç */
495 /*------------------------------------------------------------------------*/
496 rv = apr_file_open(&fin,
498 APR_READ|APR_BINARY ,
501 if (rv != APR_SUCCESS) {
502 DBG(r,"file open failed.[%s]", r->filename);
503 return HTTP_NOT_FOUND;
506 readdata = apr_palloc(r->pool, st->size);
507 rv = apr_file_read_full(fin, (void*)readdata, st->size, &readbyte);
508 if (rv != APR_SUCCESS || readbyte != st->size) {
509 DBG(r,"file read failed.[%s]", r->filename);
512 return HTTP_NOT_FOUND;
515 DBG(r,"start img convert");
518 magick_wand = NewMagickWand();
519 if (MagickReadImageBlob(magick_wand,readdata, readbyte) == MagickFalse) {
521 return HTTP_NOT_FOUND;
524 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
526 * The size of the image is changed.
528 DBG(r,"call s_fixup_size()");
530 if ((magick_wand = s_fixup_size(magick_wand, r, spec, qsp)) == NULL)
531 return HTTP_NOT_FOUND;
534 * The colors of the image is changed.
536 DBG(r,"call s_fixup_color()");
538 if ((magick_wand = s_fixup_color(magick_wand, r,spec, mode)) == NULL)
539 return HTTP_NOT_FOUND;
542 * DEPTH of the image is changed.
544 DBG(r,"call s_fixup_depth()");
546 if ((magick_wand = s_fixup_depth(magick_wand, r, spec)) == NULL)
547 return HTTP_NOT_FOUND;
551 DBG(r,"start convert and compression");
553 if (spec->available_jpeg) {
554 if (MagickSetImageCompression(magick_wand,JPEGCompression) == MagickFalse) {
556 return HTTP_NOT_FOUND;
559 if (MagickSetImageFormat(magick_wand, "jpg") == MagickFalse) {
561 return HTTP_NOT_FOUND;
564 if (MagickStripImage(magick_wand) == MagickFalse) {
566 return HTTP_NOT_FOUND;
569 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
570 return HTTP_NOT_FOUND;
572 r->content_type = apr_psprintf(r->pool, "image/jpeg");
573 DBG(r,"convert to jpg");
576 if (spec->available_png) {
578 if (MagickSetImageCompression(magick_wand,ZipCompression) == MagickFalse) {
580 return HTTP_NOT_FOUND;
583 if (MagickSetImageFormat(magick_wand, "png") == MagickFalse) {
585 return HTTP_NOT_FOUND;
588 if (MagickStripImage(magick_wand) == MagickFalse) {
590 return HTTP_NOT_FOUND;
593 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
594 return HTTP_NOT_FOUND;
596 r->content_type = apr_psprintf(r->pool, "image/png");
597 DBG(r, "convert to png");
600 if (spec->available_gif) {
602 if (MagickSetImageCompression(magick_wand,LZWCompression) == MagickFalse) {
604 return HTTP_NOT_FOUND;
607 if (MagickSetImageFormat(magick_wand, "gif") == MagickFalse) {
609 return HTTP_NOT_FOUND;
612 if (MagickStripImage(magick_wand) == MagickFalse) {
614 return HTTP_NOT_FOUND;
617 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
618 return HTTP_NOT_FOUND;
620 r->content_type = apr_psprintf(r->pool, "image/gif");
622 DBG(r,"convert to gif");
625 if (spec->available_bmp2 || spec->available_bmp4) {
627 if (MagickSetImageCompression(magick_wand,NoCompression) == MagickFalse) {
629 return HTTP_NOT_FOUND;
632 if (MagickSetImageFormat(magick_wand, "bmp") == MagickFalse) {
634 return HTTP_NOT_FOUND;
637 if (MagickStripImage(magick_wand) == MagickFalse) {
639 return HTTP_NOT_FOUND;
642 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
643 return HTTP_NOT_FOUND;
645 r->content_type = apr_psprintf(r->pool, "image/bmp");
647 DBG(r, "convert to bmp(unsupported)");
651 * Add Comment (Copyright and so on.)
653 DBG(r, "call s_add_copyright()");
655 if ((magick_wand = s_add_copyright(magick_wand, r, spec)) == NULL)
656 return HTTP_NOT_FOUND;
660 fmt = MagickGetImageFormat(magick_wand);
662 if (MagickSetImageFormat(magick_wand, "jpg") == MagickFalse) {
664 return HTTP_NOT_FOUND;
667 r->content_type = apr_psprintf(r->pool, "image/jpeg");
670 if (strcasecmp(fmt, "jpg") == 0) {
671 r->content_type = apr_psprintf(r->pool, "image/jpeg");
674 if (strcasecmp(fmt, "jpeg") == 0) {
675 r->content_type = apr_psprintf(r->pool, "image/jpeg");
678 if (strcasecmp(fmt, "gif") == 0) {
679 r->content_type = apr_psprintf(r->pool, "image/gif");
682 if (strcasecmp(fmt, "png") == 0) {
683 r->content_type = apr_psprintf(r->pool, "image/png");
688 writedata = (char*)MagickGetImageBlob(magick_wand, &writebyte);
691 DestroyMagickWand(magick_wand);
692 ERR(r,"convert failure to Jpeg [%s]", tmpfile);
694 return HTTP_INTERNAL_SERVER_ERROR;
697 DBG(r, "end convert and compression");
700 rv = apr_stat(&cache_dir_st, conf->image_cache_dir, APR_FINFO_MIN, r->pool);
701 if (rv != APR_SUCCESS) {
702 DestroyMagickWand(magick_wand);
703 ERR(r,"dir stat error.[%s]", conf->image_cache_dir);
704 if (writedata) free(writedata);
705 return HTTP_INTERNAL_SERVER_ERROR;
709 /* delete candidate */
711 /* get dir files size */
713 unsigned long total_size = 0;
715 unsigned long max_size = (! conf->image_cache_limit) ? DEFAULT_IMAGE_CACHE_LIMIT : conf->image_cache_limit;
716 char *delete_file_name;
718 rv = apr_dir_open(&dir, conf->image_cache_dir, r->pool);
719 if (rv != APR_SUCCESS) {
720 DestroyMagickWand(magick_wand);
721 ERR(r,"dir open error.[%s]", conf->image_cache_dir);
722 if (writedata) free(writedata);
723 return HTTP_INTERNAL_SERVER_ERROR;
725 memset(&dcf, 0, sizeof(apr_finfo_t));
726 dcf.atime = (apr_time_t)LONG_LONG_MAX;
729 rv = apr_dir_read(&dirf, APR_FINFO_SIZE|APR_FINFO_NAME|APR_FINFO_DIRENT|APR_FINFO_ATIME , dir);
730 if (rv != APR_SUCCESS) {
733 if (dirf.name && strcmp(dirf.name, ".") != 0 && strcmp(dirf.name, "..") != 0) {
734 total_size += (unsigned long)dirf.size;
735 DBG(r, "dirf.name=[%s] dirf.size=[%ld] dirf.atime=[%lld]", dirf.name, (long)dirf.size, (long long int)dirf.atime);
736 if (dcf.atime >= dirf.atime) {
737 memcpy(&dcf, &dirf, sizeof(apr_finfo_t));
743 if (total_size + writebyte < max_size) {
744 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);
747 if (found_file == 0 && writebyte >= max_size) {
748 ERR(r, "==========================================");
749 ERR(r, "cache space is too small...");
750 ERR(r, "At least the same size as %luByte is necessary for me.", (unsigned long)writebyte);
751 ERR(r, "Please specify the ChxjImageCacheLimit that is larger than now value. ");
752 ERR(r, "==========================================");
753 if (writedata) free(writedata);
754 return HTTP_INTERNAL_SERVER_ERROR;
756 DBG(r, "Image Cache dir is full. total_size:[%lu] max_size:[%lu]", total_size + writebyte, max_size);
757 /* search delete candidate */
758 delete_file_name = apr_psprintf(r->pool, "%s/%s", conf->image_cache_dir, dcf.name);
759 DBG(r, "delete image cache target:[%s] atime:[%lld]", delete_file_name, (long long int)dcf.atime);
760 rv = apr_file_remove(delete_file_name, r->pool);
761 if (rv != APR_SUCCESS) {
762 ERR(r, "cache file delete failure.[%s]", delete_file_name);
763 if (writedata) free(writedata);
764 return HTTP_INTERNAL_SERVER_ERROR;
766 DBG(r, "deleted image cache target:[%s]", delete_file_name);
767 if (total_size + writebyte - dcf.size < max_size) {
768 DBG(r, "OK, there is an enough size in cache.");
774 rv = apr_file_open(&fout, tmpfile,
775 APR_WRITE| APR_CREATE | APR_BINARY | APR_SHARELOCK ,APR_OS_DEFAULT,
777 if (rv != APR_SUCCESS) {
778 DestroyMagickWand(magick_wand);
779 ERR(r,"file open error.[%s]", tmpfile);
780 if (writedata) free(writedata);
781 return HTTP_INTERNAL_SERVER_ERROR;
784 rv = apr_file_write(fout, (void*)writedata, &writebyte);
785 if (rv != APR_SUCCESS) {
786 DestroyMagickWand(magick_wand);
787 apr_file_close(fout);
788 if (writedata) free(writedata);
789 return HTTP_INTERNAL_SERVER_ERROR;
793 * CRC is added for AU for EzGET.
795 if (spec->html_spec_type == CHXJ_SPEC_XHtml_Mobile_1_0
796 || spec->html_spec_type == CHXJ_SPEC_Hdml ) {
798 crc = s_add_crc(writedata, writebyte);
800 rv = apr_file_putc((crc >> 8) & 0xff, fout);
801 if (rv != APR_SUCCESS) {
802 DestroyMagickWand(magick_wand);
803 if (writedata) free(writedata);
804 return HTTP_INTERNAL_SERVER_ERROR;
807 rv = apr_file_putc( crc & 0xff, fout);
808 if (rv != APR_SUCCESS) {
809 DestroyMagickWand(magick_wand);
810 if (writedata) free(writedata);
811 return HTTP_INTERNAL_SERVER_ERROR;
815 DestroyMagickWand(magick_wand);
816 if (writedata) free(writedata);
818 rv = apr_file_close(fout);
819 if (rv != APR_SUCCESS) {
820 DBG(r,"file write error.[%s]", tmpfile);
821 return HTTP_INTERNAL_SERVER_ERROR;
829 s_create_blob_data(request_rec* r,
831 query_string_param_t *qsp,
835 apr_size_t writebyte;
837 img_conv_mode_t mode = qsp->mode;
839 char* writedata = NULL;
842 MagickWand* magick_wand;
844 magick_wand = NewMagickWand();
846 if (MagickReadImageBlob(magick_wand,indata, *len) == MagickFalse) {
852 * The size of the image is changed.
854 DBG(r, "call s_fixup_size()");
856 if ((magick_wand = s_fixup_size(magick_wand, r, spec, qsp)) == NULL)
860 * The colors of the image is changed.
862 DBG(r, "call s_fixup_color()");
864 if ((magick_wand = s_fixup_color(magick_wand, r,spec, mode)) == NULL)
868 * DEPTH of the image is changed.
871 DBG(r,"call s_fixup_depth()");
873 if ((magick_wand = s_fixup_depth(magick_wand, r, spec)) == NULL)
878 DBG(r,"start convert and compression");
880 if (spec->available_jpeg) {
881 if (MagickSetImageCompression(magick_wand,JPEGCompression) == MagickFalse) {
886 if (MagickSetImageFormat(magick_wand, "jpg") == MagickFalse) {
891 if (MagickStripImage(magick_wand) == MagickFalse) {
896 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
899 r->content_type = apr_psprintf(r->pool, "image/jpeg");
901 DBG(r, "convert to jpg");
904 if (spec->available_png) {
905 if (MagickSetImageCompression(magick_wand,ZipCompression) == MagickFalse) {
910 if (MagickSetImageFormat(magick_wand, "png") == MagickFalse) {
915 if (MagickStripImage(magick_wand) == MagickFalse) {
920 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
923 r->content_type = apr_psprintf(r->pool, "image/png");
925 DBG(r,"convert to png");
928 if (spec->available_gif) {
930 if (MagickSetImageCompression(magick_wand,LZWCompression) == MagickFalse) {
935 if (MagickSetImageFormat(magick_wand, "gif") == MagickFalse) {
940 if (MagickStripImage(magick_wand) == MagickFalse) {
945 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
948 r->content_type = apr_psprintf(r->pool, "image/gif");
950 DBG(r,"convert to gif");
953 if (spec->available_bmp2 || spec->available_bmp4) {
954 if (MagickSetImageCompression(magick_wand,NoCompression) == MagickFalse) {
959 if (MagickSetImageFormat(magick_wand, "bmp") == MagickFalse) {
964 if (MagickStripImage(magick_wand) == MagickFalse) {
969 if ((magick_wand = s_img_down_sizing(magick_wand, r, spec)) == NULL)
972 r->content_type = apr_psprintf(r->pool, "image/bmp");
974 DBG(r,"convert to bmp(unsupported)");
976 /*--------------------------------------------------------------------------*/
977 /* Add Comment (Copyright and so on.) */
978 /*--------------------------------------------------------------------------*/
979 DBG(r,"call s_add_copyright()");
981 if ((magick_wand = s_add_copyright(magick_wand, r, spec)) == NULL)
984 writedata = (char*)MagickGetImageBlob(magick_wand, &writebyte);
987 DestroyMagickWand(magick_wand);
988 DBG(r,"convert failure to Jpeg ");
992 DBG(r,"end convert and compression");
996 dst = apr_palloc(r->pool, writebyte+2);
998 memcpy(dst, writedata, writebyte);
999 /*--------------------------------------------------------------------------*/
1000 /* CRC is added for AU for EzGET. */
1001 /*--------------------------------------------------------------------------*/
1002 if (spec->html_spec_type == CHXJ_SPEC_XHtml_Mobile_1_0
1003 || spec->html_spec_type == CHXJ_SPEC_Hdml) {
1004 crc = s_add_crc(writedata, writebyte);
1005 dst[writebyte + 0] = (crc >> 8) & 0xff;
1006 dst[writebyte + 1] = (crc ) & 0xff;
1010 DestroyMagickWand(magick_wand);
1017 s_fixup_size(MagickWand* magick_wand,
1020 query_string_param_t *qsp)
1022 img_conv_mode_t mode = qsp->mode;
1030 oldw = MagickGetImageWidth(magick_wand);
1031 oldh = MagickGetImageHeight(magick_wand);
1033 DBG(r,"detect width=[%d]", oldw);
1034 DBG(r,"detect heigh=[%d]", oldh);
1039 DBG(r,"detect spec width=[%d]", spec->width);
1040 DBG(r,"detect spec heigh=[%d]", spec->heigh);
1042 c_width = spec->width;
1043 c_heigh = spec->heigh;
1046 case IMG_CONV_MODE_THUMBNAIL:
1048 DBG(r,"**** detect thumbnail mode ****");
1050 if (neww > c_width) {
1051 newh = (int)((double)newh * (double)((double)c_width / (double)neww));
1052 neww = (int)((double)neww * (double)((double)c_width / (double)neww));
1054 if (newh > c_heigh) {
1055 neww = (int)((double)neww * (double)((double)c_heigh / (double)newh));
1056 newh = (int)((double)newh * (double)((double)c_heigh / (double)newh));
1059 neww = (int)((double)(neww / 3) * 0.8);
1060 newh = (int)((double)(newh / 3) * 0.8);
1063 case IMG_CONV_MODE_WALLPAPER:
1064 case IMG_CONV_MODE_EZGET:
1066 DBG(r,"**** detect wallpaper mode ****");
1068 if (spec->wp_width && spec->wp_heigh) {
1069 c_width = spec->wp_width;
1070 c_heigh = spec->wp_heigh;
1073 DBG(r,"calc new width and height");
1075 neww = (int)((double)neww * (double)((double)c_heigh / (double)newh));
1076 newh = (int)((double)newh * (double)((double)c_heigh / (double)newh));
1078 DBG(r,"newh = [%d] neww = [%d]", newh, neww);
1083 DBG(r,"**** detect normal mode ****");
1085 if (qsp->ua_flag != UA_IGN && spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
1086 if (neww > c_width) {
1087 newh = (int)((double)newh * (double)((double)c_width / (double)neww));
1088 neww = (int)((double)neww * (double)((double)c_width / (double)neww));
1091 if (newh > c_heigh) {
1092 neww = (int)((double)neww * (double)((double)c_heigh / (double)newh));
1093 newh = (int)((double)newh * (double)((double)c_heigh / (double)newh));
1099 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
1100 DBG(r,"convert width=[%d --> %d]", oldw, neww);
1101 DBG(r,"convert heigh=[%d --> %d]", oldh, newh);
1103 MagickResetIterator(magick_wand);
1105 while (MagickNextImage(magick_wand) != MagickFalse) {
1107 case IMG_CONV_MODE_WALLPAPER:
1108 case IMG_CONV_MODE_EZGET:
1110 if (MagickResizeImage(magick_wand,neww,newh,LanczosFilter,1.0) == MagickFalse) {
1111 EXIT_MAGICK_ERROR();
1115 if (MagickCropImage(magick_wand,
1116 (unsigned long)c_width,
1117 (unsigned long)c_heigh,
1118 (long)((neww - c_width) / 2),
1119 (long)((newh - c_heigh) / 2)) == MagickFalse) {
1120 EXIT_MAGICK_ERROR();
1125 case IMG_CONV_MODE_NORMAL:
1127 DBG(r,"convert width=[%d --> %d]", neww, qsp->width);
1131 DBG(r,"convert heigh=[%d --> %d]", newh, qsp->height);
1136 if (MagickResizeImage(magick_wand,neww,newh,LanczosFilter,1.0) == MagickFalse) {
1137 EXIT_MAGICK_ERROR();
1143 if (spec->html_spec_type != CHXJ_SPEC_UNKNOWN) {
1144 if (MagickSetImageUnits(magick_wand, PixelsPerInchResolution) == MagickFalse) {
1145 EXIT_MAGICK_ERROR();
1149 if (MagickSetImageResolution(magick_wand,
1150 (double)spec->dpi_width,
1151 (double)spec->dpi_heigh) == MagickFalse) {
1152 EXIT_MAGICK_ERROR();
1156 if (MagickSetImageDispose(magick_wand, BackgroundDispose) == MagickFalse) {
1157 EXIT_MAGICK_ERROR();
1167 s_fixup_color(MagickWand* magick_wand, request_rec* r, device_table* spec, img_conv_mode_t UNUSED(mode))
1169 DBG(r,"start chxj_fixup_clor()");
1171 if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
1172 DBG(r, "Pass s_fixup_color proc");
1176 if (spec->color >= 256) {
1178 DBG(r,"call MagickQuantizeImage() spec->color=[%d]",spec->color);
1180 if (MagickQuantizeImage(magick_wand,
1185 0) == MagickFalse) {
1186 EXIT_MAGICK_ERROR();
1190 DBG(r,"call end MagickQuantizeImage() spec->color=[%d]",spec->color);
1194 DBG(r,"call MagickQuantizeImage() spec->color=[%d]",spec->color);
1196 if (MagickQuantizeImage(magick_wand,
1201 0) == MagickFalse) {
1202 EXIT_MAGICK_ERROR();
1206 DBG(r,"call end MagickQuantizeImage() spec->color=[%d]",spec->color);
1210 DBG(r,"end chxj_fixup_clor()");
1218 s_fixup_depth(MagickWand* magick_wand, request_rec* r, device_table* spec)
1220 if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
1221 DBG(r, "Pass s_fixup_depth proc");
1225 if (spec->color == 15680000) {
1226 if (MagickSetImageDepth(magick_wand, 24) == MagickFalse) {
1227 EXIT_MAGICK_ERROR();
1232 if (spec->color == 262144) {
1233 if (MagickSetImageDepth(magick_wand, 18) == MagickFalse) {
1234 EXIT_MAGICK_ERROR();
1239 if (spec->color == 65536) {
1240 if (MagickSetImageDepth(magick_wand, 16) == MagickFalse) {
1241 EXIT_MAGICK_ERROR();
1246 if (spec->color == 4096) {
1247 if (MagickSetImageDepth(magick_wand, 12) == MagickFalse) {
1248 EXIT_MAGICK_ERROR();
1253 if (spec->color == 256) {
1254 if (MagickSetImageDepth(magick_wand, 8) == MagickFalse) {
1255 EXIT_MAGICK_ERROR();
1260 if (spec->color == 4) {
1261 if (MagickSetImageDepth(magick_wand, 2) == MagickFalse) {
1262 EXIT_MAGICK_ERROR();
1267 if (spec->color == 2) {
1268 if (MagickSetImageDepth(magick_wand, 1) == MagickFalse) {
1269 EXIT_MAGICK_ERROR();
1279 s_add_copyright(MagickWand* magick_wand, request_rec* r, device_table* spec)
1281 mod_chxj_config* conf = ap_get_module_config(r->per_dir_config, &chxj_module);
1283 if (spec->html_spec_type == CHXJ_SPEC_UNKNOWN) {
1284 DBG(r, "Pass add_copiright proc");
1288 if (conf->image_copyright) {
1290 DBG(r, "Add COPYRIGHT [%s]", conf->image_copyright);
1292 if (spec->html_spec_type == CHXJ_SPEC_Jhtml) {
1293 apr_table_setn(r->headers_out, "x-jphone-copyright", "no-transfer");
1294 if (MagickCommentImage(magick_wand,
1295 apr_psprintf(r->pool,
1297 conf->image_copyright)) == MagickFalse)
1301 if (spec->html_spec_type == CHXJ_SPEC_XHtml_Mobile_1_0
1302 || spec->html_spec_type == CHXJ_SPEC_Hdml) {
1303 if (MagickCommentImage(magick_wand,
1304 apr_psprintf(r->pool,
1305 "kddi_copyright=on,%s",
1306 conf->image_copyright)) == MagickFalse)
1310 if (MagickCommentImage(magick_wand,
1311 apr_psprintf(r->pool,
1313 conf->image_copyright)) == MagickFalse)
1318 if (MagickCommentImage(magick_wand, "mod_chxj") == MagickFalse)
1324 EXIT_MAGICK_ERROR();
1329 s_img_down_sizing(MagickWand* magick_wand, request_rec* r, device_table* spec)
1331 MagickBooleanType status;
1332 unsigned long quality = 70;
1333 apr_size_t writebyte = 0;
1335 apr_size_t prev_size = 0;
1336 int revers_flag = 0;
1338 writedata = (char*)MagickGetImageBlob(magick_wand, &writebyte);
1339 prev_size = writebyte;
1342 if (MagickSetImageCompressionQuality(magick_wand, quality) == MagickFalse) {
1343 EXIT_MAGICK_ERROR();
1347 writedata = (char*)MagickGetImageBlob(magick_wand, &writebyte);
1348 if (writebyte >= prev_size || revers_flag) {
1349 DBG(r, "quality=[%ld] size=[%d]", (long)quality, (int)writebyte);
1352 if (quality > 100) {
1353 if (MagickSetImageCompression(magick_wand,NoCompression) == MagickFalse) {
1354 EXIT_MAGICK_ERROR();
1359 prev_size = writebyte;
1363 DBG(r, "quality=[%ld] size=[%d]", (long)quality, (int)writebyte);
1365 if (spec->cache == 0)
1368 if (writebyte <= (unsigned int)spec->cache)
1373 if (quality == 0 || quality > 100)
1381 && writebyte > (unsigned int)spec->cache) {
1382 unsigned long now_color = spec->color;
1383 unsigned long depth = 0;
1386 case 2: depth = 1; break;
1387 case 4: now_color = 2; depth = 1; break;
1388 case 8: now_color = 4; depth = 2; break;
1389 case 16: now_color = 8; depth = 3; break;
1390 case 256: now_color = 16; depth = 4; break;
1391 case 4096: now_color = 256; depth = 8; break;
1392 case 65536: now_color = 4096; depth = 12; break;
1393 case 262144: now_color = 65536; depth = 16; break;
1394 case 15680000: now_color = 262144; depth = 18; break;
1400 if (now_color <= 2) break;
1402 if (now_color >= 8) {
1403 status = MagickQuantizeImage(magick_wand,
1411 status = MagickQuantizeImage(magick_wand,
1417 MagickSetImageType(magick_wand, GrayscaleType);
1419 if (status == MagickFalse) {
1420 EXIT_MAGICK_ERROR();
1424 if (MagickSetImageDepth(magick_wand, depth) == MagickFalse) {
1425 EXIT_MAGICK_ERROR();
1429 writedata = (char*)MagickGetImageBlob(magick_wand, &writebyte);
1431 DBG(r,"now_color=[%ld] size=[%d]", (long)now_color, (int)writebyte);
1433 /* Once per request */
1436 while(now_color > 2);
1443 s_send_cache_file(device_table* spec, query_string_param_t* query_string, request_rec* r, const char* tmpfile)
1448 apr_size_t sendbyte;
1449 char* contentLength;
1451 rv = apr_stat(&st, tmpfile, APR_FINFO_MIN, r->pool);
1452 if (rv != APR_SUCCESS)
1453 return HTTP_NOT_FOUND;
1455 DBG(r, "mode:[%d]", query_string->mode);
1456 DBG(r, "name:[%s]", query_string->name);
1457 DBG(r, "offset:[%ld]", query_string->offset);
1458 DBG(r, "count:[%ld]", query_string->count);
1460 if (spec->available_jpeg) {
1461 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1464 if (spec->available_png) {
1465 r->content_type = apr_psprintf(r->pool, "image/png");
1468 if (spec->available_gif) {
1469 r->content_type = apr_psprintf(r->pool, "image/gif");
1472 if (spec->available_bmp2 || spec->available_bmp4) {
1473 r->content_type = apr_psprintf(r->pool, "image/bmp");
1476 if (query_string->mode != IMG_CONV_MODE_EZGET && query_string->name == NULL) {
1477 contentLength = apr_psprintf(r->pool, "%d", (int)st.size);
1478 apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1480 DBG(r,"Content-Length:[%d]", (int)st.size);
1482 rv = apr_file_open(&fout, tmpfile,
1483 APR_READ | APR_BINARY, APR_OS_DEFAULT, r->pool);
1484 if (rv != APR_SUCCESS) {
1485 DBG(r, "cache file open failed[%s]", tmpfile);
1486 return HTTP_NOT_FOUND;
1489 ap_send_fd(fout, r, 0, st.size, &sendbyte);
1490 apr_file_close(fout);
1492 DBG(r, "send file data[%d]byte", (int)sendbyte);
1495 if (query_string->mode == IMG_CONV_MODE_EZGET) {
1496 char* name = apr_pstrdup(r->pool, basename(r->filename));
1497 name[strlen(name)-4] = 0;
1498 if (strcasecmp(r->content_type, "image/jpeg") == 0) {
1500 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1501 ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".jpg", (long)st.size, "devjaww", name);
1504 if (strcasecmp(r->content_type, "image/bmp") == 0) {
1505 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1506 ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".bmp", (long)st.size, "devabm", name);
1509 if (strcasecmp(r->content_type, "image/png") == 0) {
1510 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1511 ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".png", (long)st.size, "dev8aww", name);
1514 if (strcasecmp(r->content_type, "image/gif") == 0) {
1515 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1516 ap_rprintf(r, HDML_FIRST_PAGE, r->uri, name, ".gif", (long)st.size, "devgi0z", name);
1520 if (query_string->mode == IMG_CONV_MODE_WALLPAPER && query_string->name != NULL) {
1521 if (query_string->count == -1 && query_string->offset == -1) {
1522 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1523 ap_rprintf(r, HDML_SUCCESS_PAGE);
1527 if (query_string->count == -2 && query_string->offset == -1) {
1528 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1529 ap_rprintf(r, HDML_FAIL_PAGE);
1533 ap_set_content_type(r, "application/x-up-download");
1534 contentLength = apr_psprintf(r->pool, "%ld", query_string->count);
1535 apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1537 DBG(r, "Content-Length:[%d]", (int)st.size);
1539 rv = apr_file_open(&fout, tmpfile,
1540 APR_READ | APR_BINARY, APR_OS_DEFAULT, r->pool);
1542 if (rv != APR_SUCCESS) {
1543 DBG(r,"tmpfile open failed[%s]", tmpfile);
1544 return HTTP_NOT_FOUND;
1547 ap_send_fd(fout, r, query_string->offset, query_string->count, &sendbyte);
1548 apr_file_close(fout);
1550 DBG(r, "send file data[%d]byte", (int)sendbyte);
1559 s_send_original_file(request_rec* r, const char* originalfile)
1564 apr_size_t sendbyte = 0;
1566 rv = apr_stat(&st, originalfile, APR_FINFO_MIN, r->pool);
1567 if (rv != APR_SUCCESS)
1568 return HTTP_NOT_FOUND;
1570 rv = apr_file_open(&fout, originalfile,
1571 APR_READ | APR_BINARY, APR_OS_DEFAULT, r->pool);
1572 if (rv != APR_SUCCESS) {
1573 DBG(r, "originalfile open failed[%s]", originalfile);
1574 return HTTP_NOT_FOUND;
1577 ap_send_fd(fout, r, 0, st.size, &sendbyte);
1578 apr_file_close(fout);
1580 DBG(r, "send file data[%d]byte", (int)sendbyte);
1586 s_header_only_cache_file(device_table* spec, query_string_param_t* query_string, request_rec* r, const char* tmpfile)
1590 char* contentLength;
1592 rv = apr_stat(&st, tmpfile, APR_FINFO_MIN, r->pool);
1593 if (rv != APR_SUCCESS)
1594 return HTTP_NOT_FOUND;
1596 DBG(r, "mode:[%d]", query_string->mode);
1597 DBG(r, "name:[%s]", query_string->name);
1598 DBG(r, "offset:[%ld]", query_string->offset);
1599 DBG(r, "count:[%ld]", query_string->count);
1601 if (spec->available_jpeg) {
1602 r->content_type = apr_psprintf(r->pool, "image/jpeg");
1605 if (spec->available_png) {
1606 r->content_type = apr_psprintf(r->pool, "image/png");
1609 if (spec->available_gif) {
1610 r->content_type = apr_psprintf(r->pool, "image/gif");
1613 if (spec->available_bmp2 || spec->available_bmp4) {
1614 r->content_type = apr_psprintf(r->pool, "image/bmp");
1617 if (query_string->mode != IMG_CONV_MODE_EZGET && query_string->name == NULL) {
1618 contentLength = apr_psprintf(r->pool, "%d", (int)st.size);
1619 apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1621 DBG(r,"Content-Length:[%d]", (int)st.size);
1624 if (query_string->mode == IMG_CONV_MODE_EZGET) {
1625 char* name = apr_pstrdup(r->pool, basename(r->filename));
1626 name[strlen(name)-4] = 0;
1627 if (strcasecmp(r->content_type, "image/jpeg") == 0) {
1629 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1632 if (strcasecmp(r->content_type, "image/bmp") == 0) {
1633 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1636 if (strcasecmp(r->content_type, "image/png") == 0) {
1637 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1640 if (strcasecmp(r->content_type, "image/gif") == 0) {
1641 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1645 if (query_string->mode == IMG_CONV_MODE_WALLPAPER && query_string->name != NULL) {
1646 if (query_string->count == -1 && query_string->offset == -1) {
1647 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1650 if (query_string->count == -2 && query_string->offset == -1) {
1651 ap_set_content_type(r, "text/x-hdml; charset=Shift_JIS");
1654 ap_set_content_type(r, "application/x-up-download");
1655 contentLength = apr_psprintf(r->pool, "%ld", query_string->count);
1656 apr_table_setn(r->headers_out, "Content-Length", (const char*)contentLength);
1658 DBG(r, "Content-Length:[%d]", (int)st.size);
1667 s_init_serial_pattern(apr_pool_t *p)
1669 if (!v_docomo_serial_pattern1) {
1670 v_docomo_serial_pattern1 = chxj_compile_for_preg_replace(p, "/ser[^;\\)]+");
1672 if (!v_docomo_serial_pattern2) {
1673 v_docomo_serial_pattern2 = chxj_compile_for_preg_replace(p, ";ser[^;\\)]+");
1675 if (!v_docomo_serial_pattern3) {
1676 v_docomo_serial_pattern3 = chxj_compile_for_preg_replace(p, ";icc[^;\\)]+");
1678 if (!v_softbank_serial_pattern1) {
1679 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] ");
1684 s_create_workfile_name(
1686 mod_chxj_config* conf,
1687 const char* user_agent,
1688 query_string_param_t *qsp)
1693 char* w = apr_palloc(r->pool, 256);
1695 char *new_user_agent;
1698 s_init_serial_pattern(r->pool);
1701 new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern1, "", user_agent);
1702 new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern2, "", new_user_agent);
1703 new_user_agent = chxj_preg_replace(r->pool, v_docomo_serial_pattern3, "", new_user_agent);
1706 new_user_agent = chxj_preg_replace(r->pool, v_softbank_serial_pattern1, " ", new_user_agent);
1708 DBG(r, "old user_agent:[%s] ==> new user_agent:[%s]", user_agent, new_user_agent);
1712 switch (qsp->mode) {
1713 case IMG_CONV_MODE_THUMBNAIL:
1714 fname = apr_psprintf(r->pool, "%s.%s.thumbnail", r->filename, new_user_agent);
1715 DBG(r, "mode=thumbnail [%s]", fname);
1717 case IMG_CONV_MODE_WALLPAPER:
1718 case IMG_CONV_MODE_EZGET:
1719 fname = apr_psprintf(r->pool, "%s.%s.wallpaper", r->filename, new_user_agent);
1720 DBG(r, "mode=WallPaper [%s]", fname);
1722 case IMG_CONV_MODE_NORMAL:
1725 fname = apr_psprintf(r->pool, "%s.%s", r->filename, new_user_agent);
1728 fname = apr_psprintf(r->pool, "%s.w%d", fname, qsp->width);
1731 fname = apr_psprintf(r->pool, "%s.h%d", fname, qsp->height);
1733 DBG(r,"mode=normal [%s]", fname);
1736 if (qsp->ua_flag == UA_IGN) {
1737 fname = apr_psprintf(r->pool, "%s.IGN", fname);
1740 len = strlen(fname);
1742 for (ii=0; ii<len; ii++) {
1743 if (fname[ii] == '/'
1747 || fname[ii] == ')') {
1751 w[jj++] = fname[ii];
1755 return apr_psprintf(r->pool, "%s/%s", conf->image_cache_dir,w);
1759 static unsigned short
1760 s_add_crc(const char* writedata, apr_size_t writebyte)
1762 unsigned short crc = 0xffff;
1766 for(ii=0;ii<writebyte;ii++) {
1768 crc = AU_CRC_TBL[(crc>>8^ch)&0xff]^(crc<<8);
1774 chxj_trans_name(request_rec *r)
1781 mod_chxj_config* conf;
1789 "qrc", /* QRCode½ÐÎÏÍÑ¥Õ¥¡¥¤¥ë¤Î³ÈÄ¥»Ò */
1795 int do_ext_check = TRUE;
1796 int next_ok = FALSE;
1798 DBG(r, "start chxj_trans_name()");
1800 conf = ap_get_module_config(r->per_dir_config, &chxj_module);
1803 DBG(r, "end chxj_trans_name() conf is null[%s]", r->uri);
1807 if (conf->image != CHXJ_IMG_ON) {
1808 DBG(r, "end chxj_trans_name() conf not found");
1813 DBG(r,"Match URI[%s]", r->uri);
1816 if (r->filename == NULL)
1817 r->filename = apr_pstrdup(r->pool, r->uri);
1820 if ((idx = strchr(r->filename, ':')) != NULL)
1821 filename_sv = idx+1;
1823 filename_sv = r->filename;
1825 DBG(r,"r->filename[%s]", filename_sv);
1827 ccp = ap_document_root(r);
1829 return HTTP_INTERNAL_SERVER_ERROR;
1831 docroot = apr_pstrdup(r->pool, ccp);
1832 len = strlen(docroot);
1834 if (docroot[len-1] == '/')
1835 docroot[len-1] = '\0';
1839 && *filename_sv == *r->server->path
1840 && strncmp(filename_sv, r->server->path, r->server->pathlen) == 0)
1841 filename_sv = apr_pstrcat(r->pool, docroot, (filename_sv + r->server->pathlen), NULL);
1843 filename_sv = apr_pstrcat(r->pool, docroot, filename_sv, NULL);
1845 DBG(r,"URI[%s]", filename_sv);
1847 do_ext_check = TRUE;
1848 for (ii=0; ii<7-1; ii++) {
1849 char* pos = strrchr(filename_sv, '.');
1851 if (strcasecmp(pos, ext[ii]) == 0) {
1852 do_ext_check = FALSE;
1853 fname = apr_psprintf(r->pool, "%s", filename_sv);
1860 for (ii=0; ii<7; ii++) {
1861 if (strlen(ext[ii]) == 0) {
1862 fname = apr_psprintf(r->pool, "%s", filename_sv);
1865 fname = apr_psprintf(r->pool, "%s.%s", filename_sv, ext[ii]);
1867 DBG(r,"search [%s]", fname);
1869 rv = apr_stat(&st, fname, APR_FINFO_MIN, r->pool);
1870 if (rv == APR_SUCCESS) {
1871 if (st.filetype != APR_DIR)
1878 if (fname == NULL) {
1879 DBG(r,"NotFound [%s]", r->filename);
1882 for (ii=0; ii<7-1; ii++) {
1883 char* pos = strrchr(fname, '.');
1885 if (strcasecmp(pos, ext[ii]) == 0) {
1893 DBG(r,"NotFound [%s]", r->filename);
1897 if (r->handler == NULL || strcasecmp(r->handler, "chxj-qrcode") != 0) {
1898 DBG(r,"Found [%s]", fname);
1900 r->filename = apr_psprintf(r->pool, "%s", fname);
1902 if (strcasecmp("qrc", ext[ii]) == 0)
1903 r->handler = apr_psprintf(r->pool, "chxj-qrcode");
1905 r->handler = apr_psprintf(r->pool, "chxj-picture");
1907 DBG(r, "end chxj_trans_name()");
1914 * It converts it from QUERYSTRING.
1918 static query_string_param_t*
1919 s_get_query_string_param(request_rec *r)
1927 query_string_param_t* param;
1929 s = apr_pstrdup(r->pool, r->parsed_uri.query);
1930 param = apr_palloc(r->pool, sizeof(query_string_param_t));
1931 param->mode = IMG_CONV_MODE_NORMAL;
1932 param->user_agent = NULL;
1933 param->ua_flag = UA_USE;
1940 if (s == NULL) return param;
1943 if ((pair = apr_strtok(s, "&", &pstate)) == NULL) break;
1946 name = apr_strtok(pair, "=", &vstate);
1947 value = apr_strtok(NULL, "=", &vstate);
1952 if (value && (strcasecmp(name, "mode") == 0 || strcasecmp(name, "m") == 0)) {
1957 if (strcasecmp(value, "thumbnail") == 0 || strcasecmp(value, "tb") == 0)
1958 param->mode = IMG_CONV_MODE_THUMBNAIL;
1963 if (strcasecmp(value, "WP") == 0 || strcasecmp(value, "WallPaper") == 0)
1964 param->mode = IMG_CONV_MODE_WALLPAPER;
1969 if (strcasecmp(value, "EZGET") == 0)
1970 param->mode = IMG_CONV_MODE_EZGET;
1980 if (value && (strcasecmp(name, "ua") == 0 || strcasecmp(name, "user-agent") == 0)) {
1981 ap_unescape_url(value);
1983 if ((*value == 'i' || *value == 'I') && strcasecmp(value, "IGN") == 0)
1984 param->ua_flag = UA_IGN;
1986 param->user_agent = apr_pstrdup(r->pool, value);
1992 if (value && strcasecmp(name, "name") == 0)
1993 param->name = apr_pstrdup(r->pool, value);
1998 if (value && strcasecmp(name, "offset") == 0 && (! chxj_chk_numeric(value)))
1999 param->offset = chxj_atoi(value);
2005 if (value && strcasecmp(name, "count") == 0 && (! chxj_chk_numeric(value)))
2006 param->count = chxj_atoi(value);
2011 if (value && strcasecmp(name, "w") == 0 && (! chxj_chk_numeric(value)))
2012 param->width = chxj_atoi(value);
2017 if (value && strcasecmp(name, "h") == 0 && (! chxj_chk_numeric(value)))
2018 param->height = chxj_atoi(value);
2026 if (param->mode == IMG_CONV_MODE_NORMAL && param->name)
2027 param->mode = IMG_CONV_MODE_WALLPAPER;