1965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/*
2965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Copyright (c) 1992, 1993, 1994, 1995, 1996
3965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *	The Regents of the University of California.  All rights reserved.
4965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
5965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Redistribution and use in source and binary forms, with or without
6965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * modification, are permitted provided that: (1) source code distributions
7965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * retain the above copyright notice and this paragraph in its entirety, (2)
8965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * distributions including binary code include the above copyright notice and
9965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * this paragraph in its entirety in the documentation or other materials
10965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * provided with the distribution, and (3) all advertising materials mentioning
11965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * features or use of this software display the following acknowledgement:
12965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * ``This product includes software developed by the University of California,
13965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * the University nor the names of its contributors may be used to endorse
15965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * or promote products derived from this software without specific prior
16965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * written permission.
17965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */
21965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
22965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#ifndef _WIN32
23965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#include <arpa/inet.h>
24965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#endif
25965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
26965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/*
27965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Macros to extract possibly-unaligned big-endian integral values.
28965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */
29965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#ifdef LBL_ALIGN
30965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/*
31965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * The processor doesn't natively handle unaligned loads.
32965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */
33965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#if defined(__GNUC__) && defined(HAVE___ATTRIBUTE__) && \
34965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes    (defined(__alpha) || defined(__alpha__) || \
35965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes     defined(__mips) || defined(__mips__))
36965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
37965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/*
38965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * This is a GCC-compatible compiler and we have __attribute__, which
39965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * we assume that mean we have __attribute__((packed)), and this is
40965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * MIPS or Alpha, which has instructions that can help when doing
41965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * unaligned loads.
42965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
43965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Declare packed structures containing a uint16_t and a uint32_t,
44965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * cast the pointer to point to one of those, and fetch through it;
45965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * the GCC manual doesn't appear to explicitly say that
46965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * __attribute__((packed)) causes the compiler to generate unaligned-safe
47965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * code, but it apppears to do so.
48965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
49965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * We do this in case the compiler can generate code using those
50965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * instructions to do an unaligned load and pass stuff to "ntohs()" or
51965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * "ntohl()", which might be better than than the code to fetch the
52965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * bytes one at a time and assemble them.  (That might not be the
53965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * case on a little-endian platform, such as DEC's MIPS machines and
54965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Alpha machines, where "ntohs()" and "ntohl()" might not be done
55965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * inline.)
56965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
57965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * We do this only for specific architectures because, for example,
58965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * at least some versions of GCC, when compiling for 64-bit SPARC,
59965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * generate code that assumes alignment if we do this.
60965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
61965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * XXX - add other architectures and compilers as possible and
62965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * appropriate.
63965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
64965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * HP's C compiler, indicated by __HP_cc being defined, supports
65965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * "#pragma unaligned N" in version A.05.50 and later, where "N"
66965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * specifies a number of bytes at which the typedef on the next
67965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * line is aligned, e.g.
68965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
69965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *	#pragma unalign 1
70965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *	typedef uint16_t unaligned_uint16_t;
71965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
72965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * to define unaligned_uint16_t as a 16-bit unaligned data type.
73965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * This could be presumably used, in sufficiently recent versions of
74965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * the compiler, with macros similar to those below.  This would be
75965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * useful only if that compiler could generate better code for PA-RISC
76965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * or Itanium than would be generated by a bunch of shifts-and-ORs.
77965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
78965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * DEC C, indicated by __DECC being defined, has, at least on Alpha,
79965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * an __unaligned qualifier that can be applied to pointers to get the
80965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * compiler to generate code that does unaligned loads and stores when
81965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * dereferencing the pointer in question.
82965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes *
83965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * XXX - what if the native C compiler doesn't support
84965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * __attribute__((packed))?  How can we get it to generate unaligned
85965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * accesses for *specific* items?
86965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */
87965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughestypedef struct {
88965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	uint16_t	val;
89965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes} __attribute__((packed)) unaligned_uint16_t;
90965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
91965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughestypedef struct {
92965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	uint32_t	val;
93965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes} __attribute__((packed)) unaligned_uint32_t;
94965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
95965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic inline uint16_t
96965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott HughesEXTRACT_16BITS(const void *p)
97965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes{
98965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	return ((uint16_t)ntohs(((const unaligned_uint16_t *)(p))->val));
99965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes}
100965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
101965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic inline uint32_t
102965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott HughesEXTRACT_32BITS(const void *p)
103965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes{
104965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	return ((uint32_t)ntohl(((const unaligned_uint32_t *)(p))->val));
105965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes}
106965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
107965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic inline uint64_t
108965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott HughesEXTRACT_64BITS(const void *p)
109965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes{
110965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	return ((uint64_t)(((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 0)->val)) << 32 | \
111965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes		((uint64_t)ntohl(((const unaligned_uint32_t *)(p) + 1)->val)) << 0));
112965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes}
113965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
114965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#else /* have to do it a byte at a time */
115965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/*
116965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * This isn't a GCC-compatible compiler, we don't have __attribute__,
117965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * or we do but we don't know of any better way with this instruction
118965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * set to do unaligned loads, so do unaligned loads of big-endian
119965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * quantities the hard way - fetch the bytes one at a time and
120965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * assemble them.
121965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */
122965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_16BITS(p) \
123965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
124965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
125965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_32BITS(p) \
126965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
127965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
128965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
129965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
130965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_64BITS(p) \
131965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 56) | \
132965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 48) | \
133965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 40) | \
134965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 32) | \
135965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 24) | \
136965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 16) | \
137965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 8) | \
138965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 7)) << 0)))
139965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#endif /* must special-case unaligned accesses */
140965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#else /* LBL_ALIGN */
141965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/*
142965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * The processor natively handles unaligned loads, so we can just
143965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * cast the pointer and fetch through it.
144965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */
145965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic inline uint16_t
146965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott HughesEXTRACT_16BITS(const void *p)
147965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes{
148965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	return ((uint16_t)ntohs(*(const uint16_t *)(p)));
149965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes}
150965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
151965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic inline uint32_t
152965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott HughesEXTRACT_32BITS(const void *p)
153965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes{
154965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	return ((uint32_t)ntohl(*(const uint32_t *)(p)));
155965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes}
156965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
157965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic inline uint64_t
158965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott HughesEXTRACT_64BITS(const void *p)
159965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes{
160965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	return ((uint64_t)(((uint64_t)ntohl(*((const uint32_t *)(p) + 0))) << 32 | \
161965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes		((uint64_t)ntohl(*((const uint32_t *)(p) + 1))) << 0));
162965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
163965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes}
164965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
165965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#endif /* LBL_ALIGN */
166965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
167965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_24BITS(p) \
168965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 16) | \
169965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
170965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 0)))
171965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
172965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_40BITS(p) \
173965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 32) | \
174965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 24) | \
175965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
176965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 8) | \
177965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 0)))
178965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
179965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_48BITS(p) \
180965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 40) | \
181965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 32) | \
182965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 24) | \
183965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 16) | \
184965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 8) | \
185965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 0)))
186965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
187965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_56BITS(p) \
188965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 0)) << 48) | \
189965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 40) | \
190965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 32) | \
191965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
192965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 16) | \
193965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 8) | \
194965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 0)))
195965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes
196965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/*
197965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Macros to extract possibly-unaligned little-endian integral values.
198965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * XXX - do loads on little-endian machines that support unaligned loads?
199965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */
200965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_LE_8BITS(p) (*(p))
201965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_LE_16BITS(p) \
202965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 1)) << 8) | \
203965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint16_t)(*((const uint8_t *)(p) + 0)) << 0)))
204965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_LE_32BITS(p) \
205965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 3)) << 24) | \
206965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
207965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
208965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
209965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_LE_24BITS(p) \
210965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 2)) << 16) | \
211965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 1)) << 8) | \
212965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint32_t)(*((const uint8_t *)(p) + 0)) << 0)))
213965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#define EXTRACT_LE_64BITS(p) \
214965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	((uint64_t)(((uint64_t)(*((const uint8_t *)(p) + 7)) << 56) | \
215965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 6)) << 48) | \
216965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 5)) << 40) | \
217965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 4)) << 32) | \
218965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 3)) << 24) | \
219965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 2)) << 16) | \
220965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 1)) << 8) | \
221965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes	            ((uint64_t)(*((const uint8_t *)(p) + 0)) << 0)))
222