[maemo-commits] [maemo-commits] r15427 - in projects/haf/trunk/maemo-launcher: . launcher

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Wed Apr 16 01:35:59 EEST 2008
Author: guillem
Date: 2008-04-16 01:35:58 +0300 (Wed, 16 Apr 2008)
New Revision: 15427

Modified:
   projects/haf/trunk/maemo-launcher/ChangeLog
   projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
Log:
Add new low-level put/set functions to handle comm_msg

This allows getting rid of comm_msg_unpack_mem.


Modified: projects/haf/trunk/maemo-launcher/ChangeLog
===================================================================
--- projects/haf/trunk/maemo-launcher/ChangeLog	2008-04-15 22:35:56 UTC (rev 15426)
+++ projects/haf/trunk/maemo-launcher/ChangeLog	2008-04-15 22:35:58 UTC (rev 15427)
@@ -1,3 +1,18 @@
+2008-04-15  Guillem Jover  <guillem.jover at nokia.com>
+
+	* launcher/comm_msg.c (comm_msg_unpack_mem): Remove function.
+	(comm_msg_put_u32): New function.
+	(comm_msg_get_u32): Likewise.
+	(comm_msg_set_magic): Use comm_msg_put_u32 instead of direct buffer
+	access.
+	(comm_msg_pack_int): Likewise.
+	(comm_msg_pack_str): Likewise.
+	(comm_msg_get_magic): Use comm_msg_get_u32 instead of direct buffer
+	access.
+	(comm_msg_unpack_int): Likewise. Perform boundary checks.
+	(comm_msg_unpack_str): Likewise. Get the string through the current
+	buffer read address.
+
 2008-03-20  Guillem Jover  <guillem.jover at nokia.com>
 
 	* launcher/comm_msg.h (comm_msg_set_magic): New prototype.

Modified: projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-04-15 22:35:56 UTC (rev 15426)
+++ projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-04-15 22:35:58 UTC (rev 15427)
@@ -139,32 +139,22 @@
   return true;
 }
 
-static const void *
-comm_msg_unpack_mem(comm_msg_t *msg, uint32_t *size)
+/*
+ * Low level put/get functions.
+ */
+
+static void
+comm_msg_put_u32(comm_msg_t *msg, uint32_t i)
 {
-  void *mem;
-  int old_read = msg->read;
-  uint32_t new_size;
+  memcpy(msg->buf + msg->used, &i, sizeof(i));
+  msg->used += sizeof(i);
+}
 
-  if (msg->read + sizeof(uint32_t) > msg->used)
-    return NULL;
-
-  /* Unpack the size. */
-  memcpy(&new_size, msg->buf + msg->read, sizeof(uint32_t));
-  msg->read += sizeof(uint32_t);
-
-  /* Keep a pointer to the data. */
-  mem = msg->buf + msg->read;
-  msg->read += new_size;
-
-  if (msg->read > msg->used) {
-    msg->read = old_read;
-    return NULL;
-  }
-
-  *size = new_size;
-
-  return mem;
+static void
+comm_msg_get_u32(comm_msg_t *msg, uint32_t *i)
+{
+  memcpy(i, msg->buf + msg->read, sizeof(*i));
+  msg->read += sizeof(*i);
 }
 
 /*
@@ -177,8 +167,7 @@
   if (!comm_msg_grow(msg, sizeof(magic)))
     return false;
 
-  memcpy(msg->buf + msg->used, &magic, sizeof(magic));
-  msg->used += sizeof(magic);
+  comm_msg_put_u32(msg, magic);
 
   return true;
 }
@@ -189,8 +178,7 @@
   if (msg->read + sizeof(*magic) > msg->used)
     return false;
 
-  memcpy(magic, msg->buf + msg->read, sizeof(*magic));
-  msg->read += sizeof(*magic);
+  comm_msg_get_u32(msg, magic);
 
   return true;
 }
@@ -204,12 +192,10 @@
     return false;
 
   /* Pack the size. */
-  memcpy(msg->buf + msg->used, &size, sizeof(size));
-  msg->used += sizeof(size);
+  comm_msg_put_u32(msg, size);
 
   /* Pack the data. */
-  memcpy(msg->buf + msg->used, &i, size);
-  msg->used += size;
+  comm_msg_put_u32(msg, i);
 
   return true;
 }
@@ -217,18 +203,17 @@
 bool
 comm_msg_unpack_int(comm_msg_t *msg, uint32_t *i)
 {
-  uint32_t size, *p;
+  uint32_t size;
 
-  p = (uint32_t *)comm_msg_unpack_mem(msg, &size);
-  if (!p) {
-    error("retrieving the integer\n");
+  if (msg->read + sizeof(size) + sizeof(*i) > msg->used)
     return false;
-  }
 
+  comm_msg_get_u32(msg, &size);
+
   if (size != sizeof(*i))
     return false;
 
-  *i = *p;
+  comm_msg_get_u32(msg, i);
 
   return true;
 }
@@ -244,8 +229,7 @@
     return false;
 
   /* Pack the size. */
-  memcpy(msg->buf + msg->used, &aligned_size, sizeof(size));
-  msg->used += sizeof(size);
+  comm_msg_put_u32(msg, aligned_size);
 
   /* Pack the data. */
   memcpy(msg->buf + msg->used, str, size);
@@ -267,13 +251,18 @@
   uint32_t size;
   const char *str;
 
-  str = comm_msg_unpack_mem(msg, &size);
-  if (!str)
-  {
-    error("retrieving the string\n");
+  if (msg->read + sizeof(uint32_t) > msg->used)
     return false;
-  }
 
+  comm_msg_get_u32(msg, &size);
+
+  /* Keep a pointer to the data. */
+  str = msg->buf + msg->read;
+  msg->read += size;
+
+  if (msg->read > msg->used)
+    return false;
+
   if ((size - strlen(str)) > WORD_SIZE)
     return false;
 


More information about the maemo-commits mailing list