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