changeset 30393:bbb27d65681f

Bstream cleanups: 1. g_return_val_if_fail()'s in place of XXX's. 2. byte_stream_empty() -> byte_stream_bytes_left(). I think the world is a better place now.
author ivan.komarov@soc.pidgin.im
date Wed, 28 Jul 2010 23:53:25 +0000
parents a4f579485ce6
children ed520e6e972b 1dd3ed053628
files libpurple/protocols/oscar/bstream.c libpurple/protocols/oscar/family_admin.c libpurple/protocols/oscar/family_chat.c libpurple/protocols/oscar/family_feedbag.c libpurple/protocols/oscar/family_icbm.c libpurple/protocols/oscar/family_locate.c libpurple/protocols/oscar/family_oservice.c libpurple/protocols/oscar/flap_connection.c libpurple/protocols/oscar/misc.c libpurple/protocols/oscar/oscar.h libpurple/protocols/oscar/peer_proxy.c libpurple/protocols/oscar/tlv.c
diffstat 12 files changed, 56 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/oscar/bstream.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/bstream.c	Wed Jul 28 23:53:25 2010 +0000
@@ -34,7 +34,6 @@
 
 int byte_stream_init(ByteStream *bs, guint8 *data, int len)
 {
-
 	if (bs == NULL)
 		return -1;
 
@@ -50,7 +49,7 @@
 	g_free(bs->data);
 }
 
-int byte_stream_empty(ByteStream *bs)
+int byte_stream_bytes_left(ByteStream *bs)
 {
 	return bs->len - bs->offset;
 }
@@ -62,102 +61,74 @@
 
 int byte_stream_setpos(ByteStream *bs, unsigned int off)
 {
-
-	if (off > bs->len)
-		return -1;
+	g_return_val_if_fail(off <= bs->len, -1);
 
 	bs->offset = off;
-
 	return off;
 }
 
 void byte_stream_rewind(ByteStream *bs)
 {
-
 	byte_stream_setpos(bs, 0);
-
-	return;
 }
 
 /*
  * N can be negative, which can be used for going backwards
- * in a bstream.  I'm not sure if libfaim actually does
- * this anywhere...
+ * in a bstream.
  */
 int byte_stream_advance(ByteStream *bs, int n)
 {
-
-	if ((byte_stream_curpos(bs) + n < 0) || (byte_stream_empty(bs) < n))
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail((byte_stream_curpos(bs) + n >= 0) && (n <= byte_stream_bytes_left(bs)), 0);
 
 	bs->offset += n;
-
 	return n;
 }
 
 guint8 byte_stream_get8(ByteStream *bs)
 {
-
-	if (byte_stream_empty(bs) < 1)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 1, 0);
 
 	bs->offset++;
-
 	return aimutil_get8(bs->data + bs->offset - 1);
 }
 
 guint16 byte_stream_get16(ByteStream *bs)
 {
-
-	if (byte_stream_empty(bs) < 2)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 2, 0);
 
 	bs->offset += 2;
-
 	return aimutil_get16(bs->data + bs->offset - 2);
 }
 
 guint32 byte_stream_get32(ByteStream *bs)
 {
-
-	if (byte_stream_empty(bs) < 4)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 4, 0);
 
 	bs->offset += 4;
-
 	return aimutil_get32(bs->data + bs->offset - 4);
 }
 
 guint8 byte_stream_getle8(ByteStream *bs)
 {
-
-	if (byte_stream_empty(bs) < 1)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 1, 0);
 
 	bs->offset++;
-
 	return aimutil_getle8(bs->data + bs->offset - 1);
 }
 
 guint16 byte_stream_getle16(ByteStream *bs)
 {
-
-	if (byte_stream_empty(bs) < 2)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 2, 0);
 
 	bs->offset += 2;
-
 	return aimutil_getle16(bs->data + bs->offset - 2);
 }
 
 guint32 byte_stream_getle32(ByteStream *bs)
 {
-
-	if (byte_stream_empty(bs) < 4)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 4, 0);
 
 	bs->offset += 4;
-
 	return aimutil_getle32(bs->data + bs->offset - 4);
 }
 
@@ -169,9 +140,7 @@
 
 int byte_stream_getrawbuf(ByteStream *bs, guint8 *buf, int len)
 {
-
-	if (byte_stream_empty(bs) < len)
-		return 0;
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0);
 
 	byte_stream_getrawbuf_nocheck(bs, buf, len);
 	return len;
@@ -181,13 +150,10 @@
 {
 	guint8 *ob;
 
-	if (byte_stream_empty(bs) < len)
-		return NULL;
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, NULL);
 
 	ob = g_malloc(len);
-
 	byte_stream_getrawbuf_nocheck(bs, ob, len);
-
 	return ob;
 }
 
@@ -195,94 +161,69 @@
 {
 	char *ob;
 
-	if (byte_stream_empty(bs) < len)
-		return NULL;
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, NULL);
 
 	ob = g_malloc(len + 1);
-
 	byte_stream_getrawbuf_nocheck(bs, (guint8 *)ob, len);
-
 	ob[len] = '\0';
-
 	return ob;
 }
 
 int byte_stream_put8(ByteStream *bs, guint8 v)
 {
-
-	if (byte_stream_empty(bs) < 1)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 1, 0);
 
 	bs->offset += aimutil_put8(bs->data + bs->offset, v);
-
 	return 1;
 }
 
 int byte_stream_put16(ByteStream *bs, guint16 v)
 {
-
-	if (byte_stream_empty(bs) < 2)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 2, 0);
 
 	bs->offset += aimutil_put16(bs->data + bs->offset, v);
-
 	return 2;
 }
 
 int byte_stream_put32(ByteStream *bs, guint32 v)
 {
-
-	if (byte_stream_empty(bs) < 4)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 4, 0);
 
 	bs->offset += aimutil_put32(bs->data + bs->offset, v);
-
 	return 1;
 }
 
 int byte_stream_putle8(ByteStream *bs, guint8 v)
 {
-
-	if (byte_stream_empty(bs) < 1)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 1, 0);
 
 	bs->offset += aimutil_putle8(bs->data + bs->offset, v);
-
 	return 1;
 }
 
 int byte_stream_putle16(ByteStream *bs, guint16 v)
 {
-
-	if (byte_stream_empty(bs) < 2)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 2, 0);
 
 	bs->offset += aimutil_putle16(bs->data + bs->offset, v);
-
 	return 2;
 }
 
 int byte_stream_putle32(ByteStream *bs, guint32 v)
 {
-
-	if (byte_stream_empty(bs) < 4)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= 4, 0);
 
 	bs->offset += aimutil_putle32(bs->data + bs->offset, v);
-
 	return 1;
 }
 
 
 int byte_stream_putraw(ByteStream *bs, const guint8 *v, int len)
 {
-
-	if (byte_stream_empty(bs) < len)
-		return 0; /* XXX throw an exception */
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0);
 
 	memcpy(bs->data + bs->offset, v, len);
 	bs->offset += len;
-
 	return len;
 }
 
@@ -293,17 +234,12 @@
 
 int byte_stream_putbs(ByteStream *bs, ByteStream *srcbs, int len)
 {
-
-	if (byte_stream_empty(srcbs) < len)
-		return 0; /* XXX throw exception (underrun) */
-
-	if (byte_stream_empty(bs) < len)
-		return 0; /* XXX throw exception (overflow) */
+	g_return_val_if_fail(byte_stream_bytes_left(srcbs) >= len, 0);
+	g_return_val_if_fail(byte_stream_bytes_left(bs) >= len, 0);
 
 	memcpy(bs->data + bs->offset, srcbs->data + srcbs->offset, len);
 	bs->offset += len;
 	srcbs->offset += len;
-
 	return len;
 }
 
--- a/libpurple/protocols/oscar/family_admin.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/family_admin.c	Wed Jul 28 23:53:25 2010 +0000
@@ -68,7 +68,7 @@
 	perms = byte_stream_get16(bs);
 	tlvcount = byte_stream_get16(bs);
 
-	while (tlvcount && byte_stream_empty(bs)) {
+	while (tlvcount && byte_stream_bytes_left(bs)) {
 		guint16 type, length;
 
 		type = byte_stream_get16(bs);
--- a/libpurple/protocols/oscar/family_chat.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/family_chat.c	Wed Jul 28 23:53:25 2010 +0000
@@ -122,7 +122,7 @@
 	aim_rxcallback_t userfunc;
 	int curcount = 0, ret = 0;
 
-	while (byte_stream_empty(bs)) {
+	while (byte_stream_bytes_left(bs)) {
 		curcount++;
 		userinfo = g_realloc(userinfo, curcount * sizeof(aim_userinfo_t));
 		aim_info_extract(od, bs, &userinfo[curcount-1]);
--- a/libpurple/protocols/oscar/family_feedbag.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/family_feedbag.c	Wed Jul 28 23:53:25 2010 +0000
@@ -1170,7 +1170,7 @@
 	od->ssi.numitems += byte_stream_get16(bs); /* # of items in this SSI SNAC */
 
 	/* Read in the list */
-	while (byte_stream_empty(bs) > 4) { /* last four bytes are timestamp */
+	while (byte_stream_bytes_left(bs) > 4) { /* last four bytes are timestamp */
 		if ((namelen = byte_stream_get16(bs)))
 			name = byte_stream_getstr(bs, namelen);
 		else
@@ -1288,7 +1288,7 @@
 	guint16 len, gid, bid, type;
 	GSList *data;
 
-	while (byte_stream_empty(bs)) {
+	while (byte_stream_bytes_left(bs)) {
 		if ((len = byte_stream_get16(bs)))
 			name = byte_stream_getstr(bs, len);
 		else
@@ -1326,7 +1326,7 @@
 	GSList *data;
 	struct aim_ssi_item *item;
 
-	while (byte_stream_empty(bs)) {
+	while (byte_stream_bytes_left(bs)) {
 		if ((len = byte_stream_get16(bs)))
 			name = byte_stream_getstr(bs, len);
 		else
@@ -1378,7 +1378,7 @@
 	guint16 gid, bid;
 	struct aim_ssi_item *del;
 
-	while (byte_stream_empty(bs)) {
+	while (byte_stream_bytes_left(bs)) {
 		byte_stream_advance(bs, byte_stream_get16(bs));
 		gid = byte_stream_get16(bs);
 		bid = byte_stream_get16(bs);
@@ -1411,7 +1411,7 @@
 
 	/* Read in the success/failure flags from the ack SNAC */
 	cur = od->ssi.pending;
-	while (cur && (byte_stream_empty(bs)>0)) {
+	while (cur && (byte_stream_bytes_left(bs)>0)) {
 		cur->ack = byte_stream_get16(bs);
 		cur = cur->next;
 	}
--- a/libpurple/protocols/oscar/family_icbm.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/family_icbm.c	Wed Jul 28 23:53:25 2010 +0000
@@ -919,7 +919,7 @@
 	/*
 	 * We're interested in the inner TLV 0x101, which contains precious, precious message.
 	 */
-	while (byte_stream_empty(message) >= 4) {
+	while (byte_stream_bytes_left(message) >= 4) {
 		guint16 type = byte_stream_get16(message);
 		guint16 length = byte_stream_get16(message);
 		if (type == 0x101) {
@@ -952,12 +952,12 @@
 	 * I've changed it to process the TLVs in-place.  This avoids lots
 	 * of per-IM memory allocations.
 	 */
-	while (byte_stream_empty(bs) >= 4)
+	while (byte_stream_bytes_left(bs) >= 4)
 	{
 		type = byte_stream_get16(bs);
 		length = byte_stream_get16(bs);
 
-		if (length > byte_stream_empty(bs))
+		if (length > byte_stream_bytes_left(bs))
 		{
 			purple_debug_misc("oscar", "Received an IM containing an invalid message part from %s.  They are probably trying to do something malicious.\n", userinfo->bn);
 			break;
@@ -1050,7 +1050,7 @@
 	 *     ...
 	 *   ...
 	 */
-	while (byte_stream_empty(servdata))
+	while (byte_stream_bytes_left(servdata))
 	{
 		guint16 gnlen, numb;
 		int i;
@@ -1550,7 +1550,7 @@
 	guint16 channel, nummissed, reason;
 	aim_userinfo_t userinfo;
 
-	while (byte_stream_empty(bs)) {
+	while (byte_stream_bytes_left(bs)) {
 
 		channel = byte_stream_get16(bs);
 		aim_info_extract(od, bs, &userinfo);
--- a/libpurple/protocols/oscar/family_locate.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/family_locate.c	Wed Jul 28 23:53:25 2010 +0000
@@ -583,7 +583,7 @@
 	guint64 flags = 0;
 	int offset;
 
-	for (offset = 0; byte_stream_empty(bs) && (offset < len); offset += 0x10) {
+	for (offset = 0; byte_stream_bytes_left(bs) && (offset < len); offset += 0x10) {
 		guint8 *cap;
 		int i, identified;
 
@@ -617,7 +617,7 @@
 	int offset;
 	const char *result = NULL;
 
-	for (offset = 0; byte_stream_empty(bs) && (offset < len); offset += 0x10) {
+	for (offset = 0; byte_stream_bytes_left(bs) && (offset < len); offset += 0x10) {
 		/* check wheather this capability is a custom user icon */
 		guint8 *cap;
 		int i;
@@ -643,7 +643,7 @@
 	guint64 flags = 0;
 	int offset;
 
-	for (offset = 0; byte_stream_empty(bs) && (offset < len); offset += 0x02) {
+	for (offset = 0; byte_stream_bytes_left(bs) && (offset < len); offset += 0x02) {
 		guint8 *cap;
 		int i, identified;
 
@@ -674,7 +674,7 @@
 	if (!bs)
 		return -EINVAL;
 
-	for (i = 0; byte_stream_empty(bs); i++) {
+	for (i = 0; byte_stream_bytes_left(bs); i++) {
 
 		if (aim_caps[i].flag == OSCAR_CAPABILITY_LAST)
 			break;
@@ -804,7 +804,7 @@
 		type = byte_stream_get16(bs);
 		length = byte_stream_get16(bs);
 		curpos = byte_stream_curpos(bs);
-		endpos = curpos + MIN(length, byte_stream_empty(bs));
+		endpos = curpos + MIN(length, byte_stream_bytes_left(bs));
 
 		if (type == 0x0001) {
 			/*
@@ -1010,7 +1010,7 @@
 				number2 = byte_stream_get8(bs);
 				length2 = byte_stream_get8(bs);
 
-				endpos2 = byte_stream_curpos(bs) + MIN(length2, byte_stream_empty(bs));
+				endpos2 = byte_stream_curpos(bs) + MIN(length2, byte_stream_bytes_left(bs));
 
 				switch (type2) {
 					case 0x0000: { /* This is an official buddy icon? */
--- a/libpurple/protocols/oscar/family_oservice.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/family_oservice.c	Wed Jul 28 23:53:25 2010 +0000
@@ -97,7 +97,7 @@
 {
 	int group;
 
-	while (byte_stream_empty(bs))
+	while (byte_stream_bytes_left(bs))
 	{
 		group = byte_stream_get16(bs);
 		conn->groups = g_slist_prepend(conn->groups, GUINT_TO_POINTER(group));
@@ -586,7 +586,7 @@
 
 	newevil = byte_stream_get16(bs);
 
-	if (byte_stream_empty(bs))
+	if (byte_stream_bytes_left(bs))
 		aim_info_extract(od, bs, &userinfo);
 
 	if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
@@ -763,8 +763,8 @@
 	guint8 *versions;
 
 	/* This is frivolous. (Thank you SmarterChild.) */
-	vercount = byte_stream_empty(bs)/4;
-	versions = byte_stream_getraw(bs, byte_stream_empty(bs));
+	vercount = byte_stream_bytes_left(bs)/4;
+	versions = byte_stream_getraw(bs, byte_stream_bytes_left(bs));
 	g_free(versions);
 
 	/*
--- a/libpurple/protocols/oscar/flap_connection.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/flap_connection.c	Wed Jul 28 23:53:25 2010 +0000
@@ -733,7 +733,7 @@
 	aim_module_t *cur;
 	aim_modsnac_t snac;
 
-	if (byte_stream_empty(&frame->data) < 10)
+	if (byte_stream_bytes_left(&frame->data) < 10)
 		return;
 
 	snac.family = byte_stream_get16(&frame->data);
@@ -800,7 +800,7 @@
 	GSList *tlvlist;
 	char *msg = NULL;
 
-	if (byte_stream_empty(&frame->data) == 0) {
+	if (byte_stream_bytes_left(&frame->data) == 0) {
 		/* XXX should do something with this */
 		return;
 	}
@@ -1062,8 +1062,8 @@
 		return;
 
 	/* Make sure we don't send past the end of the bs */
-	if (count > byte_stream_empty(bs))
-		count = byte_stream_empty(bs); /* truncate to remaining space */
+	if (count > byte_stream_bytes_left(bs))
+		count = byte_stream_bytes_left(bs); /* truncate to remaining space */
 
 	if (count == 0)
 		return;
--- a/libpurple/protocols/oscar/misc.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/misc.c	Wed Jul 28 23:53:25 2010 +0000
@@ -91,7 +91,7 @@
 
 	snac2 = aim_remsnac(od, snac->id);
 
-	if (byte_stream_empty(bs))
+	if (byte_stream_bytes_left(bs))
 		error = byte_stream_get16(bs);
 
 	if ((userfunc = aim_callhandler(od, snac->family, snac->subtype)))
--- a/libpurple/protocols/oscar/oscar.h	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/oscar.h	Wed Jul 28 23:53:25 2010 +0000
@@ -1209,7 +1209,7 @@
 int byte_stream_new(ByteStream *bs, guint32 len);
 int byte_stream_init(ByteStream *bs, guint8 *data, int len);
 void byte_stream_destroy(ByteStream *bs);
-int byte_stream_empty(ByteStream *bs);
+int byte_stream_bytes_left(ByteStream *bs);
 int byte_stream_curpos(ByteStream *bs);
 int byte_stream_setpos(ByteStream *bs, unsigned int off);
 void byte_stream_rewind(ByteStream *bs);
--- a/libpurple/protocols/oscar/peer_proxy.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/peer_proxy.c	Wed Jul 28 23:53:25 2010 +0000
@@ -168,7 +168,7 @@
 	}
 	else if (frame->type == PEER_PROXY_TYPE_ERROR)
 	{
-		if (byte_stream_empty(&frame->payload) >= 2)
+		if (byte_stream_bytes_left(&frame->payload) >= 2)
 		{
 			guint16 error;
 			const char *msg;
--- a/libpurple/protocols/oscar/tlv.c	Wed Jul 28 23:13:25 2010 +0000
+++ b/libpurple/protocols/oscar/tlv.c	Wed Jul 28 23:53:25 2010 +0000
@@ -49,7 +49,7 @@
 	type = byte_stream_get16(bs);
 	length = byte_stream_get16(bs);
 
-	if (length > byte_stream_empty(bs)) {
+	if (length > byte_stream_bytes_left(bs)) {
 		aim_tlvlist_free(list);
 		return NULL;
 	}
@@ -88,7 +88,7 @@
 {
 	GSList *list = NULL;
 
-	while (byte_stream_empty(bs) > 0) {
+	while (byte_stream_bytes_left(bs) > 0) {
 		list = aim_tlv_read(list, bs);
 		if (list == NULL)
 			return NULL;
@@ -122,7 +122,7 @@
 {
 	GSList *list = NULL;
 
-	while ((byte_stream_empty(bs) > 0) && (num != 0)) {
+	while ((byte_stream_bytes_left(bs) > 0) && (num != 0)) {
 		list = aim_tlv_read(list, bs);
 		if (list == NULL)
 			return NULL;
@@ -157,7 +157,7 @@
 {
 	GSList *list = NULL;
 
-	while ((byte_stream_empty(bs) > 0) && (len > 0)) {
+	while ((byte_stream_bytes_left(bs) > 0) && (len > 0)) {
 		list = aim_tlv_read(list, bs);
 		if (list == NULL)
 			return NULL;
@@ -648,7 +648,7 @@
 	/* do an initial run to test total length */
 	goodbuflen = aim_tlvlist_size(*list);
 
-	if (goodbuflen > byte_stream_empty(bs))
+	if (goodbuflen > byte_stream_bytes_left(bs))
 		return 0; /* not enough buffer */
 
 	/* do the real write-out */