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