Change the translation of the "Help" menu item to "?", so that the menu can be displa...
[reactos.git] / rosapps / smartpdf / poppler / poppler / FlateStream.cc
1 //========================================================================
2 //
3 // FlateStream.cc
4 //
5 // Copyright (C) 2005, Jeff Muizelaar
6 //
7 //========================================================================
8 #include "FlateStream.h"
9 FlateStream::FlateStream(Stream *strA, int predictor, int columns, int colors, int bits) :
10 FilterStream(strA)
11 {
12 if (predictor != 1) {
13 pred = new StreamPredictor(this, predictor, columns, colors, bits);
14 } else {
15 pred = NULL;
16 }
17 out_pos = 0;
18 memset(&d_stream, 0, sizeof(d_stream));
19 inflateInit(&d_stream);
20 }
21
22 FlateStream::~FlateStream() {
23 inflateEnd(&d_stream);
24 delete pred;
25 delete str;
26 }
27
28 void FlateStream::reset() {
29 //FIXME: what are the semantics of reset?
30 //i.e. how much intialization has to happen in the constructor?
31
32 /* reinitialize zlib */
33 inflateEnd(&d_stream);
34 memset(&d_stream, 0, sizeof(d_stream));
35 inflateInit(&d_stream);
36
37 str->reset();
38 d_stream.avail_in = 0;
39 status = Z_OK;
40 out_pos = 0;
41 out_buf_len = 0;
42 }
43
44 int FlateStream::getChar() {
45 if (pred)
46 return pred->getChar();
47 else
48 return getRawChar();
49 }
50
51 int FlateStream::lookChar() {
52 if (pred)
53 return pred->lookChar();
54
55 if (fill_buffer())
56 return EOF;
57
58 return out_buf[out_pos];
59 }
60
61 int FlateStream::fill_buffer() {
62 /* only fill the buffer if it has all been used */
63 if (out_pos >= out_buf_len) {
64 /* check if the flatestream has been exhausted */
65 if (status == Z_STREAM_END) {
66 return -1;
67 }
68
69 /* set to the begining of out_buf */
70 d_stream.avail_out = sizeof(out_buf);
71 d_stream.next_out = out_buf;
72 out_pos = 0;
73
74 while (1) {
75 /* buffer is empty so we need to fill it */
76 if (d_stream.avail_in == 0) {
77 int c;
78 /* read from the source stream */
79 while (d_stream.avail_in < sizeof(in_buf) && (c = str->getChar()) != EOF) {
80 in_buf[d_stream.avail_in++] = c;
81 }
82 d_stream.next_in = in_buf;
83 }
84
85 /* keep decompressing until we can't anymore */
86 if (d_stream.avail_out == 0 || d_stream.avail_in == 0 || (status != Z_OK && status != Z_BUF_ERROR))
87 break;
88 status = inflate(&d_stream, Z_SYNC_FLUSH);
89 }
90
91 out_buf_len = sizeof(out_buf) - d_stream.avail_out;
92 if (status != Z_OK && status != Z_STREAM_END)
93 return -1;
94 if (!out_buf_len)
95 return -1;
96 }
97
98 return 0;
99 }
100
101 GooString *FlateStream::getPSFilter(int psLevel, char *indent) {
102 GooString *s;
103
104 if (psLevel < 3 || pred) {
105 return NULL;
106 }
107 if (!(s = str->getPSFilter(psLevel, indent))) {
108 return NULL;
109 }
110 s->append(indent)->append("<< >> /FlateDecode filter\n");
111 return s;
112 }
113
114 GBool FlateStream::isBinary(GBool last) {
115 return str->isBinary(gTrue);
116 }