[Asterisk-Dev] Explicit endianness support.

David Woodhouse dwmw2 at infradead.org
Sat Mar 26 13:58:12 MST 2005


On Sat, 2005-03-26 at 07:48 -0800, Mike Taht wrote:
> I went poking about for other endian issues in the code last night,
> and found a big endian bug in iax2-parser.c. (tested, patch supplied
> below)

AFAICT that's not about endianness, that's _alignment_. It'd bite you on
a little-endian ARM box too. Everywhere get_uint32() is used, it's used
inside ntohl() anyway. Note that it's actually doing a normal big-endian
load, just byte-by-byte:
	return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];

The gcc-specific answer is to trust the compiler to know whether the
machine can load unaligned words or not, and just tell the compiler that
the word in question may be misaligned. Then if it's necessary, the
compiler will emit appropriate code to load it byte-by-byte. 

static uint32_t get_uint32(unsigned char *p)
{
  struct { uint32_t datum } __attribute__((packed)) *p2 = (void *)p;
  return p2->datum;
}

If you're willing to assume GCC, you can use that version
unconditionally. On i386 it'll just do the load because it knows the CPU
is capable of it. On ARM and SPARC etc., it'll do it manually.

-- 
dwmw2




More information about the asterisk-dev mailing list