52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
// Copyright 2017 Google LLC.
|
|
//
|
|
// This software is licensed under the terms defined in the Widevine Master
|
|
// License Agreement. For a copy of this agreement, please contact
|
|
// widevine-licensing@google.com.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef UTIL_ENDIAN_ENDIAN_H_
|
|
#define UTIL_ENDIAN_ENDIAN_H_
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
namespace widevine {
|
|
|
|
// Utilities to convert numbers between the current hosts's native byte
|
|
// order and big-endian byte order (same as network byte order)
|
|
class BigEndian {
|
|
public:
|
|
static uint32_t Load32(const char* indata) {
|
|
const uint8_t* data = reinterpret_cast<const uint8_t*>(indata);
|
|
return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
|
|
}
|
|
|
|
static uint64_t Load64(const char* data) {
|
|
return ((static_cast<uint64_t>(Load32(data)) << 32) | Load32(data + 4));
|
|
}
|
|
|
|
static void Store32(void* p, uint32_t data) {
|
|
for (int i = 0; i < 4; ++i)
|
|
(reinterpret_cast<uint8_t*>(p))[i] = (data >> ((3 - i) * 8)) & 0xFF;
|
|
}
|
|
}; // BigEndian
|
|
|
|
inline uint64_t gntohll(uint64_t n) {
|
|
uint64_t retval;
|
|
#if __BYTE_ORDER == __BIG_ENDIAN
|
|
retval = n;
|
|
#else
|
|
retval = ((uint64_t) ntohl(n & 0xFFFFFFFFLLU)) << 32;
|
|
retval |= ntohl((n & 0xFFFFFFFF00000000LLU) >> 32);
|
|
#endif
|
|
return(retval);
|
|
}
|
|
|
|
} // namespace widevine
|
|
|
|
#endif // UTIL_ENDIAN_ENDIAN_H_
|