Line data Source code
1 : #include "tommath_private.h" 2 : #ifdef BN_MP_GROW_C 3 : /* LibTomMath, multiple-precision integer library -- Tom St Denis */ 4 : /* SPDX-License-Identifier: Unlicense */ 5 : 6 : /* grow as required */ 7 98895 : mp_err mp_grow(mp_int *a, int size) 8 : { 9 3377 : int i; 10 3377 : mp_digit *tmp; 11 : 12 : /* if the alloc size is smaller alloc more ram */ 13 98895 : if (a->alloc < size) { 14 : /* reallocate the array a->dp 15 : * 16 : * We store the return in a temporary variable 17 : * in case the operation failed we don't want 18 : * to overwrite the dp member of a. 19 : */ 20 97973 : tmp = (mp_digit *) MP_REALLOC(a->dp, 21 : (size_t)a->alloc * sizeof(mp_digit), 22 : (size_t)size * sizeof(mp_digit)); 23 97973 : if (tmp == NULL) { 24 : /* reallocation failed but "a" is still valid [can be freed] */ 25 0 : return MP_MEM; 26 : } 27 : 28 : /* reallocation succeeded so set a->dp */ 29 97973 : a->dp = tmp; 30 : 31 : /* zero excess digits */ 32 97973 : i = a->alloc; 33 97973 : a->alloc = size; 34 1778423 : MP_ZERO_DIGITS(a->dp + i, a->alloc - i); 35 : } 36 95518 : return MP_OKAY; 37 : } 38 : #endif