installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / shared / Buffer.cc
1 // Copyright (c) 2011-2016 Ryan Prichard
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20
21 #include "Buffer.h"
22
23 #include <stdint.h>
24
25 #include "DebugClient.h"
26 #include "WinptyAssert.h"
27
28 // Define the READ_BUFFER_CHECK() macro.  It *must* evaluate its condition,
29 // exactly once.
30 #define READ_BUFFER_CHECK(cond)                                 \
31     do {                                                        \
32         if (!(cond)) {                                          \
33             trace("decode error: %s", #cond);                   \
34             throw DecodeError();                                \
35         }                                                       \
36     } while (false)
37
38 enum class Piece : uint8_t { Int32, Int64, WString };
39
40 void WriteBuffer::putRawData(const void *data, size_t len) {
41     const auto p = reinterpret_cast<const char*>(data);
42     m_buf.insert(m_buf.end(), p, p + len);
43 }
44
45 void WriteBuffer::replaceRawData(size_t pos, const void *data, size_t len) {
46     ASSERT(pos <= m_buf.size() && len <= m_buf.size() - pos);
47     const auto p = reinterpret_cast<const char*>(data);
48     std::copy(p, p + len, &m_buf[pos]);
49 }
50
51 void WriteBuffer::putInt32(int32_t i) {
52     putRawValue(Piece::Int32);
53     putRawValue(i);
54 }
55
56 void WriteBuffer::putInt64(int64_t i) {
57     putRawValue(Piece::Int64);
58     putRawValue(i);
59 }
60
61 // len is in characters, excluding NUL, i.e. the number of wchar_t elements
62 void WriteBuffer::putWString(const wchar_t *str, size_t len) {
63     putRawValue(Piece::WString);
64     putRawValue(static_cast<uint64_t>(len));
65     putRawData(str, sizeof(wchar_t) * len);
66 }
67
68 void ReadBuffer::getRawData(void *data, size_t len) {
69     ASSERT(m_off <= m_buf.size());
70     READ_BUFFER_CHECK(len <= m_buf.size() - m_off);
71     const char *const inp = &m_buf[m_off];
72     std::copy(inp, inp + len, reinterpret_cast<char*>(data));
73     m_off += len;
74 }
75
76 int32_t ReadBuffer::getInt32() {
77     READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::Int32);
78     return getRawValue<int32_t>();
79 }
80
81 int64_t ReadBuffer::getInt64() {
82     READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::Int64);
83     return getRawValue<int64_t>();
84 }
85
86 std::wstring ReadBuffer::getWString() {
87     READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::WString);
88     const uint64_t charLen = getRawValue<uint64_t>();
89     READ_BUFFER_CHECK(charLen <= SIZE_MAX / sizeof(wchar_t));
90     // To be strictly conforming, we can't use the convenient wstring
91     // constructor, because the string in m_buf mightn't be aligned.
92     std::wstring ret;
93     if (charLen > 0) {
94         const size_t byteLen = charLen * sizeof(wchar_t);
95         ret.resize(charLen);
96         getRawData(&ret[0], byteLen);
97     }
98     return ret;
99 }
100
101 void ReadBuffer::assertEof() {
102     READ_BUFFER_CHECK(m_off == m_buf.size());
103 }