Commit 3c84d496f0143cacec83eba0d02457df70cf7f8d
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
Showing
2 changed files
with
30 additions
and
19 deletions
include/qpdf/Pl_Flate.hh
| ... | ... | @@ -10,8 +10,6 @@ |
| 10 | 10 | |
| 11 | 11 | #include <qpdf/Pipeline.hh> |
| 12 | 12 | |
| 13 | -#include <zlib.h> | |
| 14 | - | |
| 15 | 13 | class Pl_Flate: public Pipeline |
| 16 | 14 | { |
| 17 | 15 | public: |
| ... | ... | @@ -38,7 +36,7 @@ class Pl_Flate: public Pipeline |
| 38 | 36 | int out_bufsize; |
| 39 | 37 | action_e action; |
| 40 | 38 | bool initialized; |
| 41 | - z_stream zstream; | |
| 39 | + void* zdata; | |
| 42 | 40 | }; |
| 43 | 41 | |
| 44 | 42 | #endif // __PL_FLATE_HH__ | ... | ... |
libqpdf/Pl_Flate.cc
| 1 | 1 | #include <qpdf/Pl_Flate.hh> |
| 2 | +#include <zlib.h> | |
| 2 | 3 | |
| 3 | 4 | #include <qpdf/QUtil.hh> |
| 4 | 5 | |
| ... | ... | @@ -10,7 +11,14 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next, |
| 10 | 11 | initialized(false) |
| 11 | 12 | { |
| 12 | 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 | 22 | zstream.zalloc = (alloc_func)0; |
| 15 | 23 | zstream.zfree = (free_func)0; |
| 16 | 24 | zstream.opaque = (voidpf)0; |
| ... | ... | @@ -27,6 +35,8 @@ Pl_Flate::~Pl_Flate() |
| 27 | 35 | delete [] this->outbuf; |
| 28 | 36 | this->outbuf = 0; |
| 29 | 37 | } |
| 38 | + delete (z_stream*)this->zdata; | |
| 39 | + this->zdata = 0; | |
| 30 | 40 | } |
| 31 | 41 | |
| 32 | 42 | void |
| ... | ... | @@ -44,19 +54,20 @@ Pl_Flate::write(unsigned char* data, int len) |
| 44 | 54 | void |
| 45 | 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 | 61 | if (! this->initialized) |
| 51 | 62 | { |
| 52 | 63 | int err = Z_OK; |
| 53 | 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 | 68 | else |
| 58 | 69 | { |
| 59 | - err = inflateInit(&this->zstream); | |
| 70 | + err = inflateInit(&zstream); | |
| 60 | 71 | } |
| 61 | 72 | checkError("Init", err); |
| 62 | 73 | this->initialized = true; |
| ... | ... | @@ -69,11 +80,11 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush) |
| 69 | 80 | { |
| 70 | 81 | if (action == a_deflate) |
| 71 | 82 | { |
| 72 | - err = deflate(&this->zstream, flush); | |
| 83 | + err = deflate(&zstream, flush); | |
| 73 | 84 | } |
| 74 | 85 | else |
| 75 | 86 | { |
| 76 | - err = inflate(&this->zstream, flush); | |
| 87 | + err = inflate(&zstream, flush); | |
| 77 | 88 | } |
| 78 | 89 | switch (err) |
| 79 | 90 | { |
| ... | ... | @@ -91,20 +102,20 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush) |
| 91 | 102 | |
| 92 | 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 | 108 | // There is nothing left to read, and there was |
| 98 | 109 | // sufficient buffer space to write everything we |
| 99 | 110 | // needed, so we're done for now. |
| 100 | 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 | 114 | if (ready > 0) |
| 104 | 115 | { |
| 105 | 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 | 121 | break; |
| ... | ... | @@ -123,17 +134,18 @@ Pl_Flate::finish() |
| 123 | 134 | { |
| 124 | 135 | if (this->initialized) |
| 125 | 136 | { |
| 137 | + z_stream& zstream = *((z_stream*) this->zdata); | |
| 126 | 138 | unsigned char buf[1]; |
| 127 | 139 | buf[0] = '\0'; |
| 128 | 140 | handleData(buf, 0, Z_FINISH); |
| 129 | 141 | int err = Z_OK; |
| 130 | 142 | if (action == a_deflate) |
| 131 | 143 | { |
| 132 | - err = deflateEnd(&this->zstream); | |
| 144 | + err = deflateEnd(&zstream); | |
| 133 | 145 | } |
| 134 | 146 | else |
| 135 | 147 | { |
| 136 | - err = inflateEnd(&this->zstream); | |
| 148 | + err = inflateEnd(&zstream); | |
| 137 | 149 | } |
| 138 | 150 | checkError("End", err); |
| 139 | 151 | } |
| ... | ... | @@ -147,15 +159,16 @@ Pl_Flate::finish() |
| 147 | 159 | void |
| 148 | 160 | Pl_Flate::checkError(char const* prefix, int error_code) |
| 149 | 161 | { |
| 162 | + z_stream& zstream = *((z_stream*) this->zdata); | |
| 150 | 163 | if (error_code != Z_OK) |
| 151 | 164 | { |
| 152 | 165 | char const* action_str = (action == a_deflate ? "deflate" : "inflate"); |
| 153 | 166 | std::string msg = |
| 154 | 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 | 173 | else |
| 161 | 174 | { | ... | ... |