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 18963 : mp_err mp_grow(mp_int *a, int size)
8 : {
9 : int i;
10 : mp_digit *tmp;
11 :
12 : /* if the alloc size is smaller alloc more ram */
13 18963 : 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 18734 : tmp = (mp_digit *) MP_REALLOC(a->dp,
21 : (size_t)a->alloc * sizeof(mp_digit),
22 : (size_t)size * sizeof(mp_digit));
23 18734 : 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 18734 : a->dp = tmp;
30 :
31 : /* zero excess digits */
32 18734 : i = a->alloc;
33 18734 : a->alloc = size;
34 18734 : MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
35 : }
36 18963 : return MP_OKAY;
37 : }
38 : #endif
|