Skip to content

Commit 83e9e68

Browse files
committed
encode: clean up unknown chunk writes
1 parent 05781a3 commit 83e9e68

File tree

1 file changed

+24
-42
lines changed

1 file changed

+24
-42
lines changed

spng/spng.c

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,24 @@ static int write_iend(spng_ctx *ctx)
998998
return write_data(ctx, iend_chunk, 12);
999999
}
10001000

1001+
static int write_unknown_chunks(spng_ctx *ctx, enum spng_location location)
1002+
{
1003+
if(!ctx->stored.unknown) return 0;
1004+
1005+
const struct spng_unknown_chunk *chunk = ctx->chunk_list;
1006+
1007+
uint32_t i;
1008+
for(i=0; i < ctx->n_chunks; i++, chunk++)
1009+
{
1010+
if(chunk->location != location) continue;
1011+
1012+
int ret = write_chunk(ctx, chunk->type, chunk->data, chunk->length);
1013+
if(ret) return ret;
1014+
}
1015+
1016+
return 0;
1017+
}
1018+
10011019
/* Read and check the current chunk's crc,
10021020
returns -SPNG_CRC_DISCARD if the chunk should be discarded */
10031021
static inline int read_and_check_crc(spng_ctx *ctx)
@@ -4145,20 +4163,8 @@ static int write_chunks_before_idat(spng_ctx *ctx)
41454163
if(ret) return ret;
41464164
}
41474165

4148-
if(ctx->stored.unknown)
4149-
{
4150-
uint32_t i;
4151-
for(i=0; i < ctx->n_chunks; i++)
4152-
{
4153-
struct spng_unknown_chunk *chunk = &ctx->chunk_list[i];
4154-
4155-
if(chunk->location == SPNG_AFTER_IHDR)
4156-
{
4157-
int ret = write_chunk(ctx, chunk->type, chunk->data, chunk->length);
4158-
if(ret) return ret;
4159-
}
4160-
}
4161-
}
4166+
ret = write_unknown_chunks(ctx, SPNG_AFTER_IHDR);
4167+
if(ret) return ret;
41624168

41634169
if(ctx->stored.plte)
41644170
{
@@ -4460,20 +4466,8 @@ static int write_chunks_before_idat(spng_ctx *ctx)
44604466
if(ret) return ret;
44614467
}
44624468

4463-
if(ctx->stored.unknown)
4464-
{
4465-
uint32_t i;
4466-
for(i=0; i < ctx->n_chunks; i++)
4467-
{
4468-
struct spng_unknown_chunk *chunk = &ctx->chunk_list[i];
4469-
4470-
if(chunk->location == SPNG_AFTER_PLTE)
4471-
{
4472-
int ret = write_chunk(ctx, chunk->type, chunk->data, chunk->length);
4473-
if(ret) return ret;
4474-
}
4475-
}
4476-
}
4469+
ret = write_unknown_chunks(ctx, SPNG_AFTER_PLTE);
4470+
if(ret) return ret;
44774471

44784472
return 0;
44794473
}
@@ -4482,20 +4476,8 @@ static int write_chunks_after_idat(spng_ctx *ctx)
44824476
{
44834477
if(ctx == NULL) return SPNG_EINTERNAL;
44844478

4485-
if(ctx->stored.unknown)
4486-
{
4487-
uint32_t i;
4488-
for(i=0; i < ctx->n_chunks; i++)
4489-
{
4490-
struct spng_unknown_chunk *chunk = &ctx->chunk_list[i];
4491-
4492-
if(chunk->location == SPNG_AFTER_IDAT)
4493-
{
4494-
int ret = write_chunk(ctx, chunk->type, chunk->data, chunk->length);
4495-
if(ret) return ret;
4496-
}
4497-
}
4498-
}
4479+
int ret = write_unknown_chunks(ctx, SPNG_AFTER_IDAT);
4480+
if(ret) return ret;
44994481

45004482
return write_iend(ctx);
45014483
}

0 commit comments

Comments
 (0)