diff options
author | Steve Holme <steve_holme@hotmail.com> | 2015-01-01 20:26:17 +0000 |
---|---|---|
committer | Steve Holme <steve_holme@hotmail.com> | 2015-01-01 20:45:29 +0000 |
commit | 4bb5a351b2335704c07e3b860a85612058e21e0e (patch) | |
tree | 0787cf8dceec370602050a1c5f042f429aac630c /lib | |
parent | 43792592cae9e380c317691904d0c8a7cec370d1 (diff) |
endian: Added big endian read functions
Diffstat (limited to 'lib')
-rw-r--r-- | lib/curl_endian.c | 73 | ||||
-rw-r--r-- | lib/curl_endian.h | 15 |
2 files changed, 88 insertions, 0 deletions
diff --git a/lib/curl_endian.c b/lib/curl_endian.c index 7ce2646d4..5d6119677 100644 --- a/lib/curl_endian.c +++ b/lib/curl_endian.c @@ -98,6 +98,79 @@ unsigned __int64 Curl_read64_le(unsigned char *buf) #endif /* + * Curl_read16_be() + * + * This function converts a 16-bit integer from the big endian format, as + * used in the incoming package to whatever endian format we're using + * natively. + * + * Parameters: + * + * buf [in] - A pointer to a 2 byte buffer. + * + * Returns the integer. + */ +unsigned short Curl_read16_be(unsigned char *buf) +{ + return (unsigned short)(((unsigned short)buf[0] << 8) | + ((unsigned short)buf[1])); +} + +/* + * Curl_read32_be() + * + * This function converts a 32-bit integer from the big endian format, as + * used in the incoming package to whatever endian format we're using + * natively. + * + * Parameters: + * + * buf [in] - A pointer to a 4 byte buffer. + * + * Returns the integer. + */ +unsigned int Curl_read32_be(unsigned char *buf) +{ + return ((unsigned int)buf[0] << 24) | ((unsigned int)buf[1] << 16) | + ((unsigned int)buf[2] << 8) | ((unsigned int)buf[3]); +} + +/* + * Curl_read64_be() + * + * This function converts a 64-bit integer from the big endian format, as + * used in the incoming package to whatever endian format we're using + * natively. + * + * Parameters: + * + * buf [in] - A pointer to a 8 byte buffer. + * + * Returns the integer. + */ +#if defined(HAVE_LONGLONG) +unsigned long long Curl_read64_be(unsigned char *buf) +{ + return ((unsigned long long)buf[0] << 54) | + ((unsigned long long)buf[1] << 48) | + ((unsigned long long)buf[2] << 40) | + ((unsigned long long)buf[3] << 32) | + ((unsigned long long)buf[4] << 24) | + ((unsigned long long)buf[5] << 16) | + ((unsigned long long)buf[6] << 8) | + ((unsigned long long)buf[7]); +} +#else +unsigned __int64 Curl_read64_be(unsigned char *buf) +{ + return ((unsigned __int64)buf[0] << 54) | ((unsigned __int64)buf[1] << 48) | + ((unsigned __int64)buf[2] << 40) | ((unsigned __int64)buf[3] << 32) | + ((unsigned __int64)buf[4] << 24) | ((unsigned __int64)buf[5] << 16) | + ((unsigned __int64)buf[6] << 8) | ((unsigned __int64)buf[7]); +} +#endif + +/* * Curl_write16_le() * * This function converts a 16-bit integer from the native endian format, diff --git a/lib/curl_endian.h b/lib/curl_endian.h index 47a7ae6b8..e384279fa 100644 --- a/lib/curl_endian.h +++ b/lib/curl_endian.h @@ -37,6 +37,21 @@ unsigned __int64 Curl_read64_le(unsigned char *buf); #endif #endif +/* Converts a 16-bit integer from big endian */ +unsigned short Curl_read16_be(unsigned char *buf); + +/* Converts a 32-bit integer from big endian */ +unsigned int Curl_read32_be(unsigned char *buf); + +#if (CURL_SIZEOF_CURL_OFF_T > 4) +/* Converts a 64-bit integer from big endian */ +#if defined(HAVE_LONGLONG) +unsigned long long Curl_read64_be(unsigned char *buf); +#else +unsigned __int64 Curl_read64_be(unsigned char *buf); +#endif +#endif + /* Converts a 16-bit integer to little endian */ void Curl_write16_le(const short value, unsigned char *buffer); |