Skip to content

Commit 7fd2a11

Browse files
committed
fix deflate decoding
1 parent 02cdaa8 commit 7fd2a11

3 files changed

Lines changed: 37 additions & 27 deletions

File tree

noson/src/private/compressor.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,26 @@
2323

2424
#if HAVE_ZLIB
2525
#include <zlib.h>
26+
#define DFLT_WINDOWS_BIT (-MAX_WBITS)
27+
#define GZIP_WINDOWS_BIT ((MAX_WBITS)+16)
28+
#define GZIP_CHUNK_SIZE 16384
2629
#else
27-
#define Z_NO_FLUSH 0
28-
#define Z_OK 0
29-
#define Z_STREAM_END 1
30-
#define Z_STREAM_ERROR (-2)
31-
#define Z_BUF_ERROR (-5)
30+
#define Z_NO_FLUSH 0
31+
#define Z_OK 0
32+
#define Z_STREAM_END 1
33+
#define Z_STREAM_ERROR (-2)
34+
#define Z_BUF_ERROR (-5)
35+
#define DFLT_WINDOWS_BIT 0
36+
#define GZIP_WINDOWS_BIT 0
37+
#define GZIP_CHUNK_SIZE 0
3238
#endif
3339

34-
#define GZIP_WINDOWS_BIT 15 + 16
35-
#define GZIP_CHUNK_SIZE 16384
3640
#define MIN(a,b) (a > b ? b : a)
3741
#define MAX(a,b) (a > b ? a : b)
3842

3943
using namespace NSROOT;
4044

41-
Compressor::Compressor(const char *input, size_t len, int level /*= -1*/)
45+
Compressor::Compressor(const char *input, size_t len, bool gzip, int level /*= -1*/)
4246
: m_status(Z_STREAM_ERROR)
4347
, m_flush(Z_NO_FLUSH)
4448
, m_stop(true)
@@ -57,12 +61,14 @@ Compressor::Compressor(const char *input, size_t len, int level /*= -1*/)
5761
#if HAVE_ZLIB
5862
m_output = new char[m_chunk_size];
5963
_opaque = new z_stream;
60-
m_status = _init(_opaque, m_output, m_chunk_size, level);
64+
m_status = _init(_opaque, m_output, m_chunk_size,
65+
(gzip ? GZIP_WINDOWS_BIT : DFLT_WINDOWS_BIT),
66+
level);
6167
m_stop = (m_status != Z_OK);
6268
#endif
6369
}
6470

65-
Compressor::Compressor(STREAM_READER reader, void *handle, int level /*= -1*/)
71+
Compressor::Compressor(STREAM_READER reader, void *handle, bool gzip, int level /*= -1*/)
6672
: m_status(Z_STREAM_ERROR)
6773
, m_flush(Z_NO_FLUSH)
6874
, m_stop(true)
@@ -82,7 +88,9 @@ Compressor::Compressor(STREAM_READER reader, void *handle, int level /*= -1*/)
8288
m_rstream_buf = new char[m_chunk_size];
8389
m_output = new char[m_chunk_size];
8490
_opaque = new z_stream;
85-
m_status = _init(_opaque, m_output, m_chunk_size, level);
91+
m_status = _init(_opaque, m_output, m_chunk_size,
92+
(gzip ? GZIP_WINDOWS_BIT : DFLT_WINDOWS_BIT),
93+
level);
8694
m_stop = (m_status != Z_OK);
8795
#endif
8896
}
@@ -219,7 +227,7 @@ size_t Compressor::FetchOutput(const char **data)
219227
return 0;
220228
}
221229

222-
int Compressor::_init(void *zp, void *out, size_t len, int level)
230+
int Compressor::_init(void *zp, void *out, size_t len, int wbits, int level)
223231
{
224232
#if HAVE_ZLIB
225233
z_stream *strm = static_cast<z_stream*>(zp);
@@ -231,7 +239,7 @@ int Compressor::_init(void *zp, void *out, size_t len, int level)
231239
strm->next_in = Z_NULL;
232240
strm->avail_out = len;
233241
strm->next_out = (unsigned char*)out;
234-
return deflateInit2(strm, MAX(-1, MIN(level, 9)), Z_DEFLATED, GZIP_WINDOWS_BIT, 8, Z_DEFAULT_STRATEGY);
242+
return deflateInit2(strm, MAX(-1, MIN(level, 9)), Z_DEFLATED, wbits, 8, Z_DEFAULT_STRATEGY);
235243
#else
236244
return Z_STREAM_ERROR;
237245
#endif
@@ -285,7 +293,7 @@ size_t Compressor::NextChunk()
285293
return sz;
286294
}
287295

288-
Decompressor::Decompressor(const char *input, size_t len)
296+
Decompressor::Decompressor(const char *input, size_t len, bool gzip)
289297
: m_status(Z_STREAM_ERROR)
290298
, m_stop(true)
291299
, m_chunk_size(GZIP_CHUNK_SIZE)
@@ -303,12 +311,13 @@ Decompressor::Decompressor(const char *input, size_t len)
303311
#if HAVE_ZLIB
304312
m_output = new char[m_chunk_size];
305313
_opaque = new z_stream;
306-
m_status = _init(_opaque, m_output, m_chunk_size);
314+
m_status = _init(_opaque, m_output, m_chunk_size,
315+
(gzip ? GZIP_WINDOWS_BIT : DFLT_WINDOWS_BIT));
307316
m_stop = (m_status != Z_OK);
308317
#endif
309318
}
310319

311-
Decompressor::Decompressor(STREAM_READER reader, void *handle)
320+
Decompressor::Decompressor(STREAM_READER reader, void *handle, bool gzip)
312321
: m_status(Z_STREAM_ERROR)
313322
, m_stop(true)
314323
, m_chunk_size(GZIP_CHUNK_SIZE)
@@ -327,7 +336,8 @@ Decompressor::Decompressor(STREAM_READER reader, void *handle)
327336
m_rstream_buf = new char[m_chunk_size];
328337
m_output = new char[m_chunk_size];
329338
_opaque = new z_stream;
330-
m_status = _init(_opaque, m_output, m_chunk_size);
339+
m_status = _init(_opaque, m_output, m_chunk_size,
340+
(gzip ? GZIP_WINDOWS_BIT : DFLT_WINDOWS_BIT));
331341
m_stop = (m_status != Z_OK);
332342
#endif
333343
}
@@ -464,7 +474,7 @@ size_t Decompressor::FetchOutput(const char **data)
464474
return 0;
465475
}
466476

467-
int Decompressor::_init(void *zp, void *out, size_t len)
477+
int Decompressor::_init(void *zp, void *out, size_t len, int wbits)
468478
{
469479
#if HAVE_ZLIB
470480
z_stream *strm = static_cast<z_stream*>(zp);
@@ -476,7 +486,7 @@ int Decompressor::_init(void *zp, void *out, size_t len)
476486
strm->next_in = Z_NULL;
477487
strm->avail_out = len;
478488
strm->next_out = (unsigned char*)out;
479-
return inflateInit2(strm, GZIP_WINDOWS_BIT);
489+
return inflateInit2(strm, wbits);
480490
#else
481491
return Z_STREAM_ERROR;
482492
#endif

noson/src/private/compressor.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ namespace NSROOT
3535
public:
3636
typedef int(*STREAM_READER)(void *handle, void *buf, int sz);
3737

38-
Compressor(STREAM_READER reader, void *handle, int level = -1);
39-
Compressor(const char *input, size_t len, int level = -1);
38+
Compressor(STREAM_READER reader, void *handle, bool gzip, int level = -1);
39+
Compressor(const char *input, size_t len, bool gzip, int level = -1);
4040
virtual ~Compressor();
4141

4242
/**
@@ -104,7 +104,7 @@ namespace NSROOT
104104

105105
void *_opaque;
106106

107-
static int _init(void *zp, void *out, size_t len, int level);
107+
static int _init(void *zp, void *out, size_t len, int wbits, int level);
108108
size_t NextChunk();
109109
};
110110

@@ -114,8 +114,8 @@ namespace NSROOT
114114
public:
115115
typedef int(*STREAM_READER)(void *handle, void *buf, int sz);
116116

117-
Decompressor(STREAM_READER reader, void *handle);
118-
Decompressor(const char *input, size_t len);
117+
Decompressor(STREAM_READER reader, void *handle, bool gzip);
118+
Decompressor(const char *input, size_t len, bool gzip);
119119
virtual ~Decompressor();
120120

121121
/**
@@ -182,7 +182,7 @@ namespace NSROOT
182182

183183
void *_opaque;
184184

185-
static int _init(void *zp, void *out, size_t len);
185+
static int _init(void *zp, void *out, size_t len, int wbits);
186186
size_t NextChunk();
187187
};
188188

noson/src/private/wsresponse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ int WSResponse::_response::ReadContent(char* buf, size_t buflen)
448448
{
449449
int s = 0;
450450
if (m_decoder == nullptr)
451-
m_decoder = new Decompressor(&SocketStreamReader, this);
451+
m_decoder = new Decompressor(&SocketStreamReader, this, (m_contentEncoding == WS_CENCODING_Gzip));
452452
if (m_decoder->HasOutputData())
453453
s = (int)m_decoder->ReadOutput(buf, buflen);
454454
if (s == 0 && !m_decoder->IsCompleted())
@@ -480,7 +480,7 @@ int WSResponse::_response::ReadContent(char* buf, size_t buflen)
480480
{
481481
int s = 0;
482482
if (m_decoder == nullptr)
483-
m_decoder = new Decompressor(&ChunkStreamReader, this);
483+
m_decoder = new Decompressor(&ChunkStreamReader, this, (m_contentEncoding == WS_CENCODING_Gzip));
484484
if (m_decoder->HasOutputData())
485485
s = (int)m_decoder->ReadOutput(buf, buflen);
486486
if (s == 0 && !m_decoder->IsCompleted())

0 commit comments

Comments
 (0)