diff options
Diffstat (limited to 'plugins/MirOTR/Libgcrypt/cipher/bithelp.h')
-rw-r--r-- | plugins/MirOTR/Libgcrypt/cipher/bithelp.h | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/plugins/MirOTR/Libgcrypt/cipher/bithelp.h b/plugins/MirOTR/Libgcrypt/cipher/bithelp.h index 1505324330..6e59c53fdb 100644 --- a/plugins/MirOTR/Libgcrypt/cipher/bithelp.h +++ b/plugins/MirOTR/Libgcrypt/cipher/bithelp.h @@ -20,35 +20,61 @@ #ifndef G10_BITHELP_H #define G10_BITHELP_H +#include "types.h" + /**************** * Rotate the 32 bit unsigned integer X by N bits left/right */ -#if defined(__GNUC__) && defined(__i386__) -static inline u32 -rol( u32 x, int n) +static inline u32 rol(u32 x, int n) { - __asm__("roll %%cl,%0" - :"=r" (x) - :"0" (x),"c" (n)); - return x; + return ( (x << (n&(32-1))) | (x >> ((32-n)&(32-1))) ); } + +static inline u32 ror(u32 x, int n) +{ + return ( (x >> (n&(32-1))) | (x << ((32-n)&(32-1))) ); +} + +/* Byte swap for 32-bit and 64-bit integers. If available, use compiler + provided helpers. */ +#ifdef HAVE_BUILTIN_BSWAP32 +# define _gcry_bswap32 __builtin_bswap32 #else -#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) ) +static inline u32 +_gcry_bswap32(u32 x) +{ + return ((rol(x, 8) & 0x00ff00ffL) | (ror(x, 8) & 0xff00ff00L)); +} #endif -#if defined(__GNUC__) && defined(__i386__) -static inline u32 -ror(u32 x, int n) +#ifdef HAVE_U64_TYPEDEF +# ifdef HAVE_BUILTIN_BSWAP64 +# define _gcry_bswap64 __builtin_bswap64 +# else +static inline u64 +_gcry_bswap64(u64 x) { - __asm__("rorl %%cl,%0" - :"=r" (x) - :"0" (x),"c" (n)); - return x; + return ((u64)_gcry_bswap32(x) << 32) | (_gcry_bswap32(x >> 32)); } -#else -#define ror(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) ) +# endif #endif +/* Endian dependent byte swap operations. */ +#ifdef WORDS_BIGENDIAN +# define le_bswap32(x) _gcry_bswap32(x) +# define be_bswap32(x) ((u32)(x)) +# ifdef HAVE_U64_TYPEDEF +# define le_bswap64(x) _gcry_bswap64(x) +# define be_bswap64(x) ((u64)(x)) +# endif +#else +# define le_bswap32(x) ((u32)(x)) +# define be_bswap32(x) _gcry_bswap32(x) +# ifdef HAVE_U64_TYPEDEF +# define le_bswap64(x) ((u64)(x)) +# define be_bswap64(x) _gcry_bswap64(x) +# endif +#endif #endif /*G10_BITHELP_H*/ |