size_constraints.h (1329B)
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 uint32_t dec_max_xsize = 0xFFFFFFFFu; 20 uint32_t dec_max_ysize = 0xFFFFFFFFu; 21 uint64_t dec_max_pixels = 0xFFFFFFFFu; // Might be up to ~0ull 22 }; 23 24 template <typename T, 25 class = typename std::enable_if<std::is_unsigned<T>::value>::type> 26 Status VerifyDimensions(const SizeConstraints* constraints, T xs, T ys) { 27 if (!constraints) return true; 28 29 if (xs == 0 || ys == 0) return JXL_FAILURE("Empty image."); 30 if (xs > constraints->dec_max_xsize) return JXL_FAILURE("Image too wide."); 31 if (ys > constraints->dec_max_ysize) return JXL_FAILURE("Image too tall."); 32 33 const uint64_t num_pixels = static_cast<uint64_t>(xs) * ys; 34 if (num_pixels > constraints->dec_max_pixels) { 35 return JXL_FAILURE("Image too big."); 36 } 37 38 return true; 39 } 40 41 } // namespace jxl 42 43 #endif // LIB_JXL_SIZE_CONSTRAINTS_H_