| 6 |
| 7 | #ifndef LIBTGVOIP_BUFFERINPUTSTREAM_H |
| 8 | #define LIBTGVOIP_BUFFERINPUTSTREAM_H |
| 9 |
| 10 | #include <stdio.h> |
| 11 | #include <stdint.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <assert.h> |
| 15 | #include <stdexcept> |
| 16 | #include <array> |
| 17 | #include <limits> |
| 18 | #include <stddef.h> |
| 19 | #include "threading.h" |
| 20 | #include "utils.h" |
| 21 |
| 22 | namespace tgvoip{ |
| 23 | class Buffer; |
| 24 |
| 25 | class BufferInputStream{ |
| 26 |
| 27 | public: |
| 28 | BufferInputStream(const unsigned char* data, size_t length); |
| 29 | BufferInputStream(const Buffer& buffer); |
| 30 | ~BufferInputStream(); |
| 31 | void Seek(size_t offset); |
| 32 | size_t GetLength(); |
| 33 | size_t GetOffset(); |
| 34 | size_t Remaining(); |
| 35 | unsigned char ReadByte(); |
| 36 | int64_t ReadInt64(); |
| 37 | int32_t ReadInt32(); |
| 38 | int16_t ReadInt16(); |
| 39 | int32_t ReadTlLength(); |
| 40 | void ReadBytes(unsigned char* to, size_t count); |
| 41 | void ReadBytes(Buffer& to); |
| 42 | BufferInputStream GetPartBuffer(size_t length, bool advance); |
| 43 |
| 44 | private: |
| 45 | void EnsureEnoughRemaining(size_t need); |
| 46 | const unsigned char* buffer; |
| 47 | size_t length; |
| 48 | size_t offset; |
| 49 | }; |
| 50 |
| 51 | class BufferOutputStream{ |
| 52 | friend class Buffer; |
| 53 | public: |
| 54 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferOutputStream); |
| 55 | BufferOutputStream(size_t size); |
| 56 | BufferOutputStream(unsigned char* buffer, size_t size); |
| 57 | ~BufferOutputStream(); |
| 58 | void WriteByte(unsigned char byte); |
| 59 | void WriteInt64(int64_t i); |
| 60 | void WriteInt32(int32_t i); |
| 61 | void WriteInt16(int16_t i); |
| 62 | void WriteBytes(const unsigned char* bytes, size_t count); |
| 63 | void WriteBytes(const Buffer& buffer); |
| 64 | void WriteBytes(const Buffer& buffer, size_t offset, size_t count); |
| 65 | unsigned char* GetBuffer(); |
| 66 | size_t GetLength(); |
| 67 | void Reset(); |
| 68 | void Rewind(size_t numBytes); |
| 69 |
| 70 | BufferOutputStream& operator=(BufferOutputStream&& other){ |
| 71 | if(this!=&other){ |
| 72 | if(!bufferProvided && buffer) |
| 73 | free(buffer); |
| 74 | buffer=other.buffer; |
| 75 | offset=other.offset; |
| 76 | size=other.size; |
| 77 | bufferProvided=other.bufferProvided; |
| 78 | other.buffer=NULL; |
| 79 | } |
| 80 | return *this; |
| 81 | } |
| 82 |
| 83 | private: |
| 84 | void ExpandBufferIfNeeded(size_t need); |
| 85 | unsigned char* buffer=NULL; |
| 86 | size_t size; |
| 87 | size_t offset; |
| 88 | bool bufferProvided; |
| 89 | }; |
| 90 |
| 91 | class BufferPool{ |
| 92 | public: |
| 93 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferPool); |
| 94 | BufferPool(unsigned int size, unsigned int count); |
| 95 | ~BufferPool(); |
| 96 | unsigned char* Get(); |
| 97 | void Reuse(unsigned char* buffer); |
| 98 | size_t GetSingleBufferSize(); |
| 99 | size_t GetBufferCount(); |
| 100 |
| 101 | private: |
| 102 | uint64_t usedBuffers; |
| 103 | int bufferCount; |
| 104 | size_t size; |
| 105 | unsigned char* buffers[64]; |
| 106 | Mutex mutex; |
| 107 | }; |
| 108 |
| 109 | class Buffer{ |
| 110 | public: |
| 111 | Buffer(size_t capacity){ |
| 112 | if(capacity>0) |
| 113 | data=(unsigned char *) malloc(capacity); |
| 114 | else |
| 115 | data=NULL; |
| 116 | length=capacity; |
| 117 | }; |
| 118 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(Buffer); // use Buffer::CopyOf to copy contents explicitly |
| 119 | Buffer(Buffer&& other) noexcept { |
| 120 | data=other.data; |
| 121 | length=other.length; |
| 122 | other.data=NULL; |
| 123 | }; |
| 124 | Buffer(BufferOutputStream&& stream){ |
| 125 | data=stream.buffer; |
| 126 | length=stream.offset; |
| 127 | stream.buffer=NULL; |
| 128 | } |
| 129 | Buffer(){ |
| 130 | data=NULL; |
| 131 | length=0; |
| 132 | } |
| 133 | ~Buffer(){ |
| 134 | if(data) |
| 135 | free(data); |
| 136 | data=NULL; |
| 137 | }; |
| 138 | Buffer& operator=(Buffer&& other){ |
| 139 | if(this!=&other){ |
| 140 | if(data) |
| 141 | free(data); |
| 142 | data=other.data; |
| 143 | length=other.length; |
| 144 | other.data=NULL; |
| 145 | } |
| 146 | return *this; |
| 147 | } |
| 148 | unsigned char& operator[](size_t i){ |
| 149 | if(i>=length) |
| 150 | throw std::out_of_range(""); |
| 151 | return data[i]; |
| 152 | } |
| 153 | const unsigned char& operator[](size_t i) const{ |
| 154 | if(i>=length) |
| 155 | throw std::out_of_range(""); |
| 156 | return data[i]; |
| 157 | } |
| 158 | unsigned char* operator*(){ |
| 159 | return data; |
| 160 | } |
| 161 | const unsigned char* operator*() const{ |
| 162 | return data; |
| 163 | } |
| 164 | void CopyFrom(const Buffer& other, size_t count, size_t srcOffset=0, size_t dstOffset=0){ |
| 165 | if(!other.data) |
| 166 | throw std::invalid_argument("CopyFrom can't copy from NULL"); |
| 167 | if(other.length<srcOffset+count || length<dstOffset+count) |
| 168 | throw std::out_of_range("Out of offset+count bounds of either buffer"); |
| 169 | memcpy(data+dstOffset, other.data+srcOffset, count); |
| 170 | } |
| 171 | void CopyFrom(const void* ptr, size_t dstOffset, size_t count){ |
| 172 | if(length<dstOffset+count) |
| 173 | throw std::out_of_range("Offset+count is out of bounds"); |
| 174 | memcpy(data+dstOffset, ptr, count); |
| 175 | } |
| 176 | void Resize(size_t newSize){ |
| 177 | data=(unsigned char *) realloc(data, newSize); |
| 178 | length=newSize; |
| 179 | } |
| 180 | size_t Length() const{ |
| 181 | return length; |
| 182 | } |
| 183 | bool IsEmpty() const{ |
| 184 | return length==0; |
| 185 | } |
| 186 | static Buffer CopyOf(const Buffer& other){ |
| 187 | Buffer buf(other.length); |
| 188 | buf.CopyFrom(other, other.length); |
| 189 | return buf; |
| 190 | } |
| 191 | private: |
| 192 | unsigned char* data; |
| 193 | size_t length; |
| 194 | }; |
| 195 |
| 196 | template <typename T, size_t size, typename AVG_T=T> class HistoricBuffer{ |
| 197 | public: |
| 198 | HistoricBuffer(){ |
| 199 | std::fill(data.begin(), data.end(), (T)0); |
| 200 | } |
| 201 |
| 202 | AVG_T Average(){ |
| 203 | AVG_T avg=(AVG_T)0; |
| 204 | for(T& i:data){ |
| 205 | avg+=i; |
| 206 | } |
| 207 | return avg/(AVG_T)size; |
| 208 | } |
| 209 |
| 210 | AVG_T Average(size_t firstN){ |
| 211 | AVG_T avg=(AVG_T)0; |
| 212 | for(size_t i=0;i<firstN;i++){ |
| 213 | avg+=(*this)[i]; |
| 214 | } |
| 215 | return avg/(AVG_T)firstN; |
| 216 | } |
| 217 |
| 218 | AVG_T NonZeroAverage(){ |
| 219 | AVG_T avg=(AVG_T)0; |
| 220 | int nonZeroCount=0; |
| 221 | for(T& i:data){ |
| 222 | if(i!=0){ |
| 223 | nonZeroCount++; |
| 224 | avg+=i; |
| 225 | } |
| 226 | } |
| 227 | if(nonZeroCount==0) |
| 228 | return (AVG_T)0; |
| 229 | return avg/(AVG_T)nonZeroCount; |
| 230 | } |
| 231 |
| 232 | void Add(T el){ |
| 233 | data[offset]=el; |
| 234 | offset=(offset+1)%size; |
| 235 | } |
| 236 |
| 237 | T Min(){ |
| 238 | T min=std::numeric_limits<T>::max(); |
| 239 | for(T& i:data){ |
| 240 | if(i<min) |
| 241 | min=i; |
| 242 | } |
| 243 | return min; |
| 244 | } |
| 245 |
| 246 | T Max(){ |
| 247 | T max=std::numeric_limits<T>::min(); |
| 248 | for(T& i:data){ |
| 249 | if(i>max) |
| 250 | max=i; |
| 251 | } |
| 252 | return max; |
| 253 | } |
| 254 |
| 255 | void Reset(){ |
| 256 | std::fill(data.begin(), data.end(), (T)0); |
| 257 | offset=0; |
| 258 | } |
| 259 |
| 260 | T& operator[](size_t i){ |
| 261 | assert(i<size); |
| 262 | // [0] should return the most recent entry, [1] the one before it, and so on |
| 263 | ptrdiff_t _i=offset-i-1; |
| 264 | if(_i<0) |
| 265 | _i=size+_i; |
| 266 | return data[_i]; |
| 267 | } |
| 268 |
| 269 | size_t Size(){ |
| 270 | return size; |
| 271 | } |
| 272 | private: |
| 273 | std::array<T, size> data; |
| 274 | ptrdiff_t offset=0; |
| 275 | }; |
| 276 | } |
| 277 |
| 278 | #endif //LIBTGVOIP_BUFFERINPUTSTREAM_H |
| 279 |