Generated from configure.in
[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             int id;
128             reader_init (newtag->data, newtag->length);
129             id = readu16(); //id
130             readRECT(); //bounding box
131             readMATRIX(); //matrix
132             resetbits();
133             glyphbits = readu8(); //glyphbits
134             advancebits = readu8(); //advancebits
135             while(1) {
136                 u16 flags;
137                 resetbits();
138                 flags = getbits(8);
139                 if(!flags) break;
140                 if(flags & 128) // text style record
141                 {
142                     resetbits();
143                     if(flags & 8) { // hasfont
144                         maponeid(getinputpos());
145                         id = readu16();
146                     }
147                     if(flags & 4) { // hascolor
148                         if(num==1) readRGB();
149                         else       readRGBA();
150                     }
151                     if(flags & 2) { //has x offset
152                         resetbits();
153                         readu16();
154                     }
155                     if(flags & 1) { //has y offset
156                         resetbits();
157                         readu16();
158                     }
159                     if(flags & 8) { //has height
160                         resetbits();
161                         readu16();
162                     }
163                 } else { // glyph record
164                     int t;
165                     resetbits();
166                     for(t=0;t<flags;t++) {
167                         getbits(glyphbits);
168                         getbits(advancebits);
169                     }
170                 }
171             }
172             break;
173         }
174         case TAGID_DEFINEFONTINFO:
175             maponeid(&newtag->data[0]);
176         break;
177
178         case TAGID_DEFINESHAPE3: // these thingies might have bitmap ids in their fillstyles
179         num++; //fallthrough
180         case TAGID_DEFINESHAPE2:
181         num++; //fallthrough
182         case TAGID_DEFINESHAPE: {
183             u16 count;
184             int t;
185             struct RECT r;
186             reader_init (newtag->data, newtag->length);
187             readu16(); // id;
188             r = readRECT(); // bounds
189 //          printf("%d shape bounds: %d %d %d %d\n",newtag->id,r.x1,r.y1,r.x2,r.y2);
190             resetbits();
191             count = readu8();
192             if(count == 0xff && num>1)
193                 count = readu16();
194 //          printf("%d fillstyles\n", count);
195             for(t=0;t<count;t++)
196             {
197                 int type;
198                 u8*pos;
199                 pos=getinputpos();
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);
204                 if(type == 0) {
205                     if(num == 3)
206                         readRGBA();
207                     else 
208                         readRGB();
209                 }
210                 if(type == 0x10 || type == 0x12)
211                 {
212                     readMATRIX();
213                     resetbits();
214                     readGRADIENT(num);
215                 }
216                 if(type == 0x40 || type == 0x41)
217                 {
218                     resetbits();
219                     // we made it.
220                     if(*(u16*)getinputpos() != 65535)
221                         maponeid(getinputpos());
222
223                     readu16();
224                     readMATRIX();
225                 }
226                 //...
227             }
228         }
229         break;
230         default:
231         break;
232     }
233 }
234
235 static int*bitmap;
236
237 static int get_free_id()
238 {
239     int t;
240     for (t=1;t<65536;t++)
241     {
242         if(bitmap[t] == -1)
243         {
244             bitmap[t] = 1;
245             return t;
246         }
247     }
248     return -1;
249 }
250
251 void swf_relocate (u8*data, int length, int*_bitmap)
252 {
253     int pos;
254     bitmap = _bitmap;
255     read_swf(&file, data, length);
256     memset(slaveids, -1, sizeof(slaveids));
257
258     pos = 0;
259     while(file.tags[pos].id != 0) {
260         struct swf_tag*tag = &file.tags[pos];
261         
262         map_ids(&file.tags[pos]);
263
264         if(is_defining_tag(tag->id))
265         {
266             int newid;
267             int id;
268             
269             id = getidfromtag(tag); //own id
270
271             if(bitmap[id] < 0) { //free
272                 newid = id;
273             }
274             else {
275                 newid = get_free_id(id);
276             }
277             bitmap[newid] = 1;
278             slaveids[id] = newid;
279
280             logf("<debug> sprite id %d mapped to %d",id, newid);
281             
282             setidintag(tag, newid);
283
284             logf("<debug> [sprite defs] write tag %02x (%d bytes in body)", 
285                     tag->id, tag->length);
286         } 
287         pos++;
288     }
289 }
290