GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/api/encoding.cc Lines: 60 83 72.3 %
Date: 2021-04-21 04:11:54 Branches: 60 92 65.2 %

Line Branch Exec Source
1
#include "node.h"
2
#include "string_bytes.h"
3
#include "util-inl.h"
4
#include "v8.h"
5
6
namespace node {
7
8
using v8::HandleScope;
9
using v8::Isolate;
10
using v8::Local;
11
using v8::Value;
12
13
130168
enum encoding ParseEncoding(const char* encoding,
14
                            enum encoding default_encoding) {
15

130168
  switch (encoding[0]) {
16
    case 'u':
17
    case 'U':
18
      // utf8, utf16le
19

15651
      if (encoding[1] == 't' && encoding[2] == 'f') {
20
        // Skip `-`
21
15626
        const size_t skip = encoding[3] == '-' ? 4 : 3;
22

15626
        if (encoding[skip] == '8' && encoding[skip + 1] == '\0')
23
15606
          return UTF8;
24
20
        if (strncmp(encoding + skip, "16le", 5) == 0)
25
17
          return UCS2;
26
      // ucs2
27

25
      } else if (encoding[1] == 'c' && encoding[2] == 's') {
28
22
        const size_t skip = encoding[3] == '-' ? 4 : 3;
29

22
        if (encoding[skip] == '2' && encoding[skip + 1] == '\0')
30
22
          return UCS2;
31
      }
32
6
      if (StringEqualNoCase(encoding, "utf8"))
33
        return UTF8;
34
6
      if (StringEqualNoCase(encoding, "utf-8"))
35
2
        return UTF8;
36
4
      if (StringEqualNoCase(encoding, "ucs2"))
37
        return UCS2;
38
4
      if (StringEqualNoCase(encoding, "ucs-2"))
39
        return UCS2;
40
4
      if (StringEqualNoCase(encoding, "utf16le"))
41
        return UCS2;
42
4
      if (StringEqualNoCase(encoding, "utf-16le"))
43
1
        return UCS2;
44
3
      break;
45
46
    case 'l':
47
    case 'L':
48
      // latin1
49
63573
      if (encoding[1] == 'a') {
50
63571
        if (strncmp(encoding + 2, "tin1", 5) == 0)
51
63571
          return LATIN1;
52
      }
53
2
      if (StringEqualNoCase(encoding, "latin1"))
54
        return LATIN1;
55
2
      break;
56
57
    case 'b':
58
    case 'B':
59
      // binary is a deprecated alias of latin1
60
24895
      if (encoding[1] == 'i') {
61
19
        if (strncmp(encoding + 2, "nary", 5) == 0)
62
19
          return LATIN1;
63
      // buffer
64
24876
      } else if (encoding[1] == 'u') {
65
24462
        if (strncmp(encoding + 2, "ffer", 5) == 0)
66
24462
          return BUFFER;
67
      // base64
68
414
      } else if (encoding[1] == 'a') {
69
412
        if (strncmp(encoding + 2, "se64", 5) == 0)
70
395
          return BASE64;
71
17
        if (strncmp(encoding + 2, "se64url", 8) == 0)
72
17
          return BASE64URL;
73
      }
74
2
      if (StringEqualNoCase(encoding, "binary"))
75
        return LATIN1;  // BINARY is a deprecated alias of LATIN1.
76
2
      if (StringEqualNoCase(encoding, "buffer"))
77
        return BUFFER;
78
2
      if (StringEqualNoCase(encoding, "base64"))
79
        return BASE64;
80
2
      if (StringEqualNoCase(encoding, "base64url"))
81
        return BASE64URL;
82
2
      break;
83
84
    case 'a':
85
    case 'A':
86
      // ascii
87
94
      if (encoding[1] == 's') {
88
94
        if (strncmp(encoding + 2, "cii", 4) == 0)
89
94
          return ASCII;
90
      }
91
      if (StringEqualNoCase(encoding, "ascii"))
92
        return ASCII;
93
      break;
94
95
    case 'h':
96
    case 'H':
97
      // hex
98
25954
      if (encoding[1] == 'e')
99

25954
        if (encoding[2] == 'x' && encoding[3] == '\0')
100
25954
          return HEX;
101
      if (StringEqualNoCase(encoding, "hex"))
102
        return HEX;
103
      break;
104
  }
105
8
  return default_encoding;
106
}
107
108
109
244514
enum encoding ParseEncoding(Isolate* isolate,
110
                            Local<Value> encoding_v,
111
                            enum encoding default_encoding) {
112
244514
  CHECK(!encoding_v.IsEmpty());
113
114
489028
  if (!encoding_v->IsString())
115
114346
    return default_encoding;
116
117
260336
  Utf8Value encoding(isolate, encoding_v);
118
119
130168
  return ParseEncoding(*encoding, default_encoding);
120
}
121
122
2
Local<Value> Encode(Isolate* isolate,
123
                    const char* buf,
124
                    size_t len,
125
                    enum encoding encoding) {
126
2
  CHECK_NE(encoding, UCS2);
127
  Local<Value> error;
128
4
  return StringBytes::Encode(isolate, buf, len, encoding, &error)
129
2
      .ToLocalChecked();
130
}
131
132
Local<Value> Encode(Isolate* isolate, const uint16_t* buf, size_t len) {
133
  Local<Value> error;
134
  return StringBytes::Encode(isolate, buf, len, &error)
135
      .ToLocalChecked();
136
}
137
138
// Returns -1 if the handle was not valid for decoding
139
ssize_t DecodeBytes(Isolate* isolate,
140
                    Local<Value> val,
141
                    enum encoding encoding) {
142
  HandleScope scope(isolate);
143
144
  return StringBytes::Size(isolate, val, encoding).FromMaybe(-1);
145
}
146
147
// Returns number of bytes written.
148
ssize_t DecodeWrite(Isolate* isolate,
149
                    char* buf,
150
                    size_t buflen,
151
                    Local<Value> val,
152
                    enum encoding encoding) {
153
  return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr);
154
}
155
156

14310
}  // namespace node