diff options
author | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2016-07-04 16:37:55 +0200 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2016-07-04 16:37:55 +0200 |
commit | 27b0b5723ccbcc92bbcc80dff8fb698b69e74944 (patch) | |
tree | 3b27e6dd17a7d4d00266819545ae97defa11cf81 /nx-X11/programs/Xserver/include | |
parent | d1d859dfdec4b4665e06401383ab71926e4e731c (diff) | |
parent | 286d83260216e8e53db701eed5c785aae1c716bf (diff) | |
download | nx-libs-27b0b5723ccbcc92bbcc80dff8fb698b69e74944.tar.gz nx-libs-27b0b5723ccbcc92bbcc80dff8fb698b69e74944.tar.bz2 nx-libs-27b0b5723ccbcc92bbcc80dff8fb698b69e74944.zip |
Merge branch 'sunweaver-pr/type-safe-swapping' into 3.6.x
Attributes GH PR #167: https://github.com/ArcticaProject/nx-libs/pull/167
Reviewed by: Vadim Troshchinskiy <vadim@qindel.com> -- Mon, 04 Jul 2016 07:31:50 -0700
Diffstat (limited to 'nx-X11/programs/Xserver/include')
-rw-r--r-- | nx-X11/programs/Xserver/include/misc.h | 117 |
1 files changed, 96 insertions, 21 deletions
diff --git a/nx-X11/programs/Xserver/include/misc.h b/nx-X11/programs/Xserver/include/misc.h index d2cadace2..ac19ecc3b 100644 --- a/nx-X11/programs/Xserver/include/misc.h +++ b/nx-X11/programs/Xserver/include/misc.h @@ -277,32 +277,107 @@ version_compare(uint32_t a_major, uint32_t a_minor, #define SwapRestL(stuff) \ SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff)) +#if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) +void __attribute__ ((error("wrong sized variable passed to swap"))) +wrong_size(void); +#else +static inline void +wrong_size(void) +{ +} +#endif + +#if !(defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))) +static inline int +__builtin_constant_p(int x) +{ + return 0; +} +#endif + +/* byte swap a 64-bit value */ +static inline void +swap_uint64(uint64_t *x) +{ + char n; + + n = ((char *) x)[0]; + ((char *) x)[0] = ((char *) x)[7]; + ((char *) x)[7] = n; + + n = ((char *) x)[1]; + ((char *) x)[1] = ((char *) x)[6]; + ((char *) x)[6] = n; + + n = ((char *) x)[2]; + ((char *) x)[2] = ((char *) x)[5]; + ((char *) x)[5] = n; + + n = ((char *) x)[3]; + ((char *) x)[3] = ((char *) x)[4]; + ((char *) x)[4] = n; +} + +#define swapll(x) do { \ + if (sizeof(*(x)) != 8) \ + wrong_size(); \ + swap_uint64((uint64_t *)(x)); \ + } while (0) + /* byte swap a 32-bit value */ -#define swapl(x, n) { \ - n = ((char *) (x))[0];\ - ((char *) (x))[0] = ((char *) (x))[3];\ - ((char *) (x))[3] = n;\ - n = ((char *) (x))[1];\ - ((char *) (x))[1] = ((char *) (x))[2];\ - ((char *) (x))[2] = n; } - -/* byte swap a short */ -#define swaps(x, n) { \ - n = ((char *) (x))[0];\ - ((char *) (x))[0] = ((char *) (x))[1];\ - ((char *) (x))[1] = n; } +static inline void +swap_uint32(uint32_t * x) +{ + char n = ((char *) x)[0]; + + ((char *) x)[0] = ((char *) x)[3]; + ((char *) x)[3] = n; + n = ((char *) x)[1]; + ((char *) x)[1] = ((char *) x)[2]; + ((char *) x)[2] = n; +} + +#define swapl(x) do { \ + if (sizeof(*(x)) != 4) \ + wrong_size(); \ + if (__builtin_constant_p((uintptr_t)(x) & 3) && ((uintptr_t)(x) & 3) == 0) \ + *(x) = lswapl(*(x)); \ + else \ + swap_uint32((uint32_t *)(x)); \ + } while (0) + +/* byte swap a 16-bit value */ +static inline void +swap_uint16(uint16_t * x) +{ + char n = ((char *) x)[0]; + + ((char *) x)[0] = ((char *) x)[1]; + ((char *) x)[1] = n; +} + +#define swaps(x) do { \ + if (sizeof(*(x)) != 2) \ + wrong_size(); \ + if (__builtin_constant_p((uintptr_t)(x) & 1) && ((uintptr_t)(x) & 1) == 0) \ + *(x) = lswaps(*(x)); \ + else \ + swap_uint16((uint16_t *)(x)); \ + } while (0) /* copy 32-bit value from src to dst byteswapping on the way */ -#define cpswapl(src, dst) { \ - ((char *)&(dst))[0] = ((char *) &(src))[3];\ - ((char *)&(dst))[1] = ((char *) &(src))[2];\ - ((char *)&(dst))[2] = ((char *) &(src))[1];\ - ((char *)&(dst))[3] = ((char *) &(src))[0]; } +#define cpswapl(src, dst) do { \ + if (sizeof((src)) != 4 || sizeof((dst)) != 4) \ + wrong_size(); \ + (dst) = lswapl((src)); \ + } while (0) /* copy short from src to dst byteswapping on the way */ -#define cpswaps(src, dst) { \ - ((char *) &(dst))[0] = ((char *) &(src))[1];\ - ((char *) &(dst))[1] = ((char *) &(src))[0]; } +#define cpswaps(src, dst) do { \ + if (sizeof((src)) != 2 || sizeof((dst)) != 2) \ + wrong_size(); \ + (dst) = lswaps((src)); \ + } while (0) extern void SwapLongs( CARD32 *list, |