77 #include <jasper/jas_config.h>
95 #define JAS_KIBI JAS_CAST(size_t, 1024)
96 #define JAS_MEBI (JAS_KIBI * JAS_KIBI)
100 (((x) >= 0) ? (x) : (-(x)))
103 #define JAS_MIN(x, y) \
104 (((x) < (y)) ? (x) : (y))
107 #define JAS_MAX(x, y) \
108 (((x) > (y)) ? (x) : (y))
112 #define JAS_MOD(x, y) \
113 (((x) < 0) ? (((-x) % (y)) ? ((y) - ((-(x)) % (y))) : (0)) : ((x) % (y)))
117 #define JAS_ONES(n) \
120 #define JAS_ONES_X(type, n) \
121 ((JAS_CAST(type, 1) << (n)) - 1)
123 #define JAS_POW2_X(type, n) \
124 (JAS_CAST(type, 1) << (n))
130 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
133 #pragma GCC diagnostic push
134 #pragma GCC diagnostic ignored "-Wshift-negative-value"
138 JAS_ATTRIBUTE_DISABLE_UBSAN
139 inline static int jas_int_asr(
int x,
unsigned n)
143 assert(((-1) >> 1) == -1);
151 JAS_ATTRIBUTE_DISABLE_UBSAN
152 inline static int jas_int_asl(
int x,
unsigned n)
156 assert(((-1) << 1) == -2);
164 JAS_ATTRIBUTE_DISABLE_UBSAN
165 inline static int_least32_t jas_least32_asr(int_least32_t x,
unsigned n)
169 assert(((JAS_CAST(int_least32_t, -1)) >> 1) == JAS_CAST(int_least32_t, -1));
177 JAS_ATTRIBUTE_DISABLE_UBSAN
178 inline static int_least32_t jas_least32_asl(int_least32_t x,
unsigned n)
182 assert(((JAS_CAST(int_least32_t, -1)) << 1) == JAS_CAST(int_least32_t, -2));
190 JAS_ATTRIBUTE_DISABLE_UBSAN
191 inline static int_fast32_t jas_fast32_asr(int_fast32_t x,
unsigned n)
195 assert(((JAS_CAST(int_fast32_t, -1)) >> 1) == JAS_CAST(int_fast32_t, -1));
203 JAS_ATTRIBUTE_DISABLE_UBSAN
204 inline static int_fast32_t jas_fast32_asl(int_fast32_t x,
unsigned n)
208 assert(((JAS_CAST(int_fast32_t, -1)) << 1) == JAS_CAST(int_fast32_t, -2));
215 #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 6)
216 #pragma GCC diagnostic pop
224 inline static bool jas_safe_size_mul(
size_t x,
size_t y,
size_t *result)
226 #if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
227 size_t result_buffer;
228 bool valid = !__builtin_mul_overflow(x, y, &result_buffer);
229 if (valid && result) {
230 *result = result_buffer;
235 if (x && y > SIZE_MAX / x) {
247 inline static bool jas_safe_size_mul3(
size_t a,
size_t b,
size_t c,
251 if (!jas_safe_size_mul(a, b, &tmp) ||
252 !jas_safe_size_mul(tmp, c, &tmp)) {
262 inline static bool jas_safe_size_add(
size_t x,
size_t y,
size_t *result)
264 #if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
265 size_t result_buffer;
266 bool valid = !__builtin_add_overflow(x, y, &result_buffer);
267 if (valid && result) {
268 *result = result_buffer;
272 if (y > SIZE_MAX - x) {
283 inline static bool jas_safe_size_sub(
size_t x,
size_t y,
size_t *result)
285 #if jas_has_builtin(__builtin_sub_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
286 size_t result_buffer;
287 bool valid = !__builtin_sub_overflow(x, y, &result_buffer);
288 if (valid && result) {
289 *result = result_buffer;
304 inline static bool jas_safe_intfast32_mul(int_fast32_t x, int_fast32_t y,
305 int_fast32_t *result)
307 #if jas_has_builtin(__builtin_mul_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
308 int_fast32_t result_buffer;
309 bool valid = !__builtin_mul_overflow(x, y, &result_buffer);
310 if (valid && result) {
311 *result = result_buffer;
319 if (x > INT_FAST32_MAX / y) {
324 if (y < INT_FAST32_MIN / x) {
332 if (x < INT_FAST32_MIN / y) {
336 if (x != 0 && y < INT_FAST32_MAX / x) {
350 inline static bool jas_safe_intfast32_mul3(int_fast32_t a, int_fast32_t b,
351 int_fast32_t c, int_fast32_t *result)
354 if (!jas_safe_intfast32_mul(a, b, &tmp) ||
355 !jas_safe_intfast32_mul(tmp, c, &tmp)) {
365 inline static bool jas_safe_intfast32_add(int_fast32_t x, int_fast32_t y,
366 int_fast32_t *result)
368 #if jas_has_builtin(__builtin_add_overflow) || (defined(__GNUC__) && __GNUC__ > 5)
369 int_fast32_t result_buffer;
370 bool valid = !__builtin_add_overflow(x, y, &result_buffer);
371 if (valid && result) {
372 *result = result_buffer;
376 if ((y > 0 && x > INT_FAST32_MAX - y) ||
377 (y < 0 && x < INT_FAST32_MIN - y)) {
392 inline static bool jas_safe_uint_mul(
unsigned x,
unsigned y,
unsigned *result)
395 if (x && y > UINT_MAX / x) {
410 #define JAS_SAFEUI32_MAX (0xffffffffU)
414 uint_least32_t value;
418 static inline jas_safeui32_t jas_safeui32_from_ulong(
unsigned long x)
420 jas_safeui32_t result;
421 if (x <= JAS_SAFEUI32_MAX) {
423 result.value = JAS_CAST(uint_least32_t, x);
432 static inline bool jas_safeui32_to_intfast32(jas_safeui32_t x,
435 if (x.value <= INT_FAST32_MAX) {
444 static inline jas_safeui32_t jas_safeui32_add(jas_safeui32_t x,
447 jas_safeui32_t result;
448 if (x.valid && y.valid && y.value <= UINT_LEAST32_MAX - x.value) {
450 result.value = x.value + y.value;
452 result.valid =
false;
460 jas_safeui32_t jas_safeui32_sub(jas_safeui32_t x, jas_safeui32_t y)
462 jas_safeui32_t result;
463 if (x.valid && y.valid && y.value <= x.value) {
465 result.value = x.value - y.value;
467 result.valid =
false;
474 static inline jas_safeui32_t jas_safeui32_mul(jas_safeui32_t x,
477 jas_safeui32_t result;
478 if (!x.valid || !y.valid || (x.value && y.value > UINT_LEAST32_MAX /
480 result.valid =
false;
484 result.value = x.value * y.value;
500 jas_safei64_t jas_safei64_from_intmax(intmax_t x)
502 jas_safei64_t result;
503 if (x >= INT_LEAST64_MIN && x <= INT_LEAST64_MAX) {
505 result.value = JAS_CAST(int_least64_t, x);
507 result.valid =
false;
515 jas_safei64_t jas_safei64_add(jas_safei64_t x, jas_safei64_t y)
517 jas_safei64_t result;
518 if (((y.value > 0) && (x.value > (INT_LEAST64_MAX - y.value))) ||
519 ((y.value < 0) && (x.value < (INT_LEAST64_MIN - y.value)))) {
520 result.value =
false;
524 result.value = x.value + y.value;
531 jas_safei64_t jas_safei64_sub(jas_safei64_t x, jas_safei64_t y)
533 jas_safei64_t result;
534 if ((y.value > 0 && x.value < INT_LEAST64_MIN + y.value) ||
535 (y.value < 0 && x.value > INT_LEAST64_MAX + y.value)) {
536 result.valid =
false;
540 result.value = x.value - y.value;
547 jas_safei64_t jas_safei64_mul(jas_safei64_t x, jas_safei64_t y)
549 jas_safei64_t result;
552 if (x.value > (INT_LEAST64_MAX / y.value)) {
556 if (y.value < (INT_LEAST64_MIN / x.value)) {
562 if (x.value < (INT_LEAST64_MIN / y.value)) {
566 if ( (x.value != 0) && (y.value < (INT_LEAST64_MAX / x.value))) {
572 result.value = x.value * y.value;
575 result.valid =
false;
583 jas_safei64_t jas_safei64_div(jas_safei64_t x, jas_safei64_t y)
586 jas_safei64_t result;
587 result.valid =
false;
595 jas_i32_t jas_safei64_to_i32(jas_safei64_t x, jas_i32_t invalid_value)
598 if (x.valid && x.value >= JAS_I32_MIN && x.value <= JAS_I32_MAX) {
599 result = JAS_CAST(jas_i32_t, x.value);
601 result = invalid_value;
612 uint_least64_t value;
617 jas_safeui64_t jas_safeui64_from_intmax(intmax_t x)
619 jas_safeui64_t result;
620 if (x >= 0 && x <= UINT_LEAST64_MAX) {
622 result.value = JAS_CAST(uint_least64_t, x);
624 result.valid =
false;
632 jas_safeui64_t jas_safeui64_add(jas_safeui64_t x, jas_safeui64_t y)
634 jas_safeui64_t result;
635 if (x.valid && y.valid && y.value <= UINT_LEAST64_MAX - x.value) {
637 result.value = x.value + y.value;
639 result.valid =
false;
647 jas_safeui64_t jas_safeui64_sub(jas_safeui64_t x, jas_safeui64_t y)
649 jas_safeui64_t result;
650 if (x.valid && y.valid && y.value <= x.value) {
652 result.value = x.value - y.value;
654 result.valid =
false;
662 jas_safeui64_t jas_safeui64_mul(jas_safeui64_t x, jas_safeui64_t y)
664 jas_safeui64_t result;
665 if (!x.valid || !y.valid || (x.value && y.value > UINT_LEAST64_MAX /
667 result.valid =
false;
671 result.value = x.value * y.value;
678 jas_safeui64_t jas_safeui64_div(jas_safeui64_t x, jas_safeui64_t y)
680 jas_safeui64_t result;
681 if (x.valid && y.valid && y.value) {
683 result.value = x.value / y.value;
685 result.valid =
false;
693 jas_safeui64_t jas_safeui64_pow2_intmax(intmax_t x)
695 jas_safeui64_t result;
696 if (x >= 0 && x < 64) {
698 result.value = JAS_CAST(uint_least64_t, 1) << x;
700 result.valid =
false;
708 int jas_safeui64_to_int(jas_safeui64_t x,
int invalid_value)
711 if (x.valid && x.value <= INT_MAX) {
712 result = JAS_CAST(
int, x.value);
714 result = invalid_value;
721 jas_ui32_t jas_safeui64_to_ui32(jas_safeui64_t x, jas_ui32_t invalid_value)
724 if (x.valid && x.value <= JAS_UI32_MAX) {
725 result = JAS_CAST(jas_ui32_t, x.value);
727 result = invalid_value;
734 jas_i32_t jas_safeui64_to_i32(jas_safeui64_t x, jas_i32_t invalid_value)
737 if (x.valid && x.value >= JAS_I32_MIN && x.value <= JAS_I32_MAX) {
738 result = JAS_CAST(jas_i32_t, x.value);
740 result = invalid_value;