| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2015-2019 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
/* |
| 10 |
* |
| 11 |
* Copyright (C) 2015 The Android Open Source Project |
| 12 |
* |
| 13 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 |
* you may not use this file except in compliance with the License. |
| 15 |
* You may obtain a copy of the License at |
| 16 |
* |
| 17 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 18 |
* |
| 19 |
* Unless required by applicable law or agreed to in writing, software |
| 20 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 21 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 |
* See the License for the specific language governing permissions and |
| 23 |
* limitations under the License. |
| 24 |
*/ |
| 25 |
|
| 26 |
// Functions for safe arithmetic (guarded against overflow) on integer types. |
| 27 |
|
| 28 |
#ifndef __dng_safe_arithmetic__ |
| 29 |
#define __dng_safe_arithmetic__ |
| 30 |
|
| 31 |
#include <cstddef> |
| 32 |
//#include <cstdint> |
| 33 |
#include <limits> |
| 34 |
|
| 35 |
#include "dng_exceptions.h" |
| 36 |
#include "dng_flags.h" |
| 37 |
#include "dng_types.h" |
| 38 |
|
| 39 |
#ifndef __has_builtin |
| 40 |
#define __has_builtin(x) 0 // Compatibility with non-Clang compilers. |
| 41 |
#endif |
| 42 |
|
| 43 |
#if !defined(DNG_HAS_INT128) && defined(__SIZEOF_INT128__) |
| 44 |
#define DNG_HAS_INT128 |
| 45 |
#endif |
| 46 |
|
| 47 |
// If the result of adding arg1 and arg2 will fit in an int32_t (without |
| 48 |
// under-/overflow), stores this result in *result and returns true. Otherwise, |
| 49 |
// returns false and leaves *result unchanged. |
| 50 |
bool SafeInt32Add(int32 arg1, int32 arg2, int32 *result); |
| 51 |
|
| 52 |
// Returns the result of adding arg1 and arg2 if it will fit in the result type |
| 53 |
// (without under-/overflow). Otherwise, throws a dng_exception with error code |
| 54 |
// dng_error_unknown. |
| 55 |
int32 SafeInt32Add(int32 arg1, int32 arg2); |
| 56 |
int64 SafeInt64Add(int64 arg1, int64 arg2); |
| 57 |
|
| 58 |
// If the result of adding arg1 and arg2 will fit in a uint32_t (without |
| 59 |
// wraparound), stores this result in *result and returns true. Otherwise, |
| 60 |
// returns false and leaves *result unchanged. |
| 61 |
bool SafeUint32Add(uint32 arg1, uint32 arg2, |
| 62 |
uint32 *result); |
| 63 |
|
| 64 |
// Returns the result of adding arg1 and arg2 if it will fit in the result type |
| 65 |
// (without wraparound). Otherwise, throws a dng_exception with error code |
| 66 |
// dng_error_unknown. |
| 67 |
uint32 SafeUint32Add(uint32 arg1, uint32 arg2); |
| 68 |
uint64 SafeUint64Add(uint64 arg1, uint64 arg2); |
| 69 |
|
| 70 |
// If the subtraction of arg2 from arg1 will not result in an int32_t under- or |
| 71 |
// overflow, stores this result in *result and returns true. Otherwise, |
| 72 |
// returns false and leaves *result unchanged. |
| 73 |
bool SafeInt32Sub(int32 arg1, int32 arg2, int32 *result); |
| 74 |
|
| 75 |
// Returns the result of subtracting arg2 from arg1 if this operation will not |
| 76 |
// result in an int32_t under- or overflow. Otherwise, throws a dng_exception |
| 77 |
// with error code dng_error_unknown. |
| 78 |
int32 SafeInt32Sub(int32 arg1, int32 arg2); |
| 79 |
|
| 80 |
// Returns the result of subtracting arg2 from arg1 if this operation will not |
| 81 |
// result in wraparound. Otherwise, throws a dng_exception with error code |
| 82 |
// dng_error_unknown. |
| 83 |
uint32 SafeUint32Sub(uint32 arg1, uint32 arg2); |
| 84 |
|
| 85 |
// Returns the result of multiplying arg1 and arg2 if it will fit in a int32_t |
| 86 |
// (without overflow). Otherwise, throws a dng_exception with error code |
| 87 |
// dng_error_unknown. |
| 88 |
int32 SafeInt32Mult(int32 arg1, int32 arg2); |
| 89 |
bool SafeInt32Mult(int32 arg1, int32 arg2, int32 *result); |
| 90 |
|
| 91 |
// If the result of multiplying arg1, ..., argn will fit in a uint32_t (without |
| 92 |
// wraparound), stores this result in *result and returns true. Otherwise, |
| 93 |
// returns false and leaves *result unchanged. |
| 94 |
bool SafeUint32Mult(uint32 arg1, uint32 arg2, |
| 95 |
uint32 *result); |
| 96 |
bool SafeUint32Mult(uint32 arg1, uint32 arg2, uint32 arg3, |
| 97 |
uint32 *result); |
| 98 |
bool SafeUint32Mult(uint32 arg1, uint32 arg2, uint32 arg3, |
| 99 |
uint32 arg4, uint32 *result); |
| 100 |
|
| 101 |
// Returns the result of multiplying arg1, ..., argn if it will fit in a |
| 102 |
// uint32_t (without wraparound). Otherwise, throws a dng_exception with error |
| 103 |
// code dng_error_unknown. |
| 104 |
uint32 SafeUint32Mult(uint32 arg1, uint32 arg2); |
| 105 |
uint32 SafeUint32Mult(uint32 arg1, uint32 arg2, |
| 106 |
uint32 arg3); |
| 107 |
uint32 SafeUint32Mult(uint32 arg1, uint32 arg2, |
| 108 |
uint32 arg3, uint32 arg4); |
| 109 |
|
| 110 |
// Returns the result of multiplying arg1 and arg2 if it will fit in a size_t |
| 111 |
// (without overflow). Otherwise, throws a dng_exception with error code |
| 112 |
// dng_error_unknown. |
| 113 |
std::size_t SafeSizetMult(std::size_t arg1, std::size_t arg2); |
| 114 |
|
| 115 |
namespace dng_internal { |
| 116 |
|
| 117 |
// Internal function used as fallback for SafeInt64Mult() if other optimized |
| 118 |
// computation is not supported. Don't call this function directly. |
| 119 |
int64 SafeInt64MultSlow(int64 arg1, int64 arg2); |
| 120 |
|
| 121 |
#if !qWinOS |
| 122 |
#ifdef __clang__ |
| 123 |
#ifdef __ANDROID__ |
| 124 |
// While clang says it supports __builtin_smull_overflow, the Android NDK |
| 125 |
// doesn't use the right runtime library per https://bugs.llvm.org/show_bug.cgi?id=28629 |
| 126 |
#define __USE_BUILTIN_SMULL_OVERFLOW !defined(__NDK_MAJOR__) && __has_builtin(__builtin_smull_overflow) |
| 127 |
#else |
| 128 |
#define __USE_BUILTIN_SMULL_OVERFLOW __has_builtin(__builtin_smull_overflow) |
| 129 |
#endif // __ANDROID__ |
| 130 |
#endif // __clang__ |
| 131 |
#endif // !qWinOS |
| 132 |
|
| 133 |
#ifndef __USE_BUILTIN_SMULL_OVERFLOW |
| 134 |
#define __USE_BUILTIN_SMULL_OVERFLOW 0 |
| 135 |
#endif |
| 136 |
|
| 137 |
// Internal function used as optimization for SafeInt64Mult() if Clang |
| 138 |
// __builtin_smull_overflow is supported. Don't call this function directly. |
| 139 |
#if __USE_BUILTIN_SMULL_OVERFLOW |
| 140 |
inline int64 SafeInt64MultByClang(int64 arg1, int64 arg2) { |
| 141 |
int64 result; |
| 142 |
bool failed; |
| 143 |
// sizeof() is a compile time constant so this compiles efficiently. |
| 144 |
if (sizeof(long) >= 8) { |
| 145 |
long temp_result; |
| 146 |
failed = __builtin_smull_overflow((long)arg1, (long)arg2, &temp_result); |
| 147 |
if (sizeof(long) > 8 && !failed) { |
| 148 |
failed = (temp_result > std::numeric_limits<int64>::max() || |
| 149 |
temp_result < std::numeric_limits<int64>::min()); |
| 150 |
} |
| 151 |
result = (int64)temp_result; |
| 152 |
} else if (sizeof(long long) >= 8) { |
| 153 |
long long temp_result; |
| 154 |
failed = __builtin_smulll_overflow((long long)arg1, (long long)arg2, &temp_result); |
| 155 |
if (sizeof(long long) > 8 && !failed) { |
| 156 |
failed = (temp_result > std::numeric_limits<int64>::max() || |
| 157 |
temp_result < std::numeric_limits<int64>::min()); |
| 158 |
} |
| 159 |
result = (int64)temp_result; |
| 160 |
} else { |
| 161 |
ThrowNotYetImplemented("No 64-bit capable multiply with overflow builtin."); |
| 162 |
} |
| 163 |
if (failed) { |
| 164 |
ThrowProgramError("Arithmetic overflow"); |
| 165 |
abort(); // Never reached. |
| 166 |
} |
| 167 |
return result; |
| 168 |
} |
| 169 |
#endif |
| 170 |
|
| 171 |
// Internal function used as optimization for SafeInt64Mult() if __int128 type |
| 172 |
// is supported. Don't call this function directly. |
| 173 |
#ifdef DNG_HAS_INT128 |
| 174 |
inline int64 SafeInt64MultByInt128(int64 arg1, |
| 175 |
int64 arg2) { |
| 176 |
const __int128 kInt64Max = |
| 177 |
static_cast<__int128>(std::numeric_limits<int64>::max()); |
| 178 |
const __int128 kInt64Min = |
| 179 |
static_cast<__int128>(std::numeric_limits<int64>::min()); |
| 180 |
__int128 result = static_cast<__int128>(arg1) * static_cast<__int128>(arg2); |
| 181 |
if (result > kInt64Max || result < kInt64Min) { |
| 182 |
ThrowProgramError("Arithmetic overflow"); |
| 183 |
} |
| 184 |
return static_cast<int64>(result); |
| 185 |
} |
| 186 |
#endif |
| 187 |
|
| 188 |
} // namespace dng_internal |
| 189 |
|
| 190 |
// Returns the result of multiplying arg1 and arg2 if it will fit in an int64_t |
| 191 |
// (without overflow). Otherwise, throws a dng_exception with error code |
| 192 |
// dng_error_unknown. |
| 193 |
inline int64 SafeInt64Mult(int64 arg1, int64 arg2) { |
| 194 |
#if __USE_BUILTIN_SMULL_OVERFLOW |
| 195 |
return dng_internal::SafeInt64MultByClang(arg1, arg2); |
| 196 |
#elif defined(DNG_HAS_INT128) |
| 197 |
return dng_internal::SafeInt64MultByInt128(arg1, arg2); |
| 198 |
#else |
| 199 |
return dng_internal::SafeInt64MultSlow(arg1, arg2); |
| 200 |
#endif |
| 201 |
} |
| 202 |
|
| 203 |
// Returns the result of dividing arg1 by arg2; if the result is not an integer, |
| 204 |
// rounds up to the next integer. If arg2 is zero, throws a dng_exception with |
| 205 |
// error code dng_error_unknown. |
| 206 |
// The function is safe against wraparound and will return the correct result |
| 207 |
// for all combinations of arg1 and arg2. |
| 208 |
uint32 SafeUint32DivideUp(uint32 arg1, uint32 arg2); |
| 209 |
|
| 210 |
// Finds the smallest integer multiple of 'multiple_of' that is greater than or |
| 211 |
// equal to 'val'. If this value will fit in a uint32_t, stores it in *result |
| 212 |
// and returns true. Otherwise, or if 'multiple_of' is zero, returns false and |
| 213 |
// leaves *result unchanged. |
| 214 |
bool RoundUpUint32ToMultiple(uint32 val, uint32 multiple_of, |
| 215 |
uint32 *result); |
| 216 |
|
| 217 |
// Returns the smallest integer multiple of 'multiple_of' that is greater than |
| 218 |
// or equal to 'val'. If the result will not fit in a uint32 or if |
| 219 |
// 'multiple_of' is zero, throws a dng_exception with error code |
| 220 |
// dng_error_unknown. |
| 221 |
uint32 RoundUpUint32ToMultiple(uint32 val, |
| 222 |
uint32 multiple_of); |
| 223 |
|
| 224 |
// If the uint32_t value val will fit in a int32_t, converts it to a int32_t and |
| 225 |
// stores it in *result. Otherwise, returns false and leaves *result unchanged. |
| 226 |
bool ConvertUint32ToInt32(uint32 val, int32 *result); |
| 227 |
|
| 228 |
// Returns the result of converting val to an int32_t if it can be converted |
| 229 |
// without overflow. Otherwise, throws a dng_exception with error code |
| 230 |
// dng_error_unknown. |
| 231 |
int32 ConvertUint32ToInt32(uint32 val); |
| 232 |
|
| 233 |
// Converts a value of the unsigned integer type TSrc to the unsigned integer |
| 234 |
// type TDest. If the value in 'src' cannot be converted to the type TDest |
| 235 |
// without truncation, throws a dng_exception with error code dng_error_unknown. |
| 236 |
// |
| 237 |
// Note: Though this function is typically used where TDest is a narrower type |
| 238 |
// than TSrc, it is designed to work also if TDest is wider than from TSrc or |
| 239 |
// identical to TSrc. This is useful in situations where the width of the types |
| 240 |
// involved can change depending on the architecture -- for example, the |
| 241 |
// conversion from size_t to uint32_t may either be narrowing, identical or even |
| 242 |
// widening (though the latter admittedly happens only on architectures that |
| 243 |
// aren't relevant to us). |
| 244 |
template <class TSrc, class TDest> |
| 245 |
static void ConvertUnsigned(TSrc src, TDest *dest) { |
| 246 |
#if 0 |
| 247 |
// sub-optimal run-time implementation pre-C++11 |
| 248 |
if (!(std::numeric_limits<TSrc>::is_integer && |
| 249 |
!std::numeric_limits<TSrc>::is_signed && |
| 250 |
std::numeric_limits<TDest>::is_integer && |
| 251 |
!std::numeric_limits<TDest>::is_signed)) |
| 252 |
{ |
| 253 |
ThrowProgramError ("TSrc and TDest must be unsigned integer types"); |
| 254 |
} |
| 255 |
#else |
| 256 |
// preferred compile-time implementation; requires C++11 |
| 257 |
static_assert(std::numeric_limits<TSrc>::is_integer && |
| 258 |
!std::numeric_limits<TSrc>::is_signed && |
| 259 |
std::numeric_limits<TDest>::is_integer && |
| 260 |
!std::numeric_limits<TDest>::is_signed, |
| 261 |
"TSrc and TDest must be unsigned integer types"); |
| 262 |
#endif |
| 263 |
|
| 264 |
const TDest converted = static_cast<TDest>(src); |
| 265 |
|
| 266 |
// Convert back to TSrc to check whether truncation occurred in the |
| 267 |
// conversion to TDest. |
| 268 |
if (static_cast<TSrc>(converted) != src) { |
| 269 |
ThrowProgramError("Overflow in unsigned integer conversion"); |
| 270 |
} |
| 271 |
|
| 272 |
*dest = converted; |
| 273 |
} |
| 274 |
|
| 275 |
// Returns the result of converting val to the result type using truncation if |
| 276 |
// val is in range of the result type values. Otherwise, throws a dng_exception |
| 277 |
// with error code dng_error_unknown. |
| 278 |
int32 ConvertDoubleToInt32(double val); |
| 279 |
uint32 ConvertDoubleToUint32(double val); |
| 280 |
|
| 281 |
// Returns the result of converting val to float. If val is outside of |
| 282 |
// [-FLT_MAX, FLT_MAX], -infinity and infinity is returned respectively. NaN is |
| 283 |
// returned as NaN. |
| 284 |
float ConvertDoubleToFloat(double val); |
| 285 |
|
| 286 |
/*****************************************************************************/ |
| 287 |
|
| 288 |
class dng_safe_int32; |
| 289 |
class dng_safe_uint32; |
| 290 |
|
| 291 |
/*****************************************************************************/ |
| 292 |
|
| 293 |
#define CHECK_SAFE_UINT32 \ |
| 294 |
static_assert (std::numeric_limits<T>::is_integer && \ |
| 295 |
!std::numeric_limits<T>::is_signed && \ |
| 296 |
(sizeof (T) == 4), \ |
| 297 |
"src must be unsigned 32-bit integer") |
| 298 |
|
| 299 |
class dng_safe_uint32 |
| 300 |
{ |
| 301 |
|
| 302 |
private: |
| 303 |
|
| 304 |
uint32 fValue; |
| 305 |
|
| 306 |
public: |
| 307 |
|
| 308 |
template<typename T> |
| 309 |
dng_safe_uint32 (T x) |
| 310 |
{ |
| 311 |
CHECK_SAFE_UINT32; |
| 312 |
fValue = x; |
| 313 |
} |
| 314 |
|
| 315 |
explicit dng_safe_uint32 (const dng_safe_int32 &x); |
| 316 |
|
| 317 |
inline uint32 Get () const |
| 318 |
{ |
| 319 |
return fValue; |
| 320 |
} |
| 321 |
|
| 322 |
// Compound assignment operators. |
| 323 |
|
| 324 |
dng_safe_uint32 & operator+= (const dng_safe_uint32 &x) |
| 325 |
{ |
| 326 |
fValue = SafeUint32Add (fValue, x.fValue); |
| 327 |
return *this; |
| 328 |
} |
| 329 |
|
| 330 |
template<typename T> |
| 331 |
dng_safe_uint32 & operator+= (T x) |
| 332 |
{ |
| 333 |
CHECK_SAFE_UINT32; |
| 334 |
fValue = SafeUint32Add (fValue, x); |
| 335 |
return *this; |
| 336 |
} |
| 337 |
|
| 338 |
dng_safe_uint32 & operator*= (const dng_safe_uint32 &x) |
| 339 |
{ |
| 340 |
fValue = SafeUint32Mult (fValue, x.fValue); |
| 341 |
return *this; |
| 342 |
} |
| 343 |
|
| 344 |
template<typename T> |
| 345 |
dng_safe_uint32 & operator*= (T x) |
| 346 |
{ |
| 347 |
CHECK_SAFE_UINT32; |
| 348 |
fValue = SafeUint32Mult (fValue, x); |
| 349 |
return *this; |
| 350 |
} |
| 351 |
|
| 352 |
// Binary operators. |
| 353 |
|
| 354 |
const dng_safe_uint32 operator+ (const dng_safe_uint32 &x) const |
| 355 |
{ |
| 356 |
return dng_safe_uint32 (*this) += x; |
| 357 |
} |
| 358 |
|
| 359 |
template<typename T> |
| 360 |
const dng_safe_uint32 operator+ (T x) const |
| 361 |
{ |
| 362 |
CHECK_SAFE_UINT32; |
| 363 |
return dng_safe_uint32 (*this) += x; |
| 364 |
} |
| 365 |
|
| 366 |
const dng_safe_uint32 operator* (const dng_safe_uint32 &x) const |
| 367 |
{ |
| 368 |
return dng_safe_uint32 (*this) *= x; |
| 369 |
} |
| 370 |
|
| 371 |
template<typename T> |
| 372 |
const dng_safe_uint32 operator* (T x) const |
| 373 |
{ |
| 374 |
CHECK_SAFE_UINT32; |
| 375 |
return dng_safe_uint32 (*this) *= x; |
| 376 |
} |
| 377 |
|
| 378 |
}; |
| 379 |
|
| 380 |
#undef CHECK_SAFE_UINT32 |
| 381 |
|
| 382 |
/*****************************************************************************/ |
| 383 |
|
| 384 |
#define CHECK_SAFE_INT32 \ |
| 385 |
static_assert (std::numeric_limits<T>::is_integer && \ |
| 386 |
std::numeric_limits<T>::is_signed && \ |
| 387 |
(sizeof (T) == 4), \ |
| 388 |
"src must be signed 32-bit integer") |
| 389 |
|
| 390 |
class dng_safe_int32 |
| 391 |
{ |
| 392 |
|
| 393 |
private: |
| 394 |
|
| 395 |
int32 fValue; |
| 396 |
|
| 397 |
public: |
| 398 |
|
| 399 |
template<typename T> |
| 400 |
dng_safe_int32 (T x) |
| 401 |
{ |
| 402 |
CHECK_SAFE_INT32; |
| 403 |
fValue = x; |
| 404 |
} |
| 405 |
|
| 406 |
// Construct int32 from uint32. Throws exception dng_error_overflow if |
| 407 |
// source uint32 cannot be represented as int32. |
| 408 |
|
| 409 |
explicit dng_safe_int32 (const dng_safe_uint32 &x); |
| 410 |
|
| 411 |
inline int32 Get () const |
| 412 |
{ |
| 413 |
return fValue; |
| 414 |
} |
| 415 |
|
| 416 |
// Assign from uint32. Throws exception dng_error_overflow if source |
| 417 |
// uint32 cannot be represented as int32. |
| 418 |
|
| 419 |
void Set_uint32 (uint32 x) |
| 420 |
{ |
| 421 |
if (!ConvertUint32ToInt32 (x, &fValue)) |
| 422 |
{ |
| 423 |
ThrowProgramError ("Overflow in Set_uint32"); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// Compound assignment operators. |
| 428 |
|
| 429 |
dng_safe_int32 & operator+= (const dng_safe_int32 &x) |
| 430 |
{ |
| 431 |
fValue = SafeInt32Add (fValue, x.fValue); |
| 432 |
return *this; |
| 433 |
} |
| 434 |
|
| 435 |
template<typename T> |
| 436 |
dng_safe_int32 & operator+= (T x) |
| 437 |
{ |
| 438 |
CHECK_SAFE_INT32; |
| 439 |
fValue = SafeInt32Add (fValue, x); |
| 440 |
return *this; |
| 441 |
} |
| 442 |
|
| 443 |
dng_safe_int32 & operator-= (const dng_safe_int32 &x) |
| 444 |
{ |
| 445 |
fValue = SafeInt32Sub (fValue, x.fValue); |
| 446 |
return *this; |
| 447 |
} |
| 448 |
|
| 449 |
template<typename T> |
| 450 |
dng_safe_int32 & operator-= (T x) |
| 451 |
{ |
| 452 |
CHECK_SAFE_INT32; |
| 453 |
fValue = SafeInt32Sub (fValue, x); |
| 454 |
return *this; |
| 455 |
} |
| 456 |
|
| 457 |
}; |
| 458 |
|
| 459 |
#undef CHECK_SAFE_INT32 |
| 460 |
|
| 461 |
/*****************************************************************************/ |
| 462 |
|
| 463 |
#endif // __dng_safe_arithmetic__ |