diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-08-31 16:57:03 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-09-03 22:33:42 +0200 |
commit | 71f633a32f1f5617c1c7b60ad936342a579bff74 (patch) | |
tree | bcfc550c80f66a630cc149fbc80f204d0ca6cc62 /src/node_buffer.cc | |
parent | 6455bea52ba262cea8d1c706ec8a2eec192ec643 (diff) | |
download | android-node-v8-71f633a32f1f5617c1c7b60ad936342a579bff74.tar.gz android-node-v8-71f633a32f1f5617c1c7b60ad936342a579bff74.tar.bz2 android-node-v8-71f633a32f1f5617c1c7b60ad936342a579bff74.zip |
src: allow UTF-16 in generic StringBytes decode call
This allows removing a number of special cases in other
parts of the code, at least one of which was incorrect
in expecting aligned input when that was not guaranteed.
Fixes: https://github.com/nodejs/node/issues/22358
PR-URL: https://github.com/nodejs/node/pull/22622
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'src/node_buffer.cc')
-rw-r--r-- | src/node_buffer.cc | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 4a29873c34..7c4c0d9f65 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -467,65 +467,6 @@ void StringSlice(const FunctionCallbackInfo<Value>& args) { } -template <> -void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) { - Isolate* isolate = args.GetIsolate(); - Environment* env = Environment::GetCurrent(isolate); - - THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); - SPREAD_BUFFER_ARG(args.This(), ts_obj); - - if (ts_obj_length == 0) - return args.GetReturnValue().SetEmptyString(); - - SLICE_START_END(args[0], args[1], ts_obj_length) - length /= 2; - - const char* data = ts_obj_data + start; - const uint16_t* buf; - bool release = false; - - // Node's "ucs2" encoding expects LE character data inside a Buffer, so we - // need to reorder on BE platforms. See https://nodejs.org/api/buffer.html - // regarding Node's "ucs2" encoding specification. - const bool aligned = (reinterpret_cast<uintptr_t>(data) % sizeof(*buf) == 0); - if (IsLittleEndian() && !aligned) { - // Make a copy to avoid unaligned accesses in v8::String::NewFromTwoByte(). - // This applies ONLY to little endian platforms, as misalignment will be - // handled by a byte-swapping operation in StringBytes::Encode on - // big endian platforms. - uint16_t* copy = new uint16_t[length]; - for (size_t i = 0, k = 0; i < length; i += 1, k += 2) { - // Assumes that the input is little endian. - const uint8_t lo = static_cast<uint8_t>(data[k + 0]); - const uint8_t hi = static_cast<uint8_t>(data[k + 1]); - copy[i] = lo | hi << 8; - } - buf = copy; - release = true; - } else { - buf = reinterpret_cast<const uint16_t*>(data); - } - - Local<Value> error; - MaybeLocal<Value> ret = - StringBytes::Encode(isolate, - buf, - length, - &error); - - if (release) - delete[] buf; - - if (ret.IsEmpty()) { - CHECK(!error.IsEmpty()); - isolate->ThrowException(error); - return; - } - args.GetReturnValue().Set(ret.ToLocalChecked()); -} - - // bytesCopied = copy(buffer, target[, targetStart][, sourceStart][, sourceEnd]) void Copy(const FunctionCallbackInfo<Value> &args) { Environment* env = Environment::GetCurrent(args); |