|
| 1 | +/** |
| 2 | + * This file is part of PHPinnacle/Buffer. |
| 3 | + * |
| 4 | + * (c) PHPinnacle Team <dev@phpinnacle.com> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "Buffer.hpp" |
| 11 | + |
| 12 | +/************************* COMMON *************************/ |
| 13 | +Buffer::Buffer() noexcept { |
| 14 | +} |
| 15 | + |
| 16 | +Buffer::Buffer(const std::vector<unsigned char> &_buffer) noexcept: |
| 17 | + buffer(_buffer) { |
| 18 | +} |
| 19 | + |
| 20 | +void Buffer::clear() noexcept { |
| 21 | + buffer.clear(); |
| 22 | +} |
| 23 | + |
| 24 | +void Buffer::discard(unsigned long long n) { |
| 25 | + if (buffer.size() < n) |
| 26 | + throw BufferOverflow(); |
| 27 | + |
| 28 | + buffer.erase(buffer.begin(), buffer.begin() + n); |
| 29 | +} |
| 30 | + |
| 31 | +unsigned long long Buffer::size() const noexcept { |
| 32 | + return buffer.size(); |
| 33 | +} |
| 34 | + |
| 35 | +bool Buffer::empty() const noexcept { |
| 36 | + return buffer.size() == 0; |
| 37 | +} |
| 38 | + |
| 39 | +void Buffer::merge(const Buffer &other) noexcept { |
| 40 | + buffer.insert(buffer.end(), other.buffer.begin(), other.buffer.end()); |
| 41 | +} |
| 42 | + |
| 43 | +std::string Buffer::bytes() const noexcept { |
| 44 | + std::stringstream stream; |
| 45 | + |
| 46 | + unsigned long long size = buffer.size(); |
| 47 | + for (unsigned long long i = 0; i < size; ++i) |
| 48 | + stream << buffer[i]; |
| 49 | + |
| 50 | + return stream.str(); |
| 51 | +} |
| 52 | + |
| 53 | +std::string Buffer::flush() noexcept { |
| 54 | + std::string out = bytes(); |
| 55 | + |
| 56 | + clear(); |
| 57 | + |
| 58 | + return out; |
| 59 | +} |
| 60 | + |
| 61 | +/************************* WRITING *************************/ |
| 62 | +template <class T> inline void Buffer::append(const T &val) noexcept { |
| 63 | + unsigned int size = sizeof(T); |
| 64 | + unsigned const char *array = reinterpret_cast<unsigned const char*>(&val); |
| 65 | + |
| 66 | + for (unsigned int i = 0; i < size; ++i) |
| 67 | + buffer.push_back(array[size - i - 1]); |
| 68 | +} |
| 69 | + |
| 70 | +void Buffer::appendBoolean(bool val) noexcept { |
| 71 | + append<bool>(val); |
| 72 | +} |
| 73 | + |
| 74 | +void Buffer::appendString(const std::string &str) noexcept { |
| 75 | + for (const unsigned char &s : str) appendInt8(s); |
| 76 | +} |
| 77 | + |
| 78 | +void Buffer::appendInt8(char val) noexcept { |
| 79 | + append<char>(val); |
| 80 | +} |
| 81 | +void Buffer::appendUInt8(unsigned char val) noexcept { |
| 82 | + append<unsigned char>(val); |
| 83 | +} |
| 84 | + |
| 85 | +void Buffer::appendInt16(short val) noexcept { |
| 86 | + append<short>(val); |
| 87 | +} |
| 88 | +void Buffer::appendUInt16(unsigned short val) noexcept { |
| 89 | + append<unsigned short>(val); |
| 90 | +} |
| 91 | + |
| 92 | +void Buffer::appendInt32(int val) noexcept { |
| 93 | + append<int>(val); |
| 94 | +} |
| 95 | +void Buffer::appendUInt32(unsigned int val) noexcept { |
| 96 | + append<unsigned int>(val); |
| 97 | +} |
| 98 | + |
| 99 | +void Buffer::appendInt64(long long val) noexcept { |
| 100 | + append<long long>(val); |
| 101 | +} |
| 102 | +void Buffer::appendUInt64(unsigned long long val) noexcept { |
| 103 | + append<unsigned long long>(val); |
| 104 | +} |
| 105 | + |
| 106 | +void Buffer::appendFloat(float val) noexcept { |
| 107 | + union { float fnum; unsigned long inum; } u; |
| 108 | + u.fnum = val; |
| 109 | + appendUInt32(u.inum); |
| 110 | +} |
| 111 | +void Buffer::appendDouble(double val) noexcept { |
| 112 | + union { double fnum; unsigned long long inum; } u; |
| 113 | + u.fnum = val; |
| 114 | + appendUInt64(u.inum); |
| 115 | +} |
| 116 | + |
| 117 | +/************************* READING *************************/ |
| 118 | +template <class T> inline T Buffer::read(unsigned long long offset) const { |
| 119 | + T result = 0; |
| 120 | + unsigned int size = sizeof(T); |
| 121 | + |
| 122 | + if (offset + size > buffer.size()) |
| 123 | + throw BufferOverflow(); |
| 124 | + |
| 125 | + char *dst = (char*) &result; |
| 126 | + char *src = (char*) &buffer[offset]; |
| 127 | + |
| 128 | + for (unsigned int i = 0; i < size; ++i) |
| 129 | + dst[i] = src[size - i - 1]; |
| 130 | + |
| 131 | + return result; |
| 132 | +} |
| 133 | + |
| 134 | +bool Buffer::readBoolean(unsigned long long offset) const { |
| 135 | + return read<bool>(offset); |
| 136 | +} |
| 137 | + |
| 138 | +std::string Buffer::readString(unsigned long long size, unsigned long long offset) const { |
| 139 | + if (offset + size > buffer.size()) |
| 140 | + throw BufferOverflow(); |
| 141 | + |
| 142 | + std::string result(buffer.begin() + offset, buffer.begin() + offset + size); |
| 143 | + |
| 144 | + return result; |
| 145 | +} |
| 146 | + |
| 147 | +char Buffer::readInt8(unsigned long long offset) const { |
| 148 | + return read<char>(offset); |
| 149 | +} |
| 150 | +unsigned char Buffer::readUInt8(unsigned long long offset) const { |
| 151 | + return read<unsigned char>(offset); |
| 152 | +} |
| 153 | + |
| 154 | +short Buffer::readInt16(unsigned long long offset) const { |
| 155 | + return read<short>(offset); |
| 156 | +} |
| 157 | +unsigned short Buffer::readUInt16(unsigned long long offset) const { |
| 158 | + return read<unsigned short>(offset); |
| 159 | +} |
| 160 | + |
| 161 | +int Buffer::readInt32(unsigned long long offset) const { |
| 162 | + return read<int>(offset); |
| 163 | +} |
| 164 | +unsigned int Buffer::readUInt32(unsigned long long offset) const { |
| 165 | + return read<unsigned int>(offset); |
| 166 | +} |
| 167 | + |
| 168 | +long long Buffer::readInt64(unsigned long long offset) const { |
| 169 | + return read<long long>(offset); |
| 170 | +} |
| 171 | +unsigned long long Buffer::readUInt64(unsigned long long offset) const { |
| 172 | + return read<unsigned long long>(offset); |
| 173 | +} |
| 174 | + |
| 175 | +float Buffer::readFloat(unsigned long long offset) const { |
| 176 | + return read<float>(offset); |
| 177 | +} |
| 178 | +double Buffer::readDouble(unsigned long long offset) const { |
| 179 | + return read<double>(offset); |
| 180 | +} |
| 181 | + |
| 182 | +/************************* CONSUMING *************************/ |
| 183 | +template <class T> inline T Buffer::consume() { |
| 184 | + T result = 0; |
| 185 | + unsigned int size = sizeof(T); |
| 186 | + |
| 187 | + if (size > buffer.size()) |
| 188 | + throw BufferOverflow(); |
| 189 | + |
| 190 | + char *dst = (char*) &result; |
| 191 | + char *src = (char*) &buffer[0]; |
| 192 | + |
| 193 | + for (unsigned int i = 0; i < size; ++i) |
| 194 | + dst[i] = src[size - i - 1]; |
| 195 | + |
| 196 | + buffer.erase(buffer.begin(), buffer.begin() + size); |
| 197 | + |
| 198 | + return result; |
| 199 | +} |
| 200 | + |
| 201 | +bool Buffer::consumeBool() { |
| 202 | + return consume<bool>(); |
| 203 | +} |
| 204 | + |
| 205 | +std::string Buffer::consumeString(unsigned long long size) { |
| 206 | + if (size > buffer.size()) |
| 207 | + throw BufferOverflow(); |
| 208 | + |
| 209 | + std::string result(buffer.begin(), buffer.begin() + size); |
| 210 | + |
| 211 | + buffer.erase(buffer.begin(), buffer.begin() + size); |
| 212 | + |
| 213 | + return result; |
| 214 | +} |
| 215 | + |
| 216 | +char Buffer::consumeInt8() { |
| 217 | + return consume<char>(); |
| 218 | +} |
| 219 | +unsigned char Buffer::consumeUInt8() { |
| 220 | + return consume<unsigned char>(); |
| 221 | +} |
| 222 | + |
| 223 | +short Buffer::consumeInt16() { |
| 224 | + return consume<short>(); |
| 225 | +} |
| 226 | +unsigned short Buffer::consumeUInt16() { |
| 227 | + return consume<unsigned short>(); |
| 228 | +} |
| 229 | + |
| 230 | +int Buffer::consumeInt32() { |
| 231 | + return consume<int>(); |
| 232 | +} |
| 233 | +unsigned int Buffer::consumeUInt32() { |
| 234 | + return consume<unsigned int>(); |
| 235 | +} |
| 236 | + |
| 237 | +long long Buffer::consumeInt64() { |
| 238 | + return consume<long long>(); |
| 239 | +} |
| 240 | +unsigned long long Buffer::consumeUInt64() { |
| 241 | + return consume<unsigned long long>(); |
| 242 | +} |
| 243 | + |
| 244 | +float Buffer::consumeFloat() { |
| 245 | + return consume<float>(); |
| 246 | +} |
| 247 | +double Buffer::consumeDouble() { |
| 248 | + return consume<double>(); |
| 249 | +} |
| 250 | + |
| 251 | +Buffer::~Buffer() { |
| 252 | + clear(); |
| 253 | +} |
0 commit comments