1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
|
/*
* This file is part of the Advance project.
*
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Andrea Mazzoleni
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "portable.h"
#include "zip.h"
#include "siglock.h"
#include "file.h"
#include "data.h"
#include "lib/endianrw.h"
#include <zlib.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <set>
using namespace std;
/**
* Enable pendantic checks on the zip integrity.
*/
bool zip::pedantic = false;
bool ecd_compare_sig(const unsigned char *buffer)
{
static char ecdsig[] = { 'P', 'K', 0x05, 0x06 };
return memcmp(buffer, ecdsig, 4) == 0;
}
/**
* Locate end-of-central-dir sig in buffer and return offset
* \param offset Resulting offset of cent dir start in buffer.
* \reutn If the end-of-central-dir is found.
*/
bool ecd_find_sig (const unsigned char *buffer, unsigned buflen, unsigned& offset)
{
for (int i=buflen-22; i>=0; i--) {
if (ecd_compare_sig(buffer+i)) {
offset = i;
return true;
}
}
return false;
}
#define ECD_READ_BUFFER_SIZE 4096
/**
* Read cent dir and end cent dir data
* \param f File to read.
* \param length Length of the file.
*/
bool cent_read(FILE* f, unsigned length, unsigned char*& data, unsigned& size)
{
unsigned buf_length;
if (length <= ECD_READ_BUFFER_SIZE) {
buf_length = length;
} else {
// align the read
buf_length = length - ((length - ECD_READ_BUFFER_SIZE) & ~(ECD_READ_BUFFER_SIZE-1));
}
while (true) {
if (buf_length > length)
buf_length = length;
if (fseek(f, length - buf_length, SEEK_SET) != 0) {
return false;
}
// allocate buffer
unsigned char* buf = data_alloc(buf_length);
assert(buf);
if (fread(buf, buf_length, 1, f) != 1) {
data_free(buf);
return false;
}
unsigned offset = 0;
if (ecd_find_sig(buf, buf_length, offset)) {
unsigned start_of_cent_dir = le_uint32_read(buf + offset + ZIP_EO_offset_to_start_of_cent_dir);
unsigned buf_pos = length - buf_length;
if (start_of_cent_dir >= length) {
data_free(buf);
return false;
}
size = length - start_of_cent_dir;
data = data_alloc(size);
assert(data);
if (buf_pos <= start_of_cent_dir) {
memcpy(data, buf + (start_of_cent_dir - buf_pos), size);
data_free(buf);
} else {
data_free(buf);
if (fseek(f, start_of_cent_dir, SEEK_SET) != 0) {
data_free(data);
data = 0;
return false;
}
if (fread(data, size, 1, f) != 1) {
data_free(data);
data = 0;
return false;
}
}
return true;
}
data_free(buf);
if (buf_length < 8 * ECD_READ_BUFFER_SIZE && buf_length < length) {
// grow buffer
buf_length += ECD_READ_BUFFER_SIZE;
} else {
// aborting
return false;
}
}
}
/** Code used for disk entry. */
#define ZIP_UNIQUE_DISK 0
/** Convert time_t to zip format. */
void time2zip(time_t tod, unsigned& date, unsigned& time)
{
struct tm* tm = gmtime(&tod);
assert(tm);
unsigned day = tm->tm_mday; // 1-31
unsigned mon = tm->tm_mon + 1; // 1-12
unsigned year = tm->tm_year - 80; // since 1980
date = (day & 0x1F) | ((mon & 0xF) << 5) | ((year & 0x7F) << 9);
unsigned sec = tm->tm_sec / 2; // 0-29, double to get real seconds
unsigned min = tm->tm_min; // 0-59
unsigned hour = tm->tm_hour; // 0-23
time = (sec & 0x1F) | ((min & 0x3F) << 5) | ((hour & 0x1F) << 11);
}
/** Convert zip time to to time_t. */
time_t zip2time(unsigned date, unsigned time)
{
struct tm tm;
// reset all entry
memset(&tm, 0, sizeof(tm));
// set know entry
tm.tm_mday = date & 0x1F; // 1-31
tm.tm_mon = ((date >> 5) & 0xF) - 1; // 0-11
tm.tm_year = ((date >> 9) & 0x7f) + 80; // since 1900
tm.tm_sec = (time & 0x1F) * 2; // 0-59
tm.tm_min = (time >> 5) & 0x3F; // 0-59
tm.tm_hour = (time >> 11) & 0x1F; // 0-23
return mktime(&tm);
}
zip_entry::zip_entry(const zip& Aparent)
{
memset(&info, 0xFF, sizeof(info));
parent_name = Aparent.file_get();
info.filename_length = 0;
file_name = 0;
info.local_extra_field_length = 0;
local_extra_field = 0;
info.central_extra_field_length = 0;
central_extra_field = 0;
info.file_comment_length = 0;
file_comment = 0;
info.compressed_size = 0;
data = 0;
}
zip_entry::zip_entry(const zip_entry& A)
{
info = A.info;
parent_name = A.parent_name;
file_name = data_dup(A.file_name, info.filename_length);
local_extra_field = data_dup(A.local_extra_field, info.local_extra_field_length);
central_extra_field = data_dup(A.central_extra_field, info.central_extra_field_length);
file_comment = data_dup(A.file_comment, info.file_comment_length);
data = data_dup(A.data, A.info.compressed_size);
}
zip_entry::~zip_entry()
{
data_free(file_name);
data_free(local_extra_field);
data_free(central_extra_field);
data_free(file_comment);
data_free(data);
}
zip_entry::method_t zip_entry::method_get() const
{
switch (info.compression_method) {
case ZIP_METHOD_STORE :
return store;
case ZIP_METHOD_SHRUNK :
return shrunk;
case ZIP_METHOD_REDUCE1 :
return reduce1;
case ZIP_METHOD_REDUCE2 :
return reduce2;
case ZIP_METHOD_REDUCE3 :
return reduce3;
case ZIP_METHOD_REDUCE4 :
return reduce4;
case ZIP_METHOD_IMPLODE :
switch (info.general_purpose_bit_flag & ZIP_GEN_FLAGS_IMPLODE_MASK) {
case ZIP_GEN_FLAGS_IMPLODE_4KD2T :
return implode_4kdict_2tree;
case ZIP_GEN_FLAGS_IMPLODE_8KD2T :
return implode_8kdict_2tree;
case ZIP_GEN_FLAGS_IMPLODE_4KD3T :
return implode_4kdict_3tree;
case ZIP_GEN_FLAGS_IMPLODE_8KD3T :
return implode_8kdict_3tree;
}
return unknown;
case ZIP_METHOD_DEFLATE :
switch (info.general_purpose_bit_flag & ZIP_GEN_FLAGS_DEFLATE_MASK) {
case ZIP_GEN_FLAGS_DEFLATE_NORMAL :
return deflate6;
case ZIP_GEN_FLAGS_DEFLATE_MAXIMUM :
return deflate9;
case ZIP_GEN_FLAGS_DEFLATE_FAST :
return deflate3;
case ZIP_GEN_FLAGS_DEFLATE_SUPERFAST :
return deflate1;
}
return unknown;
case ZIP_METHOD_BZIP2 :
return bzip2;
case ZIP_METHOD_LZMA :
return lzma;
}
return unknown;
}
bool zip_entry::is_text() const
{
return (info.internal_file_attrib & ZIP_INT_ATTR_TEXT) != 0;
}
void zip_entry::compressed_seek(FILE* f) const
{
// seek to local header
if (fseek(f, offset_get(), SEEK_SET) != 0) {
throw error_invalid() << "Failed seek " << parentname_get();
}
// read local header
unsigned char buf[ZIP_LO_FIXED];
if (fread(buf, ZIP_LO_FIXED, 1, f) != 1) {
throw error() << "Failed read " << parentname_get();
}
check_local(buf);
// use the local extra_field_length. It may be different than the
// central directory version in some zips.
unsigned local_extra_field_length = le_uint16_read(buf+ZIP_LO_extra_field_length);
// seek to data
if (fseek(f, info.filename_length + local_extra_field_length, SEEK_CUR) != 0) {
throw error_invalid() << "Failed seek " << parentname_get();
}
}
void zip_entry::compressed_read(unsigned char* outdata) const
{
if (data) {
memcpy(outdata, data, compressed_size_get());
} else {
FILE* f = fopen(parentname_get().c_str(), "rb");
if (!f) {
throw error() << "Failed open for reading " << parentname_get();
}
try {
compressed_seek(f);
if (compressed_size_get() > 0) {
if (fread(outdata, compressed_size_get(), 1, f) != 1) {
throw error() << "Failed read " << parentname_get();
}
}
} catch (...) {
fclose(f);
throw;
}
fclose(f);
}
}
time_t zip_entry::time_get() const
{
return zip2time(info.last_mod_file_date, info.last_mod_file_time);
}
void zip_entry::time_set(time_t tod)
{
time2zip(tod, info.last_mod_file_date, info.last_mod_file_time);
}
void zip_entry::set(method_t method, const string& Aname, const unsigned char* compdata, unsigned compsize, unsigned size, unsigned crc, unsigned date, unsigned time, bool is_text)
{
info.version_needed_to_extract = 20; // version 2.0
info.os_needed_to_extract = 0;
info.version_made_by = 20; // version 2.0
info.host_os = 0;
info.general_purpose_bit_flag = 0; // default
info.last_mod_file_date = date;
info.last_mod_file_time = time;
info.crc32 = crc;
info.compressed_size = compsize;
info.uncompressed_size = size;
info.internal_file_attrib = is_text ? ZIP_INT_ATTR_TEXT : 0;
info.external_file_attrib = 0;
switch (method) {
case store :
if (size != compsize) {
throw error_invalid() << "Zip entry size mismatch";
}
info.compression_method = ZIP_METHOD_STORE;
info.version_needed_to_extract = 10; // Version 1.0
break;
case shrunk :
info.compression_method = ZIP_METHOD_SHRUNK;
break;
case reduce1 :
info.compression_method = ZIP_METHOD_REDUCE1;
break;
case reduce2 :
info.compression_method = ZIP_METHOD_REDUCE2;
break;
case reduce3 :
info.compression_method = ZIP_METHOD_REDUCE3;
break;
case reduce4 :
info.compression_method = ZIP_METHOD_REDUCE4;
break;
case deflate1 :
case deflate2 :
info.compression_method = ZIP_METHOD_DEFLATE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_DEFLATE_SUPERFAST;
break;
case deflate3 :
case deflate4 :
case deflate5 :
info.compression_method = ZIP_METHOD_DEFLATE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_DEFLATE_FAST;
break;
case deflate6 :
case deflate7 :
case deflate8 :
info.compression_method = ZIP_METHOD_DEFLATE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_DEFLATE_NORMAL;
break;
case deflate9 :
info.compression_method = ZIP_METHOD_DEFLATE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_DEFLATE_MAXIMUM;
break;
case implode_4kdict_2tree :
info.compression_method = ZIP_METHOD_IMPLODE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_IMPLODE_4KD2T;
break;
case implode_8kdict_2tree :
info.compression_method = ZIP_METHOD_IMPLODE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_IMPLODE_8KD2T;
break;
case implode_4kdict_3tree :
info.compression_method = ZIP_METHOD_IMPLODE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_IMPLODE_4KD3T;
break;
case implode_8kdict_3tree :
info.compression_method = ZIP_METHOD_IMPLODE;
info.general_purpose_bit_flag |= ZIP_GEN_FLAGS_IMPLODE_8KD3T;
break;
case bzip2 :
info.compression_method = ZIP_METHOD_BZIP2;
break;
case lzma :
info.compression_method = ZIP_METHOD_LZMA;
break;
default :
throw error_invalid() << "Compression method not supported";
}
data_free(data);
info.compressed_size = compsize;
data = data_dup(compdata, info.compressed_size);
name_set(Aname);
data_free(local_extra_field);
info.local_extra_field_length = 0;
local_extra_field = 0;
data_free(central_extra_field);
info.central_extra_field_length = 0;
central_extra_field = 0;
data_free(file_comment);
info.file_comment_length = 0;
file_comment = 0;
}
void zip_entry::name_set(const string& Aname)
{
data_free(file_name);
info.filename_length = Aname.length();
file_name = data_alloc(info.filename_length);
memcpy(file_name, Aname.c_str(), info.filename_length);
}
string zip_entry::name_get() const
{
return string(file_name, file_name + info.filename_length);
}
/** Check central directory entry. */
void zip_entry::check_cent(const unsigned char* buf) const
{
// check signature
if (le_uint32_read(buf+ZIP_CO_central_file_header_signature) != ZIP_C_signature) {
throw error_invalid() << "Invalid central directory signature";
}
// check filename_length > 0, can't exist a file without a name
if (le_uint16_read(buf+ZIP_CO_filename_length) == 0) {
throw error_invalid() << "Empty filename in central directory";
}
}
/** Check local file header comparing with internal information. */
void zip_entry::check_local(const unsigned char* buf) const
{
if (le_uint32_read(buf+ZIP_LO_local_file_header_signature) != ZIP_L_signature) {
throw error_invalid() << "Invalid signature in local header";
}
if (info.general_purpose_bit_flag != le_uint16_read(buf+ZIP_LO_general_purpose_bit_flag)) {
throw error_invalid() << "Invalid local purpose bit flag " << info.general_purpose_bit_flag << "/" << le_uint16_read(buf+ZIP_LO_general_purpose_bit_flag);
}
if (info.compression_method != le_uint16_read(buf+ZIP_LO_compression_method)) {
throw error_invalid() << "Invalid method on local header";
}
if ((le_uint16_read(buf+ZIP_LO_general_purpose_bit_flag) & ZIP_GEN_FLAGS_DEFLATE_ZERO) != 0) {
if (zip::pedantic) {
if (le_uint32_read(buf+ZIP_LO_crc32) != 0) {
throw error_invalid() << "Not zero crc on local header " << le_uint32_read(buf+ZIP_LO_crc32);
}
if (le_uint32_read(buf+ZIP_LO_compressed_size) != 0) {
throw error_invalid() << "Not zero compressed size in local header " << le_uint32_read(buf+ZIP_LO_compressed_size);
}
if (le_uint32_read(buf+ZIP_LO_uncompressed_size) != 0) {
throw error_invalid() << "Not zero uncompressed size in local header " << le_uint32_read(buf+ZIP_LO_uncompressed_size);
}
} else {
// allow the entries to have the correct value instead of 0
if (le_uint32_read(buf+ZIP_LO_crc32) != 0 && info.crc32 != le_uint32_read(buf+ZIP_LO_crc32)) {
throw error_invalid() << "Not zero crc on local header " << le_uint32_read(buf+ZIP_LO_crc32);
}
if (le_uint32_read(buf+ZIP_LO_compressed_size) != 0 && info.compressed_size != le_uint32_read(buf+ZIP_LO_compressed_size)) {
throw error_invalid() << "Not zero compressed size in local header " << le_uint32_read(buf+ZIP_LO_compressed_size);
}
if (le_uint32_read(buf+ZIP_LO_uncompressed_size) != 0 && info.uncompressed_size != le_uint32_read(buf+ZIP_LO_uncompressed_size)) {
throw error_invalid() << "Not zero uncompressed size in local header " << le_uint32_read(buf+ZIP_LO_uncompressed_size);
}
}
} else {
if (info.crc32 != le_uint32_read(buf+ZIP_LO_crc32)) {
throw error_invalid() << "Invalid crc on local header " << info.crc32 << "/" << le_uint32_read(buf+ZIP_LO_crc32);
}
if (info.compressed_size != le_uint32_read(buf+ZIP_LO_compressed_size)) {
throw error_invalid() << "Invalid compressed size in local header " << info.compressed_size << "/" << le_uint32_read(buf+ZIP_LO_compressed_size);
}
if (info.uncompressed_size != le_uint32_read(buf+ZIP_LO_uncompressed_size)) {
throw error_invalid() << "Invalid uncompressed size in local header " << info.uncompressed_size << "/" << le_uint32_read(buf+ZIP_LO_uncompressed_size);
}
}
if (info.filename_length != le_uint16_read(buf+ZIP_LO_filename_length)) {
throw error_invalid() << "Invalid filename in local header";
}
if (info.local_extra_field_length != le_uint16_read(buf+ZIP_LO_extra_field_length)
&& info.local_extra_field_length != 0 // the .zip generated with the info-zip program have the extra field only on the local header
) {
throw error_invalid() << "Invalid extra field length in local header " << info.local_extra_field_length << "/" << le_uint16_read(buf+ZIP_LO_extra_field_length);
}
}
void zip_entry::check_descriptor(const unsigned char* buf) const
{
if (info.crc32 != le_uint32_read(buf+ZIP_DO_crc32)) {
throw error_invalid() << "Invalid crc on data descriptor " << info.crc32 << "/" << le_uint32_read(buf+ZIP_DO_crc32);
}
if (info.compressed_size != le_uint32_read(buf+ZIP_DO_compressed_size)) {
throw error_invalid() << "Invalid compressed size in data descriptor " << info.compressed_size << "/" << le_uint32_read(buf+ZIP_DO_compressed_size);
}
if (info.uncompressed_size != le_uint32_read(buf+ZIP_DO_uncompressed_size)) {
throw error_invalid() << "Invalid uncompressed size in data descriptor " << info.uncompressed_size << "/" << le_uint32_read(buf+ZIP_DO_uncompressed_size);
}
}
/** Unload compressed/uncomressed data. */
void zip_entry::unload()
{
data_free(data);
data = 0;
}
/** Skip local file header and data. */
void zip::skip_local(const unsigned char* buf, FILE* f)
{
unsigned local_extra_field_length = le_uint16_read(buf+ZIP_LO_extra_field_length);
unsigned filename_length = le_uint16_read(buf+ZIP_LO_filename_length);
unsigned compressed_size = le_uint32_read(buf+ZIP_LO_compressed_size);
// skip filename and extra field
if (fseek(f, filename_length + local_extra_field_length, SEEK_CUR) != 0) {
throw error_invalid() << "Failed seek";
}
// directory don't have data
if (compressed_size) {
if (fseek(f, compressed_size, SEEK_CUR) != 0) {
throw error_invalid() << "Failed seek";
}
}
// data descriptor
if ((le_uint16_read(buf+ZIP_LO_general_purpose_bit_flag) & ZIP_GEN_FLAGS_DEFLATE_ZERO) != 0) {
if (fseek(f, ZIP_DO_FIXED, SEEK_CUR) != 0) {
throw error_invalid() << "Failed seek";
}
}
}
/**
* Load local file header.
* \param buf Fixed size local header.
* \param f File seeked after the fixed size local header.
*/
void zip_entry::load_local(const unsigned char* buf, FILE* f, unsigned size)
{
check_local(buf);
// use the local extra_field_length. It may be different than the
// central directory version in some zips.
unsigned local_extra_field_length = le_uint16_read(buf+ZIP_LO_extra_field_length);
if (size < info.filename_length + local_extra_field_length) {
throw error_invalid() << "Overflow of filename";
}
size -= info.filename_length + local_extra_field_length;
// skip filename and extra field
if (fseek(f, info.filename_length + local_extra_field_length, SEEK_CUR) != 0) {
throw error_invalid() << "Failed seek";
}
data_free(data);
data = data_alloc(info.compressed_size);
if (size < info.compressed_size) {
throw error_invalid() << "Overflow of compressed data";
}
size -= info.compressed_size;
try {
if (info.compressed_size > 0) {
if (fread(data, info.compressed_size, 1, f) != 1) {
throw error() << "Failed read";
}
}
} catch (...) {
data_free(data);
data = 0;
throw;
}
// load the data descriptor
if ((le_uint16_read(buf+ZIP_LO_general_purpose_bit_flag) & ZIP_GEN_FLAGS_DEFLATE_ZERO) != 0) {
unsigned char data_desc[ZIP_DO_FIXED];
if (size < ZIP_DO_FIXED) {
throw error_invalid() << "Overflow of data descriptor";
}
size -= ZIP_DO_FIXED;
if (fread(data_desc, ZIP_DO_FIXED, 1, f) != 1) {
throw error() << "Failed read";
}
check_descriptor(data_desc);
}
}
/**
* Save local file header.
* \param f File seeked at correct position.
*/
void zip_entry::save_local(FILE* f)
{
long offset = ftell(f);
if (offset<0)
throw error() << "Failed tell";
info.relative_offset_of_local_header = offset;
// write header
unsigned char buf[ZIP_LO_FIXED];
le_uint32_write(buf+ZIP_LO_local_file_header_signature, ZIP_L_signature);
le_uint8_write(buf+ZIP_LO_version_needed_to_extract, info.version_needed_to_extract);
le_uint8_write(buf+ZIP_LO_os_needed_to_extract, info.os_needed_to_extract);
// clear the "data descriptor" bit
le_uint16_write(buf+ZIP_LO_general_purpose_bit_flag, info.general_purpose_bit_flag & ~ZIP_GEN_FLAGS_DEFLATE_ZERO);
le_uint16_write(buf+ZIP_LO_compression_method, info.compression_method);
le_uint16_write(buf+ZIP_LO_last_mod_file_time, info.last_mod_file_time);
le_uint16_write(buf+ZIP_LO_last_mod_file_date, info.last_mod_file_date);
le_uint32_write(buf+ZIP_LO_crc32, info.crc32);
le_uint32_write(buf+ZIP_LO_compressed_size, info.compressed_size);
le_uint32_write(buf+ZIP_LO_uncompressed_size, info.uncompressed_size);
le_uint16_write(buf+ZIP_LO_filename_length, info.filename_length);
le_uint16_write(buf+ZIP_LO_extra_field_length, info.local_extra_field_length);
if (fwrite(buf, ZIP_LO_FIXED, 1, f) != 1) {
throw error() << "Failed write";
}
// write filename
if (fwrite(file_name, info.filename_length, 1, f) != 1) {
throw error() << "Failed write";
}
// write the extra field
if (info.local_extra_field_length && fwrite(local_extra_field, info.local_extra_field_length, 1, f) != 1) {
throw error() << "Failed write";
}
// write data, directories don't have data
if (info.compressed_size) {
assert(data);
if (fwrite(data, info.compressed_size, 1, f) != 1) {
throw error() << "Failed write";
}
}
}
/**
* Load cent dir.
* \param buf Fixed size cent dir.
* \param f File seeked after the fixed size cent dir.
*/
void zip_entry::load_cent(const unsigned char* buf, unsigned& skip)
{
const unsigned char* o_buf = buf;
check_cent(buf);
// read header
info.version_made_by = le_uint8_read(buf+ZIP_CO_version_made_by);
info.host_os = le_uint8_read(buf+ZIP_CO_host_os);
info.version_needed_to_extract = le_uint8_read(buf+ZIP_CO_version_needed_to_extract);
info.os_needed_to_extract = le_uint8_read(buf+ZIP_CO_os_needed_to_extract);
info.general_purpose_bit_flag = le_uint16_read(buf+ZIP_CO_general_purpose_bit_flag);
info.compression_method = le_uint16_read(buf+ZIP_CO_compression_method);
info.last_mod_file_time = le_uint16_read(buf+ZIP_CO_last_mod_file_time);
info.last_mod_file_date = le_uint16_read(buf+ZIP_CO_last_mod_file_date);
info.crc32 = le_uint32_read(buf+ZIP_CO_crc32);
info.compressed_size = le_uint32_read(buf+ZIP_CO_compressed_size);
info.uncompressed_size = le_uint32_read(buf+ZIP_CO_uncompressed_size);
info.filename_length = le_uint16_read(buf+ZIP_CO_filename_length);
info.central_extra_field_length = le_uint16_read(buf+ZIP_CO_extra_field_length);
info.file_comment_length = le_uint16_read(buf+ZIP_CO_file_comment_length);
info.internal_file_attrib = le_uint16_read(buf+ZIP_CO_internal_file_attrib);
info.external_file_attrib = le_uint32_read(buf+ZIP_CO_external_file_attrib);
info.relative_offset_of_local_header = le_uint32_read(buf+ZIP_CO_relative_offset_of_local_header);
buf += ZIP_CO_FIXED;
// read filename
data_free(file_name);
file_name = data_alloc(info.filename_length);
memcpy(file_name, buf, info.filename_length);
buf += info.filename_length;
// read extra field
data_free(central_extra_field);
central_extra_field = data_dup(buf, info.central_extra_field_length);
buf += info.central_extra_field_length;
// read comment
data_free(file_comment);
file_comment = data_dup(buf, info.file_comment_length);
buf += info.file_comment_length;
skip = buf - o_buf;
}
/**
* Save cent dir.
* \param f File seeked at correct position.
*/
void zip_entry::save_cent(FILE* f)
{
unsigned char buf[ZIP_CO_FIXED];
le_uint32_write(buf+ZIP_CO_central_file_header_signature, ZIP_C_signature);
le_uint8_write(buf+ZIP_CO_version_made_by, info.version_made_by);
le_uint8_write(buf+ZIP_CO_host_os, info.host_os);
le_uint8_write(buf+ZIP_CO_version_needed_to_extract, info.version_needed_to_extract);
le_uint8_write(buf+ZIP_CO_os_needed_to_extract, info.os_needed_to_extract);
// clear the "data descriptor" bit
le_uint16_write(buf+ZIP_CO_general_purpose_bit_flag, info.general_purpose_bit_flag & ~ZIP_GEN_FLAGS_DEFLATE_ZERO);
le_uint16_write(buf+ZIP_CO_compression_method, info.compression_method);
le_uint16_write(buf+ZIP_CO_last_mod_file_time, info.last_mod_file_time);
le_uint16_write(buf+ZIP_CO_last_mod_file_date, info.last_mod_file_date);
le_uint32_write(buf+ZIP_CO_crc32, info.crc32);
le_uint32_write(buf+ZIP_CO_compressed_size, info.compressed_size);
le_uint32_write(buf+ZIP_CO_uncompressed_size, info.uncompressed_size);
le_uint16_write(buf+ZIP_CO_filename_length, info.filename_length);
le_uint16_write(buf+ZIP_CO_extra_field_length, info.central_extra_field_length);
le_uint16_write(buf+ZIP_CO_file_comment_length, info.file_comment_length);
le_uint16_write(buf+ZIP_CO_disk_number_start, ZIP_UNIQUE_DISK);
le_uint16_write(buf+ZIP_CO_internal_file_attrib, info.internal_file_attrib);
le_uint32_write(buf+ZIP_CO_external_file_attrib, info.external_file_attrib);
le_uint32_write(buf+ZIP_CO_relative_offset_of_local_header, info.relative_offset_of_local_header);
if (fwrite(buf, ZIP_CO_FIXED, 1, f) != 1) {
throw error() << "Failed write";
}
// write filename
if (fwrite(file_name, info.filename_length, 1, f) != 1) {
throw error() << "Failed write";
}
// write extra field
if (info.central_extra_field_length && fwrite(central_extra_field, info.central_extra_field_length, 1, f) != 1) {
throw error() << "Failed write";
}
// write comment
if (info.file_comment_length && fwrite(file_comment, info.file_comment_length, 1, f) != 1) {
throw error() << "Failed write";
}
}
zip::zip(const std::string& Apath) : path(Apath)
{
flag.open = false;
flag.read = false;
flag.modify = false;
zipfile_comment = 0;
}
zip::zip(const zip& A) : map(A.map), path(A.path)
{
flag = A.flag;
info = A.info;
zipfile_comment = data_dup(A.zipfile_comment, A.info.zipfile_comment_length);
}
zip::~zip()
{
if (is_open())
close();
}
void zip::create()
{
assert(!flag.open);
info.offset_to_start_of_cent_dir = 0;
info.zipfile_comment_length = 0;
data_free(zipfile_comment);
zipfile_comment = 0;
flag.read = true;
flag.open = true;
flag.modify = false;
}
void zip::open()
{
assert(!flag.open);
struct stat s;
if (stat(path.c_str(), &s) != 0) {
if (errno != ENOENT)
throw error() << "Failed stat";
// create the file if it's missing
create();
return;
}
unsigned length = s.st_size;
// open file
FILE* f = fopen(path.c_str(), "rb");
if (!f)
throw error() << "Failed open for reading";
unsigned char* data = 0;
unsigned data_size = 0;
try {
if (!cent_read(f, length, data, data_size))
throw error_invalid() << "Failed read end of central directory";
} catch (...) {
fclose(f);
throw;
}
fclose(f);
// position in data
unsigned data_pos = 0;
try {
// central dir
while (le_uint32_read(data+data_pos) == ZIP_C_signature) {
iterator i = map.insert(map.end(), zip_entry(path));
unsigned skip = 0;
try {
i->load_cent(data + data_pos, skip);
} catch (...) {
map.erase(i);
throw;
}
data_pos += skip;
}
// end of central dir
if (le_uint32_read(data+data_pos) != ZIP_E_signature)
throw error_invalid() << "Invalid end of central dir signature";
info.offset_to_start_of_cent_dir = le_uint32_read(data+data_pos+ZIP_EO_offset_to_start_of_cent_dir);
info.zipfile_comment_length = le_uint16_read(data+data_pos+ZIP_EO_zipfile_comment_length);
data_pos += ZIP_EO_FIXED;
if (info.offset_to_start_of_cent_dir != length - data_size)
throw error_invalid() << "Invalid end of central directory start address";
// comment
data_free(zipfile_comment);
zipfile_comment = data_dup(data+data_pos, info.zipfile_comment_length);
data_pos += info.zipfile_comment_length;
} catch (...) {
data_free(data);
throw;
}
// delete cent data
data_free(data);
if (pedantic) {
// don't accept garbage at the end of file
if (data_pos != data_size)
throw error_invalid() << data_size - data_pos << " unused bytes at the end of the central directory";
}
flag.open = true;
flag.read = false;
flag.modify = false;
}
/**
* Close a zip file.
*/
void zip::close()
{
// deinizialize
flag.open = false;
flag.read = false;
flag.modify = false;
data_free(zipfile_comment);
zipfile_comment = 0;
path = "";
map.erase(map.begin(), map.end());
}
/**
* Discarge compressed data read.
* \note You cannot call unload() if zip is modified, you must call reopen().
*/
void zip::unload()
{
assert(flag.open && flag.read && !flag.modify);
for(iterator i=begin();i!=end();++i)
i->unload();
flag.read = false;
}
/**
* Reopen from zip on disk, lose any modify.
* \note Equivalent close and open.
*/
void zip::reopen()
{
assert(flag.open);
close();
open();
}
/**
* Load a zip file.
*/
void zip::load()
{
assert(flag.open && !flag.read);
flag.modify = false;
FILE* f = fopen(path.c_str(), "rb");
if (!f)
throw error() << "Failed open for reading";
try {
long offset = 0;
unsigned count = 0;
while (static_cast<unsigned long>(offset) < info.offset_to_start_of_cent_dir) {
unsigned char buf[ZIP_LO_FIXED];
// search the next item, assume entries in random order
iterator next;
bool next_set = false;
for(iterator i=begin();i!=end();++i) {
if (i->offset_get() >= static_cast<unsigned long>(offset)) {
if (!next_set || i->offset_get() < next->offset_get()) {
next_set = true;
next = i;
}
}
}
// if not found exit
if (!next_set) {
if (pedantic)
throw error_invalid() << info.offset_to_start_of_cent_dir - static_cast<unsigned long>(offset) << " unused bytes after the last local header at offset " << offset;
else
break;
}
// check for invalid start
if (next->offset_get() >= info.offset_to_start_of_cent_dir) {
throw error_invalid() << "Overflow in central directory";
}
// check for a data hole
if (next->offset_get() > static_cast<unsigned long>(offset)) {
if (pedantic)
throw error_invalid() << next->offset_get() - static_cast<unsigned long>(offset) << " unused bytes at offset " << offset;
else {
// set the correct position
if (fseek(f, next->offset_get(), SEEK_SET) != 0)
throw error() << "Failed fseek";
offset = next->offset_get();
}
}
// search the next item, assume entries in random order
iterator next_next;
bool next_next_set = false;
for(iterator i=begin();i!=end();++i) {
if (i->offset_get() > static_cast<unsigned long>(offset)) {
if (!next_next_set || i->offset_get() < next_next->offset_get()) {
next_next_set = true;
next_next = i;
}
}
}
unsigned long end_offset;
if (next_next_set)
end_offset = next_next->offset_get();
else
end_offset = info.offset_to_start_of_cent_dir;
if (end_offset < next->offset_get() + ZIP_LO_FIXED) {
throw error_invalid() << "Invalid local header size at offset " << offset;
}
if (fread(buf, ZIP_LO_FIXED, 1, f) != 1)
throw error() << "Failed read";
next->load_local(buf, f, end_offset - next->offset_get() - ZIP_LO_FIXED);
++count;
offset = ftell(f);
if (offset < 0)
throw error() << "Failed tell";
}
if (static_cast<unsigned long>(offset) != info.offset_to_start_of_cent_dir) {
if (pedantic)
throw error_invalid() << "Invalid central directory start";
}
if (count != size())
throw error_invalid() << "Invalid central directory, expected " << size() << " local headers, got " << count;
} catch (...) {
fclose(f);
throw;
}
fclose(f);
flag.read = true;
}
unsigned zip::size_not_zero() const
{
unsigned count = 0;
// check if it contains at least one file with size > 0 (directories are excluded)
for(const_iterator i=begin();i!=end();++i) {
if (i->uncompressed_size_get() > 0) {
++count;
}
}
return count;
}
/**
* Save a zip file.
*/
void zip::save()
{
assert(flag.open && flag.read);
flag.modify = false;
if (!empty()) {
// prevent external signal
sig_auto_lock sal;
// temp name of the saved file
string save_path = file_temp(path);
FILE* f = fopen(save_path.c_str(), "wb");
if (!f)
throw error() << "Failed open for writing of " << save_path;
try {
// write local header
for(iterator i=begin();i!=end();++i)
i->save_local(f);
long cent_offset = ftell(f);
if (cent_offset<0)
throw error() << "Failed tell";
// new cent start
info.offset_to_start_of_cent_dir = cent_offset;
// write cent dir
for(iterator i=begin();i!=end();++i)
i->save_cent(f);
long end_cent_offset = ftell(f);
if (end_cent_offset<0)
throw error() << "Failed tell";
// write end of cent dir
unsigned char buf[ZIP_EO_FIXED];
le_uint32_write(buf+ZIP_EO_end_of_central_dir_signature, ZIP_E_signature);
le_uint16_write(buf+ZIP_EO_number_of_this_disk, ZIP_UNIQUE_DISK);
le_uint16_write(buf+ZIP_EO_number_of_disk_start_cent_dir, ZIP_UNIQUE_DISK);
le_uint16_write(buf+ZIP_EO_total_entries_cent_dir_this_disk, size());
le_uint16_write(buf+ZIP_EO_total_entries_cent_dir, size());
le_uint32_write(buf+ZIP_EO_size_of_cent_dir, end_cent_offset - cent_offset);
le_uint32_write(buf+ZIP_EO_offset_to_start_of_cent_dir, cent_offset);
le_uint16_write(buf+ZIP_EO_zipfile_comment_length, info.zipfile_comment_length);
if (fwrite(buf, ZIP_EO_FIXED, 1, f) != 1)
throw error() << "Failed write";
// write comment
if (info.zipfile_comment_length && fwrite(zipfile_comment, info.zipfile_comment_length, 1, f) != 1)
throw error() << "Failed write";
} catch (...) {
fclose(f);
remove(save_path.c_str());
throw;
}
fclose(f);
// delete the file if exists
if (access(path.c_str(), F_OK) == 0) {
if (remove(path.c_str()) != 0) {
remove(save_path.c_str());
throw error() << "Failed delete of " << path;
}
}
// rename the new version with the correct name
if (::rename(save_path.c_str(), path.c_str()) != 0) {
throw error() << "Failed rename of " << save_path << " to " << path;
}
} else {
// reset the cent start
info.offset_to_start_of_cent_dir = 0;
// delete the file if exists
if (access(path.c_str(), F_OK) == 0) {
if (remove(path.c_str()) != 0)
throw error() << "Failed delete of " << path;
}
}
}
/**
* Remove a compressed file.
*/
void zip::erase(iterator i)
{
assert(flag.read);
flag.modify = true;
map.erase(i);
}
/**
* Rename a compressed file.
* \note No filename overwrite check.
*/
void zip::rename(iterator i, const string& Aname)
{
assert(flag.read);
flag.modify = true;
i->name_set(Aname);
}
/**
* Insert a zip entry.
*/
zip::iterator zip::insert(const zip_entry& A, const string& Aname)
{
iterator i;
assert(flag.read);
unsigned char* data = data_alloc(A.compressed_size_get());
assert(data);
try {
A.compressed_read(data);
i = map.insert(map.end(), zip_entry(path));
try {
i->set(A.method_get(), Aname, data, A.compressed_size_get(), A.uncompressed_size_get(), A.crc_get(), A.zipdate_get(), A.ziptime_get(), A.is_text());
} catch (...) {
map.erase(i);
throw;
}
flag.modify = true;
} catch (...) {
data_free(data);
throw;
}
data_free(data);
return i;
}
/**
* Add data to a zip file.
* The data is deflated or stored. No filename overwrite check.
*/
zip::iterator zip::insert_uncompressed(const string& Aname, const unsigned char* data, unsigned size, unsigned crc, time_t tod, bool is_text)
{
iterator i;
assert(flag.read);
assert(crc == crc32(0, (const unsigned char*)data, size));
unsigned date = 0;
unsigned time = 0;
time2zip(tod, date, time);
i = map.insert(map.end(), zip_entry(path));
try {
i->set(zip_entry::store, Aname, data, size, size, crc, date, time, is_text);
} catch (...) {
map.erase(i);
throw;
}
flag.modify = true;
return i;
}
|