ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
image.c 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

Definition in file util/image.c.

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, bytes_per_pixel);
47 }
48
49 *out_size = pixel_count * bytes_per_pixel;
50 return ASCIICHAT_OK;
51}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ 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}
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24

References ASCIICHAT_OK, ERROR_INVALID_PARAM, height, SET_ERRNO, and width.

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 53 of file util/image.c.

53 {
54 if (!out_size) {
55 return SET_ERRNO(ERROR_INVALID_PARAM, "out_size is NULL");
56 }
57
58 // Calculate pixel count first
59 size_t pixel_count;
61 if (err != ASCIICHAT_OK) {
62 return err;
63 }
64
65 // Calculate size with 3 bytes per pixel (RGB)
66 return image_calc_pixel_buffer_size(pixel_count, 3, out_size);
67}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
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, height, image_calc_pixel_buffer_size(), image_calc_pixel_count(), SET_ERRNO, and width.

Referenced by handle_image_frame_packet(), and session_host_inject_frame().

◆ 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 69 of file util/image.c.

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

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

◆ 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 111 of file util/image.c.

111 {
112 if (requested_size > IMAGE_MAX_PIXELS_SIZE) {
113 return SET_ERRNO(ERROR_INVALID_PARAM, "Image buffer size exceeds maximum: %zu > %zu bytes", requested_size, IMAGE_MAX_PIXELS_SIZE);
114 }
115
116 return ASCIICHAT_OK;
117}
#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 97 of file util/image.c.

97 {
98 // Check for zero dimensions
99 if (width == 0 || height == 0) {
100 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions must be non-zero: %zu x %zu", width, height);
101 }
102
103 // Check against maximum allowed dimensions
105 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions exceed maximum: %zu x %zu (max %u x %u)", width, height, IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT);
106 }
107
108 return ASCIICHAT_OK;
109}
#define IMAGE_MAX_WIDTH
An owned RGB image with dimensions and row-major pixel storage.
#define IMAGE_MAX_HEIGHT
Maximum image height (4K resolution).

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

Referenced by handle_image_frame_packet(), image_new(), and session_host_inject_frame().