summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/compressbzip2.cpp
authorulf69 <ulf69>2004-09-15 17:53:22 (UTC)
committer ulf69 <ulf69>2004-09-15 17:53:22 (UTC)
commitd3925ba5bd25224bc4a60d3d6a107c464994a1ea (patch) (unidiff)
tree60f69da1d2b79ee3081e7ef5c09a46470ca6eda0 /pwmanager/pwmanager/compressbzip2.cpp
parentce83a3479d23b9e8a59c745ccd0a0b14f64ef4e8 (diff)
downloadkdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.zip
kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.tar.gz
kdepimpi-d3925ba5bd25224bc4a60d3d6a107c464994a1ea.tar.bz2
initial revision
Diffstat (limited to 'pwmanager/pwmanager/compressbzip2.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/compressbzip2.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/pwmanager/pwmanager/compressbzip2.cpp b/pwmanager/pwmanager/compressbzip2.cpp
new file mode 100644
index 0000000..c3a9edd
--- a/dev/null
+++ b/pwmanager/pwmanager/compressbzip2.cpp
@@ -0,0 +1,138 @@
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 "compressbzip2.h"
21
22#include <stdlib.h>
23
24#define BZ_NO_STDIO
25#include <bzlib.h>
26
27 #define BZ_BLOCK_SIZE 9
28 #define BZ_WORK_FACTOR 30
29 #define EXPAND_COMPRESS_DESTBUF_BYTES 1024
30 #define EXPAND_DECOMPRESS_DESTBUF_BYTES (1024 * 10)
31
32
33bool CompressBzip2::compress(string *d)
34{
35 int ret;
36 char *dest, *source;
37 unsigned int sourceLen, destLen;
38
39 sourceLen = d->length();
40 destLen = calcCompressDestLen(sourceLen);
41 source = static_cast<char *>(malloc(sourceLen));
42 if (!source)
43 return false;
44 memcpy(source, d->c_str(), sourceLen);
45 dest = static_cast<char *>(malloc(destLen));
46 if (!dest) {
47 free(source);
48 return false;
49 }
50 while (1) {
51 ret = BZ2_bzBuffToBuffCompress(dest,
52 &destLen,
53 source,
54 sourceLen,
55 BZ_BLOCK_SIZE,
56 0,
57 BZ_WORK_FACTOR);
58 switch (ret) {
59 case BZ_OK:
60 goto out;
61 case BZ_OUTBUFF_FULL:
62 /* we don't use realloc(), because it may
63 * (in this case) unneccessarily copy the old block
64 * to the new allocated block.
65 */
66 free(dest);
67 destLen += EXPAND_COMPRESS_DESTBUF_BYTES;
68 dest = static_cast<char *>(malloc(destLen));
69 if (!dest) {
70 free(source);
71 return false;
72 }
73 break;
74 default:
75 free(source),
76 free(dest);
77 return false;
78 }
79 }
80out:
81 free(source);
82 d->assign(dest, destLen);
83 free(dest);
84 return true;
85}
86
87bool CompressBzip2::decompress(string *d)
88{
89 int ret;
90 char *dest, *source;
91 unsigned int sourceLen, destLen;
92
93 sourceLen = d->length();
94 destLen = calcDecompressDestLen(sourceLen);
95 source = static_cast<char *>(malloc(sourceLen));
96 if (!source)
97 return false;
98 memcpy(source, d->c_str(), sourceLen);
99 dest = static_cast<char *>(malloc(destLen));
100 if (!dest) {
101 free(source);
102 return false;
103 }
104 while (1) {
105 ret = BZ2_bzBuffToBuffDecompress(dest,
106 &destLen,
107 source,
108 sourceLen,
109 0,
110 0);
111 switch (ret) {
112 case BZ_OK:
113 goto out;
114 case BZ_OUTBUFF_FULL:
115 /* we don't use realloc(), because it may
116 * (in this case) unneccessarily copy the old block
117 * to the new allocated block.
118 */
119 free(dest);
120 destLen += EXPAND_DECOMPRESS_DESTBUF_BYTES;
121 dest = static_cast<char *>(malloc(destLen));
122 if (!dest) {
123 free(source);
124 return false;
125 }
126 break;
127 default:
128 free(source),
129 free(dest);
130 return false;
131 }
132 }
133out:
134 free(source);
135 d->assign(dest, destLen);
136 free(dest);
137 return true;
138}