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;
127 reader_init (newtag->data, newtag->length);
129 readRECT(); //bounding box
130 readMATRIX(); //matrix
132 glyphbits = readu8(); //glyphbits
133 advancebits = readu8(); //advancebits
135 u16 flags = getbits(8);
136 printf("define text flags: %02x\n", flags);
138 if(flags & 128) // text style record
140 if(flags & 8) { // hasfont
141 maponeid(getinputpos());
145 if(flags & 4) { // hascolor
146 if(num==1) readRGB();
149 if(flags & 2) { //has x offset
153 if(flags & 1) { //has y offset
157 if(flags & 8) { //has height
161 } else { // glyph record
163 getbits(advancebits);
169 case TAGID_DEFINEFONTINFO:
170 maponeid(&newtag->data[0]);
173 case TAGID_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
175 case TAGID_DEFINESHAPE2:
177 case TAGID_DEFINESHAPE: {
181 reader_init (newtag->data, newtag->length);
183 r = readRECT(); // bounds
184 // printf("%d shape bounds: %d %d %d %d\n",newtag->id,r.x1,r.y1,r.x2,r.y2);
187 if(count == 0xff && num>1)
189 // printf("%d fillstyles\n", count);
195 // printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
196 // pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);
197 type = readu8(); //type
198 // printf("fillstyle %d is type 0x%02x\n", t, type);
205 if(type == 0x10 || type == 0x12)
211 if(type == 0x40 || type == 0x41)
215 if(*(u16*)getinputpos() != 65535)
216 maponeid(getinputpos());
232 static int get_free_id()
235 for (t=1;t<65536;t++)
246 void swf_relocate (u8*data, int length, int*_bitmap)
250 read_swf(&file, data, length);
251 memset(slaveids, -1, sizeof(slaveids));
254 while(file.tags[pos].id != 0) {
255 struct swf_tag*tag = &file.tags[pos];
257 map_ids(&file.tags[pos]);
259 if(is_defining_tag(tag->id))
264 id = getidfromtag(tag); //own id
266 if(bitmap[id] < 0) { //free
270 newid = get_free_id(id);
273 slaveids[id] = newid;
275 logf("<debug> sprite id %d mapped to %d",id, newid);
277 setidintag(tag, newid);
279 logf("<debug> [sprite defs] write tag %02x (%d bytes in body)",
280 tag->id, tag->length);