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 static int max_width, max_height;
33 =item i_set_image_file_limits(width, height, bytes)
36 =synopsis i_set_image_file_limits(500, 500, 1000000);
38 Set limits on the sizes of images read by Imager.
40 Setting a limit to 0 means that limit is ignored.
42 Negative limits result in failure.
44 Returns non-zero on success.
50 i_set_image_file_limits(int width, int height, int bytes) {
54 i_push_error(0, "width must be non-negative");
58 i_push_error(0, "height must be non-negative");
62 i_push_error(0, "bytes must be non-negative");
74 =item i_get_image_file_limits(&width, &height, &bytes)
77 =synopsis i_get_image_file_limits(&width, &height, &bytes)
79 Retrieves the file limits set by i_set_image_file_limits().
85 i_get_image_file_limits(int *width, int *height, int *bytes) {
96 =item i_int_check_image_file_limits(width, height, channels, sample_size)
99 =synopsis i_i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))
101 Checks the size of a file in memory against the configured image file
104 This also range checks the values to those permitted by Imager and
105 checks for overflows in calculating the size.
107 Returns non-zero if the file is within limits.
109 This function is intended to be called by image file read functions.
115 i_int_check_image_file_limits(int width, int height, int channels, int sample_size) {
120 i_push_errorf(0, "file size limit - image width of %d is not positive",
124 if (max_width && width > max_width) {
125 i_push_errorf(0, "file size limit - image width of %d exceeds limit of %d",
131 i_push_errorf(0, "file size limit - image height %d is not positive",
136 if (max_height && height > max_height) {
137 i_push_errorf(0, "file size limit - image height of %d "
138 "exceeds limit of %d", height, max_height);
142 if (channels < 1 || channels > MAXCHANNELS) {
143 i_push_errorf(0, "file size limit - channels %d out of range",
148 if (sample_size < 1 || sample_size > sizeof(long double)) {
149 i_push_errorf(0, "file size limit - sample_size %d out of range",
154 /* This overflow check is a bit more paranoid than usual.
155 We don't protect it under max_bytes since we always want to check
158 bytes = width * height * channels * sample_size;
159 if (bytes / width != height * channels * sample_size
160 || bytes / height != width * channels * sample_size) {
161 i_push_error(0, "file size limit - integer overflow calculating storage");
165 if (bytes > max_bytes) {
166 i_push_errorf(0, "file size limit - storage size of %d "
167 "exceeds limit of %d", bytes, max_bytes);