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 im_set_image_file_limits(ctx, width, height, bytes)
32 X<im_set_image_file_limits API>X<i_set_image_file_limits API>
34 =synopsis im_set_image_file_limits(aIMCTX, 500, 500, 1000000);
35 =synopsis i_set_image_file_limits(500, 500, 1000000);
37 Set limits on the sizes of images read by Imager.
39 Setting a limit to 0 means that limit is ignored.
41 Negative limits result in failure.
49 i_img_dim width, height - maximum width and height.
53 size_t bytes - maximum size in memory in bytes. A value of zero sets
54 this limit to one gigabyte.
58 Returns non-zero on success.
60 Also callable as C<i_set_image_file_limits(width, height, bytes)>.
66 im_set_image_file_limits(pIMCTX, i_img_dim width, i_img_dim height, size_t bytes) {
70 i_push_error(0, "width must be non-negative");
74 i_push_error(0, "height must be non-negative");
78 i_push_error(0, "bytes must be non-negative");
82 aIMCTX->max_width = width;
83 aIMCTX->max_height = height;
84 aIMCTX->max_bytes = bytes ? bytes : DEF_BYTES_LIMIT;
90 =item im_get_image_file_limits(ctx, &width, &height, &bytes)
91 X<im_get_image_file_limits API>X<i_get_image_file_limits>
93 =synopsis im_get_image_file_limits(aIMCTX, &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.
110 Also callable as C<i_get_image_file_limits(&width, &height, &bytes)>.
116 im_get_image_file_limits(pIMCTX, i_img_dim *width, i_img_dim *height, size_t *bytes) {
117 im_clear_error(aIMCTX);
119 *width = aIMCTX->max_width;
120 *height = aIMCTX->max_height;
121 *bytes = aIMCTX->max_bytes;
127 =item im_int_check_image_file_limits(width, height, channels, sample_size)
128 X<im_int_check_image_file_limits API>X<i_int_check_image_file_limits>
130 =synopsis im_int_check_image_file_limits(aIMCTX, width, height, channels, sizeof(i_sample_t))
131 =synopsis i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))
133 Checks the size of a file in memory against the configured image file
136 This also range checks the values to those permitted by Imager and
137 checks for overflows in calculating the size.
139 Returns non-zero if the file is within limits.
141 This function is intended to be called by image file read functions.
143 Also callable as C<i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t)>.
149 im_int_check_image_file_limits(pIMCTX, i_img_dim width, i_img_dim height, int channels, size_t sample_size) {
151 im_clear_error(aIMCTX);
154 im_push_errorf(aIMCTX, 0, "file size limit - image width of %" i_DF " is not positive",
158 if (aIMCTX->max_width && width > aIMCTX->max_width) {
159 im_push_errorf(aIMCTX, 0, "file size limit - image width of %" i_DF " exceeds limit of %" i_DF,
160 i_DFc(width), i_DFc(aIMCTX->max_width));
165 im_push_errorf(aIMCTX, 0, "file size limit - image height of %" i_DF " is not positive",
170 if (aIMCTX->max_height && height > aIMCTX->max_height) {
171 im_push_errorf(aIMCTX, 0, "file size limit - image height of %" i_DF
172 " exceeds limit of %" i_DF, i_DFc(height), i_DFc(aIMCTX->max_height));
176 if (channels < 1 || channels > MAXCHANNELS) {
177 im_push_errorf(aIMCTX, 0, "file size limit - channels %d out of range",
182 if (sample_size < 1 || sample_size > sizeof(long double)) {
183 im_push_errorf(aIMCTX, 0, "file size limit - sample_size %ld out of range",
188 /* This overflow check is a bit more paranoid than usual.
189 We don't protect it under max_bytes since we always want to check
192 bytes = width * height * channels * sample_size;
193 if (bytes / width != height * channels * sample_size
194 || bytes / height != width * channels * sample_size) {
195 im_push_error(aIMCTX, 0, "file size limit - integer overflow calculating storage");
198 if (aIMCTX->max_bytes) {
199 if (bytes > aIMCTX->max_bytes) {
200 im_push_errorf(aIMCTX, 0, "file size limit - storage size of %lu "
201 "exceeds limit of %lu", (unsigned long)bytes,
202 (unsigned long)aIMCTX->max_bytes);