1 #ifndef __ZIPTOOL_H__\r
2 #define __ZIPTOOL_H__\r
3 /**\r
4 * This is intended to be a standalone, reduced capability\r
5 * implementation of Gzip and Zip functionality. Its\r
6 * targeted use case is for archiving and retrieving single files\r
7 * which use these encoding types. Being memory based and\r
8 * non-optimized, it is not useful in cases where very large\r
9 * archives are needed or where high performance is desired.\r
10 * However, it should hopefully work well for smaller,\r
11 * one-at-a-time tasks. What you get in return is the ability\r
12 * to drop these files into your project and remove the dependencies\r
13 * on ZLib and Info-Zip. Enjoy.\r
14 *\r
15 * Authors:\r
16 * Bob Jamison\r
17 *\r
18 * Copyright (C) 2006 Bob Jamison\r
19 *\r
20 * This library is free software; you can redistribute it and/or\r
21 * modify it under the terms of the GNU Lesser General Public\r
22 * License as published by the Free Software Foundation; either\r
23 * version 2.1 of the License, or (at your option) any later version.\r
24 *\r
25 * This library is distributed in the hope that it will be useful,\r
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
28 * Lesser General Public License for more details.\r
29 *\r
30 * You should have received a copy of the GNU Lesser General Public\r
31 * License along with this library; if not, write to the Free Software\r
32 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\r
33 */\r
34 \r
35 #define MAX_BITS 8\r
36 \r
37 #include <vector>\r
38 #include <string>\r
39 \r
40 \r
41 //########################################################################\r
42 //# A D L E R 3 2\r
43 //########################################################################\r
44 \r
45 class Adler32\r
46 {\r
47 public:\r
48 \r
49 Adler32();\r
50 \r
51 virtual ~Adler32();\r
52 \r
53 void reset();\r
54 \r
55 void update(unsigned char b);\r
56 \r
57 void update(char *str);\r
58 \r
59 unsigned long getValue();\r
60 \r
61 private:\r
62 \r
63 unsigned long value;\r
64 \r
65 };\r
66 \r
67 \r
68 //########################################################################\r
69 //# C R C 3 2\r
70 //########################################################################\r
71 \r
72 class Crc32\r
73 {\r
74 public:\r
75 \r
76 Crc32();\r
77 \r
78 virtual ~Crc32();\r
79 \r
80 void reset();\r
81 \r
82 void update(unsigned char b);\r
83 \r
84 void update(char *str);\r
85 \r
86 void update(const std::vector<unsigned char> &buf);\r
87 \r
88 unsigned long getValue();\r
89 \r
90 private:\r
91 \r
92 unsigned long value;\r
93 \r
94 };\r
95 \r
96 \r
97 \r
98 \r
99 \r
100 \r
101 //########################################################################\r
102 //# G Z I P S T R E A M S\r
103 //########################################################################\r
104 \r
105 class GzipFile\r
106 {\r
107 public:\r
108 \r
109 /**\r
110 *\r
111 */\r
112 GzipFile();\r
113 \r
114 /**\r
115 *\r
116 */\r
117 virtual ~GzipFile();\r
118 \r
119 /**\r
120 *\r
121 */\r
122 virtual void put(unsigned char ch);\r
123 \r
124 /**\r
125 *\r
126 */\r
127 virtual void setData(const std::vector<unsigned char> &str);\r
128 \r
129 /**\r
130 *\r
131 */\r
132 virtual void clearData();\r
133 \r
134 /**\r
135 *\r
136 */\r
137 virtual std::vector<unsigned char> &getData();\r
138 \r
139 /**\r
140 *\r
141 */\r
142 virtual std::string &getFileName();\r
143 \r
144 /**\r
145 *\r
146 */\r
147 virtual void setFileName(const std::string &val);\r
148 \r
149 \r
150 //######################\r
151 //# U T I L I T Y\r
152 //######################\r
153 \r
154 /**\r
155 *\r
156 */\r
157 virtual bool readFile(const std::string &fName);\r
158 \r
159 //######################\r
160 //# W R I T E\r
161 //######################\r
162 \r
163 /**\r
164 *\r
165 */\r
166 virtual bool write();\r
167 \r
168 /**\r
169 *\r
170 */\r
171 virtual bool writeBuffer(std::vector<unsigned char> &outbuf);\r
172 \r
173 /**\r
174 *\r
175 */\r
176 virtual bool writeFile(const std::string &fileName);\r
177 \r
178 \r
179 //######################\r
180 //# R E A D\r
181 //######################\r
182 \r
183 \r
184 /**\r
185 *\r
186 */\r
187 virtual bool read();\r
188 \r
189 /**\r
190 *\r
191 */\r
192 virtual bool readBuffer(const std::vector<unsigned char> &inbuf);\r
193 \r
194 /**\r
195 *\r
196 */\r
197 virtual bool loadFile(const std::string &fileName);\r
198 \r
199 \r
200 \r
201 private:\r
202 \r
203 std::vector<unsigned char> data;\r
204 std::string fileName;\r
205 \r
206 //debug messages\r
207 void error(char *fmt, ...);\r
208 void trace(char *fmt, ...);\r
209 \r
210 unsigned long crc;\r
211 \r
212 std::vector<unsigned char> fileBuf;\r
213 unsigned long fileBufPos;\r
214 \r
215 bool getByte(unsigned char *ch);\r
216 bool getLong(unsigned long *val);\r
217 \r
218 bool putByte(unsigned char ch);\r
219 bool putLong(unsigned long val);\r
220 };\r
221 \r
222 \r
223 \r
224 \r
225 //########################################################################\r
226 //# Z I P F I L E\r
227 //########################################################################\r
228 \r
229 \r
230 /**\r
231 *\r
232 */\r
233 class ZipEntry\r
234 {\r
235 public:\r
236 \r
237 /**\r
238 *\r
239 */\r
240 ZipEntry();\r
241 \r
242 /**\r
243 *\r
244 */\r
245 ZipEntry(const std::string &fileName,\r
246 const std::string &comment);\r
247 \r
248 /**\r
249 *\r
250 */\r
251 virtual ~ZipEntry();\r
252 \r
253 /**\r
254 *\r
255 */\r
256 virtual std::string getFileName();\r
257 \r
258 /**\r
259 *\r
260 */\r
261 virtual void setFileName(const std::string &val);\r
262 \r
263 /**\r
264 *\r
265 */\r
266 virtual std::string getComment();\r
267 \r
268 /**\r
269 *\r
270 */\r
271 virtual void setComment(const std::string &val);\r
272 \r
273 /**\r
274 *\r
275 */\r
276 virtual unsigned long getCompressedSize();\r
277 \r
278 /**\r
279 *\r
280 */\r
281 virtual int getCompressionMethod();\r
282 \r
283 /**\r
284 *\r
285 */\r
286 virtual void setCompressionMethod(int val);\r
287 \r
288 /**\r
289 *\r
290 */\r
291 virtual std::vector<unsigned char> &getCompressedData();\r
292 \r
293 /**\r
294 *\r
295 */\r
296 virtual void setCompressedData(const std::vector<unsigned char> &val);\r
297 \r
298 /**\r
299 *\r
300 */\r
301 virtual unsigned long getUncompressedSize();\r
302 \r
303 /**\r
304 *\r
305 */\r
306 virtual std::vector<unsigned char> &getUncompressedData();\r
307 \r
308 /**\r
309 *\r
310 */\r
311 virtual void setUncompressedData(const std::vector<unsigned char> &val);\r
312 \r
313 /**\r
314 *\r
315 */\r
316 virtual void write(unsigned char ch);\r
317 \r
318 /**\r
319 *\r
320 */\r
321 virtual void finish();\r
322 \r
323 /**\r
324 *\r
325 */\r
326 virtual unsigned long getCrc();\r
327 \r
328 /**\r
329 *\r
330 */\r
331 virtual bool readFile(const std::string &fileNameArg,\r
332 const std::string &commentArg);\r
333 \r
334 /**\r
335 *\r
336 */\r
337 virtual void setPosition(unsigned long val);\r
338 \r
339 /**\r
340 *\r
341 */\r
342 virtual unsigned long getPosition();\r
343 \r
344 private:\r
345 \r
346 unsigned long crc;\r
347 \r
348 std::string fileName;\r
349 std::string comment;\r
350 \r
351 int compressionMethod;\r
352 \r
353 std::vector<unsigned char> compressedData;\r
354 std::vector<unsigned char> uncompressedData;\r
355 \r
356 unsigned long position;\r
357 };\r
358 \r
359 \r
360 \r
361 \r
362 \r
363 \r
364 \r
365 \r
366 \r
367 /**\r
368 * This class sits over the zlib and gzip code to\r
369 * implement a PKWare or Info-Zip .zip file reader and\r
370 * writer\r
371 */\r
372 class ZipFile\r
373 {\r
374 public:\r
375 \r
376 /**\r
377 *\r
378 */\r
379 ZipFile();\r
380 \r
381 /**\r
382 *\r
383 */\r
384 virtual ~ZipFile();\r
385 \r
386 //######################\r
387 //# V A R I A B L E S\r
388 //######################\r
389 \r
390 /**\r
391 *\r
392 */\r
393 virtual void setComment(const std::string &val);\r
394 \r
395 /**\r
396 *\r
397 */\r
398 virtual std::string getComment();\r
399 \r
400 /**\r
401 * Return the list of entries currently in this file\r
402 */\r
403 std::vector<ZipEntry *> &getEntries();\r
404 \r
405 \r
406 //######################\r
407 //# U T I L I T Y\r
408 //######################\r
409 \r
410 /**\r
411 *\r
412 */\r
413 virtual bool addFile(const std::string &fileNameArg,\r
414 const std::string &commentArg);\r
415 \r
416 //######################\r
417 //# W R I T E\r
418 //######################\r
419 \r
420 /**\r
421 *\r
422 */\r
423 virtual bool write();\r
424 \r
425 /**\r
426 *\r
427 */\r
428 virtual bool writeBuffer(std::vector<unsigned char> &outbuf);\r
429 \r
430 /**\r
431 *\r
432 */\r
433 virtual bool writeFile(const std::string &fileName);\r
434 \r
435 \r
436 //######################\r
437 //# R E A D\r
438 //######################\r
439 \r
440 \r
441 /**\r
442 *\r
443 */\r
444 virtual bool read();\r
445 \r
446 /**\r
447 *\r
448 */\r
449 virtual bool readBuffer(const std::vector<unsigned char> &inbuf);\r
450 \r
451 /**\r
452 *\r
453 */\r
454 virtual bool readFile(const std::string &fileName);\r
455 \r
456 \r
457 private:\r
458 \r
459 //debug messages\r
460 void error(char *fmt, ...);\r
461 void trace(char *fmt, ...);\r
462 \r
463 //# Private writing methods\r
464 \r
465 /**\r
466 *\r
467 */\r
468 bool putLong(unsigned long val);\r
469 \r
470 /**\r
471 *\r
472 */\r
473 bool putInt(unsigned int val);\r
474 \r
475 \r
476 /**\r
477 *\r
478 */\r
479 bool putByte(unsigned char val);\r
480 \r
481 /**\r
482 *\r
483 */\r
484 bool writeFileData();\r
485 \r
486 /**\r
487 *\r
488 */\r
489 bool writeCentralDirectory();\r
490 \r
491 \r
492 //# Private reading methods\r
493 \r
494 /**\r
495 *\r
496 */\r
497 bool getLong(unsigned long *val);\r
498 \r
499 /**\r
500 *\r
501 */\r
502 bool getInt(unsigned int *val);\r
503 \r
504 /**\r
505 *\r
506 */\r
507 bool getByte(unsigned char *val);\r
508 \r
509 /**\r
510 *\r
511 */\r
512 bool readFileData();\r
513 \r
514 /**\r
515 *\r
516 */\r
517 bool readCentralDirectory();\r
518 \r
519 \r
520 std::vector<ZipEntry *> entries;\r
521 \r
522 std::vector<unsigned char> fileBuf;\r
523 unsigned long fileBufPos;\r
524 \r
525 std::string comment;\r
526 };\r
527 \r
528 \r
529 \r
530 \r
531 \r
532 \r
533 #endif /* __ZIPTOOL_H__ */\r
534 \r
535 \r
536 //########################################################################\r
537 //# E N D O F F I L E\r
538 //########################################################################\r
539 \r