installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / shared / Buffer.h
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 #ifndef WINPTY_SHARED_BUFFER_H
22 #define WINPTY_SHARED_BUFFER_H
23
24 #include <stdint.h>
25 #include <string.h>
26
27 #include <algorithm>
28 #include <utility>
29 #include <vector>
30 #include <string>
31
32 #include "WinptyException.h"
33
34 class WriteBuffer {
35 private:
36     std::vector<char> m_buf;
37
38 public:
39     WriteBuffer() {}
40
41     template <typename T> void putRawValue(const T &t) {
42         putRawData(&t, sizeof(t));
43     }
44     template <typename T> void replaceRawValue(size_t pos, const T &t) {
45         replaceRawData(pos, &t, sizeof(t));
46     }
47
48     void putRawData(const void *data, size_t len);
49     void replaceRawData(size_t pos, const void *data, size_t len);
50     void putInt32(int32_t i);
51     void putInt64(int64_t i);
52     void putWString(const wchar_t *str, size_t len);
53     void putWString(const wchar_t *str)         { putWString(str, wcslen(str)); }
54     void putWString(const std::wstring &str)    { putWString(str.data(), str.size()); }
55     std::vector<char> &buf()                    { return m_buf; }
56
57     // MSVC 2013 does not generate these automatically, so help it out.
58     WriteBuffer(WriteBuffer &&other) : m_buf(std::move(other.m_buf)) {}
59     WriteBuffer &operator=(WriteBuffer &&other) {
60         m_buf = std::move(other.m_buf);
61         return *this;
62     }
63 };
64
65 class ReadBuffer {
66 public:
67     class DecodeError : public WinptyException {
68         virtual const wchar_t *what() const WINPTY_NOEXCEPT override {
69             return L"DecodeError: RPC message decoding error";
70         }
71     };
72
73 private:
74     std::vector<char> m_buf;
75     size_t m_off = 0;
76
77 public:
78     explicit ReadBuffer(std::vector<char> &&buf) : m_buf(std::move(buf)) {}
79
80     template <typename T> T getRawValue() {
81         T ret = {};
82         getRawData(&ret, sizeof(ret));
83         return ret;
84     }
85
86     void getRawData(void *data, size_t len);
87     int32_t getInt32();
88     int64_t getInt64();
89     std::wstring getWString();
90     void assertEof();
91
92     // MSVC 2013 does not generate these automatically, so help it out.
93     ReadBuffer(ReadBuffer &&other) :
94         m_buf(std::move(other.m_buf)), m_off(other.m_off) {}
95     ReadBuffer &operator=(ReadBuffer &&other) {
96         m_buf = std::move(other.m_buf);
97         m_off = other.m_off;
98         return *this;
99     }
100 };
101
102 #endif // WINPTY_SHARED_BUFFER_H