From 55a2f40a8ec668388dde27a681014319a203a7a2 Mon Sep 17 00:00:00 2001 From: yoya Date: Thu, 3 Feb 2011 14:06:39 +0000 Subject: [PATCH] =?utf8?q?-=20(get|replace)ShapeData=20=E3=81=AE=E5=AE=9F?= =?utf8?q?=E8=A3=85=20-=20(get|replace)TagContentsByCID=20=E3=81=AE?= =?utf8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/swfed/trunk@360 7c90b180-03d5-4157-b861-58a559ae9d1e --- src/php_swfed.c | 74 +++++++++++++++++++++++---------- src/php_swfed.h | 2 + src/swf_object.c | 124 ++++++++++++++++++++++++++++++++++++++++++++----------- src/swf_object.h | 6 +++ 4 files changed, 161 insertions(+), 45 deletions(-) diff --git a/src/php_swfed.c b/src/php_swfed.c index b26b6ff..ad9211a 100644 --- a/src/php_swfed.c +++ b/src/php_swfed.c @@ -63,8 +63,8 @@ zend_function_entry swfed_functions[] = { PHP_ME(swfed, replaceTagData, NULL, 0) PHP_ME(swfed, getTagContentsByCID, NULL, 0) PHP_ME(swfed, replaceTagContentsByCID, NULL, 0) - PHP_MALIAS(swfed, getShapeData, getTagContentsByCID, NULL, 0) - PHP_MALIAS(swfed, replaceShapeData, replaceTagContentsByCID, NULL, 0) + PHP_ME(swfed, getShapeData, NULL, 0) + PHP_ME(swfed, replaceShapeData, NULL, 0) PHP_ME(swfed, setShapeAdjustMode, NULL, 0) PHP_ME(swfed, getShapeIdListByBitmapRef, NULL, 0) @@ -503,7 +503,6 @@ PHP_METHOD(swfed, getTagData) { long tag_seqno = 0; swf_object_t *swf = NULL; unsigned char *data_ref = NULL; - char *new_buff = NULL; unsigned long data_len = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag_seqno) == FAILURE) { @@ -515,13 +514,7 @@ PHP_METHOD(swfed, getTagData) { fprintf(stderr, "getTagData: Can't get_tagdata\n"); RETURN_FALSE; } - new_buff = emalloc(data_len); - if (new_buff == NULL) { - fprintf(stderr, "getTagData: Can't emalloc new_buff\n"); - RETURN_FALSE; - } - memcpy(new_buff, data_ref, data_len); - RETURN_STRINGL(new_buff, data_len, 0); + RETURN_STRINGL(data_ref, data_len, 1); } PHP_METHOD(swfed, replaceTagData) { @@ -542,8 +535,7 @@ PHP_METHOD(swfed, replaceTagData) { } swf = get_swf_object(getThis() TSRMLS_CC); result = swf_object_replace_tagdata(swf, tag_seqno, - (unsigned char *)data, - & data_len); + (unsigned char *)data, data_len); if (result) { RETURN_FALSE; } @@ -556,23 +548,16 @@ PHP_METHOD(swfed, getTagContentsByCID) { unsigned char *data_ref = NULL; char *new_buff = NULL; unsigned long data_len = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &cid) == FAILURE) { RETURN_FALSE; } swf = get_swf_object(getThis() TSRMLS_CC); data_ref = swf_object_get_tagcontents_bycid(swf, cid, &data_len); if (data_ref == NULL) { - fprintf(stderr, "getTagContentsByCID: Can't get_tagdata\n"); - RETURN_FALSE; - } - new_buff = emalloc(data_len); - if (new_buff == NULL) { - fprintf(stderr, "getTagContentsByCID: Can't emalloc new_buff\n"); + fprintf(stderr, "getTagContentsByCID: Can't get_tagcontents_bycid\n"); RETURN_FALSE; } - memcpy(new_buff, data_ref, data_len); - RETURN_STRINGL(new_buff, data_len, 0); + RETURN_STRINGL(data_ref, data_len, 1); } PHP_METHOD(swfed, replaceTagContentsByCID) { @@ -594,7 +579,52 @@ PHP_METHOD(swfed, replaceTagContentsByCID) { swf = get_swf_object(getThis() TSRMLS_CC); result = swf_object_replace_tagcontents_bycid(swf, cid, (unsigned char *)data, - & data_len); + data_len); + if (result) { + RETURN_FALSE; + } + RETURN_TRUE; +} + + +PHP_METHOD(swfed, getShapeData) { + long cid = 0; + swf_object_t *swf = NULL; + unsigned char *data_ref = NULL; + char *new_buff = NULL; + unsigned long data_len = 0; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &cid) == FAILURE) { + RETURN_FALSE; + } + swf = get_swf_object(getThis() TSRMLS_CC); + data_ref = swf_object_get_shapedata(swf, cid, &data_len); + if (data_ref == NULL) { + fprintf(stderr, "getShapeData: Can't get_tagcontents_bycid\n"); + RETURN_FALSE; + } + RETURN_STRINGL(data_ref, data_len, 1); +} + +PHP_METHOD(swfed, replaceShapeData) { + char *data = NULL; + unsigned long data_len = 0; + long cid = 0; + swf_object_t *swf = NULL; + int result = 0; + switch (ZEND_NUM_ARGS()) { + default: + WRONG_PARAM_COUNT; + RETURN_FALSE; /* XXX */ + case 2: + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &cid, &data, &data_len) == FAILURE) { + RETURN_FALSE; + } + break; + } + swf = get_swf_object(getThis() TSRMLS_CC); + result = swf_object_replace_shapedata(swf, cid, + (unsigned char *)data, + data_len); if (result) { RETURN_FALSE; } diff --git a/src/php_swfed.h b/src/php_swfed.h index d3e0f10..9d84ea5 100644 --- a/src/php_swfed.h +++ b/src/php_swfed.h @@ -58,6 +58,8 @@ PHP_METHOD(swfed, replaceTagData); PHP_METHOD(swfed, getTagContentsByCID); PHP_METHOD(swfed, replaceTagContentsByCID); // +PHP_METHOD(swfed, getShapeData); +PHP_METHOD(swfed, replaceShapeData); PHP_METHOD(swfed, setShapeAdjustMode); PHP_METHOD(swfed, getShapeIdListByBitmapRef); PHP_METHOD(swfed, getBitmapSize); diff --git a/src/swf_object.c b/src/swf_object.c index 9e79368..f9ca68a 100644 --- a/src/swf_object.c +++ b/src/swf_object.c @@ -210,18 +210,20 @@ swf_object_get_tagdata(swf_object_t *swf, int tag_seqno, tag = tag->next; } if (tag) { - if (tag->data) { - *length = tag->length; - return tag->data; - } if (tag->detail) { bitstream_t *bs; - unsigned char *data; + if (tag->data) { + free(tag->data); + tag->data = NULL; + } bs = bitstream_open(); swf_tag_build(bs, tag, swf); - data = bitstream_steal(bs, length); + tag->data = bitstream_steal(bs, &(tag->length)); bitstream_close(bs); - return data; + } + if (tag->data) { + *length = tag->length; + return tag->data; } } return NULL; @@ -257,26 +259,30 @@ swf_object_get_tagcontents_bycid(swf_object_t *swf, int cid, swf_tag_t *tag; tag = swf->tag; while (tag) { - if (swf_tag_identity(tag, cid)) { - break; + if (swf_tag_identity(tag, cid) == 0) { + break; // match } + tag = tag->next; } if (tag) { - if (tag->data) { - *length = tag->length - 2; - return tag->data + 2; - } if (tag->detail) { bitstream_t *bs; - unsigned char *data; + if (tag->data) { + free(tag->data); + tag->data = NULL; + } bs = bitstream_open(); swf_tag_build(bs, tag, swf); - data = bitstream_steal(bs, length); + tag->data = bitstream_steal(bs, &(tag->length)); bitstream_close(bs); - *length -= 2; - return data + 2; + } + if (tag->data) { + unsigned char *data; + *length = tag->length - 2; + return tag->data + 2; } } + *length = 0; return NULL; } @@ -287,25 +293,97 @@ swf_object_replace_tagcontents_bycid(swf_object_t *swf, int cid, swf_tag_t *tag; tag = swf->tag; while (tag) { - if (swf_tag_identity(tag, cid)) { - break; + if (swf_tag_identity(tag, cid) == 0) { + break; // match } + tag = tag->next; } if (tag) { + if (tag->detail) { + swf_tag_destroy(tag); + } if (tag->data) { free(tag->data); + tag->data = NULL; } - if (tag->detail) { // FIXME + tag->length = length + 2; + tag->data = malloc(length + 2); + tag->data[0] = cid & 0xff; + tag->data[1] = cid >> 8; + memcpy(tag->data + 2, data, length); + return 0; // success + } + return 1; // failure +} + + +unsigned char * +swf_object_get_shapedata(swf_object_t *swf, int cid, unsigned long *length) { + swf_tag_t *tag; + tag = swf->tag; + while (tag) { + if (swf_tag_identity(tag, cid) == 0) { + break; // match + } + tag = tag->next; + } + if (tag) { + if (! isShapeTag(tag->tag)) { + fprintf(stderr, ""); + return NULL; + } + if (tag->detail) { + bitstream_t *bs; + if (tag->data) { + free(tag->data); + tag->data = NULL; + } + bs = bitstream_open(); + swf_tag_build(bs, tag, swf); + tag->data = bitstream_steal(bs, &(tag->length)); + bitstream_close(bs); + } + if (tag->data) { + unsigned char *data; + *length = tag->length - 2; + return tag->data + 2; + } + } + *length = 0; + return NULL; +} + +int +swf_object_replace_shapedata(swf_object_t *swf, int cid, + unsigned char *data, + unsigned long length) { + swf_tag_t *tag; + tag = swf->tag; + while (tag) { + if (swf_tag_identity(tag, cid) == 0) { + break; // match + } + tag = tag->next; + } + if (tag) { + if (! isShapeTag(tag->tag)) { + return 1; // failure + } + if (tag->detail) { swf_tag_destroy(tag); } + if (tag->data) { + free(tag->data); + tag->data = NULL; + } tag->length = length + 2; tag->data = malloc(length + 2); tag->data[0] = cid & 0xff; tag->data[1] = cid >> 8; - memcpy(tag->data, data + 2, length); - return 0; + memcpy(tag->data + 2, data, length); + return 0; // success } - return 1; + return 1; // failure } /* --- */ diff --git a/src/swf_object.h b/src/swf_object.h index 4fe1759..73bd3b1 100644 --- a/src/swf_object.h +++ b/src/swf_object.h @@ -51,6 +51,12 @@ extern swf_tag_t *swf_object_search_bitmap_tag(swf_object_t *swf, /* --- */ +extern unsigned char *swf_object_get_shapedata(swf_object_t *swf, + int shape_id, + unsigned long *length); +extern int swf_object_replace_shapedata(swf_object_t *swf, int shape_id, + unsigned char *data, + unsigned long length); extern int swf_object_set_shape_adjust_mode(swf_object_t *swf, unsigned mode); extern int swf_object_adjust_shapebitmap(swf_object_t *swf, int bitmap_id, int old_width, int old_height, -- 2.11.0