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.
30 =item i_set_image_file_limits(width, height, bytes)
33 =synopsis i_set_image_file_limits(500, 500, 1000000);
35 Set limits on the sizes of images read by Imager.
37 Setting a limit to 0 means that limit is ignored.
39 Negative limits result in failure.
47 i_img_dim width, height - maximum width and height.
51 size_t bytes - maximum size in memory in bytes. A value of zero sets
52 this limit to one gigabyte.
56 Returns non-zero on success.
62 im_set_image_file_limits(im_context_t ctx, i_img_dim width, i_img_dim height, size_t bytes) {
66 i_push_error(0, "width must be non-negative");
70 i_push_error(0, "height must be non-negative");
74 i_push_error(0, "bytes must be non-negative");
78 ctx->max_width = width;
79 ctx->max_height = height;
80 ctx->max_bytes = bytes ? bytes : DEF_BYTES_LIMIT;
86 =item i_get_image_file_limits(&width, &height, &bytes)
89 =synopsis i_get_image_file_limits(&width, &height, &bytes)
91 Retrieves the file limits set by i_set_image_file_limits().
97 i_img_dim *width, *height - the maximum width and height of the image.
101 size_t *bytes - size in memory of the image in bytes.
109 im_get_image_file_limits(im_context_t ctx, i_img_dim *width, i_img_dim *height, size_t *bytes) {
112 *width = ctx->max_width;
113 *height = ctx->max_height;
114 *bytes = ctx->max_bytes;
120 =item i_int_check_image_file_limits(width, height, channels, sample_size)
123 =synopsis i_i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))
125 Checks the size of a file in memory against the configured image file
128 This also range checks the values to those permitted by Imager and
129 checks for overflows in calculating the size.
131 Returns non-zero if the file is within limits.
133 This function is intended to be called by image file read functions.
139 im_int_check_image_file_limits(im_context_t ctx, i_img_dim width, i_img_dim height, int channels, size_t sample_size) {
144 im_push_errorf(ctx, 0, "file size limit - image width of %" i_DF " is not positive",
148 if (ctx->max_width && width > ctx->max_width) {
149 im_push_errorf(ctx, 0, "file size limit - image width of %" i_DF " exceeds limit of %" i_DF,
150 i_DFc(width), i_DFc(ctx->max_width));
155 im_push_errorf(ctx, 0, "file size limit - image height of %" i_DF " is not positive",
160 if (ctx->max_height && height > ctx->max_height) {
161 im_push_errorf(ctx, 0, "file size limit - image height of %" i_DF
162 " exceeds limit of %" i_DF, i_DFc(height), i_DFc(ctx->max_height));
166 if (channels < 1 || channels > MAXCHANNELS) {
167 im_push_errorf(ctx, 0, "file size limit - channels %d out of range",
172 if (sample_size < 1 || sample_size > sizeof(long double)) {
173 im_push_errorf(ctx, 0, "file size limit - sample_size %ld out of range",
178 /* This overflow check is a bit more paranoid than usual.
179 We don't protect it under max_bytes since we always want to check
182 bytes = width * height * channels * sample_size;
183 if (bytes / width != height * channels * sample_size
184 || bytes / height != width * channels * sample_size) {
185 i_push_error(0, "file size limit - integer overflow calculating storage");
188 if (ctx->max_bytes) {
189 if (bytes > ctx->max_bytes) {
190 im_push_errorf(ctx, 0, "file size limit - storage size of %lu "
191 "exceeds limit of %lu", (unsigned long)bytes,
192 (unsigned long)ctx->max_bytes);