1 // Copyright (c) 2011-2016 Ryan Prichard
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
25 #include "DebugClient.h"
26 #include "WinptyAssert.h"
28 // Define the READ_BUFFER_CHECK() macro. It *must* evaluate its condition,
30 #define READ_BUFFER_CHECK(cond) \
33 trace("decode error: %s", #cond); \
34 throw DecodeError(); \
38 enum class Piece : uint8_t { Int32, Int64, WString };
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);
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]);
51 void WriteBuffer::putInt32(int32_t i) {
52 putRawValue(Piece::Int32);
56 void WriteBuffer::putInt64(int64_t i) {
57 putRawValue(Piece::Int64);
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);
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));
76 int32_t ReadBuffer::getInt32() {
77 READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::Int32);
78 return getRawValue<int32_t>();
81 int64_t ReadBuffer::getInt64() {
82 READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::Int64);
83 return getRawValue<int64_t>();
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.
94 const size_t byteLen = charLen * sizeof(wchar_t);
96 getRawData(&ret[0], byteLen);
101 void ReadBuffer::assertEof() {
102 READ_BUFFER_CHECK(m_off == m_buf.size());