GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/string_bytes.h Lines: 14 14 100.0 %
Date: 2016-07-23 Branches: 2 2 100.0 %

Line Exec Source
1
#ifndef SRC_STRING_BYTES_H_
2
#define SRC_STRING_BYTES_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
// Decodes a v8::Local<v8::String> or Buffer to a raw char*
7
8
#include "v8.h"
9
#include "node.h"
10
#include "env.h"
11
#include "env-inl.h"
12
#include "util.h"
13
14
namespace node {
15
16
class StringBytes {
17
 public:
18
1416
  class InlineDecoder : public MaybeStackBuffer<char> {
19
   public:
20
354
    inline bool Decode(Environment* env,
21
                       v8::Local<v8::String> string,
22
                       v8::Local<v8::Value> encoding,
23
                       enum encoding _default) {
24
354
      enum encoding enc = ParseEncoding(env->isolate(), encoding, _default);
25
354
      if (!StringBytes::IsValidString(env->isolate(), string, enc)) {
26
5
        env->ThrowTypeError("Bad input string");
27
5
        return false;
28
      }
29
30
698
      const size_t storage = StringBytes::StorageSize(env->isolate(),
31
                                                      string,
32
349
                                                      enc);
33
349
      AllocateSufficientStorage(storage);
34
698
      const size_t length = StringBytes::Write(env->isolate(),
35
                                               out(),
36
                                               storage,
37
                                               string,
38
349
                                               enc);
39
40
      // No zero terminator is included when using this method.
41
698
      SetLength(length);
42
349
      return true;
43
    }
44
45
349
    inline size_t size() const { return length(); }
46
  };
47
48
  // Does the string match the encoding? Quick but non-exhaustive.
49
  // Example: a HEX string must have a length that's a multiple of two.
50
  // FIXME(bnoordhuis) IsMaybeValidString()? Naming things is hard...
51
  static bool IsValidString(v8::Isolate* isolate,
52
                            v8::Local<v8::String> string,
53
                            enum encoding enc);
54
55
  // Fast, but can be 2 bytes oversized for Base64, and
56
  // as much as triple UTF-8 strings <= 65536 chars in length
57
  static size_t StorageSize(v8::Isolate* isolate,
58
                            v8::Local<v8::Value> val,
59
                            enum encoding enc);
60
61
  // Precise byte count, but slightly slower for Base64 and
62
  // very much slower for UTF-8
63
  static size_t Size(v8::Isolate* isolate,
64
                     v8::Local<v8::Value> val,
65
                     enum encoding enc);
66
67
  // If the string is external then assign external properties to data and len,
68
  // then return true. If not return false.
69
  static bool GetExternalParts(v8::Isolate* isolate,
70
                               v8::Local<v8::Value> val,
71
                               const char** data,
72
                               size_t* len);
73
74
  // Write the bytes from the string or buffer into the char*
75
  // returns the number of bytes written, which will always be
76
  // <= buflen.  Use StorageSize/Size first to know how much
77
  // memory to allocate.
78
  static size_t Write(v8::Isolate* isolate,
79
                      char* buf,
80
                      size_t buflen,
81
                      v8::Local<v8::Value> val,
82
                      enum encoding enc,
83
                      int* chars_written = nullptr);
84
85
  // Take the bytes in the src, and turn it into a Buffer or String.
86
  // Don't call with encoding=UCS2.
87
  static v8::Local<v8::Value> Encode(v8::Isolate* isolate,
88
                                     const char* buf,
89
                                     size_t buflen,
90
                                     enum encoding encoding);
91
92
  // The input buffer should be in host endianness.
93
  static v8::Local<v8::Value> Encode(v8::Isolate* isolate,
94
                                     const uint16_t* buf,
95
                                     size_t buflen);
96
97
  static v8::Local<v8::Value> Encode(v8::Isolate* isolate,
98
                                     const char* buf,
99
                                     enum encoding encoding);
100
101
 private:
102
  static size_t WriteUCS2(char* buf,
103
                          size_t buflen,
104
                          size_t nbytes,
105
                          const char* data,
106
                          v8::Local<v8::String> str,
107
                          int flags,
108
                          size_t* chars_written);
109
};
110
111
}  // namespace node
112
113
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
114
115
#endif  // SRC_STRING_BYTES_H_