bio_dump.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 1995-2025 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. if (n < 0)
  43. return -1;
  44. for (j = 0; j < dump_width; j++) {
  45. if (SPACE(buf, n, 3)) {
  46. if (((i * dump_width) + j) >= len) {
  47. strcpy(buf + n, " ");
  48. } else {
  49. ch = *(s + i * dump_width + j) & 0xff;
  50. BIO_snprintf(buf + n, 4, "%02x%c", ch,
  51. j == 7 ? '-' : ' ');
  52. }
  53. n += 3;
  54. }
  55. }
  56. if (SPACE(buf, n, 2)) {
  57. strcpy(buf + n, " ");
  58. n += 2;
  59. }
  60. for (j = 0; j < dump_width; j++) {
  61. if (((i * dump_width) + j) >= len)
  62. break;
  63. if (SPACE(buf, n, 1)) {
  64. ch = *(s + i * dump_width + j) & 0xff;
  65. #ifndef CHARSET_EBCDIC
  66. buf[n++] = ((ch >= ' ') && (ch <= '~')) ? ch : '.';
  67. #else
  68. buf[n++] = ((ch >= os_toascii[' ']) && (ch <= os_toascii['~']))
  69. ? os_toebcdic[ch]
  70. : '.';
  71. #endif
  72. buf[n] = '\0';
  73. }
  74. }
  75. if (SPACE(buf, n, 1)) {
  76. buf[n++] = '\n';
  77. buf[n] = '\0';
  78. }
  79. /*
  80. * if this is the last call then update the ddt_dump thing so that we
  81. * will move the selection point in the debug window
  82. */
  83. res = cb((void *)buf, n, u);
  84. if (res < 0)
  85. return res;
  86. ret += res;
  87. }
  88. return ret;
  89. }
  90. #ifndef OPENSSL_NO_STDIO
  91. static int write_fp(const void *data, size_t len, void *fp)
  92. {
  93. return UP_fwrite(data, len, 1, fp);
  94. }
  95. int BIO_dump_fp(FILE *fp, const void *s, int len)
  96. {
  97. return BIO_dump_cb(write_fp, fp, s, len);
  98. }
  99. int BIO_dump_indent_fp(FILE *fp, const void *s, int len, int indent)
  100. {
  101. return BIO_dump_indent_cb(write_fp, fp, s, len, indent);
  102. }
  103. #endif
  104. static int write_bio(const void *data, size_t len, void *bp)
  105. {
  106. return BIO_write((BIO *)bp, (const char *)data, len);
  107. }
  108. int BIO_dump(BIO *bp, const void *s, int len)
  109. {
  110. return BIO_dump_cb(write_bio, bp, s, len);
  111. }
  112. int BIO_dump_indent(BIO *bp, const void *s, int len, int indent)
  113. {
  114. return BIO_dump_indent_cb(write_bio, bp, s, len, indent);
  115. }
  116. int BIO_hex_string(BIO *out, int indent, int width, const void *data,
  117. int datalen)
  118. {
  119. const unsigned char *d = data;
  120. int i, j = 0;
  121. if (datalen < 1)
  122. return 1;
  123. for (i = 0; i < datalen - 1; i++) {
  124. if (i && !j)
  125. BIO_printf(out, "%*s", indent, "");
  126. BIO_printf(out, "%02X:", d[i]);
  127. if (++j >= width) {
  128. j = 0;
  129. BIO_printf(out, "\n");
  130. }
  131. }
  132. if (i && !j)
  133. BIO_printf(out, "%*s", indent, "");
  134. BIO_printf(out, "%02X", d[datalen - 1]);
  135. return 1;
  136. }