Wireshark-dev: Re: [Wireshark-dev] R: Problems encountered on Windows building from a "source t
From: Loris Degioanni <loris.degioanni@xxxxxxxxx>
Date: Sat, 30 Dec 2006 08:12:45 -0800
Guy Harris wrote:

Davide Schiera wrote:

The structures are used to wrap the header of a 802.11 packet.
The base format of these structures are
	UCHAR	fc[2];
	UCHAR	dur[2];
	UCHAR	addr1[AIRPDCAP_MAC_LEN];	// addr1[6]
	UCHAR	addr2[AIRPDCAP_MAC_LEN];
	UCHAR	addr3[AIRPDCAP_MAC_LEN];
	UCHAR	seq[2];
If any compiler align each field at the desired position (fc:0, dur:2,
addr1:4, ...) the #pragma pack is definitely unneeded. If not, there is
another way to do the same thing?

If the alignment were at the desired position, it would be because the compiler added extra padding between the fields. Alignment is implementation-defined in C89, but I suspect the chances that a compiler would add padding between arrays of 1-byte quantities are less than the chances that a compiler would support "#pragma pack" in that particular fashion, so leaving the "#pragma pack" out is probably safer.

I've consistently seen both VC and gcc putting padding between values in an array, especially in a case like this one: there's good chance that dur will be at byte 4 instead of 2. To avoid the problem, I use #pragma pack with VC, and __attribute__((__packed__)) with GCC

typedef struct _foo
{
  char first;
  char second;
}__attribute__((__packed__))
foo;

With the right #ifdefs, you can easily make the thing portable.

Loris


(UCHAR expands to guchar, which is ultimately typedeffed to "unsigned char"; the chances that a compiler for a platform supporting Wirehark doesn't implement "unsigned char" as an 8-bit byte is probably also less than the chances that a compiler doesn't support "#pragma pack" in that particular fashion.)
_______________________________________________
Wireshark-dev mailing list
Wireshark-dev@xxxxxxxxxxxxx
http://www.wireshark.org/mailman/listinfo/wireshark-dev