From 84245258e84d865fb1b5a0a6a781c099946398df Mon Sep 17 00:00:00 2001
From: kramm <kramm>
Date: Wed, 31 Mar 2004 18:38:06 +0000
Subject: [PATCH] reworked tag memory allocation

---
 lib/python/SWF.c     |   13 ++++++++++---
 lib/python/tag.c     |   45 ++++++++++++++++++++-------------------------
 lib/python/taglist.c |   23 +++++++++++++++--------
 lib/python/taglist.h |    3 +++
 4 files changed, 48 insertions(+), 36 deletions(-)

diff --git a/lib/python/SWF.c b/lib/python/SWF.c
index d8ff006..fd7c423 100644
--- a/lib/python/SWF.c
+++ b/lib/python/SWF.c
@@ -128,6 +128,8 @@ static PyObject* f_load(PyObject* self, PyObject* args)
     if(swf->taglist == NULL) {
 	return NULL;
     }
+    
+    swf_FreeTags(&swf->swf);
     swf->swf.firstTag = 0;
     
     return (PyObject*)swf;
@@ -160,6 +162,8 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
 	swf->compressed = 1;
     
     swf->firstTag = taglist_getTAGs(swfo->taglist);
+    if(!swf->firstTag)
+	return NULL;
 
     // fix the file, in case it is empty or not terminated properly
     {
@@ -167,7 +171,6 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
 	if(!tag)
 	    tag = swf->firstTag = swf_InsertTag(0,ST_END);
 	while(tag && tag->next) {
-	    mylog(" tag:%08x\n", tag);
 	    tag = tag->next;
 	}
 	if(tag->id != ST_END) {
@@ -194,8 +197,10 @@ static PyObject * swf_save(PyObject* self, PyObject* args, PyObject* kwargs)
 	    }
     }
     close(fi);
-    
-    swf->firstTag = 0;
+
+    /* TODO: why is this segfaulting?? */
+   /* swf_FreeTags(swf);
+    swf->firstTag = 0;*/
     
     return PY_NONE;
 }
@@ -206,6 +211,8 @@ static PyObject * swf_writeCGI(PyObject* self, PyObject* args)
     if(!self || !PyArg_ParseTuple(args,"")) 
 	return NULL;
     swf->swf.firstTag = taglist_getTAGs(swf->taglist);
+    if(!swf->swf.firstTag)
+	return NULL;
     swf_WriteCGI(&swf->swf);
     swf->swf.firstTag = 0;
     return PY_NONE;
diff --git a/lib/python/tag.c b/lib/python/tag.c
index 8ab17ca..afc7b11 100644
--- a/lib/python/tag.c
+++ b/lib/python/tag.c
@@ -143,8 +143,11 @@ PyObject* tag_new2(TAG*t, PyObject* tagmap)
     tag->font = 0;
     tag->character = 0;
     tag->placeobject = 0;
-    tag->tag = t;
     tag->tagmap = tagmap_new();
+    // copy tag
+    tag->tag = swf_InsertTag(0, t->id);
+    swf_SetBlock(tag->tag, t->data, t->len);
+    t = tag->tag;
     
     int num = swf_GetNumUsedIDs(t);
     int * positions = malloc(num*sizeof(int));
@@ -159,6 +162,7 @@ PyObject* tag_new2(TAG*t, PyObject* tagmap)
 	}
 	tagmap_addMapping(tag->tagmap, id, obj);
     }
+    free(positions);
     return (PyObject*)tag;
 }
 
@@ -325,22 +329,20 @@ static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
     return (PyObject*)tag;
 }
 
-TAG* tag_getRAWTAG(PyObject*self)
-{
-    TagObject*tag = (TagObject*)self;
-    return tag->tag;
-}
-
 /* serialize */
 TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
 {
     TagObject*tag = (TagObject*)self;
-    TAG* t = tag_getRAWTAG(self);
-    mylog(" %08x(%d) tag_getTAG tagmap=%08x tag=%08x\n", (int)self, self->ob_refcnt, tagmap, t);
-    t->next = 0;
-    t->prev = prevTag;
-    if(prevTag)
-	prevTag->next = t;
+    
+    TAG* t = swf_InsertTag(prevTag, tag->tag->id);
+    swf_SetBlock(t, tag->tag->data, tag->tag->len);
+
+    mylog(" %08x(%d) tag_getTAG tagmap=%08x tag=%08x/%08x\n", (int)self, self->ob_refcnt, tagmap, tag->tag, t);
+
+    if(swf_isDefiningTag(t)) {
+	int newid = tagmap_add(tagmap, self);
+	swf_SetDefineID(t, newid);
+    }
 
     int num = swf_GetNumUsedIDs(t);
     int * positions = malloc(num*sizeof(int));
@@ -349,18 +351,11 @@ TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
     for(i=0;i<num;i++) {
 	int id = GET16(&t->data[positions[i]]);
 	PyObject* obj =  tagmap_id2obj(tag->tagmap, id);
-	mylog(" %08x(%d) tag_getTAG: id %d is %08x\n", (int)tag, tag->ob_refcnt, id, obj);
-	assert(obj!=NULL);
-	TAG*othertag = tag_getRAWTAG(obj);
-	int newid = tagmap_add(tagmap, obj);
-	mylog(" %08x(%d) tag_getTAG: othertag->tagid=%d, new ID: %d\n", (int)tag, tag->ob_refcnt, othertag->id, newid);
-
-	/* here comes the big hack- we define the *other* tags define ID.
-	   This assumes that the other tag is not yet written or processed,
-	   and we are, apart from the calling taglist, the only ones who know
-	   about it.  */
-	swf_SetDefineID(othertag, newid);
-
+	if(obj==NULL) {
+	    PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:"));
+	    return 0;
+	}
+	int newid = tagmap_obj2id(tagmap, obj);
 	PUT16(&t->data[positions[i]], newid);
     }
     return t;
diff --git a/lib/python/taglist.c b/lib/python/taglist.c
index e16f84a..a5d28d1 100644
--- a/lib/python/taglist.c
+++ b/lib/python/taglist.c
@@ -13,14 +13,12 @@
 typedef struct {
     PyObject_HEAD
     PyObject* taglist;
-    PyObject* tagmap;
 } TagListObject;
 //----------------------------------------------------------------------------
 PyObject * taglist_new()
 {
     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
     mylog("+%08x(%d) taglist_new", (int)taglist, taglist->ob_refcnt);
-    taglist->tagmap = tagmap_new();
     taglist->taglist = PyList_New(0);
     return (PyObject*)taglist;
 }
@@ -29,7 +27,9 @@ PyObject * taglist_new2(TAG*tag)
 {
     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
     mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
-    taglist->tagmap = tagmap_new();
+    PyObject* tagmap = tagmap_new();
+
+    swf_FoldAllTags(tag);
 
     int nr=0;
     TAG*t = tag;
@@ -48,24 +48,27 @@ PyObject * taglist_new2(TAG*tag)
     nr = 0;
     t = tag;
     while(t) {
-	PyObject*newtag = tag_new2(t, taglist->tagmap);
+	PyObject*newtag = tag_new2(t, tagmap);
 	if(newtag==NULL) {
 	    // pass through exception
+	    Py_DECREF(tagmap);
 	    return NULL;
 	}
 	PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
 	if(swf_isDefiningTag(t)) {
 	    int id = swf_GetDefineID(t);
-	    tagmap_addMapping(taglist->tagmap, id, newtag);
+	    tagmap_addMapping(tagmap, id, newtag);
 	}
 	nr++;
 	t=t->next;
     }
+    Py_DECREF(tagmap);
     return (PyObject*)taglist;
 }
 //----------------------------------------------------------------------------
 TAG* taglist_getTAGs(PyObject*self)
 {
+    PyObject* tagmap = tagmap_new();
     if(!PY_CHECK_TYPE(self,&TagListClass)) {
 	PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
 	return 0;
@@ -81,11 +84,17 @@ TAG* taglist_getTAGs(PyObject*self)
     mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
     for(t=0;t<l;t++) {
 	PyObject*item = PyList_GetItem(taglist->taglist, t);
-	tag = tag_getTAG(item, tag, taglist->tagmap);
+	tag = tag_getTAG(item, tag, tagmap);
+	if(!tag) {
+	    //pass through errors
+	    Py_DECREF(tagmap);
+	    return 0;
+	}
 	if(!firstTag)
 	    firstTag = tag;
 	mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
     }
+    Py_DECREF(tagmap);
     return firstTag;
 }
 //----------------------------------------------------------------------------
@@ -137,8 +146,6 @@ static void taglist_dealloc(PyObject* self)
     mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
     Py_DECREF(taglist->taglist);
     taglist->taglist = 0;
-    Py_DECREF(taglist->tagmap);
-    taglist->tagmap= 0;
     PyObject_Del(self);
 }
 //----------------------------------------------------------------------------
diff --git a/lib/python/taglist.h b/lib/python/taglist.h
index 5112964..f111e96 100644
--- a/lib/python/taglist.h
+++ b/lib/python/taglist.h
@@ -30,7 +30,10 @@
 extern PyTypeObject TagListClass;
 
 PyObject * taglist_new();
+
+/* warning: will modify tag order*/
 PyObject * taglist_new2(TAG*tag);
+
 TAG* taglist_getTAGs(PyObject*);
 
 PyMethodDef* taglist_getMethods();
-- 
1.7.10.4