Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for customize inflate #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lodepng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1291,11 +1291,11 @@ unsigned lodepng_inflate(unsigned char** out, size_t* outsize,

static unsigned inflate(unsigned char** out, size_t* outsize,
const unsigned char* in, size_t insize,
const LodePNGDecompressSettings* settings)
const LodePNGDecompressSettings* settings, size_t outbuffsize)
{
if(settings->custom_inflate)
{
return settings->custom_inflate(out, outsize, in, insize, settings);
return settings->custom_inflate(out, outsize, in, insize, settings, outbuffsize);
}
else
{
Expand Down Expand Up @@ -2120,7 +2120,7 @@ static unsigned adler32(const unsigned char* data, unsigned len)
#ifdef LODEPNG_COMPILE_DECODER

unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
size_t insize, const LodePNGDecompressSettings* settings)
size_t insize, const LodePNGDecompressSettings* settings, size_t outbuffsize)
{
unsigned error = 0;
unsigned CM, CINFO, FDICT;
Expand Down Expand Up @@ -2151,7 +2151,7 @@ unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const uns
return 26;
}

error = inflate(out, outsize, in + 2, insize - 2, settings);
error = inflate(out, outsize, in + 2, insize - 2, settings, outbuffsize);
if(error) return error;

if(!settings->ignore_adler32)
Expand All @@ -2165,15 +2165,15 @@ unsigned lodepng_zlib_decompress(unsigned char** out, size_t* outsize, const uns
}

static unsigned zlib_decompress(unsigned char** out, size_t* outsize, const unsigned char* in,
size_t insize, const LodePNGDecompressSettings* settings)
size_t insize, const LodePNGDecompressSettings* settings, size_t outbuffsize)
{
if(settings->custom_zlib)
{
return settings->custom_zlib(out, outsize, in, insize, settings);
}
else
{
return lodepng_zlib_decompress(out, outsize, in, insize, settings);
return lodepng_zlib_decompress(out, outsize, in, insize, settings, outbuffsize);
}
}

Expand Down Expand Up @@ -2288,6 +2288,13 @@ const LodePNGCompressSettings lodepng_default_compress_settings = {2, 1, DEFAULT

#ifdef LODEPNG_COMPILE_DECODER

const LodePNGDecoderSettings* lodepng_user_decoder_settings = nullptr;

void lodepng_decoder_settings_user_regist(LodePNGDecoderSettings* settings)
{
lodepng_user_decoder_settings = settings;
}

void lodepng_decompress_settings_init(LodePNGDecompressSettings* settings)
{
settings->ignore_adler32 = 0;
Expand Down Expand Up @@ -4634,7 +4641,7 @@ static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSetting
/*will fail if zlib error, e.g. if length is too small*/
error = zlib_decompress(&decoded.data, &decoded.size,
(unsigned char*)(&data[string2_begin]),
length, zlibsettings);
length, zlibsettings, decoded.allocsize);
if(error) break;
ucvector_push_back(&decoded, 0);

Expand Down Expand Up @@ -4717,7 +4724,7 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting
/*will fail if zlib error, e.g. if length is too small*/
error = zlib_decompress(&decoded.data, &decoded.size,
(unsigned char*)(&data[begin]),
length, zlibsettings);
length, zlibsettings, decoded.allocsize);
if(error) break;
if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size;
ucvector_push_back(&decoded, 0);
Expand Down Expand Up @@ -4838,7 +4845,7 @@ static unsigned readChunk_iCCP(LodePNGInfo* info, const LodePNGDecompressSetting
ucvector_init(&decoded);
error = zlib_decompress(&decoded.data, &decoded.size,
(unsigned char*)(&data[string2_begin]),
length, zlibsettings);
length, zlibsettings, decoded.allocsize);
if(!error) {
info->iccp_profile_size = decoded.size;
info->iccp_profile = (unsigned char*)lodepng_malloc(decoded.size);
Expand Down Expand Up @@ -5147,7 +5154,7 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
if(!state->error)
{
state->error = zlib_decompress(&scanlines.data, &scanlines.size, idat.data,
idat.size, &state->decoder.zlibsettings);
idat.size, &state->decoder.zlibsettings, scanlines.allocsize);
if(!state->error && scanlines.size != predict) state->error = 91; /*decompressed size doesn't match prediction*/
}
ucvector_cleanup(&idat);
Expand Down Expand Up @@ -5260,6 +5267,10 @@ unsigned lodepng_decode24_file(unsigned char** out, unsigned* w, unsigned* h, co

void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings)
{
if (lodepng_user_decoder_settings) {
*settings = *lodepng_user_decoder_settings;
return;
}
settings->color_convert = 1;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
settings->read_text_chunks = 1;
Expand Down Expand Up @@ -6659,7 +6670,7 @@ unsigned decompress(std::vector<unsigned char>& out, const unsigned char* in, si
{
unsigned char* buffer = 0;
size_t buffersize = 0;
unsigned error = zlib_decompress(&buffer, &buffersize, in, insize, &settings);
unsigned error = zlib_decompress(&buffer, &buffersize, in, insize, &settings, out.size());
if(buffer)
{
out.insert(out.end(), &buffer[0], &buffer[buffersize]);
Expand Down
3 changes: 2 additions & 1 deletion lodepng.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ struct LodePNGDecompressSettings
zlib function will call custom_deflate*/
unsigned (*custom_inflate)(unsigned char**, size_t*,
const unsigned char*, size_t,
const LodePNGDecompressSettings*);
const LodePNGDecompressSettings*, size_t outbuffsize);

const void* custom_context; /*optional custom settings for custom functions*/
};
Expand Down Expand Up @@ -637,6 +637,7 @@ typedef struct LodePNGDecoderSettings
} LodePNGDecoderSettings;

void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings);
void lodepng_decoder_settings_user_regist(LodePNGDecoderSettings* settings);
#endif /*LODEPNG_COMPILE_DECODER*/

#ifdef LODEPNG_COMPILE_ENCODER
Expand Down