/src/libjxl/lib/extras/size_constraints.h
Line | Count | Source |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #ifndef LIB_JXL_SIZE_CONSTRAINTS_H_ |
7 | | #define LIB_JXL_SIZE_CONSTRAINTS_H_ |
8 | | |
9 | | #include <cstdint> |
10 | | #include <type_traits> |
11 | | |
12 | | #include "lib/jxl/base/status.h" |
13 | | |
14 | | namespace jxl { |
15 | | |
16 | | struct SizeConstraints { |
17 | | // Upper limit on pixel dimensions/area, enforced by VerifyDimensions |
18 | | // (called from decoders). Fuzzers set smaller values to limit memory use. |
19 | | // Default values correspond to JXL level 10. |
20 | | uint32_t dec_max_xsize = 1u << 30; |
21 | | uint32_t dec_max_ysize = 1u << 30; |
22 | | uint64_t dec_max_pixels = static_cast<uint64_t>(1u) << 40; |
23 | | }; |
24 | | |
25 | | template <typename T, |
26 | | class = typename std::enable_if<std::is_unsigned<T>::value>::type> |
27 | 82.1k | Status VerifyDimensions(const SizeConstraints* constraints, T xs, T ys) { |
28 | 82.1k | SizeConstraints limit = {}; |
29 | 82.1k | if (constraints) limit = *constraints; |
30 | | |
31 | 82.1k | if (xs == 0 || ys == 0) return JXL_FAILURE("Empty image."); |
32 | 82.0k | if (xs > limit.dec_max_xsize) return JXL_FAILURE("Image too wide."); |
33 | 81.9k | if (ys > limit.dec_max_ysize) return JXL_FAILURE("Image too tall."); |
34 | | |
35 | 81.8k | const uint64_t num_pixels = static_cast<uint64_t>(xs) * ys; |
36 | 81.8k | if (num_pixels > limit.dec_max_pixels) { |
37 | 68 | return JXL_FAILURE("Image too big."); |
38 | 68 | } |
39 | | |
40 | 81.7k | return true; |
41 | 81.8k | } jxl::Status jxl::VerifyDimensions<unsigned long, void>(jxl::SizeConstraints const*, unsigned long, unsigned long) Line | Count | Source | 27 | 49.8k | Status VerifyDimensions(const SizeConstraints* constraints, T xs, T ys) { | 28 | 49.8k | SizeConstraints limit = {}; | 29 | 49.8k | if (constraints) limit = *constraints; | 30 | | | 31 | 49.8k | if (xs == 0 || ys == 0) return JXL_FAILURE("Empty image."); | 32 | 49.7k | if (xs > limit.dec_max_xsize) return JXL_FAILURE("Image too wide."); | 33 | 49.6k | if (ys > limit.dec_max_ysize) return JXL_FAILURE("Image too tall."); | 34 | | | 35 | 49.5k | const uint64_t num_pixels = static_cast<uint64_t>(xs) * ys; | 36 | 49.5k | if (num_pixels > limit.dec_max_pixels) { | 37 | 49 | return JXL_FAILURE("Image too big."); | 38 | 49 | } | 39 | | | 40 | 49.5k | return true; | 41 | 49.5k | } |
jxl::Status jxl::VerifyDimensions<unsigned int, void>(jxl::SizeConstraints const*, unsigned int, unsigned int) Line | Count | Source | 27 | 32.2k | Status VerifyDimensions(const SizeConstraints* constraints, T xs, T ys) { | 28 | 32.2k | SizeConstraints limit = {}; | 29 | 32.2k | if (constraints) limit = *constraints; | 30 | | | 31 | 32.2k | if (xs == 0 || ys == 0) return JXL_FAILURE("Empty image."); | 32 | 32.2k | if (xs > limit.dec_max_xsize) return JXL_FAILURE("Image too wide."); | 33 | 32.2k | if (ys > limit.dec_max_ysize) return JXL_FAILURE("Image too tall."); | 34 | | | 35 | 32.2k | const uint64_t num_pixels = static_cast<uint64_t>(xs) * ys; | 36 | 32.2k | if (num_pixels > limit.dec_max_pixels) { | 37 | 19 | return JXL_FAILURE("Image too big."); | 38 | 19 | } | 39 | | | 40 | 32.2k | return true; | 41 | 32.2k | } |
|
42 | | |
43 | | } // namespace jxl |
44 | | |
45 | | #endif // LIB_JXL_SIZE_CONSTRAINTS_H_ |