| 2493 | } |
| 2494 | |
| 2495 | static bool LoadExternalFile(std::vector<unsigned char> *out, std::string *err, |
| 2496 | std::string *warn, const std::string &filename, |
| 2497 | const std::string &basedir, bool required, |
| 2498 | size_t reqBytes, bool checkSize, |
| 2499 | size_t maxFileSize, FsCallbacks *fs) { |
| 2500 | if (fs == nullptr || fs->FileExists == nullptr || |
| 2501 | fs->ExpandFilePath == nullptr || fs->ReadWholeFile == nullptr) { |
| 2502 | // This is a developer error, assert() ? |
| 2503 | if (err) { |
| 2504 | (*err) += "FS callback[s] not set\n"; |
| 2505 | } |
| 2506 | return false; |
| 2507 | } |
| 2508 | |
| 2509 | std::string *failMsgOut = required ? err : warn; |
| 2510 | |
| 2511 | out->clear(); |
| 2512 | |
| 2513 | std::vector<std::string> paths; |
| 2514 | paths.push_back(basedir); |
| 2515 | paths.push_back("."); |
| 2516 | |
| 2517 | std::string filepath = FindFile(paths, filename, fs); |
| 2518 | if (filepath.empty() || filename.empty()) { |
| 2519 | if (failMsgOut) { |
| 2520 | (*failMsgOut) += "File not found : " + filename + "\n"; |
| 2521 | } |
| 2522 | return false; |
| 2523 | } |
| 2524 | |
| 2525 | // Check file size |
| 2526 | if (fs->GetFileSizeInBytes) { |
| 2527 | size_t file_size{0}; |
| 2528 | std::string _err; |
| 2529 | bool ok = |
| 2530 | fs->GetFileSizeInBytes(&file_size, &_err, filepath, fs->user_data); |
| 2531 | if (!ok) { |
| 2532 | if (_err.size()) { |
| 2533 | if (failMsgOut) { |
| 2534 | (*failMsgOut) += "Getting file size failed : " + filename + |
| 2535 | ", err = " + _err + "\n"; |
| 2536 | } |
| 2537 | } |
| 2538 | return false; |
| 2539 | } |
| 2540 | |
| 2541 | if (file_size > maxFileSize) { |
| 2542 | if (failMsgOut) { |
| 2543 | (*failMsgOut) += "File size " + std::to_string(file_size) + |
| 2544 | " exceeds maximum allowed file size " + |
| 2545 | std::to_string(maxFileSize) + " : " + filepath + "\n"; |
| 2546 | } |
| 2547 | return false; |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | std::vector<unsigned char> buf; |
| 2552 | std::string fileReadErr; |