Helper to consume all bytes from a BytesSource
| 387 | |
| 388 | // Helper to consume all bytes from a BytesSource |
| 389 | std::vector<uint8_t> ConsumeBytes(BytesSource source) { |
| 390 | if (source.inner == 0) { |
| 391 | return {}; |
| 392 | } |
| 393 | |
| 394 | // Take a buffer from the pool (typically 64 KiB pre-allocated) |
| 395 | IterBuf iter_buf = IterBuf::take(); |
| 396 | |
| 397 | // Get the remaining length to reserve exact buffer size |
| 398 | uint32_t remaining_len = 0; |
| 399 | auto ret = FFI::bytes_source_remaining_length(source, &remaining_len); |
| 400 | if (ret != 0) { |
| 401 | // If we can't get the length, fall back to incremental reading |
| 402 | // This shouldn't happen with current host implementation |
| 403 | constexpr size_t CHUNK_SIZE = 1024; |
| 404 | iter_buf.reserve(CHUNK_SIZE); |
| 405 | |
| 406 | while (true) { |
| 407 | size_t chunk_size = CHUNK_SIZE; |
| 408 | size_t old_size = iter_buf.size(); |
| 409 | iter_buf.resize(old_size + chunk_size); |
| 410 | |
| 411 | ret = FFI::bytes_source_read(source, iter_buf.data() + old_size, &chunk_size); |
| 412 | iter_buf.resize(old_size + chunk_size); // Resize to actual bytes read |
| 413 | |
| 414 | if (ret == -1) { // EXHAUSTED |
| 415 | break; |
| 416 | } else if (ret != 0) { // Error |
| 417 | fprintf(stderr, "ERROR: Failed to read from BytesSource: %d\n", ret); |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | return iter_buf.release(); |
| 422 | } |
| 423 | |
| 424 | // Reserve exact size needed (often no-op since pool buffer is 64 KiB) |
| 425 | iter_buf.resize(remaining_len); // Resize to exact size BEFORE reading |
| 426 | |
| 427 | // Read all bytes - should complete in one call since we have capacity |
| 428 | size_t bytes_read = 0; |
| 429 | while (bytes_read < remaining_len) { |
| 430 | size_t chunk_size = remaining_len - bytes_read; |
| 431 | ret = FFI::bytes_source_read(source, iter_buf.data() + bytes_read, &chunk_size); |
| 432 | bytes_read += chunk_size; |
| 433 | |
| 434 | if (ret == -1) { // EXHAUSTED |
| 435 | break; |
| 436 | } else if (ret != 0) { // Error |
| 437 | fprintf(stderr, "ERROR: Failed to read from BytesSource: %d\n", ret); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // Resize to actual bytes read if different (shouldn't normally happen) |
| 443 | if (bytes_read != remaining_len) { |
| 444 | iter_buf.resize(bytes_read); |
| 445 | } |
| 446 |
no test coverage detected
searching dependent graphs…