Code

723d95aa5963f270d713003169f58d4128113bfb
[inkscape.git] / ziptool.h
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 \r
36 #include <vector>\r
37 #include <string>\r
38 \r
39 \r
40 //########################################################################\r
41 //#  A D L E R  3 2\r
42 //########################################################################\r
43 \r
44 class Adler32\r
45 {\r
46 public:\r
47 \r
48     Adler32();\r
49 \r
50     virtual ~Adler32();\r
51 \r
52     void reset();\r
53 \r
54     void update(unsigned char b);\r
55 \r
56     void update(char *str);\r
57 \r
58     unsigned long getValue();\r
59 \r
60 private:\r
61 \r
62     unsigned long value;\r
63 \r
64 };\r
65 \r
66 \r
67 //########################################################################\r
68 //#  C R C  3 2\r
69 //########################################################################\r
70 \r
71 class Crc32\r
72 {\r
73 public:\r
74 \r
75     Crc32();\r
76 \r
77     virtual ~Crc32();\r
78 \r
79     void reset();\r
80 \r
81     void update(unsigned char b);\r
82 \r
83     void update(char *str);\r
84 \r
85     void update(const std::vector<unsigned char> &buf);\r
86 \r
87     unsigned long getValue();\r
88 \r
89 private:\r
90 \r
91     unsigned long value;\r
92 \r
93 };\r
94 \r
95 \r
96 \r
97 \r
98 \r
99 \r
100 //########################################################################\r
101 //#  G Z I P    S T R E A M S\r
102 //########################################################################\r
103 \r
104 class GzipFile\r
105 {\r
106 public:\r
107 \r
108     /**\r
109      *\r
110      */\r
111     GzipFile();\r
112 \r
113     /**\r
114      *\r
115      */\r
116     virtual ~GzipFile();\r
117 \r
118     /**\r
119      *\r
120      */\r
121     virtual void put(unsigned char ch);\r
122 \r
123     /**\r
124      *\r
125      */\r
126     virtual void setData(const std::vector<unsigned char> &str);\r
127 \r
128     /**\r
129      *\r
130      */\r
131     virtual void clearData();\r
132 \r
133     /**\r
134      *\r
135      */\r
136     virtual std::vector<unsigned char> &getData();\r
137 \r
138     /**\r
139      *\r
140      */\r
141     virtual std::string &getFileName();\r
142 \r
143     /**\r
144      *\r
145      */\r
146     virtual void setFileName(const std::string &val);\r
147 \r
148 \r
149     //######################\r
150     //# U T I L I T Y\r
151     //######################\r
152 \r
153     /**\r
154      *\r
155      */\r
156     virtual bool readFile(const std::string &fName);\r
157 \r
158     //######################\r
159     //# W R I T E\r
160     //######################\r
161 \r
162     /**\r
163      *\r
164      */\r
165     virtual bool write();\r
166 \r
167     /**\r
168      *\r
169      */\r
170     virtual bool writeBuffer(std::vector<unsigned char> &outbuf);\r
171 \r
172     /**\r
173      *\r
174      */\r
175     virtual bool writeFile(const std::string &fileName);\r
176 \r
177 \r
178     //######################\r
179     //# R E A D\r
180     //######################\r
181 \r
182 \r
183     /**\r
184      *\r
185      */\r
186     virtual bool read();\r
187 \r
188     /**\r
189      *\r
190      */\r
191     virtual bool readBuffer(const std::vector<unsigned char> &inbuf);\r
192 \r
193     /**\r
194      *\r
195      */\r
196     virtual bool loadFile(const std::string &fileName);\r
197 \r
198 \r
199 \r
200 private:\r
201 \r
202     std::vector<unsigned char> data;\r
203     std::string fileName;\r
204 \r
205     //debug messages\r
206     void error(char *fmt, ...);\r
207     void trace(char *fmt, ...);\r
208 \r
209     unsigned long crc;\r
210 \r
211     std::vector<unsigned char> fileBuf;\r
212     unsigned long fileBufPos;\r
213 \r
214     bool getByte(unsigned char *ch);\r
215     bool getLong(unsigned long *val);\r
216 \r
217     bool putByte(unsigned char ch);\r
218     bool putLong(unsigned long val);\r
219 };\r
220 \r
221 \r
222 \r
223 \r
224 //########################################################################\r
225 //#  Z I P    F I L E\r
226 //########################################################################\r
227 \r
228 \r
229 /**\r
230  *\r
231  */\r
232 class ZipEntry\r
233 {\r
234 public:\r
235 \r
236     /**\r
237      *\r
238      */\r
239     ZipEntry();\r
240 \r
241     /**\r
242      *\r
243      */\r
244     ZipEntry(const std::string &fileName,\r
245              const std::string &comment);\r
246 \r
247     /**\r
248      *\r
249      */\r
250     virtual ~ZipEntry();\r
251 \r
252     /**\r
253      *\r
254      */\r
255     virtual std::string getFileName();\r
256 \r
257     /**\r
258      *\r
259      */\r
260     virtual void setFileName(const std::string &val);\r
261 \r
262     /**\r
263      *\r
264      */\r
265     virtual std::string getComment();\r
266 \r
267     /**\r
268      *\r
269      */\r
270     virtual void setComment(const std::string &val);\r
271 \r
272     /**\r
273      *\r
274      */\r
275     virtual unsigned long getCompressedSize();\r
276 \r
277     /**\r
278      *\r
279      */\r
280     virtual int getCompressionMethod();\r
281 \r
282     /**\r
283      *\r
284      */\r
285     virtual void setCompressionMethod(int val);\r
286 \r
287     /**\r
288      *\r
289      */\r
290     virtual std::vector<unsigned char> &getCompressedData();\r
291 \r
292     /**\r
293      *\r
294      */\r
295     virtual void setCompressedData(const std::vector<unsigned char> &val);\r
296 \r
297     /**\r
298      *\r
299      */\r
300     virtual unsigned long getUncompressedSize();\r
301 \r
302     /**\r
303      *\r
304      */\r
305     virtual std::vector<unsigned char> &getUncompressedData();\r
306 \r
307     /**\r
308      *\r
309      */\r
310     virtual void setUncompressedData(const std::vector<unsigned char> &val);\r
311 \r
312     /**\r
313      *\r
314      */\r
315     virtual void write(unsigned char ch);\r
316 \r
317     /**\r
318      *\r
319      */\r
320     virtual void finish();\r
321 \r
322     /**\r
323      *\r
324      */\r
325     virtual unsigned long getCrc();\r
326 \r
327     /**\r
328      *\r
329      */\r
330     virtual bool readFile(const std::string &fileNameArg,\r
331                           const std::string &commentArg);\r
332 \r
333     /**\r
334      *\r
335      */\r
336     virtual void setPosition(unsigned long val);\r
337 \r
338     /**\r
339      *\r
340      */\r
341     virtual unsigned long getPosition();\r
342 \r
343 private:\r
344 \r
345     unsigned long crc;\r
346 \r
347     std::string fileName;\r
348     std::string comment;\r
349 \r
350     int compressionMethod;\r
351 \r
352     std::vector<unsigned char> compressedData;\r
353     std::vector<unsigned char> uncompressedData;\r
354 \r
355     unsigned long position;\r
356 };\r
357 \r
358 \r
359 \r
360 \r
361 \r
362 \r
363 \r
364 \r
365 \r
366 /**\r
367  * This class sits over the zlib and gzip code to\r
368  * implement a PKWare or Info-Zip .zip file reader and\r
369  * writer\r
370  */\r
371 class ZipFile\r
372 {\r
373 public:\r
374 \r
375     /**\r
376      *\r
377      */\r
378     ZipFile();\r
379 \r
380     /**\r
381      *\r
382      */\r
383     virtual ~ZipFile();\r
384 \r
385     //######################\r
386     //# V A R I A B L E S\r
387     //######################\r
388 \r
389     /**\r
390      *\r
391      */\r
392     virtual void setComment(const std::string &val);\r
393 \r
394     /**\r
395      *\r
396      */\r
397     virtual std::string getComment();\r
398 \r
399     /**\r
400      * Return the list of entries currently in this file\r
401      */\r
402     std::vector<ZipEntry *> &getEntries();\r
403 \r
404 \r
405     //######################\r
406     //# U T I L I T Y\r
407     //######################\r
408 \r
409     /**\r
410      *\r
411      */\r
412     virtual bool addFile(const std::string &fileNameArg,\r
413                          const std::string &commentArg);\r
414 \r
415     //######################\r
416     //# W R I T E\r
417     //######################\r
418 \r
419     /**\r
420      *\r
421      */\r
422     virtual bool write();\r
423 \r
424     /**\r
425      *\r
426      */\r
427     virtual bool writeBuffer(std::vector<unsigned char> &outbuf);\r
428 \r
429     /**\r
430      *\r
431      */\r
432     virtual bool writeFile(const std::string &fileName);\r
433 \r
434 \r
435     //######################\r
436     //# R E A D\r
437     //######################\r
438 \r
439 \r
440     /**\r
441      *\r
442      */\r
443     virtual bool read();\r
444 \r
445     /**\r
446      *\r
447      */\r
448     virtual bool readBuffer(const std::vector<unsigned char> &inbuf);\r
449 \r
450     /**\r
451      *\r
452      */\r
453     virtual bool readFile(const std::string &fileName);\r
454 \r
455 \r
456 private:\r
457 \r
458     //debug messages\r
459     void error(char *fmt, ...);\r
460     void trace(char *fmt, ...);\r
461 \r
462     //# Private writing methods\r
463 \r
464     /**\r
465      *\r
466      */\r
467     bool putLong(unsigned long val);\r
468 \r
469     /**\r
470      *\r
471      */\r
472     bool putInt(unsigned int val);\r
473 \r
474 \r
475     /**\r
476      *\r
477      */\r
478     bool putByte(unsigned char val);\r
479 \r
480     /**\r
481      *\r
482      */\r
483     bool writeFileData();\r
484 \r
485     /**\r
486      *\r
487      */\r
488     bool writeCentralDirectory();\r
489 \r
490 \r
491     //# Private reading methods\r
492 \r
493     /**\r
494      *\r
495      */\r
496     bool getLong(unsigned long *val);\r
497 \r
498     /**\r
499      *\r
500      */\r
501     bool getInt(unsigned int *val);\r
502 \r
503     /**\r
504      *\r
505      */\r
506     bool getByte(unsigned char *val);\r
507 \r
508     /**\r
509      *\r
510      */\r
511     bool readFileData();\r
512 \r
513     /**\r
514      *\r
515      */\r
516     bool readCentralDirectory();\r
517 \r
518 \r
519     std::vector<ZipEntry *> entries;\r
520 \r
521     std::vector<unsigned char> fileBuf;\r
522     unsigned long fileBufPos;\r
523 \r
524     std::string comment;\r
525 };\r
526 \r
527 \r
528 \r
529 \r
530 \r
531 \r
532 #endif /* __ZIPTOOL_H__ */\r
533 \r
534 \r
535 //########################################################################\r
536 //#  E N D    O F    F I L E\r
537 //########################################################################\r
538 \r