changeset 28655:06fabb28bc69

The "packed" attribute on the raw_chunk data-structure seems to be a GCC extension. (ie, no alignment padding between members of the structure) To build with compilers which don't support this packed attribute, we've now use methods to access/modify the fields in the packed MXit chunk header.
author andrew.victor@mxit.com
date Tue, 24 Nov 2009 13:23:27 +0000
parents 86fec8bfe88c
children f261b4978ae4
files libpurple/protocols/mxit/chunk.c libpurple/protocols/mxit/chunk.h libpurple/protocols/mxit/mxit.c libpurple/protocols/mxit/protocol.c
diffstat 4 files changed, 79 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/mxit/chunk.c	Tue Nov 24 11:50:55 2009 +0000
+++ b/libpurple/protocols/mxit/chunk.c	Tue Nov 24 13:23:27 2009 +0000
@@ -576,18 +576,17 @@
 
 	/* parse the resource chunks */
 	while ( chunklen > 0 ) {
-		struct raw_chunk* chunkhdr = ( struct raw_chunk * ) &chunkdata[pos];
-		chunkhdr->length = ntohl( chunkhdr->length );		/* host byte-order */
+		gchar* chunk = &chunkdata[pos];
 
 		/* start of chunk data */
-		pos += sizeof( struct raw_chunk );
+		pos += MXIT_CHUNK_HEADER_SIZE;
 
-		switch ( chunkhdr->type ) {
+		switch ( chunk_type( chunk ) ) {
 			case CP_CHUNK_SPLASH :			/* splash image */
 				{
 					struct splash_chunk* splash = g_new0( struct splash_chunk, 1 );
 
-					mxit_chunk_parse_splash( &chunkdata[pos], chunkhdr->length, splash );
+					mxit_chunk_parse_splash( &chunkdata[pos], chunk_length( chunk ), splash );
 
 					cr->resources = g_list_append( cr->resources, splash );
 					break;
@@ -600,12 +599,12 @@
 					break;
 				}
 			default:
-				purple_debug_info( MXIT_PLUGIN_ID, "Unsupported custom resource chunk received (%i)\n", chunkhdr->type );
+				purple_debug_info( MXIT_PLUGIN_ID, "Unsupported custom resource chunk received (%i)\n", chunk_type( chunk) );
 		}
 
 		/* skip over data to next resource chunk */
-		pos += chunkhdr->length;
-		chunklen -= ( sizeof( struct raw_chunk ) + chunkhdr->length );
+		pos += chunk_length( chunk );
+		chunklen -= ( MXIT_CHUNK_HEADER_SIZE + chunk_length( chunk ) );
 	}
 }
 
--- a/libpurple/protocols/mxit/chunk.h	Tue Nov 24 11:50:55 2009 +0000
+++ b/libpurple/protocols/mxit/chunk.h	Tue Nov 24 13:23:27 2009 +0000
@@ -31,6 +31,8 @@
 
 
 #define		MXIT_CHUNK_FILEID_LEN		8			/* bytes */
+#define		MXIT_CHUNK_HEADER_SIZE		5			/* type (1 byte) + length (4 bytes) */
+
 
 /* Multimedia chunk types */
 #define		CP_CHUNK_NONE				0x00		/* (0) no chunk */
@@ -68,13 +70,35 @@
 #define		REJECT_BAD_RECIPIENT		4
 
 /*
- * a Chunk header
+ * Chunk header manipulation functions
  */
-struct raw_chunk {
-	guint8		type;
-	guint32		length;
-	gchar		data[0];
-} __attribute__ ((packed));
+static inline guint chunk_type( gchar* chunkheader )
+{
+	return *chunkheader;
+}
+
+static inline void set_chunk_type( gchar* chunkheader, guint type )
+{
+	*chunkheader = type;
+}
+
+static inline guint32 chunk_length( gchar* chunkheader )
+{
+	guint32 length = *( (const guint32*) &chunkheader[1] );
+	return htonl( length );
+}
+
+static inline void set_chunk_length( gchar* chunkheader, guint32 size )
+{
+	size = htonl( size );
+	memcpy( &chunkheader[1], &size, sizeof( guint32 ) );
+}
+
+static inline gchar* chunk_data( gchar* chunkheader )
+{
+	return &chunkheader[MXIT_CHUNK_HEADER_SIZE];
+}
+
 
 struct offerfile_chunk {
 	char	fileid[MXIT_CHUNK_FILEID_LEN];
--- a/libpurple/protocols/mxit/mxit.c	Tue Nov 24 11:50:55 2009 +0000
+++ b/libpurple/protocols/mxit/mxit.c	Tue Nov 24 13:23:27 2009 +0000
@@ -689,9 +689,6 @@
 
 	option = purple_account_option_bool_new( _( "Enable splash-screen popup" ), MXIT_CONFIG_SPLASHPOPUP, FALSE );
 	proto_info.protocol_options = g_list_append( proto_info.protocol_options, option );
-
-	if ( sizeof( struct raw_chunk ) != 5 )
-		g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "sizeof(struct raw_chunk) != 5!  MXit probably won't work!\n");
 }
 
 PURPLE_INIT_PLUGIN( mxit, init_plugin, plugin_info );
--- a/libpurple/protocols/mxit/protocol.c	Tue Nov 24 11:50:55 2009 +0000
+++ b/libpurple/protocols/mxit/protocol.c	Tue Nov 24 13:23:27 2009 +0000
@@ -1035,7 +1035,7 @@
 {
 	char				data[CP_MAX_PACKET];
 	int					datalen		= 0;
-	struct raw_chunk*	chunk;
+	gchar*				chunk;
 	int					size;
 
 	purple_debug_info( MXIT_PLUGIN_ID, "SENDING FILE '%s' of %i bytes to user '%s'\n", filename, buflen, username );
@@ -1044,17 +1044,17 @@
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
-
-	size = mxit_chunk_create_senddirect( chunk->data, username, filename, buf, buflen );
+	chunk = &data[datalen];
+
+	size = mxit_chunk_create_senddirect( chunk_data( chunk ), username, filename, buf, buflen );
 	if ( size < 0 ) {
 		purple_debug_error( MXIT_PLUGIN_ID, "Error creating senddirect chunk (%i)\n", size );
 		return;
 	}
 
-	chunk->type = CP_CHUNK_DIRECT_SND;
-	chunk->length = htonl( size );
-	datalen += sizeof( struct raw_chunk ) + size;
+	set_chunk_type( chunk, CP_CHUNK_DIRECT_SND );
+	set_chunk_length( chunk, size );
+	datalen += MXIT_CHUNK_HEADER_SIZE + size;
 
 	/* send the byte stream to the mxit server */
 	mxit_queue_packet( session, data, datalen, CP_CMD_MEDIA );
@@ -1071,7 +1071,7 @@
 {
 	char				data[CP_MAX_PACKET];
 	int					datalen		= 0;
-	struct raw_chunk*	chunk;
+	gchar*				chunk;
 	int					size;
 
 	purple_debug_info( MXIT_PLUGIN_ID, "mxit_send_file_reject\n" );
@@ -1080,17 +1080,17 @@
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
-
-	size = mxit_chunk_create_reject( chunk->data, fileid );
+	chunk = &data[datalen];
+
+	size = mxit_chunk_create_reject( chunk_data( chunk ), fileid );
 	if ( size < 0 ) {
 		purple_debug_error( MXIT_PLUGIN_ID, "Error creating reject chunk (%i)\n", size );
 		return;
 	}
 
-	chunk->type = CP_CHUNK_REJECT;
-	chunk->length = htonl( size );
-	datalen += sizeof( struct raw_chunk ) + size;
+	set_chunk_type( chunk, CP_CHUNK_REJECT );
+	set_chunk_length( chunk, size );
+	datalen += MXIT_CHUNK_HEADER_SIZE + size;
 
 	/* send the byte stream to the mxit server */
 	mxit_queue_packet( session, data, datalen, CP_CMD_MEDIA );
@@ -1109,7 +1109,7 @@
 {
 	char				data[CP_MAX_PACKET];
 	int					datalen		= 0;
-	struct raw_chunk*	chunk;
+	gchar*				chunk;
 	int					size;
 
 	purple_debug_info( MXIT_PLUGIN_ID, "mxit_send_file_accept\n" );
@@ -1118,17 +1118,17 @@
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
-
-	size = mxit_chunk_create_get( chunk->data, fileid, filesize, offset );
+	chunk = &data[datalen];
+
+	size = mxit_chunk_create_get( chunk_data(chunk), fileid, filesize, offset );
 	if ( size < 0 ) {
 		purple_debug_error( MXIT_PLUGIN_ID, "Error creating getfile chunk (%i)\n", size );
 		return;
 	}
 
-	chunk->type = CP_CHUNK_GET;
-	chunk->length = htonl( size );
-	datalen += sizeof( struct raw_chunk ) + size;
+	set_chunk_type( chunk, CP_CHUNK_GET );
+	set_chunk_length( chunk, size );
+	datalen += MXIT_CHUNK_HEADER_SIZE + size;
 
 	/* send the byte stream to the mxit server */
 	mxit_queue_packet( session, data, datalen, CP_CMD_MEDIA );
@@ -1145,7 +1145,7 @@
 {
 	char				data[CP_MAX_PACKET];
 	int					datalen		= 0;
-	struct raw_chunk*	chunk;
+	gchar*				chunk;
 	int					size;
 
 	purple_debug_info( MXIT_PLUGIN_ID, "mxit_send_file_received\n" );
@@ -1154,17 +1154,17 @@
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
-
-	size = mxit_chunk_create_received( chunk->data, fileid, status );
+	chunk = &data[datalen];
+
+	size = mxit_chunk_create_received( chunk_data(chunk), fileid, status );
 	if ( size < 0 ) {
 		purple_debug_error( MXIT_PLUGIN_ID, "Error creating received chunk (%i)\n", size );
 		return;
 	}
 
-	chunk->type = CP_CHUNK_RECIEVED;
-	chunk->length = htonl( size );
-	datalen += sizeof( struct raw_chunk ) + size;
+	set_chunk_type( chunk, CP_CHUNK_RECIEVED );
+	set_chunk_length( chunk, size );
+	datalen += MXIT_CHUNK_HEADER_SIZE + size;
 
 	/* send the byte stream to the mxit server */
 	mxit_queue_packet( session, data, datalen, CP_CMD_MEDIA );
@@ -1182,7 +1182,7 @@
 {
 	char				data[CP_MAX_PACKET];
 	int					datalen		= 0;
-	struct raw_chunk*	chunk;
+	gchar*				chunk;
 	int					size;
 
 	purple_debug_info( MXIT_PLUGIN_ID, "mxit_set_avatar: %i bytes\n", avatarlen );
@@ -1191,17 +1191,17 @@
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
-
-	size = mxit_chunk_create_set_avatar( chunk->data, avatar, avatarlen );
+	chunk = &data[datalen];
+
+	size = mxit_chunk_create_set_avatar( chunk_data(chunk), avatar, avatarlen );
 	if ( size < 0 ) {
 		purple_debug_error( MXIT_PLUGIN_ID, "Error creating set avatar chunk (%i)\n", size );
 		return;
 	}
 
-	chunk->type = CP_CHUNK_SET_AVATAR;
-	chunk->length = htonl( size );
-	datalen += sizeof( struct raw_chunk ) + size;
+	set_chunk_type( chunk, CP_CHUNK_SET_AVATAR );
+	set_chunk_length( chunk, size );
+	datalen += MXIT_CHUNK_HEADER_SIZE + size;
 
 	/* send the byte stream to the mxit server */
 	mxit_queue_packet( session, data, datalen, CP_CMD_MEDIA );
@@ -1221,7 +1221,7 @@
 {
 	char				data[CP_MAX_PACKET];
 	int					datalen		= 0;
-	struct raw_chunk*	chunk;
+	gchar*				chunk;
 	int					size;
 
 	purple_debug_info( MXIT_PLUGIN_ID, "mxit_get_avatar: %s\n", mxitId );
@@ -1230,17 +1230,17 @@
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
-
-	size = mxit_chunk_create_get_avatar( chunk->data, mxitId, avatarId, MXIT_AVATAR_SIZE );
+	chunk = &data[datalen];
+
+	size = mxit_chunk_create_get_avatar( chunk_data(chunk), mxitId, avatarId, MXIT_AVATAR_SIZE );
 	if ( size < 0 ) {
 		purple_debug_error( MXIT_PLUGIN_ID, "Error creating get avatar chunk (%i)\n", size );
 		return;
 	}
 
-	chunk->type = CP_CHUNK_GET_AVATAR;
-	chunk->length = htonl( size );
-	datalen += sizeof( struct raw_chunk ) + size;
+	set_chunk_type( chunk, CP_CHUNK_GET_AVATAR );
+	set_chunk_length( chunk, size );
+	datalen += MXIT_CHUNK_HEADER_SIZE + size;
 
 	/* send the byte stream to the mxit server */
 	mxit_queue_packet( session, data, datalen, CP_CMD_MEDIA );