bn_mp_grow.c

Go to the documentation of this file.
00001 #include <tommath.h>
00002 #ifdef BN_MP_GROW_C
00003 /* LibTomMath, multiple-precision integer library -- Tom St Denis
00004  *
00005  * LibTomMath is a library that provides multiple-precision
00006  * integer arithmetic as well as number theoretic functionality.
00007  *
00008  * The library was designed directly after the MPI library by
00009  * Michael Fromberger but has been written from scratch with
00010  * additional optimizations in place.
00011  *
00012  * The library is free for all purposes without any express
00013  * guarantee it works.
00014  *
00015  * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
00016  */
00017 
00018 /* grow as required */
00019 int mp_grow (mp_int * a, int size)
00020 {
00021   int     i;
00022   mp_digit *tmp;
00023 
00024   /* if the alloc size is smaller alloc more ram */
00025   if (a->alloc < size) {
00026     /* ensure there are always at least MP_PREC digits extra on top */
00027     size += (MP_PREC * 2) - (size % MP_PREC);
00028 
00029     /* reallocate the array a->dp
00030      *
00031      * We store the return in a temporary variable
00032      * in case the operation failed we don't want
00033      * to overwrite the dp member of a.
00034      */
00035     tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size);
00036     if (tmp == NULL) {
00037       /* reallocation failed but "a" is still valid [can be freed] */
00038       return MP_MEM;
00039     }
00040 
00041     /* reallocation succeeded so set a->dp */
00042     a->dp = tmp;
00043 
00044     /* zero excess digits */
00045     i        = a->alloc;
00046     a->alloc = size;
00047     for (; i < a->alloc; i++) {
00048       a->dp[i] = 0;
00049     }
00050   }
00051   return MP_OKAY;
00052 }
00053 #endif
00054 
00055 /* $Source: /cvsroot/tcl/libtommath/bn_mp_grow.c,v $ */
00056 /* $Revision: 1.1.1.3 $ */
00057 /* $Date: 2006/12/01 00:08:11 $ */



Generated on Wed Mar 12 12:18:24 2008 by  doxygen 1.5.1