security/nss/lib/freebl/mpi/utils/basecvt.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * basecvt.c
michael@0 3 *
michael@0 4 * Convert integer values specified on the command line from one input
michael@0 5 * base to another. Accepts input and output bases between 2 and 36
michael@0 6 * inclusive.
michael@0 7 *
michael@0 8 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 9 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 11
michael@0 12 #include <stdio.h>
michael@0 13 #include <stdlib.h>
michael@0 14 #include <string.h>
michael@0 15
michael@0 16 #include "mpi.h"
michael@0 17
michael@0 18 #define IBASE 10
michael@0 19 #define OBASE 16
michael@0 20 #define USAGE "Usage: %s ibase obase [value]\n"
michael@0 21 #define MAXBASE 64
michael@0 22 #define MINBASE 2
michael@0 23
michael@0 24 int main(int argc, char *argv[])
michael@0 25 {
michael@0 26 int ix, ibase = IBASE, obase = OBASE;
michael@0 27 mp_int val;
michael@0 28
michael@0 29 ix = 1;
michael@0 30 if(ix < argc) {
michael@0 31 ibase = atoi(argv[ix++]);
michael@0 32
michael@0 33 if(ibase < MINBASE || ibase > MAXBASE) {
michael@0 34 fprintf(stderr, "%s: input radix must be between %d and %d inclusive\n",
michael@0 35 argv[0], MINBASE, MAXBASE);
michael@0 36 return 1;
michael@0 37 }
michael@0 38 }
michael@0 39 if(ix < argc) {
michael@0 40 obase = atoi(argv[ix++]);
michael@0 41
michael@0 42 if(obase < MINBASE || obase > MAXBASE) {
michael@0 43 fprintf(stderr, "%s: output radix must be between %d and %d inclusive\n",
michael@0 44 argv[0], MINBASE, MAXBASE);
michael@0 45 return 1;
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 mp_init(&val);
michael@0 50 while(ix < argc) {
michael@0 51 char *out;
michael@0 52 int outlen;
michael@0 53
michael@0 54 mp_read_radix(&val, argv[ix++], ibase);
michael@0 55
michael@0 56 outlen = mp_radix_size(&val, obase);
michael@0 57 out = calloc(outlen, sizeof(char));
michael@0 58 mp_toradix(&val, out, obase);
michael@0 59
michael@0 60 printf("%s\n", out);
michael@0 61 free(out);
michael@0 62 }
michael@0 63
michael@0 64 mp_clear(&val);
michael@0 65
michael@0 66 return 0;
michael@0 67 }

mercurial