File: gzipcompressstream.cpp

package info (click to toggle)
strigi 0.7.8-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,556 kB
  • ctags: 6,064
  • sloc: cpp: 41,963; ansic: 1,199; perl: 483; java: 367; python: 345; xml: 177; sh: 150; makefile: 27
file content (135 lines) | stat: -rw-r--r-- 4,148 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* This file is part of Strigi Desktop Search
 *
 * Copyright (C) 2006 Ben van Klinken <bvklinken@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "gzipcompressstream.h"
#include <zlib.h>
#include <iostream>

using namespace Strigi;
using namespace std;

GZipCompressInputStream::GZipCompressInputStream(InputStream* input, int level) {
    // initialize values that signal state
    m_status = Ok;
    zstream = 0;
    if ( level < 0 || level > 9 ) {
        level = Z_DEFAULT_COMPRESSION;
    }

    this->input = input;

    // initialize the z_stream
    zstream = (z_stream_s*)malloc(sizeof(z_stream_s));
    zstream->zalloc = Z_NULL;
    zstream->zfree = Z_NULL;
    zstream->opaque = Z_NULL;
    zstream->avail_in = 0;

    // initialize for writing gzip streams
    int r = deflateInit(zstream, level);
    if (r != Z_OK) {
        m_error = "Error initializing GZipCompressInputStream.";
        dealloc();
        m_status = Error;
        return;
    }

    // signal that we need to read into the buffer
    zstream->avail_out = 1;
}
GZipCompressInputStream::~GZipCompressInputStream() {
    dealloc();
}
void
GZipCompressInputStream::dealloc() {
    if (zstream) {
        deflateEnd(zstream);
        free(zstream);
        zstream = 0;
    }
}
void
GZipCompressInputStream::readFromStream() {
    // read data from the input stream
    const char* inStart;
    int32_t nread;
    nread = input->read(inStart, 1, 0);
    if (nread < -1) {
        m_status = Error;
        m_error = input->error();
    } else if (nread < 1) {
        zstream->avail_in = 0; //bail...
    } else {
        zstream->next_in = (Bytef*)inStart;
        zstream->avail_in = nread;
    }
}
int32_t
GZipCompressInputStream::fillBuffer(char* start, int32_t space) {
    cerr << "GZCI " << this << " " << zstream << endl;
    if (zstream == 0) return -1;

    // make sure we can write into the buffer
    zstream->avail_out = space;
    zstream->next_out = (Bytef*)start;

    int r;
    // make sure there is data to decompress
    if (zstream->avail_in == 0) {
        readFromStream();
        if (m_status == Error) {
            cerr << "error " << endl;
            // no data was read
            return -1;
        } else if (zstream->avail_in == 0) {
            r = deflate(zstream, Z_FINISH);
            int32_t nwritten = space - zstream->avail_out;
            cerr << "GZCI end " << this << " " << nwritten << " " << m_status
<< endl;
            if (r != Z_OK) {
                cerr << "GZCI streamend " << r << endl;
                dealloc();
                if (r != Z_STREAM_END) {
                    fprintf(stderr, "deflate should report Z_STREAM_END\n");
                    return -1;
                }
            }
            return nwritten;
        }
    }
    r = deflate(zstream, Z_NO_FLUSH);
    // inform the buffer of the number of bytes that was read
    int32_t nwritten = space - zstream->avail_out;
    switch (r) {
    case Z_NEED_DICT:
        m_error = "Z_NEED_DICT while inflating stream.";
        m_status = Error;
        break;
    case Z_DATA_ERROR:
        m_error = "Z_DATA_ERROR while inflating stream.";
        m_status = Error;
        break;
    case Z_MEM_ERROR:
        m_error = "Z_MEM_ERROR while inflating stream.";
        m_status = Error;
        break;
    }
    cerr << "GZCI more " << this << " " << nwritten << endl;
    return nwritten;
}