bn_mp_copy.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <tommath.h>
  2. #ifdef BN_MP_COPY_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. /* copy, b = a */
  18. int
  19. mp_copy (mp_int * a, mp_int * b)
  20. {
  21. int res, n;
  22. /* if dst == src do nothing */
  23. if (a == b) {
  24. return MP_OKAY;
  25. }
  26. /* grow dest */
  27. if (b->alloc < a->used) {
  28. if ((res = mp_grow (b, a->used)) != MP_OKAY) {
  29. return res;
  30. }
  31. }
  32. /* zero b and copy the parameters over */
  33. {
  34. register mp_digit *tmpa, *tmpb;
  35. /* pointer aliases */
  36. /* source */
  37. tmpa = a->dp;
  38. /* destination */
  39. tmpb = b->dp;
  40. /* copy all the digits */
  41. for (n = 0; n < a->used; n++) {
  42. *tmpb++ = *tmpa++;
  43. }
  44. /* clear high digits */
  45. for (; n < b->used; n++) {
  46. *tmpb++ = 0;
  47. }
  48. }
  49. /* copy used count and sign */
  50. b->used = a->used;
  51. b->sign = a->sign;
  52. return MP_OKAY;
  53. }
  54. #endif
  55. /* $Source: /cvs/libtom/libtommath/bn_mp_copy.c,v $ */
  56. /* $Revision: 1.3 $ */
  57. /* $Date: 2006/03/31 14:18:44 $ */