1 /*
2   Name:     bintest.c
3   Purpose:  Test driver for binary input/output formats from IMath.
4   Author:   M. J. Fromberger
5 
6   Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.
7 
8   Permission is hereby granted, free of charge, to any person obtaining a copy
9   of this software and associated documentation files (the "Software"), to deal
10   in the Software without restriction, including without limitation the rights
11   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12   copies of the Software, and to permit persons to whom the Software is
13   furnished to do so, subject to the following conditions:
14 
15   The above copyright notice and this permission notice shall be included in
16   all copies or substantial portions of the Software.
17 
18   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
21   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   SOFTWARE.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "imath.h"
32 
main(int argc,char * argv[])33 int main(int argc, char *argv[]) {
34   unsigned char buf[512];
35   mpz_t v, w;
36   mp_result res;
37   int len;
38 
39   if (argc < 2 || argv[1][0] == '\0') {
40     fprintf(stderr, "Usage: bintest <value>\n");
41     return 1;
42   }
43 
44   mp_int_init(&v);
45   mp_int_init(&w);
46   res = mp_int_read_string(&v, 10, argv[1]);
47   printf("Result code from mp_int_read_string() = %d (%s)\n", res,
48          mp_error_string(res));
49 
50   len = mp_int_binary_len(&v);
51   printf("%d bytes needed to write this value in 2's complement.\n", len);
52 
53   res = mp_int_to_binary(&v, buf, sizeof(buf));
54   printf("Result code from mp_int_to_binary() = %d (%s)\n", res,
55          mp_error_string(res));
56   if (res != MP_OK) {
57     return 1;
58   }
59   int ix;
60   for (ix = 0; ix < (len - 1); ++ix) {
61     printf("%d.", buf[ix]);
62   }
63   printf("%d\n", buf[ix]);
64 
65   /* Try converting back... */
66   res = mp_int_read_binary(&w, buf, len);
67   printf("Result code from mp_int_read_binary() = %d (%s)\n", res,
68          mp_error_string(res));
69   if (res == MP_OK) {
70     mp_int_to_string(&w, 10, (char *)buf, sizeof(buf));
71 
72     printf("[%s]\n\n", buf);
73   }
74 
75   len = mp_int_unsigned_len(&v);
76   printf("%d bytes needed to write this value as unsigned.\n", len);
77 
78   res = mp_int_to_unsigned(&v, buf, sizeof(buf));
79   printf("Result code from mp_int_to_unsigned() = %d\n", res);
80   if (res == MP_OK) {
81     int ix;
82 
83     for (ix = 0; ix < (len - 1); ++ix) {
84       printf("%d.", buf[ix]);
85     }
86 
87     printf("%d\n", buf[ix]);
88   } else {
89     return 1;
90   }
91 
92   res = mp_int_read_unsigned(&w, buf, len);
93   printf("Result code from mp_int_read_unsigned() = %d (%s)\n", res,
94          mp_error_string(res));
95   if (res == MP_OK) {
96     mp_int_to_string(&w, 10, (char *)buf, sizeof(buf));
97 
98     printf("[%s]\n\n", buf);
99   }
100 
101   mp_int_clear(&v);
102   mp_int_clear(&w);
103   return 0;
104 }
105