summaryrefslogtreecommitdiff
path: root/contrib/paq.cpp
blob: 9de5a0181546248bf429c2f1afa863c9babd4bcf (plain)
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <exception>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/gzip.hpp>

#ifdef _WIN32
	#define DIR_SEPARATOR "\\"
#else
	#define DIR_SEPARATOR "/"
#endif

using namespace std;

class my_exception : public exception
{
private:
	string cause;

public:
	my_exception(const string& cause) : cause(cause) { }
	~my_exception() throw () { }
	virtual const char* what() const throw() { return cause.c_str(); }
};

struct index_entry
{
	index_entry(const string& file_name, bool is_dir) : file_name(file_name), is_dir(is_dir), file_size(0) { }

	unsigned int file_size;
	string file_name;
	bool is_dir;
};

class paq_extractor
{
private:
	list<index_entry> index;
	vector<char> files;

	void process(const vector<char>& v)
	{
		unsigned int i = 4, depth = 1;
		while (depth)
		{
			unsigned int fname_size = static_cast<unsigned char>(v[i++]);
			if (fname_size)
			{
				if (i + fname_size + 6 > v.size()) throw my_exception("Unexpected end of PAQ file");
				index.push_back(index_entry(string(&v[i], &v[i + fname_size]), v[i + fname_size + 4]));
				if (index.back().is_dir)
					++depth;
				else
					index.back().file_size = *reinterpret_cast<const unsigned int*>(&v[i + fname_size]);
				i += fname_size + 5;
			}
			else
			{
				index.push_back(index_entry(string(""), 0));
				--depth;
			}
		}

		files.assign(v.begin() + i, v.end());
		for(list<index_entry>::iterator i_it = index.begin(); i_it != index.end(); i_it++)
			i += i_it->file_size;
		if (i < v.size()) throw my_exception("Unexpected end of PAQ file");
	}

	void extract_r(string path, list<index_entry>::iterator& i_it, vector<char>::iterator& f_it)
	{
		boost::filesystem::create_directory(path);
		for(i_it; i_it->file_name.size() != 0; i_it++)
			if (i_it->is_dir)
				extract_r(path + DIR_SEPARATOR + i_it++->file_name, i_it, f_it);
			else
			{
				const string file_path = path + DIR_SEPARATOR + i_it->file_name;
				cout << "Extracting " << file_path << endl;
				ofstream out(file_path.c_str(), ios::binary);
				if (!out) throw my_exception(string("Can't open ") + file_path);
				if (i_it->file_size > 4)
				{
					boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
					in.push(boost::iostreams::gzip_decompressor());
					in.push(boost::iostreams::array_source(&(*f_it) + 4, i_it->file_size - 4));
					boost::iostreams::copy(in, out);
					out.close();
					if (*reinterpret_cast<unsigned int*>(&(*f_it)) != boost::filesystem::file_size(file_path)) throw my_exception(string("Uncompressed file size of ") + file_path + " inconsistent with PAQ entry for this file");
				}
				f_it += i_it->file_size;
			}
	}

	void print_r(string path, list<index_entry>::iterator& i_it)
	{
		for(i_it; i_it->file_name.size() != 0; i_it++)
			if (i_it->is_dir)
				print_r(path + DIR_SEPARATOR + i_it++->file_name, i_it);
			else
				cout << (path + DIR_SEPARATOR + i_it->file_name).c_str() + 1 << endl;
	}

public:
	paq_extractor(const char* paq_path)
	{
		ifstream paq(paq_path, ios::binary);
		if (!paq) throw my_exception(string("Can't open ") + paq_path);

		unsigned int file_size = boost::filesystem::file_size(paq_path);
		if (file_size < 5) throw my_exception(string(paq_path) + " doesn't seem to be a valid PAQ archive");
		vector<char> v(file_size);
		paq.read(&v[0], file_size);
		paq.close();

		if (*reinterpret_cast<unsigned int*>(&v[0]) != 0x5141504E) throw my_exception("No valid signature found in PAQ file");

		process(v);
	}

	void extract_to(const char* path) { list<index_entry>::iterator ib = index.begin(); vector<char>::iterator fb = files.begin(); extract_r(string(path), ib, fb); }

	void print_index() { list<index_entry>::iterator b = index.begin(); print_r(string(), b); }
};

class paq_builder
{
private:
	list<index_entry> index;
	list<vector<char> > files;

	void process(string path)
	{
		boost::filesystem::directory_iterator end;
		for (boost::filesystem::directory_iterator itr(path.c_str()); itr != end ; ++itr)
		{
			index.push_back(index_entry(itr->filename(), boost::filesystem::is_directory(itr->status())));
			if (index.back().is_dir)
				process(path + DIR_SEPARATOR + itr->filename());
			else
			{
				cout << "Adding " << path << DIR_SEPARATOR << itr->filename() << endl;
				ifstream in(itr->string().c_str(), ios::binary);
				if (!in) throw my_exception(string("Can't open ") + itr->string());
				vector<char> v(4);
				*reinterpret_cast<unsigned int*>(&v[0]) = boost::filesystem::file_size(itr->path());
				boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
				out.push(boost::iostreams::gzip_compressor());
				out.push(boost::iostreams::back_inserter(v));
				boost::iostreams::copy(in, out);
				files.push_back(v);
			}
		}
		index.push_back(index_entry(string(""), 0));
	}

public:
	paq_builder(const char* path)
	{
		if (!boost::filesystem::exists(path) || !boost::filesystem::is_directory(path))
			throw my_exception(string(path) + " doesn't exist or isn't a directory!");

		process(string(path));
	}

	void write_to_file(const char* path)
	{
		ofstream paq(path, ios::binary);
		if (!paq) throw my_exception(string("Can't open ") + path);

		paq.write("NPAQ", 4);

		list<vector<char> >::iterator f_it = files.begin();
		for (list<index_entry>::iterator i_it = index.begin(); i_it != index.end(); i_it++)
		{
			paq << static_cast<char>(i_it->file_name.size());
			if (i_it->file_name.size())
			{
				paq.write(i_it->file_name.c_str(), i_it->file_name.size());
				unsigned int file_size = i_it->is_dir ? 0 : f_it++->size();
				paq.write(reinterpret_cast<char*>(&file_size), 4);
				paq << static_cast<char>(file_size ? 0 : 1);
			}
		}
		
		for(f_it = files.begin(); f_it != files.end(); f_it++)
			paq.write(&(*f_it)[0], f_it->size());
	}
};

void process_command_line(int argc, char** argv)
{
	if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "/?"))
	{
		cout << "PAQ archiving utility, version 1.0 (" << __DATE__ << ")" << endl;
		cout << "Copyright (C) 2009 Jes, http://www.bessab.com" << endl << endl;
		cout << "Usage: " << argv[0] << " [action] [paq file] {in/out directory}" << endl << endl;
		cout << "  The following actions are supported:" << endl << endl;
		cout << "  -l list files in a PAQ archive" << endl;
		cout << "  -x extract PAQ archive to the specified directory" << endl;
		cout << "  -c create PAQ archive of an entire directory (including its sub-directories)" << endl;
		return;
	}
	
	if (!strcmp(argv[1], "-c") && argc > 3)
	{
			paq_builder(argv[3]).write_to_file(argv[2]);
			return;
	}

	if (!strcmp(argv[1], "-x") && argc > 3)
	{
			paq_extractor(argv[2]).extract_to(argv[3]);
			return;
	}

	if (!strcmp(argv[1], "-l") && argc > 2)
	{
			paq_extractor(argv[2]).print_index();
			return;
	}
	
	throw my_exception("Syntax error in the command line, use --help option for any help");
}

int main(int argc, char** argv)
{
	try
	{
		process_command_line(argc, argv);
	}
	catch (exception& e)
	{
		cerr << e.what() << endl;
		return -1;
	}

	return 0;
}