source: trunk/poppler/mypoppler/poppler/FlateStream.cc @ 461

Last change on this file since 461 was 257, checked in by Eugene Romanenko, 13 years ago

PDF plugin: Poppler library updated to version 0.10.0

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