2
0

bio_dump.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 1995-2021 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 res, 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. res = cb((void *)buf, n, u);
  82. if (res < 0)
  83. return res;
  84. ret += res;
  85. }
  86. return ret;
  87. }
  88. #ifndef OPENSSL_NO_STDIO
  89. static int write_fp(const void *data, size_t len, void *fp)
  90. {
  91. return UP_fwrite(data, len, 1, fp);
  92. }
  93. int BIO_dump_fp(FILE *fp, const void *s, int len)
  94. {
  95. return BIO_dump_cb(write_fp, fp, s, len);
  96. }
  97. int BIO_dump_indent_fp(FILE *fp, const void *s, int len, int indent)
  98. {
  99. return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
  100. }
  101. #endif
  102. static int write_bio(const void *data, size_t len, void *bp)
  103. {
  104. return BIO_write((BIO *)bp, (const char *)data, len);
  105. }
  106. int BIO_dump(BIO *bp, const void *s, int len)
  107. {
  108. return BIO_dump_cb(write_bio, bp, s, len);
  109. }
  110. int BIO_dump_indent(BIO *bp, const void *s, int len, int indent)
  111. {
  112. return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
  113. }
  114. int BIO_hex_string(BIO *out, int indent, int width, const void *data,
  115. int datalen)
  116. {
  117. const unsigned char *d = data;
  118. int i, j = 0;
  119. if (datalen < 1)
  120. return 1;
  121. for (i = 0; i < datalen - 1; i++) {
  122. if (i && !j)
  123. BIO_printf(out, "%*s", indent, "");
  124. BIO_printf(out, "%02X:", d[i]);
  125. if (++j >= width) {
  126. j = 0;
  127. BIO_printf(out, "\n");
  128. }
  129. }
  130. if (i && !j)
  131. BIO_printf(out, "%*s", indent, "");
  132. BIO_printf(out, "%02X", d[datalen - 1]);
  133. return 1;
  134. }