Debian Codegraph @

libtommath/bn_mp_grow.c

raw
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 mp_err mp_grow(mp_int *a, int size)
8 {
9 int i;
10 mp_digit *tmp;
11
12 if (size < 0) {
13 return MP_VAL;
14 }
15
16 /* if the alloc size is smaller alloc more ram */
17 if (a->alloc < size) {
18 /* reallocate the array a->dp
19 *
20 * We store the return in a temporary variable
21 * in case the operation failed we don't want
22 * to overwrite the dp member of a.
23 */
24 tmp = (mp_digit *) MP_REALLOC(a->dp,
25 (size_t)a->alloc * sizeof(mp_digit),
26 (size_t)size * sizeof(mp_digit));
27 if (tmp == NULL) {
28 /* reallocation failed but "a" is still valid [can be freed] */
29 return MP_MEM;
30 }
31
32 /* reallocation succeeded so set a->dp */
33 a->dp = tmp;
34
35 /* zero excess digits */
36 i = a->alloc;
37 a->alloc = size;
38 MP_ZERO_DIGITS(a->dp + i, a->alloc - i);
39 }
40 return MP_OKAY;
41 }
42 #endif