~ubuntu-branches/debian/squeeze/sword/squeeze

« back to all changes in this revision

Viewing changes to src/modules/common/zipcomprs.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Glassey
  • Date: 2004-01-15 15:50:07 UTC
  • Revision ID: james.westby@ubuntu.com-20040115155007-n9mz4x0zxrs1isd3
Tags: upstream-1.5.7
ImportĀ upstreamĀ versionĀ 1.5.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *  swcomprs.cpp   - code for class 'ZipCompress'- a driver class that provides
 
3
 *                              compression utilities. - using zlib
 
4
 */
 
5
 
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
#include <stdio.h>
 
9
#include <zipcomprs.h>
 
10
#include <zlib.h>
 
11
 
 
12
SWORD_NAMESPACE_START
 
13
 
 
14
/******************************************************************************
 
15
 * ZipCompress Constructor - Initializes data for instance of ZipCompress
 
16
 *
 
17
 */
 
18
 
 
19
ZipCompress::ZipCompress() : SWCompress()
 
20
{
 
21
//      fprintf(stderr, "init compress\n");
 
22
}
 
23
 
 
24
 
 
25
/******************************************************************************
 
26
 * ZipCompress Destructor - Cleans up instance of ZipCompress
 
27
 */
 
28
 
 
29
ZipCompress::~ZipCompress() {
 
30
}
 
31
 
 
32
 
 
33
/******************************************************************************
 
34
 * ZipCompress::Encode  - This function "encodes" the input stream into the
 
35
 *                                              output stream.
 
36
 *                                              The GetChars() and SendChars() functions are
 
37
 *                                              used to separate this method from the actual
 
38
 *                                              i/o.
 
39
 *              NOTE:                   must set zlen for parent class to know length of
 
40
 *                                              compressed buffer.
 
41
 */
 
42
 
 
43
void ZipCompress::Encode(void)
 
44
{
 
45
/*
 
46
ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
 
47
                                 const Bytef *source, uLong sourceLen));
 
48
     Compresses the source buffer into the destination buffer.  sourceLen is
 
49
   the byte length of the source buffer. Upon entry, destLen is the total
 
50
   size of the destination buffer, which must be at least 0.1% larger than
 
51
   sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
 
52
   compressed buffer.
 
53
     This function can be used to compress a whole file at once if the
 
54
   input file is mmap'ed.
 
55
     compress returns Z_OK if success, Z_MEM_ERROR if there was not
 
56
   enough memory, Z_BUF_ERROR if there was not enough room in the output
 
57
   buffer.
 
58
*/
 
59
        direct = 0;     // set direction needed by parent [Get|Send]Chars()
 
60
 
 
61
        // get buffer
 
62
        char chunk[1024];
 
63
        char *buf = (char *)calloc(1, 1024);
 
64
        char *chunkbuf = buf;
 
65
        unsigned long chunklen;
 
66
        unsigned long len = 0;
 
67
        while((chunklen = GetChars(chunk, 1023))) {
 
68
                memcpy(chunkbuf, chunk, chunklen);
 
69
                len += chunklen;
 
70
                if (chunklen < 1023)
 
71
                        break;
 
72
                else    buf = (char *)realloc(buf, len + 1024);
 
73
                chunkbuf = buf+len;
 
74
        }
 
75
 
 
76
 
 
77
        zlen = (long) (len*1.001)+15;
 
78
        char *zbuf = new char[zlen+1];
 
79
        if (len)
 
80
        {
 
81
                //printf("Doing compress\n");
 
82
                if (compress((Bytef*)zbuf, &zlen, (const Bytef*)buf, len)!=Z_OK)
 
83
                {
 
84
                        printf("ERROR in compression\n");
 
85
                }
 
86
                else {
 
87
                        SendChars(zbuf, zlen);
 
88
                }
 
89
        }
 
90
        else
 
91
        {
 
92
                fprintf(stderr, "No buffer to compress\n");
 
93
        }
 
94
        delete [] zbuf;
 
95
        free (buf);
 
96
}
 
97
 
 
98
 
 
99
/******************************************************************************
 
100
 * ZipCompress::Decode  - This function "decodes" the input stream into the
 
101
 *                                              output stream.
 
102
 *                                              The GetChars() and SendChars() functions are
 
103
 *                                              used to separate this method from the actual
 
104
 *                                              i/o.
 
105
 */
 
106
 
 
107
void ZipCompress::Decode(void)
 
108
{
 
109
/*
 
110
ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
 
111
                                   const Bytef *source, uLong sourceLen));
 
112
     Decompresses the source buffer into the destination buffer.  sourceLen is
 
113
   the byte length of the source buffer. Upon entry, destLen is the total
 
114
   size of the destination buffer, which must be large enough to hold the
 
115
   entire uncompressed data. (The size of the uncompressed data must have
 
116
   been saved previously by the compressor and transmitted to the decompressor
 
117
   by some mechanism outside the scope of this compression library.)
 
118
   Upon exit, destLen is the actual size of the compressed buffer.
 
119
     This function can be used to decompress a whole file at once if the
 
120
   input file is mmap'ed.
 
121
 
 
122
     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
 
123
   enough memory, Z_BUF_ERROR if there was not enough room in the output
 
124
   buffer, or Z_DATA_ERROR if the input data was corrupted.
 
125
*/
 
126
 
 
127
        // get buffer
 
128
        char chunk[1024];
 
129
        char *zbuf = (char *)calloc(1, 1024);
 
130
        char *chunkbuf = zbuf;
 
131
        int chunklen;
 
132
        unsigned long zlen = 0;
 
133
        while((chunklen = GetChars(chunk, 1023))) {
 
134
                memcpy(chunkbuf, chunk, chunklen);
 
135
                zlen += chunklen;
 
136
                if (chunklen < 1023)
 
137
                        break;
 
138
                else    zbuf = (char *)realloc(zbuf, zlen + 1024);
 
139
                chunkbuf = zbuf + zlen;
 
140
        }
 
141
 
 
142
        //printf("Decoding complength{%ld} uncomp{%ld}\n", zlen, blen);
 
143
        if (zlen) {
 
144
                unsigned long blen = zlen*20;   // trust compression is less than 1000%
 
145
                char *buf = new char[blen]; 
 
146
                //printf("Doing decompress {%s}\n", zbuf);
 
147
                if (uncompress((Bytef*)buf, &blen, (Bytef*)zbuf, zlen) != Z_OK) {
 
148
                        fprintf(stderr, "no room in outbuffer to during decompression. see zipcomp.cpp\n");
 
149
                }
 
150
                SendChars(buf, blen);
 
151
                delete [] buf;
 
152
                slen = blen;
 
153
        }
 
154
        else {
 
155
                fprintf(stderr, "No buffer to decompress!\n");
 
156
        }
 
157
        //printf("Finished decoding\n");
 
158
        free (zbuf);
 
159
}
 
160
 
 
161
SWORD_NAMESPACE_END