bn_mp_init_multi.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <tommath.h>
  2. #ifdef BN_MP_INIT_MULTI_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. #include <stdarg.h>
  18. int mp_init_multi(mp_int *mp, ...)
  19. {
  20. mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
  21. int n = 0; /* Number of ok inits */
  22. mp_int* cur_arg = mp;
  23. va_list args;
  24. va_start(args, mp); /* init args to next argument from caller */
  25. while (cur_arg != NULL) {
  26. if (mp_init(cur_arg) != MP_OKAY) {
  27. /* Oops - error! Back-track and mp_clear what we already
  28. succeeded in init-ing, then return error.
  29. */
  30. va_list clean_args;
  31. /* end the current list */
  32. va_end(args);
  33. /* now start cleaning up */
  34. cur_arg = mp;
  35. va_start(clean_args, mp);
  36. while (n--) {
  37. mp_clear(cur_arg);
  38. cur_arg = va_arg(clean_args, mp_int*);
  39. }
  40. va_end(clean_args);
  41. res = MP_MEM;
  42. break;
  43. }
  44. n++;
  45. cur_arg = va_arg(args, mp_int*);
  46. }
  47. va_end(args);
  48. return res; /* Assumed ok, if error flagged above. */
  49. }
  50. #endif
  51. /* $Source: /cvs/libtom/libtommath/bn_mp_init_multi.c,v $ */
  52. /* $Revision: 1.3 $ */
  53. /* $Date: 2006/03/31 14:18:44 $ */