Skip to content

Commit af4d8b9

Browse files
author
pfeatherstone
committed
macos doesn't support endian.h
1 parent 4587a78 commit af4d8b9

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

src/http_async.h

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22

3-
#include <endian.h>
43
#include <boost/asio/version.hpp>
54
#include <boost/asio/compose.hpp>
65
#include <boost/asio/read.hpp>
@@ -149,6 +148,66 @@ namespace http
149148

150149
namespace details
151150
{
151+
152+
//----------------------------------------------------------------------------------------------------------------
153+
154+
constexpr uint16_t byte_swap16(uint16_t v)
155+
{
156+
return static_cast<uint16_t>(((v & 0x00FF) << 8) | ((v & 0xFF00) >> 8));
157+
}
158+
159+
constexpr uint32_t byte_swap32(uint32_t v)
160+
{
161+
return static_cast<uint32_t>(((v & 0x000000FF) << 24) |
162+
((v & 0x0000FF00) << 8) |
163+
((v & 0x00FF0000) >> 8) |
164+
((v & 0xFF000000) >> 24));
165+
}
166+
167+
constexpr uint64_t byte_swap64(uint64_t v)
168+
{
169+
return static_cast<uint64_t>(((v & 0x00000000000000FFULL) << 56) |
170+
((v & 0x000000000000FF00ULL) << 40) |
171+
((v & 0x0000000000FF0000ULL) << 24) |
172+
((v & 0x00000000FF000000ULL) << 8) |
173+
((v & 0x000000FF00000000ULL) >> 8) |
174+
((v & 0x0000FF0000000000ULL) >> 24) |
175+
((v & 0x00FF000000000000ULL) >> 40) |
176+
((v & 0xFF00000000000000ULL) >> 56));
177+
}
178+
179+
static_assert(byte_swap16(0x1234) == 0x3412, "bad swap");
180+
static_assert(byte_swap32(0x12345678) == 0x78563412, "bad swap");
181+
static_assert(byte_swap64(0x123456789abcdef1) == 0xf1debc9a78563412, "bad swap");
182+
183+
//----------------------------------------------------------------------------------------------------------------
184+
185+
inline bool is_little_endian()
186+
{
187+
constexpr uint32_t v{0x01020304};
188+
const auto* ptr{reinterpret_cast<const unsigned char*>(&v)};
189+
return ptr[0] == 0x04;
190+
}
191+
192+
//----------------------------------------------------------------------------------------------------------------
193+
194+
inline uint16_t host_to_b16(uint16_t v)
195+
{
196+
return is_little_endian() ? byte_swap16(v) : v;
197+
}
198+
199+
inline uint32_t host_to_b32(uint32_t v)
200+
{
201+
return is_little_endian() ? byte_swap32(v) : v;
202+
}
203+
204+
inline uint64_t host_to_b64(uint64_t v)
205+
{
206+
return is_little_endian() ? byte_swap64(v) : v;
207+
}
208+
209+
//----------------------------------------------------------------------------------------------------------------
210+
152211
template<class AsyncReadStream, class Message>
153212
struct async_http_read_impl
154213
{
@@ -701,14 +760,14 @@ namespace http
701760

702761
if (hdr.paylen == 126)
703762
{
704-
uint16_t len = htobe16((uint16_t)pay_len);
763+
uint16_t len = host_to_b16((uint16_t)pay_len);
705764
memcpy(&buf[off], &len, 2);
706765
off += 2;
707766
}
708767

709768
else if (hdr.paylen == 127)
710769
{
711-
uint64_t len = htobe64((uint64_t)pay_len);
770+
uint64_t len = host_to_b64((uint64_t)pay_len);
712771
memcpy(&buf[off], &len, 8);
713772
off += 8;
714773
}

0 commit comments

Comments
 (0)