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