security/nss/lib/freebl/mpi/tests/mptest-6.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/tests/mptest-6.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/*
     1.5 + *  Simple test driver for MPI library
     1.6 + *
     1.7 + *  Test 6: Output functions
     1.8 + *
     1.9 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +#include <stdlib.h>
    1.15 +#include <string.h>
    1.16 +#include <ctype.h>
    1.17 +#include <limits.h>
    1.18 +
    1.19 +#include "mpi.h"
    1.20 +
    1.21 +void print_buf(FILE *ofp, char *buf, int len)
    1.22 +{
    1.23 +  int ix, brk = 0;
    1.24 +
    1.25 +  for(ix = 0; ix < len; ix++) {
    1.26 +    fprintf(ofp, "%02X ", buf[ix]);
    1.27 +
    1.28 +    brk = (brk + 1) & 0xF;
    1.29 +    if(!brk) 
    1.30 +      fputc('\n', ofp);
    1.31 +  }
    1.32 +
    1.33 +  if(brk)
    1.34 +    fputc('\n', ofp);
    1.35 +
    1.36 +}
    1.37 +
    1.38 +int main(int argc, char *argv[])
    1.39 +{
    1.40 +  int       ix, size;
    1.41 +  mp_int    a;
    1.42 +  char     *buf;
    1.43 +
    1.44 +  if(argc < 2) {
    1.45 +    fprintf(stderr, "Usage: %s <a>\n", argv[0]);
    1.46 +    return 1;
    1.47 +  }
    1.48 +
    1.49 +  printf("Test 6: Output functions\n\n");
    1.50 +
    1.51 +  mp_init(&a);
    1.52 +
    1.53 +  mp_read_radix(&a, argv[1], 10);
    1.54 +
    1.55 +  printf("\nConverting to a string:\n");
    1.56 +
    1.57 +  printf("Rx Size Representation\n");
    1.58 +  for(ix = 2; ix <= MAX_RADIX; ix++) {
    1.59 +    size = mp_radix_size(&a, ix);
    1.60 +
    1.61 +    buf = calloc(size, sizeof(char));
    1.62 +    mp_toradix(&a, buf, ix);
    1.63 +    printf("%2d: %3d: %s\n", ix, size, buf);
    1.64 +    free(buf);
    1.65 +
    1.66 +  }
    1.67 +
    1.68 +  printf("\nRaw output:\n");
    1.69 +  size = mp_raw_size(&a);
    1.70 +  buf = calloc(size, sizeof(char));
    1.71 +
    1.72 +  printf("Size:  %d bytes\n", size);
    1.73 +
    1.74 +  mp_toraw(&a, buf);
    1.75 +  print_buf(stdout, buf, size);
    1.76 +  free(buf);
    1.77 +  
    1.78 +  mp_clear(&a);
    1.79 +
    1.80 +  return 0;
    1.81 +}

mercurial