| 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 | |
| 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, format: "Usage: bintest <value>\n" ); |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | mp_int_init(z: &v); |
| 45 | mp_int_init(z: &w); |
| 46 | res = mp_int_read_string(z: &v, radix: 10, str: argv[1]); |
| 47 | printf(format: "Result code from mp_int_read_string() = %d (%s)\n" , res, |
| 48 | mp_error_string(res)); |
| 49 | |
| 50 | len = mp_int_binary_len(z: &v); |
| 51 | printf(format: "%d bytes needed to write this value in 2's complement.\n" , len); |
| 52 | |
| 53 | res = mp_int_to_binary(z: &v, buf, limit: sizeof(buf)); |
| 54 | printf(format: "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(format: "%d." , buf[ix]); |
| 62 | } |
| 63 | printf(format: "%d\n" , buf[ix]); |
| 64 | |
| 65 | /* Try converting back... */ |
| 66 | res = mp_int_read_binary(z: &w, buf, len); |
| 67 | printf(format: "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(z: &w, radix: 10, str: (char *)buf, limit: sizeof(buf)); |
| 71 | |
| 72 | printf(format: "[%s]\n\n" , buf); |
| 73 | } |
| 74 | |
| 75 | len = mp_int_unsigned_len(z: &v); |
| 76 | printf(format: "%d bytes needed to write this value as unsigned.\n" , len); |
| 77 | |
| 78 | res = mp_int_to_unsigned(z: &v, buf, limit: sizeof(buf)); |
| 79 | printf(format: "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(format: "%d." , buf[ix]); |
| 85 | } |
| 86 | |
| 87 | printf(format: "%d\n" , buf[ix]); |
| 88 | } else { |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | res = mp_int_read_unsigned(z: &w, buf, len); |
| 93 | printf(format: "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(z: &w, radix: 10, str: (char *)buf, limit: sizeof(buf)); |
| 97 | |
| 98 | printf(format: "[%s]\n\n" , buf); |
| 99 | } |
| 100 | |
| 101 | mp_int_clear(z: &v); |
| 102 | mp_int_clear(z: &w); |
| 103 | return 0; |
| 104 | } |
| 105 | |