]> git.imager.perl.org - imager.git/blob - limits.c
update sub-module READMEs
[imager.git] / limits.c
1 /*
2 =head1 NAME
3
4 limits.c - manages data/functions for limiting the sizes of images read from files.
5
6 =head1 SYNOPSIS
7
8   // user code
9   if (!i_set_image_file_limits(max_width, max_height, max_bytes)) {
10     // error
11   }
12   i_get_image_file_limits(&max_width, &max_height, &max_bytes);
13
14   // file reader implementations
15   if (!i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))) {
16     // error handling
17   }
18
19 =head1 DESCRIPTION
20
21 Manage limits for image files read by Imager.
22
23 Setting a value of zero means that limit will be ignored.
24   
25  */
26
27 #define IMAGER_NO_CONTEXT
28 #include "imageri.h"
29
30 /*
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>
33 =category Files
34 =synopsis im_set_image_file_limits(aIMCTX, 500, 500, 1000000);
35 =synopsis i_set_image_file_limits(500, 500, 1000000);
36
37 Set limits on the sizes of images read by Imager.
38
39 Setting a limit to 0 means that limit is ignored.
40
41 Negative limits result in failure.
42
43 Parameters:
44
45 =over
46
47 =item *
48
49 i_img_dim width, height - maximum width and height.
50
51 =item *
52
53 size_t bytes - maximum size in memory in bytes.  A value of zero sets
54 this limit to one gigabyte.
55
56 =back
57
58 Returns non-zero on success.
59
60 Also callable as C<i_set_image_file_limits(width, height, bytes)>.
61
62 =cut
63 */
64
65 int
66 im_set_image_file_limits(pIMCTX, i_img_dim width, i_img_dim height, size_t bytes) {
67   i_clear_error();
68
69   if (width < 0) {
70     i_push_error(0, "width must be non-negative");
71     return 0;
72   }
73   if (height < 0) {
74     i_push_error(0, "height must be non-negative");
75     return 0;
76   }
77   if (bytes < 0) {
78     i_push_error(0, "bytes must be non-negative");
79     return 0;
80   }
81
82   aIMCTX->max_width = width;
83   aIMCTX->max_height = height;
84   aIMCTX->max_bytes = bytes ? bytes : DEF_BYTES_LIMIT;
85
86   return 1;
87 }
88
89 /*
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>
92 =category Files
93 =synopsis im_get_image_file_limits(aIMCTX, &width, &height, &bytes)
94 =synopsis i_get_image_file_limits(&width, &height, &bytes)
95
96 Retrieves the file limits set by i_set_image_file_limits().
97
98 =over
99
100 =item *
101
102 i_img_dim *width, *height - the maximum width and height of the image.
103
104 =item *
105
106 size_t *bytes - size in memory of the image in bytes.
107
108 =back
109
110 Also callable as C<i_get_image_file_limits(&width, &height, &bytes)>.
111
112 =cut
113 */
114
115 int
116 im_get_image_file_limits(pIMCTX, i_img_dim *width, i_img_dim *height, size_t *bytes) {
117   im_clear_error(aIMCTX);
118
119   *width = aIMCTX->max_width;
120   *height = aIMCTX->max_height;
121   *bytes = aIMCTX->max_bytes;
122
123   return 1;
124 }
125
126 /*
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>
129 =category Files
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))
132
133 Checks the size of a file in memory against the configured image file
134 limits.
135
136 This also range checks the values to those permitted by Imager and
137 checks for overflows in calculating the size.
138
139 Returns non-zero if the file is within limits.
140
141 This function is intended to be called by image file read functions.
142
143 Also callable as C<i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t)>.
144
145 =cut
146 */
147
148 int
149 im_int_check_image_file_limits(pIMCTX, i_img_dim width, i_img_dim height, int channels, size_t sample_size) {
150   size_t bytes;
151   im_clear_error(aIMCTX);
152   
153   if (width <= 0) {
154     im_push_errorf(aIMCTX, 0, "file size limit - image width of %" i_DF " is not positive",
155                   i_DFc(width));
156     return 0;
157   }
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));
161     return 0;
162   }
163
164   if (height <= 0) {
165     im_push_errorf(aIMCTX, 0, "file size limit - image height of %" i_DF " is not positive",
166                   i_DFc(height));
167     return 0;
168   }
169
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));
173     return 0;
174   }
175
176   if (channels < 1 || channels > MAXCHANNELS) {
177     im_push_errorf(aIMCTX, 0, "file size limit - channels %d out of range",
178                   channels);
179     return 0;
180   }
181   
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",
184                   (long)sample_size);
185     return 0;
186   }
187
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 
190      for overflow.
191   */
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");
196     return 0;
197   }
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);
203       return 0;
204     }
205   }
206
207   return 1;
208 }