b_dump.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Stolen from tjh's ssl/ssl_trc.c stuff.
  11. */
  12. #include <stdio.h>
  13. #include "bio_local.h"
  14. #define DUMP_WIDTH 16
  15. #define DUMP_WIDTH_LESS_INDENT(i) (DUMP_WIDTH - ((i - (i > 6 ? 6 : i) + 3) / 4))
  16. #define SPACE(buf, pos, n) (sizeof(buf) - (pos) > (n))
  17. int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
  18. void *u, const void *s, int len)
  19. {
  20. return BIO_dump_indent_cb(cb, u, s, len, 0);
  21. }
  22. int BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
  23. void *u, const void *v, int len, int indent)
  24. {
  25. const unsigned char *s = v;
  26. int ret = 0;
  27. char buf[288 + 1];
  28. int i, j, rows, n;
  29. unsigned char ch;
  30. int dump_width;
  31. if (indent < 0)
  32. indent = 0;
  33. else if (indent > 64)
  34. indent = 64;
  35. dump_width = DUMP_WIDTH_LESS_INDENT(indent);
  36. rows = len / dump_width;
  37. if ((rows * dump_width) < len)
  38. rows++;
  39. for (i = 0; i < rows; i++) {
  40. n = BIO_snprintf(buf, sizeof(buf), "%*s%04x - ", indent, "",
  41. i * dump_width);
  42. for (j = 0; j < dump_width; j++) {
  43. if (SPACE(buf, n, 3)) {
  44. if (((i * dump_width) + j) >= len) {
  45. strcpy(buf + n, " ");
  46. } else {
  47. ch = *(s + i * dump_width + j) & 0xff;
  48. BIO_snprintf(buf + n, 4, "%02x%c", ch,
  49. j == 7 ? '-' : ' ');
  50. }
  51. n += 3;
  52. }
  53. }
  54. if (SPACE(buf, n, 2)) {
  55. strcpy(buf + n, " ");
  56. n += 2;
  57. }
  58. for (j = 0; j < dump_width; j++) {
  59. if (((i * dump_width) + j) >= len)
  60. break;
  61. if (SPACE(buf, n, 1)) {
  62. ch = *(s + i * dump_width + j) & 0xff;
  63. #ifndef CHARSET_EBCDIC
  64. buf[n++] = ((ch >= ' ') && (ch <= '~')) ? ch : '.';
  65. #else
  66. buf[n++] = ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
  67. ? os_toebcdic[ch]
  68. : '.';
  69. #endif
  70. buf[n] = '\0';
  71. }
  72. }
  73. if (SPACE(buf, n, 1)) {
  74. buf[n++] = '\n';
  75. buf[n] = '\0';
  76. }
  77. /*
  78. * if this is the last call then update the ddt_dump thing so that we
  79. * will move the selection point in the debug window
  80. */
  81. ret += cb((void *)buf, n, u);
  82. }
  83. return ret;
  84. }
  85. #ifndef OPENSSL_NO_STDIO
  86. static int write_fp(const void *data, size_t len, void *fp)
  87. {
  88. return UP_fwrite(data, len, 1, fp);
  89. }
  90. int BIO_dump_fp(FILE *fp, const void *s, int len)
  91. {
  92. return BIO_dump_cb(write_fp, fp, s, len);
  93. }
  94. int BIO_dump_indent_fp(FILE *fp, const void *s, int len, int indent)
  95. {
  96. return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
  97. }
  98. #endif
  99. static int write_bio(const void *data, size_t len, void *bp)
  100. {
  101. return BIO_write((BIO *)bp, (const char *)data, len);
  102. }
  103. int BIO_dump(BIO *bp, const void *s, int len)
  104. {
  105. return BIO_dump_cb(write_bio, bp, s, len);
  106. }
  107. int BIO_dump_indent(BIO *bp, const void *s, int len, int indent)
  108. {
  109. return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
  110. }
  111. int BIO_hex_string(BIO *out, int indent, int width, const void *data,
  112. int datalen)
  113. {
  114. const unsigned char *d = data;
  115. int i, j = 0;
  116. if (datalen < 1)
  117. return 1;
  118. for (i = 0; i < datalen - 1; i++) {
  119. if (i && !j)
  120. BIO_printf(out, "%*s", indent, "");
  121. BIO_printf(out, "%02X:", d[i]);
  122. j = (j + 1) % width;
  123. if (!j)
  124. BIO_printf(out, "\n");
  125. }
  126. if (i && !j)
  127. BIO_printf(out, "%*s", indent, "");
  128. BIO_printf(out, "%02X", d[datalen - 1]);
  129. return 1;
  130. }