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());
112 case TAGID_DEFINEEDITTEXT: {
114 reader_init (newtag->data, newtag->length);
116 readRECT(); //bounding box
121 maponeid(getinputpos());
124 case TAGID_DEFINETEXT2:
126 case TAGID_DEFINETEXT: {
127 int glyphbits, advancebits;
129 reader_init (newtag->data, newtag->length);
131 readRECT(); //bounding box
132 readMATRIX(); //matrix
134 glyphbits = readu8(); //glyphbits
135 advancebits = readu8(); //advancebits
141 if(flags & 128) // text style record
144 if(flags & 8) { // hasfont
145 maponeid(getinputpos());
148 if(flags & 4) { // hascolor
149 if(num==1) readRGB();
152 if(flags & 2) { //has x offset
156 if(flags & 1) { //has y offset
160 if(flags & 8) { //has height
164 } else { // glyph record
167 for(t=0;t<flags;t++) {
169 getbits(advancebits);
175 case TAGID_DEFINEFONTINFO:
176 maponeid(&newtag->data[0]);
179 case TAGID_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
181 case TAGID_DEFINESHAPE2:
183 case TAGID_DEFINESHAPE: {
187 reader_init (newtag->data, newtag->length);
189 r = readRECT(); // bounds
190 // printf("%d shape bounds: %d %d %d %d\n",newtag->id,r.x1,r.y1,r.x2,r.y2);
193 if(count == 0xff && num>1) // defineshape2,3 only
195 // printf("%d fillstyles\n", count);
201 // printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
202 // pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);
204 type = readu8(); //type
205 // printf("fillstyle %d is type 0x%02x\n", t, type);
212 if(type == 0x10 || type == 0x12)
218 if(type == 0x40 || type == 0x41)
222 if(*(u16*)getinputpos() != 65535)
223 maponeid(getinputpos());
239 static int get_free_id()
242 for (t=1;t<65536;t++)
253 void swf_relocate (u8*data, int length, int*_bitmap)
257 read_swf(&file, data, length);
258 memset(slaveids, -1, sizeof(slaveids));
261 while(file.tags[pos].id != 0) {
262 struct swf_tag*tag = &file.tags[pos];
264 logf("<debug> relocator: processing tag %02x", tag->id);
265 map_ids(&file.tags[pos]);
267 if(is_defining_tag(tag->id))
272 id = getidfromtag(tag); //own id
274 if(bitmap[id] < 0) { //free
278 newid = get_free_id(id);
281 slaveids[id] = newid;
283 logf("<debug> relocator: id %d mapped to %d",id, newid);
285 setidintag(tag, newid);
287 logf("<debug> [reloc] write tag %02x (%d bytes in body)",
288 tag->id, tag->length);