cochran_emc.h: packed structs

Dirk Hohndel dirk at hohndel.org
Tue Oct 28 09:32:17 PDT 2014


On Tue, Oct 28, 2014 at 08:54:26AM -0700, Thiago Macieira wrote:
> On Tuesday 28 October 2014 17:39:27 Lubomir I. Ivanov wrote:
> > > __declspec(packed)
> > 
> > i don't have that in MSVC 2003; perhaps it's a newer feature.
> 
> Yeah, I was wrong, it's not there.
> 
> http://msdn.microsoft.com/en-us/library/dabb5z75.aspx
> 
> That's probably why the GCC attribute is disabled in MinGW for "MS struct" 
> compatibility. So it's not a bug, it's a feature.
> 
> The suggestion is to not require packing of structs. Reorder the members so 
> you don't need packing, if you can, or try to use different types if you really 
> need the members in that order due to data storage or protocol.

Hm :-(

The reason so many people reverse engineering want packed structures is
that it makes it so much easier to write readable code.

A typical 8 byte sample might look like this:

struct sample {
	unsigned char temp; // in F
	unsigned int16_t depth; // in 1/16 ft
	unsigned int16_t deco : 1;
	unsigned int16_t tank_idx : 2;
	unsigned int16_t marker : 1;
	unsigned int16_t ascend_warn : 1;
	unsigned int16_t compass : 9;
	unsigned int16_t pressure; // in psi
	unsigned char flags;
}

I made this up - it's much simpler than some real structures actually are.

The goal is to allow me to say

if (sample->deco) { ... }

instead of

if (data[3] & 0x01) { ... }

and

show_direction(sample.compass)

instead of 

show_direction((array_16_le(data + 3) >> 5) & 0x1f)

/D


More information about the subsurface mailing list