diff --git a/nan.h b/nan.h index ae06696d..7aa02559 100644 --- a/nan.h +++ b/nan.h @@ -2435,6 +2435,16 @@ enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; # include "nan_string_bytes.h" // NOLINT(build/include) #endif +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +namespace imp { +inline void SwapBytes16(const uint16_t* src, uint16_t* dst, size_t count) { + for (size_t i = 0; i < count; i++) { + dst[i] = static_cast((src[i] >> 8) | (src[i] << 8)); + } +} +} // namespace imp +#endif + #if NODE_MAJOR_VERSION >= 24 inline MaybeLocal TryEncode( const void *buf, size_t len, enum Encoding encoding = BINARY) { @@ -2442,10 +2452,22 @@ inline MaybeLocal TryEncode( node::encoding node_enc = static_cast(encoding); if (encoding == UCS2) { +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + size_t count = len / 2; + uint16_t* swapped = new uint16_t[count]; + imp::SwapBytes16(reinterpret_cast(buf), swapped, count); + MaybeLocal result = node::TryEncode( + isolate + , swapped + , count); + delete[] swapped; + return result; +#else return node::TryEncode( isolate , reinterpret_cast(buf) , len / 2); +#endif } else { return node::TryEncode( isolate @@ -2469,10 +2491,22 @@ inline v8::Local Encode( node::encoding node_enc = static_cast(encoding); if (encoding == UCS2) { +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + size_t count = len / 2; + uint16_t* swapped = new uint16_t[count]; + imp::SwapBytes16(reinterpret_cast(buf), swapped, count); + v8::Local result = node::Encode( + isolate + , swapped + , count); + delete[] swapped; + return result; +#else return node::Encode( isolate , reinterpret_cast(buf) , len / 2); +#endif } else { return node::Encode( isolate @@ -2481,14 +2515,51 @@ inline v8::Local Encode( , node_enc); } #elif (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) +# if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + if (encoding == UCS2) { + size_t count = len / 2; + uint16_t* swapped = new uint16_t[count]; + imp::SwapBytes16(reinterpret_cast(buf), swapped, count); + v8::Local result = node::Encode( + v8::Isolate::GetCurrent() + , reinterpret_cast(swapped) + , len + , static_cast(encoding)); + delete[] swapped; + return result; + } +# endif return node::Encode( v8::Isolate::GetCurrent() , buf, len , static_cast(encoding)); #else # if NODE_MODULE_VERSION >= NODE_0_10_MODULE_VERSION +# if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + if (encoding == UCS2) { + size_t count = len / 2; + uint16_t* swapped = new uint16_t[count]; + imp::SwapBytes16(reinterpret_cast(buf), swapped, count); + v8::Local result = + node::Encode(reinterpret_cast(swapped), len, + static_cast(encoding)); + delete[] swapped; + return result; + } +# endif return node::Encode(buf, len, static_cast(encoding)); # else +# if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + if (encoding == UCS2) { + size_t count = len / 2; + uint16_t* swapped = new uint16_t[count]; + imp::SwapBytes16(reinterpret_cast(buf), swapped, count); + v8::Local result = + imp::Encode(reinterpret_cast(swapped), len, encoding); + delete[] swapped; + return result; + } +# endif return imp::Encode(reinterpret_cast(buf), len, encoding); # endif #endif @@ -3200,3 +3271,4 @@ MakeMaybe(MaybeMaybe v) { } // end of namespace Nan #endif // NAN_H_ +