Diffstat (limited to 'pwmanager/pwmanager/compressgzip.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | pwmanager/pwmanager/compressgzip.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/compressgzip.cpp b/pwmanager/pwmanager/compressgzip.cpp new file mode 100644 index 0000000..0a325c6 --- a/dev/null +++ b/pwmanager/pwmanager/compressgzip.cpp | |||
@@ -0,0 +1,115 @@ | |||
1 | /*************************************************************************** | ||
2 | * * | ||
3 | * copyright (C) 2003, 2004 by Michael Buesch * | ||
4 | * email: mbuesch@freenet.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License version 2 * | ||
8 | * as published by the Free Software Foundation. * | ||
9 | * * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | /*************************************************************************** | ||
13 | * copyright (C) 2004 by Ulf Schenk | ||
14 | * This file is originaly based on version 1.0.1 of pwmanager | ||
15 | * and was modified to run on embedded devices that run microkde | ||
16 | * | ||
17 | * $Id$ | ||
18 | **************************************************************************/ | ||
19 | |||
20 | #include "compressgzip.h" | ||
21 | |||
22 | #include <stdlib.h> | ||
23 | #include <zlib.h> | ||
24 | |||
25 | #define COMPRESSION_LEVEL Z_BEST_COMPRESSION | ||
26 | #define EXPAND_COMPRESS_DESTBUF_BYTES 1024 | ||
27 | #define EXPAND_DECOMPRESS_DESTBUF_BYTES (1024 * 10) | ||
28 | |||
29 | |||
30 | bool CompressGzip::compress(string *d) | ||
31 | { | ||
32 | int ret; | ||
33 | Bytef *dest; | ||
34 | const Bytef *source; | ||
35 | unsigned long sourceLen, destLen; | ||
36 | |||
37 | source = reinterpret_cast<const Bytef *>(d->c_str()); | ||
38 | sourceLen = d->length(); | ||
39 | destLen = calcCompressDestLen(sourceLen); | ||
40 | dest = static_cast<Bytef *>(malloc(destLen)); | ||
41 | if (!dest) | ||
42 | return false; | ||
43 | while (1) { | ||
44 | ret = compress2(dest, | ||
45 | static_cast<uLongf *>(&destLen), | ||
46 | source, | ||
47 | static_cast<uLong>(sourceLen), | ||
48 | COMPRESSION_LEVEL); | ||
49 | switch (ret) { | ||
50 | case Z_OK: | ||
51 | goto out; | ||
52 | case Z_BUF_ERROR: | ||
53 | /* we don't use realloc(), because it may | ||
54 | * (in this case) unneccessarily copy the old block | ||
55 | * to the new allocated block. | ||
56 | */ | ||
57 | free(dest); | ||
58 | destLen += EXPAND_COMPRESS_DESTBUF_BYTES; | ||
59 | dest = static_cast<Bytef *>(malloc(destLen)); | ||
60 | if (!dest) | ||
61 | return false; | ||
62 | break; | ||
63 | default: | ||
64 | free(dest); | ||
65 | return false; | ||
66 | } | ||
67 | } | ||
68 | out: | ||
69 | d->assign(reinterpret_cast<char *>(dest), destLen); | ||
70 | free(dest); | ||
71 | return true; | ||
72 | } | ||
73 | |||
74 | bool CompressGzip::decompress(string *d) | ||
75 | { | ||
76 | int ret; | ||
77 | Bytef *dest; | ||
78 | const Bytef *source; | ||
79 | unsigned long sourceLen, destLen; | ||
80 | |||
81 | source = reinterpret_cast<const Bytef *>(d->c_str()); | ||
82 | sourceLen = d->length(); | ||
83 | destLen = calcDecompressDestLen(sourceLen); | ||
84 | dest = static_cast<Bytef *>(malloc(destLen)); | ||
85 | if (!dest) | ||
86 | return false; | ||
87 | while (1) { | ||
88 | ret = uncompress(dest, | ||
89 | static_cast<uLongf *>(&destLen), | ||
90 | source, | ||
91 | static_cast<uLong>(sourceLen)); | ||
92 | switch (ret) { | ||
93 | case Z_OK: | ||
94 | goto out; | ||
95 | case Z_BUF_ERROR: | ||
96 | /* we don't use realloc(), because it may | ||
97 | * (in this case) unneccessarily copy the old block | ||
98 | * to the new allocated block. | ||
99 | */ | ||
100 | free(dest); | ||
101 | destLen += EXPAND_DECOMPRESS_DESTBUF_BYTES; | ||
102 | dest = static_cast<Bytef *>(malloc(destLen)); | ||
103 | if (!dest) | ||
104 | return false; | ||
105 | break; | ||
106 | default: | ||
107 | free(dest); | ||
108 | return false; | ||
109 | } | ||
110 | } | ||
111 | out: | ||
112 | d->assign(reinterpret_cast<char *>(dest), destLen); | ||
113 | free(dest); | ||
114 | return true; | ||
115 | } | ||