/
zipfile.cpp
120 lines (112 loc) · 3.91 KB
/
zipfile.cpp
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
/*
* Copyright (C) 2016 Jussi Pakkanen.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of version 3 of the GNU General Public License as published
* by the Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include"zipfile.h"
#include"zimp.h"
#include<sys/mman.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<cstdio>
#include<cerrno>
#include<cstring>
#include<stdexcept>
#include<memory>
#include<future>
#include<thread>
const constexpr int LOCAL_SIG = 0x04034b50;
namespace {
localheader read_local_entry(FILE *f) {
localheader h;
h.signature = LOCAL_SIG;
fread(&h.needed_version, sizeof(uint16_t), 1, f);
fread(&h.gp_bitflag, sizeof(uint16_t), 1, f);
fread(&h.compression, sizeof(uint16_t), 1, f);
fread(&h.last_mod_time, sizeof(uint16_t), 1, f);
fread(&h.last_mod_date, sizeof(uint16_t), 1, f);
fread(&h.crc32, 4, 1, f);
fread(&h.compressed_size, sizeof(uint32_t), 1, f);
fread(&h.uncompressed_size, sizeof(uint32_t), 1, f);
fread(&h.file_name_length, sizeof(uint16_t), 1, f);
fread(&h.extra_field_length, sizeof(uint16_t), 1, f);
h.fname.insert(0, h.file_name_length, 'a');
h.extra.insert(0, h.extra_field_length, 'b');
fread(&(h.fname[0]), h.file_name_length, 1, f);
fread(&(h.extra[0]), h.extra_field_length, 1, f);
return h;
}
}
ZipFile::ZipFile(const char *fname) : zipfile(fname) {
std::unique_ptr<FILE, int(*)(FILE *f)> ifile(fopen(fname, "r"), fclose);
if(!ifile) {
throw std::runtime_error("Could not open input file.");
}
while(true) {
auto curloc = ftell(ifile.get());
uint32_t head;
fread(&head, sizeof(uint32_t), 1, ifile.get());
if(head != LOCAL_SIG) {
fseek(ifile.get(), curloc, SEEK_SET);
break;
}
entries.push_back(read_local_entry(ifile.get()));
data_offsets.push_back(ftell(ifile.get()));
fseek(ifile.get(), entries.back().compressed_size, SEEK_CUR);
if(entries.back().gp_bitflag & (1<<2)) {
fseek(ifile.get(), 3*4, SEEK_CUR);
}
}
fseek(ifile.get(), 0, SEEK_END);
fsize = ftell(ifile.get());
}
void ZipFile::unzip() const {
int fd = open(zipfile.c_str(), O_RDONLY);
if(fd < 0) {
std::string msg("Could not open zip file:");
msg += strerror(errno);
throw std::runtime_error(msg);
}
auto unmapper = [&](void* d) { munmap(d, fsize); };
std::unique_ptr<void, decltype(unmapper)> data(mmap(nullptr, fsize, PROT_READ, MAP_PRIVATE, fd, 0),
unmapper);
close(fd);
if(!data) {
std::string msg("Could not mmap zip file:");
msg += strerror(errno);
throw std::runtime_error(msg);
}
unsigned char *file_start = (unsigned char*)(data.get());
std::vector<std::future<void>> futures;
for(size_t i=0; i<entries.size(); i++) {
if(entries[i].compression != 8) {
printf("Skipping %s, is not compressed with deflate.\n", entries[i].fname.c_str());
continue;
}
// printf("Uncompressing %s.\n", entries[i].fname.c_str());
auto deftask = [this, file_start, i](){
inflate_to_file(file_start + data_offsets[i],
entries[i].compressed_size,
entries[i].fname);
};
futures.emplace_back(std::async(std::launch::async, deftask));
}
for(const auto &i : futures) {
try {
i.wait();
} catch(const std::exception &e) {
printf("%s\n", e.what());
}
}
}