13aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* This file is generated automatically by configure */
23aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* It is valid only for the system type powerpc-apple-darwin9.8.0 */
33aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
43aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef __BYTEORDER_H
53aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define __BYTEORDER_H
63aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
73aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* ntohl and relatives live here */
83aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#include <arpa/inet.h>
93aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
103aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* Define generic byte swapping functions */
113aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#include <machine/byte_order.h>
123aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define swap16(x) NXSwapShort(x)
133aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define swap32(x) NXSwapLong(x)
143aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define swap64(x) NXSwapLongLong(x)
153aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
163aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* The byte swapping macros have the form: */
173aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/*   EENN[a]toh or htoEENN[a] where EE is be (big endian) or */
183aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* le (little-endian), NN is 16 or 32 (number of bits) and a, */
193aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* if present, indicates that the endian side is a pointer to an */
203aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* array of uint8_t bytes instead of an integer of the specified length. */
213aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* h refers to the host's ordering method. */
223aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
233aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* So, to convert a 32-bit integer stored in a buffer in little-endian */
243aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* format into a uint32_t usable on this machine, you could use: */
253aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/*   uint32_t value = le32atoh(&buf[3]); */
263aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* To put that value back into the buffer, you could use: */
273aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/*   htole32a(&buf[3], value); */
283aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
293aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* Define aliases for the standard byte swapping macros */
303aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* Arguments to these macros must be properly aligned on natural word */
313aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* boundaries in order to work properly on all architectures */
323aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef htobe16
333aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define htobe16(x) htons(x)
343aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
353aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef htobe32
363aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define htobe32(x) htonl(x)
373aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
383aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef be16toh
393aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define be16toh(x) ntohs(x)
403aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
413aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef be32toh
423aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define be32toh(x) ntohl(x)
433aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
443aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
453aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define HTOBE16(x) (x) = htobe16(x)
463aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define HTOBE32(x) (x) = htobe32(x)
473aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define BE32TOH(x) (x) = be32toh(x)
483aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define BE16TOH(x) (x) = be16toh(x)
493aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
503aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* Define our own extended byte swapping macros for big-endian machines */
513aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef htole16
523aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define htole16(x)      swap16(x)
533aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
543aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef htole32
553aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define htole32(x)      swap32(x)
563aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
573aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef le16toh
583aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define le16toh(x)      swap16(x)
593aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
603aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef le32toh
613aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define le32toh(x)      swap32(x)
623aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
633aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef le64toh
643aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define le64toh(x)      swap64(x)
653aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
663aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
673aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef htobe64
683aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define htobe64(x)      (x)
693aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
703aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#ifndef be64toh
713aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev# define be64toh(x)      (x)
723aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif
733aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
743aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define HTOLE16(x)      (x) = htole16(x)
753aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define HTOLE32(x)      (x) = htole32(x)
763aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define LE16TOH(x)      (x) = le16toh(x)
773aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define LE32TOH(x)      (x) = le32toh(x)
783aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define LE64TOH(x)      (x) = le64toh(x)
793aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
803aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define HTOBE64(x)      (void) (x)
813aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define BE64TOH(x)      (void) (x)
823aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
833aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* Define the C99 standard length-specific integer types */
843aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#include <stdint.h>
853aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
863aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* Here are some macros to create integers from a byte array */
873aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* These are used to get and put integers from/into a uint8_t array */
883aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* with a specific endianness.  This is the most portable way to generate */
893aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* and read messages to a network or serial device.  Each member of a */
903aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* packet structure must be handled separately. */
913aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
923aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev/* Non-optimized but portable macros */
933aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define be16atoh(x)     ((uint16_t)(((x)[0]<<8)|(x)[1]))
943aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define be32atoh(x)     ((uint32_t)(((x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]))
953aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define be64atoh_x(x,off,shift) 	(((uint64_t)((x)[off]))<<shift)
963aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define be64atoh(x)     ((uint64_t)(be64atoh_x(x,0,56)|be64atoh_x(x,1,48)|be64atoh_x(x,2,40)| \
973aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        be64atoh_x(x,3,32)|be64atoh_x(x,4,24)|be64atoh_x(x,5,16)|be64atoh_x(x,6,8)|((x)[7])))
983aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define le16atoh(x)     ((uint16_t)(((x)[1]<<8)|(x)[0]))
993aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define le32atoh(x)     ((uint32_t)(((x)[3]<<24)|((x)[2]<<16)|((x)[1]<<8)|(x)[0]))
1003aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define le64atoh_x(x,off,shift) (((uint64_t)(x)[off])<<shift)
1013aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define le64atoh(x)     ((uint64_t)(le64atoh_x(x,7,56)|le64atoh_x(x,6,48)|le64atoh_x(x,5,40)| \
1023aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        le64atoh_x(x,4,32)|le64atoh_x(x,3,24)|le64atoh_x(x,2,16)|le64atoh_x(x,1,8)|((x)[0])))
1033aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
1043aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define htobe16a(a,x)   (a)[0]=(uint8_t)((x)>>8), (a)[1]=(uint8_t)(x)
1053aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define htobe32a(a,x)   (a)[0]=(uint8_t)((x)>>24), (a)[1]=(uint8_t)((x)>>16), \
1063aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[2]=(uint8_t)((x)>>8), (a)[3]=(uint8_t)(x)
1073aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define htobe64a(a,x)   (a)[0]=(uint8_t)((x)>>56), (a)[1]=(uint8_t)((x)>>48), \
1083aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[2]=(uint8_t)((x)>>40), (a)[3]=(uint8_t)((x)>>32), \
1093aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[4]=(uint8_t)((x)>>24), (a)[5]=(uint8_t)((x)>>16), \
1103aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[6]=(uint8_t)((x)>>8), (a)[7]=(uint8_t)(x)
1113aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define htole16a(a,x)   (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
1123aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define htole32a(a,x)   (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
1133aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
1143aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#define htole64a(a,x)   (a)[7]=(uint8_t)((x)>>56), (a)[6]=(uint8_t)((x)>>48), \
1153aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[5]=(uint8_t)((x)>>40), (a)[4]=(uint8_t)((x)>>32), \
1163aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \
1173aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev        (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
1183aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev
1193aa430dc5437a98734b36f996f9b17081a589143Yavor Goulishev#endif /*__BYTEORDER_H*/
120