Skip to content
Snippets Groups Projects
Commit 6a936edb authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add support for non-native endian 64-bit integers

parent c5ad7fc5
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #3138 passed
...@@ -652,7 +652,11 @@ js_readbin(JSContext *cx, uintN argc, jsval *arglist) ...@@ -652,7 +652,11 @@ js_readbin(JSContext *cx, uintN argc, jsval *arglist)
JS_SET_RVAL(cx, arglist, UINT_TO_JSVAL(*l)); JS_SET_RVAL(cx, arglist, UINT_TO_JSVAL(*l));
break; break;
case sizeof(uint64_t): case sizeof(uint64_t):
JS_SET_RVAL(cx, arglist, DOUBLE_TO_JSVAL(*q)); if (p->network_byte_order)
*q = BE_INT64(*q);
else
*q = LE_INT64(*q);
JS_SET_RVAL(cx, arglist, DOUBLE_TO_JSVAL((double)*q));
break; break;
} }
} }
...@@ -681,7 +685,11 @@ js_readbin(JSContext *cx, uintN argc, jsval *arglist) ...@@ -681,7 +685,11 @@ js_readbin(JSContext *cx, uintN argc, jsval *arglist)
v=UINT_TO_JSVAL(*(l++)); v=UINT_TO_JSVAL(*(l++));
break; break;
case sizeof(uint64_t): case sizeof(uint64_t):
v = DOUBLE_TO_JSVAL(*(q++)); if (p->network_byte_order)
*q = BE_INT64(*q);
else
*q = LE_INT64(*q);
v = DOUBLE_TO_JSVAL((double)*(q++));
break; break;
} }
if(!JS_SetElement(cx, array, i, &v)) { if(!JS_SetElement(cx, array, i, &v)) {
...@@ -1936,6 +1944,10 @@ js_writebin(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1936,6 +1944,10 @@ js_writebin(JSContext *cx, uintN argc, jsval *arglist)
*o.sq = (int64_t)val; *o.sq = (int64_t)val;
else else
*o.q = (uint64_t)val; *o.q = (uint64_t)val;
if (p->network_byte_order)
*o.q = BE_INT64(*o.q);
else
*o.q = LE_INT64(*o.q);
break; break;
} }
} }
...@@ -1980,6 +1992,10 @@ js_writebin(JSContext *cx, uintN argc, jsval *arglist) ...@@ -1980,6 +1992,10 @@ js_writebin(JSContext *cx, uintN argc, jsval *arglist)
*o.sq = (int64_t)val; *o.sq = (int64_t)val;
else else
*o.q = (uint64_t)val; *o.q = (uint64_t)val;
if (p->network_byte_order)
*o.q = BE_INT64(*o.q);
else
*o.q = LE_INT64(*o.q);
o.q++; o.q++;
break; break;
} }
......
...@@ -29,9 +29,17 @@ ...@@ -29,9 +29,17 @@
/************************/ /************************/
#define BYTE_SWAP_16(x) ((((WORD)(x)&0xff00)>>8) | (((WORD)(x)&0x00ff)<<8)) #define BYTE_SWAP_16(x) ((((WORD)(x)&0xff00)>>8) | (((WORD)(x)&0x00ff)<<8))
#define BYTE_SWAP_32(x) ((((DWORD)(x)&0xff000000)>>24) | (((DWORD)(x)&0x00ff0000)>>8) | (((DWORD)(x)&0x0000ff00)<<8) | (((DWORD)(x)&0x000000ff)<<24)) #define BYTE_SWAP_32(x) ((((DWORD)(x)&0xff000000)>>24) | (((DWORD)(x)&0x00ff0000)>>8) | (((DWORD)(x)&0x0000ff00)<<8) | (((DWORD)(x)&0x000000ff)<<24))
#define BYTE_SWAP_64(x) ((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
( ((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
( ((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
( ((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
( ((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
( ((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
( ((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
( ((uint64_t)(x) & 0x00000000000000ffULL) << 56))
/* auto-detect integer size */ /* auto-detect integer size */
#define BYTE_SWAP_INT(x) (sizeof(x)==2 ? BYTE_SWAP_16(x) : sizeof(x)==4 ? BYTE_SWAP_32(x) : (x)) #define BYTE_SWAP_INT(x) (sizeof(x)==2 ? BYTE_SWAP_16(x) : sizeof(x)==4 ? BYTE_SWAP_32(x) : sizeof(x)==8 ? BYTE_SWAP_64(x) : (x))
/********************************/ /********************************/
/* Architecture-specific macros */ /* Architecture-specific macros */
...@@ -42,11 +50,13 @@ ...@@ -42,11 +50,13 @@
#define BE_LONG(x) (x) #define BE_LONG(x) (x)
#define BE_INT16(x) (x) #define BE_INT16(x) (x)
#define BE_INT32(x) (x) #define BE_INT32(x) (x)
#define BE_INT64(x) (x)
#define BE_INT(x) (x) #define BE_INT(x) (x)
#define LE_SHORT(x) BYTE_SWAP_16(x) #define LE_SHORT(x) BYTE_SWAP_16(x)
#define LE_LONG(x) BYTE_SWAP_32(x) #define LE_LONG(x) BYTE_SWAP_32(x)
#define LE_INT16(x) BYTE_SWAP_16(x) #define LE_INT16(x) BYTE_SWAP_16(x)
#define LE_INT32(x) BYTE_SWAP_32(x) #define LE_INT32(x) BYTE_SWAP_32(x)
#define LE_INT64(x) BYTE_SWAP_64(x)
#define LE_INT(x) BYTE_SWAP_INT(x) #define LE_INT(x) BYTE_SWAP_INT(x)
#else /* Little Endian (e.g. Intel) */ #else /* Little Endian (e.g. Intel) */
...@@ -55,11 +65,13 @@ ...@@ -55,11 +65,13 @@
#define LE_LONG(x) (x) #define LE_LONG(x) (x)
#define LE_INT16(x) (x) #define LE_INT16(x) (x)
#define LE_INT32(x) (x) #define LE_INT32(x) (x)
#define LE_INT64(x) (x)
#define LE_INT(x) (x) #define LE_INT(x) (x)
#define BE_SHORT(x) BYTE_SWAP_16(x) #define BE_SHORT(x) BYTE_SWAP_16(x)
#define BE_LONG(x) BYTE_SWAP_32(x) #define BE_LONG(x) BYTE_SWAP_32(x)
#define BE_INT16(x) BYTE_SWAP_16(x) #define BE_INT16(x) BYTE_SWAP_16(x)
#define BE_INT32(x) BYTE_SWAP_32(x) #define BE_INT32(x) BYTE_SWAP_32(x)
#define BE_INT64(x) BYTE_SWAP_64(x)
#define BE_INT(x) BYTE_SWAP_INT(x) #define BE_INT(x) BYTE_SWAP_INT(x)
#endif #endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment