| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "storezip.h" |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdint.h> |
| 19 | #include <map> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace pnnx { |
| 24 | |
| 25 | // https://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed |
| 26 | #ifdef _MSC_VER |
| 27 | #define PACK(__Declaration__) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop)) |
| 28 | #else |
| 29 | #define PACK(__Declaration__) __Declaration__ __attribute__((__packed__)) |
| 30 | #endif |
| 31 | |
| 32 | PACK(struct local_file_header { |
| 33 | uint16_t version; |
| 34 | uint16_t flag; |
| 35 | uint16_t compression; |
| 36 | uint16_t last_modify_time; |
| 37 | uint16_t last_modify_date; |
| 38 | uint32_t crc32; |
| 39 | uint32_t compressed_size; |
| 40 | uint32_t uncompressed_size; |
| 41 | uint16_t file_name_length; |
| 42 | uint16_t extra_field_length; |
| 43 | }); |
| 44 | |
| 45 | PACK(struct zip64_extended_extra_field { |
| 46 | uint64_t uncompressed_size; |
| 47 | uint64_t compressed_size; |
| 48 | uint64_t lfh_offset; |
| 49 | uint32_t disk_number; |
| 50 | }); |
| 51 | |
| 52 | PACK(struct central_directory_file_header { |
| 53 | uint16_t version_made; |
| 54 | uint16_t version; |
| 55 | uint16_t flag; |
| 56 | uint16_t compression; |
| 57 | uint16_t last_modify_time; |
| 58 | uint16_t last_modify_date; |
| 59 | uint32_t crc32; |
| 60 | uint32_t compressed_size; |
| 61 | uint32_t uncompressed_size; |
| 62 | uint16_t file_name_length; |
| 63 | uint16_t extra_field_length; |
| 64 | uint16_t file_comment_length; |
| 65 | uint16_t start_disk; |
| 66 | uint16_t internal_file_attrs; |
| 67 | uint32_t external_file_attrs; |
| 68 | uint32_t lfh_offset; |
| 69 | }); |
| 70 | |
| 71 | PACK(struct zip64_end_of_central_directory_record { |
| 72 | uint64_t size_of_eocd64_m12; |
| 73 | uint16_t version_made_by; |
| 74 | uint16_t version_min_required; |
| 75 | uint32_t disk_number; |
| 76 | uint32_t start_disk; |
| 77 | uint64_t cd_records; |
| 78 | uint64_t total_cd_records; |
| 79 | uint64_t cd_size; |
| 80 | uint64_t cd_offset; |
| 81 | }); |
| 82 | |
| 83 | PACK(struct zip64_end_of_central_directory_locator { |
| 84 | uint32_t eocdr64_disk_number; |
| 85 | uint64_t eocdr64_offset; |
| 86 | uint32_t disk_count; |
| 87 | }); |
| 88 | |
| 89 | PACK(struct end_of_central_directory_record { |
| 90 | uint16_t disk_number; |
| 91 | uint16_t start_disk; |
| 92 | uint16_t cd_records; |
| 93 | uint16_t total_cd_records; |
| 94 | uint32_t cd_size; |
| 95 | uint32_t cd_offset; |
| 96 | uint16_t comment_length; |
| 97 | }); |
| 98 | |
| 99 | static uint32_t CRC32_TABLE[256]; |
| 100 | |
| 101 | static void CRC32_TABLE_INIT() |
| 102 | { |
| 103 | for (int i = 0; i < 256; i++) |
| 104 | { |
| 105 | uint32_t c = i; |
| 106 | for (int j = 0; j < 8; j++) |
| 107 | { |
| 108 | if (c & 1) |
| 109 | c = (c >> 1) ^ 0xedb88320; |
| 110 | else |
| 111 | c >>= 1; |
| 112 | } |
| 113 | CRC32_TABLE[i] = c; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static uint32_t CRC32(uint32_t x, unsigned char ch) |
| 118 | { |
| 119 | return (x >> 8) ^ CRC32_TABLE[(x ^ ch) & 0xff]; |
| 120 | } |
| 121 | |
| 122 | static uint32_t CRC32_buffer(const unsigned char* data, uint64_t len) |
| 123 | { |
| 124 | uint32_t x = 0xffffffff; |
| 125 | |
| 126 | for (uint64_t i = 0; i < len; i++) |
| 127 | x = CRC32(x, data[i]); |
| 128 | |
| 129 | return x ^ 0xffffffff; |
| 130 | } |
| 131 | |
| 132 | StoreZipReader::StoreZipReader() |
| 133 | { |
| 134 | fp = 0; |
| 135 | } |
| 136 | |
| 137 | StoreZipReader::~StoreZipReader() |
| 138 | { |
| 139 | close(); |
| 140 | } |
| 141 | |
| 142 | int StoreZipReader::open(const std::string& path) |
| 143 | { |
| 144 | close(); |
| 145 | |
| 146 | fp = fopen(path.c_str(), "rb"); |
| 147 | if (!fp) |
| 148 | { |
| 149 | fprintf(stderr, "open failed\n"); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | while (!feof(fp)) |
| 154 | { |
| 155 | // peek signature |
| 156 | uint32_t signature; |
| 157 | int nread = fread((char*)&signature, sizeof(signature), 1, fp); |
| 158 | if (nread != 1) |
| 159 | break; |
| 160 | |
| 161 | // fprintf(stderr, "signature = %x\n", signature); |
| 162 | |
| 163 | if (signature == 0x04034b50) |
| 164 | { |
| 165 | local_file_header lfh; |
| 166 | fread((char*)&lfh, sizeof(lfh), 1, fp); |
| 167 | |
| 168 | if (lfh.flag & 0x08) |
| 169 | { |
| 170 | fprintf(stderr, "zip file contains data descriptor, this is not supported yet\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | if (lfh.compression != 0 || lfh.compressed_size != lfh.uncompressed_size) |
| 175 | { |
| 176 | fprintf(stderr, "not stored zip file %d %d\n", lfh.compressed_size, lfh.uncompressed_size); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | // file name |
| 181 | std::string name; |
| 182 | name.resize(lfh.file_name_length); |
| 183 | fread((char*)name.data(), name.size(), 1, fp); |
| 184 | |
| 185 | uint64_t compressed_size = lfh.compressed_size; |
| 186 | uint64_t uncompressed_size = lfh.uncompressed_size; |
| 187 | if (compressed_size == 0xffffffff && uncompressed_size == 0xffffffff) |
| 188 | { |
| 189 | uint16_t extra_offset = 0; |
| 190 | while (extra_offset < lfh.extra_field_length) |
| 191 | { |
| 192 | uint16_t extra_id; |
| 193 | uint16_t extra_size; |
| 194 | fread((char*)&extra_id, sizeof(extra_id), 1, fp); |
| 195 | fread((char*)&extra_size, sizeof(extra_size), 1, fp); |
| 196 | if (extra_id != 0x0001) |
| 197 | { |
| 198 | // skip this extra field block |
| 199 | fseek(fp, extra_size - 4, SEEK_CUR); |
| 200 | extra_offset += extra_size; |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | // zip64 extra field |
| 205 | zip64_extended_extra_field zip64_eef; |
| 206 | fread((char*)&zip64_eef, sizeof(zip64_eef), 1, fp); |
| 207 | |
| 208 | compressed_size = zip64_eef.compressed_size; |
| 209 | uncompressed_size = zip64_eef.uncompressed_size; |
| 210 | |
| 211 | // skip remaining extra field blocks |
| 212 | fseek(fp, lfh.extra_field_length - extra_offset - 4 - sizeof(zip64_eef), SEEK_CUR); |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | // skip extra field |
| 219 | fseek(fp, lfh.extra_field_length, SEEK_CUR); |
| 220 | } |
| 221 | |
| 222 | StoreZipMeta fm; |
| 223 | fm.offset = ftell(fp); |
| 224 | fm.size = compressed_size; |
| 225 | |
| 226 | filemetas[name] = fm; |
| 227 | |
| 228 | // fprintf(stderr, "%s = %d %d\n", name.c_str(), fm.offset, fm.size); |
| 229 | |
| 230 | fseek(fp, compressed_size, SEEK_CUR); |
| 231 | } |
| 232 | else if (signature == 0x02014b50) |
| 233 | { |
| 234 | central_directory_file_header cdfh; |
| 235 | fread((char*)&cdfh, sizeof(cdfh), 1, fp); |
| 236 | |
| 237 | // skip file name |
| 238 | fseek(fp, cdfh.file_name_length, SEEK_CUR); |
| 239 | |
| 240 | // skip extra field |
| 241 | fseek(fp, cdfh.extra_field_length, SEEK_CUR); |
| 242 | |
| 243 | // skip file comment |
| 244 | fseek(fp, cdfh.file_comment_length, SEEK_CUR); |
| 245 | } |
| 246 | else if (signature == 0x06054b50) |
| 247 | { |
| 248 | end_of_central_directory_record eocdr; |
| 249 | fread((char*)&eocdr, sizeof(eocdr), 1, fp); |
| 250 | |
| 251 | // skip comment |
| 252 | fseek(fp, eocdr.comment_length, SEEK_CUR); |
| 253 | } |
| 254 | else if (signature == 0x06064b50) |
| 255 | { |
| 256 | zip64_end_of_central_directory_record eocdr64; |
| 257 | fread((char*)&eocdr64, sizeof(eocdr64), 1, fp); |
| 258 | |
| 259 | // skip comment |
| 260 | fseek(fp, eocdr64.size_of_eocd64_m12 - 44, SEEK_CUR); |
| 261 | } |
| 262 | else if (signature == 0x07064b50) |
| 263 | { |
| 264 | zip64_end_of_central_directory_locator eocdl64; |
| 265 | fread((char*)&eocdl64, sizeof(eocdl64), 1, fp); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | fprintf(stderr, "unsupported signature %x\n", signature); |
| 270 | return -1; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | std::vector<std::string> StoreZipReader::get_names() const |
| 278 | { |
| 279 | std::vector<std::string> names; |
| 280 | for (std::map<std::string, StoreZipMeta>::const_iterator it = filemetas.begin(); it != filemetas.end(); ++it) |
| 281 | { |
| 282 | names.push_back(it->first); |
| 283 | } |
| 284 | |
| 285 | return names; |
| 286 | } |
| 287 | |
| 288 | uint64_t StoreZipReader::get_file_size(const std::string& name) const |
| 289 | { |
| 290 | if (filemetas.find(name) == filemetas.end()) |
| 291 | { |
| 292 | fprintf(stderr, "no such file %s\n", name.c_str()); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | return filemetas.at(name).size; |
| 297 | } |
| 298 | |
| 299 | int StoreZipReader::read_file(const std::string& name, char* data) |
| 300 | { |
| 301 | if (filemetas.find(name) == filemetas.end()) |
| 302 | { |
| 303 | fprintf(stderr, "no such file %s\n", name.c_str()); |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | uint64_t offset = filemetas[name].offset; |
| 308 | uint64_t size = filemetas[name].size; |
| 309 | |
| 310 | fseek(fp, offset, SEEK_SET); |
| 311 | fread(data, size, 1, fp); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int StoreZipReader::close() |
| 317 | { |
| 318 | if (!fp) |
| 319 | return 0; |
| 320 | |
| 321 | fclose(fp); |
| 322 | fp = 0; |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | StoreZipWriter::StoreZipWriter() |
| 328 | { |
| 329 | fp = 0; |
| 330 | |
| 331 | CRC32_TABLE_INIT(); |
| 332 | } |
| 333 | |
| 334 | StoreZipWriter::~StoreZipWriter() |
| 335 | { |
| 336 | close(); |
| 337 | } |
| 338 | |
| 339 | int StoreZipWriter::open(const std::string& path) |
| 340 | { |
| 341 | close(); |
| 342 | |
| 343 | fp = fopen(path.c_str(), "wb"); |
| 344 | if (!fp) |
| 345 | { |
| 346 | fprintf(stderr, "open failed\n"); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | int StoreZipWriter::write_file(const std::string& name, const char* data, uint64_t size) |
| 354 | { |
| 355 | long offset = ftell(fp); |
| 356 | |
| 357 | uint32_t signature = 0x04034b50; |
| 358 | fwrite((char*)&signature, sizeof(signature), 1, fp); |
| 359 | |
| 360 | uint32_t crc32 = CRC32_buffer((const unsigned char*)data, size); |
| 361 | |
| 362 | local_file_header lfh; |
| 363 | lfh.version = 0; |
| 364 | lfh.flag = 0; |
| 365 | lfh.compression = 0; |
| 366 | lfh.last_modify_time = 0; |
| 367 | lfh.last_modify_date = 0; |
| 368 | lfh.crc32 = crc32; |
| 369 | lfh.compressed_size = 0xffffffff; |
| 370 | lfh.uncompressed_size = 0xffffffff; |
| 371 | lfh.file_name_length = name.size(); |
| 372 | |
| 373 | // zip64 extra field |
| 374 | zip64_extended_extra_field zip64_eef; |
| 375 | zip64_eef.uncompressed_size = size; |
| 376 | zip64_eef.compressed_size = size; |
| 377 | zip64_eef.lfh_offset = 0; |
| 378 | zip64_eef.disk_number = 0; |
| 379 | |
| 380 | uint16_t extra_id = 0x0001; |
| 381 | uint16_t extra_size = sizeof(zip64_eef); |
| 382 | |
| 383 | lfh.extra_field_length = sizeof(extra_id) + sizeof(extra_size) + sizeof(zip64_eef); |
| 384 | |
| 385 | fwrite((char*)&lfh, sizeof(lfh), 1, fp); |
| 386 | |
| 387 | fwrite((char*)name.c_str(), name.size(), 1, fp); |
| 388 | |
| 389 | fwrite((char*)&extra_id, sizeof(extra_id), 1, fp); |
| 390 | fwrite((char*)&extra_size, sizeof(extra_size), 1, fp); |
| 391 | fwrite((char*)&zip64_eef, sizeof(zip64_eef), 1, fp); |
| 392 | |
| 393 | fwrite(data, size, 1, fp); |
| 394 | |
| 395 | StoreZipMeta szm; |
| 396 | szm.name = name; |
| 397 | szm.lfh_offset = offset; |
| 398 | szm.crc32 = crc32; |
| 399 | szm.size = size; |
| 400 | |
| 401 | filemetas.push_back(szm); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int StoreZipWriter::close() |
| 407 | { |
| 408 | if (!fp) |
| 409 | return 0; |
| 410 | |
| 411 | long offset = ftell(fp); |
| 412 | |
| 413 | for (const StoreZipMeta& szm : filemetas) |
| 414 | { |
| 415 | uint32_t signature = 0x02014b50; |
| 416 | fwrite((char*)&signature, sizeof(signature), 1, fp); |
| 417 | |
| 418 | central_directory_file_header cdfh; |
| 419 | cdfh.version_made = 0; |
| 420 | cdfh.version = 0; |
| 421 | cdfh.flag = 0; |
| 422 | cdfh.compression = 0; |
| 423 | cdfh.last_modify_time = 0; |
| 424 | cdfh.last_modify_date = 0; |
| 425 | cdfh.crc32 = szm.crc32; |
| 426 | cdfh.compressed_size = 0xffffffff; |
| 427 | cdfh.uncompressed_size = 0xffffffff; |
| 428 | cdfh.file_name_length = szm.name.size(); |
| 429 | cdfh.file_comment_length = 0; |
| 430 | cdfh.start_disk = 0xffff; |
| 431 | cdfh.internal_file_attrs = 0; |
| 432 | cdfh.external_file_attrs = 0; |
| 433 | cdfh.lfh_offset = 0xffffffff; |
| 434 | |
| 435 | // zip64 extra field |
| 436 | zip64_extended_extra_field zip64_eef; |
| 437 | zip64_eef.uncompressed_size = szm.size; |
| 438 | zip64_eef.compressed_size = szm.size; |
| 439 | zip64_eef.lfh_offset = szm.lfh_offset; |
| 440 | zip64_eef.disk_number = 0; |
| 441 | |
| 442 | uint16_t extra_id = 0x0001; |
| 443 | uint16_t extra_size = sizeof(zip64_eef); |
| 444 | |
| 445 | cdfh.extra_field_length = sizeof(extra_id) + sizeof(extra_size) + sizeof(zip64_eef); |
| 446 | |
| 447 | fwrite((char*)&cdfh, sizeof(cdfh), 1, fp); |
| 448 | |
| 449 | fwrite((char*)szm.name.c_str(), szm.name.size(), 1, fp); |
| 450 | |
| 451 | fwrite((char*)&extra_id, sizeof(extra_id), 1, fp); |
| 452 | fwrite((char*)&extra_size, sizeof(extra_size), 1, fp); |
| 453 | fwrite((char*)&zip64_eef, sizeof(zip64_eef), 1, fp); |
| 454 | } |
| 455 | |
| 456 | long offset2 = ftell(fp); |
| 457 | |
| 458 | { |
| 459 | uint32_t signature = 0x06064b50; |
| 460 | fwrite((char*)&signature, sizeof(signature), 1, fp); |
| 461 | |
| 462 | zip64_end_of_central_directory_record eocdr64; |
| 463 | eocdr64.size_of_eocd64_m12 = sizeof(eocdr64) - 8; |
| 464 | eocdr64.version_made_by = 0; |
| 465 | eocdr64.version_min_required = 0; |
| 466 | eocdr64.disk_number = 0; |
| 467 | eocdr64.start_disk = 0; |
| 468 | eocdr64.cd_records = filemetas.size(); |
| 469 | eocdr64.total_cd_records = filemetas.size(); |
| 470 | eocdr64.cd_size = offset2 - offset; |
| 471 | eocdr64.cd_offset = offset; |
| 472 | |
| 473 | fwrite((char*)&eocdr64, sizeof(eocdr64), 1, fp); |
| 474 | } |
| 475 | |
| 476 | { |
| 477 | uint32_t signature = 0x07064b50; |
| 478 | fwrite((char*)&signature, sizeof(signature), 1, fp); |
| 479 | |
| 480 | zip64_end_of_central_directory_locator eocdl64; |
| 481 | eocdl64.eocdr64_disk_number = 0; |
| 482 | eocdl64.eocdr64_offset = offset2; |
| 483 | eocdl64.disk_count = 1; |
| 484 | |
| 485 | fwrite((char*)&eocdl64, sizeof(eocdl64), 1, fp); |
| 486 | } |
| 487 | |
| 488 | { |
| 489 | uint32_t signature = 0x06054b50; |
| 490 | fwrite((char*)&signature, sizeof(signature), 1, fp); |
| 491 | |
| 492 | end_of_central_directory_record eocdr; |
| 493 | eocdr.disk_number = 0xffff; |
| 494 | eocdr.start_disk = 0xffff; |
| 495 | eocdr.cd_records = 0xffff; |
| 496 | eocdr.total_cd_records = 0xffff; |
| 497 | eocdr.cd_size = 0xffffffff; |
| 498 | eocdr.cd_offset = 0xffffffff; |
| 499 | eocdr.comment_length = 0; |
| 500 | |
| 501 | fwrite((char*)&eocdr, sizeof(eocdr), 1, fp); |
| 502 | } |
| 503 | |
| 504 | fclose(fp); |
| 505 | fp = 0; |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | } // namespace pnnx |
| 511 | |
| 512 | #if 0 |
| 513 | int main() |
| 514 | { |
| 515 | using namespace pnnx; |
| 516 | |
| 517 | { |
| 518 | uint64_t len = 1*1024*1024*1024; |
| 519 | // uint64_t len = 1*1024*1024; |
| 520 | char* data1g = new char[len]; |
| 521 | |
| 522 | StoreZipWriter szw; |
| 523 | |
| 524 | szw.open("szw.zip"); |
| 525 | |
| 526 | szw.write_file("a.py", data1g, len); |
| 527 | szw.write_file("b.param", data1g, 44); |
| 528 | szw.write_file("c.bin", data1g, len); |
| 529 | szw.write_file("d.txt", data1g, len); |
| 530 | szw.write_file("e.jpg", data1g, len); |
| 531 | szw.write_file("f.png", data1g, len); |
| 532 | |
| 533 | szw.close(); |
| 534 | |
| 535 | delete[] data1g; |
| 536 | } |
| 537 | |
| 538 | { |
| 539 | StoreZipReader sz; |
| 540 | |
| 541 | sz.open("szw.zip"); |
| 542 | |
| 543 | std::vector<std::string> names = sz.get_names(); |
| 544 | |
| 545 | for (size_t i = 0; i < names.size(); i++) |
| 546 | { |
| 547 | uint64_t size = sz.get_file_size(names[i]); |
| 548 | |
| 549 | fprintf(stderr, "%s %lu\n", names[i].c_str(), size); |
| 550 | } |
| 551 | |
| 552 | sz.close(); |
| 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | #endif |