added jpeg3 extraction support to swfextract
[swftools.git] / lib / as3 / import.c
1 /* import.c
2
3    Extension module for the rfxswf library.
4    Part of the swftools package.
5
6    Copyright (c) 2009 Matthias Kramm <kramm@quiss.org>
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include "import.h"
23 #include "abc.h"
24 #include "registry.h"
25 #include "common.h"
26 #include "tokenizer.h"
27 #include "assets.h"
28 #include "../os.h"
29 #include "../xml.h"
30 #ifdef HAVE_ZZIP
31 #include "zzip/lib.h"
32 #endif
33
34 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*a);
35
36 void as3_import_abc(char*filename)
37 {
38     TAG*tag = swf_InsertTag(0, ST_RAWABC);
39     memfile_t*file = memfile_open(filename);
40     tag->data = file->data;
41     tag->len = file->len;
42     abc_file_t*abc = swf_ReadABC(tag);
43     import_code(abc, filename, 0, 0);
44     import_code(abc, filename, 1, 0);
45     swf_FreeABC(abc);
46     memfile_close(file);
47     free(tag);
48 }
49
50 typedef struct _deps {
51     const char*name;
52     struct _deps*next;
53 } deps_t;
54
55 void as3_import_swf2(reader_t*r, char*filename, dict_t*deps)
56 {
57     SWF _swf,*swf=&_swf;
58     if(swf_ReadSWF2(r, &_swf)<0)
59         return;
60     swf_FoldAll(swf);
61
62     TAG*tag = swf->firstTag;
63
64     asset_resolver_t* assets =  swf_ParseAssets(swf);
65
66     asset_bundle_list_t* asset_bundles = list_new();
67
68     dict_t*name2bundle = dict_new();
69     /* pass 1 */
70     while(tag) {
71         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
72             abc_file_t*abc = swf_ReadABC(tag);
73             import_code(abc, filename, 0, 0);
74             NEW(asset_bundle_t, a);
75             a->file = abc;
76             if(abc->name) {
77                 dict_put(name2bundle, abc->name, a);
78             }
79             list_append(asset_bundles, a);
80         }
81         tag = tag->next;
82     }
83
84     tag = swf->firstTag;
85     asset_bundle_list_t*b = asset_bundles;
86     /* pass 2 */
87     while(tag) {
88         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
89             asset_bundle_t*a = b->asset_bundle;
90             abc_file_t*abc = a->file;
91             swf_ResolveAssets(assets, abc);
92             if(deps && abc->name) {
93                 deps_t*d = dict_lookup(deps, abc->name);
94                 while(d) {
95                     if(d->name) {
96                         asset_bundle_t*other = dict_lookup(name2bundle, d->name);
97                         list_append(a->dependencies, other);
98                     }
99                     d = d->next;
100                 }
101             }
102             registry_add_asset(a);
103             import_code(abc, filename, 1, a);
104             b=b->next;
105         }
106         tag = tag->next;
107     }
108
109     dict_destroy(name2bundle);
110     list_free(asset_bundles);
111
112     //swf_FreeTags(swf); // FIXME: mem leak
113 }
114
115 void as3_import_swf(char*filename)
116 {
117     reader_t reader;
118     reader_init_filereader2(&reader, filename);
119     as3_import_swf2(&reader, filename, 0);
120     reader.dealloc(&reader);
121 }
122
123 #ifdef HAVE_ZZIP
124 typedef struct _catalog_state {
125     char*xml_filename;
126     char in_libraries;
127     char*library;
128     char*script;
129     dict_t*deps;
130     deps_t*current_deps;
131     dict_t*name2deps;
132     dict_t*id2script;
133     ZZIP_DIR*dir;
134 } catalog_state_t;
135
136 const char* fa(catalog_state_t*state, xmlattribute_t*attr, const char*name)
137 {
138     while(attr) {
139         if(!strcmp(attr->name, name)) return attr->value;
140         attr = attr->next;
141     }
142     syntaxerror("error parsing %s: attribute %s missing", state->xml_filename, name);
143 }
144
145 void catalog_start_tag(xmlconsumer_t*c, char*name, xmlattribute_t*attr)
146 {
147     catalog_state_t*state = (catalog_state_t*)c->internal;
148     if(!strcmp(name, "libraries")) {
149         state->in_libraries = 1;
150     } else if(!strcmp(name, "library")) {
151         state->library = strdup(fa(state, attr, "path"));
152     } else if(!strcmp(name, "script")) {
153         state->script = strdup(fa(state, attr, "name"));
154     } else if(!strcmp(name, "def")) {
155         dict_put(state->id2script, strdup(fa(state, attr, "id")), strdup(state->script));
156     } else if(!strcmp(name, "dep")) {
157         NEW(deps_t,d);
158         d->name = strdup(fa(state, attr, "id"));
159         d->next = state->current_deps;
160         state->current_deps = d;
161     }
162 }
163 void catalog_data(xmlconsumer_t*c, char*data, int len)
164 {
165     catalog_state_t*state = (catalog_state_t*)c->internal;
166 }
167 void catalog_end_tag(xmlconsumer_t*c, char*name)
168 {
169     catalog_state_t*state = (catalog_state_t*)c->internal;
170     if(!strcmp(name, "libraries")) {
171         state->in_libraries = 0;
172     } else if(!strcmp(name, "library")) {
173         ZZIP_FILE*file = zzip_file_open(state->dir, state->library, 0);
174
175         DICT_ITERATE_DATA(state->deps,deps_t*,deps) {
176             while(deps) {
177                 char*script = dict_lookup(state->id2script, deps->name);
178                 if(!script) {
179                     //as3_warning("when importing %s: depencency %s referenced in catalog.xml, but not found.", state->xml_filename, deps->name);
180                 }
181                 deps->name = script;
182                 deps = deps->next;
183             }
184         }
185
186         if(!file) {
187             as3_warning("when importing %s: %s referenced in catalog.xml, but not found.", state->xml_filename, state->library);
188         } else {
189             reader_t r;
190             reader_init_zzipreader(&r, file);
191             as3_import_swf2(&r, state->library, state->deps);
192             r.dealloc(&r);
193             zzip_file_close(file);
194         }
195         dict_destroy(state->deps);
196         state->deps = 0;
197         free(state->library);
198         state->library = 0;
199     } else if(!strcmp(name, "script")) {
200         dict_put(state->deps, state->script, state->current_deps);
201         free(state->script);
202         state->current_deps = 0;
203         state->script = 0;
204     }
205 }
206 void as3_import_zipfile(char*filename)
207 {
208     ZZIP_DIR*dir = zzip_opendir(filename);
209     if(!dir) as3_error("Error reading %s\n", filename);
210     ZZIP_FILE*file = zzip_file_open(dir, "catalog.xml", 0);
211     reader_t r;
212     reader_init_zzipreader(&r, file);
213
214     xmlconsumer_t c;
215     catalog_state_t state;
216     memset(&state, 0, sizeof(state));
217     state.dir = dir;
218     state.xml_filename = filename;
219     state.name2deps = dict_new();
220     state.id2script = dict_new();
221     state.deps = dict_new();
222     c.start_tag = catalog_start_tag;
223     c.data= catalog_data;
224     c.end_tag = catalog_end_tag;
225     c.internal = &state;
226
227     xml_parse(&r, &c);
228
229     r.dealloc(&r);
230 }
231 #else
232 void as3_import_zipfile(char*filename)
233 {
234     as3_warning("No zipfile support compiled in- can't import %s\n", filename);
235 }
236 #endif
237
238 void as3_import_file(char*filename)
239 {
240     FILE*fi = fopen(filename, "rb");
241     if(!fi) return;
242     char head[3];
243     fread(head, 3, 1, fi);
244     fclose(fi);
245     if(!strncmp(head, "FWS", 3) ||
246        !strncmp(head, "CWS", 3)) {
247         as3_import_swf(filename);
248     } else if(!strncmp(head, "PK", 2)) {
249         as3_import_zipfile(filename);
250     } else {
251         as3_import_abc(filename);
252     }
253 }
254
255 static int compare_traits(const void*v1, const void*v2)
256 {
257     trait_t* x1 = *(trait_t**)v1;
258     trait_t* x2 = *(trait_t**)v2;
259     int i = strcmp(x1->name->ns->name, x2->name->ns->name);
260     if(i)
261         return i;
262     return strcmp(x1->name->name, x2->name->name);
263 }
264
265 static classinfo_t*resolve_class(char*filename, char*what, multiname_t*n)
266 {
267     if(!n) return 0;
268     if(!n->name[0]) return 0;
269     if(!strcmp(n->name, "void")) 
270         return &voidclass;
271
272     classinfo_t*c = 0;
273     if(n->ns && n->ns->name) {
274         c = (classinfo_t*)registry_find(n->ns->name, n->name);
275     } else if(n->namespace_set) {
276         namespace_list_t*s = n->namespace_set->namespaces;
277         while(s) {
278             c = (classinfo_t*)registry_find(s->namespace->name, n->name);
279             if(c)
280                 break;
281             s = s->next;
282         }
283     }
284
285     if(!c) {
286         as3_warning("import %s: couldn't resolve %s %s.%s", filename, what, n->ns->name, n->name);
287         return 0;
288     }
289     if(c->kind != INFOTYPE_CLASS)
290         as3_warning("import %s: %s %s resolves to something that's not a class", filename, what, n->name);
291     return c;
292 }
293
294 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*asset_bundle)
295 {
296     abc_file_t*abc = _abc;
297     int t;
298     if(pass==0) {
299         for(t=0;t<abc->classes->num;t++) {
300             abc_class_t*cls = array_getvalue(abc->classes, t);
301             U8 access = cls->classname->ns->access;
302             if(access==ACCESS_PRIVATE ||
303                access==ACCESS_PACKAGEINTERNAL)
304                 continue;
305             //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
306             //    continue;
307
308             const char*package = strdup(cls->classname->ns->name);
309             const char*name = strdup(cls->classname->name);
310
311             multiname_list_t*i=cls->interfaces;
312             classinfo_t*c = classinfo_register(access, package, name, list_length(i));
313             c->flags|=FLAG_BUILTIN;
314
315             if(cls->flags & CLASS_FINAL)
316                 c->flags |= FLAG_FINAL;
317             if(cls->flags & CLASS_INTERFACE)
318                 c->flags |= FLAG_INTERFACE;
319             if(!(cls->flags & CLASS_SEALED))
320                 c->flags |= FLAG_DYNAMIC;
321         }
322         return;
323     }
324     
325     for(t=0;t<abc->classes->num;t++) {
326         abc_class_t*cls = array_getvalue(abc->classes, t);
327         const char*package = strdup(cls->classname->ns->name);
328         const char*name = strdup(cls->classname->name);
329         classinfo_t*c = (classinfo_t*)registry_find(package, name);
330         if(!c) continue;
331
332         c->assets = asset_bundle;
333
334         int nr = 0;
335         multiname_list_t*i = cls->interfaces;
336         while(i) {
337             c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
338             i = i->next;
339         }
340         c->superclass = resolve_class(filename, "superclass", cls->superclass);
341       
342         trait_list_t*l=0;
343         char is_static = 0;
344         l = cls->traits;
345         if(!l) {
346             l = cls->static_traits;
347             is_static = 1;
348         }
349         dict_t*names = dict_new();
350         while(l) {
351             trait_t*trait = l->trait;
352             U8 access = trait->name->ns->access;
353
354             if(access==ACCESS_PRIVATE)
355                 goto cont;
356             const char*name = trait->name->name;
357             char* ns = access==ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
358         
359             if(registry_findmember(c, ns, name, 0, is_static))
360                 goto cont;
361
362             name = strdup(name);
363
364             memberinfo_t*s = 0;
365             if(trait->kind == TRAIT_METHOD) {
366                 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name, is_static);
367                 s->return_type = resolve_class(filename, "return type", trait->method->return_type);
368                 dict_put(names, name, 0);
369             } else if(trait->kind == TRAIT_SLOT) {
370                 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
371                 s->type = resolve_class(filename, "type", trait->type_name);
372                 dict_put(names, name, 0);
373             } else if(trait->kind == TRAIT_GETTER) {
374                 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
375                 s->type = resolve_class(filename, "type", trait->method->return_type);
376                 dict_put(names, name, 0);
377             } else if(trait->kind == TRAIT_CONST) {
378                 /* some variables (e.g. XML.length) are apparently both a method and a slot.
379                    needs split of static/non-static first */
380                 if(!dict_contains(names, name)) {
381                     varinfo_t*v = (varinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
382                     v->type = resolve_class(filename, "type", trait->type_name);
383                     v->flags |= FLAG_CONST;
384                     /* leave this alone for now- it blows up the file too much 
385                     v->value = constant_clone(trait->value);*/
386                     s = (memberinfo_t*)v;
387                     dict_put(names, name, 0);
388                 } else 
389                     goto cont;
390
391             } else {
392                 goto cont;
393             }
394
395             s->flags = is_static?FLAG_STATIC:0;
396             s->flags |= FLAG_BUILTIN;
397             s->parent = c;
398
399             cont:
400             l = l->next;
401             if(!l && !is_static) {
402                 l = cls->static_traits;
403                 is_static = 1;
404             }
405         }
406         dict_destroy(names);
407     }
408
409 #   define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
410
411     /* count public functions */
412     int num_methods=0;
413     for(t=0;t<abc->scripts->num;t++) {
414         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
415         for(;l;l=l->next) {
416             num_methods += IS_PUBLIC_MEMBER(l->trait);
417         }
418     }
419     trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
420     num_methods=0;
421     for(t=0;t<abc->scripts->num;t++) {
422         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
423         for(;l;l=l->next) {
424             if(IS_PUBLIC_MEMBER(l->trait)) {
425                 traits[num_methods++] = l->trait;
426             }
427         }
428     }
429     qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
430     for(t=0;t<num_methods;t++) {
431         trait_t*trait = traits[t];
432         if(IS_PUBLIC_MEMBER(trait)) {
433             U8 access = trait->name->ns->access;
434             const char*package = strdup(trait->name->ns->name);
435             const char*name = strdup(trait->name->name);
436             char np = 0;
437             memberinfo_t*m = 0;
438             if(trait->kind == TRAIT_METHOD) {
439                 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
440                 m->return_type = resolve_class(filename, "return type", trait->method->return_type);
441             } else {
442                 varinfo_t*v = varinfo_register_global(access, package, name);
443                 v->type = resolve_class(filename, "type", trait->type_name);
444                 v->value = constant_clone(trait->value);
445                 v->flags |= trait->kind==TRAIT_CONST?FLAG_CONST:0;
446                 m = (memberinfo_t*)v;
447             }
448             m->flags |= FLAG_BUILTIN;
449             m->parent = 0;
450         }
451     }
452 }
453
454 void as3_import_code(void*_abc)
455 {
456     import_code(_abc, "", 0, 0);
457     import_code(_abc, "", 1, 0);
458 }