| 55 | }; |
| 56 | |
| 57 | std::map<std::string, ZipEntry> read_zip_entries(const std::vector<uint8_t> & bytes) { |
| 58 | if (bytes.size() < 22) { |
| 59 | throw std::runtime_error("torch .bin is too small to be a zip archive"); |
| 60 | } |
| 61 | // Scan backwards for the end-of-central-directory record (no archive comment in torch files). |
| 62 | size_t eocd = bytes.size(); |
| 63 | const size_t scan_limit = bytes.size() >= (22 + 0xffff) ? bytes.size() - (22 + 0xffff) : 0; |
| 64 | for (size_t pos = bytes.size() - 22 + 1; pos-- > scan_limit;) { |
| 65 | if (read_u32le(bytes.data() + pos) == kEocdSignature) { |
| 66 | eocd = pos; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | if (eocd == bytes.size()) { |
| 71 | throw std::runtime_error("torch .bin end-of-central-directory record not found"); |
| 72 | } |
| 73 | const uint16_t entry_count = read_u16le(bytes.data() + eocd + 10); |
| 74 | const uint32_t central_offset = read_u32le(bytes.data() + eocd + 16); |
| 75 | if (central_offset == kZip64Sentinel) { |
| 76 | throw std::runtime_error("torch .bin uses zip64, which is not supported"); |
| 77 | } |
| 78 | |
| 79 | std::map<std::string, ZipEntry> entries; |
| 80 | size_t cursor = central_offset; |
| 81 | for (uint16_t index = 0; index < entry_count; ++index) { |
| 82 | if (cursor + 46 > bytes.size() || read_u32le(bytes.data() + cursor) != kCentralSignature) { |
| 83 | throw std::runtime_error("torch .bin central directory is malformed"); |
| 84 | } |
| 85 | const uint16_t method = read_u16le(bytes.data() + cursor + 10); |
| 86 | const uint32_t compressed = read_u32le(bytes.data() + cursor + 20); |
| 87 | const uint32_t uncompressed = read_u32le(bytes.data() + cursor + 24); |
| 88 | const uint16_t name_len = read_u16le(bytes.data() + cursor + 28); |
| 89 | const uint16_t extra_len = read_u16le(bytes.data() + cursor + 30); |
| 90 | const uint16_t comment_len = read_u16le(bytes.data() + cursor + 32); |
| 91 | const uint32_t local_offset = read_u32le(bytes.data() + cursor + 42); |
| 92 | if (cursor + 46 + name_len > bytes.size()) { |
| 93 | throw std::runtime_error("torch .bin central directory entry is truncated"); |
| 94 | } |
| 95 | std::string name(reinterpret_cast<const char *>(bytes.data() + cursor + 46), name_len); |
| 96 | |
| 97 | if (local_offset == kZip64Sentinel || compressed != uncompressed) { |
| 98 | throw std::runtime_error("torch .bin entry is compressed or zip64, which is not supported: " + name); |
| 99 | } |
| 100 | if (method != 0) { |
| 101 | throw std::runtime_error("torch .bin entry is not stored uncompressed: " + name); |
| 102 | } |
| 103 | if (local_offset + 30 > bytes.size() || read_u32le(bytes.data() + local_offset) != kLocalSignature) { |
| 104 | throw std::runtime_error("torch .bin local header is malformed: " + name); |
| 105 | } |
| 106 | const uint16_t local_name_len = read_u16le(bytes.data() + local_offset + 26); |
| 107 | const uint16_t local_extra_len = read_u16le(bytes.data() + local_offset + 28); |
| 108 | const size_t data_offset = local_offset + 30 + local_name_len + local_extra_len; |
| 109 | if (data_offset + uncompressed > bytes.size()) { |
| 110 | throw std::runtime_error("torch .bin entry data is out of bounds: " + name); |
| 111 | } |
| 112 | entries.emplace(std::move(name), ZipEntry{data_offset, uncompressed}); |
| 113 | cursor += 46 + name_len + extra_len + comment_len; |
| 114 | } |
no test coverage detected