OSDN Git Service

Once more, Allow building with either giflib 4.2 or 5.0.
[swfed/swfed.git] / src / php_swfed.c
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2008 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Yoshihiro Yamazaki <yoya@awm.jp>                             |
16   +----------------------------------------------------------------------+
17 */
18
19 /* $Id:$ */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "php_swfed.h"
29
30 #include "swf_define.h"
31 #include "y_keyvalue.h"
32 #include "bitmap_util.h"
33
34 #include "swf_tag_jpeg.h"
35 #include "swf_tag_lossless.h"
36 #include "swf_tag_edit.h"
37 #include "swf_tag_sound.h"
38 #include "swf_tag_action.h"
39 #include "swf_tag_sprite.h"
40 #include "swf_tag_shape.h"
41 #include "swf_tag_place.h"
42 #include "swf_tag.h"
43 #include "swf_object.h"
44
45 #define SWFED_VERSION "0.63a"
46
47 #define get_zend_hash_value_long(table, key, value) do { \
48         zval **tmp = NULL; \
49         if (zend_hash_find(table, key, sizeof(key), (void**)&tmp) == SUCCESS) { \
50             convert_to_long_ex(tmp); \
51             value = Z_LVAL_PP(tmp); \
52         } \
53     } while (0);
54
55 #define get_zend_hash_value_boolean(table, key, value) do { \
56         zval **tmp = NULL; \
57         if (zend_hash_find(table, key, sizeof(key), (void**)&tmp) == SUCCESS) { \
58             convert_to_boolean_ex(tmp); \
59             value = Z_LVAL_PP(tmp); \
60         } \
61     } while (0);
62
63 static int
64 param_is_null(int n TSRMLS_DC) {
65     zval *p;
66     zend_parse_parameters(n TSRMLS_CC, "z", &p);
67     if(Z_TYPE_P(p) == IS_NULL) {
68         return 1; // true
69     }
70     return 0; // false
71 }
72
73
74 /* If you declare any globals in php_swfed.h uncomment this:
75 ZEND_DECLARE_MODULE_GLOBALS(swfed)
76 */
77
78 /* True global resources - no need for thread safety here */
79 static int le_swfed;
80
81 /* {{{ swfed_functions[]
82  *
83  * Every user visible function must have an entry in swfed_functions[].
84  */
85 zend_function_entry swfed_functions[] = {
86     PHP_ME(swfed,  __construct, NULL, 0)
87     PHP_ME(swfed,  input, NULL, 0)
88     PHP_ME(swfed,  output, NULL, 0)
89     PHP_ME(swfed,  swfInfo, NULL, 0)
90     PHP_ME(swfed,  _destroy_and_exit, NULL, 0) // for debug
91
92     PHP_ME(swfed,  getHeaderInfo, NULL, 0)
93     PHP_ME(swfed,  setHeaderInfo, NULL, 0)
94     PHP_ME(swfed,  getTagList, NULL, 0)
95     PHP_ME(swfed,  getTagDetail, NULL, 0)
96     PHP_ME(swfed,  getTagData, NULL, 0)
97     PHP_ME(swfed,  replaceTagData, NULL, 0)
98     PHP_ME(swfed,  getTagDataByCID, NULL, 0)
99     PHP_ME(swfed,  replaceTagDataByCID, NULL, 0)
100     PHP_ME(swfed,  getTagContentsByCID, NULL, 0)
101     PHP_ME(swfed,  replaceTagContentsByCID, NULL, 0)
102     PHP_ME(swfed,  removeTag, NULL, 0)
103     PHP_ME(swfed,  printTagData, NULL, 0)
104
105     PHP_ME(swfed,  getShapeData, NULL, 0)
106     PHP_ME(swfed,  replaceShapeData, NULL, 0)
107     PHP_ME(swfed,  setShapeAdjustMode, NULL, 0)
108     PHP_ME(swfed,  getShapeIdListByBitmapRef, NULL, 0)
109     PHP_ME(swfed,  getBitmapSize, NULL, 0)
110     PHP_ME(swfed,  getJpegData, NULL, 0)
111     PHP_ME(swfed,  getJpegAlpha, NULL, 0)
112     PHP_ME(swfed,  replaceJpegData, NULL, 0)
113     PHP_ME(swfed,  getPNGData, NULL, 0)
114     PHP_ME(swfed,  replacePNGData, NULL, 0)
115     PHP_ME(swfed,  replaceGIFData, NULL, 0)
116     PHP_ME(swfed,  replaceBitmapData, NULL, 0)
117     PHP_ME(swfed,  convertBitmapDataToJpegTag, NULL, 0)
118     PHP_ME(swfed,  applyShapeMatrixFactor, NULL, 0)
119     PHP_ME(swfed,  applyShapeRectFactor, NULL, 0)
120     PHP_ME(swfed,  getSoundData, NULL, 0)
121     PHP_ME(swfed,  replaceMLDData, NULL, 0)
122     PHP_ME(swfed,  getEditString, NULL, 0)
123     PHP_ME(swfed,  replaceEditString, NULL, 0)
124     PHP_ME(swfed,  getActionData, NULL, 0)
125     PHP_ME(swfed,  disasmActionData, NULL, 0)
126     PHP_ME(swfed,  setActionVariables, NULL, 0)
127     PHP_ME(swfed,  replaceActionStrings, NULL, 0)
128     PHP_ME(swfed,  replaceMovieClip, NULL, 0)
129
130     PHP_ME(swfed,  setCompressLevel, NULL, 0)
131     PHP_ME(swfed,  rebuild, NULL, 0)
132     PHP_ME(swfed,  purgeUselessContents, NULL, 0)
133
134     PHP_ME(swfed,  isShapeTagData, NULL, 0)
135     PHP_ME(swfed,  isBitmapTagData, NULL, 0)
136     {NULL, NULL, NULL}  /* Must be the last line in swfed_functions[] */
137 };
138 /* }}} */
139
140 /* {{{ swfed_module_entry
141  */
142 zend_module_entry swfed_module_entry = {
143 #if ZEND_MODULE_API_NO >= 20010901
144         STANDARD_MODULE_HEADER,
145 #endif
146         "swfed",
147         NULL, // no global functions
148         PHP_MINIT(swfed),
149         PHP_MSHUTDOWN(swfed),
150         PHP_RINIT(swfed),               /* Replace with NULL if there's nothing to do at request start */
151         PHP_RSHUTDOWN(swfed),   /* Replace with NULL if there's nothing to do at request end */
152         PHP_MINFO(swfed),
153 #if ZEND_MODULE_API_NO >= 20010901
154         SWFED_VERSION, /* Replace with version number for your extension */
155 #endif
156         STANDARD_MODULE_PROPERTIES
157 };
158 /* }}} */
159
160 #ifdef COMPILE_DL_SWFED
161 ZEND_GET_MODULE(swfed)
162 #endif
163
164 /* {{{ PHP_INI
165  */
166 /* Remove comments and fill if you need to have entries in php.ini
167 PHP_INI_BEGIN()
168     STD_PHP_INI_ENTRY("swfed.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_swfed_globals, swfed_globals)
169     STD_PHP_INI_ENTRY("swfed.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_swfed_globals, swfed_globals)
170 PHP_INI_END()
171 */
172 /* }}} */
173
174 /* {{{ php_swfed_init_globals
175  */
176 /* Uncomment this function if you have INI entries
177 static void php_swfed_init_globals(zend_swfed_globals *swfed_globals)
178 {
179         swfed_globals->global_value = 0;
180         swfed_globals->global_string = NULL;
181 }
182 */
183 /* }}} */
184
185
186 static void free_swfed_resource(zend_rsrc_list_entry *resource TSRMLS_DC);
187 static swf_object_t  *get_swf_object(zval *obj TSRMLS_DC);
188
189 /* {{{ PHP_MINIT_FUNCTION
190  */
191 PHP_MINIT_FUNCTION(swfed)
192 {
193         /* If you have INI entries, uncomment these lines 
194         ZEND_INIT_MODULE_GLOBALS(swfed, php_swfed_init_globals, NULL);
195         REGISTER_INI_ENTRIES();
196         */
197         zend_class_entry ce;
198         INIT_CLASS_ENTRY(ce, "SWFEditor", swfed_functions);
199         swfeditor_ce = zend_register_internal_class(&ce TSRMLS_CC);
200         le_swfed = zend_register_list_destructors_ex(free_swfed_resource, NULL, "SWFEditor", module_number);
201     
202         zend_declare_property_stringl(swfeditor_ce,
203                                 "swf_object", strlen("swf_object"),
204                                 "", 0, ZEND_ACC_PUBLIC TSRMLS_CC);
205         // class const
206         REGISTER_SWFED_CLASS_CONST_LONG("SHAPE_BITMAP_NONE", SWFED_SHAPE_BITMAP_NONE);
207         REGISTER_SWFED_CLASS_CONST_LONG("SHAPE_BITMAP_MATRIX_RESCALE", SWFED_SHAPE_BITMAP_MATRIX_RESCALE);
208         REGISTER_SWFED_CLASS_CONST_LONG("SHAPE_BITMAP_RECT_RESIZE", SWFED_SHAPE_BITMAP_RECT_RESIZE);
209         REGISTER_SWFED_CLASS_CONST_LONG("SHAPE_BITMAP_TYPE_TILLED", SWFED_SHAPE_BITMAP_TYPE_TILLED);
210         return SUCCESS;
211 }
212
213 /* }}} */
214
215 /* {{{ PHP_MSHUTDOWN_FUNCTION
216  */
217 PHP_MSHUTDOWN_FUNCTION(swfed)
218 {
219         /* uncomment this line if you have INI entries
220         UNREGISTER_INI_ENTRIES();
221         */
222         return SUCCESS;
223 }
224 /* }}} */
225
226 /* Remove if there's nothing to do at request start */
227 /* {{{ PHP_RINIT_FUNCTION
228  */
229 PHP_RINIT_FUNCTION(swfed)
230 {
231         return SUCCESS;
232 }
233 /* }}} */
234
235 /* Remove if there's nothing to do at request end */
236 /* {{{ PHP_RSHUTDOWN_FUNCTION
237  */
238 PHP_RSHUTDOWN_FUNCTION(swfed)
239 {
240         return SUCCESS;
241 }
242 /* }}} */
243
244 /* {{{ PHP_MINFO_FUNCTION
245  */
246 PHP_MINFO_FUNCTION(swfed)
247 {
248         int gif_support = 0;
249         int png_support = 0;
250         php_info_print_table_start();
251         php_info_print_table_header(2, "SWF Editor support", "enabled");
252         php_info_print_table_row(2, "SWF Editor version", SWFED_VERSION);
253 #ifdef HAVE_PNG
254         png_support = 1;
255 #endif
256 #ifdef HAVE_GIF
257         gif_support = 1;
258 #endif
259         php_info_print_table_row(2, "SWF Editor PNG support", png_support?"yes":"no");
260         php_info_print_table_row(2, "SWF Editor GIF support", gif_support?"yes":"no");
261         php_info_print_table_end();
262
263         /* Remove comments if you have entries in php.ini
264         DISPLAY_INI_ENTRIES();
265         */
266 }
267 /* }}} */
268
269
270 /* Remove the following function when you have succesfully modified config.m4
271    so that your module can be compiled into PHP, it exists only for testing
272    purposes. */
273
274 /* Every user-visible function in PHP should document itself in the source */
275 /* {{{ proto string confirm_swfed_compiled(string arg)
276    Return a string to confirm that the module is compiled in */
277 PHP_FUNCTION(confirm_swfed_compiled)
278 {
279         char *arg = NULL;
280         int arg_len, len;
281         char string[256];
282
283         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
284                 return;
285         }
286
287         len = sprintf(string, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "swfed", arg);
288 //         RETURN_STRINGL(string, len, 0);
289     RETURN_STRINGL(string, len, 1);
290 }
291 /* }}} */
292 /* The previous line is meant for vim and emacs, so it can correctly fold and 
293    unfold functions in source code. See the corresponding marks just before 
294    function definition, where the functions purpose is also documented. Please 
295    follow this convention for the convenience of others editing your code.
296 */
297
298
299 /*
300  * Local variables:
301  * tab-width: 4
302  * c-basic-offset: 4
303  * End:
304  * vim600: noet sw=4 ts=4 fdm=marker
305  * vim<600: noet sw=4 ts=4
306  */
307
308 PHP_METHOD(swfed, __construct) {
309     swf_object_t *swf = swf_object_open();
310     int ret = 0;
311     if (swf == NULL) {
312         php_error_docref(NULL TSRMLS_CC, E_ERROR, "Couldn't create swf object");
313     }
314 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5)
315     ret = zend_list_insert(swf, le_swfed TSRMLS_CC);
316 #else
317     ret = zend_list_insert(swf, le_swfed);
318 #endif
319     object_init_ex(getThis(), swfeditor_ce);
320     add_property_resource(getThis(), "swfed", ret);
321     zend_list_addref(ret);
322 }
323
324 PHP_METHOD(swfed, input) {
325     char *data = NULL;
326     int data_len = 0;
327     swf_object_t *swf = NULL;
328     int result = 0;
329     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
330                               "s", &data, &data_len) == FAILURE) {
331         RETURN_FALSE;
332     }
333     swf = get_swf_object(getThis() TSRMLS_CC);
334     result = swf_object_input(swf, (unsigned char *) data, data_len);
335     if (result) {
336         RETURN_FALSE;
337     }
338     RETURN_TRUE;
339 }
340
341 PHP_METHOD(swfed, output) {
342     unsigned long len = 0;
343     unsigned char *data = NULL;
344     char *new_buff = NULL;
345     swf_object_t *swf = NULL;
346     if (ZEND_NUM_ARGS() != 0) {
347         WRONG_PARAM_COUNT;
348         RETURN_FALSE; /* XXX */
349     }
350     swf = get_swf_object(getThis() TSRMLS_CC);
351     data = swf_object_output(swf, &len);
352     new_buff = emalloc(len);
353     if (new_buff == NULL) {
354         fprintf(stderr, "output: Can't emalloc new_buff\n");
355         free(data);
356         RETURN_FALSE;
357     }
358     memcpy(new_buff, data, len);
359     free(data);
360     RETURN_STRINGL(new_buff, len, 0);
361 }
362
363 PHP_METHOD(swfed, swfInfo) {
364     swf_object_t *swf = get_swf_object(getThis() TSRMLS_CC);
365     swf_object_print(swf);
366     RETURN_TRUE;
367 }
368
369 PHP_METHOD(swfed, _destroy_and_exit) { // for debug
370     swf_object_t *swf = NULL;
371     swf = get_swf_object(getThis() TSRMLS_CC);
372     swf_object_close(swf);
373     exit(0);
374 }
375
376 PHP_METHOD(swfed, getHeaderInfo) {
377     swf_object_t *swf = get_swf_object(getThis() TSRMLS_CC);
378     array_init(return_value);
379     if (memcmp(swf->header.magic, "CWS", 3) == 0) {
380         add_assoc_bool(return_value, "compress", 1);
381     } else { // FWS
382         add_assoc_bool(return_value, "compress", 0);
383     }
384     add_assoc_long(return_value, "version", swf->header.version);
385
386     add_assoc_long(return_value, "x_min", swf->header_movie.frame_size.x_min / SWF_TWIPS);
387     add_assoc_long(return_value, "y_min", swf->header_movie.frame_size.y_min / SWF_TWIPS);
388     add_assoc_long(return_value, "x_max", swf->header_movie.frame_size.x_max / SWF_TWIPS);
389     add_assoc_long(return_value, "y_max", swf->header_movie.frame_size.y_max / SWF_TWIPS);
390     
391     
392 }
393
394 PHP_METHOD(swfed, setHeaderInfo) {
395     zval *header_info = NULL;
396     swf_object_t *swf = NULL;
397     HashTable *header_table = NULL;
398     zval **tmp = NULL;
399     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a",
400                               &header_info) == FAILURE) {
401         php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid parameters");
402         RETURN_FALSE;
403     }
404     swf = get_swf_object(getThis() TSRMLS_CC);
405     header_table = Z_ARRVAL_P(header_info);
406
407     // FWS or CWS
408     if (zend_hash_find(header_table, "compress", sizeof("compress"), (void**)&tmp) == SUCCESS) {
409         convert_to_boolean_ex(tmp);
410         if (Z_LVAL_PP(tmp) != 0) {
411             memcpy(swf->header.magic, "CWS", 3);
412         } else {
413             memcpy(swf->header.magic, "FWS", 3);
414         }
415     }
416
417     // Version
418     if (zend_hash_find(header_table, "version", sizeof("version"), (void**)&tmp) == SUCCESS) {
419         convert_to_long_ex(tmp);
420         swf->header.version = Z_LVAL_PP(tmp);
421     }
422
423     // FrameRect
424     if (zend_hash_find(header_table, "x_min", sizeof("x_min"), (void**)&tmp) == SUCCESS) {
425         convert_to_long_ex(tmp);
426         swf->header_movie.frame_size.x_min = Z_LVAL_PP(tmp) * SWF_TWIPS;
427     }
428     if (zend_hash_find(header_table, "y_min", sizeof("y_min"), (void**)&tmp) == SUCCESS) {
429         convert_to_long_ex(tmp);
430         swf->header_movie.frame_size.y_min = Z_LVAL_PP(tmp) * SWF_TWIPS;
431     }
432     if (zend_hash_find(header_table, "x_max", sizeof("x_max"), (void**)&tmp) == SUCCESS) {
433         convert_to_long_ex(tmp);
434         swf->header_movie.frame_size.x_max = Z_LVAL_PP(tmp) * SWF_TWIPS;
435     }
436     if (zend_hash_find(header_table, "y_max", sizeof("y_max"), (void**)&tmp) == SUCCESS) {
437         convert_to_long_ex(tmp);
438         swf->header_movie.frame_size.y_max = Z_LVAL_PP(tmp) * SWF_TWIPS;
439     }
440     RETURN_TRUE;
441 }
442
443 PHP_METHOD(swfed, getTagList) {
444     int i = 0;
445     zval *data = NULL;
446     swf_object_t *swf = NULL;
447     swf_tag_t *tag = NULL;
448     swf_tag_info_t *tag_info = NULL;
449     if (ZEND_NUM_ARGS() != 0) {
450         WRONG_PARAM_COUNT;
451         RETURN_FALSE; /* XXX */
452     }
453     swf = get_swf_object(getThis() TSRMLS_CC);
454     array_init(return_value);
455     for (tag=swf->tag_head ; tag ; tag=tag->next) {
456         ALLOC_INIT_ZVAL(data);
457         array_init(data);
458         add_assoc_long(data, "code", tag->code);
459         add_assoc_long(data, "tag", tag->code);
460         tag_info = get_swf_tag_info(tag->code);
461         if (tag_info && tag_info->name) {
462             add_assoc_string_ex(data,
463                                 "tagName", sizeof("tagName"),
464                                 (char *)tag_info->name, 1);
465         }
466         add_assoc_long(data, "length", tag->length);
467         if (tag_info && tag_info->detail_handler) {
468             add_assoc_bool(data, "detail", 1);
469         }
470         add_index_zval(return_value, i, data);
471         i++;
472     }
473 }
474
475 PHP_METHOD(swfed, getTagDetail) {
476     long tag_seqno = 0;
477     swf_object_t *swf = NULL;
478     swf_tag_t *tag = NULL;
479     swf_tag_info_t *tag_info = NULL;
480     int i = 0;
481     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
482                               "l", &tag_seqno) == FAILURE) {
483         RETURN_FALSE;
484     }
485     swf = get_swf_object(getThis() TSRMLS_CC);
486     i = 0;
487     for (tag=swf->tag_head ; tag ; tag=tag->next) {
488         if (i == tag_seqno) {
489             break;
490         }
491         i++;
492     }
493     if (tag == NULL) {
494         RETURN_FALSE;
495     }
496     tag_info = get_swf_tag_info(tag->code);
497     if ((tag_info == NULL) || (tag_info->detail_handler == NULL)) {
498         RETURN_FALSE;
499     }
500     if (swf_tag_create_input_detail(tag, swf) == NULL) {
501         RETURN_FALSE; // can't create detail
502     }
503     switch (tag->code) {
504         swf_tag_jpeg_detail_t     *tag_jpeg;
505         swf_tag_lossless_detail_t *tag_lossless;
506         swf_tag_edit_detail_t     *tag_edit;
507         swf_tag_sound_detail_t    *tag_sound;
508         swf_tag_action_detail_t   *tag_action;
509         swf_tag_sprite_detail_t   *tag_sprite;
510         swf_tag_shape_detail_t    *tag_shape;
511         swf_tag_place_detail_t    *tag_place;
512         swf_action_t   *action;
513         int action_list_count;
514         zval *data = NULL;
515         int *bitmap_id_list, bitmap_id_list_num;
516       case 6:  // DefineBitsJPEG
517       case 21: // DefineBitsJPEG2
518       case 35: // DefineBitsJPEG3
519         tag_jpeg = tag->detail;
520         array_init(return_value);
521         add_assoc_long(return_value, "image_id", tag_jpeg->image_id);
522         add_assoc_long(return_value, "jpeg_data_len", tag_jpeg->jpeg_data_len);
523         if ((tag_jpeg->alpha_data != NULL) &&
524             (tag_jpeg->alpha_data_len > 0)) {
525             add_assoc_long(return_value, "alpha_data_len", tag_jpeg->alpha_data_len);
526         }
527         break;
528       case 20: // DefineBitsLossless
529       case 36: // DefineBitsLossless2
530         tag_lossless = tag->detail;
531         array_init(return_value);
532         add_assoc_long(return_value, "image_id", tag_lossless->image_id);
533         add_assoc_long(return_value, "format", tag_lossless->format);
534         add_assoc_long(return_value, "width", tag_lossless->width);
535         add_assoc_long(return_value, "height", tag_lossless->height);
536         if (tag_lossless->format == 3) {
537             add_assoc_long(return_value, "colormap_count", tag_lossless->colormap_count);
538         }
539         break;
540     case 14: // DefineSound
541         tag_sound = tag->detail;
542         array_init(return_value);
543         add_assoc_long(return_value, "sound_id", tag_sound->sound_id);
544         add_assoc_long(return_value, "format", (unsigned long) tag_sound->sound_format);
545         add_assoc_long(return_value, "rate", (unsigned long) tag_sound->sound_rate);
546         add_assoc_long(return_value, "is_16bits", tag_sound->sound_is_16bits?1:0);
547         add_assoc_long(return_value, "is_stereo", tag_sound->sound_is_stereo?1:0);
548         add_assoc_long(return_value, "sound_samples_count", tag_sound->sound_samples_count);
549         add_assoc_long(return_value, "sound_data_len", tag_sound->sound_data_len);
550         break;
551     case 12: // DoAction
552     case 59: // DoInitAction
553         tag_action = tag->detail;
554         array_init(return_value);
555         if (tag->code == 59) { // DoInitAction
556             add_assoc_long(return_value, "action_sprite", tag_action->action_sprite);
557         }
558         action = tag_action->action_list->head;
559         if (action) {
560             for (action_list_count = 0 ; action ; action_list_count++) {
561                 action = action->next;
562             }
563             add_assoc_long(return_value, "action_list_count", action_list_count);
564         }
565         break;
566       case 37: // DefineEditText;
567         tag_edit = tag->detail;
568         array_init(return_value);
569         add_assoc_long(return_value, "edit_id", tag_edit->edit_id);
570         if (tag_edit->edit_variable_name && tag_edit->edit_variable_name[0]) {
571             add_assoc_string_ex(return_value, "variable_name",
572                                 sizeof("variable_name"),
573                                 (char *)tag_edit->edit_variable_name, 1);
574         }
575         if (tag_edit->edit_initial_text && tag_edit->edit_initial_text[0]) {
576             add_assoc_string_ex(return_value, "initial_text",
577                                 sizeof("initial_text"),
578                                 (char *)tag_edit->edit_initial_text, 1);
579         }
580         break;
581       case 39: // DefineSprite;
582         tag_sprite = tag->detail;
583         array_init(return_value);
584         add_assoc_long(return_value, "sprite_id", tag_sprite->sprite_id);
585         add_assoc_long(return_value, "frame_count", tag_sprite->frame_count);
586         break;
587       case 2: // DefineShape;
588       case 22: // DefineShape2;
589       case 32: // DefineShape3;
590       case 46: // DefineMorphShape;
591         tag_shape = tag->detail;
592         array_init(return_value);
593         add_assoc_long(return_value, "shape_id", tag_shape->shape_id);
594         add_assoc_long(return_value, "fill_styles.count", tag_shape->shape_with_style.styles.fill_styles.count);
595         add_assoc_long(return_value, "line_styles.count", tag_shape->shape_with_style.styles.line_styles.count);
596 //        tag_shape->shape_with_style.shape_records
597         bitmap_id_list = swf_tag_shape_bitmap_get_refcid_list(tag, &bitmap_id_list_num);
598         if (bitmap_id_list) {
599             int i;
600             ALLOC_INIT_ZVAL(data);
601             array_init(data);
602             for (i = 0 ; i < bitmap_id_list_num ; i++) {
603                 add_index_long(data, i , bitmap_id_list[i]);
604             }
605             add_assoc_zval(return_value, "bitmap_ref", data);
606             free(bitmap_id_list);
607         }
608         break;
609       case 4: // PlaceObject
610       case 26: // PlaceObject2
611         tag_place = tag->detail;
612         array_init(return_value);
613         if ((tag->code == 4) || tag_place->flag_has_character) {
614             add_assoc_long(return_value, "character_id", tag_place->character_id);
615         }
616         add_assoc_long(return_value, "depth", tag_place->depth);
617         if (tag_place->flag_has_name) {
618             add_assoc_string_ex(return_value, "name",
619                                 sizeof("name"), (char *)tag_place->name, 1);
620         }
621
622       default:
623           break;
624         RETURN_FALSE;
625     }
626     /* return_value */
627 }
628
629 PHP_METHOD(swfed, getTagData) {
630     long tag_seqno = 0;
631     swf_object_t *swf = NULL;
632     unsigned char *data = NULL, *new_buff;
633     unsigned long data_len = 0;
634     
635     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag_seqno) == FAILURE) {
636         RETURN_FALSE;
637     }
638     swf = get_swf_object(getThis() TSRMLS_CC);
639     data = swf_object_get_tagdata(swf, tag_seqno, &data_len);
640     if (data == NULL) {
641         fprintf(stderr, "getTagData: Can't get_tagdata\n");
642         RETURN_FALSE;
643     }
644     new_buff = emalloc(data_len);
645     memcpy(new_buff, data, data_len);
646     free(data);
647     RETURN_STRINGL(new_buff, data_len, 0);
648 }
649
650 PHP_METHOD(swfed, replaceTagData) {
651     char *data = NULL;
652     int data_len = 0;
653     long tag_seqno = 0;
654     swf_object_t *swf = NULL;
655     int result = 0;
656     switch (ZEND_NUM_ARGS()) {
657       default:
658         WRONG_PARAM_COUNT;
659         RETURN_FALSE; /* XXX */
660       case 2:
661         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &tag_seqno, &data, &data_len) == FAILURE) {
662             RETURN_FALSE;
663         }
664         break;
665     }
666     swf = get_swf_object(getThis() TSRMLS_CC);
667     result = swf_object_replace_tagdata(swf, tag_seqno,
668                                         (unsigned char *)data, 
669                                         (unsigned long) data_len);
670     if (result) {
671         RETURN_FALSE;
672     }
673     RETURN_TRUE;
674 }
675
676 PHP_METHOD(swfed, getTagDataByCID) {
677     long cid = 0;
678     swf_object_t *swf = NULL;
679     unsigned char *data_ref = NULL;
680     unsigned long data_len = 0;
681     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &cid) == FAILURE) {
682         RETURN_FALSE;
683     }
684     swf = get_swf_object(getThis() TSRMLS_CC);
685     data_ref = swf_object_get_tagdata_bycid(swf, cid, &data_len);
686     if (data_ref == NULL) {
687         fprintf(stderr, "getTagDataByCID: Can't get_tagdata_bycid\n");
688         RETURN_FALSE;
689     }
690     RETURN_STRINGL((char *) data_ref, data_len, 1);
691 }
692
693 PHP_METHOD(swfed, replaceTagDataByCID) {
694     char *data = NULL;
695     unsigned long data_len = 0;
696     long cid = 0;
697     swf_object_t *swf = NULL;
698     int result = 0;
699     switch (ZEND_NUM_ARGS()) {
700       default:
701         WRONG_PARAM_COUNT;
702         RETURN_FALSE; /* XXX */
703       case 2:
704         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &cid, &data, &data_len) == FAILURE) {
705             RETURN_FALSE;
706         }
707         break;
708     }
709     swf = get_swf_object(getThis() TSRMLS_CC);
710     result = swf_object_replace_tagdata_bycid(swf, cid,
711                                               (unsigned char *)data,
712                                               data_len);
713     if (result) {
714         RETURN_FALSE;
715     }
716     RETURN_TRUE;
717 }
718
719 PHP_METHOD(swfed, getTagContentsByCID) {
720     long cid = 0;
721     swf_object_t *swf = NULL;
722     unsigned char *data_ref = NULL;
723     unsigned long data_len = 0;
724     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &cid) == FAILURE) {
725         RETURN_FALSE;
726     }
727     swf = get_swf_object(getThis() TSRMLS_CC);
728     data_ref = swf_object_get_tagcontents_bycid(swf, cid, &data_len);
729     if (data_ref == NULL) {
730         fprintf(stderr, "getTagContentsByCID: Can't get_tagcontents_bycid\n");
731         RETURN_FALSE;
732     }
733     RETURN_STRINGL((char *)data_ref, data_len, 1);
734 }
735
736 PHP_METHOD(swfed, replaceTagContentsByCID) {
737     char *data = NULL;
738     unsigned long data_len = 0;
739     long cid = 0;
740     swf_object_t *swf = NULL;
741     int result = 0;
742     switch (ZEND_NUM_ARGS()) {
743       default:
744         WRONG_PARAM_COUNT;
745         RETURN_FALSE; /* XXX */
746       case 2:
747         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &cid, &data, &data_len) == FAILURE) {
748             RETURN_FALSE;
749         }
750         break;
751     }
752     swf = get_swf_object(getThis() TSRMLS_CC);
753     result = swf_object_replace_tagcontents_bycid(swf, cid,
754                                                   (unsigned char *)data,
755                                                   data_len);
756     if (result) {
757         RETURN_FALSE;
758     }
759     RETURN_TRUE;
760 }
761
762 PHP_METHOD(swfed, removeTag) {
763     long tag_seqno = 0;
764     long tag_seqno_in_sprite = -1;
765     swf_object_t *swf = NULL;
766     int ret;
767
768     switch (ZEND_NUM_ARGS()) {
769       case 1:
770         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag_seqno) == FAILURE) {
771             RETURN_FALSE;
772         }
773         break;
774       case 2:
775         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &tag_seqno, &tag_seqno_in_sprite) == FAILURE) {
776             RETURN_FALSE;
777         }
778         break;
779     }
780     swf = get_swf_object(getThis() TSRMLS_CC);
781     ret = swf_object_remove_tag(swf, tag_seqno, tag_seqno_in_sprite);
782     if (ret) {
783         RETURN_FALSE;
784     }
785     RETURN_TRUE;
786 }
787
788 PHP_METHOD(swfed, printTagData) {
789     char *data = NULL;
790     int data_len = 0;
791     swf_object_t *swf = NULL;
792     int ret = 0;
793     switch (ZEND_NUM_ARGS()) {
794       default:
795         WRONG_PARAM_COUNT;
796         RETURN_FALSE; /* XXX */
797       case 1:
798         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) {
799             RETURN_FALSE;
800         }
801         break;
802     }
803     swf = get_swf_object(getThis() TSRMLS_CC);
804     ret = swf_object_print_tagdata(swf, (unsigned char *)data,
805                                    (unsigned long) data_len);
806     if (ret) {
807         RETURN_FALSE;
808     }
809     RETURN_TRUE;
810 }
811
812 PHP_METHOD(swfed, getShapeData) {
813     long cid = 0;
814     swf_object_t *swf = NULL;
815     unsigned char *data = NULL, *new_buff;
816     unsigned long data_len = 0;
817     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &cid) == FAILURE) {
818         RETURN_FALSE;
819     }
820     swf = get_swf_object(getThis() TSRMLS_CC);
821     data = swf_object_get_shapedata(swf, cid, &data_len);
822     if (data == NULL) {
823         fprintf(stderr, "getShapeData: Can't swf_object_get_shapedata (cid=%ld)\n", cid);
824         RETURN_FALSE;
825     }
826     new_buff = emalloc(data_len);
827     memcpy(new_buff, data, data_len);
828     free(data);
829     RETURN_STRINGL((char *)new_buff, data_len, 0);
830 }
831
832 PHP_METHOD(swfed, replaceShapeData) {
833     char *data = NULL;
834     int data_len = 0;
835     long cid = 0;
836     swf_object_t *swf = NULL;
837     int result = 0;
838     switch (ZEND_NUM_ARGS()) {
839       default:
840         WRONG_PARAM_COUNT;
841         RETURN_FALSE; /* XXX */
842       case 2:
843         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &cid, &data, &data_len) == FAILURE) {
844             RETURN_FALSE;
845         }
846         break;
847     }
848     swf = get_swf_object(getThis() TSRMLS_CC);
849     result = swf_object_replace_shapedata(swf, cid,
850                                           (unsigned char *)data,
851                                           (unsigned long)data_len);
852     if (result) {
853         RETURN_FALSE;
854     }
855     RETURN_TRUE;
856 }
857
858 PHP_METHOD(swfed, setShapeAdjustMode) {
859     swf_object_t *swf = NULL;
860     unsigned long mode = 0;
861     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
862                               "l", &mode) == FAILURE) {
863         RETURN_FALSE;
864     }
865     swf = get_swf_object(getThis() TSRMLS_CC);
866     swf_object_set_shape_adjust_mode(swf, mode);
867     RETURN_TRUE;
868 }
869
870 PHP_METHOD(swfed, getShapeIdListByBitmapRef) {
871     long bitmap_id;
872     int *bitmap_id_list, bitmap_id_list_num;
873     swf_object_t *swf = NULL;
874     swf_tag_t *tag = NULL;
875     int i;
876     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
877                               "l", &bitmap_id) == FAILURE) {
878         RETURN_FALSE;
879     }
880     swf = get_swf_object(getThis() TSRMLS_CC);
881     array_init(return_value);
882     i = 0;
883     for (tag = swf->tag_head ; tag ; tag=tag->next) {
884         register int tag_code = tag->code;
885         if (isShapeTag(tag_code)) {
886             bitmap_id_list = swf_tag_shape_bitmap_get_refcid_list(tag, &bitmap_id_list_num);
887             if (bitmap_id_list) {
888                 int j;
889                 for (j=0 ; j < bitmap_id_list_num ; j++) {
890                     if (bitmap_id_list[j] == bitmap_id) {
891                         swf_tag_shape_detail_t *swf_tag_shape = tag->detail;
892                         add_index_long(return_value, i, (long) swf_tag_shape->shape_id);
893                         i++;
894                         break;
895                     }
896                 }
897                 free(bitmap_id_list);
898             }
899         }
900     }
901 }
902
903 PHP_METHOD(swfed, getBitmapSize) {
904     long bitmap_id;
905     swf_object_t *swf = NULL;
906     int width, height;
907     int ret;
908     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
909                               "l", &bitmap_id) == FAILURE) {
910         RETURN_FALSE;
911     }
912     swf = get_swf_object(getThis() TSRMLS_CC);
913     ret = swf_object_get_bitmap_size(swf, bitmap_id, &width, &height);
914     if (ret) {
915         RETURN_FALSE;
916     }
917     array_init(return_value);
918     add_assoc_long(return_value, "width", (long) width);
919     add_assoc_long(return_value, "height", (long) height);
920 }
921
922 PHP_METHOD(swfed, getJpegData) {
923     unsigned long image_id = 0;
924     unsigned long len = 0;
925     unsigned char *data = NULL;
926     char *new_buff = NULL;
927     swf_object_t *swf = NULL;
928     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
929                               "l", &image_id) == FAILURE) {
930         RETURN_FALSE;
931     }
932     swf = get_swf_object(getThis() TSRMLS_CC);
933     data = swf_object_get_jpegdata(swf, &len, image_id);
934     if (data == NULL) {
935         RETURN_FALSE;
936     }
937     new_buff = emalloc(len);
938     if (new_buff == NULL) {
939         fprintf(stderr, "getJpegData Can't emalloc new_buff\n");
940         free(data);
941         RETURN_FALSE;
942     }
943     memcpy(new_buff, data, len);
944     free(data);
945     RETURN_STRINGL(new_buff, (int) len, 0);
946 }
947
948 PHP_METHOD(swfed, getJpegAlpha) {
949     unsigned long image_id = 0;
950     unsigned long len = 0;
951     unsigned char *data = NULL;
952     char *new_buff = NULL;
953     swf_object_t *swf = NULL;
954     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
955                               "l", &image_id) == FAILURE) {
956         RETURN_FALSE;
957     }
958     swf = get_swf_object(getThis() TSRMLS_CC);
959     data = swf_object_get_alphadata(swf, &len, image_id);
960     if (data == NULL) {
961         RETURN_FALSE;
962     }
963     new_buff = emalloc(len);
964     if (new_buff == NULL) {
965         fprintf(stderr, "getJpegAlpha Can't emalloc new_buff\n");
966         free(data);
967         RETURN_FALSE;
968     }
969     memcpy(new_buff, data, len);
970     free(data);
971     RETURN_STRINGL(new_buff, len, 0);
972 }
973
974 PHP_METHOD(swfed, replaceJpegData) {
975     char *data = NULL, *alpha_data = NULL;
976     int data_len = 0 , alpha_data_len = 0;
977     long image_id = 0;
978     swf_object_t *swf = NULL;
979     int result = 0;
980     switch (ZEND_NUM_ARGS()) {
981       default:
982         WRONG_PARAM_COUNT;
983         RETURN_FALSE; /* XXX */
984       case 2:
985         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &image_id, &data, &data_len) == FAILURE) {
986             RETURN_FALSE;
987         }
988          break;
989       case 3:
990         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lss", &image_id, &data, &data_len, &alpha_data, &alpha_data_len) == FAILURE) {
991             RETURN_FALSE;
992         }
993         break;
994     }
995     swf = get_swf_object(getThis() TSRMLS_CC);
996     result = swf_object_replace_jpegdata(swf, image_id,
997                                          (unsigned char *)data,
998                                          (unsigned long) data_len,
999                                          (unsigned char *)alpha_data,
1000                                          (unsigned long) alpha_data_len,
1001                                          0);
1002     if (result) {
1003         RETURN_FALSE;
1004     }
1005     RETURN_TRUE;
1006 }
1007
1008 PHP_METHOD(swfed, getPNGData) {
1009 #ifndef HAVE_PNG
1010     fprintf(stderr, "replacePNGData: no png library\n");
1011     RETURN_FALSE;
1012 #else /* HAVE_PNG */
1013     unsigned long image_id = 0;
1014     unsigned long len = 0;
1015     unsigned char *data = NULL;
1016     char *new_buff = NULL;
1017     swf_object_t *swf = NULL;
1018     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1019                               "l", &image_id) == FAILURE) {
1020         RETURN_FALSE;
1021     }
1022     swf = get_swf_object(getThis() TSRMLS_CC);
1023     data = swf_object_get_pngdata(swf, &len, image_id);
1024     if (data == NULL) {
1025         RETURN_FALSE;
1026     }
1027     new_buff = emalloc(len);
1028     if (new_buff == NULL) {
1029         fprintf(stderr, "getPNGData: Can't emalloc new_buff\n");
1030         free(data);
1031         RETURN_FALSE;
1032     }
1033     memcpy(new_buff, data, len);
1034     free(data);
1035     RETURN_STRINGL(new_buff, (int) len, 0);
1036 #endif /* HAVE_PNG */
1037 }
1038
1039 PHP_METHOD(swfed, replacePNGData) {
1040 #ifndef HAVE_PNG
1041     fprintf(stderr, "replacePNGData: no png library\n");
1042     RETURN_FALSE;
1043 #else  /* HAVE_PNG */
1044     long image_id = 0;
1045     char *data = NULL;
1046     int data_len = 0;
1047     zval *opts = NULL;
1048     HashTable *opts_table = NULL;
1049     int rgb15 = -1;
1050     swf_object_t *swf;
1051     int result = 0;
1052     switch (ZEND_NUM_ARGS()) {
1053       default:
1054         WRONG_PARAM_COUNT;
1055         RETURN_FALSE; /* XXX */
1056       case 2:
1057       case 3:
1058         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|a", &image_id, &data, &data_len, &opts) == FAILURE) {
1059             RETURN_FALSE;
1060         }
1061         break;
1062     }
1063     if (opts) {
1064         opts_table = Z_ARRVAL_P(opts);
1065         get_zend_hash_value_boolean(opts_table, "rgb15",  rgb15);
1066     }
1067     
1068     swf = get_swf_object(getThis() TSRMLS_CC);
1069
1070     result = swf_object_replace_pngdata(swf, image_id,
1071                                         (unsigned char *)data,
1072                                         (unsigned long) data_len,
1073                                         rgb15);
1074     if (result) {
1075         RETURN_FALSE;
1076     }
1077     RETURN_TRUE;
1078 #endif /* HAVE_PNG */
1079 }
1080
1081 PHP_METHOD(swfed, replaceGIFData) {
1082 #ifndef HAVE_GIF
1083     fprintf(stderr, "replaceGIFData: no gif library\n");
1084     RETURN_FALSE;
1085 #else /* HAVE_GIF */
1086     long image_id = 0;
1087     char *data = NULL;
1088     int data_len = 0;
1089     swf_object_t *swf = NULL;
1090     int result = 0;
1091     switch (ZEND_NUM_ARGS()) {
1092       default:
1093         WRONG_PARAM_COUNT;
1094         RETURN_FALSE; /* XXX */
1095       case 2:
1096         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &image_id, &data, &data_len) == FAILURE) {
1097             RETURN_FALSE;
1098         }
1099         break;
1100     }
1101     swf = get_swf_object(getThis() TSRMLS_CC);
1102     result = swf_object_replace_gifdata(swf, image_id,
1103                                         (unsigned char *)data,
1104                                         (unsigned long) data_len);
1105     if (result) {
1106         RETURN_FALSE;
1107     }
1108     RETURN_TRUE;
1109 #endif /* HAVE_GIF */
1110 }
1111
1112 PHP_METHOD(swfed, replaceBitmapData) {
1113     char *data = NULL, *alpha_data = NULL;
1114     int data_len = 0 , alpha_data_len = 0;
1115     zval *arg1 = NULL, *arg4 = NULL;
1116     int image_id = 0;
1117     HashTable *image_info_table = NULL;
1118     int width = -1, height = -1;
1119     int red = -1, green = -1, blue = -1;
1120     HashTable *opts_table = NULL;
1121     int without_converting = 0;
1122     int rgb15 = -1;
1123     swf_object_t *swf = NULL;
1124     int result = 0;
1125     int bitmap_format;
1126     if (param_is_null(1 TSRMLS_CC)) {
1127         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1128         RETURN_FALSE; /* XXX */
1129     }
1130     switch (ZEND_NUM_ARGS()) {
1131       default:
1132         WRONG_PARAM_COUNT;
1133         RETURN_FALSE; /* XXX */
1134       case 2:
1135       case 3:
1136       case 4:
1137       case 5:
1138         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
1139                                      ZEND_NUM_ARGS() TSRMLS_CC, "zs|sz", &arg1, &data, &data_len, &alpha_data, &alpha_data_len, &arg4) == SUCCESS) {
1140             ; // OK
1141         } else {
1142             php_error(E_WARNING, "%s() expects parameter 2 to be string given", get_active_function_name(TSRMLS_C));
1143             RETURN_FALSE;
1144         }
1145         break;
1146     }
1147     swf = get_swf_object(getThis() TSRMLS_CC);
1148
1149     if (Z_TYPE_P(arg1) != IS_ARRAY) { // image_id (integer)
1150         if (Z_TYPE_P(arg1) != IS_LONG) {
1151             convert_to_long(arg1);
1152         }
1153         image_id = (int) Z_LVAL_P(arg1);
1154     } else { // or image_info (array)
1155         image_info_table = Z_ARRVAL_P(arg1);
1156         get_zend_hash_value_long(image_info_table, "width",  width);
1157         get_zend_hash_value_long(image_info_table, "height", height);
1158         get_zend_hash_value_long(image_info_table, "red",    red);
1159         get_zend_hash_value_long(image_info_table, "green",  green);
1160         get_zend_hash_value_long(image_info_table, "blue",   blue);
1161         image_id = swf_object_search_cid_by_bitmap_condition(swf, width, height,
1162                                                              red, green, blue);
1163         if (image_id <= 0) {
1164             php_error(E_WARNING, "%s() cid not found by bitmap condition(width=%d height=%d red=%d green=%d blue=%d)", get_active_function_name(TSRMLS_C),
1165                       width, height, red, green, blue);
1166             RETURN_FALSE;
1167         }
1168     }
1169     if (arg4 != NULL) {
1170         if (Z_TYPE_P(arg4) != IS_ARRAY) { // without_converting (boolean)
1171             if (Z_TYPE_P(arg4) != IS_BOOL) {
1172                 convert_to_boolean(arg4);
1173             }
1174             without_converting = (int) Z_LVAL_P(arg4);
1175         } else { // or opts (array)
1176             opts_table = Z_ARRVAL_P(arg4);
1177             get_zend_hash_value_boolean(opts_table, "without_converting",  without_converting);
1178             get_zend_hash_value_boolean(opts_table, "rgb15", rgb15);
1179         }
1180     }
1181     bitmap_format = detect_bitmap_format((unsigned char*) data, data_len);
1182     if (without_converting) { // for v8 JPEG Tag
1183         switch (bitmap_format) {
1184         case BITMAP_UTIL_FORMAT_JPEG:
1185         case BITMAP_UTIL_FORMAT_PNG:
1186         case BITMAP_UTIL_FORMAT_GIF:
1187             result = swf_object_replace_jpegdata(swf, image_id,
1188                                                  (unsigned char *)data,
1189                                                  (unsigned long) data_len,
1190                                                  (unsigned char *) 0,
1191                                                  (unsigned long) 0,
1192                                                  1);
1193             break;
1194         default:
1195             php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown Bitmap Format");
1196             RETURN_FALSE;
1197         }
1198     } else {
1199         switch (bitmap_format) {
1200         case BITMAP_UTIL_FORMAT_JPEG:
1201             result = swf_object_replace_jpegdata(swf, image_id,
1202                                                  (unsigned char *)data,
1203                                                  (unsigned long) data_len,
1204                                                  (unsigned char *)alpha_data,
1205                                                  (unsigned long) alpha_data_len,
1206                                                  0);
1207             break;
1208         case BITMAP_UTIL_FORMAT_PNG:
1209             result = swf_object_replace_pngdata(swf, image_id,
1210                                                 (unsigned char *)data,
1211                                                 (unsigned long) data_len,
1212                                                 rgb15);
1213             
1214             break;
1215         case BITMAP_UTIL_FORMAT_GIF:
1216             result = swf_object_replace_gifdata(swf, image_id,
1217                                                 (unsigned char *)data,
1218                                                 (unsigned long) data_len);
1219             
1220             break;
1221         default:
1222             php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown Bitmap Format");
1223             RETURN_FALSE;
1224         }
1225     }
1226     if (result) {
1227         RETURN_FALSE;
1228     }
1229     RETURN_TRUE;
1230 }
1231
1232 PHP_METHOD(swfed, convertBitmapDataToJpegTag) {
1233     swf_object_t *swf = NULL;
1234     int ret;
1235     swf = get_swf_object(getThis() TSRMLS_CC);
1236     ret = swf_object_convert_bitmapdata_tojpegtag(swf);
1237     if (ret) {
1238         RETURN_FALSE;
1239     }
1240     RETURN_TRUE;
1241 }
1242
1243 PHP_METHOD(swfed, applyShapeMatrixFactor) {
1244     long shape_id = 0;
1245     double scale_x = 1, scale_y = 1, rotate_rad = 0;
1246     long trans_x = 0, trans_y = 0;
1247     swf_object_t *swf = NULL;
1248     int result;
1249     if (param_is_null(1 TSRMLS_CC)) {
1250         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1251         RETURN_FALSE; /* XXX */
1252     }
1253     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1254                               "ldddll", &shape_id, &scale_x, &scale_y,
1255                               &rotate_rad, &trans_x, &trans_y) == FAILURE) {
1256         RETURN_FALSE;
1257     }
1258     swf = get_swf_object(getThis() TSRMLS_CC);
1259     result = swf_object_apply_shapematrix_factor(swf, shape_id, -1,
1260                                                  scale_x, scale_y, rotate_rad,
1261                                                  trans_x, trans_y);
1262     if (result) {
1263         RETURN_FALSE;
1264     }
1265
1266     RETURN_TRUE;
1267 }
1268
1269 PHP_METHOD(swfed, applyShapeRectFactor) {
1270     long shape_id = 0;
1271     double scale_x = 1, scale_y = 1;
1272     long trans_x = 0, trans_y = 0;
1273     swf_object_t *swf = NULL;
1274     int result;
1275     if (param_is_null(1 TSRMLS_CC)) {
1276         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1277         RETURN_FALSE; /* XXX */
1278     }
1279     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1280                               "lddll", &shape_id, &scale_x, &scale_y,
1281                               &trans_x, &trans_y) == FAILURE) {
1282         RETURN_FALSE;
1283     }
1284     swf = get_swf_object(getThis() TSRMLS_CC);
1285     result = swf_object_apply_shaperect_factor(swf, shape_id, -1,
1286                                                  scale_x, scale_y,
1287                                                  trans_x, trans_y);
1288     if (result) {
1289         RETURN_FALSE;
1290     }
1291     RETURN_TRUE;
1292 }
1293
1294 /*
1295 PHP_METHOD(swfed, adjustShapeSizeToBitmap) {
1296     RETURN_TRUE;
1297 }
1298
1299 PHP_METHOD(swfed, adjustShapeScaleToBitmap) {
1300     RETURN_TRUE;
1301 }
1302 */
1303
1304 PHP_METHOD(swfed, getSoundData) {
1305     unsigned long sound_id = 0;
1306     unsigned long len = 0;
1307     unsigned char *data = NULL;
1308     char *new_buff = NULL;
1309     swf_object_t *swf = NULL;
1310     if (param_is_null(1 TSRMLS_CC)) {
1311         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1312         RETURN_FALSE; /* XXX */
1313     }
1314     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1315                               "l", &sound_id) == FAILURE) {
1316         RETURN_FALSE;
1317     }
1318     swf = get_swf_object(getThis() TSRMLS_CC);
1319     data = swf_object_get_sounddata(swf, &len, sound_id);
1320     if (data == NULL) {
1321         RETURN_FALSE;
1322     }
1323     new_buff = emalloc(len);
1324     if (new_buff == NULL) {
1325         fprintf(stderr, "getSoundData: Can't emalloc new_buff\n");
1326         free(data);
1327         RETURN_FALSE;
1328     }
1329     memcpy(new_buff, data, len);
1330     free(data);
1331     RETURN_STRINGL(new_buff, (int) len, 0);
1332 }
1333
1334 PHP_METHOD(swfed, replaceMLDData) {
1335     long sound_id = 0;
1336     char *data = NULL;
1337     int data_len = 0;
1338     swf_object_t *swf = NULL;
1339     int result = 0;
1340     if (param_is_null(1 TSRMLS_CC)) {
1341         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1342         RETURN_FALSE;
1343     }
1344     switch (ZEND_NUM_ARGS()) {
1345       default:
1346         WRONG_PARAM_COUNT;
1347         RETURN_FALSE; /* XXX */
1348       case 2:
1349         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &sound_id, &data, &data_len) == FAILURE) {
1350             RETURN_FALSE;
1351         }
1352         break;
1353     }
1354     swf = get_swf_object(getThis() TSRMLS_CC);
1355     result = swf_object_replace_melodata(swf, sound_id,
1356                                          (unsigned char *)data,
1357                                          (unsigned long) data_len);
1358     if (result) {
1359         RETURN_FALSE;
1360     }
1361     RETURN_TRUE;
1362 }
1363
1364 PHP_METHOD(swfed, getEditString) {
1365     char *var_name = NULL;
1366     int  var_name_len = 0;
1367     swf_object_t *swf = NULL;
1368     char *data = NULL, *new_buff = NULL;
1369     int str_len = 0;
1370     int error = 0;
1371     if (param_is_null(1 TSRMLS_CC)) {
1372         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1373         RETURN_FALSE;
1374     }
1375     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
1376                               &var_name, &var_name_len) == FAILURE) {
1377         RETURN_FALSE;
1378     }
1379     swf = get_swf_object(getThis() TSRMLS_CC);
1380     data = swf_object_get_editstring(swf, var_name, var_name_len, &error);
1381     if (data == NULL) {
1382         if (error) {
1383             RETURN_FALSE;
1384         } else {
1385             RETURN_NULL();
1386         }
1387     }
1388     str_len = strlen(data);
1389     new_buff = emalloc(str_len);
1390     if (new_buff == NULL) {
1391         fprintf(stderr, "getEditString: Can't emalloc new_buff\n");
1392         free(data);
1393         RETURN_FALSE;
1394     }
1395     memcpy(new_buff, data, str_len);
1396     free(data);
1397     RETURN_STRINGL(new_buff, str_len, 0);
1398 }
1399
1400 PHP_METHOD(swfed, replaceEditString) {
1401     char *var_name = NULL, *ini_text = NULL;
1402     int  var_name_len = 0, ini_text_len = 0;
1403     swf_object_t *swf = NULL;
1404     int result = 0;
1405     if (param_is_null(1 TSRMLS_CC)) {
1406         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1407         RETURN_FALSE;
1408     }
1409     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
1410                               &var_name, &var_name_len,
1411                               &ini_text, &ini_text_len) == FAILURE) {
1412         RETURN_FALSE;
1413     }
1414     swf = get_swf_object(getThis() TSRMLS_CC);    
1415     result = swf_object_replace_editstring(swf, var_name,  var_name_len,
1416                                            ini_text, ini_text_len);
1417     if (result) {
1418         RETURN_FALSE;
1419     }
1420     RETURN_TRUE;
1421 }
1422
1423 PHP_METHOD(swfed, getActionData) {
1424     long tag_seqno = 0;
1425     swf_object_t *swf = NULL;
1426     unsigned char *data = NULL;
1427     char *new_buff = NULL;
1428     unsigned long data_len = 0;
1429     
1430     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag_seqno) == FAILURE) {
1431         RETURN_FALSE;
1432     }
1433     swf = get_swf_object(getThis() TSRMLS_CC);
1434     data = swf_object_get_actiondata(swf, &data_len, tag_seqno);
1435     if (data == NULL) {
1436         fprintf(stderr, "getActionData: Can't get_actiondata\n");
1437         RETURN_FALSE;
1438     }
1439     new_buff = emalloc(data_len);
1440     if (new_buff == NULL) {
1441         fprintf(stderr, "getActionData: Can't emalloc new_buff\n");
1442         free(data);
1443         RETURN_FALSE;
1444     }
1445     memcpy(new_buff, data, data_len);
1446     free(data);
1447     RETURN_STRINGL(new_buff, data_len, 0);
1448 }
1449
1450 PHP_METHOD(swfed, disasmActionData) {
1451     char *data = NULL;
1452     int data_len = 0;
1453     bitstream_t *bs = NULL;
1454     swf_action_list_t *action_list = NULL;
1455     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1456                               "s", &data, &data_len) == FAILURE) {
1457         RETURN_FALSE;
1458     }
1459     array_init(return_value);
1460     bs = bitstream_open();
1461     bitstream_input(bs, (unsigned char*) data, data_len);
1462     action_list = swf_action_list_create();
1463     swf_action_list_parse(bs, action_list);
1464     bitstream_close(bs);
1465     if (action_list) {
1466         swf_action_t *action = action_list->head;
1467         while(action) {
1468             printf("\t");
1469 //            swf_action_disasm(action);
1470             action = action->next;
1471         }
1472     }
1473     swf_action_list_destroy(action_list);
1474     RETURN_TRUE;
1475 }
1476
1477 PHP_METHOD(swfed, setActionVariables) {
1478     zval *zid, *arr, **entry;
1479     HashTable *arr_hash;
1480     HashPosition    pos;
1481     char            *str_key, *str_value;
1482     uint            str_key_len, str_value_len;
1483     ulong tmp;
1484     char tmp_str[17];
1485     int ret;
1486     y_keyvalue_t *kv;
1487     swf_object_t *swf = get_swf_object(getThis() TSRMLS_CC);
1488     
1489     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) {
1490         RETURN_FALSE;
1491     }
1492     kv = y_keyvalue_open();
1493
1494     arr_hash = Z_ARRVAL_P(arr);
1495     zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
1496     while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &
1497                                          pos) == SUCCESS) {
1498         convert_to_string_ex(entry);
1499         str_value = Z_STRVAL_PP(entry);
1500         str_value_len = Z_STRLEN_PP(entry);
1501         ret = zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &str_key, &
1502                                            str_key_len, &tmp, 0, &pos);
1503         switch (ret) {
1504         case HASH_KEY_IS_STRING:
1505             y_keyvalue_set(kv, str_key, str_key_len - 1, str_value, str_value_len);
1506             break;
1507         case HASH_KEY_IS_LONG:
1508             snprintf(tmp_str, 17, "%ld\0", tmp);
1509             y_keyvalue_set(kv, tmp_str, strlen(tmp_str), str_value, str_value_len);
1510             break;
1511         default:
1512             php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys invalid type(%d).", ret);
1513         }
1514         zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
1515     }
1516     swf_object_insert_action_setvariables(swf, kv);
1517     y_keyvalue_close(kv);
1518     RETURN_TRUE;
1519 }
1520
1521 PHP_METHOD(swfed, replaceActionStrings) {
1522     zval *zid, *arr, **entry;
1523     HashTable *arr_hash;
1524     HashPosition    pos;
1525     char            *str_key, *str_value;
1526     uint            str_key_len, str_value_len;
1527     ulong tmp;
1528     char tmp_str[17];
1529     int ret;
1530     y_keyvalue_t *kv;
1531     swf_object_t *swf = get_swf_object(getThis() TSRMLS_CC);
1532     
1533     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) {
1534         RETURN_FALSE;
1535     }
1536     kv = y_keyvalue_open();
1537
1538     arr_hash = Z_ARRVAL_P(arr);
1539     zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
1540     while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &
1541                                          pos) == SUCCESS) {
1542         convert_to_string_ex(entry);
1543         str_value = Z_STRVAL_PP(entry);
1544         str_value_len = Z_STRLEN_PP(entry);
1545         ret = zend_hash_get_current_key_ex(Z_ARRVAL_P(arr), &str_key, &
1546                                            str_key_len, &tmp, 0, &pos);
1547         switch (ret) {
1548         case HASH_KEY_IS_STRING:
1549             y_keyvalue_set(kv, str_key, str_key_len - 1, str_value, str_value_len);
1550             break;
1551         case HASH_KEY_IS_LONG:
1552             snprintf(tmp_str, 17, "%ld\0", tmp);
1553             y_keyvalue_set(kv, tmp_str, strlen(tmp_str), str_value, str_value_len);
1554             break;
1555         default:
1556             php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array keys invalid type(%d).", ret);
1557         }
1558         zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos);
1559     }
1560     swf_object_replace_action_strings(swf, kv);
1561     y_keyvalue_close(kv);
1562     RETURN_TRUE;
1563 }
1564
1565 PHP_METHOD(swfed, replaceMovieClip) {
1566     char *instance_name = NULL, *swf_data = NULL;
1567     int  instance_name_len = 0, swf_data_len = 0;
1568     swf_object_t *swf = NULL;
1569     zend_bool unused_cid_purge = 1; // ¥Ç¥Õ¥©¥ë¥È on
1570     int result = 0;
1571     if (param_is_null(1 TSRMLS_CC)) {
1572         php_error(E_WARNING, "%s() 1st arg must be not NULL", get_active_function_name(TSRMLS_C));
1573         RETURN_FALSE;
1574     }
1575     switch (ZEND_NUM_ARGS()) {
1576       default:
1577         WRONG_PARAM_COUNT;
1578         RETURN_FALSE; /* XXX */
1579       case 2:
1580         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
1581                                   &instance_name, &instance_name_len,
1582                                   &swf_data, &swf_data_len) == FAILURE) {
1583           RETURN_FALSE;
1584         }
1585       break;
1586       case 3:
1587         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssb",
1588                                   &instance_name, &instance_name_len,
1589                                   &swf_data, &swf_data_len, &unused_cid_purge) == FAILURE) {
1590             // unused_cid_purge ¤Ï̵»ë¤·¤Þ¤¹¡£
1591           RETURN_FALSE;
1592         }
1593         break;
1594     }
1595     swf = get_swf_object(getThis() TSRMLS_CC);    
1596     result = swf_object_replace_movieclip(swf,
1597                                           (unsigned char *)instance_name,
1598                                           instance_name_len,
1599                                           (unsigned char *)swf_data,
1600                                           swf_data_len);
1601     if (result) {
1602         RETURN_FALSE;
1603     }
1604     RETURN_TRUE;
1605 }
1606
1607 PHP_METHOD(swfed, rebuild) {
1608     swf_object_t *swf = get_swf_object(getThis() TSRMLS_CC);
1609     if (swf_object_rebuild(swf)) {
1610         RETURN_FALSE;
1611     }
1612     RETURN_TRUE;
1613 }
1614
1615 PHP_METHOD(swfed, purgeUselessContents) {
1616     swf_object_t *swf = get_swf_object(getThis() TSRMLS_CC);
1617     swf_object_purge_contents(swf);
1618     RETURN_TRUE;
1619 }
1620
1621 PHP_METHOD(swfed, setCompressLevel) {
1622     unsigned long compress_level = 6 ; // Z_DEFAULT_COMPRESSION
1623     swf_object_t *swf;
1624     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1625                               "l", &compress_level) == FAILURE) {
1626         RETURN_FALSE;
1627     }
1628     swf = get_swf_object(getThis() TSRMLS_CC);
1629     swf->compress_level = compress_level;
1630     RETURN_TRUE;
1631 }
1632
1633 PHP_METHOD(swfed, isShapeTagData) {
1634     char *data = NULL;
1635     int data_len = 0;
1636     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1637                               "s", &data, &data_len) == FAILURE) {
1638         RETURN_FALSE;
1639     }
1640     if (swf_object_is_shape_tagdata((unsigned char *)data,
1641                                     data_len) == 0) {
1642         RETURN_FALSE;
1643     }
1644     RETURN_TRUE;      
1645 }
1646
1647 PHP_METHOD(swfed, isBitmapTagData) {
1648     char *data = NULL;
1649     int data_len = 0;
1650     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
1651                               "s", &data, &data_len) == FAILURE) {
1652         RETURN_FALSE;
1653     }
1654     if (swf_object_is_bitmap_tagdata((unsigned char *)data,
1655                                      data_len) == 0) {
1656         RETURN_FALSE;
1657     }
1658     RETURN_TRUE;      
1659 }
1660
1661
1662 static swf_object_t  *get_swf_object(zval *obj TSRMLS_DC) {
1663 //    zval *data, **tmp;
1664     zval **tmp = NULL;
1665     swf_object_t *swf = NULL;
1666     int id = 0, type = 0;
1667 /* XXX: zend_read_property 
1668     data = zend_read_property(Z_OBJCE_P(obj), obj, "swf_object",
1669                                                           strlen("swf_object"), 1 TSRMLS_CC);
1670 */
1671     if (zend_hash_find(Z_OBJPROP_P(obj), "swfed", strlen("swfed") + 1,
1672                        (void **)&tmp) == FAILURE) {
1673         return NULL;
1674     }
1675     id = Z_LVAL_PP(tmp);
1676     swf = (swf_object_t *) zend_list_find(id, &type);
1677     return swf;
1678 }
1679
1680 static void free_swfed_resource(zend_rsrc_list_entry *resource TSRMLS_DC)
1681 {
1682 //    printf("SWFEditor->destory\n");
1683     swf_object_close((swf_object_t *) resource->ptr);
1684 }
1685