[Swfdec] 4 commits - libswfdec-gtk/swfdec_gtk_loader.c libswfdec/swfdec_as_context.c libswfdec/swfdec_as_frame.c libswfdec/swfdec_as_frame_internal.h libswfdec/swfdec_as_interpret.c libswfdec/swfdec_loader.c libswfdec/swfdec_loader.h libswfdec/swfdec_load_object.c libswfdec/swfdec_net_stream.c

Benjamin Otte company at kemper.freedesktop.org
Sun Oct 28 11:04:43 PDT 2007


 libswfdec-gtk/swfdec_gtk_loader.c    |    3 -
 libswfdec/swfdec_as_context.c        |    2 -
 libswfdec/swfdec_as_frame.c          |   56 +++++++++++++++++++----------------
 libswfdec/swfdec_as_frame_internal.h |    4 --
 libswfdec/swfdec_as_interpret.c      |   45 +++++++---------------------
 libswfdec/swfdec_load_object.c       |   10 ++++--
 libswfdec/swfdec_loader.c            |   26 ++++++++--------
 libswfdec/swfdec_loader.h            |    4 +-
 libswfdec/swfdec_net_stream.c        |    8 ++---
 9 files changed, 73 insertions(+), 85 deletions(-)

New commits:
commit eb9d4887c22c7b50b86d57f14e7ca672ec329e90
Author: Benjamin Otte <otte at gnome.org>
Date:   Sun Oct 28 19:04:34 2007 +0100

    set 0 content-length, but not a too huge one

diff --git a/libswfdec-gtk/swfdec_gtk_loader.c b/libswfdec-gtk/swfdec_gtk_loader.c
index f61a999..fb93cf9 100644
--- a/libswfdec-gtk/swfdec_gtk_loader.c
+++ b/libswfdec-gtk/swfdec_gtk_loader.c
@@ -112,8 +112,7 @@ swfdec_gtk_loader_headers (SoupMessage *msg, gpointer loader)
 
   errno = 0;
   l = strtoul (s, &end, 10);
-  // FIXME: need a way to allow 0-size files
-  if (errno == 0 && *end == 0 && l > 0)
+  if (errno == 0 && *end == 0 && l <= G_MAXLONG)
     swfdec_loader_set_size (loader, l);
 }
 
commit 4a6886684a3f3c99f303d678425646ba99f2f8fd
Author: Benjamin Otte <otte at gnome.org>
Date:   Sun Oct 28 19:04:12 2007 +0100

    fix docs

diff --git a/libswfdec/swfdec_loader.c b/libswfdec/swfdec_loader.c
index e79ba1e..ca84656 100644
--- a/libswfdec/swfdec_loader.c
+++ b/libswfdec/swfdec_loader.c
@@ -540,15 +540,15 @@ swfdec_loader_set_size (SwfdecLoader *loader, gulong size)
  * swfdec_loader_get_size:
  * @loader: a #SwfdecLoader
  *
- * Queries the amount of bytes inside @loader. If the size is unknown, 0 is 
- * returned.
+ * Queries the amount of bytes inside @loader. If the size is unknown, -1 is 
+ * returned. Otherwise the number is greater or equal to 0.
  *
- * Returns: the total number of bytes for this loader or 0 if unknown
+ * Returns: the total number of bytes for this loader or -1 if unknown
  **/
 glong
 swfdec_loader_get_size (SwfdecLoader *loader)
 {
-  g_return_val_if_fail (SWFDEC_IS_LOADER (loader), 0);
+  g_return_val_if_fail (SWFDEC_IS_LOADER (loader), -1);
 
   return loader->size;
 }
commit 931ba99893d1f223068cc188e498d82100387ddb
Author: Benjamin Otte <otte at gnome.org>
Date:   Sun Oct 28 19:00:31 2007 +0100

    allow 0 as the size of a loader

diff --git a/libswfdec/swfdec_as_interpret.c b/libswfdec/swfdec_as_interpret.c
index 3defeca..7125e05 100644
--- a/libswfdec/swfdec_as_interpret.c
+++ b/libswfdec/swfdec_as_interpret.c
@@ -2542,7 +2542,6 @@ swfdec_action_throw (SwfdecAsContext *cx, guint action, const guint8 *data,
 }
 
 typedef struct {
-  int			ref_count;
   const guint8 *	catch_start;
   const guint8 *	finally_start;
   guint			catch_size;
@@ -2556,25 +2555,12 @@ typedef struct {
 } TryData;
 
 static void
-swfdec_action_try_data_ref (gpointer data)
+swfdec_action_try_data_free (gpointer data)
 {
   TryData *try_data = data;
 
   g_return_if_fail (try_data != NULL);
 
-  try_data->ref_count++;
-}
-
-static void
-swfdec_action_try_data_unref (gpointer data)
-{
-  TryData *try_data = data;
-
-  g_return_if_fail (try_data != NULL);
-
-  if (--try_data->ref_count > 0)
-    return;
-
   if (!try_data->use_register)
     g_free (try_data->variable_name);
   g_free (try_data);
@@ -2595,7 +2581,7 @@ swfdec_action_try_end_finally (SwfdecAsFrame *frame, gpointer data)
   if (!cx->exception)
     swfdec_as_context_throw (cx, exception_value);
 
-  swfdec_as_frame_pop_block (frame);
+  g_free (data);
 }
 
 static void
@@ -2610,9 +2596,6 @@ swfdec_action_try_end_catch (SwfdecAsFrame *frame, gpointer data)
 
   cx = SWFDEC_AS_OBJECT (frame)->context;
 
-  swfdec_action_try_data_ref (try_data);
-  swfdec_as_frame_pop_block (frame);
-
   if (swfdec_as_context_catch (cx, &val))
   {
     // we got an exception while in catch block:
@@ -2625,10 +2608,10 @@ swfdec_action_try_end_catch (SwfdecAsFrame *frame, gpointer data)
     // FIXME: the exception value is not marked while finally block runs
     swfdec_as_frame_push_block (frame, try_data->finally_start,
 	try_data->finally_start + try_data->finally_size,
-	swfdec_action_try_end_finally, exception_value, g_free);
+	swfdec_action_try_end_finally, exception_value);
   }
 
-  swfdec_action_try_data_unref (try_data);
+  swfdec_action_try_data_free (try_data);
 }
 
 static void
@@ -2650,9 +2633,6 @@ swfdec_action_try_end_try (SwfdecAsFrame *frame, gpointer data)
 
   cx = SWFDEC_AS_OBJECT (frame)->context;
 
-  swfdec_action_try_data_ref (try_data);
-  swfdec_as_frame_pop_block (frame);
-
   if (swfdec_as_context_catch (cx, &val))
   {
     // we got an exception while in try block:
@@ -2695,13 +2675,14 @@ swfdec_action_try_end_try (SwfdecAsFrame *frame, gpointer data)
       }
     }
 
-    swfdec_action_try_data_ref (try_data);
     swfdec_as_frame_push_block (frame, try_data->catch_start,
 	try_data->catch_start + try_data->catch_size,
-	swfdec_action_try_end_catch, try_data, swfdec_action_try_data_unref);
+	swfdec_action_try_end_catch, try_data);
+  } 
+  else 
+  {
+    swfdec_action_try_data_free (try_data);
   }
-
-  swfdec_action_try_data_unref (try_data);
 }
 
 static void
@@ -2720,7 +2701,6 @@ swfdec_action_try (SwfdecAsContext *cx, guint action, const guint8 *data, guint
   }
 
   try_data = g_malloc0 (sizeof (TryData));
-  swfdec_action_try_data_ref (try_data);
 
   swfdec_bits_init_data (&bits, data, len);
 
@@ -2750,10 +2730,10 @@ swfdec_action_try (SwfdecAsContext *cx, guint action, const guint8 *data, guint
 
   if (try_data->catch_start || try_data->finally_start) {
     swfdec_as_frame_push_block (cx->frame, data + len, data + len + try_size,
-	swfdec_action_try_end_try, try_data, swfdec_action_try_data_unref);
+	swfdec_action_try_end_try, try_data);
   } else {
     SWFDEC_ERROR ("Try without neither catch or finally block");
-    swfdec_action_try_data_unref (try_data);
+    swfdec_action_try_data_free (try_data);
   }
 }
 
@@ -2762,7 +2742,6 @@ swfdec_action_pop_with (SwfdecAsFrame *frame, gpointer with_object)
 {
   g_assert (frame->scope_chain->data == with_object);
   frame->scope_chain = g_slist_delete_link (frame->scope_chain, frame->scope_chain);
-  swfdec_as_frame_pop_block (frame);
 }
 
 static void
@@ -2784,7 +2763,7 @@ swfdec_action_with (SwfdecAsContext *cx, guint action, const guint8 *data, guint
   } else {
     cx->frame->scope_chain = g_slist_prepend (cx->frame->scope_chain, object);
     swfdec_as_frame_push_block (cx->frame, data + len, data + len + offset,
-	swfdec_action_pop_with, object, NULL);
+	swfdec_action_pop_with, object);
   }
   swfdec_as_stack_pop (cx);
 }
diff --git a/libswfdec/swfdec_load_object.c b/libswfdec/swfdec_load_object.c
index 09ac8c8..ebd6fdc 100644
--- a/libswfdec/swfdec_load_object.c
+++ b/libswfdec/swfdec_load_object.c
@@ -41,14 +41,18 @@ static void
 swfdec_load_object_loader_target_parse (SwfdecLoaderTarget *target,
     SwfdecLoader *loader)
 {
-  SwfdecAsValue val;
   SwfdecLoadObject *load_object = SWFDEC_LOAD_OBJECT (target);
+  SwfdecAsValue val;
+  glong size;
 
-  SWFDEC_AS_VALUE_SET_INT (&val, swfdec_loader_get_loaded (loader));
+  SWFDEC_AS_VALUE_SET_NUMBER (&val, swfdec_loader_get_loaded (loader));
   swfdec_as_object_set_variable_and_flags (load_object->target,
       SWFDEC_AS_STR__bytesLoaded, &val, SWFDEC_AS_VARIABLE_HIDDEN);
 
-  SWFDEC_AS_VALUE_SET_INT (&val, swfdec_loader_get_size (loader));
+  size = swfdec_loader_get_size (loader);
+  if (size < 0) 
+    size = swfdec_loader_get_loaded (loader);
+  SWFDEC_AS_VALUE_SET_NUMBER (&val, size);
   swfdec_as_object_set_variable_and_flags (load_object->target,
       SWFDEC_AS_STR__bytesTotal, &val, SWFDEC_AS_VARIABLE_HIDDEN);
 }
diff --git a/libswfdec/swfdec_loader.c b/libswfdec/swfdec_loader.c
index 9bde057..e79ba1e 100644
--- a/libswfdec/swfdec_loader.c
+++ b/libswfdec/swfdec_loader.c
@@ -112,7 +112,7 @@ swfdec_loader_get_property (GObject *object, guint param_id, GValue *value,
       g_value_set_enum (value, loader->data_type);
       break;
     case PROP_SIZE:
-      g_value_set_ulong (value, loader->size);
+      g_value_set_long (value, loader->size);
       break;
     case PROP_LOADED:
       g_value_set_ulong (value, swfdec_loader_get_loaded (loader));
@@ -137,8 +137,8 @@ swfdec_loader_set_property (GObject *object, guint param_id, const GValue *value
       swfdec_loader_error (loader, g_value_get_string (value));
       break;
     case PROP_SIZE:
-      if (loader->size == 0 && g_value_get_ulong (value) > 0)
-	swfdec_loader_set_size (loader, g_value_get_ulong (value));
+      if (loader->size == -1 && g_value_get_long (value) >= 0)
+	swfdec_loader_set_size (loader, g_value_get_long (value));
       break;
     case PROP_URL:
       loader->url = g_value_dup_boxed (value);
@@ -186,8 +186,8 @@ swfdec_loader_class_init (SwfdecLoaderClass *klass)
       g_param_spec_enum ("data-type", "data type", "the data's type as identified by Swfdec",
 	  SWFDEC_TYPE_LOADER_DATA_TYPE, SWFDEC_LOADER_DATA_UNKNOWN, G_PARAM_READABLE));
   g_object_class_install_property (object_class, PROP_SIZE,
-      g_param_spec_ulong ("size", "size", "amount of bytes in loader",
-	  0, G_MAXULONG, 0, G_PARAM_READWRITE));
+      g_param_spec_long ("size", "size", "amount of bytes in loader",
+	  -1, G_MAXLONG, -1, G_PARAM_READWRITE));
   g_object_class_install_property (object_class, PROP_LOADED,
       g_param_spec_ulong ("loaded", "loaded", "bytes already loaded",
 	  0, G_MAXULONG, 0, G_PARAM_READWRITE));
@@ -201,6 +201,8 @@ swfdec_loader_init (SwfdecLoader *loader)
 {
   loader->queue = swfdec_buffer_queue_new ();
   loader->data_type = SWFDEC_LOADER_DATA_UNKNOWN;
+
+  loader->size = -1;
 }
 
 /*** INTERNAL API ***/
@@ -527,8 +529,8 @@ void
 swfdec_loader_set_size (SwfdecLoader *loader, gulong size)
 {
   g_return_if_fail (SWFDEC_IS_LOADER (loader));
-  g_return_if_fail (loader->size == 0);
-  g_return_if_fail (size > 0);
+  g_return_if_fail (loader->size == -1);
+  g_return_if_fail (size <= G_MAXLONG);
 
   loader->size = size;
   g_object_notify (G_OBJECT (loader), "size");
@@ -543,7 +545,7 @@ swfdec_loader_set_size (SwfdecLoader *loader, gulong size)
  *
  * Returns: the total number of bytes for this loader or 0 if unknown
  **/
-gulong
+glong
 swfdec_loader_get_size (SwfdecLoader *loader)
 {
   g_return_val_if_fail (SWFDEC_IS_LOADER (loader), 0);
diff --git a/libswfdec/swfdec_loader.h b/libswfdec/swfdec_loader.h
index 61e0b31..614907a 100644
--- a/libswfdec/swfdec_loader.h
+++ b/libswfdec/swfdec_loader.h
@@ -58,7 +58,7 @@ struct _SwfdecLoader
   /*< private >*/
   guint			state;		/* SwfdecLoaderState the loader is currently in */
   SwfdecURL *		url;		/* the URL for this loader in UTF-8 - must be set on creation */
-  gulong		size;		/* number of bytes in stream or 0 if unknown */
+  glong			size;		/* number of bytes in stream or -1 if unknown */
   char *		error;		/* error message if in error state or NULL */
   gpointer		target;		/* SwfdecLoaderTarget that gets notified about loading progress */
   gpointer		player;		/* SwfdecPlayer belonging to target or %NULL */
@@ -93,7 +93,7 @@ const SwfdecURL *
 		swfdec_loader_get_url		(SwfdecLoader *		loader);
 void		swfdec_loader_set_size		(SwfdecLoader *		loader,
 						 gulong			size);
-gulong		swfdec_loader_get_size		(SwfdecLoader *		loader);
+glong		swfdec_loader_get_size		(SwfdecLoader *		loader);
 gulong		swfdec_loader_get_loaded	(SwfdecLoader *		loader);
 char *  	swfdec_loader_get_filename	(SwfdecLoader *		loader);
 SwfdecLoaderDataType
diff --git a/libswfdec/swfdec_net_stream.c b/libswfdec/swfdec_net_stream.c
index c401125..fa119ba 100644
--- a/libswfdec/swfdec_net_stream.c
+++ b/libswfdec/swfdec_net_stream.c
@@ -412,19 +412,19 @@ swfdec_net_stream_get_variable (SwfdecAsObject *object, SwfdecAsObject *orig,
     if (stream->loader == NULL)
       SWFDEC_AS_VALUE_SET_INT (val, 0);
     else
-      SWFDEC_AS_VALUE_SET_INT (val, swfdec_loader_get_loaded (stream->loader));
+      SWFDEC_AS_VALUE_SET_NUMBER (val, swfdec_loader_get_loaded (stream->loader));
     *flags = 0;
     return TRUE;
   } else if (variable == SWFDEC_AS_STR_bytesTotal) {
-    guint bytes;
+    glong bytes;
     if (stream->loader == NULL) {
       bytes = 0;
     } else { 
       bytes = swfdec_loader_get_size (stream->loader);
-      if (bytes == 0)
+      if (bytes < 0)
 	bytes = swfdec_loader_get_loaded (stream->loader);
     }
-    SWFDEC_AS_VALUE_SET_INT (val, bytes);
+    SWFDEC_AS_VALUE_SET_NUMBER (val, bytes);
     *flags = 0;
     return TRUE;
   }
commit dfb094f0c03c2b2d59b0f38484c10165d9e3298e
Author: Benjamin Otte <otte at gnome.org>
Date:   Sun Oct 28 19:00:14 2007 +0100

    get rid of 2 ways to pop blocks.
    
    blocks are now popped automatically and whenever that has happened, the func
    registered with a block is called. It is free to push new blocks.

diff --git a/libswfdec/swfdec_as_context.c b/libswfdec/swfdec_as_context.c
index f83632f..54a1ac8 100644
--- a/libswfdec/swfdec_as_context.c
+++ b/libswfdec/swfdec_as_context.c
@@ -865,7 +865,7 @@ start:
     }
     if (check_block && (pc < frame->block_start || pc >= frame->block_end)) {
       SWFDEC_LOG ("code exited block");
-      swfdec_as_frame_check_block (frame);
+      swfdec_as_frame_pop_block (frame);
       pc = frame->pc;
       if (frame != context->frame)
 	goto start;
diff --git a/libswfdec/swfdec_as_frame.c b/libswfdec/swfdec_as_frame.c
index c902406..c101b9c 100644
--- a/libswfdec/swfdec_as_frame.c
+++ b/libswfdec/swfdec_as_frame.c
@@ -203,16 +203,28 @@ swfdec_as_stack_iterator_next (SwfdecAsStackIterator *iter)
 typedef struct {
   const guint8 *		start;	/* start of block */
   const guint8 *		end;	/* end of block (hitting this address will exit the block) */
-  SwfdecAsFrameBlockFunc	func;	/* function to call when block is exited */
+  SwfdecAsFrameBlockFunc	func;	/* function to call when block is exited (or frame is destroyed) */
   gpointer			data;	/* data to pass to function */
-  GDestroyNotify		destroy;/* destroy function called for data */
 } SwfdecAsFrameBlock;
 
+/**
+ * swfdec_as_frame_push_block:
+ * @frame: a #SwfdecAsFrame
+ * @start: start of block
+ * @end: byte after end of block
+ * @func: function to call when block gets exited
+ * @data: data to pass to @func
+ *
+ * Registers a function that guards a block of memory. When the function 
+ * exits this block of memory, it will call this function. This can happen
+ * either when the program counter leaves the guarded region, when the function
+ * returns or when the context aborted due to an unrecoverable error.
+ **/
 void
 swfdec_as_frame_push_block (SwfdecAsFrame *frame, const guint8 *start, 
-    const guint8 *end, SwfdecAsFrameBlockFunc func, gpointer data, GDestroyNotify destroy)
+    const guint8 *end, SwfdecAsFrameBlockFunc func, gpointer data)
 {
-  SwfdecAsFrameBlock block = { start, end, func, data, destroy };
+  SwfdecAsFrameBlock block = { start, end, func, data };
 
   g_return_if_fail (SWFDEC_IS_AS_FRAME (frame));
   g_return_if_fail (start <= end);
@@ -228,14 +240,16 @@ swfdec_as_frame_push_block (SwfdecAsFrame *frame, const guint8 *start,
 void
 swfdec_as_frame_pop_block (SwfdecAsFrame *frame)
 {
+  SwfdecAsFrameBlockFunc func;
+  gpointer data;
   SwfdecAsFrameBlock *block;
 
-  g_assert (frame->blocks->len > 0);
+  g_return_if_fail (SWFDEC_IS_AS_FRAME (frame));
+  g_return_if_fail (frame->blocks->len > 0);
 
   block = &g_array_index (frame->blocks, SwfdecAsFrameBlock, frame->blocks->len - 1);
-  if (block->destroy) {
-    block->destroy (block->data);
-  }
+  func = block->func;
+  data = block->data;
   g_array_set_size (frame->blocks, frame->blocks->len - 1);
   if (frame->blocks->len) {
     block--;
@@ -245,19 +259,8 @@ swfdec_as_frame_pop_block (SwfdecAsFrame *frame)
     frame->block_start = frame->script->buffer->data;
     frame->block_end = frame->script->buffer->data + frame->script->buffer->length;
   }
-}
-
-void
-swfdec_as_frame_check_block (SwfdecAsFrame *frame)
-{
-  SwfdecAsFrameBlock *block;
-
-  g_return_if_fail (SWFDEC_IS_AS_FRAME (frame));
-  if (frame->blocks->len == 0)
-    return;
-
-  block = &g_array_index (frame->blocks, SwfdecAsFrameBlock, frame->blocks->len - 1);
-  block->func (frame, block->data);
+  /* only call function after we popped the block, so the block can push a new one */
+  func (frame, data);
 }
 
 /*** FRAME ***/
@@ -269,6 +272,11 @@ swfdec_as_frame_dispose (GObject *object)
 {
   SwfdecAsFrame *frame = SWFDEC_AS_FRAME (object);
 
+  /* pop blocks while state is intact */
+  while (frame->blocks->len > 0)
+    swfdec_as_frame_pop_block (frame);
+
+  /* clean up */
   g_slice_free1 (sizeof (SwfdecAsValue) * frame->n_registers, frame->registers);
   if (frame->security) {
     g_object_unref (frame->security);
@@ -282,8 +290,6 @@ swfdec_as_frame_dispose (GObject *object)
     swfdec_buffer_unref (frame->constant_pool_buffer);
     frame->constant_pool_buffer = NULL;
   }
-  while (frame->blocks->len > 0)
-    swfdec_as_frame_pop_block (frame);
   g_array_free (frame->blocks, TRUE);
   g_slist_free (frame->scope_chain);
   if (frame->script) {
@@ -799,9 +805,9 @@ swfdec_as_frame_handle_exception (SwfdecAsFrame *frame)
 
   /* pop blocks in the hope that we are inside a Try block */
   while (cx->exception && frame->blocks->len) {
-    swfdec_as_frame_check_block (frame);
+    swfdec_as_frame_pop_block (frame);
   }
-  /* exit frame, nothing caught the exception */
+  /* no Try blocks caught it, exit frame */
   if (cx->exception) {
     swfdec_as_frame_return (frame, NULL);
   }
diff --git a/libswfdec/swfdec_as_frame_internal.h b/libswfdec/swfdec_as_frame_internal.h
index 1e2075d..d50a9ce 100644
--- a/libswfdec/swfdec_as_frame_internal.h
+++ b/libswfdec/swfdec_as_frame_internal.h
@@ -101,10 +101,8 @@ void		swfdec_as_frame_push_block	(SwfdecAsFrame *	frame,
 						 const guint8 *		start,
 						 const guint8 *		end,
 						 SwfdecAsFrameBlockFunc	func,
-						 gpointer		data,
-						 GDestroyNotify		destroy);
+						 gpointer		data);
 void		swfdec_as_frame_pop_block	(SwfdecAsFrame *	frame);
-void		swfdec_as_frame_check_block	(SwfdecAsFrame *	frame);
 void		swfdec_as_frame_handle_exception(SwfdecAsFrame *	frame);
 
 


More information about the Swfdec mailing list