ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
image.h File Reference

🖼️ Safe overflow-checked buffer size calculations for images and video frames More...

Go to the source code of this file.

Functions

asciichat_error_t image_calc_pixel_count (size_t width, size_t height, size_t *out_pixel_count)
 Calculate pixel count with overflow checking.
 
asciichat_error_t image_calc_pixel_buffer_size (size_t pixel_count, size_t bytes_per_pixel, size_t *out_size)
 Calculate pixel buffer size with overflow checking.
 
asciichat_error_t image_calc_rgb_size (size_t width, size_t height, size_t *out_size)
 Calculate total RGB buffer size from dimensions.
 
asciichat_error_t image_calc_total_allocation (size_t width, size_t height, size_t struct_size, size_t bytes_per_pixel, size_t *out_size)
 Calculate combined size of image struct + pixel buffer.
 
asciichat_error_t image_validate_dimensions (size_t width, size_t height)
 Validate image dimensions (non-zero, within limits)
 
asciichat_error_t image_validate_buffer_size (size_t requested_size)
 Validate buffer size against maximum allocation limit.
 

Detailed Description

🖼️ Safe overflow-checked buffer size calculations for images and video frames

Provides helpers for calculating buffer sizes with proper overflow detection. All functions return error codes and set errno for detailed error context.

Definition in file util/image.h.

Function Documentation

◆ image_calc_pixel_buffer_size()

asciichat_error_t image_calc_pixel_buffer_size ( size_t  pixel_count,
size_t  bytes_per_pixel,
size_t *  out_size 
)

Calculate pixel buffer size with overflow checking.

Parameters
pixel_countNumber of pixels
bytes_per_pixelSize of each pixel (e.g., 3 for RGB, 4 for RGBA)
out_sizeOutput: calculated buffer size in bytes
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM on overflow

Safe wrapper for: size = pixel_count * bytes_per_pixel Prevents integer overflow by checking before multiplication

Definition at line 31 of file util/image.c.

31 {
32 if (!out_size) {
33 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
34 }
35
36 if (pixel_count == 0) {
37 return SET_ERRNO(ERROR_INVALID_PARAM, "Pixel count must be non-zero");
38 }
39
40 if (bytes_per_pixel == 0) {
41 return SET_ERRNO(ERROR_INVALID_PARAM, "bytes_per_pixel must be non-zero");
42 }
43
44 // Check overflow before multiplication
45 if (pixel_count > SIZE_MAX / bytes_per_pixel) {
46 return SET_ERRNO(ERROR_INVALID_PARAM, "Pixel buffer too large (would overflow): %zu pixels * %zu bpp", pixel_count,
47 bytes_per_pixel);
48 }
49
50 *out_size = pixel_count * bytes_per_pixel;
51 return ASCIICHAT_OK;
52}
#define SET_ERRNO(code, context_msg,...)
Set error code with custom context message and log it, returning the error code.
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM

References ASCIICHAT_OK, ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by image_calc_rgb_size(), and image_calc_total_allocation().

◆ image_calc_pixel_count()

asciichat_error_t image_calc_pixel_count ( size_t  width,
size_t  height,
size_t *  out_pixel_count 
)

Calculate pixel count with overflow checking.

Parameters
widthImage width (pixels)
heightImage height (pixels)
out_pixel_countOutput: calculated pixel count
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM on overflow

Safe wrapper for: pixel_count = width * height Prevents integer overflow by checking before multiplication

Definition at line 12 of file util/image.c.

12 {
13 if (!out_pixel_count) {
14 return SET_ERRNO(ERROR_INVALID_PARAM, "out_pixel_count is NULL");
15 }
16
17 // Check for zero dimensions
18 if (width == 0 || height == 0) {
19 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions must be non-zero: %zu x %zu", width, height);
20 }
21
22 // Check overflow before multiplication
23 if (height > SIZE_MAX / width) {
24 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions too large (would overflow): %zu x %zu", width, height);
25 }
26
27 *out_pixel_count = width * height;
28 return ASCIICHAT_OK;
29}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, and SET_ERRNO.

Referenced by image_calc_rgb_size(), and image_calc_total_allocation().

◆ image_calc_rgb_size()

asciichat_error_t image_calc_rgb_size ( size_t  width,
size_t  height,
size_t *  out_size 
)

Calculate total RGB buffer size from dimensions.

Parameters
widthImage width (pixels)
heightImage height (pixels)
out_sizeOutput: total buffer size (width * height * 3 bytes)
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM on overflow

Convenience function for RGB images (3 bytes per pixel). Safe wrapper for: size = width * height * 3

Definition at line 54 of file util/image.c.

54 {
55 if (!out_size) {
56 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
57 }
58
59 // Calculate pixel count first
60 size_t pixel_count;
61 asciichat_error_t err = image_calc_pixel_count(width, height, &pixel_count);
62 if (err != ASCIICHAT_OK) {
63 return err;
64 }
65
66 // Calculate size with 3 bytes per pixel (RGB)
67 return image_calc_pixel_buffer_size(pixel_count, 3, out_size);
68}
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
asciichat_error_t image_calc_pixel_buffer_size(size_t pixel_count, size_t bytes_per_pixel, size_t *out_size)
Calculate pixel buffer size with overflow checking.
Definition util/image.c:31
asciichat_error_t image_calc_pixel_count(size_t width, size_t height, size_t *out_pixel_count)
Calculate pixel count with overflow checking.
Definition util/image.c:12

References ASCIICHAT_OK, ERROR_INVALID_PARAM, image_calc_pixel_buffer_size(), image_calc_pixel_count(), and SET_ERRNO.

Referenced by handle_image_frame_packet().

◆ image_calc_total_allocation()

asciichat_error_t image_calc_total_allocation ( size_t  width,
size_t  height,
size_t  struct_size,
size_t  bytes_per_pixel,
size_t *  out_size 
)

Calculate combined size of image struct + pixel buffer.

Parameters
widthImage width (pixels)
heightImage height (pixels)
struct_sizeSize of image_t structure (typically sizeof(image_t))
bytes_per_pixelSize of each pixel
out_sizeOutput: total allocation size
Returns
ASCIICHAT_OK on success, ERROR_INVALID_PARAM on overflow

Used for pool allocations where struct and pixels are contiguous. Safe calculation of: struct_size + (width * height * bytes_per_pixel)

Definition at line 70 of file util/image.c.

71 {
72 if (!out_size) {
73 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
74 }
75
76 // Calculate pixel count first with overflow checking
77 size_t pixel_count;
78 asciichat_error_t err = image_calc_pixel_count(width, height, &pixel_count);
79 if (err != ASCIICHAT_OK) {
80 return err;
81 }
82
83 // Calculate pixel buffer size
84 size_t pixel_buffer_size;
85 err = image_calc_pixel_buffer_size(pixel_count, bytes_per_pixel, &pixel_buffer_size);
86 if (err != ASCIICHAT_OK) {
87 return err;
88 }
89
90 // Check overflow when adding struct size
91 if (pixel_buffer_size > SIZE_MAX - struct_size) {
92 return SET_ERRNO(ERROR_INVALID_PARAM, "Total allocation size would overflow: struct=%zu + pixels=%zu", struct_size,
93 pixel_buffer_size);
94 }
95
96 *out_size = struct_size + pixel_buffer_size;
97 return ASCIICHAT_OK;
98}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, image_calc_pixel_buffer_size(), image_calc_pixel_count(), and SET_ERRNO.

◆ image_validate_buffer_size()

asciichat_error_t image_validate_buffer_size ( size_t  requested_size)

Validate buffer size against maximum allocation limit.

Parameters
requested_sizeRequested buffer size
Returns
ASCIICHAT_OK if valid, ERROR_INVALID_PARAM if exceeds maximum

Checks that requested_size <= IMAGE_MAX_PIXELS_SIZE Sets errno with detailed context if validation fails

Definition at line 115 of file util/image.c.

115 {
116 if (requested_size > IMAGE_MAX_PIXELS_SIZE) {
117 return SET_ERRNO(ERROR_INVALID_PARAM, "Image buffer size exceeds maximum: %zu > %zu bytes", requested_size,
119 }
120
121 return ASCIICHAT_OK;
122}
#define IMAGE_MAX_PIXELS_SIZE
Maximum pixel data size in bytes.

References ASCIICHAT_OK, ERROR_INVALID_PARAM, IMAGE_MAX_PIXELS_SIZE, and SET_ERRNO.

Referenced by handle_image_frame_packet().

◆ image_validate_dimensions()

asciichat_error_t image_validate_dimensions ( size_t  width,
size_t  height 
)

Validate image dimensions (non-zero, within limits)

Parameters
widthImage width to validate
heightImage height to validate
Returns
ASCIICHAT_OK if valid, ERROR_INVALID_PARAM if invalid

Checks that:

  • Both width and height are non-zero
  • Dimensions don't exceed IMAGE_MAX_WIDTH and IMAGE_MAX_HEIGHT Sets errno with detailed context if validation fails

Definition at line 12 of file video.c.

12 {
13 if (width > 0 && height > 0 && width <= 10000 && height <= 10000) {
14 return ASCIICHAT_OK;
15 }
17}

References ASCIICHAT_OK, ERROR_INVALID_PARAM, IMAGE_MAX_HEIGHT, IMAGE_MAX_WIDTH, and SET_ERRNO.

Referenced by handle_image_frame_packet(), and image_new().