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.
27 #define IMAGER_NO_CONTEXT
31 =item i_set_image_file_limits(width, height, bytes)
34 =synopsis i_set_image_file_limits(500, 500, 1000000);
36 Set limits on the sizes of images read by Imager.
38 Setting a limit to 0 means that limit is ignored.
40 Negative limits result in failure.
48 i_img_dim width, height - maximum width and height.
52 size_t bytes - maximum size in memory in bytes. A value of zero sets
53 this limit to one gigabyte.
57 Returns non-zero on success.
63 im_set_image_file_limits(pIMCTX, i_img_dim width, i_img_dim height, size_t bytes) {
67 i_push_error(0, "width must be non-negative");
71 i_push_error(0, "height must be non-negative");
75 i_push_error(0, "bytes must be non-negative");
79 aIMCTX->max_width = width;
80 aIMCTX->max_height = height;
81 aIMCTX->max_bytes = bytes ? bytes : DEF_BYTES_LIMIT;
87 =item i_get_image_file_limits(&width, &height, &bytes)
90 =synopsis i_get_image_file_limits(&width, &height, &bytes)
92 Retrieves the file limits set by i_set_image_file_limits().
98 i_img_dim *width, *height - the maximum width and height of the image.
102 size_t *bytes - size in memory of the image in bytes.
110 im_get_image_file_limits(pIMCTX, i_img_dim *width, i_img_dim *height, size_t *bytes) {
111 im_clear_error(aIMCTX);
113 *width = aIMCTX->max_width;
114 *height = aIMCTX->max_height;
115 *bytes = aIMCTX->max_bytes;
121 =item i_int_check_image_file_limits(width, height, channels, sample_size)
124 =synopsis i_i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))
126 Checks the size of a file in memory against the configured image file
129 This also range checks the values to those permitted by Imager and
130 checks for overflows in calculating the size.
132 Returns non-zero if the file is within limits.
134 This function is intended to be called by image file read functions.
140 im_int_check_image_file_limits(pIMCTX, i_img_dim width, i_img_dim height, int channels, size_t sample_size) {
142 im_clear_error(aIMCTX);
145 im_push_errorf(aIMCTX, 0, "file size limit - image width of %" i_DF " is not positive",
149 if (aIMCTX->max_width && width > aIMCTX->max_width) {
150 im_push_errorf(aIMCTX, 0, "file size limit - image width of %" i_DF " exceeds limit of %" i_DF,
151 i_DFc(width), i_DFc(aIMCTX->max_width));
156 im_push_errorf(aIMCTX, 0, "file size limit - image height of %" i_DF " is not positive",
161 if (aIMCTX->max_height && height > aIMCTX->max_height) {
162 im_push_errorf(aIMCTX, 0, "file size limit - image height of %" i_DF
163 " exceeds limit of %" i_DF, i_DFc(height), i_DFc(aIMCTX->max_height));
167 if (channels < 1 || channels > MAXCHANNELS) {
168 im_push_errorf(aIMCTX, 0, "file size limit - channels %d out of range",
173 if (sample_size < 1 || sample_size > sizeof(long double)) {
174 im_push_errorf(aIMCTX, 0, "file size limit - sample_size %ld out of range",
179 /* This overflow check is a bit more paranoid than usual.
180 We don't protect it under max_bytes since we always want to check
183 bytes = width * height * channels * sample_size;
184 if (bytes / width != height * channels * sample_size
185 || bytes / height != width * channels * sample_size) {
186 im_push_error(aIMCTX, 0, "file size limit - integer overflow calculating storage");
189 if (aIMCTX->max_bytes) {
190 if (bytes > aIMCTX->max_bytes) {
191 im_push_errorf(aIMCTX, 0, "file size limit - storage size of %lu "
192 "exceeds limit of %lu", (unsigned long)bytes,
193 (unsigned long)aIMCTX->max_bytes);