Skip to content
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
35 changes: 22 additions & 13 deletions lodepng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2837,13 +2837,6 @@ unsigned lodepng_chunk_append(unsigned char** out, size_t* outsize, const unsign
size_t total_chunk_length, new_length;
unsigned char *chunk_start, *new_buffer;

if(!lodepng_chunk_type_name_valid(chunk)) {
return 121; /* invalid chunk type name */
}
if(lodepng_chunk_reserved(chunk)) {
return 122; /* invalid third lowercase character */
}

if(lodepng_addofl(lodepng_chunk_length(chunk), 12, &total_chunk_length)) return 77;
if(lodepng_addofl(*outsize, total_chunk_length, &new_length)) return 77;

Expand Down Expand Up @@ -5430,10 +5423,10 @@ static void decodeGeneric(unsigned char** out, unsigned* w, unsigned* h,
if(state->error) break;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
} else /*it's not an implemented chunk type, so ignore it: skip over the data*/ {
if(!lodepng_chunk_type_name_valid(chunk)) {
if(!state->decoder.ignore_invalid_name && !lodepng_chunk_type_name_valid(chunk)) {
CERROR_BREAK(state->error, 121); /* invalid chunk type name */
}
if(lodepng_chunk_reserved(chunk)) {
if(!state->decoder.ignore_reserved_name && lodepng_chunk_reserved(chunk)) {
CERROR_BREAK(state->error, 122); /* invalid third lowercase character */
}

Expand Down Expand Up @@ -5592,6 +5585,8 @@ void lodepng_decoder_settings_init(LodePNGDecoderSettings* settings) {
settings->remember_unknown_chunks = 0;
settings->max_text_size = 16777216;
settings->max_icc_size = 16777216; /* 16MB is much more than enough for any reasonable ICC profile */
settings->ignore_invalid_name = 0;
settings->ignore_reserved_name = 0;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
settings->ignore_crc = 0;
settings->ignore_critical = 0;
Expand Down Expand Up @@ -6436,9 +6431,16 @@ static unsigned preProcessScanlines(unsigned char** out, size_t* outsize, const
}

#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize) {
static unsigned addUnknownChunks(ucvector* out, unsigned char* data, size_t datasize, unsigned ignore_invalid, unsigned ignore_reserved) {
unsigned char* inchunk = data;
while((size_t)(inchunk - data) < datasize) {
if(!ignore_invalid && !lodepng_chunk_type_name_valid(inchunk)) {
return 121; /* invalid chunk type name */
}
if(!ignore_reserved && lodepng_chunk_reserved(inchunk)) {
return 122; /* invalid third lowercase character */
}

CERROR_TRY_RETURN(lodepng_chunk_append(&out->data, &out->size, inchunk));
out->allocsize = out->size; /*fix the allocsize again*/
inchunk = lodepng_chunk_next(inchunk, data + datasize);
Expand Down Expand Up @@ -6476,6 +6478,10 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,
LodePNGInfo info;
const LodePNGInfo* info_png = &state->info_png;
LodePNGColorMode auto_color;
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
unsigned ignore_invalid = state->encoder.ignore_invalid_name;
unsigned ignore_reserved = state->encoder.ignore_reserved_name;
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/

lodepng_info_init(&info);
lodepng_color_mode_init(&auto_color);
Expand Down Expand Up @@ -6663,7 +6669,7 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
/*unknown chunks between IHDR and PLTE*/
if(info.unknown_chunks_data[0]) {
state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0]);
state->error = addUnknownChunks(&outv, info.unknown_chunks_data[0], info.unknown_chunks_size[0], ignore_invalid, ignore_reserved);
if(state->error) goto cleanup;
}
/*color profile chunks must come before PLTE */
Expand Down Expand Up @@ -6731,7 +6737,7 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,

/*unknown chunks between PLTE and IDAT*/
if(info.unknown_chunks_data[1]) {
state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1]);
state->error = addUnknownChunks(&outv, info.unknown_chunks_data[1], info.unknown_chunks_size[1], ignore_invalid, ignore_reserved);
if(state->error) goto cleanup;
}
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
Expand Down Expand Up @@ -6798,7 +6804,7 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,

/*unknown chunks between IDAT and IEND*/
if(info.unknown_chunks_data[2]) {
state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2]);
state->error = addUnknownChunks(&outv, info.unknown_chunks_data[2], info.unknown_chunks_size[2], ignore_invalid, ignore_reserved);
if(state->error) goto cleanup;
}
#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
Expand Down Expand Up @@ -6871,6 +6877,9 @@ void lodepng_encoder_settings_init(LodePNGEncoderSettings* settings) {
#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
settings->add_id = 0;
settings->text_compression = 1;
settings->ignore_invalid_name = 0;
settings->ignore_reserved_name = 0;

#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
}

Expand Down
11 changes: 10 additions & 1 deletion lodepng.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,6 @@ typedef struct LodePNGDecoderSettings {
errors: srgb rendering intent value, size of content of ancillary chunks, more than 79 characters for some
strings, placement/combination rules for ancillary chunks, crc of unknown chunks, allowed characters
in string keys, invalid characters in chunk types names, etc... */

unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/

#ifdef LODEPNG_COMPILE_ANCILLARY_CHUNKS
Expand All @@ -832,6 +831,9 @@ typedef struct LodePNGDecoderSettings {
/*store all bytes from unknown chunks in the LodePNGInfo (off by default, useful for a png editor)*/
unsigned remember_unknown_chunks;

unsigned ignore_invalid_name; /*ignore error when chunk names contain invalid characters*/
unsigned ignore_reserved_name; /*ignore error when chunk name has the reserved bit set*/

/* maximum size for decompressed text chunks. If a text chunk's text is larger than this, an error is returned,
unless reading text chunks is disabled or this limit is set higher or disabled. Set to 0 to allow any size.
By default it is a value that prevents unreasonably large strings from hogging memory. */
Expand Down Expand Up @@ -936,6 +938,9 @@ typedef struct LodePNGEncoderSettings {
unsigned add_id;
/*encode text chunks as zTXt chunks instead of tEXt chunks, and use compression in iTXt chunks*/
unsigned text_compression;
unsigned ignore_invalid_name; /*ignore error when chunk names contain invalid characters*/
unsigned ignore_reserved_name; /*ignore error when chunk name has the reserved bit set*/

#endif /*LODEPNG_COMPILE_ANCILLARY_CHUNKS*/
} LodePNGEncoderSettings;

Expand Down Expand Up @@ -1959,6 +1964,8 @@ state.decoder.zlibsettings.custom_...: use custom inflate function
state.decoder.ignore_crc: ignore CRC checksums
state.decoder.ignore_critical: ignore unknown critical chunks
state.decoder.ignore_end: ignore missing IEND chunk. May fail if this corruption causes other errors
state.decoder.ignore_invalid_name: do not generate error on invalid names on chunk types
state.decoder.ignore_reserved_name: do not generate error on reserved names on chunck types
state.decoder.color_convert: convert internal PNG color to chosen one
state.decoder.read_text_chunks: whether to read in text metadata chunks
state.decoder.remember_unknown_chunks: whether to read in unknown chunks
Expand All @@ -1982,6 +1989,8 @@ state.encoder.filter_strategy: PNG filter strategy to encode with
state.encoder.force_palette: add palette even if not encoding to one
state.encoder.add_id: add LodePNG identifier and version as a text chunk
state.encoder.text_compression: use compressed text chunks for metadata
state.encoder.ignore_invalid_name: do not generate error on invalid names on chunk types
state.encoder.ignore_reserved_name: do not generate error on reserved names on chunck types
state.info_raw.colortype: color type of raw input image you provide
state.info_raw.bitdepth: bit depth of raw input image you provide
state.info_raw: more color settings, see struct LodePNGColorMode
Expand Down