Welcome to mirror list, hosted at ThFree Co, Russian Federation.

encoding.cc « api « src - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3ccfd6c84b786567a8a611ebb3b2c9516ebbeaa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "node.h"
#include "string_bytes.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':
    case 'U':
      // Note: the two first conditions are needed for performance reasons
      // as "utf8"/"utf-8" is a common case.
      // (same for other cases below)

      // utf8, utf16le
      if (encoding[1] == 't' && encoding[2] == 'f') {
        // Skip `-`
        const size_t skip = encoding[3] == '-' ? 4 : 3;
        if (encoding[skip] == '8' && encoding[skip + 1] == '\0')
          return UTF8;
        if (strncmp(encoding + skip, "16le", 5) == 0)
          return UCS2;
      // ucs2
      } else if (encoding[1] == 'c' && encoding[2] == 's') {
        const size_t skip = encoding[3] == '-' ? 4 : 3;
        if (encoding[skip] == '2' && encoding[skip + 1] == '\0')
          return UCS2;
      }
      if (StringEqualNoCase(encoding, "utf8"))
        return UTF8;
      if (StringEqualNoCase(encoding, "utf-8"))
        return UTF8;
      if (StringEqualNoCase(encoding, "ucs2"))
        return UCS2;
      if (StringEqualNoCase(encoding, "ucs-2"))
        return UCS2;
      if (StringEqualNoCase(encoding, "utf16le"))
        return UCS2;
      if (StringEqualNoCase(encoding, "utf-16le"))
        return UCS2;
      break;

    case 'l':
    case 'L':
      // latin1
      if (encoding[1] == 'a') {
        if (strncmp(encoding + 2, "tin1", 5) == 0)
          return LATIN1;
      }
      if (StringEqualNoCase(encoding, "latin1"))
        return LATIN1;
      break;

    case 'b':
    case 'B':
      // binary is a deprecated alias of latin1
      if (encoding[1] == 'i') {
        if (strncmp(encoding + 2, "nary", 5) == 0)
          return LATIN1;
      // buffer
      } else if (encoding[1] == 'u') {
        if (strncmp(encoding + 2, "ffer", 5) == 0)
          return BUFFER;
      // base64
      } else if (encoding[1] == 'a') {
        if (strncmp(encoding + 2, "se64", 5) == 0)
          return BASE64;
        if (strncmp(encoding + 2, "se64url", 8) == 0)
          return BASE64URL;
      }
      if (StringEqualNoCase(encoding, "binary"))
        return LATIN1;  // BINARY is a deprecated alias of LATIN1.
      if (StringEqualNoCase(encoding, "buffer"))
        return BUFFER;
      if (StringEqualNoCase(encoding, "base64"))
        return BASE64;
      if (StringEqualNoCase(encoding, "base64url"))
        return BASE64URL;
      break;

    case 'a':
    case 'A':
      // ascii
      if (encoding[1] == 's') {
        if (strncmp(encoding + 2, "cii", 4) == 0)
          return ASCII;
      }
      if (StringEqualNoCase(encoding, "ascii"))
        return ASCII;
      break;

    case 'h':
    case 'H':
      // hex
      if (encoding[1] == 'e')
        if (encoding[2] == 'x' && encoding[3] == '\0')
          return HEX;
      if (StringEqualNoCase(encoding, "hex"))
        return HEX;
      break;
  }
  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;

  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).FromMaybe(-1);
}

// 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);
}

}  // namespace node