Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/hexdump.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/bio.h>
5
6 #include <limits.h>
7 #include <string.h>
8
9 #include "../internal.h"
10
11
12 // hexdump_ctx contains the state of a hexdump.
13 struct hexdump_ctx {
14 BIO *bio;
15 char right_chars[18]; // the contents of the right-hand side, ASCII dump.
16 unsigned used; // number of bytes in the current line.
17 size_t n; // number of bytes total.
18 unsigned indent;
19 };
20
21 static void hexbyte(char *out, uint8_t b) {
22 static const char hextable[] = "0123456789abcdef";
23 out[0] = hextable[b>>4];
24 out[1] = hextable[b&0x0f];
25 }
26
27 static char to_char(uint8_t b) {
28 if (b < 32 || b > 126) {
29 return '.';
30 }
31 return b;
32 }
33
34 // hexdump_write adds |len| bytes of |data| to the current hex dump described by
35 // |ctx|.
36 static int hexdump_write(struct hexdump_ctx *ctx, const uint8_t *data,
37 size_t len) {
38 char buf[10];
39 unsigned l;
40
41 // Output lines look like:
42 // 00000010 2e 2f 30 31 32 33 34 35 36 37 38 ... 3c 3d // |./0123456789:;<=|
43 // ^ offset ^ extra space ^ ASCII of line
44
45 for (size_t i = 0; i < len; i++) {
46 if (ctx->used == 0) {
47 // The beginning of a line.
48 if (!BIO_indent(ctx->bio, ctx->indent, UINT_MAX)) {
49 return 0;
50 }
51
52 hexbyte(&buf[0], ctx->n >> 24);
53 hexbyte(&buf[2], ctx->n >> 16);
54 hexbyte(&buf[4], ctx->n >> 8);
55 hexbyte(&buf[6], ctx->n);
56 buf[8] = buf[9] = ' ';
57 if (BIO_write(ctx->bio, buf, 10) < 0) {
58 return 0;
59 }
60 }
61
62 hexbyte(buf, data[i]);
63 buf[2] = ' ';
64 l = 3;
65 if (ctx->used == 7) {
66 // There's an additional space after the 8th byte.
67 buf[3] = ' ';
68 l = 4;
69 } else if (ctx->used == 15) {
70 // At the end of the line there's an extra space and the bar for the
71 // right column.
72 buf[3] = ' ';
73 buf[4] = '|';
74 l = 5;
75 }
76
77 if (BIO_write(ctx->bio, buf, l) < 0) {
78 return 0;
79 }
80 ctx->right_chars[ctx->used] = to_char(data[i]);
81 ctx->used++;
82 ctx->n++;
83 if (ctx->used == 16) {
84 ctx->right_chars[16] = '|';
85 ctx->right_chars[17] = '\n';
86 if (BIO_write(ctx->bio, ctx->right_chars, sizeof(ctx->right_chars)) < 0) {
87 return 0;
88 }
89 ctx->used = 0;
90 }
91 }
92
93 return 1;
94 }
95
96 // finish flushes any buffered data in |ctx|.
97 static int finish(struct hexdump_ctx *ctx) {
98 // See the comments in |hexdump| for the details of this format.
99 const unsigned n_bytes = ctx->used;
100 unsigned l;
101 char buf[5];
102
103 if (n_bytes == 0) {
104 return 1;
105 }
106
107 OPENSSL_memset(buf, ' ', 4);
108 buf[4] = '|';
109
110 for (; ctx->used < 16; ctx->used++) {
111 l = 3;
112 if (ctx->used == 7) {
113 l = 4;
114 } else if (ctx->used == 15) {
115 l = 5;
116 }
117 if (BIO_write(ctx->bio, buf, l) < 0) {
118 return 0;
119 }
120 }
121
122 ctx->right_chars[n_bytes] = '|';
123 ctx->right_chars[n_bytes + 1] = '\n';
124 if (BIO_write(ctx->bio, ctx->right_chars, n_bytes + 2) < 0) {
125 return 0;
126 }
127 return 1;
128 }
129
130 int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, unsigned indent) {
131 struct hexdump_ctx ctx;
132 OPENSSL_memset(&ctx, 0, sizeof(ctx));
133 ctx.bio = bio;
134 ctx.indent = indent;
135
136 if (!hexdump_write(&ctx, data, len) || !finish(&ctx)) {
137 return 0;
138 }
139
140 return 1;
141 }
142
143 int BIO_dump(BIO *bio, const void *data, int len) {
144 if (bio == NULL || data == NULL || len < 0) {
145 return -1;
146 }
147
148 // Use a temporary memory BIO to capture the formatted output
149 int ret = -1;
150 BIO *mbio = BIO_new(BIO_s_mem());
151 if (mbio == NULL) {
152 return -1;
153 }
154
155 // Generate the hexdump to the memory BIO
156 if (!BIO_hexdump(mbio, (const uint8_t *)data, (size_t)len, 0)) {
157 goto err;
158 }
159
160 // Get the formatted content
161 const uint8_t *contents = NULL;
162 size_t content_len = 0;
163 if (!BIO_mem_contents(mbio, &contents, &content_len)) {
164 goto err;
165 }
166
167 // Write to the original BIO and return the exact bytes written
168 ret = BIO_write(bio, contents, content_len);
169
170 err:
171 BIO_free(mbio);
172 return ret;
173 }