bnprint.c 1.5 KB

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