| 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 <bitset> |
| 19 | #include <stddef.h> |
| 20 | #include "threading.h" |
| 21 | #include "utils.h" |
| 22 |
| 23 | namespace tgvoip{ |
| 24 | class Buffer; |
| 25 |
| 26 | class BufferInputStream{ |
| 27 |
| 28 | public: |
| 29 | BufferInputStream(const unsigned char* data, size_t length); |
| 30 | BufferInputStream(const Buffer& buffer); |
| 31 | ~BufferInputStream(); |
| 32 | void Seek(size_t offset); |
| 33 | size_t GetLength(); |
| 34 | size_t GetOffset(); |
| 35 | size_t Remaining(); |
| 36 | unsigned char ReadByte(); |
| 37 | int64_t ReadInt64(); |
| 38 | int32_t ReadInt32(); |
| 39 | int16_t ReadInt16(); |
| 40 | int32_t ReadTlLength(); |
| 41 | void ReadBytes(unsigned char* to, size_t count); |
| 42 | void ReadBytes(Buffer& to); |
| 43 | BufferInputStream GetPartBuffer(size_t length, bool advance); |
| 44 |
| 45 | private: |
| 46 | void EnsureEnoughRemaining(size_t need); |
| 47 | const unsigned char* buffer; |
| 48 | size_t length; |
| 49 | size_t offset; |
| 50 | }; |
| 51 |
| 52 | class BufferOutputStream{ |
| 53 | friend class Buffer; |
| 54 | public: |
| 55 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferOutputStream); |
| 56 | BufferOutputStream(size_t size); |
| 57 | BufferOutputStream(unsigned char* buffer, size_t size); |
| 58 | ~BufferOutputStream(); |
| 59 | void WriteByte(unsigned char byte); |
| 60 | void WriteInt64(int64_t i); |
| 61 | void WriteInt32(int32_t i); |
| 62 | void WriteInt16(int16_t i); |
| 63 | void WriteBytes(const unsigned char* bytes, size_t count); |
| 64 | void WriteBytes(const Buffer& buffer); |
| 65 | void WriteBytes(const Buffer& buffer, size_t offset, size_t count); |
| 66 | unsigned char* GetBuffer(); |
| 67 | size_t GetLength(); |
| 68 | void Reset(); |
| 69 | void Rewind(size_t numBytes); |
| 70 |
| 71 | BufferOutputStream& operator=(BufferOutputStream&& other){ |
| 72 | if(this!=&other){ |
| 73 | if(!bufferProvided && buffer) |
| 74 | free(buffer); |
| 75 | buffer=other.buffer; |
| 76 | offset=other.offset; |
| 77 | size=other.size; |
| 78 | bufferProvided=other.bufferProvided; |
| 79 | other.buffer=NULL; |
| 80 | } |
| 81 | return *this; |
| 82 | } |
| 83 |
| 84 | private: |
| 85 | void ExpandBufferIfNeeded(size_t need); |
| 86 | unsigned char* buffer=NULL; |
| 87 | size_t size; |
| 88 | size_t offset; |
| 89 | bool bufferProvided; |
| 90 | }; |
| 91 |
| 92 | class Buffer{ |
| 93 | public: |
| 94 | Buffer(size_t capacity){ |
| 95 | if(capacity>0){ |
| 96 | data=(unsigned char *) malloc(capacity); |
| 97 | if(!data) |
| 98 | throw std::bad_alloc(); |
| 99 | }else{ |
| 100 | data=NULL; |
| 101 | } |
| 102 | length=capacity; |
| 103 | }; |
| 104 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(Buffer); // use Buffer::CopyOf to copy contents explicitly |
| 105 | Buffer(Buffer&& other) noexcept { |
| 106 | data=other.data; |
| 107 | length=other.length; |
| 108 | freeFn=other.freeFn; |
| 109 | reallocFn=other.reallocFn; |
| 110 | other.data=NULL; |
| 111 | }; |
| 112 | Buffer(BufferOutputStream&& stream){ |
| 113 | data=stream.buffer; |
| 114 | length=stream.offset; |
| 115 | stream.buffer=NULL; |
| 116 | } |
| 117 | Buffer(){ |
| 118 | data=NULL; |
| 119 | length=0; |
| 120 | } |
| 121 | ~Buffer(){ |
| 122 | if(data){ |
| 123 | if(freeFn) |
| 124 | freeFn(data); |
| 125 | else |
| 126 | free(data); |
| 127 | } |
| 128 | data=NULL; |
| 129 | length=0; |
| 130 | }; |
| 131 | Buffer& operator=(Buffer&& other){ |
| 132 | if(this!=&other){ |
| 133 | if(data){ |
| 134 | if(freeFn) |
| 135 | freeFn(data); |
| 136 | else |
| 137 | free(data); |
| 138 | } |
| 139 | data=other.data; |
| 140 | length=other.length; |
| 141 | freeFn=other.freeFn; |
| 142 | reallocFn=other.reallocFn; |
| 143 | other.data=NULL; |
| 144 | other.length=0; |
| 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 | if(reallocFn) |
| 178 | data=(unsigned char *) reallocFn(data, newSize); |
| 179 | else |
| 180 | data=(unsigned char *) realloc(data, newSize); |
| 181 | if(!data) |
| 182 | throw std::bad_alloc(); |
| 183 | length=newSize; |
| 184 | } |
| 185 | size_t Length() const{ |
| 186 | return length; |
| 187 | } |
| 188 | bool IsEmpty() const{ |
| 189 | return length==0 || !data; |
| 190 | } |
| 191 | static Buffer CopyOf(const Buffer& other){ |
| 192 | if(other.IsEmpty()) |
| 193 | return Buffer(); |
| 194 | Buffer buf(other.length); |
| 195 | buf.CopyFrom(other, other.length); |
| 196 | return buf; |
| 197 | } |
| 198 | static Buffer CopyOf(const Buffer& other, size_t offset, size_t length){ |
| 199 | if(offset+length>other.Length()) |
| 200 | throw std::out_of_range("offset+length out of bounds"); |
| 201 | Buffer buf(length); |
| 202 | buf.CopyFrom(other, length, offset); |
| 203 | return buf; |
| 204 | } |
| 205 | static Buffer Wrap(unsigned char* data, size_t size, std::function<void(void*)> freeFn, std::function<void*(void*, size_t)> reallocFn){ |
| 206 | Buffer b=Buffer(); |
| 207 | b.data=data; |
| 208 | b.length=size; |
| 209 | b.freeFn=freeFn; |
| 210 | b.reallocFn=reallocFn; |
| 211 | return b; |
| 212 | } |
| 213 | private: |
| 214 | unsigned char* data; |
| 215 | size_t length; |
| 216 | std::function<void(void*)> freeFn; |
| 217 | std::function<void*(void*, size_t)> reallocFn; |
| 218 | }; |
| 219 |
| 220 | template <typename T, size_t size, typename AVG_T=T> class HistoricBuffer{ |
| 221 | public: |
| 222 | HistoricBuffer(){ |
| 223 | std::fill(data.begin(), data.end(), (T)0); |
| 224 | } |
| 225 |
| 226 | AVG_T Average() const { |
| 227 | AVG_T avg=(AVG_T)0; |
| 228 | for(T i:data){ |
| 229 | avg+=i; |
| 230 | } |
| 231 | return avg/(AVG_T)size; |
| 232 | } |
| 233 |
| 234 | AVG_T Average(size_t firstN) const { |
| 235 | AVG_T avg=(AVG_T)0; |
| 236 | for(size_t i=0;i<firstN;i++){ |
| 237 | avg+=(*this)[i]; |
| 238 | } |
| 239 | return avg/(AVG_T)firstN; |
| 240 | } |
| 241 |
| 242 | AVG_T NonZeroAverage() const { |
| 243 | AVG_T avg=(AVG_T)0; |
| 244 | int nonZeroCount=0; |
| 245 | for(T i:data){ |
| 246 | if(i!=0){ |
| 247 | nonZeroCount++; |
| 248 | avg+=i; |
| 249 | } |
| 250 | } |
| 251 | if(nonZeroCount==0) |
| 252 | return (AVG_T)0; |
| 253 | return avg/(AVG_T)nonZeroCount; |
| 254 | } |
| 255 |
| 256 | void Add(T el){ |
| 257 | data[offset]=el; |
| 258 | offset=(offset+1)%size; |
| 259 | } |
| 260 |
| 261 | T Min() const { |
| 262 | T min=std::numeric_limits<T>::max(); |
| 263 | for(T i:data){ |
| 264 | if(i<min) |
| 265 | min=i; |
| 266 | } |
| 267 | return min; |
| 268 | } |
| 269 |
| 270 | T Max() const { |
| 271 | T max=std::numeric_limits<T>::min(); |
| 272 | for(T i:data){ |
| 273 | if(i>max) |
| 274 | max=i; |
| 275 | } |
| 276 | return max; |
| 277 | } |
| 278 |
| 279 | void Reset(){ |
| 280 | std::fill(data.begin(), data.end(), (T)0); |
| 281 | offset=0; |
| 282 | } |
| 283 |
| 284 | T operator[](size_t i) const { |
| 285 | assert(i<size); |
| 286 | // [0] should return the most recent entry, [1] the one before it, and so on |
| 287 | ptrdiff_t _i=offset-i-1; |
| 288 | if(_i<0) |
| 289 | _i=size+_i; |
| 290 | return data[_i]; |
| 291 | } |
| 292 |
| 293 | T& operator[](size_t i){ |
| 294 | assert(i<size); |
| 295 | // [0] should return the most recent entry, [1] the one before it, and so on |
| 296 | ptrdiff_t _i=offset-i-1; |
| 297 | if(_i<0) |
| 298 | _i=size+_i; |
| 299 | return data[_i]; |
| 300 | } |
| 301 |
| 302 | size_t Size() const { |
| 303 | return size; |
| 304 | } |
| 305 | private: |
| 306 | std::array<T, size> data; |
| 307 | ptrdiff_t offset=0; |
| 308 | }; |
| 309 |
| 310 | template <size_t bufSize, size_t bufCount> class BufferPool{ |
| 311 | public: |
| 312 | TGVOIP_DISALLOW_COPY_AND_ASSIGN(BufferPool); |
| 313 | BufferPool(){ |
| 314 | bufferStart=(unsigned char*)malloc(bufSize*bufCount); |
| 315 | if(!bufferStart) |
| 316 | throw std::bad_alloc(); |
| 317 | }; |
| 318 | ~BufferPool(){ |
| 319 | assert(usedBuffers.none()); |
| 320 | free(bufferStart); |
| 321 | }; |
| 322 | Buffer Get(){ |
| 323 | auto freeFn=[this](void* _buf){ |
| 324 | assert(_buf!=NULL); |
| 325 | unsigned char* buf=(unsigned char*)_buf; |
| 326 | size_t offset=buf-bufferStart; |
| 327 | assert(offset%bufSize==0); |
| 328 | size_t index=offset/bufSize; |
| 329 | assert(index<bufCount); |
| 330 |
| 331 | MutexGuard m(mutex); |
| 332 | assert(usedBuffers.test(index)); |
| 333 | usedBuffers[index]=0; |
| 334 | }; |
| 335 | auto resizeFn=[](void* buf, size_t newSize)->void*{ |
| 336 | if(newSize>bufSize) |
| 337 | throw std::invalid_argument("newSize>bufferSize"); |
| 338 | return buf; |
| 339 | }; |
| 340 | MutexGuard m(mutex); |
| 341 | for(size_t i=0;i<bufCount;i++){ |
| 342 | if(!usedBuffers[i]){ |
| 343 | usedBuffers[i]=1; |
| 344 | return Buffer::Wrap(bufferStart+(bufSize*i), bufSize, freeFn, resizeFn); |
| 345 | } |
| 346 | } |
| 347 | throw std::bad_alloc(); |
| 348 | } |
| 349 |
| 350 | private: |
| 351 | std::bitset<bufCount> usedBuffers; |
| 352 | unsigned char* bufferStart; |
| 353 | Mutex mutex; |
| 354 | }; |
| 355 | } |
| 356 |
| 357 | #endif //LIBTGVOIP_BUFFERINPUTSTREAM_H |
| 358 |