bn_mp_grow.c 1.5 KB

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