f3243a322af2f40526f468d470452a8a3c5c6ebd
[swftools.git] / src / reloc.c
1 /* reloc.h
2    Implements swf_relocate(), which changes the id range of a swf file in
3    memory.
4
5    Part of the swftools package.
6    
7    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
8
9    This file is distributed under the GPL, see file COPYING for details */
10
11 #include "flash.h"
12
13 static struct swffile file;
14
15 int slaveids[65536];
16
17
18 void map_ids_mem(u8*mem, int length);
19 static struct swf_tag* map_ids(struct swf_tag*tag)
20 {
21     map_ids_mem(tag->fulldata, tag->fulllength);
22     return tag;
23 }
24
25 void maponeid(void*idpos)
26 {
27     u16*idptr = (u16*)idpos;
28     if(slaveids[*idptr]<0) {
29         logf("<error> Trying to map id never encountered before: id=%d", *idptr);
30         return ;
31     }
32     logf("<debug> mapping %d to %d", *idptr, slaveids[*idptr]);
33     *idptr =  slaveids[*idptr];
34 }
35
36
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)
40 {
41     int num=1;
42     struct swf_tag newtag_instance;
43     struct swf_tag*newtag = &newtag_instance;
44     reader_init (mem, length);
45     swf_read_tag(newtag);
46
47     switch(newtag->id)
48     {
49         case TAGID_PLACEOBJECT:
50             maponeid(&newtag->data[0]);
51         break;
52         case TAGID_PLACEOBJECT2:
53             // only if placeflaghascharacter
54             if(!(newtag->data[0]&2))
55                 break;
56             maponeid(&newtag->data[3]);
57         break;
58         case TAGID_REMOVEOBJECT:
59             maponeid(&newtag->data[0]);
60         break;
61         case TAGID_STARTSOUND:
62             maponeid(&newtag->data[0]);
63         break;
64         case TAGID_DEFINESPRITE: {
65             u8*mem = &newtag->data[4];
66             int len = newtag->length-4;
67
68             while(1) {
69                 u8*fmem = mem;
70                 int flen = len;
71                 struct swf_tag sprtag;
72
73                 reader_init (mem, len);
74                 swf_read_tag (&sprtag);
75
76                 mem = getinputpos();
77                 len = getinputlength();
78
79                 if(sprtag.id == TAGID_END)
80                     break;
81
82                 map_ids_mem (fmem,flen);
83             }
84         } 
85         break;
86         case TAGID_DEFINEBUTTON2: // has some font ids in the button records
87             num++; 
88         //fallthrough
89         case TAGID_DEFINEBUTTON:
90             reader_init (newtag->data, newtag->length);
91             readu16(); //button id
92             if(num>1)
93             { 
94                 readu8(); //flag
95                 readu16(); //offset
96             }
97             while(1)
98             {
99                 u16 charid;
100                 if(!readu8()) //flags
101                     break; 
102                 charid = *(u16*)getinputpos();
103                 maponeid(getinputpos());
104                 readu16(); //char
105                 readu16(); //layer
106                 readMATRIX();
107                 readCXFORM();
108             }
109             // ...
110         break;
111         case TAGID_DEFINEEDITTEXT:  {
112             u8 flags1,flags2;
113             reader_init (newtag->data, newtag->length);
114             readu16(); //id
115             readRECT(); //bounding box
116             resetbits();
117             flags1 = readu8();
118             flags2 = readu8();
119             if(flags1 & 128)
120                 maponeid(getinputpos());
121         }
122         break;
123         case TAGID_DEFINETEXT2:
124             num ++;
125         case TAGID_DEFINETEXT: { 
126             int glyphbits, advancebits;
127             reader_init (newtag->data, newtag->length);
128             readu16(); //id
129             readRECT(); //bounding box
130             readMATRIX(); //matrix
131             resetbits();
132             glyphbits = readu8(); //glyphbits
133             advancebits = readu8(); //advancebits
134             while(1) {
135                 u16 flags = getbits(8);
136                 if(!flags) break;
137                 if(flags & 128) // text style record
138                 {
139                     if(flags & 8) { // hasfont
140                         maponeid(getinputpos());
141                         resetbits();
142                         readu16();
143                     }
144                     if(flags & 4) { // hascolor
145                         if(num==1) readRGB();
146                         else       readRGBA();
147                     }
148                     if(flags & 2) { //has x offset
149                         resetbits();
150                         readu16();
151                     }
152                     if(flags & 1) { //has y offset
153                         resetbits();
154                         readu16();
155                     }
156                     if(flags & 8) { //has height
157                         resetbits();
158                         readu16();
159                     }
160                 } else { // glyph record
161                     getbits(glyphbits);
162                     getbits(advancebits);
163                     break;
164                 }
165             }
166             break;
167         }
168         case TAGID_DEFINEFONTINFO:
169             maponeid(&newtag->data[0]);
170         break;
171
172         case TAGID_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
173         num++; //fallthrough
174         case TAGID_DEFINESHAPE2:
175         num++; //fallthrough
176         case TAGID_DEFINESHAPE: {
177             u16 count;
178             int t;
179             struct RECT r;
180             reader_init (newtag->data, newtag->length);
181             readu16(); // id;
182             r = readRECT(); // bounds
183 //          printf("%d shape bounds: %d %d %d %d\n",newtag->id,r.x1,r.y1,r.x2,r.y2);
184             resetbits();
185             count = readu8();
186             if(count == 0xff && num>1)
187                 count = readu16();
188 //          printf("%d fillstyles\n", count);
189             for(t=0;t<count;t++)
190             {
191                 int type;
192                 u8*pos;
193                 pos=getinputpos();
194 //              printf("%02x %02x %02x %02x %02x %02x %02x %02x\n", 
195 //                      pos[0],pos[1],pos[2],pos[3],pos[4],pos[5],pos[6],pos[7]);
196                 type = readu8(); //type
197 //              printf("fillstyle %d is type 0x%02x\n", t, type);
198                 if(type == 0) {
199                     if(num == 3)
200                         readRGBA();
201                     else 
202                         readRGB();
203                 }
204                 if(type == 0x10 || type == 0x12)
205                 {
206                     readMATRIX();
207                     resetbits();
208                     readGRADIENT(num);
209                 }
210                 if(type == 0x40 || type == 0x41)
211                 {
212                     resetbits();
213                     // we made it.
214                     if(*(u16*)getinputpos() != 65535)
215                         maponeid(getinputpos());
216
217                     readu16();
218                     readMATRIX();
219                 }
220                 //...
221             }
222         }
223         break;
224         default:
225         break;
226     }
227 }
228
229 static int*bitmap;
230
231 static int get_free_id()
232 {
233     int t;
234     for (t=1;t<65536;t++)
235     {
236         if(bitmap[t] == -1)
237         {
238             bitmap[t] = 1;
239             return t;
240         }
241     }
242     return -1;
243 }
244
245 void swf_relocate (u8*data, int length, int*_bitmap)
246 {
247     int pos;
248     bitmap = _bitmap;
249     read_swf(&file, data, length);
250     memset(slaveids, -1, sizeof(slaveids));
251
252     pos = 0;
253     while(file.tags[pos].id != 0) {
254         struct swf_tag*tag = &file.tags[pos];
255         
256         map_ids(&file.tags[pos]);
257
258         if(is_defining_tag(tag->id))
259         {
260             int newid;
261             int id;
262             
263             id = getidfromtag(tag); //own id
264
265             if(bitmap[id] < 0) { //free
266                 newid = id;
267             }
268             else {
269                 newid = get_free_id(id);
270             }
271             bitmap[newid] = 1;
272             slaveids[id] = newid;
273
274             logf("<debug> sprite id %d mapped to %d",id, newid);
275             
276             setidintag(tag, newid);
277
278             logf("<debug> [sprite defs] write tag %02x (%d bytes in body)", 
279                     tag->id, tag->length);
280         } 
281         pos++;
282     }
283 }
284