| 516 | #endif |
| 517 | |
| 518 | bool read_file_to_vec(const char* pFilename, uint8_vec& data) |
| 519 | { |
| 520 | FILE* pFile = nullptr; |
| 521 | #ifdef _WIN32 |
| 522 | fopen_s(&pFile, pFilename, "rb"); |
| 523 | #else |
| 524 | pFile = fopen(pFilename, "rb"); |
| 525 | #endif |
| 526 | if (!pFile) |
| 527 | return false; |
| 528 | |
| 529 | fseek(pFile, 0, SEEK_END); |
| 530 | #ifdef _WIN32 |
| 531 | int64_t filesize = _ftelli64(pFile); |
| 532 | #else |
| 533 | int64_t filesize = ftello(pFile); |
| 534 | #endif |
| 535 | if (filesize < 0) |
| 536 | { |
| 537 | fclose(pFile); |
| 538 | return false; |
| 539 | } |
| 540 | fseek(pFile, 0, SEEK_SET); |
| 541 | |
| 542 | if (sizeof(size_t) == sizeof(uint32_t)) |
| 543 | { |
| 544 | if (filesize > 0x70000000) |
| 545 | { |
| 546 | // File might be too big to load safely in one alloc |
| 547 | fclose(pFile); |
| 548 | return false; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (!data.try_resize((size_t)filesize)) |
| 553 | { |
| 554 | fclose(pFile); |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | if (filesize) |
| 559 | { |
| 560 | if (fread(&data[0], 1, (size_t)filesize, pFile) != (size_t)filesize) |
| 561 | { |
| 562 | fclose(pFile); |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | fclose(pFile); |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | bool write_data_to_file(const char* pFilename, const void* pData, size_t len) |
| 572 | { |
no test coverage detected