| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bio.h> |
| 16 |
| 17 | #include <limits.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include "../internal.h" |
| 21 |
| 22 |
| 23 | namespace { |
| 24 | // hexdump_ctx contains the state of a hexdump. |
| 25 | struct hexdump_ctx { |
| 26 | BIO *bio; |
| 27 | char right_chars[18]; // the contents of the right-hand side, ASCII dump. |
| 28 | unsigned used; // number of bytes in the current line. |
| 29 | size_t n; // number of bytes total. |
| 30 | unsigned indent; |
| 31 | }; |
| 32 | } // namespace |
| 33 |
| 34 | static void hexbyte(char *out, uint8_t b) { |
| 35 | static const char hextable[] = "0123456789abcdef"; |
| 36 | out[0] = hextable[b >> 4]; |
| 37 | out[1] = hextable[b & 0x0f]; |
| 38 | } |
| 39 |
| 40 | static char to_char(uint8_t b) { |
| 41 | if (b < 32 || b > 126) { |
| 42 | return '.'; |
| 43 | } |
| 44 | return b; |
| 45 | } |
| 46 |
| 47 | // hexdump_write adds |len| bytes of |data| to the current hex dump described by |
| 48 | // |ctx|. |
| 49 | static int hexdump_write(struct hexdump_ctx *ctx, const uint8_t *data, |
| 50 | size_t len) { |
| 51 | char buf[10]; |
| 52 | unsigned l; |
| 53 |
| 54 | // Output lines look like: |
| 55 | // 00000010 2e 2f 30 31 32 33 34 35 36 37 38 ... 3c 3d // |./0123456789:;<=| |
| 56 | // ^ offset ^ extra space ^ ASCII of line |
| 57 |
| 58 | for (size_t i = 0; i < len; i++) { |
| 59 | if (ctx->used == 0) { |
| 60 | // The beginning of a line. |
| 61 | BIO_indent(ctx->bio, ctx->indent, UINT_MAX); |
| 62 |
| 63 | hexbyte(&buf[0], ctx->n >> 24); |
| 64 | hexbyte(&buf[2], ctx->n >> 16); |
| 65 | hexbyte(&buf[4], ctx->n >> 8); |
| 66 | hexbyte(&buf[6], ctx->n); |
| 67 | buf[8] = buf[9] = ' '; |
| 68 | if (BIO_write(ctx->bio, buf, 10) < 0) { |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 |
| 73 | hexbyte(buf, data[i]); |
| 74 | buf[2] = ' '; |
| 75 | l = 3; |
| 76 | if (ctx->used == 7) { |
| 77 | // There's an additional space after the 8th byte. |
| 78 | buf[3] = ' '; |
| 79 | l = 4; |
| 80 | } else if (ctx->used == 15) { |
| 81 | // At the end of the line there's an extra space and the bar for the |
| 82 | // right column. |
| 83 | buf[3] = ' '; |
| 84 | buf[4] = '|'; |
| 85 | l = 5; |
| 86 | } |
| 87 |
| 88 | if (BIO_write(ctx->bio, buf, l) < 0) { |
| 89 | return 0; |
| 90 | } |
| 91 | ctx->right_chars[ctx->used] = to_char(data[i]); |
| 92 | ctx->used++; |
| 93 | ctx->n++; |
| 94 | if (ctx->used == 16) { |
| 95 | ctx->right_chars[16] = '|'; |
| 96 | ctx->right_chars[17] = '\n'; |
| 97 | if (BIO_write(ctx->bio, ctx->right_chars, sizeof(ctx->right_chars)) < 0) { |
| 98 | return 0; |
| 99 | } |
| 100 | ctx->used = 0; |
| 101 | } |
| 102 | } |
| 103 |
| 104 | return 1; |
| 105 | } |
| 106 |
| 107 | // finish flushes any buffered data in |ctx|. |
| 108 | static int finish(struct hexdump_ctx *ctx) { |
| 109 | // See the comments in |hexdump| for the details of this format. |
| 110 | const unsigned n_bytes = ctx->used; |
| 111 | unsigned l; |
| 112 | char buf[5]; |
| 113 |
| 114 | if (n_bytes == 0) { |
| 115 | return 1; |
| 116 | } |
| 117 |
| 118 | OPENSSL_memset(buf, ' ', 4); |
| 119 | buf[4] = '|'; |
| 120 |
| 121 | for (; ctx->used < 16; ctx->used++) { |
| 122 | l = 3; |
| 123 | if (ctx->used == 7) { |
| 124 | l = 4; |
| 125 | } else if (ctx->used == 15) { |
| 126 | l = 5; |
| 127 | } |
| 128 | if (BIO_write(ctx->bio, buf, l) < 0) { |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 |
| 133 | ctx->right_chars[n_bytes] = '|'; |
| 134 | ctx->right_chars[n_bytes + 1] = '\n'; |
| 135 | if (BIO_write(ctx->bio, ctx->right_chars, n_bytes + 2) < 0) { |
| 136 | return 0; |
| 137 | } |
| 138 | return 1; |
| 139 | } |
| 140 |
| 141 | int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, unsigned indent) { |
| 142 | struct hexdump_ctx ctx; |
| 143 | OPENSSL_memset(&ctx, 0, sizeof(ctx)); |
| 144 | ctx.bio = bio; |
| 145 | ctx.indent = indent; |
| 146 |
| 147 | if (!hexdump_write(&ctx, data, len) || !finish(&ctx)) { |
| 148 | return 0; |
| 149 | } |
| 150 |
| 151 | return 1; |
| 152 | } |
| 153 |