pidgin.mxit: c2a65b2e: The "packed" attribute on the raw_chunk ...

andrew.victor at mxit.com andrew.victor at mxit.com
Tue Nov 24 08:25:31 EST 2009


-----------------------------------------------------------------
Revision: c2a65b2e8127f46f7c79c0bac6eaac81365319d1
Ancestor: 1dd6343ce54e8fa78a72d88c53fc368d5c6e2949
Author: andrew.victor at mxit.com
Date: 2009-11-24T13:23:27
Branch: im.pidgin.pidgin.mxit
URL: http://d.pidgin.im/viewmtn/revision/info/c2a65b2e8127f46f7c79c0bac6eaac81365319d1

Modified files:
        libpurple/protocols/mxit/chunk.c
        libpurple/protocols/mxit/chunk.h
        libpurple/protocols/mxit/mxit.c
        libpurple/protocols/mxit/protocol.c

ChangeLog: 

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.


-------------- next part --------------
============================================================
--- libpurple/protocols/mxit/chunk.c	33356d4782b08c16eca8230508bdc1218ad681a9
+++ libpurple/protocols/mxit/chunk.c	64c5798193c2d2d0e03799c80e7a733d9d8eff22
@@ -576,18 +576,17 @@ void mxit_chunk_parse_cr( char* chunkdat
 
 	/* 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 @@ void mxit_chunk_parse_cr( char* chunkdat
 					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 ) );
 	}
 }
 
============================================================
--- libpurple/protocols/mxit/chunk.h	fac26a86acafffb69ff84c8d39f8ed65de33eed0
+++ libpurple/protocols/mxit/chunk.h	165ec1fe03510c157204cb85723f6d5787074659
@@ -31,7 +31,9 @@
 
 
 #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 */
 #define		CP_CHUNK_CUSTOM				0x01		/* (1) custom resource */
@@ -68,14 +70,36 @@
 #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];
 	char	username[MXIT_CP_MAX_JID_LEN + 1];
============================================================
--- libpurple/protocols/mxit/mxit.c	dcefb6a3fa94eb2636fb6e32016dd6d2692355f1
+++ libpurple/protocols/mxit/mxit.c	c416cfc4a0be9ea91d5b55e9bb12483082f2bdf6
@@ -689,9 +689,6 @@ static void init_plugin( PurplePlugin* p
 
 	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 );
============================================================
--- libpurple/protocols/mxit/protocol.c	d9b30fea2f51965d79b98fd2f49c28732cd3c42f
+++ libpurple/protocols/mxit/protocol.c	db1083453d8cf0da0b4884e8ff8a7713da386e4e
@@ -1035,7 +1035,7 @@ void mxit_send_file( struct MXitSession*
 {
 	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 @@ void mxit_send_file( struct MXitSession*
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
+	chunk = &data[datalen];
 
-	size = mxit_chunk_create_senddirect( chunk->data, username, filename, buf, buflen );
+	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 @@ void mxit_send_file_reject( struct MXitS
 {
 	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 @@ void mxit_send_file_reject( struct MXitS
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
+	chunk = &data[datalen];
 
-	size = mxit_chunk_create_reject( chunk->data, fileid );
+	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 @@ void mxit_send_file_accept( struct MXitS
 {
 	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 @@ void mxit_send_file_accept( struct MXitS
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
+	chunk = &data[datalen];
 
-	size = mxit_chunk_create_get( chunk->data, fileid, filesize, offset );
+	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 @@ void mxit_send_file_received( struct MXi
 {
 	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 @@ void mxit_send_file_received( struct MXi
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
+	chunk = &data[datalen];
 
-	size = mxit_chunk_create_received( chunk->data, fileid, status );
+	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 @@ void mxit_set_avatar( struct MXitSession
 {
 	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 @@ void mxit_set_avatar( struct MXitSession
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
+	chunk = &data[datalen];
 
-	size = mxit_chunk_create_set_avatar( chunk->data, avatar, avatarlen );
+	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 @@ void mxit_get_avatar( struct MXitSession
 {
 	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 @@ void mxit_get_avatar( struct MXitSession
 	datalen = sprintf( data, "ms=" );
 
 	/* map chunk header over data buffer */
-	chunk = (struct raw_chunk *) &data[datalen];
+	chunk = &data[datalen];
 
-	size = mxit_chunk_create_get_avatar( chunk->data, mxitId, avatarId, MXIT_AVATAR_SIZE );
+	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 );


More information about the Commits mailing list