File: zip_dir.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (238 lines) | stat: -rw-r--r-- 5,749 bytes parent folder | download | duplicates (5)
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
#include <string.h>
#include "zip_dir.h"
#include "logger.h"
#include "chunk.h"
#include "exception.h"
#include "ioexception.h"
#include "zip_file.h"

using namespace mrt;

//mrt::ZipDirectory xxx;
struct LocalFileHeader {

	unsigned version;
	unsigned flags;
	unsigned method;
	unsigned mtime, mdate;
	unsigned crc32;
	unsigned csize, usize;
	
	std::string fname;
	mrt::Chunk extra;

	size_t data_offset;
	
private: 
	unsigned fsize, esize;
	
protected: 
	void read0(const mrt::BaseFile &file) {
		file.readLE16(version);
		file.readLE16(flags);
		file.readLE16(method);

		file.readLE16(mtime);
		file.readLE16(mdate);
		
		file.readLE32(crc32);
		file.readLE32(csize);
		file.readLE32(usize);

		//LOG_DEBUG(("version: %d, flags: %04x, compression: %d, crc32: %08x, size: %u/%u", version, flags, compression, crc32, csize, usize));
		
		file.readLE16(fsize);
		file.readLE16(esize);
	}

	void readFE(const mrt::BaseFile &file) {
		if (fsize > 0) {
			extra.set_size(fsize);
			if (file.read(extra.get_ptr(), fsize) != fsize)
				throw_ex(("unexpected end of archive"));

			fname.assign((const char *)extra.get_ptr(), fsize);
		} else {
			fname.clear();
		}

		if (esize > 0) {
			extra.set_size(esize);
			if (file.read(extra.get_ptr(), esize) != esize)
				throw_ex(("unexpected end of archive"));
		} else {
			extra.free();
		}
		data_offset = file.tell();
		//LOG_DEBUG(("file: \"%s\", extra data: %s, data offset: %u", fname.c_str(), extra.dump().c_str(), (unsigned)data_offset));
	}

public: 
	void read(const mrt::BaseFile &file) {
		read0(file);
		readFE(file);
		file.seek(csize, SEEK_CUR);
	}
};

struct CentralDirectorySignature : public LocalFileHeader {
	mrt::Chunk comment;
	
	unsigned disk_number;
	unsigned internal_attrs, external_attrs;
	int header_offset;
private: 
	unsigned comment_size;
public: 	
	void read(const mrt::BaseFile &file) {
		unsigned version_made;
		file.readLE16(version_made);
		//LOG_DEBUG(("central directory signature, made by version %d", version_made));
		read0(file);
		file.readLE16(comment_size);
		file.readLE16(disk_number);
		
		file.readLE16(internal_attrs);
		file.readLE32(external_attrs);
		file.readLE32(header_offset);
		
		readFE(file);
		
		if (comment_size > 0) {
			comment.set_size(comment_size);
			if (file.read(comment.get_ptr(), comment_size) != comment_size)
				throw_ex(("unexpected end of the archive"));
		} else {
			comment.free();
		}
		//LOG_DEBUG(("comment: %s, header offset: %d", comment.dump().c_str(), header_offset));
	}	
};


struct EndOfCentralDirectorySignature {
	unsigned disk_number, central_disk_number, central_on_this_disk;
	unsigned entries;
	
	unsigned size;
	int central_offset;

	mrt::Chunk comment;
	
private: 
	unsigned comment_size;
public: 	
	void read(const mrt::BaseFile &file) {
		file.readLE16(disk_number);
		file.readLE16(central_disk_number);
		file.readLE16(central_on_this_disk);
		file.readLE16(entries);
		
		file.readLE32(size);
		file.readLE32(central_offset);
		file.readLE16(comment_size);
		if (comment_size > 0) {
			comment.set_size(comment_size);
			if (file.read(comment.get_ptr(), comment_size) != comment_size) 
				throw_ex(("unexpected end of the archive"));
		} else comment.free();
	}
};


ZipDirectory::ZipDirectory(const std::string &zip) : fname(zip) {
	LOG_DEBUG(("opening archive: %s", zip.c_str()));
	archive.open(zip, "rb");
	unsigned magic;
	while(!archive.eof()) {
		try {
			archive.readLE32(magic);
		} catch(...) {
			break;
		}
		if (magic == 0x04034b50) {
			LocalFileHeader lfh;
			lfh.read(archive);
			
			FileDesc &file = headers[lfh.fname];
			file.flags = lfh.flags;
			file.method = lfh.method;
			file.csize = lfh.csize;
			file.usize = lfh.usize;
			file.offset = lfh.data_offset;
		} else if (magic == 0x02014b50) {
			CentralDirectorySignature cds;
			cds.read(archive);
		} else if (magic == 0x06054b50) {
			EndOfCentralDirectorySignature ecds;
			ecds.read(archive);
		} else {
			LOG_WARN(("unknown magic: %08x", magic));
			break;
		}
	}
	LOG_DEBUG(("loaded %u files.", (unsigned)headers.size()));
}

void ZipDirectory::open(const std::string &path_) {
}

bool ZipDirectory::opened() const {
	return false;
}

const std::string ZipDirectory::read() const {
	return std::string();
}

void ZipDirectory::close() {}

void ZipDirectory::create(const std::string &path, const bool recurse) {}

ZipDirectory::~ZipDirectory() {
	archive.close();
}

ZipFile * ZipDirectory::open_file(const std::string &name_) const {
	std::string name = mrt::FSNode::normalize(name_);
	Headers::const_iterator i = headers.find(name);
	if (i == headers.end())
		return NULL;
	const FileDesc &file = i->second;

	FILE * f = fopen(fname.c_str(), "rb");
	if (f == NULL)
		throw_io(("fopen(%s)", fname.c_str()));	
	
	return new ZipFile(f, file.method, file.flags, file.offset, file.csize, file.usize);
}

bool mrt::ZipDirectory::lessnocase::operator()(const std::string& s1, const std::string& s2) const {
#ifdef _WINDOWS
		return _stricmp(s1.c_str(), s2.c_str()) < 0;
#else
		return strcasecmp(s1.c_str(), s2.c_str()) < 0;
#endif
}

bool ZipDirectory::exists(const std::string &fname_) const {
	std::string fname = mrt::FSNode::normalize(fname_);
	return headers.find(fname) != headers.end();
}

void ZipDirectory::enumerate(std::vector<std::string>&files, const std::string &root) const {
	if (root.empty()) {
		for(Headers::const_iterator i = headers.begin(); i != headers.end(); ++i) {
			files.push_back(i->first);
		}
	} else {
		for(Headers::const_iterator i = headers.begin(); i != headers.end(); ++i) {
			const std::string &fname = i->first;
			if (fname.compare(0, root.size(), root))
				continue;
			std::string file = fname.substr(root.size() + 1);
			if (!file.empty())
				files.push_back(file);
		}
	}
}