4 limits.c - manages data/functions for limiting the sizes of images read from files.
9 if (!i_set_image_file_limits(max_width, max_height, max_bytes)) {
12 i_get_image_file_limits(&max_width, &max_height, &max_bytes);
14 // file reader implementations
15 if (!i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))) {
21 Manage limits for image files read by Imager.
23 Setting a value of zero means that limit will be ignored.
29 #define DEF_BYTES_LIMIT 0x40000000
31 static i_img_dim max_width, max_height;
32 static size_t max_bytes = DEF_BYTES_LIMIT;
35 =item i_set_image_file_limits(width, height, bytes)
38 =synopsis i_set_image_file_limits(500, 500, 1000000);
40 Set limits on the sizes of images read by Imager.
42 Setting a limit to 0 means that limit is ignored.
44 Negative limits result in failure.
52 i_img_dim width, height - maximum width and height.
56 size_t bytes - maximum size in memory in bytes. A value of zero sets
57 this limit to one gigabyte.
61 Returns non-zero on success.
67 i_set_image_file_limits(i_img_dim width, i_img_dim height, size_t bytes) {
71 i_push_error(0, "width must be non-negative");
75 i_push_error(0, "height must be non-negative");
79 i_push_error(0, "bytes must be non-negative");
85 max_bytes = bytes ? bytes : DEF_BYTES_LIMIT;
91 =item i_get_image_file_limits(&width, &height, &bytes)
94 =synopsis i_get_image_file_limits(&width, &height, &bytes)
96 Retrieves the file limits set by i_set_image_file_limits().
102 i_img_dim *width, *height - the maximum width and height of the image.
106 size_t *bytes - size in memory of the image in bytes.
114 i_get_image_file_limits(i_img_dim *width, i_img_dim *height, size_t *bytes) {
118 *height = max_height;
125 =item i_int_check_image_file_limits(width, height, channels, sample_size)
128 =synopsis i_i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))
130 Checks the size of a file in memory against the configured image file
133 This also range checks the values to those permitted by Imager and
134 checks for overflows in calculating the size.
136 Returns non-zero if the file is within limits.
138 This function is intended to be called by image file read functions.
144 i_int_check_image_file_limits(i_img_dim width, i_img_dim height, int channels, size_t sample_size) {
149 i_push_errorf(0, "file size limit - image width of %" i_DF " is not positive",
153 if (max_width && width > max_width) {
154 i_push_errorf(0, "file size limit - image width of %" i_DF " exceeds limit of %" i_DF,
155 i_DFc(width), i_DFc(max_width));
160 i_push_errorf(0, "file size limit - image height %" i_DF " is not positive",
165 if (max_height && height > max_height) {
166 i_push_errorf(0, "file size limit - image height of %" i_DF
167 " exceeds limit of %" i_DF, i_DFc(height), i_DFc(max_height));
171 if (channels < 1 || channels > MAXCHANNELS) {
172 i_push_errorf(0, "file size limit - channels %d out of range",
177 if (sample_size < 1 || sample_size > sizeof(long double)) {
178 i_push_errorf(0, "file size limit - sample_size %ld out of range",
183 /* This overflow check is a bit more paranoid than usual.
184 We don't protect it under max_bytes since we always want to check
187 bytes = width * height * channels * sample_size;
188 if (bytes / width != height * channels * sample_size
189 || bytes / height != width * channels * sample_size) {
190 i_push_error(0, "file size limit - integer overflow calculating storage");
194 if (bytes > max_bytes) {
195 i_push_errorf(0, "file size limit - storage size of %lu "
196 "exceeds limit of %lu", (unsigned long)bytes,
197 (unsigned long)max_bytes);