potential endianness fix

This commit is contained in:
Peter Thorson
2013-04-30 17:42:36 -05:00
parent df8d7f3f0d
commit 40c10ff6e7
2 changed files with 14 additions and 3 deletions

View File

@@ -40,6 +40,12 @@ namespace websocketpp {
namespace lib {
namespace net {
inline bool is_little_endian() {
short int val = 0x1;
char *ptr = (char*)&val;
return (ptr[0] == 1);
}
#define TYP_INIT 0
#define TYP_SMLE 1
#define TYP_BIGE 2
@@ -56,7 +62,7 @@ inline uint64_t htonll(uint64_t src) {
typ = (x.c[7] == 0x01ULL) ? TYP_BIGE : TYP_SMLE;
}
if (typ == TYP_BIGE)
return src;
return src;
x.ull = src;
c = x.c[0]; x.c[0] = x.c[7]; x.c[7] = c;
c = x.c[1]; x.c[1] = x.c[6]; x.c[6] = c;

View File

@@ -590,8 +590,13 @@ inline size_t prepare_masking_key(const masking_key_type& key) {
* to zero and less than sizeof(size_t).
*/
inline size_t circshift_prepared_key(size_t prepared_key, size_t offset) {
size_t temp = prepared_key << (sizeof(size_t)-offset)*8;
return (prepared_key >> offset*8) | temp;
if (lib::net::is_little_endian()) {
size_t temp = prepared_key << (sizeof(size_t)-offset)*8;
return (prepared_key >> offset*8) | temp;
} else {
size_t temp = prepared_key >> (sizeof(size_t)-offset)*8;
return (prepared_key << offset*8) | temp;
}
}
/// Byte by byte mask/unmask