2 Implements swf_relocate(), which changes the id range of a swf file in
5 Part of the swftools package.
7 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
9 This file is distributed under the GPL, see file COPYING for details */
13 static struct swffile file;
18 void map_ids_mem(u8*mem, int length);
19 static struct swf_tag* map_ids(struct swf_tag*tag)
21 map_ids_mem(tag->fulldata, tag->fulllength);
25 void maponeid(void*idpos)
27 u16*idptr = (u16*)idpos;
28 if(slaveids[*idptr]<0) {
29 logf("<error> Trying to map id never encountered before: id=%d", *idptr);
32 logf("<debug> mapping %d to %d", *idptr, slaveids[*idptr]);
33 *idptr = slaveids[*idptr];
37 // take a memory region which contains a tag, and
38 // map the ids inside this tag to new values
39 void map_ids_mem(u8*mem, int length)
42 struct swf_tag newtag_instance;
43 struct swf_tag*newtag = &newtag_instance;
44 reader_init (mem, length);
49 case TAGID_PLACEOBJECT:
50 maponeid(&newtag->data[0]);
52 case TAGID_PLACEOBJECT2:
53 // only if placeflaghascharacter
54 if(!(newtag->data[0]&2))
56 maponeid(&newtag->data[3]);
58 case TAGID_REMOVEOBJECT:
59 maponeid(&newtag->data[0]);
61 case TAGID_STARTSOUND:
62 maponeid(&newtag->data[0]);
64 case TAGID_DEFINESPRITE: {
65 u8*mem = &newtag->data[4];
66 int len = newtag->length-4;
71 struct swf_tag sprtag;
73 reader_init (mem, len);
74 swf_read_tag (&sprtag);
77 len = getinputlength();
79 if(sprtag.id == TAGID_END)
82 map_ids_mem (fmem,flen);
86 case TAGID_DEFINEBUTTON2: // has some font ids in the button records
89 case TAGID_DEFINEBUTTON:
90 reader_init (newtag->data, newtag->length);
91 readu16(); //button id
100 if(!readu8()) //flags
102 charid = *(u16*)getinputpos();
103 maponeid(getinputpos());
111 case TAGID_DEFINEEDITTEXT: {
113 reader_init (newtag->data, newtag->length);
115 readRECT(); //bounding box
120 maponeid(getinputpos());
123 case TAGID_DEFINETEXT2:
125 case TAGID_DEFINETEXT: {
126 int glyphbits, advancebits;
128 reader_init (newtag->data, newtag->length);
130 readRECT(); //bounding box
131 readMATRIX(); //matrix
133 glyphbits = readu8(); //glyphbits
134 advancebits = readu8(); //advancebits
140 if(flags & 128) // text style record
143 if(flags & 8) { // hasfont
144 maponeid(getinputpos());
147 if(flags & 4) { // hascolor
148 if(num==1) readRGB();
151 if(flags & 2) { //has x offset
155 if(flags & 1) { //has y offset
159 if(flags & 8) { //has height
163 } else { // glyph record
166 for(t=0;t<flags;t++) {
168 getbits(advancebits);
174 case TAGID_DEFINEFONTINFO:
175 maponeid(&newtag->data[0]);
178 case TAGID_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
180 case TAGID_DEFINESHAPE2:
182 case TAGID_DEFINESHAPE: {
186 reader_init (newtag->data, newtag->length);
188 r = readRECT(); // bounds
189 // printf("%d shape bounds: %d %d %d %d\n",newtag->id,r.x1,r.y1,r.x2,r.y2);
192 if(count == 0xff && num>1)
194 // printf("%d fillstyles\n", count);
200 // printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
201 // pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);
202 type = readu8(); //type
203 // printf("fillstyle %d is type 0x%02x\n", t, type);
210 if(type == 0x10 || type == 0x12)
216 if(type == 0x40 || type == 0x41)
220 if(*(u16*)getinputpos() != 65535)
221 maponeid(getinputpos());
237 static int get_free_id()
240 for (t=1;t<65536;t++)
251 void swf_relocate (u8*data, int length, int*_bitmap)
255 read_swf(&file, data, length);
256 memset(slaveids, -1, sizeof(slaveids));
259 while(file.tags[pos].id != 0) {
260 struct swf_tag*tag = &file.tags[pos];
262 map_ids(&file.tags[pos]);
264 if(is_defining_tag(tag->id))
269 id = getidfromtag(tag); //own id
271 if(bitmap[id] < 0) { //free
275 newid = get_free_id(id);
278 slaveids[id] = newid;
280 logf("<debug> sprite id %d mapped to %d",id, newid);
282 setidintag(tag, newid);
284 logf("<debug> [sprite defs] write tag %02x (%d bytes in body)",
285 tag->id, tag->length);