-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip_archive.cpp
More file actions
172 lines (141 loc) · 6 KB
/
zip_archive.cpp
File metadata and controls
172 lines (141 loc) · 6 KB
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
#include "zip_archive.h"
#include <algorithm>
#include <fstream>
#include <iostream>
namespace gd {
namespace {
uint8_t read_u8(std::istream & input) {
char byte = 0;
input.read(&byte, 1);
return static_cast<uint8_t>(byte);
}
uint16_t read_u16(std::istream & input) {
uint8_t bytes[2] = {0, 0};
input.read(reinterpret_cast<char *>(bytes), sizeof(bytes));
return static_cast<uint16_t>(bytes[0]) |
(static_cast<uint16_t>(bytes[1]) << 8);
}
uint32_t read_u32(std::istream & input) {
uint8_t bytes[4] = {0, 0, 0, 0};
input.read(reinterpret_cast<char *>(bytes), sizeof(bytes));
return static_cast<uint32_t>(bytes[0]) |
(static_cast<uint32_t>(bytes[1]) << 8) |
(static_cast<uint32_t>(bytes[2]) << 16) |
(static_cast<uint32_t>(bytes[3]) << 24);
}
} // namespace
std::optional<uint64_t> find_end_of_central_directory(std::ifstream & input, uint64_t file_size) {
const uint64_t max_scan = 65535 + 22;
const uint64_t read_size = std::min<uint64_t>(file_size, max_scan);
std::vector<uint8_t> tail(read_size);
input.seekg(static_cast<std::streamoff>(file_size - read_size), std::ios::beg);
input.read(reinterpret_cast<char *>(tail.data()), static_cast<std::streamsize>(tail.size()));
if (!input) {
return std::nullopt;
}
for (int64_t index = static_cast<int64_t>(tail.size()) - 22; index >= 0; --index) {
const size_t i = static_cast<size_t>(index);
if (tail[i + 0] == 0x50 && tail[i + 1] == 0x4b && tail[i + 2] == 0x05 && tail[i + 3] == 0x06) {
return file_size - read_size + static_cast<uint64_t>(index);
}
}
return std::nullopt;
}
std::optional<std::vector<ZipEntry>> read_zip_entries(const fs::path & archive_path) {
std::ifstream input(archive_path, std::ios::binary);
if (!input) {
std::cerr << "Failed to open archive: " << archive_path << '\n';
return std::nullopt;
}
input.seekg(0, std::ios::end);
const uint64_t file_size = static_cast<uint64_t>(input.tellg());
if (file_size < 22) {
std::cerr << "Archive is too small to be a valid zip file: " << archive_path << '\n';
return std::nullopt;
}
const std::optional<uint64_t> eocd_offset = find_end_of_central_directory(input, file_size);
if (!eocd_offset.has_value()) {
std::cerr << "Could not locate zip central directory: " << archive_path << '\n';
return std::nullopt;
}
input.seekg(static_cast<std::streamoff>(*eocd_offset), std::ios::beg);
if (read_u32(input) != 0x06054b50U) {
std::cerr << "Invalid EOCD signature in: " << archive_path << '\n';
return std::nullopt;
}
(void) read_u16(input);
(void) read_u16(input);
(void) read_u16(input);
const uint16_t total_entries = read_u16(input);
(void) read_u32(input);
const uint32_t central_directory_offset = read_u32(input);
(void) read_u16(input);
std::vector<ZipEntry> entries;
entries.reserve(total_entries);
input.seekg(static_cast<std::streamoff>(central_directory_offset), std::ios::beg);
for (uint16_t index = 0; index < total_entries; ++index) {
if (read_u32(input) != 0x02014b50U) {
std::cerr << "Invalid central directory header in: " << archive_path << '\n';
return std::nullopt;
}
(void) read_u16(input);
(void) read_u16(input);
(void) read_u16(input);
ZipEntry entry;
entry.compression_method = read_u16(input);
(void) read_u16(input);
(void) read_u16(input);
(void) read_u32(input);
entry.compressed_size = read_u32(input);
entry.uncompressed_size = read_u32(input);
const uint16_t file_name_length = read_u16(input);
const uint16_t extra_field_length = read_u16(input);
const uint16_t file_comment_length = read_u16(input);
(void) read_u16(input);
(void) read_u16(input);
(void) read_u32(input);
entry.local_header_offset = read_u32(input);
entry.name.resize(file_name_length);
input.read(entry.name.data(), static_cast<std::streamsize>(file_name_length));
std::streampos central_resume = input.tellg();
input.seekg(static_cast<std::streamoff>(entry.local_header_offset), std::ios::beg);
if (read_u32(input) != 0x04034b50U) {
std::cerr << "Invalid local header for entry: " << entry.name << '\n';
return std::nullopt;
}
(void) read_u16(input);
(void) read_u16(input);
(void) read_u16(input);
(void) read_u16(input);
(void) read_u16(input);
(void) read_u32(input);
(void) read_u32(input);
(void) read_u32(input);
const uint16_t local_name_length = read_u16(input);
const uint16_t local_extra_length = read_u16(input);
entry.data_offset = static_cast<uint64_t>(entry.local_header_offset) + 30 + local_name_length + local_extra_length;
input.seekg(central_resume + static_cast<std::streamoff>(extra_field_length + file_comment_length), std::ios::beg);
entries.push_back(std::move(entry));
}
return entries;
}
std::optional<std::vector<uint8_t>> read_stored_zip_entry(const fs::path & archive_path, const ZipEntry & entry) {
if (entry.compression_method != 0) {
std::cerr << "Unsupported compression method " << entry.compression_method << " for entry: " << entry.name << '\n';
return std::nullopt;
}
std::ifstream input(archive_path, std::ios::binary);
if (!input) {
std::cerr << "Failed to reopen archive: " << archive_path << '\n';
return std::nullopt;
}
input.seekg(static_cast<std::streamoff>(entry.data_offset), std::ios::beg);
std::vector<uint8_t> data(entry.uncompressed_size);
input.read(reinterpret_cast<char *>(data.data()), static_cast<std::streamsize>(data.size()));
if (!input) {
std::cerr << "Failed to read payload for entry: " << entry.name << '\n';
return std::nullopt;
}
return data;
}
} // namespace gd