Coverage Report

Created: 2026-02-14 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/bio/hexdump.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
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
using namespace bssl;
24
25
namespace {
26
// hexdump_ctx contains the state of a hexdump.
27
struct hexdump_ctx {
28
  BIO *bio;
29
  char right_chars[18];  // the contents of the right-hand side, ASCII dump.
30
  unsigned used;         // number of bytes in the current line.
31
  size_t n;              // number of bytes total.
32
  unsigned indent;
33
};
34
}  // namespace
35
36
0
static void hexbyte(char *out, uint8_t b) {
37
0
  static const char hextable[] = "0123456789abcdef";
38
0
  out[0] = hextable[b >> 4];
39
0
  out[1] = hextable[b & 0x0f];
40
0
}
41
42
0
static char to_char(uint8_t b) {
43
0
  if (b < 32 || b > 126) {
44
0
    return '.';
45
0
  }
46
0
  return b;
47
0
}
48
49
// hexdump_write adds |len| bytes of |data| to the current hex dump described by
50
// |ctx|.
51
static int hexdump_write(struct hexdump_ctx *ctx, const uint8_t *data,
52
0
                         size_t len) {
53
0
  char buf[10];
54
0
  unsigned l;
55
56
  // Output lines look like:
57
  // 00000010  2e 2f 30 31 32 33 34 35  36 37 38 ... 3c 3d // |./0123456789:;<=|
58
  // ^ offset                          ^ extra space           ^ ASCII of line
59
60
0
  for (size_t i = 0; i < len; i++) {
61
0
    if (ctx->used == 0) {
62
      // The beginning of a line.
63
0
      BIO_indent(ctx->bio, ctx->indent, UINT_MAX);
64
65
0
      hexbyte(&buf[0], ctx->n >> 24);
66
0
      hexbyte(&buf[2], ctx->n >> 16);
67
0
      hexbyte(&buf[4], ctx->n >> 8);
68
0
      hexbyte(&buf[6], ctx->n);
69
0
      buf[8] = buf[9] = ' ';
70
0
      if (BIO_write(ctx->bio, buf, 10) < 0) {
71
0
        return 0;
72
0
      }
73
0
    }
74
75
0
    hexbyte(buf, data[i]);
76
0
    buf[2] = ' ';
77
0
    l = 3;
78
0
    if (ctx->used == 7) {
79
      // There's an additional space after the 8th byte.
80
0
      buf[3] = ' ';
81
0
      l = 4;
82
0
    } else if (ctx->used == 15) {
83
      // At the end of the line there's an extra space and the bar for the
84
      // right column.
85
0
      buf[3] = ' ';
86
0
      buf[4] = '|';
87
0
      l = 5;
88
0
    }
89
90
0
    if (BIO_write(ctx->bio, buf, l) < 0) {
91
0
      return 0;
92
0
    }
93
0
    ctx->right_chars[ctx->used] = to_char(data[i]);
94
0
    ctx->used++;
95
0
    ctx->n++;
96
0
    if (ctx->used == 16) {
97
0
      ctx->right_chars[16] = '|';
98
0
      ctx->right_chars[17] = '\n';
99
0
      if (BIO_write(ctx->bio, ctx->right_chars, sizeof(ctx->right_chars)) < 0) {
100
0
        return 0;
101
0
      }
102
0
      ctx->used = 0;
103
0
    }
104
0
  }
105
106
0
  return 1;
107
0
}
108
109
// finish flushes any buffered data in |ctx|.
110
0
static int finish(struct hexdump_ctx *ctx) {
111
  // See the comments in |hexdump| for the details of this format.
112
0
  const unsigned n_bytes = ctx->used;
113
0
  unsigned l;
114
0
  char buf[5];
115
116
0
  if (n_bytes == 0) {
117
0
    return 1;
118
0
  }
119
120
0
  OPENSSL_memset(buf, ' ', 4);
121
0
  buf[4] = '|';
122
123
0
  for (; ctx->used < 16; ctx->used++) {
124
0
    l = 3;
125
0
    if (ctx->used == 7) {
126
0
      l = 4;
127
0
    } else if (ctx->used == 15) {
128
0
      l = 5;
129
0
    }
130
0
    if (BIO_write(ctx->bio, buf, l) < 0) {
131
0
      return 0;
132
0
    }
133
0
  }
134
135
0
  ctx->right_chars[n_bytes] = '|';
136
0
  ctx->right_chars[n_bytes + 1] = '\n';
137
0
  if (BIO_write(ctx->bio, ctx->right_chars, n_bytes + 2) < 0) {
138
0
    return 0;
139
0
  }
140
0
  return 1;
141
0
}
142
143
0
int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, unsigned indent) {
144
0
  struct hexdump_ctx ctx;
145
0
  OPENSSL_memset(&ctx, 0, sizeof(ctx));
146
0
  ctx.bio = bio;
147
0
  ctx.indent = indent;
148
149
0
  if (!hexdump_write(&ctx, data, len) || !finish(&ctx)) {
150
0
    return 0;
151
0
  }
152
153
0
  return 1;
154
0
}