ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
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)
 
asciichat_error_t image_calc_pixel_buffer_size (size_t pixel_count, size_t bytes_per_pixel, size_t *out_size)
 
asciichat_error_t image_calc_rgb_size (size_t width, size_t height, size_t *out_size)
 
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)
 
asciichat_error_t image_validate_dimensions (size_t width, size_t height)
 
asciichat_error_t image_validate_buffer_size (size_t requested_size)
 

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 
)

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}

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 
)

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}

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 
)

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 image_calc_pixel_buffer_size(size_t pixel_count, size_t bytes_per_pixel, size_t *out_size)
Definition util/image.c:31
asciichat_error_t image_calc_pixel_count(size_t width, size_t height, size_t *out_pixel_count)
Definition util/image.c:12

References image_calc_pixel_buffer_size(), and image_calc_pixel_count().

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 
)

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 image_calc_pixel_buffer_size(), and image_calc_pixel_count().

◆ image_validate_buffer_size()

asciichat_error_t image_validate_buffer_size ( size_t  requested_size)

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,
118 IMAGE_MAX_PIXELS_SIZE);
119 }
120
121 return ASCIICHAT_OK;
122}

Referenced by handle_image_frame_packet().

◆ image_validate_dimensions()

asciichat_error_t image_validate_dimensions ( size_t  width,
size_t  height 
)

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

100 {
101 // Check for zero dimensions
102 if (width == 0 || height == 0) {
103 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions must be non-zero: %zu x %zu", width, height);
104 }
105
106 // Check against maximum allowed dimensions
107 if (width > IMAGE_MAX_WIDTH || height > IMAGE_MAX_HEIGHT) {
108 return SET_ERRNO(ERROR_INVALID_PARAM, "Image dimensions exceed maximum: %zu x %zu (max %u x %u)", width, height,
109 IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT);
110 }
111
112 return ASCIICHAT_OK;
113}