home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / BNLIB / BNPRINT.C < prev    next >
C/C++ Source or Header  |  1996-05-16  |  2KB  |  77 lines

  1. /*
  2.  * bnprint.c - Print a bignum, for debugging purposes.
  3.  *
  4.  * Copyright (c) 1995  Colin Plumb.  All rights reserved.
  5.  * For licensing and other legal details, see the file legal.c.
  6.  */
  7. #ifndef HAVE_CONFIG_H
  8. #define HAVE_CONFIG_H 0
  9. #endif
  10. #if HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13.  
  14. /*
  15.  * Some compilers complain about #if FOO if FOO isn't defined,
  16.  * so do the ANSI-mandated thing explicitly...
  17.  */
  18. #ifndef NO_STRING_H
  19. #define NO_STRING_H 0
  20. #endif
  21. #ifndef HAVE_STRINGS_H
  22. #define HAVE_STRINGS_H 0
  23. #endif
  24.  
  25. #include <stdio.h>
  26.  
  27. #if !NO_STRING_H
  28. #include <string.h>
  29. #elif HAVE_STRINGS_H
  30. #include <strings.h>
  31. #endif
  32.  
  33. #include "bn.h"
  34. #include "bnprint.h"
  35.  
  36. #include "kludge.h"
  37.  
  38. int
  39. bnPrint(FILE *f, char const *prefix, struct BigNum const *bn,
  40.     char const *suffix)
  41. {
  42.     unsigned char temp[32];    /* How much to print on one line */
  43.     unsigned len;
  44.     size_t i;
  45.  
  46.     if (prefix && fputs(prefix, f) < 0)
  47.         return EOF;
  48.  
  49.     len = (bnBits(bn) + 7)/ 8;
  50.  
  51.     if (!len) {
  52.         if (putc('0', f) < 0)
  53.             return EOF;
  54.     } else {
  55.         while (len > sizeof(temp)) {
  56.             len -= sizeof(temp);
  57.             bnExtractBigBytes(bn, temp, len, sizeof(temp));
  58.             for (i = 0; i < sizeof(temp); i++)
  59.                 if (fprintf(f, "%02X", temp[i]) < 0)
  60.                     return EOF;
  61.             if (putc('\\', f) < 0 || putc('\n', f) < 0)
  62.                 return EOF;
  63.             if (prefix) {
  64.                 i = strlen(prefix);
  65.                 while (i--)
  66.                     if (putc(' ', f) < 0)
  67.                         return EOF;
  68.             }
  69.         }
  70.         bnExtractBigBytes(bn, temp, 0, len);
  71.         for (i = 0; i < len; i++)
  72.             if (fprintf(f, "%02X", temp[i]) < 0)
  73.                 return EOF;
  74.     }
  75.     return suffix ?    fputs(suffix, f) : 0;
  76. }
  77.