2
0

b_dump.c 4.0 KB

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