1 #ifndef SRC_STRING_DECODER_H_
2 #define SRC_STRING_DECODER_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "node.h"
7 
8 namespace node {
9 
10 class StringDecoder {
11  public:
StringDecoder()12   StringDecoder() { state_[kEncodingField] = BUFFER; }
13   inline void SetEncoding(enum encoding encoding);
14   inline enum encoding Encoding() const;
15 
16   inline char* IncompleteCharacterBuffer();
17   inline unsigned MissingBytes() const;
18   inline unsigned BufferedBytes() const;
19 
20   // Decode a string from the specified encoding.
21   // The value pointed to by `nread` will be modified to reflect that
22   // less data may have been read because it ended on an incomplete character
23   // and more data may have been read because a previously incomplete character
24   // was finished.
25   v8::MaybeLocal<v8::String> DecodeData(v8::Isolate* isolate,
26                                         const char* data,
27                                         size_t* nread);
28   // Flush an incomplete character. For character encodings like UTF8 this
29   // means printing replacement characters, buf for e.g. Base64 the returned
30   // string contains more data.
31   v8::MaybeLocal<v8::String> FlushData(v8::Isolate* isolate);
32 
33   enum Fields {
34     kIncompleteCharactersStart = 0,
35     kIncompleteCharactersEnd = 4,
36     kMissingBytes = 4,
37     kBufferedBytes = 5,
38     kEncodingField = 6,
39     kNumFields = 7
40   };
41 
42  private:
43   uint8_t state_[kNumFields] = {};
44 };
45 
46 }  // namespace node
47 
48 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
49 
50 #endif   // SRC_STRING_DECODER_H_
51