From c0efacf6d17f17b37073965e1c91c782b86d2738 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Thu, 13 Dec 2012 05:21:56 +0000 Subject: fix float->int32_t and int32_t->float type-punning svn path=/branches/pd-extended/0.43/externals/oscx/; revision=16678 --- OSC-client.c | 32 ++++++++++++++++---------------- OSC-client.h | 25 +++++++++++++++---------- dumpOSC.c | 14 ++++++++++---- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/OSC-client.c b/OSC-client.c index 73bc1a1..e68b6f4 100644 --- a/OSC-client.c +++ b/OSC-client.c @@ -138,7 +138,7 @@ int OSC_packetSize(OSCbuf *buf) { } static void PatchMessageSize(OSCbuf *buf) { - int4byte size; + int32_t size; size = buf->bufptr - ((char *) buf->thisMsgSize) - 4; *(buf->thisMsgSize) = htonl(size); } @@ -172,8 +172,8 @@ int OSC_openBundle(OSCbuf *buf, OSCTimeTag tt) { /* This bundle is inside another bundle, so we need to leave a blank size count for the size of this current bundle. */ CheckOverflow(buf, 20); - *((int4byte *)buf->bufptr) = 0xaaaaaaaa; - buf->prevCounts[buf->bundleDepth] = (int4byte *)buf->bufptr; + *((int32_t *)buf->bufptr) = 0xaaaaaaaa; + buf->prevCounts[buf->bundleDepth] = (int32_t *)buf->bufptr; buf->bufptr += 4; } @@ -185,7 +185,7 @@ int OSC_openBundle(OSCbuf *buf, OSCTimeTag tt) { if (htonl(1) != 1) { /* Byte swap the 8-byte integer time tag */ - int4byte *intp = (int4byte *)buf->bufptr; + int32_t *intp = (int32_t *)buf->bufptr; intp[0] = htonl(intp[0]); intp[1] = htonl(intp[1]); @@ -194,7 +194,7 @@ int OSC_openBundle(OSCbuf *buf, OSCTimeTag tt) { (Otherwise tt is a struct of two 32-bit words, and even though each word was wrong-endian, they were in the right order in the struct.) */ - int4byte temp = intp[0]; + int32_t temp = intp[0]; intp[0] = intp[1]; intp[1] = temp; } @@ -258,7 +258,7 @@ int OSC_closeAllBundles(OSCbuf *buf) { } int OSC_writeAddress(OSCbuf *buf, char *name) { - int4byte paddedLength; + int32_t paddedLength; if (buf->state == ONE_MSG_ARGS) { OSC_errorMessage = "This packet is not a bundle, so you can't write another address"; @@ -285,7 +285,7 @@ int OSC_writeAddress(OSCbuf *buf, char *name) { /* Close the old message */ PatchMessageSize(buf); } - buf->thisMsgSize = (int4byte *)buf->bufptr; + buf->thisMsgSize = (int32_t *)buf->bufptr; *(buf->thisMsgSize) = 0xbbbbbbbb; buf->bufptr += 4; buf->state = GET_ARGS; @@ -301,7 +301,7 @@ int OSC_writeAddress(OSCbuf *buf, char *name) { int OSC_writeAddressAndTypes(OSCbuf *buf, char *name, char *types) { int result; - int4byte paddedLength; + int32_t paddedLength; if (CheckTypeTag(buf, '\0')) return 9; @@ -343,7 +343,7 @@ static int CheckTypeTag(OSCbuf *buf, char expectedType) { int OSC_writeFloatArg(OSCbuf *buf, float arg) { - int4byte *intp; + ls_pcast32 *pc; //int result; CheckOverflow(buf, 4); @@ -351,8 +351,8 @@ int OSC_writeFloatArg(OSCbuf *buf, float arg) { if (CheckTypeTag(buf, 'f')) return 9; /* Pretend arg is a long int so we can use htonl() */ - intp = ((int4byte *) &arg); - *((int4byte *) buf->bufptr) = htonl(*intp); + pc = (ls_pcast32 *)&arg; + *((int32_t *) buf->bufptr) = htonl((*pc).i); buf->bufptr += 4; @@ -364,16 +364,16 @@ int OSC_writeFloatArg(OSCbuf *buf, float arg) { int OSC_writeFloatArgs(OSCbuf *buf, int numFloats, float *args) { int i; - int4byte *intp; + int32_t *intp; CheckOverflow(buf, 4 * numFloats); /* Pretend args are long ints so we can use htonl() */ - intp = ((int4byte *) args); + intp = ((int32_t *) args); for (i = 0; i < numFloats; i++) { if (CheckTypeTag(buf, 'f')) return 9; - *((int4byte *) buf->bufptr) = htonl(intp[i]); + *((int32_t *) buf->bufptr) = htonl(intp[i]); buf->bufptr += 4; } @@ -381,11 +381,11 @@ int OSC_writeFloatArgs(OSCbuf *buf, int numFloats, float *args) { return 0; } -int OSC_writeIntArg(OSCbuf *buf, int4byte arg) { +int OSC_writeIntArg(OSCbuf *buf, int32_t arg) { CheckOverflow(buf, 4); if (CheckTypeTag(buf, 'i')) return 9; - *((int4byte *) buf->bufptr) = htonl(arg); + *((int32_t *) buf->bufptr) = htonl(arg); buf->bufptr += 4; buf->gettingFirstUntypedArg = 0; diff --git a/OSC-client.h b/OSC-client.h index 1be5531..ca104c6 100644 --- a/OSC-client.h +++ b/OSC-client.h @@ -54,16 +54,21 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. #include "OSC-timetag.h" -/* The int4byte type has to be a 4-byte integer. You may have to - change this to long or something else on your system. */ -#ifdef __MWERKS__ - /* In Metrowerks you can set ints to be 2 or 4 bytes on 68K, but long is - always 4 bytes */ - typedef long int4byte; +#ifdef _MSC_VER +/* Microsoft Visual Studio is not C99, it does not provide stdint.h */ +typedef signed __int32 int32_t; +#elif defined(IRIX) +typedef long int32_t; /* a data type that has 32 bits */ #else - typedef int int4byte; +# include #endif +/* 32 bit "pointer cast" union */ +typedef union { + float f; + int32_t i; +} ls_pcast32; + /* The maximum depth of bundles within bundles within bundles within... This is the size of a static array. If you exceed this limit you'll get an error message. */ @@ -80,9 +85,9 @@ typedef struct OSCbuf_struct { int size; /* Size of the buffer */ char *bufptr; /* Current position as we fill the buffer */ int state; /* State of partially-constructed message */ - int4byte *thisMsgSize; /* Pointer to count field before + int32_t *thisMsgSize; /* Pointer to count field before currently-being-written message */ - int4byte *prevCounts[MAX_BUNDLE_NESTING]; + int32_t *prevCounts[MAX_BUNDLE_NESTING]; /* Pointers to count field before each currently open bundle */ int bundleDepth; /* How many sub-sub-bundles are we in now? */ @@ -165,7 +170,7 @@ int OSC_writeAddress(OSCbuf *buf, char *name); int OSC_writeAddressAndTypes(OSCbuf *buf, char *name, char *types); int OSC_writeFloatArg(OSCbuf *buf, float arg); int OSC_writeFloatArgs(OSCbuf *buf, int numFloats, float *args); -int OSC_writeIntArg(OSCbuf *buf, int4byte arg); +int OSC_writeIntArg(OSCbuf *buf, int32_t arg); int OSC_writeStringArg(OSCbuf *buf, char *arg); extern char *OSC_errorMessage; diff --git a/dumpOSC.c b/dumpOSC.c index 6fc4da6..cf90da5 100644 --- a/dumpOSC.c +++ b/dumpOSC.c @@ -156,6 +156,12 @@ static int unixinitudp(int chan); #endif +/* 32 bit "pointer cast" union */ +typedef union { + float f; + long i; +} ls_pcast32; + static int initudp(int chan); static void closeudp(int sockfd); @@ -810,12 +816,12 @@ static void dumpOSC_PrintTypeTaggedArgs(t_dumpOSC *x, void *v, int n) { break; case 'f': { - int i = ntohl(*((int *) p)); - t_float *floatp = ((t_float *) (&i)); + long i = ntohl(*((long *) p)); + ls_pcast32 *pc = (ls_pcast32 *)(&i); #ifdef DEBUG - post("float: %f", *floatp); + post("float: %f", (*pc).f); #endif - SETFLOAT(mya+myargc,*floatp); + SETFLOAT(mya+myargc, (*pc).f); myargc++; p += 4; -- cgit v1.2.1