Merge pull request #4 from thesamesam/develop
[libtompoly.git] / pb_init_multi.c
blobf704b8608ff428c2372089feb5ca2e609f3f4bdf
1 /* LibTomPoly, Polynomial Basis Math -- Tom St Denis
2 *
3 * LibTomPoly is a public domain library that provides
4 * polynomial basis arithmetic support. It relies on
5 * LibTomMath for large integer support.
7 * This library is free for all purposes without any
8 * express guarantee that it works.
10 * Tom St Denis, tomstdenis@iahu.ca, http://poly.libtomcrypt.org
12 #include <tompoly.h>
13 #include <stdarg.h>
15 int pb_init_multi(mp_int *characteristic, pb_poly *pb, ...)
17 mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
18 int n = 0; /* Number of ok inits */
19 pb_poly* cur_arg = pb;
20 va_list args;
22 va_start(args, pb); /* init args to next argument from caller */
23 while (cur_arg != NULL) {
24 if (pb_init(cur_arg, characteristic) != MP_OKAY) {
25 /* Oops - error! Back-track and mp_clear what we already
26 succeeded in init-ing, then return error.
28 va_list clean_args;
30 /* end the current list */
31 va_end(args);
33 /* now start cleaning up */
34 cur_arg = pb;
35 va_start(clean_args, pb);
36 while (n--) {
37 pb_clear(cur_arg);
38 cur_arg = va_arg(clean_args, pb_poly*);
40 va_end(clean_args);
41 res = MP_MEM;
42 break;
44 n++;
45 cur_arg = va_arg(args, pb_poly*);
47 va_end(args);
48 return res; /* Assumed ok, if error flagged above. */