Commit 3c84d496f0143cacec83eba0d02457df70cf7f8d

Authored by Jay Berkenbilt
1 parent b84b698a

avoid having to include zlib.h in Pl_Flate.hh

git-svn-id: svn+q:///qpdf/trunk@882 71b93d88-0707-0410-a8cf-f5a4172ac649
include/qpdf/Pl_Flate.hh
@@ -10,8 +10,6 @@ @@ -10,8 +10,6 @@
10 10
11 #include <qpdf/Pipeline.hh> 11 #include <qpdf/Pipeline.hh>
12 12
13 -#include <zlib.h>  
14 -  
15 class Pl_Flate: public Pipeline 13 class Pl_Flate: public Pipeline
16 { 14 {
17 public: 15 public:
@@ -38,7 +36,7 @@ class Pl_Flate: public Pipeline @@ -38,7 +36,7 @@ class Pl_Flate: public Pipeline
38 int out_bufsize; 36 int out_bufsize;
39 action_e action; 37 action_e action;
40 bool initialized; 38 bool initialized;
41 - z_stream zstream; 39 + void* zdata;
42 }; 40 };
43 41
44 #endif // __PL_FLATE_HH__ 42 #endif // __PL_FLATE_HH__
libqpdf/Pl_Flate.cc
1 #include <qpdf/Pl_Flate.hh> 1 #include <qpdf/Pl_Flate.hh>
  2 +#include <zlib.h>
2 3
3 #include <qpdf/QUtil.hh> 4 #include <qpdf/QUtil.hh>
4 5
@@ -10,7 +11,14 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next, @@ -10,7 +11,14 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
10 initialized(false) 11 initialized(false)
11 { 12 {
12 this->outbuf = new unsigned char[out_bufsize]; 13 this->outbuf = new unsigned char[out_bufsize];
13 - 14 + // Indirect through zdata to reach the z_stream so we don't have
  15 + // to include zlib.h in Pl_Flate.hh. This means people using
  16 + // shared library versions of qpdf don't have to have zlib
  17 + // development files available, which particularly helps in a
  18 + // Windows environment.
  19 + this->zdata = new z_stream;
  20 +
  21 + z_stream& zstream = *((z_stream*) this->zdata);
14 zstream.zalloc = (alloc_func)0; 22 zstream.zalloc = (alloc_func)0;
15 zstream.zfree = (free_func)0; 23 zstream.zfree = (free_func)0;
16 zstream.opaque = (voidpf)0; 24 zstream.opaque = (voidpf)0;
@@ -27,6 +35,8 @@ Pl_Flate::~Pl_Flate() @@ -27,6 +35,8 @@ Pl_Flate::~Pl_Flate()
27 delete [] this->outbuf; 35 delete [] this->outbuf;
28 this->outbuf = 0; 36 this->outbuf = 0;
29 } 37 }
  38 + delete (z_stream*)this->zdata;
  39 + this->zdata = 0;
30 } 40 }
31 41
32 void 42 void
@@ -44,19 +54,20 @@ Pl_Flate::write(unsigned char* data, int len) @@ -44,19 +54,20 @@ Pl_Flate::write(unsigned char* data, int len)
44 void 54 void
45 Pl_Flate::handleData(unsigned char* data, int len, int flush) 55 Pl_Flate::handleData(unsigned char* data, int len, int flush)
46 { 56 {
47 - this->zstream.next_in = data;  
48 - this->zstream.avail_in = len; 57 + z_stream& zstream = *((z_stream*) this->zdata);
  58 + zstream.next_in = data;
  59 + zstream.avail_in = len;
49 60
50 if (! this->initialized) 61 if (! this->initialized)
51 { 62 {
52 int err = Z_OK; 63 int err = Z_OK;
53 if (this->action == a_deflate) 64 if (this->action == a_deflate)
54 { 65 {
55 - err = deflateInit(&this->zstream, Z_DEFAULT_COMPRESSION); 66 + err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
56 } 67 }
57 else 68 else
58 { 69 {
59 - err = inflateInit(&this->zstream); 70 + err = inflateInit(&zstream);
60 } 71 }
61 checkError("Init", err); 72 checkError("Init", err);
62 this->initialized = true; 73 this->initialized = true;
@@ -69,11 +80,11 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush) @@ -69,11 +80,11 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
69 { 80 {
70 if (action == a_deflate) 81 if (action == a_deflate)
71 { 82 {
72 - err = deflate(&this->zstream, flush); 83 + err = deflate(&zstream, flush);
73 } 84 }
74 else 85 else
75 { 86 {
76 - err = inflate(&this->zstream, flush); 87 + err = inflate(&zstream, flush);
77 } 88 }
78 switch (err) 89 switch (err)
79 { 90 {
@@ -91,20 +102,20 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush) @@ -91,20 +102,20 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
91 102
92 case Z_OK: 103 case Z_OK:
93 { 104 {
94 - if ((this->zstream.avail_in == 0) &&  
95 - (this->zstream.avail_out > 0)) 105 + if ((zstream.avail_in == 0) &&
  106 + (zstream.avail_out > 0))
96 { 107 {
97 // There is nothing left to read, and there was 108 // There is nothing left to read, and there was
98 // sufficient buffer space to write everything we 109 // sufficient buffer space to write everything we
99 // needed, so we're done for now. 110 // needed, so we're done for now.
100 done = true; 111 done = true;
101 } 112 }
102 - uLong ready = (this->out_bufsize - this->zstream.avail_out); 113 + uLong ready = (this->out_bufsize - zstream.avail_out);
103 if (ready > 0) 114 if (ready > 0)
104 { 115 {
105 this->getNext()->write(this->outbuf, ready); 116 this->getNext()->write(this->outbuf, ready);
106 - this->zstream.next_out = this->outbuf;  
107 - this->zstream.avail_out = this->out_bufsize; 117 + zstream.next_out = this->outbuf;
  118 + zstream.avail_out = this->out_bufsize;
108 } 119 }
109 } 120 }
110 break; 121 break;
@@ -123,17 +134,18 @@ Pl_Flate::finish() @@ -123,17 +134,18 @@ Pl_Flate::finish()
123 { 134 {
124 if (this->initialized) 135 if (this->initialized)
125 { 136 {
  137 + z_stream& zstream = *((z_stream*) this->zdata);
126 unsigned char buf[1]; 138 unsigned char buf[1];
127 buf[0] = '\0'; 139 buf[0] = '\0';
128 handleData(buf, 0, Z_FINISH); 140 handleData(buf, 0, Z_FINISH);
129 int err = Z_OK; 141 int err = Z_OK;
130 if (action == a_deflate) 142 if (action == a_deflate)
131 { 143 {
132 - err = deflateEnd(&this->zstream); 144 + err = deflateEnd(&zstream);
133 } 145 }
134 else 146 else
135 { 147 {
136 - err = inflateEnd(&this->zstream); 148 + err = inflateEnd(&zstream);
137 } 149 }
138 checkError("End", err); 150 checkError("End", err);
139 } 151 }
@@ -147,15 +159,16 @@ Pl_Flate::finish() @@ -147,15 +159,16 @@ Pl_Flate::finish()
147 void 159 void
148 Pl_Flate::checkError(char const* prefix, int error_code) 160 Pl_Flate::checkError(char const* prefix, int error_code)
149 { 161 {
  162 + z_stream& zstream = *((z_stream*) this->zdata);
150 if (error_code != Z_OK) 163 if (error_code != Z_OK)
151 { 164 {
152 char const* action_str = (action == a_deflate ? "deflate" : "inflate"); 165 char const* action_str = (action == a_deflate ? "deflate" : "inflate");
153 std::string msg = 166 std::string msg =
154 this->identifier + ": " + action_str + ": " + prefix + ": "; 167 this->identifier + ": " + action_str + ": " + prefix + ": ";
155 168
156 - if (this->zstream.msg) 169 + if (zstream.msg)
157 { 170 {
158 - msg += this->zstream.msg; 171 + msg += zstream.msg;
159 } 172 }
160 else 173 else
161 { 174 {