aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/Pl_Flate.cc
blob: 627127717d5244c1de1933d5b4393f889a2eabca (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <qpdf/Pl_Flate.hh>

#include <qpdf/QUtil.hh>

Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
		   action_e action, int out_bufsize) :
    Pipeline(identifier, next),
    out_bufsize(out_bufsize),
    action(action),
    initialized(false)
{
    this->outbuf = new unsigned char[out_bufsize];

    zstream.zalloc = (alloc_func)0;
    zstream.zfree = (free_func)0;
    zstream.opaque = (voidpf)0;
    zstream.next_in = 0;
    zstream.avail_in = 0;
    zstream.next_out = this->outbuf;
    zstream.avail_out = out_bufsize;
}

Pl_Flate::~Pl_Flate()
{
    if (this->outbuf)
    {
	delete [] this->outbuf;
	this->outbuf = 0;
    }
}

void
Pl_Flate::write(unsigned char* data, int len)
{
    if (this->outbuf == 0)
    {
	throw std::logic_error(
	    this->identifier +
	    ": Pl_Flate: write() called after finish() called");
    }
    handleData(data, len, Z_NO_FLUSH);
}

void
Pl_Flate::handleData(unsigned char* data, int len, int flush)
{
    this->zstream.next_in = data;
    this->zstream.avail_in = len;

    if (! this->initialized)
    {
	int err = Z_OK;
	if (this->action == a_deflate)
	{
	    err = deflateInit(&this->zstream, Z_DEFAULT_COMPRESSION);
	}
	else
	{
	    err = inflateInit(&this->zstream);
	}
	checkError("Init", err);
	this->initialized = true;
    }

    int err = Z_OK;

    bool done = false;
    while (! done)
    {
	if (action == a_deflate)
	{
	    err = deflate(&this->zstream, flush);
	}
	else
	{
	    err = inflate(&this->zstream, flush);
	}
	switch (err)
	{
	  case Z_BUF_ERROR:
	    // Probably shouldn't be able to happen, but possible as a
	    // boundary condition: if the last call to inflate exactly
	    // filled the output buffer, it's possible that the next
	    // call to inflate could have nothing to do.
	    done = true;
	    break;

	  case Z_STREAM_END:
	    done = true;
	    // fall through

	  case Z_OK:
	    {
		if ((this->zstream.avail_in == 0) &&
		    (this->zstream.avail_out > 0))
		{
		    // There is nothing left to read, and there was
		    // sufficient buffer space to write everything we
		    // needed, so we're done for now.
		    done = true;
		}
		uLong ready = (this->out_bufsize - this->zstream.avail_out);
		if (ready > 0)
		{
		    this->getNext()->write(this->outbuf, ready);
		    this->zstream.next_out = this->outbuf;
		    this->zstream.avail_out = this->out_bufsize;
		}
	    }
	    break;

	  default:
	    this->checkError("data", err);
	    break;
	}
    }
}

void
Pl_Flate::finish()
{
    if (this->outbuf)
    {
	if (this->initialized)
	{
	    unsigned char buf[1];
	    buf[0] = '\0';
	    handleData(buf, 0, Z_FINISH);
	    int err = Z_OK;
	    if (action == a_deflate)
	    {
		err = deflateEnd(&this->zstream);
	    }
	    else
	    {
		err = inflateEnd(&this->zstream);
	    }
	    checkError("End", err);
	}

	delete [] this->outbuf;
	this->outbuf = 0;
    }
    this->getNext()->finish();
}

void
Pl_Flate::checkError(char const* prefix, int error_code)
{
    if (error_code != Z_OK)
    {
	char const* action_str = (action == a_deflate ? "deflate" : "inflate");
	std::string msg =
	    this->identifier + ": " + action_str + ": " + prefix + ": ";

	if (this->zstream.msg)
	{
	    msg += this->zstream.msg;
	}
	else
	{
	    switch (error_code)
	    {
	      case Z_ERRNO:
		msg += "zlib system error";
		break;

	      case Z_STREAM_ERROR:
		msg += "zlib stream error";
		break;

	      case Z_DATA_ERROR:
		msg += "zlib data error";
		break;

	      case Z_MEM_ERROR:
		msg += "zlib memory error";
		break;

	      case Z_BUF_ERROR:
		msg += "zlib buffer error";
		break;

	      case Z_VERSION_ERROR:
		msg += "zlib version error";
		break;

	      default:
		msg += std::string("zlib unknown error (") +
		    QUtil::int_to_string(error_code) + ")";
		break;
	    }
	}

	throw std::runtime_error(msg);
    }
}