| 6 |
| * Copyright Nikolai Kudashov, 2015-2018. |
| 7 | */ |
| 8 |
| 9 | #include <memory.h> |
| 10 | #include <stdlib.h> |
| 11 | #include "NativeByteBuffer.h" |
| 12 | #include "FileLog.h" |
| 13 | #include "ByteArray.h" |
| 14 | #include "ConnectionsManager.h" |
| 15 | #include "BuffersStorage.h" |
| 16 |
| 17 | NativeByteBuffer::NativeByteBuffer(uint32_t size) { |
| 18 | #ifdef ANDROID |
| 19 | if (jclass_ByteBuffer != nullptr) { |
| 20 | JNIEnv *env = 0; |
| 21 | if (javaVm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) { |
| 22 | if (LOGS_ENABLED) DEBUG_E("can't get jnienv"); |
| 23 | exit(1); |
| 24 | } |
| 25 | javaByteBuffer = env->CallStaticObjectMethod(jclass_ByteBuffer, jclass_ByteBuffer_allocateDirect, size); |
| 26 | if (javaByteBuffer == nullptr) { |
| 27 | if (LOGS_ENABLED) DEBUG_E("can't create javaByteBuffer"); |
| 28 | exit(1); |
| 29 | } |
| 30 | jobject globalRef = env->NewGlobalRef(javaByteBuffer); |
| 31 | env->DeleteLocalRef(javaByteBuffer); |
| 32 | javaByteBuffer = globalRef; |
| 33 | buffer = (uint8_t *) env->GetDirectBufferAddress(javaByteBuffer); |
| 34 | bufferOwner = false; |
| 35 | } else { |
| 36 | #endif |
| 37 | buffer = new uint8_t[size]; |
| 38 | bufferOwner = true; |
| 39 | #ifdef ANDROID |
| 40 | } |
| 41 | #endif |
| 42 | if (buffer == nullptr) { |
| 43 | if (LOGS_ENABLED) DEBUG_E("can't allocate NativeByteBuffer buffer"); |
| 44 | exit(1); |
| 45 | } |
| 46 | _limit = _capacity = size; |
| 47 | } |
| 48 |
| 49 | NativeByteBuffer::NativeByteBuffer(bool calculate) { |
| 50 | calculateSizeOnly = calculate; |
| 51 | } |
| 52 |
| 53 | NativeByteBuffer::NativeByteBuffer(uint8_t *buff, uint32_t length) { |
| 54 | buffer = buff; |
| 55 | sliced = true; |
| 56 | _limit = _capacity = length; |
| 57 | } |
| 58 |
| 59 | NativeByteBuffer::~NativeByteBuffer() { |
| 60 | #ifdef ANDROID |
| 61 | if (javaByteBuffer != nullptr) { |
| 62 | JNIEnv *env = 0; |
| 63 | if (javaVm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) { |
| 64 | if (LOGS_ENABLED) DEBUG_E("can't get jnienv"); |
| 65 | exit(1); |
| 66 | } |
| 67 | env->DeleteGlobalRef(javaByteBuffer); |
| 68 | javaByteBuffer = nullptr; |
| 69 | } |
| 70 | #endif |
| 71 | if (bufferOwner && !sliced && buffer != nullptr) { |
| 72 | delete[] buffer; |
| 73 | buffer = nullptr; |
| 74 | } |
| 75 | } |
| 76 |
| 77 | uint32_t NativeByteBuffer::position() { |
| 78 | return _position; |
| 79 | } |
| 80 |
| 81 | void NativeByteBuffer::position(uint32_t position) { |
| 82 | if (position > _limit) { |
| 83 | return; |
| 84 | } |
| 85 | _position = position; |
| 86 | } |
| 87 |
| 88 | uint32_t NativeByteBuffer::capacity() { |
| 89 | return _capacity; |
| 90 | } |
| 91 |
| 92 | uint32_t NativeByteBuffer::limit() { |
| 93 | return _limit; |
| 94 | } |
| 95 |
| 96 | uint32_t NativeByteBuffer::remaining() { |
| 97 | return _limit - _position; |
| 98 | } |
| 99 |
| 100 | void NativeByteBuffer::clearCapacity() { |
| 101 | if (!calculateSizeOnly) { |
| 102 | return; |
| 103 | } |
| 104 | _capacity = 0; |
| 105 | } |
| 106 |
| 107 | void NativeByteBuffer::limit(uint32_t limit) { |
| 108 | if (limit > _capacity) { |
| 109 | return; |
| 110 | } |
| 111 | if (_position > limit) { |
| 112 | _position = limit; |
| 113 | } |
| 114 | _limit = limit; |
| 115 | } |
| 116 |
| 117 | void NativeByteBuffer::flip() { |
| 118 | _limit = _position; |
| 119 | _position = 0; |
| 120 | } |
| 121 |
| 122 | void NativeByteBuffer::clear() { |
| 123 | _position = 0; |
| 124 | _limit = _capacity; |
| 125 | } |
| 126 |
| 127 | uint8_t *NativeByteBuffer::bytes() { |
| 128 | return buffer; |
| 129 | } |
| 130 |
| 131 | void NativeByteBuffer::rewind() { |
| 132 | _position = 0; |
| 133 | } |
| 134 |
| 135 | void NativeByteBuffer::compact() { |
| 136 | if (_position == _limit) { |
| 137 | return; |
| 138 | } |
| 139 | memmove(buffer, buffer + _position, sizeof(uint8_t) * (_limit - _position)); |
| 140 | _position = (_limit - _position); |
| 141 | _limit = _capacity; |
| 142 | } |
| 143 |
| 144 | bool NativeByteBuffer::hasRemaining() { |
| 145 | return _position < _limit; |
| 146 | } |
| 147 |
| 148 | void NativeByteBuffer::skip(uint32_t length) { |
| 149 | if (!calculateSizeOnly) { |
| 150 | if (_position + length > _limit) { |
| 151 | return; |
| 152 | } |
| 153 | _position += length; |
| 154 | } else { |
| 155 | _capacity += length; |
| 156 | } |
| 157 | } |
| 158 |
| 159 | void NativeByteBuffer::writeInt32(int32_t x, bool *error) { |
| 160 | if (!calculateSizeOnly) { |
| 161 | if (_position + 4 > _limit) { |
| 162 | if (error != nullptr) { |
| 163 | *error = true; |
| 164 | } |
| 165 | if (LOGS_ENABLED) DEBUG_E("write int32 error"); |
| 166 | return; |
| 167 | } |
| 168 | buffer[_position++] = (uint8_t) x; |
| 169 | buffer[_position++] = (uint8_t) (x >> 8); |
| 170 | buffer[_position++] = (uint8_t) (x >> 16); |
| 171 | buffer[_position++] = (uint8_t) (x >> 24); |
| 172 | } else { |
| 173 | _capacity += 4; |
| 174 | } |
| 175 | } |
| 176 |
| 177 | void NativeByteBuffer::writeInt64(int64_t x, bool *error) { |
| 178 | if (!calculateSizeOnly) { |
| 179 | if (_position + 8 > _limit) { |
| 180 | if (error != nullptr) { |
| 181 | *error = true; |
| 182 | } |
| 183 | if (LOGS_ENABLED) DEBUG_E("write int64 error"); |
| 184 | return; |
| 185 | } |
| 186 | buffer[_position++] = (uint8_t) x; |
| 187 | buffer[_position++] = (uint8_t) (x >> 8); |
| 188 | buffer[_position++] = (uint8_t) (x >> 16); |
| 189 | buffer[_position++] = (uint8_t) (x >> 24); |
| 190 | buffer[_position++] = (uint8_t) (x >> 32); |
| 191 | buffer[_position++] = (uint8_t) (x >> 40); |
| 192 | buffer[_position++] = (uint8_t) (x >> 48); |
| 193 | buffer[_position++] = (uint8_t) (x >> 56); |
| 194 | } else { |
| 195 | _capacity += 8; |
| 196 | } |
| 197 | } |
| 198 |
| 199 | void NativeByteBuffer::writeBool(bool value, bool *error) { |
| 200 | if (!calculateSizeOnly) { |
| 201 | if (value) { |
| 202 | writeInt32(0x997275b5, error); |
| 203 | } else { |
| 204 | writeInt32(0xbc799737, error); |
| 205 | } |
| 206 | } else { |
| 207 | _capacity += 4; |
| 208 | } |
| 209 | } |
| 210 |
| 211 | void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t length, bool *error) { |
| 212 | if (!calculateSizeOnly) { |
| 213 | if (_position + length > _limit) { |
| 214 | if (error != nullptr) { |
| 215 | *error = true; |
| 216 | } |
| 217 | if (LOGS_ENABLED) DEBUG_E("write bytes error"); |
| 218 | return; |
| 219 | } |
| 220 | writeBytesInternal(b, 0, length); |
| 221 | } else { |
| 222 | _capacity += length; |
| 223 | } |
| 224 | } |
| 225 |
| 226 | void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t offset, uint32_t length, bool *error) { |
| 227 | if (!calculateSizeOnly) { |
| 228 | if (_position + length > _limit) { |
| 229 | if (error != nullptr) { |
| 230 | *error = true; |
| 231 | } |
| 232 | if (LOGS_ENABLED) DEBUG_E("write bytes error"); |
| 233 | return; |
| 234 | } |
| 235 | writeBytesInternal(b, offset, length); |
| 236 | } else { |
| 237 | _capacity += length; |
| 238 | } |
| 239 | } |
| 240 |
| 241 | void NativeByteBuffer::writeBytes(NativeByteBuffer *b, bool *error) { |
| 242 | uint32_t length = b->_limit - b->_position; |
| 243 | if (length == 0) { |
| 244 | return; |
| 245 | } |
| 246 | if (!calculateSizeOnly) { |
| 247 | if (_position + length > _limit) { |
| 248 | if (error != nullptr) { |
| 249 | *error = true; |
| 250 | } |
| 251 | if (LOGS_ENABLED) DEBUG_E("write bytes error"); |
| 252 | return; |
| 253 | } |
| 254 | writeBytesInternal(b->buffer + b->_position, 0, length); |
| 255 | b->position(b->limit()); |
| 256 | } else { |
| 257 | _capacity += length; |
| 258 | } |
| 259 | } |
| 260 |
| 261 | void NativeByteBuffer::writeBytes(ByteArray *b, bool *error) { |
| 262 | if (!calculateSizeOnly) { |
| 263 | if (_position + b->length > _limit) { |
| 264 | if (error != nullptr) { |
| 265 | *error = true; |
| 266 | } |
| 267 | if (LOGS_ENABLED) DEBUG_E("write bytes error"); |
| 268 | return; |
| 269 | } |
| 270 | writeBytesInternal(b->bytes, 0, b->length); |
| 271 | } else { |
| 272 | _capacity += b->length; |
| 273 | } |
| 274 | } |
| 275 |
| 276 | void NativeByteBuffer::writeBytesInternal(uint8_t *b, uint32_t offset, uint32_t length) { |
| 277 | memcpy(buffer + _position, b + offset, sizeof(uint8_t) * length); |
| 278 | _position += length; |
| 279 | } |
| 280 |
| 281 | void NativeByteBuffer::writeByte(uint8_t i, bool *error) { |
| 282 | if (!calculateSizeOnly) { |
| 283 | if (_position + 1 > _limit) { |
| 284 | if (error != nullptr) { |
| 285 | *error = true; |
| 286 | } |
| 287 | if (LOGS_ENABLED) DEBUG_E("write byte error"); |
| 288 | return; |
| 289 | } |
| 290 | buffer[_position++] = i; |
| 291 | } else { |
| 292 | _capacity += 1; |
| 293 | } |
| 294 | } |
| 295 |
| 296 | void NativeByteBuffer::writeString(std::string s, bool *error) { |
| 297 | writeByteArray((uint8_t *) s.c_str(), (uint32_t) s.length(), error); |
| 298 | } |
| 299 |
| 300 | void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t offset, uint32_t length, bool *error) { |
| 301 | if (length <= 253) { |
| 302 | if (!calculateSizeOnly) { |
| 303 | if (_position + 1 > _limit) { |
| 304 | if (error != nullptr) { |
| 305 | *error = true; |
| 306 | } |
| 307 | if (LOGS_ENABLED) DEBUG_E("write byte array error"); |
| 308 | return; |
| 309 | } |
| 310 | buffer[_position++] = (uint8_t) length; |
| 311 | } else { |
| 312 | _capacity += 1; |
| 313 | } |
| 314 | } else { |
| 315 | if (!calculateSizeOnly) { |
| 316 | if (_position + 4 > _limit) { |
| 317 | if (error != nullptr) { |
| 318 | *error = true; |
| 319 | } |
| 320 | if (LOGS_ENABLED) DEBUG_E("write byte array error"); |
| 321 | return; |
| 322 | } |
| 323 | buffer[_position++] = (uint8_t) 254; |
| 324 | buffer[_position++] = (uint8_t) length; |
| 325 | buffer[_position++] = (uint8_t) (length >> 8); |
| 326 | buffer[_position++] = (uint8_t) (length >> 16); |
| 327 | } else { |
| 328 | _capacity += 4; |
| 329 | } |
| 330 | } |
| 331 | if (!calculateSizeOnly) { |
| 332 | if (_position + length > _limit) { |
| 333 | if (error != nullptr) { |
| 334 | *error = true; |
| 335 | } |
| 336 | if (LOGS_ENABLED) DEBUG_E("write byte array error"); |
| 337 | return; |
| 338 | } |
| 339 | writeBytesInternal(b, offset, length); |
| 340 | } else { |
| 341 | _capacity += length; |
| 342 | } |
| 343 | uint32_t addition = (length + (length <= 253 ? 1 : 4)) % 4; |
| 344 | if (addition != 0) { |
| 345 | addition = 4 - addition; |
| 346 | } |
| 347 | if (!calculateSizeOnly && _position + addition > _limit) { |
| 348 | if (error != nullptr) { |
| 349 | *error = true; |
| 350 | } |
| 351 | if (LOGS_ENABLED) DEBUG_E("write byte array error"); |
| 352 | return; |
| 353 | } |
| 354 | for (uint32_t a = 0; a < addition; a++) { |
| 355 | if (!calculateSizeOnly) { |
| 356 | buffer[_position++] = (uint8_t) 0; |
| 357 | } else { |
| 358 | _capacity += 1; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 |
| 363 | void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t length, bool *error) { |
| 364 | writeByteArray(b, 0, length, error); |
| 365 | } |
| 366 |
| 367 | void NativeByteBuffer::writeByteArray(NativeByteBuffer *b, bool *error) { |
| 368 | b->rewind(); |
| 369 | writeByteArray(b->buffer, 0, b->limit(), error); |
| 370 | } |
| 371 |
| 372 | void NativeByteBuffer::writeByteArray(ByteArray *b, bool *error) { |
| 373 | writeByteArray(b->bytes, 0, b->length, error); |
| 374 | } |
| 375 |
| 376 | void NativeByteBuffer::writeDouble(double d, bool *error) { |
| 377 | int64_t value; |
| 378 | memcpy(&value, &d, sizeof(int64_t)); |
| 379 | writeInt64(value, error); |
| 380 | } |
| 381 |
| 382 | void NativeByteBuffer::writeInt32(int32_t x) { |
| 383 | writeInt32(x, nullptr); |
| 384 | } |
| 385 |
| 386 | void NativeByteBuffer::writeInt64(int64_t x) { |
| 387 | writeInt64(x, nullptr); |
| 388 | } |
| 389 |
| 390 | void NativeByteBuffer::writeBool(bool value) { |
| 391 | writeBool(value, nullptr); |
| 392 | } |
| 393 |
| 394 | void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t length) { |
| 395 | writeBytes(b, length, nullptr); |
| 396 | } |
| 397 |
| 398 | void NativeByteBuffer::writeBytes(uint8_t *b, uint32_t offset, uint32_t length) { |
| 399 | writeBytes(b, offset, length, nullptr); |
| 400 | } |
| 401 |
| 402 | void NativeByteBuffer::writeBytes(ByteArray *b) { |
| 403 | writeBytes(b, nullptr); |
| 404 | } |
| 405 |
| 406 | void NativeByteBuffer::writeBytes(NativeByteBuffer *b) { |
| 407 | writeBytes(b, nullptr); |
| 408 | } |
| 409 |
| 410 | void NativeByteBuffer::writeByte(uint8_t i) { |
| 411 | writeByte(i, nullptr); |
| 412 | } |
| 413 |
| 414 | void NativeByteBuffer::writeString(std::string s) { |
| 415 | writeString(s, nullptr); |
| 416 | } |
| 417 |
| 418 | void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t offset, uint32_t length) { |
| 419 | writeByteArray(b, offset, length, nullptr); |
| 420 | } |
| 421 |
| 422 | void NativeByteBuffer::writeByteArray(uint8_t *b, uint32_t length) { |
| 423 | writeByteArray(b, length, nullptr); |
| 424 | } |
| 425 |
| 426 | void NativeByteBuffer::writeByteArray(NativeByteBuffer *b) { |
| 427 | writeByteArray(b, nullptr); |
| 428 | } |
| 429 |
| 430 | void NativeByteBuffer::writeByteArray(ByteArray *b) { |
| 431 | writeByteArray(b->bytes, b->length, nullptr); |
| 432 | } |
| 433 |
| 434 | void NativeByteBuffer::writeDouble(double d) { |
| 435 | writeDouble(d, nullptr); |
| 436 | } |
| 437 |
| 438 | int32_t NativeByteBuffer::readInt32(bool *error) { |
| 439 | if (_position + 4 > _limit) { |
| 440 | if (error != nullptr) { |
| 441 | *error = true; |
| 442 | } |
| 443 | if (LOGS_ENABLED) DEBUG_E("read int32 error"); |
| 444 | return 0; |
| 445 | } |
| 446 | int32_t result = ((buffer[_position] & 0xff)) | |
| 447 | ((buffer[_position + 1] & 0xff) << 8) | |
| 448 | ((buffer[_position + 2] & 0xff) << 16) | |
| 449 | ((buffer[_position + 3] & 0xff) << 24); |
| 450 | _position += 4; |
| 451 | return result; |
| 452 | } |
| 453 |
| 454 | uint32_t NativeByteBuffer::readUint32(bool *error) { |
| 455 | return (uint32_t) readInt32(error); |
| 456 | } |
| 457 |
| 458 | uint64_t NativeByteBuffer::readUint64(bool *error) { |
| 459 | return (uint64_t) readInt64(error); |
| 460 | } |
| 461 |
| 462 | int32_t NativeByteBuffer::readBigInt32(bool *error) { |
| 463 | if (_position + 4 > _limit) { |
| 464 | if (error != nullptr) { |
| 465 | *error = true; |
| 466 | } |
| 467 | if (LOGS_ENABLED) DEBUG_E("read big int32 error"); |
| 468 | return 0; |
| 469 | } |
| 470 | int32_t result = ((buffer[_position] & 0xff) << 24) | |
| 471 | ((buffer[_position + 1] & 0xff) << 16) | |
| 472 | ((buffer[_position + 2] & 0xff) << 8) | |
| 473 | ((buffer[_position + 3] & 0xff)); |
| 474 | _position += 4; |
| 475 | return result; |
| 476 | } |
| 477 |
| 478 | int64_t NativeByteBuffer::readInt64(bool *error) { |
| 479 | if (_position + 8 > _limit) { |
| 480 | if (error != nullptr) { |
| 481 | *error = true; |
| 482 | } |
| 483 | if (LOGS_ENABLED) DEBUG_E("read int64 error"); |
| 484 | return 0; |
| 485 | } |
| 486 | int64_t result = ((int64_t) (buffer[_position] & 0xff)) | |
| 487 | ((int64_t) (buffer[_position + 1] & 0xff) << 8) | |
| 488 | ((int64_t) (buffer[_position + 2] & 0xff) << 16) | |
| 489 | ((int64_t) (buffer[_position + 3] & 0xff) << 24) | |
| 490 | ((int64_t) (buffer[_position + 4] & 0xff) << 32) | |
| 491 | ((int64_t) (buffer[_position + 5] & 0xff) << 40) | |
| 492 | ((int64_t) (buffer[_position + 6] & 0xff) << 48) | |
| 493 | ((int64_t) (buffer[_position + 7] & 0xff) << 56); |
| 494 | _position += 8; |
| 495 | return result; |
| 496 | } |
| 497 |
| 498 | uint8_t NativeByteBuffer::readByte(bool *error) { |
| 499 | if (_position + 1 > _limit) { |
| 500 | if (error != nullptr) { |
| 501 | *error = true; |
| 502 | } |
| 503 | if (LOGS_ENABLED) DEBUG_E("read byte error"); |
| 504 | return 0; |
| 505 | } |
| 506 | return buffer[_position++]; |
| 507 | } |
| 508 |
| 509 | bool NativeByteBuffer::readBool(bool *error) { |
| 510 | uint32_t consructor = readUint32(error); |
| 511 | if (consructor == 0x997275b5) { |
| 512 | return true; |
| 513 | } else if (consructor == 0xbc799737) { |
| 514 | return false; |
| 515 | } |
| 516 | if (error != nullptr) { |
| 517 | *error = true; |
| 518 | if (LOGS_ENABLED) DEBUG_E("read bool error"); |
| 519 | } |
| 520 | return false; |
| 521 | } |
| 522 |
| 523 | void NativeByteBuffer::readBytes(uint8_t *b, uint32_t length, bool *error) { |
| 524 | if (length > _limit - _position) { |
| 525 | if (error != nullptr) { |
| 526 | *error = true; |
| 527 | } |
| 528 | if (LOGS_ENABLED) DEBUG_E("read bytes error"); |
| 529 | return; |
| 530 | } |
| 531 | memcpy(b, buffer + _position, length); |
| 532 | _position += length; |
| 533 | } |
| 534 |
| 535 | ByteArray *NativeByteBuffer::readBytes(uint32_t length, bool *error) { |
| 536 | if (length > _limit - _position) { |
| 537 | if (error != nullptr) { |
| 538 | *error = true; |
| 539 | } |
| 540 | if (LOGS_ENABLED) DEBUG_E("read bytes error"); |
| 541 | return nullptr; |
| 542 | } |
| 543 | ByteArray *byteArray = new ByteArray(length); |
| 544 | memcpy(byteArray->bytes, buffer + _position, sizeof(uint8_t) * length); |
| 545 | _position += length; |
| 546 | return byteArray; |
| 547 | } |
| 548 |
| 549 | std::string NativeByteBuffer::readString(bool *error) { |
| 550 | uint32_t sl = 1; |
| 551 | if (_position + 1 > _limit) { |
| 552 | if (error != nullptr) { |
| 553 | *error = true; |
| 554 | } |
| 555 | if (LOGS_ENABLED) DEBUG_E("read string error"); |
| 556 | return std::string(""); |
| 557 | } |
| 558 | uint32_t l = buffer[_position++]; |
| 559 | if (l >= 254) { |
| 560 | if (_position + 3 > _limit) { |
| 561 | if (error != nullptr) { |
| 562 | *error = true; |
| 563 | } |
| 564 | if (LOGS_ENABLED) DEBUG_E("read string error"); |
| 565 | return std::string(""); |
| 566 | } |
| 567 | l = buffer[_position] | (buffer[_position + 1] << 8) | (buffer[_position + 2] << 16); |
| 568 | _position += 3; |
| 569 | sl = 4; |
| 570 | } |
| 571 | uint32_t addition = (l + sl) % 4; |
| 572 | if (addition != 0) { |
| 573 | addition = 4 - addition; |
| 574 | } |
| 575 | if (_position + l + addition > _limit) { |
| 576 | if (error != nullptr) { |
| 577 | *error = true; |
| 578 | } |
| 579 | if (LOGS_ENABLED) DEBUG_E("read string error"); |
| 580 | return std::string(""); |
| 581 | } |
| 582 | std::string result = std::string((const char *) (buffer + _position), l); |
| 583 | _position += l + addition; |
| 584 | return result; |
| 585 | } |
| 586 |
| 587 | ByteArray *NativeByteBuffer::readByteArray(bool *error) { |
| 588 | uint32_t sl = 1; |
| 589 | if (_position + 1 > _limit) { |
| 590 | if (error != nullptr) { |
| 591 | *error = true; |
| 592 | } |
| 593 | if (LOGS_ENABLED) DEBUG_E("read byte array error"); |
| 594 | return nullptr; |
| 595 | } |
| 596 | uint32_t l = buffer[_position++]; |
| 597 | if (l >= 254) { |
| 598 | if (_position + 3 > _limit) { |
| 599 | if (error != nullptr) { |
| 600 | *error = true; |
| 601 | } |
| 602 | if (LOGS_ENABLED) DEBUG_E("read byte array error"); |
| 603 | return nullptr; |
| 604 | } |
| 605 | l = buffer[_position] | (buffer[_position + 1] << 8) | (buffer[_position + 2] << 16); |
| 606 | _position += 3; |
| 607 | sl = 4; |
| 608 | } |
| 609 | uint32_t addition = (l + sl) % 4; |
| 610 | if (addition != 0) { |
| 611 | addition = 4 - addition; |
| 612 | } |
| 613 | if (_position + l + addition > _limit) { |
| 614 | if (error != nullptr) { |
| 615 | *error = true; |
| 616 | } |
| 617 | if (LOGS_ENABLED) DEBUG_E("read byte array error"); |
| 618 | return nullptr; |
| 619 | } |
| 620 | ByteArray *result = new ByteArray(l); |
| 621 | memcpy(result->bytes, buffer + _position, sizeof(uint8_t) * l); |
| 622 | _position += l + addition; |
| 623 | return result; |
| 624 | } |
| 625 |
| 626 | NativeByteBuffer *NativeByteBuffer::readByteBuffer(bool copy, bool *error) { |
| 627 | uint32_t sl = 1; |
| 628 | if (_position + 1 > _limit) { |
| 629 | if (error != nullptr) { |
| 630 | *error = true; |
| 631 | } |
| 632 | if (LOGS_ENABLED) DEBUG_E("read byte buffer error"); |
| 633 | return nullptr; |
| 634 | } |
| 635 | uint32_t l = buffer[_position++]; |
| 636 | if (l >= 254) { |
| 637 | if (_position + 3 > _limit) { |
| 638 | if (error != nullptr) { |
| 639 | *error = true; |
| 640 | } |
| 641 | if (LOGS_ENABLED) DEBUG_E("read byte buffer error"); |
| 642 | return nullptr; |
| 643 | } |
| 644 | l = buffer[_position] | (buffer[_position + 1] << 8) | (buffer[_position + 2] << 16); |
| 645 | _position += 3; |
| 646 | sl = 4; |
| 647 | } |
| 648 | uint32_t addition = (l + sl) % 4; |
| 649 | if (addition != 0) { |
| 650 | addition = 4 - addition; |
| 651 | } |
| 652 | if (_position + l + addition > _limit) { |
| 653 | if (error != nullptr) { |
| 654 | *error = true; |
| 655 | } |
| 656 | if (LOGS_ENABLED) DEBUG_E("read byte buffer error"); |
| 657 | return nullptr; |
| 658 | } |
| 659 | NativeByteBuffer *result = nullptr; |
| 660 | if (copy) { |
| 661 | result = BuffersStorage::getInstance().getFreeBuffer(l); |
| 662 | memcpy(result->buffer, buffer + _position, sizeof(uint8_t) * l); |
| 663 | } else { |
| 664 | result = new NativeByteBuffer(buffer + _position, l); |
| 665 | } |
| 666 | _position += l + addition; |
| 667 | return result; |
| 668 | } |
| 669 |
| 670 | double NativeByteBuffer::readDouble(bool *error) { |
| 671 | double value; |
| 672 | int64_t value2 = readInt64(error); |
| 673 | memcpy(&value, &value2, sizeof(double)); |
| 674 | return value; |
| 675 | } |
| 676 |
| 677 | void NativeByteBuffer::reuse() { |
| 678 | if (sliced) { |
| 679 | return; |
| 680 | } |
| 681 | BuffersStorage::getInstance().reuseFreeBuffer(this); |
| 682 | } |
| 683 |
| 684 | #ifdef ANDROID |
| 685 | jobject NativeByteBuffer::getJavaByteBuffer() { |
| 686 | if (javaByteBuffer == nullptr && javaVm != nullptr) { |
| 687 | JNIEnv *env = 0; |
| 688 | if (javaVm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) { |
| 689 | if (LOGS_ENABLED) DEBUG_E("can't get jnienv"); |
| 690 | exit(1); |
| 691 | } |
| 692 | javaByteBuffer = env->NewDirectByteBuffer(buffer, _capacity); |
| 693 | if (javaByteBuffer == nullptr) { |
| 694 | if (LOGS_ENABLED) DEBUG_E("can't allocate NativeByteBuffer buffer"); |
| 695 | exit(1); |
| 696 | } |
| 697 | jobject globalRef = env->NewGlobalRef(javaByteBuffer); |
| 698 | env->DeleteLocalRef(javaByteBuffer); |
| 699 | javaByteBuffer = globalRef; |
| 700 | } |
| 701 | return javaByteBuffer; |
| 702 | } |
| 703 | #endif |
| 704 |