Index: channels/iax2-parser.c =================================================================== RCS file: /usr/cvsroot/asterisk/channels/iax2-parser.c,v retrieving revision 1.38 diff -u -r1.38 iax2-parser.c --- channels/iax2-parser.c 17 Mar 2005 23:12:15 -0000 1.38 +++ channels/iax2-parser.c 26 Mar 2005 23:42:53 -0000 @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -30,22 +31,6 @@ static int iframes = 0; static int oframes = 0; -#if defined(SOLARIS) && defined(__sparc__) -static unsigned int get_uint32(unsigned char *p) -{ - return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; -} - -static unsigned short get_uint16(unsigned char *p) -{ - return (p[0] << 8) | p[1] ; -} - -#else -#define get_uint32(p) (*((unsigned int *)(p))) -#define get_uint16(p) (*((unsigned short *)(p))) -#endif - static void internaloutput(const char *str) { fputs(str, stdout); @@ -102,7 +87,7 @@ static void dump_int(char *output, int maxlen, void *value, int len) { if (len == (int)sizeof(unsigned int)) - snprintf(output, maxlen, "%lu", (unsigned long)ntohl(get_uint32(value))); + snprintf(output, maxlen, "%lu", (unsigned long)ntohl(get_unaligned_uint32(value))); else snprintf(output, maxlen, "Invalid INT"); } @@ -110,7 +95,7 @@ static void dump_short(char *output, int maxlen, void *value, int len) { if (len == (int)sizeof(unsigned short)) - snprintf(output, maxlen, "%d", ntohs(get_uint16(value))); + snprintf(output, maxlen, "%d", ntohs(get_unaligned_uint16(value))); else snprintf(output, maxlen, "Invalid SHORT"); } @@ -140,8 +125,8 @@ { char buf[256] = ""; if (len == (int)sizeof(unsigned int)) - snprintf(output, maxlen, "%lu (%s)", (unsigned long)ntohl(get_uint32(value)), - iax_provflags2str(buf, sizeof(buf), ntohl(get_uint32(value)))); + snprintf(output, maxlen, "%lu (%s)", (unsigned long)ntohl(get_unaligned_uint32(value)), + iax_provflags2str(buf, sizeof(buf), ntohl(get_unaligned_uint32(value)))); else snprintf(output, maxlen, "Invalid INT"); } @@ -600,14 +585,14 @@ snprintf(tmp, (int)sizeof(tmp), "Expecting capability to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else - ies->capability = ntohl(get_uint32(data + 2)); + ies->capability = ntohl(get_unaligned_uint32(data + 2)); break; case IAX_IE_FORMAT: if (len != (int)sizeof(unsigned int)) { snprintf(tmp, (int)sizeof(tmp), "Expecting format to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else - ies->format = ntohl(get_uint32(data + 2)); + ies->format = ntohl(get_unaligned_uint32(data + 2)); break; case IAX_IE_LANGUAGE: ies->language = data + 2; @@ -617,21 +602,21 @@ snprintf(tmp, (int)sizeof(tmp), "Expecting version to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->version = ntohs(get_uint16(data + 2)); + ies->version = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_ADSICPE: if (len != (int)sizeof(unsigned short)) { snprintf(tmp, (int)sizeof(tmp), "Expecting adsicpe to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->adsicpe = ntohs(get_uint16(data + 2)); + ies->adsicpe = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_SAMPLINGRATE: if (len != (int)sizeof(unsigned short)) { snprintf(tmp, (int)sizeof(tmp), "Expecting samplingrate to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->samprate = ntohs(get_uint16(data + 2)); + ies->samprate = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_DNID: ies->dnid = data + 2; @@ -644,14 +629,14 @@ snprintf(tmp, (int)sizeof(tmp), "Expecting authmethods to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->authmethods = ntohs(get_uint16(data + 2)); + ies->authmethods = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_ENCRYPTION: if (len != (int)sizeof(unsigned short)) { snprintf(tmp, (int)sizeof(tmp), "Expecting encryption to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->encmethods = ntohs(get_uint16(data + 2)); + ies->encmethods = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_CHALLENGE: ies->challenge = data + 2; @@ -670,21 +655,21 @@ snprintf(tmp, (int)sizeof(tmp), "Expecting refresh to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->refresh = ntohs(get_uint16(data + 2)); + ies->refresh = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_DPSTATUS: if (len != (int)sizeof(unsigned short)) { snprintf(tmp, (int)sizeof(tmp), "Expecting dpstatus to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->dpstatus = ntohs(get_uint16(data + 2)); + ies->dpstatus = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_CALLNO: if (len != (int)sizeof(unsigned short)) { snprintf(tmp, (int)sizeof(tmp), "Expecting callno to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->callno = ntohs(get_uint16(data + 2)); + ies->callno = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_CAUSE: ies->cause = data + 2; @@ -710,7 +695,7 @@ snprintf(tmp, (int)sizeof(tmp), "Expecting msgcount to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->msgcount = ntohs(get_uint16(data + 2)); + ies->msgcount = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_AUTOANSWER: ies->autoanswer = 1; @@ -723,21 +708,21 @@ snprintf(tmp, (int)sizeof(tmp), "Expecting transferid to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else - ies->transferid = ntohl(get_uint32(data + 2)); + ies->transferid = ntohl(get_unaligned_uint32(data + 2)); break; case IAX_IE_DATETIME: if (len != (int)sizeof(unsigned int)) { snprintf(tmp, (int)sizeof(tmp), "Expecting date/time to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else - ies->datetime = ntohl(get_uint32(data + 2)); + ies->datetime = ntohl(get_unaligned_uint32(data + 2)); break; case IAX_IE_FIRMWAREVER: if (len != (int)sizeof(unsigned short)) { snprintf(tmp, (int)sizeof(tmp), "Expecting firmwarever to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->firmwarever = ntohs(get_uint16(data + 2)); + ies->firmwarever = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_DEVICETYPE: ies->devicetype = data + 2; @@ -750,7 +735,7 @@ snprintf(tmp, (int)sizeof(tmp), "Expected block desc to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else - ies->fwdesc = ntohl(get_uint32(data + 2)); + ies->fwdesc = ntohl(get_unaligned_uint32(data + 2)); break; case IAX_IE_FWBLOCKDATA: ies->fwdata = data + 2; @@ -766,7 +751,7 @@ errorf(tmp); } else { ies->provverpres = 1; - ies->provver = ntohl(get_uint32(data + 2)); + ies->provver = ntohl(get_unaligned_uint32(data + 2)); } break; case IAX_IE_CALLINGPRES: @@ -790,14 +775,14 @@ snprintf(tmp, (int)sizeof(tmp), "Expecting callingtns to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else - ies->calling_tns = ntohs(get_uint16(data + 2)); + ies->calling_tns = ntohs(get_unaligned_uint16(data + 2)); break; case IAX_IE_RR_JITTER: if (len != (int)sizeof(unsigned int)) { snprintf(tmp, (int)sizeof(tmp), "Expected jitter rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else { - ies->rr_jitter = ntohl(get_uint32(data + 2)); + ies->rr_jitter = ntohl(get_unaligned_uint32(data + 2)); } break; case IAX_IE_RR_LOSS: @@ -805,7 +790,7 @@ snprintf(tmp, (int)sizeof(tmp), "Expected loss rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else { - ies->rr_loss = ntohl(get_uint32(data + 2)); + ies->rr_loss = ntohl(get_unaligned_uint32(data + 2)); } break; case IAX_IE_RR_PKTS: @@ -813,7 +798,7 @@ snprintf(tmp, (int)sizeof(tmp), "Expected packets rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else { - ies->rr_pkts = ntohl(get_uint32(data + 2)); + ies->rr_pkts = ntohl(get_unaligned_uint32(data + 2)); } break; case IAX_IE_RR_DELAY: @@ -821,7 +806,7 @@ snprintf(tmp, (int)sizeof(tmp), "Expected loss rr to be %d bytes long but was %d\n", (int)sizeof(unsigned short), len); errorf(tmp); } else { - ies->rr_delay = ntohs(get_uint16(data + 2)); + ies->rr_delay = ntohs(get_unaligned_uint16(data + 2)); } break; case IAX_IE_RR_DROPPED: @@ -829,7 +814,7 @@ snprintf(tmp, (int)sizeof(tmp), "Expected packets rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else { - ies->rr_dropped = ntohl(get_uint32(data + 2)); + ies->rr_dropped = ntohl(get_unaligned_uint32(data + 2)); } break; case IAX_IE_RR_OOO: @@ -837,7 +822,7 @@ snprintf(tmp, (int)sizeof(tmp), "Expected packets rr to be %d bytes long but was %d\n", (int)sizeof(unsigned int), len); errorf(tmp); } else { - ies->rr_ooo = ntohl(get_uint32(data + 2)); + ies->rr_ooo = ntohl(get_unaligned_uint32(data + 2)); } break; default: --- /dev/null 2005-03-24 19:56:56.172477248 +0000 +++ include/asterisk/unaligned.h 2005-03-26 23:39:08.000000000 +0000 @@ -0,0 +1,95 @@ +/* + * Asterisk -- A telephony toolkit for Linux. + * + * Asterisk internal frame definitions. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer + * + * This program is free software, distributed under the terms of + * the GNU Lesser General Public License. Other components of + * Asterisk are distributed under The GNU General Public License + * only. + */ + +#ifndef _ASTERISK_UNALIGNED_H +#define _ASTERISK_UNALIGNED_H + +#if defined(__cplusplus) || defined(c_plusplus) +extern "C" { +#endif + +#ifdef __GNUC__ +/* If we just tell GCC what's going on, we can trust it to behave optimally */ +static inline unsigned int get_unaligned_uint32(void *p) +{ + struct { unsigned int d; } __attribute__((packed)) *pp = (void *)p; + + return pp->d; +} +static inline unsigned short get_unaligned_uint16(void *p) +{ + struct { unsigned short d; } __attribute__((packed)) *pp = (void *)p; + + return pp->d; +} + +static inline void put_unaligned_uint32(void *p, unsigned int datum) +{ + struct { unsigned int d; } __attribute__((packed)) *pp = (void *)p; + + pp->d = datum; +} + +static inline void put_unaligned_uint16(void *p, unsigned short datum) +{ + struct { unsigned short d; } __attribute__((packed)) *pp = (void *)p; + + pp->d = datum; +} +#elif defined(SOLARIS) && defined(__sparc__) +static inline unsigned int get_unaligned_uint32(void *p) +{ + unsigned char *cp = p; + + return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | cp[3]; +} + +static inline unsigned short get_unaligned_uint16(void *p) +{ + unsigned char *cp = p; + + return (cp[0] << 8) | cp[1] ; +} + +static inline void put_unaligned_uint32(void *p, unsigned int datum) +{ + unsigned char *cp = p; + + cp[0] = datum >> 24; + cp[1] = datum >> 16; + cp[2] = datum >> 8; + cp[3] = datum; +} + +static inline void put_unaligned_uint16(void *p, unsigned int datum) +{ + unsigned char *cp = p; + + cp[0] = datum >> 8; + cp[1] = datum; +} +#else /* Not GCC, not Solaris/SPARC. Assume we can handle direct load/store. */ +#define get_uint32(p) (*((unsigned int *)(p))) +#define get_uint16(p) (*((unsigned short *)(p))) +#define put_uint32(p,d) do { unsigned int *__P = (p); *__P = d; } while(0) +#define put_uint16(p,d) do { unsigned short *__P = (p); *__P = d; } while(0) +#endif + +#if defined(__cplusplus) || defined(c_plusplus) +} +#endif + + +#endif /* _ASTERISK_UNALIGNED_H */