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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/utils/basecvt.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,67 @@
     1.4 +/*
     1.5 + *  basecvt.c
     1.6 + *
     1.7 + *  Convert integer values specified on the command line from one input
     1.8 + *  base to another.  Accepts input and output bases between 2 and 36
     1.9 + *  inclusive.
    1.10 + *
    1.11 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.12 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.13 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.14 +
    1.15 +#include <stdio.h>
    1.16 +#include <stdlib.h>
    1.17 +#include <string.h>
    1.18 +
    1.19 +#include "mpi.h"
    1.20 +
    1.21 +#define IBASE     10
    1.22 +#define OBASE     16
    1.23 +#define USAGE     "Usage: %s ibase obase [value]\n"
    1.24 +#define MAXBASE   64
    1.25 +#define MINBASE   2
    1.26 +
    1.27 +int main(int argc, char *argv[])
    1.28 +{
    1.29 +  int    ix, ibase = IBASE, obase = OBASE;
    1.30 +  mp_int val;
    1.31 +
    1.32 +  ix = 1;
    1.33 +  if(ix < argc) {
    1.34 +    ibase = atoi(argv[ix++]);
    1.35 +    
    1.36 +    if(ibase < MINBASE || ibase > MAXBASE) {
    1.37 +      fprintf(stderr, "%s: input radix must be between %d and %d inclusive\n",
    1.38 +	      argv[0], MINBASE, MAXBASE);
    1.39 +      return 1;
    1.40 +    }
    1.41 +  }
    1.42 +  if(ix < argc) {
    1.43 +    obase = atoi(argv[ix++]);
    1.44 +
    1.45 +    if(obase < MINBASE || obase > MAXBASE) {
    1.46 +      fprintf(stderr, "%s: output radix must be between %d and %d inclusive\n",
    1.47 +	      argv[0], MINBASE, MAXBASE);
    1.48 +      return 1;
    1.49 +    }
    1.50 +  }
    1.51 +
    1.52 +  mp_init(&val);
    1.53 +  while(ix < argc) {
    1.54 +    char  *out;
    1.55 +    int    outlen;
    1.56 +
    1.57 +    mp_read_radix(&val, argv[ix++], ibase);
    1.58 +
    1.59 +    outlen = mp_radix_size(&val, obase);
    1.60 +    out = calloc(outlen, sizeof(char));
    1.61 +    mp_toradix(&val, out, obase);
    1.62 +
    1.63 +    printf("%s\n", out);
    1.64 +    free(out);
    1.65 +  }
    1.66 +
    1.67 +  mp_clear(&val);
    1.68 +
    1.69 +  return 0;
    1.70 +}

mercurial