Home | History | Annotate | Line # | Download | only in libtommath
      1 /*	$NetBSD: bn_mp_grow.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
      2 
      3 #include <tommath.h>
      4 #ifdef BN_MP_GROW_C
      5 /* LibTomMath, multiple-precision integer library -- Tom St Denis
      6  *
      7  * LibTomMath is a library that provides multiple-precision
      8  * integer arithmetic as well as number theoretic functionality.
      9  *
     10  * The library was designed directly after the MPI library by
     11  * Michael Fromberger but has been written from scratch with
     12  * additional optimizations in place.
     13  *
     14  * The library is free for all purposes without any express
     15  * guarantee it works.
     16  *
     17  * Tom St Denis, tomstdenis (at) gmail.com, http://libtom.org
     18  */
     19 
     20 /* grow as required */
     21 int mp_grow (mp_int * a, int size)
     22 {
     23   int     i;
     24   mp_digit *tmp;
     25 
     26   /* if the alloc size is smaller alloc more ram */
     27   if (a->alloc < size) {
     28     /* ensure there are always at least MP_PREC digits extra on top */
     29     size += (MP_PREC * 2) - (size % MP_PREC);
     30 
     31     /* reallocate the array a->dp
     32      *
     33      * We store the return in a temporary variable
     34      * in case the operation failed we don't want
     35      * to overwrite the dp member of a.
     36      */
     37     tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size);
     38     if (tmp == NULL) {
     39       /* reallocation failed but "a" is still valid [can be freed] */
     40       return MP_MEM;
     41     }
     42 
     43     /* reallocation succeeded so set a->dp */
     44     a->dp = tmp;
     45 
     46     /* zero excess digits */
     47     i        = a->alloc;
     48     a->alloc = size;
     49     for (; i < a->alloc; i++) {
     50       a->dp[i] = 0;
     51     }
     52   }
     53   return MP_OKAY;
     54 }
     55 #endif
     56 
     57 /* Source: /cvs/libtom/libtommath/bn_mp_grow.c,v  */
     58 /* Revision: 1.4  */
     59 /* Date: 2006/12/28 01:25:13  */
     60