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_DEFINEBUTTONCXFORM: {
51 maponeid(&newtag->data[0]); //button id
52 reader_init (newtag->data, newtag->length);
55 maponeid(&newtag->data[0]);
59 readu32(); // in point
61 readu32(); // out points
63 readu16(); // loop count
66 int npoints = readu8();
68 for(s=0;s<npoints;s++)
77 case TAGID_DEFINEBUTTONSOUND:
78 maponeid(&newtag->data[0]); //button id
80 case TAGID_PLACEOBJECT:
81 maponeid(&newtag->data[0]);
83 case TAGID_PLACEOBJECT2:
84 // only if placeflaghascharacter
85 if(!(newtag->data[0]&2))
87 maponeid(&newtag->data[3]);
89 case TAGID_REMOVEOBJECT:
90 maponeid(&newtag->data[0]);
92 case TAGID_STARTSOUND:
93 maponeid(&newtag->data[0]);
95 case TAGID_DEFINESPRITE: {
96 u8*mem = &newtag->data[4];
97 int len = newtag->length-4;
102 struct swf_tag sprtag;
104 reader_init (mem, len);
105 swf_read_tag (&sprtag);
108 len = getinputlength();
110 if(sprtag.id == TAGID_END)
113 map_ids_mem (fmem,flen);
117 case TAGID_DEFINEBUTTON2: // has some font ids in the button records
120 case TAGID_DEFINEBUTTON:
121 reader_init (newtag->data, newtag->length);
122 readu16(); //button id
131 if(!readu8()) //flags
133 charid = *(u16*)getinputpos();
134 maponeid(getinputpos());
143 case TAGID_DEFINEEDITTEXT: {
145 reader_init (newtag->data, newtag->length);
147 readRECT(); //bounding box
152 maponeid(getinputpos());
155 case TAGID_DEFINETEXT2:
157 case TAGID_DEFINETEXT: {
158 int glyphbits, advancebits;
160 reader_init (newtag->data, newtag->length);
162 readRECT(); //bounding box
164 readMATRIX(); //matrix
166 glyphbits = readu8(); //glyphbits
167 advancebits = readu8(); //advancebits
173 if(flags & 128) // text style record
176 if(flags & 8) { // hasfont
177 maponeid(getinputpos());
180 if(flags & 4) { // hascolor
181 if(num==1) readRGB();
184 if(flags & 2) { //has x offset
188 if(flags & 1) { //has y offset
192 if(flags & 8) { //has height
196 } else { // glyph record
199 for(t=0;t<flags;t++) {
201 getbits(advancebits);
207 case TAGID_DEFINEFONTINFO:
208 maponeid(&newtag->data[0]);
211 case TAGID_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
213 case TAGID_DEFINESHAPE2:
215 case TAGID_DEFINESHAPE: {
219 reader_init (newtag->data, newtag->length);
221 r = readRECT(); // bounds
222 // printf("%d shape bounds: %d %d %d %d\n",newtag->id,r.x1,r.y1,r.x2,r.y2);
225 if(count == 0xff && num>1) // defineshape2,3 only
227 // printf("%d fillstyles\n", count);
233 // printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
234 // pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);
236 type = readu8(); //type
237 // printf("fillstyle %d is type 0x%02x\n", t, type);
244 if(type == 0x10 || type == 0x12)
251 if(type == 0x40 || type == 0x41)
255 if(*(u16*)getinputpos() != 65535)
256 maponeid(getinputpos());
272 static int get_free_id()
275 for (t=1;t<65536;t++)
286 void swf_relocate (u8*data, int length, int*_bitmap)
290 read_swf(&file, data, length);
291 memset(slaveids, -1, sizeof(slaveids));
294 while(file.tags[pos].id != 0) {
295 struct swf_tag*tag = &file.tags[pos];
297 logf("<debug> relocator: processing tag %02x", tag->id);
298 map_ids(&file.tags[pos]);
300 if(is_defining_tag(tag->id))
305 id = getidfromtag(tag); //own id
307 if(bitmap[id] < 0) { //free
311 newid = get_free_id(id);
314 slaveids[id] = newid;
316 logf("<debug> relocator: id %d mapped to %d",id, newid);
318 setidintag(tag, newid);
320 logf("<debug> [reloc] write tag %02x (%d bytes in body)",
321 tag->id, tag->length);