| 6 |
| 7 | #include "Buffers.h" |
| 8 | #include <assert.h> |
| 9 | #include <string.h> |
| 10 | #include <exception> |
| 11 | #include <stdexcept> |
| 12 | #include <stdlib.h> |
| 13 | #include "logging.h" |
| 14 |
| 15 | using namespace tgvoip; |
| 16 |
| 17 | #pragma mark - BufferInputStream |
| 18 |
| 19 | BufferInputStream::BufferInputStream(const unsigned char* data, size_t length){ |
| 20 | this->buffer=data; |
| 21 | this->length=length; |
| 22 | offset=0; |
| 23 | } |
| 24 |
| 25 | BufferInputStream::BufferInputStream(const Buffer &buffer){ |
| 26 | this->buffer=*buffer; |
| 27 | this->length=buffer.Length(); |
| 28 | offset=0; |
| 29 | } |
| 30 |
| 31 | BufferInputStream::~BufferInputStream(){ |
| 32 |
| 33 | } |
| 34 |
| 35 |
| 36 | void BufferInputStream::Seek(size_t offset){ |
| 37 | if(offset>length){ |
| 38 | throw std::out_of_range("Not enough bytes in buffer"); |
| 39 | } |
| 40 | this->offset=offset; |
| 41 | } |
| 42 |
| 43 | size_t BufferInputStream::GetLength(){ |
| 44 | return length; |
| 45 | } |
| 46 |
| 47 | size_t BufferInputStream::GetOffset(){ |
| 48 | return offset; |
| 49 | } |
| 50 |
| 51 | size_t BufferInputStream::Remaining(){ |
| 52 | return length-offset; |
| 53 | } |
| 54 |
| 55 | unsigned char BufferInputStream::ReadByte(){ |
| 56 | EnsureEnoughRemaining(1); |
| 57 | return (unsigned char)buffer[offset++]; |
| 58 | } |
| 59 |
| 60 | int32_t BufferInputStream::ReadInt32(){ |
| 61 | EnsureEnoughRemaining(4); |
| 62 | int32_t res=((int32_t)buffer[offset] & 0xFF) | |
| 63 | (((int32_t)buffer[offset+1] & 0xFF) << 8) | |
| 64 | (((int32_t)buffer[offset+2] & 0xFF) << 16) | |
| 65 | (((int32_t)buffer[offset+3] & 0xFF) << 24); |
| 66 | offset+=4; |
| 67 | return res; |
| 68 | } |
| 69 |
| 70 | int64_t BufferInputStream::ReadInt64(){ |
| 71 | EnsureEnoughRemaining(8); |
| 72 | int64_t res=((int64_t)buffer[offset] & 0xFF) | |
| 73 | (((int64_t)buffer[offset+1] & 0xFF) << 8) | |
| 74 | (((int64_t)buffer[offset+2] & 0xFF) << 16) | |
| 75 | (((int64_t)buffer[offset+3] & 0xFF) << 24) | |
| 76 | (((int64_t)buffer[offset+4] & 0xFF) << 32) | |
| 77 | (((int64_t)buffer[offset+5] & 0xFF) << 40) | |
| 78 | (((int64_t)buffer[offset+6] & 0xFF) << 48) | |
| 79 | (((int64_t)buffer[offset+7] & 0xFF) << 56); |
| 80 | offset+=8; |
| 81 | return res; |
| 82 | } |
| 83 |
| 84 | int16_t BufferInputStream::ReadInt16(){ |
| 85 | EnsureEnoughRemaining(2); |
| 86 | int16_t res=(uint16_t)buffer[offset] | ((uint16_t)buffer[offset+1] << 8); |
| 87 | offset+=2; |
| 88 | return res; |
| 89 | } |
| 90 |
| 91 |
| 92 | int32_t BufferInputStream::ReadTlLength(){ |
| 93 | unsigned char l=ReadByte(); |
| 94 | if(l<254) |
| 95 | return l; |
| 96 | assert(length-offset>=3); |
| 97 | EnsureEnoughRemaining(3); |
| 98 | int32_t res=((int32_t)buffer[offset] & 0xFF) | |
| 99 | (((int32_t)buffer[offset+1] & 0xFF) << 8) | |
| 100 | (((int32_t)buffer[offset+2] & 0xFF) << 16); |
| 101 | offset+=3; |
| 102 | return res; |
| 103 | } |
| 104 |
| 105 | void BufferInputStream::ReadBytes(unsigned char *to, size_t count){ |
| 106 | EnsureEnoughRemaining(count); |
| 107 | memcpy(to, buffer+offset, count); |
| 108 | offset+=count; |
| 109 | } |
| 110 |
| 111 | void BufferInputStream::ReadBytes(Buffer &to){ |
| 112 | ReadBytes(*to, to.Length()); |
| 113 | } |
| 114 |
| 115 | BufferInputStream BufferInputStream::GetPartBuffer(size_t length, bool advance){ |
| 116 | EnsureEnoughRemaining(length); |
| 117 | BufferInputStream s=BufferInputStream(buffer+offset, length); |
| 118 | if(advance) |
| 119 | offset+=length; |
| 120 | return s; |
| 121 | } |
| 122 |
| 123 | void BufferInputStream::EnsureEnoughRemaining(size_t need){ |
| 124 | if(length-offset<need){ |
| 125 | throw std::out_of_range("Not enough bytes in buffer"); |
| 126 | } |
| 127 | } |
| 128 |
| 129 | #pragma mark - BufferOutputStream |
| 130 |
| 131 | BufferOutputStream::BufferOutputStream(size_t size){ |
| 132 | buffer=(unsigned char*) malloc(size); |
| 133 | if(!buffer) |
| 134 | throw std::bad_alloc(); |
| 135 | offset=0; |
| 136 | this->size=size; |
| 137 | bufferProvided=false; |
| 138 | } |
| 139 |
| 140 | BufferOutputStream::BufferOutputStream(unsigned char *buffer, size_t size){ |
| 141 | this->buffer=buffer; |
| 142 | this->size=size; |
| 143 | offset=0; |
| 144 | bufferProvided=true; |
| 145 | } |
| 146 |
| 147 | BufferOutputStream::~BufferOutputStream(){ |
| 148 | if(!bufferProvided && buffer) |
| 149 | free(buffer); |
| 150 | } |
| 151 |
| 152 | void BufferOutputStream::WriteByte(unsigned char byte){ |
| 153 | this->ExpandBufferIfNeeded(1); |
| 154 | buffer[offset++]=byte; |
| 155 | } |
| 156 |
| 157 | void BufferOutputStream::WriteInt32(int32_t i){ |
| 158 | this->ExpandBufferIfNeeded(4); |
| 159 | buffer[offset+3]=(unsigned char)((i >> 24) & 0xFF); |
| 160 | buffer[offset+2]=(unsigned char)((i >> 16) & 0xFF); |
| 161 | buffer[offset+1]=(unsigned char)((i >> 8) & 0xFF); |
| 162 | buffer[offset]=(unsigned char)(i & 0xFF); |
| 163 | offset+=4; |
| 164 | } |
| 165 |
| 166 | void BufferOutputStream::WriteInt64(int64_t i){ |
| 167 | this->ExpandBufferIfNeeded(8); |
| 168 | buffer[offset+7]=(unsigned char)((i >> 56) & 0xFF); |
| 169 | buffer[offset+6]=(unsigned char)((i >> 48) & 0xFF); |
| 170 | buffer[offset+5]=(unsigned char)((i >> 40) & 0xFF); |
| 171 | buffer[offset+4]=(unsigned char)((i >> 32) & 0xFF); |
| 172 | buffer[offset+3]=(unsigned char)((i >> 24) & 0xFF); |
| 173 | buffer[offset+2]=(unsigned char)((i >> 16) & 0xFF); |
| 174 | buffer[offset+1]=(unsigned char)((i >> 8) & 0xFF); |
| 175 | buffer[offset]=(unsigned char)(i & 0xFF); |
| 176 | offset+=8; |
| 177 | } |
| 178 |
| 179 | void BufferOutputStream::WriteInt16(int16_t i){ |
| 180 | this->ExpandBufferIfNeeded(2); |
| 181 | buffer[offset+1]=(unsigned char)((i >> 8) & 0xFF); |
| 182 | buffer[offset]=(unsigned char)(i & 0xFF); |
| 183 | offset+=2; |
| 184 | } |
| 185 |
| 186 | void BufferOutputStream::WriteBytes(const unsigned char *bytes, size_t count){ |
| 187 | this->ExpandBufferIfNeeded(count); |
| 188 | memcpy(buffer+offset, bytes, count); |
| 189 | offset+=count; |
| 190 | } |
| 191 |
| 192 | void BufferOutputStream::WriteBytes(const Buffer &buffer){ |
| 193 | WriteBytes(*buffer, buffer.Length()); |
| 194 | } |
| 195 |
| 196 | void BufferOutputStream::WriteBytes(const Buffer &buffer, size_t offset, size_t count){ |
| 197 | if(offset+count>buffer.Length()) |
| 198 | throw std::out_of_range("offset out of buffer bounds"); |
| 199 | WriteBytes(*buffer+offset, count); |
| 200 | } |
| 201 |
| 202 | unsigned char *BufferOutputStream::GetBuffer(){ |
| 203 | return buffer; |
| 204 | } |
| 205 |
| 206 | size_t BufferOutputStream::GetLength(){ |
| 207 | return offset; |
| 208 | } |
| 209 |
| 210 | void BufferOutputStream::ExpandBufferIfNeeded(size_t need){ |
| 211 | if(offset+need>size){ |
| 212 | if(bufferProvided){ |
| 213 | throw std::out_of_range("buffer overflow"); |
| 214 | } |
| 215 | if(need<1024){ |
| 216 | buffer=(unsigned char *) realloc(buffer, size+1024); |
| 217 | size+=1024; |
| 218 | }else{ |
| 219 | buffer=(unsigned char *) realloc(buffer, size+need); |
| 220 | size+=need; |
| 221 | } |
| 222 | if(!buffer) |
| 223 | throw std::bad_alloc(); |
| 224 | } |
| 225 | } |
| 226 |
| 227 |
| 228 | void BufferOutputStream::Reset(){ |
| 229 | offset=0; |
| 230 | } |
| 231 |
| 232 | void BufferOutputStream::Rewind(size_t numBytes){ |
| 233 | if(numBytes>offset) |
| 234 | throw std::out_of_range("buffer underflow"); |
| 235 | offset-=numBytes; |
| 236 | } |
| 237 |
| 238 | #pragma mark - BufferPool |
| 239 |
| 240 | BufferPool::BufferPool(unsigned int size, unsigned int count){ |
| 241 | assert(count<=64); |
| 242 | buffers[0]=(unsigned char*) malloc(size*count); |
| 243 | bufferCount=count; |
| 244 | unsigned int i; |
| 245 | for(i=1;i<count;i++){ |
| 246 | buffers[i]=buffers[0]+i*size; |
| 247 | } |
| 248 | usedBuffers=0; |
| 249 | this->size=size; |
| 250 | } |
| 251 |
| 252 | BufferPool::~BufferPool(){ |
| 253 | free(buffers[0]); |
| 254 | } |
| 255 |
| 256 | unsigned char* BufferPool::Get(){ |
| 257 | MutexGuard m(mutex); |
| 258 | int i; |
| 259 | for(i=0;i<bufferCount;i++){ |
| 260 | if(!((usedBuffers >> i) & 1)){ |
| 261 | usedBuffers|=(1LL << i); |
| 262 | return buffers[i]; |
| 263 | } |
| 264 | } |
| 265 | return NULL; |
| 266 | } |
| 267 |
| 268 | void BufferPool::Reuse(unsigned char* buffer){ |
| 269 | MutexGuard m(mutex); |
| 270 | int i; |
| 271 | for(i=0;i<bufferCount;i++){ |
| 272 | if(buffers[i]==buffer){ |
| 273 | usedBuffers&= ~(1LL << i); |
| 274 | return; |
| 275 | } |
| 276 | } |
| 277 | LOGE("pointer passed isn't a valid buffer from this pool"); |
| 278 | abort(); |
| 279 | } |
| 280 |
| 281 | size_t BufferPool::GetSingleBufferSize(){ |
| 282 | return size; |
| 283 | } |
| 284 |
| 285 | size_t BufferPool::GetBufferCount(){ |
| 286 | return (size_t) bufferCount; |
| 287 | } |
| 288 |
| 289 | #pragma mark - Buffer |
| 290 |
| 291 |