diff options
author | Bryon Leung <teslaslegacy@gmail.com> | 2015-10-07 20:47:57 +0300 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2015-11-24 17:26:23 +0300 |
commit | 2ccde01980f8577fbe36130689e6a85653989f51 (patch) | |
tree | 0f72ecc8fc6247be68177ebe15a0baa03c7a634b /src/string_bytes.cc | |
parent | 8bc80386879538de63cd6f2aef288f59324eb004 (diff) |
src: add BE support to StringBytes::Encode()
Versions of Node.js after v0.12 have relocated byte-swapping away from
the StringBytes::Encode function, thereby causing a nan test (which
accesses this function directly) to fail on big-endian machines.
This change re-introduces byte swapping in StringBytes::Encode,
done via a call to a function in util-inl. Another change in
NodeBuffer::StringSlice was necessary to avoid double byte swapping
in big-endian function calls to StringSlice.
PR-URL: https://github.com/nodejs/node/pull/3410
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'src/string_bytes.cc')
-rw-r--r-- | src/string_bytes.cc | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 90fafd40cd3..a916caf75e8 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -6,6 +6,7 @@ #include <limits.h> #include <string.h> // memcpy +#include <vector> // When creating strings >= this length v8's gc spins up and consumes // most of the execution time. For these cases it's more performant to @@ -406,9 +407,7 @@ size_t StringBytes::Write(Isolate* isolate, reinterpret_cast<uintptr_t>(buf) % sizeof(uint16_t); if (is_aligned) { uint16_t* const dst = reinterpret_cast<uint16_t*>(buf); - for (size_t i = 0; i < nchars; i++) - dst[i] = dst[i] << 8 | dst[i] >> 8; - break; + SwapBytes(dst, dst, nchars); } ASSERT_EQ(sizeof(uint16_t), 2); @@ -857,7 +856,16 @@ Local<Value> StringBytes::Encode(Isolate* isolate, const uint16_t* buf, size_t buflen) { Local<String> val; - + std::vector<uint16_t> dst; + if (IsBigEndian()) { + // Node's "ucs2" encoding expects LE character data inside a + // Buffer, so we need to reorder on BE platforms. See + // http://nodejs.org/api/buffer.html regarding Node's "ucs2" + // encoding specification + dst.resize(buflen); + SwapBytes(&dst[0], buf, buflen); + buf = &dst[0]; + } if (buflen < EXTERN_APEX) { val = String::NewFromTwoByte(isolate, buf, |