changeset 15090:033e1604cf63

[gaim-migrate @ 17875] Add a helper function byte_stream_new() that allocates memory for a bytestream in addition to just initializing the data structure. 9 files changed, 56 insertions(+), 91 deletions(-) committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sat, 02 Dec 2006 10:17:13 +0000
parents 25c3a33c6485
children 9cdc8bb39cf2
files libgaim/protocols/oscar/bstream.c libgaim/protocols/oscar/family_icbm.c libgaim/protocols/oscar/family_locate.c libgaim/protocols/oscar/flap_connection.c libgaim/protocols/oscar/odc.c libgaim/protocols/oscar/oft.c libgaim/protocols/oscar/oscar.h libgaim/protocols/oscar/peer_proxy.c libgaim/protocols/oscar/tlv.c
diffstat 9 files changed, 56 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/libgaim/protocols/oscar/bstream.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/bstream.c	Sat Dec 02 10:17:13 2006 +0000
@@ -24,10 +24,18 @@
 
 #include "oscar.h"
 
+int byte_stream_new(ByteStream *bs, guint32 len)
+{
+	if (bs == NULL)
+		return -1;
+
+	return byte_stream_init(bs, g_malloc(len), len);
+}
+
 int byte_stream_init(ByteStream *bs, guint8 *data, int len)
 {
 
-	if (!bs)
+	if (bs == NULL)
 		return -1;
 
 	bs->data = data;
--- a/libgaim/protocols/oscar/family_icbm.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/family_icbm.c	Sat Dec 02 10:17:13 2006 +0000
@@ -313,7 +313,7 @@
 		msgtlvlen += 4 /* charset */ + args->msglen;
 	}
 
-	byte_stream_init(&data, g_malloc(msgtlvlen + 128), msgtlvlen + 128);
+	byte_stream_new(&data, msgtlvlen + 128);
 
 	/* Generate an ICBM cookie */
 	aim_icbm_makecookie(cookie);
@@ -452,8 +452,6 @@
 	struct aim_invite_priv *priv;
 	guchar cookie[8];
 	aim_tlvlist_t *otl = NULL, *itl = NULL;
-	guint8 *hdr;
-	int hdrlen;
 	ByteStream hdrbs;
 
 	if (!od || !(conn = flap_connection_findbygroup(od, 0x0004)))
@@ -494,9 +492,7 @@
 	 * raw data, followed by a series of TLVs.
 	 *
 	 */
-	hdrlen = 2+8+16+6+4+4+strlen(msg)+4+2+1+strlen(roomname)+2;
-	hdr = malloc(hdrlen);
-	byte_stream_init(&hdrbs, hdr, hdrlen);
+	byte_stream_new(&hdrbs, 2+8+16+6+4+4+strlen(msg)+4+2+1+strlen(roomname)+2);
 
 	byte_stream_put16(&hdrbs, 0x0000); /* Unknown! */
 	byte_stream_putraw(&hdrbs, cookie, sizeof(cookie)); /* I think... */
@@ -508,11 +504,11 @@
 	aim_tlvlist_add_chatroom(&itl, 0x2711, exchange, roomname, instance);
 	aim_tlvlist_write(&hdrbs, &itl);
 
-	aim_tlvlist_add_raw(&otl, 0x0005, byte_stream_curpos(&hdrbs), hdr);
+	aim_tlvlist_add_raw(&otl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
+	g_free(hdrbs.data);
 
 	aim_tlvlist_write(&frame->data, &otl);
 
-	free(hdr);
 	aim_tlvlist_free(&itl);
 	aim_tlvlist_free(&otl);
 
@@ -694,8 +690,6 @@
 	FlapFrame *frame;
 	aim_snacid_t snacid;
 	aim_tlvlist_t *tl = NULL, *itl = NULL;
-	int hdrlen;
-	guint8 *hdr;
 	ByteStream hdrbs;
 
 	od = peer_conn->od;
@@ -713,9 +707,7 @@
 
 	aim_tlvlist_add_noval(&tl, 0x0003);
 
-	hdrlen = 64;
-	hdr = malloc(hdrlen);
-	byte_stream_init(&hdrbs, hdr, hdrlen);
+	byte_stream_new(&hdrbs, 64);
 
 	byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_CANCEL);
 	byte_stream_putraw(&hdrbs, peer_conn->cookie, 8);
@@ -725,11 +717,11 @@
 	aim_tlvlist_add_16(&itl, 0x000b, 0x0001);
 	aim_tlvlist_write(&hdrbs, &itl);
 
-	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdr);
+	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
+	g_free(hdrbs.data);
 
 	aim_tlvlist_write(&frame->data, &tl);
 
-	free(hdr);
 	aim_tlvlist_free(&itl);
 	aim_tlvlist_free(&tl);
 
@@ -784,8 +776,6 @@
 	FlapFrame *frame;
 	aim_snacid_t snacid;
 	aim_tlvlist_t *tl = NULL, *itl = NULL;
-	int hdrlen;
-	guint8 *hdr;
 	ByteStream hdrbs;
 
 	conn = flap_connection_findbygroup(od, 0x0004);
@@ -802,9 +792,7 @@
 
 	aim_tlvlist_add_noval(&tl, 0x0003);
 
-	hdrlen = 128;
-	hdr = malloc(hdrlen);
-	byte_stream_init(&hdrbs, hdr, hdrlen);
+	byte_stream_new(&hdrbs, 128);
 
 	byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
 	byte_stream_putraw(&hdrbs, cookie, 8);
@@ -817,11 +805,11 @@
 	aim_tlvlist_add_noval(&itl, 0x000f);
 	aim_tlvlist_write(&hdrbs, &itl);
 
-	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdr);
+	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
+	g_free(hdrbs.data);
 
 	aim_tlvlist_write(&frame->data, &tl);
 
-	free(hdr);
 	aim_tlvlist_free(&itl);
 	aim_tlvlist_free(&tl);
 
@@ -839,8 +827,6 @@
 	FlapFrame *frame;
 	aim_snacid_t snacid;
 	aim_tlvlist_t *tl = NULL, *itl = NULL;
-	int hdrlen;
-	guint8 *hdr;
 	ByteStream hdrbs;
 	guint8 ip_comp[4];
 
@@ -858,9 +844,7 @@
 
 	aim_tlvlist_add_noval(&tl, 0x0003);
 
-	hdrlen = 128;
-	hdr = malloc(hdrlen);
-	byte_stream_init(&hdrbs, hdr, hdrlen);
+	byte_stream_new(&hdrbs, 128);
 
 	byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
 	byte_stream_putraw(&hdrbs, cookie, 8);
@@ -883,11 +867,11 @@
 
 	aim_tlvlist_write(&hdrbs, &itl);
 
-	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdr);
+	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
+	g_free(hdrbs.data);
 
 	aim_tlvlist_write(&frame->data, &tl);
 
-	free(hdr);
 	aim_tlvlist_free(&itl);
 	aim_tlvlist_free(&tl);
 
@@ -905,8 +889,6 @@
 	FlapFrame *frame;
 	aim_snacid_t snacid;
 	aim_tlvlist_t *tl = NULL, *itl = NULL;
-	int hdrlen, buflen;
-	guint8 *hdr;
 	ByteStream hdrbs;
 
 	conn = flap_connection_findbygroup(od, 0x0004);
@@ -923,9 +905,7 @@
 
 	aim_tlvlist_add_noval(&tl, 0x0003);
 
-	hdrlen = 512;
-	hdr = malloc(hdrlen);
-	byte_stream_init(&hdrbs, hdr, hdrlen);
+	byte_stream_new(&hdrbs, 512);
 
 	byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
 	byte_stream_putraw(&hdrbs, cookie, 8);
@@ -954,8 +934,7 @@
 		ByteStream bs;
 
 		/* Begin TLV t(2711) */
-		buflen = 2+2+4+strlen(filename)+1;
-		byte_stream_init(&bs, malloc(buflen), buflen);
+		byte_stream_new(&bs, 2+2+4+strlen(filename)+1);
 		byte_stream_put16(&bs, (numfiles > 1) ? 0x0002 : 0x0001);
 		byte_stream_put16(&bs, numfiles);
 		byte_stream_put32(&bs, size);
@@ -965,16 +944,16 @@
 		byte_stream_put8(&bs, 0x00);
 
 		aim_tlvlist_add_raw(&itl, 0x2711, bs.len, bs.data);
-		free(bs.data);
+		g_free(bs.data);
 		/* End TLV t(2711) */
 	}
 
 	aim_tlvlist_write(&hdrbs, &itl);
-	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdr);
+	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
+	g_free(hdrbs.data);
 
 	aim_tlvlist_write(&frame->data, &tl);
 
-	free(hdr);
 	aim_tlvlist_free(&itl);
 	aim_tlvlist_free(&tl);
 
@@ -992,8 +971,6 @@
 	FlapFrame *frame;
 	aim_snacid_t snacid;
 	aim_tlvlist_t *tl = NULL, *itl = NULL;
-	int hdrlen, buflen;
-	guint8 *hdr;
 	ByteStream hdrbs;
 	guint8 ip_comp[4];
 
@@ -1011,9 +988,7 @@
 
 	aim_tlvlist_add_noval(&tl, 0x0003);
 
-	hdrlen = 512;
-	hdr = malloc(hdrlen);
-	byte_stream_init(&hdrbs, hdr, hdrlen);
+	byte_stream_new(&hdrbs, 512);
 
 	byte_stream_put16(&hdrbs, AIM_RENDEZVOUS_PROPOSE);
 	byte_stream_putraw(&hdrbs, cookie, 8);
@@ -1050,8 +1025,7 @@
 		ByteStream bs;
 
 		/* Begin TLV t(2711) */
-		buflen = 2+2+4+strlen(filename)+1;
-		byte_stream_init(&bs, malloc(buflen), buflen);
+		byte_stream_new(&bs, 2+2+4+strlen(filename)+1);
 		byte_stream_put16(&bs, (numfiles > 1) ? 0x0002 : 0x0001);
 		byte_stream_put16(&bs, numfiles);
 		byte_stream_put32(&bs, size);
@@ -1061,17 +1035,17 @@
 		byte_stream_put8(&bs, 0x00);
 
 		aim_tlvlist_add_raw(&itl, 0x2711, bs.len, bs.data);
-		free(bs.data);
+		g_free(bs.data);
 		/* End TLV t(2711) */
 	}
 
 	aim_tlvlist_write(&hdrbs, &itl);
 
-	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdr);
+	aim_tlvlist_add_raw(&tl, 0x0005, byte_stream_curpos(&hdrbs), hdrbs.data);
+	g_free(hdrbs.data);
 
 	aim_tlvlist_write(&frame->data, &tl);
 
-	free(hdr);
 	aim_tlvlist_free(&itl);
 	aim_tlvlist_free(&tl);
 
--- a/libgaim/protocols/oscar/family_locate.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/family_locate.c	Sat Dec 02 10:17:13 2006 +0000
@@ -1356,15 +1356,13 @@
 aim_locate_getinfoshort(OscarData *od, const char *sn, guint32 flags)
 {
 	FlapConnection *conn;
-	unsigned int length;
 	ByteStream data;
 	aim_snacid_t snacid;
 
 	if (!od || !(conn = flap_connection_findbygroup(od, SNAC_FAMILY_LOCATE)) || !sn)
 		return -EINVAL;
 
-	length = 4 + 1 + strlen(sn);
-	byte_stream_init(&data, g_malloc(length), length);
+	byte_stream_new(&data, 4 + 1 + strlen(sn));
 	byte_stream_put32(&data, flags);
 	byte_stream_put8(&data, strlen(sn));
 	byte_stream_putstr(&data, sn);
--- a/libgaim/protocols/oscar/flap_connection.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/flap_connection.c	Sat Dec 02 10:17:13 2006 +0000
@@ -480,11 +480,7 @@
 	frame->channel = channel;
 
 	if (datalen > 0)
-	{
-		guint8 *data;
-		data = g_malloc(datalen);
-		byte_stream_init(&frame->data, data, datalen);
-	}
+		byte_stream_new(&frame->data, datalen);
 
 	return frame;
 }
@@ -819,7 +815,7 @@
 
 	payloadlen = byte_stream_curpos(&frame->data);
 
-	byte_stream_init(&bs, malloc(6 + payloadlen), 6 + payloadlen);
+	byte_stream_new(&bs, 6 + payloadlen);
 
 	/* FLAP header */
 	byte_stream_put8(&bs, 0x2a);
@@ -835,7 +831,7 @@
 	byte_stream_rewind(&bs);
 	flap_connection_send_byte_stream(&bs, conn, bslen);
 
-	free(bs.data); /* XXX byte_stream_free */
+	g_free(bs.data); /* XXX byte_stream_free */
 }
 
 void
--- a/libgaim/protocols/oscar/odc.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/odc.c	Sat Dec 02 10:17:13 2006 +0000
@@ -96,8 +96,7 @@
 	memcpy(frame->cookie, conn->cookie, 8);
 
 	length = 76;
-	byte_stream_init(&bs, malloc(length + frame->payload.len),
-			length + frame->payload.len);
+	byte_stream_new(&bs, length + frame->payload.len);
 	byte_stream_putraw(&bs, conn->magic, 4);
 	byte_stream_put16(&bs, length);
 	byte_stream_put16(&bs, frame->type);
@@ -120,7 +119,7 @@
 
 	peer_connection_send(conn, &bs);
 
-	free(bs.data);
+	g_free(bs.data);
 }
 
 /**
@@ -188,7 +187,7 @@
 	frame.payload.len = len;
 	frame.encoding = encoding;
 	frame.flags = autoreply;
-	byte_stream_init(&frame.payload, malloc(len), len);
+	byte_stream_new(&frame.payload, len);
 	byte_stream_putraw(&frame.payload, (guint8 *)msg, len);
 
 	peer_odc_send(conn, &frame);
--- a/libgaim/protocols/oscar/oft.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/oft.c	Sat Dec 02 10:17:13 2006 +0000
@@ -160,7 +160,7 @@
 	ByteStream bs;
 
 	length = 192 + MAX(64, frame->name_length + 1);
-	byte_stream_init(&bs, malloc(length), length);
+	byte_stream_new(&bs, length);
 	byte_stream_putraw(&bs, conn->magic, 4);
 	byte_stream_put16(&bs, length);
 	byte_stream_put16(&bs, frame->type);
@@ -197,7 +197,7 @@
 
 	peer_connection_send(conn, &bs);
 
-	free(bs.data);
+	g_free(bs.data);
 }
 
 void
--- a/libgaim/protocols/oscar/oscar.h	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/oscar.h	Sat Dec 02 10:17:13 2006 +0000
@@ -1504,6 +1504,7 @@
 void aim_genericreq_s(OscarData *od, FlapConnection *conn, guint16 family, guint16 subtype, guint16 *);
 
 /* bstream.c */
+int byte_stream_new(ByteStream *bs, guint32 len);
 int byte_stream_init(ByteStream *bs, guint8 *data, int len);
 int byte_stream_empty(ByteStream *bs);
 int byte_stream_curpos(ByteStream *bs);
--- a/libgaim/protocols/oscar/peer_proxy.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/peer_proxy.c	Sat Dec 02 10:17:13 2006 +0000
@@ -38,7 +38,7 @@
 			frame->flags, frame->payload.len);
 
 	length = 12 + frame->payload.len;
-	byte_stream_init(&bs, malloc(length), length);
+	byte_stream_new(&bs, length);
 	byte_stream_put16(&bs, length - 2);
 	byte_stream_put16(&bs, PEER_PROXY_PACKET_VERSION);
 	byte_stream_put16(&bs, frame->type);
@@ -48,7 +48,7 @@
 
 	peer_connection_send(conn, &bs);
 
-	free(bs.data);
+	g_free(bs.data);
 }
 
 /**
@@ -65,7 +65,6 @@
 	GaimAccount *account;
 	const gchar *sn;
 	guint8 sn_length;
-	size_t length;
 
 	memset(&frame, 0, sizeof(ProxyFrame));
 	frame.type = PEER_PROXY_TYPE_CREATE;
@@ -74,8 +73,7 @@
 	account = gaim_connection_get_account(conn->od->gc);
 	sn = gaim_account_get_username(account);
 	sn_length = strlen(sn);
-	length = 1 + sn_length + 8 + 20;
-	byte_stream_init(&frame.payload, malloc(length), length);
+	byte_stream_new(&frame.payload, 1 + sn_length + 8 + 20);
 	byte_stream_put8(&frame.payload, sn_length);
 	byte_stream_putraw(&frame.payload, (const guint8 *)sn, sn_length);
 	byte_stream_putraw(&frame.payload, conn->cookie, 8);
@@ -103,7 +101,6 @@
 	GaimAccount *account;
 	const gchar *sn;
 	guint8 sn_length;
-	size_t length;
 
 	memset(&frame, 0, sizeof(ProxyFrame));
 	frame.type = PEER_PROXY_TYPE_JOIN;
@@ -112,8 +109,7 @@
 	account = gaim_connection_get_account(conn->od->gc);
 	sn = gaim_account_get_username(account);
 	sn_length = strlen(sn);
-	length = 1 + sn_length + 2 + 8 + 20;
-	byte_stream_init(&frame.payload, malloc(length), length);
+	byte_stream_new(&frame.payload, 1 + sn_length + 2 + 8 + 20);
 	byte_stream_put8(&frame.payload, sn_length);
 	byte_stream_putraw(&frame.payload, (const guint8 *)sn, sn_length);
 	byte_stream_put16(&frame.payload, pin);
--- a/libgaim/protocols/oscar/tlv.c	Sat Dec 02 09:44:53 2006 +0000
+++ b/libgaim/protocols/oscar/tlv.c	Sat Dec 02 10:17:13 2006 +0000
@@ -264,8 +264,8 @@
 	if (aim_tlvlist_size(&one) != aim_tlvlist_size(&two))
 		return 1;
 
-	byte_stream_init(&bs1, ((guint8 *)malloc(aim_tlvlist_size(&one)*sizeof(guint8))), aim_tlvlist_size(&one));
-	byte_stream_init(&bs2, ((guint8 *)malloc(aim_tlvlist_size(&two)*sizeof(guint8))), aim_tlvlist_size(&two));
+	byte_stream_new(&bs1, aim_tlvlist_size(&one));
+	byte_stream_new(&bs2, aim_tlvlist_size(&two));
 
 	aim_tlvlist_write(&bs1, &one);
 	aim_tlvlist_write(&bs2, &two);
@@ -276,8 +276,8 @@
 		return 1;
 	}
 
-	free(bs1.data);
-	free(bs2.data);
+	g_free(bs1.data);
+	g_free(bs2.data);
 
 	return 0;
 }
@@ -512,23 +512,19 @@
  */
 int aim_tlvlist_add_chatroom(aim_tlvlist_t **list, guint16 type, guint16 exchange, const char *roomname, guint16 instance)
 {
-	guint8 *buf;
 	int len;
 	ByteStream bs;
 
-	len = 2 + 1 + strlen(roomname) + 2;
-
-	buf = malloc(len);
-	byte_stream_init(&bs, buf, len);
+	byte_stream_new(&bs, 2 + 1 + strlen(roomname) + 2);
 
 	byte_stream_put16(&bs, exchange);
 	byte_stream_put8(&bs, strlen(roomname));
 	byte_stream_putstr(&bs, roomname);
 	byte_stream_put16(&bs, instance);
 
-	len = aim_tlvlist_add_raw(list, type, byte_stream_curpos(&bs), buf);
+	len = aim_tlvlist_add_raw(list, type, byte_stream_curpos(&bs), bs.data);
 
-	free(buf);
+	g_free(bs.data);
 
 	return len;
 }
@@ -563,7 +559,6 @@
  */
 int aim_tlvlist_add_frozentlvlist(aim_tlvlist_t **list, guint16 type, aim_tlvlist_t **tl)
 {
-	guint8 *buf;
 	int buflen;
 	ByteStream bs;
 
@@ -572,15 +567,13 @@
 	if (buflen <= 0)
 		return 0;
 
-	buf = malloc(buflen);
-
-	byte_stream_init(&bs, buf, buflen);
+	byte_stream_new(&bs, buflen);
 
 	aim_tlvlist_write(&bs, tl);
 
-	aim_tlvlist_add_raw(list, type, byte_stream_curpos(&bs), buf);
+	aim_tlvlist_add_raw(list, type, byte_stream_curpos(&bs), bs.data);
 
-	free(buf);
+	g_free(bs.data);
 
 	return buflen;
 }