7f61ede22df060646b1d34c8bac6d9963cb75f26
[swftools.git] / lib / python / tag.c
1 #include <Python.h>
2 #undef HAVE_STAT
3 #include "../rfxswf.h"
4 #include "../log.h"
5 #include "./pyutils.h"
6 #include "primitives.h"
7 #include "action.h"
8 #include "tag.h"
9 #include "tagmap.h"
10
11 //----------------------------------------------------------------------------
12
13 typedef struct _TagObject {
14     PyObject_HEAD
15     tag_internals_t internals;
16 } TagObject;
17
18 //----------------------------------------------------------------------------
19 static PyMethodDef generic_methods[] = 
20 {
21   {NULL, NULL, 0, NULL}
22 };
23 static tag_internals_t generic_tag =
24 {
25     parse: 0,
26     dealloc: 0,
27     fillTAG: 0,
28     tagfunctions: generic_methods,
29     datasize: 0,
30 };
31 //----------------------------------------------------------------------------
32
33 static struct tag_parser {
34     int id;
35     tag_internals_t*spec;
36     struct tag_parser* next;
37 } tag_parsers[1024];
38 static char parsers_initialized = 0;
39
40 void register_tag(int id, tag_internals_t*spec)
41 {
42     assert(id>=0 && id<1024);
43     if(!parsers_initialized) {
44         memset(tag_parsers, 0, sizeof(tag_parsers));
45         parsers_initialized = 1;
46     }
47     tag_parsers[id].id = id;
48     tag_parsers[id].spec = spec;
49 };
50
51 static tag_internals_t* get_parser(int id)
52 {
53     if(parsers_initialized<2) {
54         int t;
55         struct tag_parser*last = &tag_parsers[0];
56         for(t=0;t<1024;t++) {
57             if(tag_parsers[t].spec) {
58                 last->next = &tag_parsers[t];
59                 last = &tag_parsers[t];
60             }
61         }
62         parsers_initialized = 2;
63     }
64     assert(id>=0 && id<1024);
65     return tag_parsers[id].spec;
66 }
67
68 //----------------------------------------------------------------------------
69 static void tag_dealloc(PyObject * self)
70 {
71     TagObject*tag = (TagObject*)self;
72     if(tag->internals.tag)
73         mylog("-%08x(%d) tag_dealoc [%s]\n", (int)self, self->ob_refcnt, swf_TagGetName(tag->internals.tag));
74     else
75         mylog("-%08x(%d) tag_dealoc [?]\n", (int)self, self->ob_refcnt);
76     if(tag->internals.dealloc) {
77         if(!tag->internals.data)
78             mylog("-%08x(%d) tag_dealoc: Warning: calling dealloc without any data(?)\n", (int)self, self->ob_refcnt);
79         tag->internals.dealloc(&tag->internals);
80     }
81     if(tag->internals.data) {
82         free(tag->internals.data);
83         tag->internals.data = 0;
84     }
85     if(tag->internals.tag) {
86         swf_DeleteTag(tag->internals.tag);
87         tag->internals.tag = 0;
88     }
89     Py_DECREF(tag->internals.tagmap);
90     tag->internals.tagmap = 0;
91     PyObject_Del(self);
92 }
93 //----------------------------------------------------------------------------
94 static PyObject* tag_setU8(PyObject * self, PyObject*other)
95 {
96     return NULL;
97 }
98 //----------------------------------------------------------------------------
99 static PyMethodDef common_tagfunctions[] =
100 {{"setU8", tag_setU8, METH_VARARGS, "sets a byte to the tag data"},
101  {NULL, NULL, 0, NULL}
102 };
103
104 static int fillTAG(PyObject*self) 
105 {
106     TagObject*tag = (TagObject*)self;
107     if(tag->internals.tag)
108         return 1;
109     if(!tag->internals.fillTAG) {
110         PyErr_SetString(PyExc_Exception, setError("No way to fill TAG with data"));
111         return 0;
112     }
113     if(!tag->internals.fillTAG(&tag->internals)) {
114         return 0; // pass through exception
115     }
116     if(!tag->internals.tag) {
117         PyErr_SetString(PyExc_Exception, setError("Couldn't fill tag"));
118         return 0;
119     }
120     return 1;
121 }
122 static PyObject* tag_getattr(PyObject * self, char* a)
123 {
124     TagObject*tag = (TagObject*)self;
125     PyObject* ret = NULL;
126     int t;
127
128     /* -- fields -- */
129     if(!strcmp(a, "tagid")) {
130         if(!fillTAG(self))
131             return 0;
132         return Py_BuildValue("i", tag->internals.tag->id);
133     }
134     if(!strcmp(a, "name")) {
135         if(!fillTAG(self))
136             return 0;
137         char* name = swf_TagGetName(tag->internals.tag);
138         return Py_BuildValue("s", name);
139     }
140     if(tag->internals.getattr) {
141         PyObject* ret = tag->internals.getattr(&tag->internals, a);
142         if(ret)
143             return ret;
144     }
145     
146     /* search for a tag specific function */
147     if(tag->internals.tagfunctions) {
148         mylog(" %08x(%d) tag_getattr: tag has specific functions\n", (int)self, self->ob_refcnt);
149         ret = Py_FindMethod(tag->internals.tagfunctions, self, a);
150         if(!ret) return ret;
151         ret = FindMethodMore(ret, common_tagfunctions, self, a);
152         mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
153         return ret;
154     }
155    
156     ret = Py_FindMethod(common_tagfunctions, self, a);
157
158     mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
159     return ret;
160 }
161 static int tag_setattr(PyObject * self, char* a, PyObject * o)
162 {
163     TagObject*tag = (TagObject*)self;
164     if(tag->internals.setattr) {
165         int ret = tag->internals.setattr(&tag->internals, a, o);
166         return ret;
167     }
168     return 1;
169 }
170 //----------------------------------------------------------------------------
171 //                     Tag Constructors
172 //----------------------------------------------------------------------------
173 PyObject* tag_new(tag_internals_t*tag_internals)
174 {
175     TagObject*tag = PyObject_New(TagObject, &TagClass);
176     mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt);
177     memcpy(&tag->internals, tag_internals, sizeof(tag_internals_t));
178     if(tag->internals.datasize) {
179         tag->internals.data = malloc(tag->internals.datasize);
180         memset(tag->internals.data , 0, tag->internals.datasize);
181     } else {
182         tag->internals.data = 0;
183     }
184     tag->internals.tag = 0;
185     tag->internals.tagmap = tagmap_new();
186
187     return (PyObject*)tag;
188 }
189 PyObject* tag_new2(TAG*t, PyObject* tagmap)
190 {
191     TagObject*tag = PyObject_New(TagObject, &TagClass);
192     mylog("+%08x(%d) tag_new2 tag=%08x id=%d (%s)\n", (int)tag, tag->ob_refcnt, t, t->id, swf_TagGetName(t));
193     
194     PyObject*mytagmap = tagmap_new();
195
196     int num = swf_GetNumUsedIDs(t);
197     if(num) { // tag has dependencies
198         int * positions = malloc(num*sizeof(int));
199         swf_GetUsedIDs(t, positions);
200         int i;
201         for(i=0;i<num;i++) {
202             int id = GET16(&t->data[positions[i]]);
203             PyObject*obj = tagmap_id2obj(tagmap, id);
204             if(obj==NULL) {
205                 PyErr_SetString(PyExc_Exception, setError("TagID %d not defined", id));
206                 return NULL;
207             }
208             //mylog("+%08x(%d) tag_new2 handling id %d at %d/%d\n", (int)tag, tag->ob_refcnt, id, positions[i], t->len);
209             //mylog("+%08x(%d) tag_new2 add dependency %d to id %d, object %08x(%d)\n", (int)tag, tag->ob_refcnt, i, id, obj, obj->ob_refcnt);
210             tagmap_addMapping(mytagmap, id, obj);
211         }
212         free(positions);
213     }
214
215     tag_internals_t*spec = get_parser(t->id);
216     if(spec) {
217         memcpy(&tag->internals, spec, sizeof(tag_internals_t));
218     } else {
219         memcpy(&tag->internals, &generic_tag, sizeof(tag_internals_t));
220     }
221     if(tag->internals.datasize) {
222         tag->internals.data = malloc(tag->internals.datasize);
223         memset(tag->internals.data, 0, tag->internals.datasize);
224     } else {
225         tag->internals.data = 0;
226     }
227     tag->internals.tag = swf_InsertTag(0, t->id);
228     swf_SetBlock(tag->internals.tag, t->data, t->len);
229     tag->internals.tagmap = mytagmap;
230
231     // call tag->internals.init()?
232
233     return (PyObject*)tag;
234 }
235 //----------------------------------------------------------------------------
236 /* serialize */
237 TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
238 {
239     TagObject*tag = (TagObject*)self;
240
241     if(!fillTAG(self))
242         return 0;
243     mylog(" %08x(%d) tag_getTAG: tag=%08x id=%d (%s)", (int)self, self->ob_refcnt, tag->internals.tag, tag->internals.tag->id, swf_TagGetName(tag->internals.tag));
244
245     TAG* t = swf_InsertTag(prevTag, tag->internals.tag->id);
246     swf_SetBlock(t, tag->internals.tag->data, tag->internals.tag->len);
247     
248     if(swf_isDefiningTag(t)) {
249         int newid = tagmap_add(tagmap, self);
250         swf_SetDefineID(t, newid);
251     }
252
253     int num = swf_GetNumUsedIDs(t);
254     if(num) { // tag has dependencies
255         int * positions = malloc(num*sizeof(int));
256         swf_GetUsedIDs(t, positions);
257         int i;
258         for(i=0;i<num;i++) {
259             int id = GET16(&t->data[positions[i]]);
260             PyObject* obj =  tagmap_id2obj(tag->internals.tagmap, id);
261             if(obj==NULL) {
262                 PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:"));
263                 free(positions);
264                 return 0;
265             }
266             //int newid = tagmap_obj2id(tag->internals.tagmap, obj);
267             int newid = tagmap_obj2id(tagmap, obj);
268             if(newid>=0) {
269                 mylog(" %08x(%d) tag_getTAG: dependency %d) %d->%08x -> assigning(%08x) id %d", (int)self, self->ob_refcnt, i, id, obj, tagmap, newid);
270             } else {
271                 /* TODO: this is only needed for sprites, so maybe it should throw an
272                    exception otherwise */
273                 newid = tagmap_add(tagmap, obj);
274                 mylog(" %08x(%d) tag_getTAG: added dependency %d) %d->%08x -> assigning(%08x) id %d", (int)self, self->ob_refcnt, i, id, obj, tagmap, newid);
275             }
276             PUT16(&t->data[positions[i]], newid);
277         }
278         free(positions);
279     }
280     return t;
281 }
282 //----------------------------------------------------------------------------
283 tag_internals_t* tag_getinternals(PyObject*self)
284 {
285     TagObject*tag = (TagObject*)self;
286     mylog(" %08x(%d) tag_getInternals\n", (int)self, self->ob_refcnt);
287     return &tag->internals;
288 }
289 //----------------------------------------------------------------------------
290 PyObject* tag_getDependencies(PyObject*self)
291 {
292     TagObject*tag = (TagObject*)self;
293     mylog(" %08x(%d) tag_getDependencies\n", (int)self, self->ob_refcnt);
294     return tagmap_getObjectList(tag->internals.tagmap);
295 }
296 //----------------------------------------------------------------------------
297 int tag_print(PyObject * self, FILE * fi, int flags)
298 {
299     TagObject*tag = (TagObject*)self;
300     mylog(" %08x(%d) tag_print flags=%08x\n", (int)self, self->ob_refcnt, flags);
301     if(!fillTAG(self))
302         return -1;
303         fprintf(fi, "tag-%08x-%d-%s", (int)tag->internals.tag, tag->internals.tag->id, swf_TagGetName(tag->internals.tag));
304     return 0;
305 }
306 //----------------------------------------------------------------------------
307 PyTypeObject TagClass = 
308 {
309     PyObject_HEAD_INIT(NULL)
310     0,
311     tp_name: "Tag",
312     tp_basicsize: sizeof(TagObject),
313     tp_itemsize: 0,
314     tp_dealloc: tag_dealloc,
315     tp_print: tag_print,
316     tp_getattr: tag_getattr,
317     tp_setattr: tag_setattr,
318 };