|
1 /* |
|
2 * Simple test driver for MPI library |
|
3 * |
|
4 * Test 6: Output functions |
|
5 * |
|
6 * This Source Code Form is subject to the terms of the Mozilla Public |
|
7 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
9 |
|
10 #include <stdio.h> |
|
11 #include <stdlib.h> |
|
12 #include <string.h> |
|
13 #include <ctype.h> |
|
14 #include <limits.h> |
|
15 |
|
16 #include "mpi.h" |
|
17 |
|
18 void print_buf(FILE *ofp, char *buf, int len) |
|
19 { |
|
20 int ix, brk = 0; |
|
21 |
|
22 for(ix = 0; ix < len; ix++) { |
|
23 fprintf(ofp, "%02X ", buf[ix]); |
|
24 |
|
25 brk = (brk + 1) & 0xF; |
|
26 if(!brk) |
|
27 fputc('\n', ofp); |
|
28 } |
|
29 |
|
30 if(brk) |
|
31 fputc('\n', ofp); |
|
32 |
|
33 } |
|
34 |
|
35 int main(int argc, char *argv[]) |
|
36 { |
|
37 int ix, size; |
|
38 mp_int a; |
|
39 char *buf; |
|
40 |
|
41 if(argc < 2) { |
|
42 fprintf(stderr, "Usage: %s <a>\n", argv[0]); |
|
43 return 1; |
|
44 } |
|
45 |
|
46 printf("Test 6: Output functions\n\n"); |
|
47 |
|
48 mp_init(&a); |
|
49 |
|
50 mp_read_radix(&a, argv[1], 10); |
|
51 |
|
52 printf("\nConverting to a string:\n"); |
|
53 |
|
54 printf("Rx Size Representation\n"); |
|
55 for(ix = 2; ix <= MAX_RADIX; ix++) { |
|
56 size = mp_radix_size(&a, ix); |
|
57 |
|
58 buf = calloc(size, sizeof(char)); |
|
59 mp_toradix(&a, buf, ix); |
|
60 printf("%2d: %3d: %s\n", ix, size, buf); |
|
61 free(buf); |
|
62 |
|
63 } |
|
64 |
|
65 printf("\nRaw output:\n"); |
|
66 size = mp_raw_size(&a); |
|
67 buf = calloc(size, sizeof(char)); |
|
68 |
|
69 printf("Size: %d bytes\n", size); |
|
70 |
|
71 mp_toraw(&a, buf); |
|
72 print_buf(stdout, buf, size); |
|
73 free(buf); |
|
74 |
|
75 mp_clear(&a); |
|
76 |
|
77 return 0; |
|
78 } |