Fix buffer overflow and properly init pixel array
This commit is contained in:
parent
68254ac452
commit
1c2f37ede6
|
|
@ -1 +1,2 @@
|
|||
a.out
|
||||
*.bmp
|
||||
|
|
|
|||
17
lib/bmp.c
17
lib/bmp.c
|
|
@ -5,16 +5,20 @@
|
|||
#include "bmp.h"
|
||||
|
||||
uint32_t calc_bitmap_pixel_array_size_bytes(bitmap *bitmap_in) {
|
||||
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 */
|
||||
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 bits */
|
||||
uint32_t bytes_unpadded = (bitmap_in->image_width * bitmap_in->image_height * bitmap_in->bits_per_pixel) / 8;
|
||||
return bytes_unpadded + (bytes_unpadded % 8); /* Add padding to ensure 4 byte allignment */
|
||||
};
|
||||
|
||||
uint32_t calc_bitmap_file_size_bytes(bitmap *bitmap_in) {
|
||||
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 */
|
||||
/* We will assume that the bitmap bits per pixel is always a multiple of 8 and sizeof byte is always 8 bits */
|
||||
return sizeof(bitmap) + calc_bitmap_pixel_array_size_bytes(bitmap_in) - 2 ; /* Take away two to account for compiler padding struct */
|
||||
};
|
||||
|
||||
uint32_t calc_bitmap_header_size_bytes() {
|
||||
return sizeof(bitmap) - 2 ; /* Take away two to account for compiler padding struct */
|
||||
};
|
||||
|
||||
bitmap init_bitmap(int32_t image_width_in, int32_t image_height_in) {
|
||||
bitmap new_bitmap;
|
||||
/* Bitmap Info Header */
|
||||
|
|
@ -44,12 +48,17 @@ bitmap init_bitmap(int32_t image_width_in, int32_t image_height_in) {
|
|||
|
||||
int write_to_bitmap(bitmap *bitmap_in, char *filename) {
|
||||
int8_t write_status;
|
||||
uint32_t current_byte;
|
||||
uint8_t blank_byte_buffer[1] = { 0 };
|
||||
uint8_t *bitmap_in_byte_ptr = (uint8_t *)bitmap_in + 2; /* Offset by two to account for padding */
|
||||
int8_t fd = open(filename, O_WRONLY | O_CREAT);
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
/* This is writing out of bounds (bitmap < bitmap_file_size), please fix */
|
||||
write_status = write(fd, bitmap_in_byte_ptr, calc_bitmap_file_size_bytes(bitmap_in));
|
||||
/* Write Header */
|
||||
write_status = write(fd, bitmap_in_byte_ptr, calc_bitmap_header_size_bytes());
|
||||
/* Write blank pixel array */
|
||||
for (current_byte = bitmap_in->image_data_start_offset; current_byte < calc_bitmap_file_size_bytes(bitmap_in); ++current_byte)
|
||||
write_status = pwrite(fd, blank_byte_buffer, 1, current_byte);
|
||||
if (fd == -1) {
|
||||
close(fd);
|
||||
return -1;
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ bitmap init_bitmap(int32_t image_width_in, int32_t image_height_in);
|
|||
|
||||
uint32_t calc_bitmap_file_size_bytes(bitmap *bitmap_in);
|
||||
uint32_t calc_bitmap_pixel_array_size_bytes(bitmap *bitmap_in);
|
||||
uint32_t calc_bitmap_header_size_bytes();
|
||||
|
||||
int write_to_bitmap(bitmap *bitmap_in, char *filename);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue