Permalink
Browse files

zlib, feat: supports maxSize when decompressing.

  • Loading branch information...
xicilion committed Jun 24, 2018
1 parent 172c4ea commit af14abd12bac0734a74fd27c075aca9b531e5853
View
@@ -61,8 +61,14 @@ class ZlibStream : public Stream_base {
if (pThis->m_pThis->strm.avail_in == 0 && pThis->m_flush == Z_NO_FLUSH)
return pThis->done();
pThis->m_buffer = new Buffer((const char*)pThis->m_pThis->m_outBuffer,
ZLIB_CHUNK - pThis->m_pThis->strm.avail_out);
int32_t size = ZLIB_CHUNK - pThis->m_pThis->strm.avail_out;
if (pThis->m_pThis->m_maxSize >= 0) {
if (size > pThis->m_pThis->m_maxSize - pThis->m_pThis->m_dataSize)
return CALL_E_OVERFLOW;
pThis->m_pThis->m_dataSize += size;
}
pThis->m_buffer = new Buffer((const char*)pThis->m_pThis->m_outBuffer, size);
pThis->m_pThis->resetBuffer();
return pThis->m_stm->write(pThis->m_buffer, pThis);
}
@@ -76,8 +82,10 @@ class ZlibStream : public Stream_base {
};
public:
ZlibStream(Stream_base* stm)
ZlibStream(Stream_base* stm, int32_t maxSize = -1)
: m_stm(stm)
, m_maxSize(maxSize)
, m_dataSize(0)
{
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
@@ -259,6 +267,8 @@ class ZlibStream : public Stream_base {
protected:
z_stream strm;
obj_ptr<Stream_base> m_stm;
int32_t m_maxSize;
int32_t m_dataSize;
unsigned char m_outBuffer[ZLIB_CHUNK];
};
@@ -284,8 +294,8 @@ class def_base : public ZlibStream {
class inf_base : public ZlibStream {
public:
inf_base(Stream_base* stm)
: ZlibStream(stm)
inf_base(Stream_base* stm, int32_t maxSize)
: ZlibStream(stm, maxSize)
{
}
@@ -316,8 +326,8 @@ class inf_base : public ZlibStream {
class inf : public inf_base {
public:
inf(Stream_base* stm)
: inf_base(stm)
inf(Stream_base* stm, int32_t maxSize)
: inf_base(stm, maxSize)
{
inflateInit(&strm);
}
@@ -334,8 +344,8 @@ class def : public def_base {
class gunz : public inf_base {
public:
gunz(Stream_base* stm)
: inf_base(stm)
gunz(Stream_base* stm, int32_t maxSize)
: inf_base(stm, maxSize)
{
inflateInit2(&strm, 15 + 16);
}
@@ -352,8 +362,8 @@ class gz : public def_base {
class infraw : public inf_base {
public:
infraw(Stream_base* stm)
: inf_base(stm)
infraw(Stream_base* stm, int32_t maxSize)
: inf_base(stm, maxSize)
{
inflateInit2(&strm, -15);
}
View
@@ -34,28 +34,28 @@ class zlib_base : public object_base {
// zlib_base
static result_t createDeflate(Stream_base* to, obj_ptr<Stream_base>& retVal);
static result_t createDeflateRaw(Stream_base* to, obj_ptr<Stream_base>& retVal);
static result_t createGunzip(Stream_base* to, obj_ptr<Stream_base>& retVal);
static result_t createGunzip(Stream_base* to, int32_t maxSize, obj_ptr<Stream_base>& retVal);
static result_t createGzip(Stream_base* to, obj_ptr<Stream_base>& retVal);
static result_t createInflate(Stream_base* to, obj_ptr<Stream_base>& retVal);
static result_t createInflateRaw(Stream_base* to, obj_ptr<Stream_base>& retVal);
static result_t createInflate(Stream_base* to, int32_t maxSize, obj_ptr<Stream_base>& retVal);
static result_t createInflateRaw(Stream_base* to, int32_t maxSize, obj_ptr<Stream_base>& retVal);
static result_t deflate(Buffer_base* data, int32_t level, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t deflateTo(Buffer_base* data, Stream_base* stm, int32_t level, AsyncEvent* ac);
static result_t deflateTo(Stream_base* src, Stream_base* stm, int32_t level, AsyncEvent* ac);
static result_t inflate(Buffer_base* data, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t inflateTo(Buffer_base* data, Stream_base* stm, AsyncEvent* ac);
static result_t inflateTo(Stream_base* src, Stream_base* stm, AsyncEvent* ac);
static result_t inflate(Buffer_base* data, int32_t maxSize, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t inflateTo(Buffer_base* data, Stream_base* stm, int32_t maxSize, AsyncEvent* ac);
static result_t inflateTo(Stream_base* src, Stream_base* stm, int32_t maxSize, AsyncEvent* ac);
static result_t gzip(Buffer_base* data, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t gzipTo(Buffer_base* data, Stream_base* stm, AsyncEvent* ac);
static result_t gzipTo(Stream_base* src, Stream_base* stm, AsyncEvent* ac);
static result_t gunzip(Buffer_base* data, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t gunzipTo(Buffer_base* data, Stream_base* stm, AsyncEvent* ac);
static result_t gunzipTo(Stream_base* src, Stream_base* stm, AsyncEvent* ac);
static result_t gunzip(Buffer_base* data, int32_t maxSize, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t gunzipTo(Buffer_base* data, Stream_base* stm, int32_t maxSize, AsyncEvent* ac);
static result_t gunzipTo(Stream_base* src, Stream_base* stm, int32_t maxSize, AsyncEvent* ac);
static result_t deflateRaw(Buffer_base* data, int32_t level, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t deflateRawTo(Buffer_base* data, Stream_base* stm, int32_t level, AsyncEvent* ac);
static result_t deflateRawTo(Stream_base* src, Stream_base* stm, int32_t level, AsyncEvent* ac);
static result_t inflateRaw(Buffer_base* data, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t inflateRawTo(Buffer_base* data, Stream_base* stm, AsyncEvent* ac);
static result_t inflateRawTo(Stream_base* src, Stream_base* stm, AsyncEvent* ac);
static result_t inflateRaw(Buffer_base* data, int32_t maxSize, obj_ptr<Buffer_base>& retVal, AsyncEvent* ac);
static result_t inflateRawTo(Buffer_base* data, Stream_base* stm, int32_t maxSize, AsyncEvent* ac);
static result_t inflateRawTo(Stream_base* src, Stream_base* stm, int32_t maxSize, AsyncEvent* ac);
public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args)
@@ -92,21 +92,21 @@ class zlib_base : public object_base {
ASYNC_STATICVALUE3(zlib_base, deflate, Buffer_base*, int32_t, obj_ptr<Buffer_base>);
ASYNC_STATIC3(zlib_base, deflateTo, Buffer_base*, Stream_base*, int32_t);
ASYNC_STATIC3(zlib_base, deflateTo, Stream_base*, Stream_base*, int32_t);
ASYNC_STATICVALUE2(zlib_base, inflate, Buffer_base*, obj_ptr<Buffer_base>);
ASYNC_STATIC2(zlib_base, inflateTo, Buffer_base*, Stream_base*);
ASYNC_STATIC2(zlib_base, inflateTo, Stream_base*, Stream_base*);
ASYNC_STATICVALUE3(zlib_base, inflate, Buffer_base*, int32_t, obj_ptr<Buffer_base>);
ASYNC_STATIC3(zlib_base, inflateTo, Buffer_base*, Stream_base*, int32_t);
ASYNC_STATIC3(zlib_base, inflateTo, Stream_base*, Stream_base*, int32_t);
ASYNC_STATICVALUE2(zlib_base, gzip, Buffer_base*, obj_ptr<Buffer_base>);
ASYNC_STATIC2(zlib_base, gzipTo, Buffer_base*, Stream_base*);
ASYNC_STATIC2(zlib_base, gzipTo, Stream_base*, Stream_base*);
ASYNC_STATICVALUE2(zlib_base, gunzip, Buffer_base*, obj_ptr<Buffer_base>);
ASYNC_STATIC2(zlib_base, gunzipTo, Buffer_base*, Stream_base*);
ASYNC_STATIC2(zlib_base, gunzipTo, Stream_base*, Stream_base*);
ASYNC_STATICVALUE3(zlib_base, gunzip, Buffer_base*, int32_t, obj_ptr<Buffer_base>);
ASYNC_STATIC3(zlib_base, gunzipTo, Buffer_base*, Stream_base*, int32_t);
ASYNC_STATIC3(zlib_base, gunzipTo, Stream_base*, Stream_base*, int32_t);
ASYNC_STATICVALUE3(zlib_base, deflateRaw, Buffer_base*, int32_t, obj_ptr<Buffer_base>);
ASYNC_STATIC3(zlib_base, deflateRawTo, Buffer_base*, Stream_base*, int32_t);
ASYNC_STATIC3(zlib_base, deflateRawTo, Stream_base*, Stream_base*, int32_t);
ASYNC_STATICVALUE2(zlib_base, inflateRaw, Buffer_base*, obj_ptr<Buffer_base>);
ASYNC_STATIC2(zlib_base, inflateRawTo, Buffer_base*, Stream_base*);
ASYNC_STATIC2(zlib_base, inflateRawTo, Stream_base*, Stream_base*);
ASYNC_STATICVALUE3(zlib_base, inflateRaw, Buffer_base*, int32_t, obj_ptr<Buffer_base>);
ASYNC_STATIC3(zlib_base, inflateRawTo, Buffer_base*, Stream_base*, int32_t);
ASYNC_STATIC3(zlib_base, inflateRawTo, Stream_base*, Stream_base*, int32_t);
};
}
@@ -205,11 +205,12 @@ inline void zlib_base::s_createGunzip(const v8::FunctionCallbackInfo<v8::Value>&
METHOD_NAME("zlib.createGunzip");
METHOD_ENTER();
METHOD_OVER(1, 1);
METHOD_OVER(2, 1);
ARG(obj_ptr<Stream_base>, 0);
OPT_ARG(int32_t, 1, -1);
hr = createGunzip(v0, vr);
hr = createGunzip(v0, v1, vr);
METHOD_RETURN();
}
@@ -237,11 +238,12 @@ inline void zlib_base::s_createInflate(const v8::FunctionCallbackInfo<v8::Value>
METHOD_NAME("zlib.createInflate");
METHOD_ENTER();
METHOD_OVER(1, 1);
METHOD_OVER(2, 1);
ARG(obj_ptr<Stream_base>, 0);
OPT_ARG(int32_t, 1, -1);
hr = createInflate(v0, vr);
hr = createInflate(v0, v1, vr);
METHOD_RETURN();
}
@@ -253,11 +255,12 @@ inline void zlib_base::s_createInflateRaw(const v8::FunctionCallbackInfo<v8::Val
METHOD_NAME("zlib.createInflateRaw");
METHOD_ENTER();
METHOD_OVER(1, 1);
METHOD_OVER(2, 1);
ARG(obj_ptr<Stream_base>, 0);
OPT_ARG(int32_t, 1, -1);
hr = createInflateRaw(v0, vr);
hr = createInflateRaw(v0, v1, vr);
METHOD_RETURN();
}
@@ -322,15 +325,16 @@ inline void zlib_base::s_inflate(const v8::FunctionCallbackInfo<v8::Value>& args
METHOD_NAME("zlib.inflate");
METHOD_ENTER();
ASYNC_METHOD_OVER(1, 1);
ASYNC_METHOD_OVER(2, 1);
ARG(obj_ptr<Buffer_base>, 0);
OPT_ARG(int32_t, 1, -1);
if (!cb.IsEmpty()) {
acb_inflate(v0, cb);
acb_inflate(v0, v1, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_inflate(v0, vr);
hr = ac_inflate(v0, v1, vr);
METHOD_RETURN();
}
@@ -340,27 +344,29 @@ inline void zlib_base::s_inflateTo(const v8::FunctionCallbackInfo<v8::Value>& ar
METHOD_NAME("zlib.inflateTo");
METHOD_ENTER();
ASYNC_METHOD_OVER(2, 2);
ASYNC_METHOD_OVER(3, 2);
ARG(obj_ptr<Buffer_base>, 0);
ARG(obj_ptr<Stream_base>, 1);
OPT_ARG(int32_t, 2, -1);
if (!cb.IsEmpty()) {
acb_inflateTo(v0, v1, cb);
acb_inflateTo(v0, v1, v2, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_inflateTo(v0, v1);
hr = ac_inflateTo(v0, v1, v2);
ASYNC_METHOD_OVER(2, 2);
ASYNC_METHOD_OVER(3, 2);
ARG(obj_ptr<Stream_base>, 0);
ARG(obj_ptr<Stream_base>, 1);
OPT_ARG(int32_t, 2, -1);
if (!cb.IsEmpty()) {
acb_inflateTo(v0, v1, cb);
acb_inflateTo(v0, v1, v2, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_inflateTo(v0, v1);
hr = ac_inflateTo(v0, v1, v2);
METHOD_VOID();
}
@@ -422,15 +428,16 @@ inline void zlib_base::s_gunzip(const v8::FunctionCallbackInfo<v8::Value>& args)
METHOD_NAME("zlib.gunzip");
METHOD_ENTER();
ASYNC_METHOD_OVER(1, 1);
ASYNC_METHOD_OVER(2, 1);
ARG(obj_ptr<Buffer_base>, 0);
OPT_ARG(int32_t, 1, -1);
if (!cb.IsEmpty()) {
acb_gunzip(v0, cb);
acb_gunzip(v0, v1, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_gunzip(v0, vr);
hr = ac_gunzip(v0, v1, vr);
METHOD_RETURN();
}
@@ -440,27 +447,29 @@ inline void zlib_base::s_gunzipTo(const v8::FunctionCallbackInfo<v8::Value>& arg
METHOD_NAME("zlib.gunzipTo");
METHOD_ENTER();
ASYNC_METHOD_OVER(2, 2);
ASYNC_METHOD_OVER(3, 2);
ARG(obj_ptr<Buffer_base>, 0);
ARG(obj_ptr<Stream_base>, 1);
OPT_ARG(int32_t, 2, -1);
if (!cb.IsEmpty()) {
acb_gunzipTo(v0, v1, cb);
acb_gunzipTo(v0, v1, v2, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_gunzipTo(v0, v1);
hr = ac_gunzipTo(v0, v1, v2);
ASYNC_METHOD_OVER(2, 2);
ASYNC_METHOD_OVER(3, 2);
ARG(obj_ptr<Stream_base>, 0);
ARG(obj_ptr<Stream_base>, 1);
OPT_ARG(int32_t, 2, -1);
if (!cb.IsEmpty()) {
acb_gunzipTo(v0, v1, cb);
acb_gunzipTo(v0, v1, v2, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_gunzipTo(v0, v1);
hr = ac_gunzipTo(v0, v1, v2);
METHOD_VOID();
}
@@ -525,15 +534,16 @@ inline void zlib_base::s_inflateRaw(const v8::FunctionCallbackInfo<v8::Value>& a
METHOD_NAME("zlib.inflateRaw");
METHOD_ENTER();
ASYNC_METHOD_OVER(1, 1);
ASYNC_METHOD_OVER(2, 1);
ARG(obj_ptr<Buffer_base>, 0);
OPT_ARG(int32_t, 1, -1);
if (!cb.IsEmpty()) {
acb_inflateRaw(v0, cb);
acb_inflateRaw(v0, v1, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_inflateRaw(v0, vr);
hr = ac_inflateRaw(v0, v1, vr);
METHOD_RETURN();
}
@@ -543,27 +553,29 @@ inline void zlib_base::s_inflateRawTo(const v8::FunctionCallbackInfo<v8::Value>&
METHOD_NAME("zlib.inflateRawTo");
METHOD_ENTER();
ASYNC_METHOD_OVER(2, 2);
ASYNC_METHOD_OVER(3, 2);
ARG(obj_ptr<Buffer_base>, 0);
ARG(obj_ptr<Stream_base>, 1);
OPT_ARG(int32_t, 2, -1);
if (!cb.IsEmpty()) {
acb_inflateRawTo(v0, v1, cb);
acb_inflateRawTo(v0, v1, v2, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_inflateRawTo(v0, v1);
hr = ac_inflateRawTo(v0, v1, v2);
ASYNC_METHOD_OVER(2, 2);
ASYNC_METHOD_OVER(3, 2);
ARG(obj_ptr<Stream_base>, 0);
ARG(obj_ptr<Stream_base>, 1);
OPT_ARG(int32_t, 2, -1);
if (!cb.IsEmpty()) {
acb_inflateRawTo(v0, v1, cb);
acb_inflateRawTo(v0, v1, v2, cb);
hr = CALL_RETURN_NULL;
} else
hr = ac_inflateRawTo(v0, v1);
hr = ac_inflateRawTo(v0, v1, v2);
METHOD_VOID();
}
View
@@ -31,7 +31,7 @@ struct OptData {
void getDate(obj_ptr<Buffer_base>& data) const
{
obj_ptr<Buffer_base> zdata = new Buffer(source, size);
zlib_base::cc_inflate(zdata, data);
zlib_base::cc_inflate(zdata, -1, data);
}
};
@@ -331,9 +331,11 @@ result_t HttpClient::request(Stream_base* conn, HttpRequest_base* req,
exlib::string str = hdr.string();
if (str == "gzip")
return zlib_base::gunzipTo(pThis->m_body, pThis->m_unzip, pThis);
return zlib_base::gunzipTo(pThis->m_body, pThis->m_unzip,
pThis->m_maxBodySize, pThis);
else if (str == "deflate")
return zlib_base::inflateRawTo(pThis->m_body, pThis->m_unzip, pThis);
return zlib_base::inflateRawTo(pThis->m_body, pThis->m_unzip,
pThis->m_maxBodySize, pThis);
}
return 0;
@@ -21,7 +21,7 @@ result_t JscLoader::run(SandBox::Context* ctx, Buffer_base* src, exlib::string n
result_t hr;
obj_ptr<Buffer_base> unz;
hr = zlib_base::cc_gunzip(src, unz);
hr = zlib_base::cc_gunzip(src, -1, unz);
if (hr < 0)
return hr;
Oops, something went wrong.

0 comments on commit af14abd

Please sign in to comment.