Skip to content

Commit

Permalink
Cleanup and better safety checks, 7
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaaaa123456789 committed Jan 22, 2022
1 parent d2fc1f9 commit 121d9be
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ void prepare_image_buffer_data (struct context * context, const void * restrict
load_file(context, buffer);
return;
case PLUM_BUFFER:
context -> data = ((struct plum_buffer *) buffer) -> data;
context -> size = ((struct plum_buffer *) buffer) -> size;
context -> data = ((const struct plum_buffer *) buffer) -> data;
context -> size = ((const struct plum_buffer *) buffer) -> size;
return;
case PLUM_CALLBACK:
load_from_callback(context, buffer);
Expand Down Expand Up @@ -134,10 +134,10 @@ void load_from_callback (struct context * context, const struct plum_callback *
void * resize_read_buffer (struct context * context, void * buffer, size_t * restrict allocated) {
// will set the buffer to its initial size on first call (buffer = NULL, allocated = ignored), or extend it on further calls
if (buffer)
if (*allocated <= 0x20000u)
if (*allocated < (0x20000u - sizeof(union allocator_node)))
*allocated += 0x4000;
else
*allocated += (size_t) 0x4000 << (bit_width(*allocated - 1) - 17);
*allocated += (size_t) 0x4000 << (bit_width(*allocated + sizeof(union allocator_node)) - 17);
else
*allocated = 0x4000 - sizeof(union allocator_node); // keep the buffer aligned to memory pages
return ctxrealloc(context, buffer, *allocated);
Expand Down
3 changes: 2 additions & 1 deletion src/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ struct plum_metadata * plum_allocate_metadata (struct plum_image * image, size_t
struct plum_metadata result;
max_align_t alignment;
} * result = plum_malloc(image, sizeof *result + size);
if (result) result -> result = (struct plum_metadata) {
if (!result) return NULL;
result -> result = (struct plum_metadata) {
.type = PLUM_METADATA_NONE,
.size = size,
.data = result + 1,
Expand Down
23 changes: 12 additions & 11 deletions src/newstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
struct plum_image * plum_new_image (void) {
union allocator_node * allocator = NULL;
struct plum_image * image = allocate(&allocator, sizeof *image);
if (image) *image = (struct plum_image) {.allocator = allocator};
if (image) *image = (struct plum_image) {.allocator = allocator}; // zero-initialize all other members
return image;
}

Expand All @@ -25,8 +25,7 @@ struct plum_image * plum_copy_image (const struct plum_image * image) {
allocated -> type = current -> type;
memcpy(allocated -> data, current -> data, current -> size);
struct plum_metadata * last = copy -> metadata = allocated;
while (current -> next) {
current = current -> next;
while (current = current -> next) {
allocated = plum_allocate_metadata(copy, current -> size);
if (!allocated) goto error;
allocated -> type = current -> type;
Expand All @@ -35,15 +34,17 @@ struct plum_image * plum_copy_image (const struct plum_image * image) {
last = allocated;
}
}
size_t size = plum_pixel_buffer_size(image);
if (!size) goto error;
void * buffer = plum_malloc(copy, size);
if (!buffer) goto error;
memcpy(buffer, image -> data, size);
copy -> data = buffer;
if (image -> width && image -> height && image -> frames) {
size_t size = plum_pixel_buffer_size(image);
if (!size) goto error;
void * buffer = plum_malloc(copy, size);
if (!buffer) goto error;
memcpy(buffer, image -> data, size);
copy -> data = buffer;
}
if (image -> palette) {
size = plum_palette_buffer_size(image);
buffer = plum_malloc(copy, size);
size_t size = plum_palette_buffer_size(image);
void * buffer = plum_malloc(copy, size);
if (!buffer) goto error;
memcpy(buffer, image -> palette, size);
copy -> palette = buffer;
Expand Down
7 changes: 3 additions & 4 deletions src/palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ void generate_palette (struct context * context, unsigned flags) {
if (result >= 0) {
plum_free(context -> image, context -> image -> data);
context -> image -> data = indexes;
context -> image -> palette = palette;
context -> image -> max_palette_index = result;
palette = plum_realloc(context -> image, palette, plum_color_buffer_size(result + 1, context -> image -> color_format));
if (palette) context -> image -> palette = palette;
context -> image -> palette = plum_realloc(context -> image, palette, plum_color_buffer_size(result + 1, context -> image -> color_format));
if (!context -> image -> palette) throw(context, PLUM_ERR_OUT_OF_MEMORY);
} else if (result == -PLUM_ERR_TOO_MANY_COLORS) {
plum_free(context -> image, palette);
plum_free(context -> image, indexes);
Expand Down Expand Up @@ -72,6 +71,7 @@ void reduce_palette (struct plum_image * image) {
uint64_t colors[0x200];
// converting up to 64-bit and later back to the original format is lossless
plum_convert_colors(colors, image -> palette, image -> max_palette_index + 1, PLUM_COLOR_64, image -> color_format);
memcpy(colors + 0x100, colors, sizeof(uint64_t) * (image -> max_palette_index + 1));
for (p = image -> max_palette_index; p != SIZE_MAX; p --) {
colors[2 * p + 1] = colors[p];
colors[2 * p] = p;
Expand All @@ -86,7 +86,6 @@ void reduce_palette (struct plum_image * image) {
ref = map[colors[2 * p]] = colors[2 * p];
else
map[colors[2 * p]] = ref;
plum_convert_colors(colors + 0x100, image -> palette, image -> max_palette_index + 1, PLUM_COLOR_64, image -> color_format);
for (p = 0, ref = 0; p <= image -> max_palette_index; p ++)
if (used[p]) {
map[p] = ref;
Expand Down
8 changes: 3 additions & 5 deletions src/pngcompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
unsigned char * compress_PNG_data (struct context * context, const unsigned char * restrict data, size_t size, size_t extra, size_t * restrict output_size) {
// extra is the number of zero bytes inserted before the compressed data; they are not included in the size
unsigned char * output = ctxmalloc(context, extra + 8); // two bytes extra to handle leftover bits in dataword
size_t p, inoffset = 0, outoffset = extra + 2;
memset(output, 0, extra);
output[extra] = 0x78;
output[extra + 1] = 0x5e;
size_t p, inoffset = 0, outoffset = extra + byteappend(output + extra, 0x78, 0x5e);
uint16_t * references = ctxmalloc(context, 0x8000u * PNG_MAX_LOOKBACK_COUNT * sizeof *references);
for (p = 0; p < (0x8000u * PNG_MAX_LOOKBACK_COUNT); p ++) references[p] = 0xffffu;
uint32_t dataword = 0;
Expand Down Expand Up @@ -70,7 +68,7 @@ struct compressed_PNG_code * generate_compressed_PNG_block (struct context * con
if (length = find_PNG_reference(data, references, current_offset, size, &backref)) {
// we found a matching back reference, so emit any pending literals and the reference
for (; literals; literals --) emit_PNG_code(context, &codes, &allocated, count, data[current_offset - literals], 0);
emit_PNG_code(context, &codes, &allocated, count, -length, current_offset - backref);
emit_PNG_code(context, &codes, &allocated, count, -(int) length, current_offset - backref);
score -= length - 1;
if (score < 0) score = 0;
for (; length; length --) append_PNG_reference(data, current_offset ++, references);
Expand Down Expand Up @@ -121,7 +119,7 @@ unsigned find_PNG_reference (const unsigned char * data, const uint16_t * refere
size_t backref, found;
unsigned p, length, best = 0;
for (p = 0; (p < PNG_MAX_LOOKBACK_COUNT) && (references[search + p] != 0xffffu); p ++) {
backref = (current_offset & ~(size_t) 0x7fff) | references[search + p];
backref = (current_offset & bitnegate(0x7fff)) | references[search + p];
if (backref >= current_offset)
if (current_offset < 0x8000u)
continue;
Expand Down

0 comments on commit 121d9be

Please sign in to comment.