Skip to content

Commit

Permalink
src: add node_encoding.cc
Browse files Browse the repository at this point in the history
Continue moving bits of code out of node.cc ... add node_encoding.cc
as a home for `ParseEncoding` and related functions.

Backport-PR-URL: #21798
PR-URL: #21112
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
jasnell authored and rvagg committed Aug 16, 2018
1 parent 0321afe commit eaf99d9
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 123 deletions.
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
'src/node_constants.cc',
'src/node_contextify.cc',
'src/node_debug_options.cc',
'src/node_encoding.cc',
'src/node_file.cc',
'src/node_http2.cc',
'src/node_http_parser.cc',
Expand Down
123 changes: 0 additions & 123 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1532,129 +1532,6 @@ Local<Value> MakeCallback(Isolate* isolate,
.FromMaybe(Local<Value>()));
}


enum encoding ParseEncoding(const char* encoding,
enum encoding default_encoding) {
switch (encoding[0]) {
case 'u':
// utf8, utf16le
if (encoding[1] == 't' && encoding[2] == 'f') {
// Skip `-`
encoding += encoding[3] == '-' ? 4 : 3;
if (encoding[0] == '8' && encoding[1] == '\0')
return UTF8;
if (strncmp(encoding, "16le", 4) == 0)
return UCS2;

// ucs2
} else if (encoding[1] == 'c' && encoding[2] == 's') {
encoding += encoding[3] == '-' ? 4 : 3;
if (encoding[0] == '2' && encoding[1] == '\0')
return UCS2;
}
break;
case 'l':
// latin1
if (encoding[1] == 'a') {
if (strncmp(encoding + 2, "tin1", 4) == 0)
return LATIN1;
}
break;
case 'b':
// binary
if (encoding[1] == 'i') {
if (strncmp(encoding + 2, "nary", 4) == 0)
return LATIN1;

// buffer
} else if (encoding[1] == 'u') {
if (strncmp(encoding + 2, "ffer", 4) == 0)
return BUFFER;
}
break;
case '\0':
return default_encoding;
default:
break;
}

if (StringEqualNoCase(encoding, "utf8")) {
return UTF8;
} else if (StringEqualNoCase(encoding, "utf-8")) {
return UTF8;
} else if (StringEqualNoCase(encoding, "ascii")) {
return ASCII;
} else if (StringEqualNoCase(encoding, "base64")) {
return BASE64;
} else if (StringEqualNoCase(encoding, "ucs2")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "ucs-2")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "utf16le")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "utf-16le")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "latin1")) {
return LATIN1;
} else if (StringEqualNoCase(encoding, "binary")) {
return LATIN1; // BINARY is a deprecated alias of LATIN1.
} else if (StringEqualNoCase(encoding, "buffer")) {
return BUFFER;
} else if (StringEqualNoCase(encoding, "hex")) {
return HEX;
} else {
return default_encoding;
}
}


enum encoding ParseEncoding(Isolate* isolate,
Local<Value> encoding_v,
enum encoding default_encoding) {
CHECK(!encoding_v.IsEmpty());

if (!encoding_v->IsString())
return default_encoding;

node::Utf8Value encoding(isolate, encoding_v);

return ParseEncoding(*encoding, default_encoding);
}

Local<Value> Encode(Isolate* isolate,
const char* buf,
size_t len,
enum encoding encoding) {
CHECK_NE(encoding, UCS2);
Local<Value> error;
return StringBytes::Encode(isolate, buf, len, encoding, &error)
.ToLocalChecked();
}

Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) {
Local<Value> error;
return StringBytes::Encode(isolate, buf, len, &error)
.ToLocalChecked();
}

// Returns -1 if the handle was not valid for decoding
ssize_t DecodeBytes(Isolate* isolate,
Local<Value> val,
enum encoding encoding) {
HandleScope scope(isolate);

return StringBytes::Size(isolate, val, encoding);
}

// Returns number of bytes written.
ssize_t DecodeWrite(Isolate* isolate,
char* buf,
size_t buflen,
Local<Value> val,
enum encoding encoding) {
return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr);
}

bool IsExceptionDecorated(Environment* env, Local<Value> er) {
if (!er.IsEmpty() && er->IsObject()) {
Local<Object> err_obj = er.As<Object>();
Expand Down
138 changes: 138 additions & 0 deletions src/node_encoding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "node.h"
#include "env.h"
#include "env-inl.h"
#include "string_bytes.h"
#include "util.h"
#include "util-inl.h"
#include "v8.h"

namespace node {

using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Value;

enum encoding ParseEncoding(const char* encoding,
enum encoding default_encoding) {
switch (encoding[0]) {
case 'u':
// utf8, utf16le
if (encoding[1] == 't' && encoding[2] == 'f') {
// Skip `-`
encoding += encoding[3] == '-' ? 4 : 3;
if (encoding[0] == '8' && encoding[1] == '\0')
return UTF8;
if (strncmp(encoding, "16le", 4) == 0)
return UCS2;

// ucs2
} else if (encoding[1] == 'c' && encoding[2] == 's') {
encoding += encoding[3] == '-' ? 4 : 3;
if (encoding[0] == '2' && encoding[1] == '\0')
return UCS2;
}
break;
case 'l':
// latin1
if (encoding[1] == 'a') {
if (strncmp(encoding + 2, "tin1", 4) == 0)
return LATIN1;
}
break;
case 'b':
// binary
if (encoding[1] == 'i') {
if (strncmp(encoding + 2, "nary", 4) == 0)
return LATIN1;

// buffer
} else if (encoding[1] == 'u') {
if (strncmp(encoding + 2, "ffer", 4) == 0)
return BUFFER;
}
break;
case '\0':
return default_encoding;
default:
break;
}

if (StringEqualNoCase(encoding, "utf8")) {
return UTF8;
} else if (StringEqualNoCase(encoding, "utf-8")) {
return UTF8;
} else if (StringEqualNoCase(encoding, "ascii")) {
return ASCII;
} else if (StringEqualNoCase(encoding, "base64")) {
return BASE64;
} else if (StringEqualNoCase(encoding, "ucs2")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "ucs-2")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "utf16le")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "utf-16le")) {
return UCS2;
} else if (StringEqualNoCase(encoding, "latin1")) {
return LATIN1;
} else if (StringEqualNoCase(encoding, "binary")) {
return LATIN1; // BINARY is a deprecated alias of LATIN1.
} else if (StringEqualNoCase(encoding, "buffer")) {
return BUFFER;
} else if (StringEqualNoCase(encoding, "hex")) {
return HEX;
} else {
return default_encoding;
}
}


enum encoding ParseEncoding(Isolate* isolate,
Local<Value> encoding_v,
enum encoding default_encoding) {
CHECK(!encoding_v.IsEmpty());

if (!encoding_v->IsString())
return default_encoding;

node::Utf8Value encoding(isolate, encoding_v);

return ParseEncoding(*encoding, default_encoding);
}

Local<Value> Encode(Isolate* isolate,
const char* buf,
size_t len,
enum encoding encoding) {
CHECK_NE(encoding, UCS2);
Local<Value> error;
return StringBytes::Encode(isolate, buf, len, encoding, &error)
.ToLocalChecked();
}

Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) {
Local<Value> error;
return StringBytes::Encode(isolate, buf, len, &error)
.ToLocalChecked();
}

// Returns -1 if the handle was not valid for decoding
ssize_t DecodeBytes(Isolate* isolate,
Local<Value> val,
enum encoding encoding) {
HandleScope scope(isolate);

return StringBytes::Size(isolate, val, encoding);
}

// Returns number of bytes written.
ssize_t DecodeWrite(Isolate* isolate,
char* buf,
size_t buflen,
Local<Value> val,
enum encoding encoding) {
return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr);
}

} // namespace node

0 comments on commit eaf99d9

Please sign in to comment.