• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 16249365818

13 Jul 2025 12:56PM UTC coverage: 90.618% (+0.002%) from 90.616%
16249365818

Pull #4985

github

web-flow
Merge 34acb0b10 into cf74a5db8
Pull Request #4985: Enable and fix clang-tidy warning cppcoreguidelines-prefer-member-initializer

99527 of 109831 relevant lines covered (90.62%)

12274327.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.38
/src/lib/filters/hex_filt.cpp
1
/*
2
* Hex Encoder/Decoder
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/filters.h>
9

10
#include <botan/exceptn.h>
11
#include <botan/hex.h>
12
#include <botan/mem_ops.h>
13
#include <algorithm>
14

15
namespace Botan {
16

17
/**
18
* Size used for internal buffer in hex encoder/decoder
19
*/
20
const size_t HEX_CODEC_BUFFER_SIZE = 256;
21

22
/*
23
* Hex_Encoder Constructor
24
*/
25
Hex_Encoder::Hex_Encoder(bool breaks, size_t length, Case c) : m_casing(c), m_line_length(breaks ? length : 0) {
18✔
26
   m_in.resize(HEX_CODEC_BUFFER_SIZE);
10✔
27
   m_out.resize(2 * m_in.size());
10✔
28
}
10✔
29

30
/*
31
* Hex_Encoder Constructor
32
*/
33
Hex_Encoder::Hex_Encoder(Case c) : m_casing(c), m_line_length(0) {
×
34
   m_in.resize(HEX_CODEC_BUFFER_SIZE);
×
35
   m_out.resize(2 * m_in.size());
×
36
}
×
37

38
/*
39
* Encode and send a block
40
*/
41
void Hex_Encoder::encode_and_send(const uint8_t block[], size_t length) {
37✔
42
   hex_encode(cast_uint8_ptr_to_char(m_out.data()), block, length, m_casing == Uppercase);
37✔
43

44
   if(m_line_length == 0) {
37✔
45
      send(m_out, 2 * length);
35✔
46
   } else {
47
      size_t remaining = 2 * length, offset = 0;
2✔
48
      while(remaining > 0) {
9✔
49
         size_t sent = std::min(m_line_length - m_counter, remaining);
7✔
50
         send(&m_out[offset], sent);
7✔
51
         m_counter += sent;
7✔
52
         remaining -= sent;
7✔
53
         offset += sent;
7✔
54
         if(m_counter == m_line_length) {
7✔
55
            send('\n');
6✔
56
            m_counter = 0;
6✔
57
         }
58
      }
59
   }
60
}
37✔
61

62
/*
63
* Convert some data into hex format
64
*/
65
void Hex_Encoder::write(const uint8_t input[], size_t length) {
37✔
66
   const size_t initial_fill = std::min(m_in.size() - m_position, length);
37✔
67
   copy_mem(&m_in[m_position], input, initial_fill);
37✔
68

69
   if(m_position + length >= m_in.size()) {
37✔
70
      encode_and_send(m_in.data(), m_in.size());
×
71
      input += (m_in.size() - m_position);
×
72
      length -= (m_in.size() - m_position);
×
73
      while(length >= m_in.size()) {
×
74
         encode_and_send(input, m_in.size());
×
75
         input += m_in.size();
×
76
         length -= m_in.size();
×
77
      }
78
      copy_mem(m_in.data(), input, length);
×
79
      m_position = 0;
×
80
   }
81
   m_position += length;
37✔
82
}
37✔
83

84
/*
85
* Flush buffers
86
*/
87
void Hex_Encoder::end_msg() {
37✔
88
   encode_and_send(m_in.data(), m_position);
37✔
89
   if(m_counter > 0 && m_line_length > 0) {
37✔
90
      send('\n');
1✔
91
   }
92
   m_counter = m_position = 0;
37✔
93
}
37✔
94

95
/*
96
* Hex_Decoder Constructor
97
*/
98
Hex_Decoder::Hex_Decoder(Decoder_Checking c) : m_checking(c) {
7✔
99
   m_in.resize(HEX_CODEC_BUFFER_SIZE);
7✔
100
   m_out.resize(m_in.size() / 2);
7✔
101
}
7✔
102

103
/*
104
* Convert some data from hex format
105
*/
106
void Hex_Decoder::write(const uint8_t input[], size_t length) {
23✔
107
   while(length > 0) {
46✔
108
      size_t to_copy = std::min<size_t>(length, m_in.size() - m_position);
23✔
109
      copy_mem(&m_in[m_position], input, to_copy);
23✔
110
      m_position += to_copy;
23✔
111

112
      size_t consumed = 0;
23✔
113
      size_t written =
23✔
114
         hex_decode(m_out.data(), cast_uint8_ptr_to_char(m_in.data()), m_position, consumed, m_checking != FULL_CHECK);
23✔
115

116
      send(m_out, written);
23✔
117

118
      if(consumed != m_position) {
23✔
119
         copy_mem(m_in.data(), m_in.data() + consumed, m_position - consumed);
×
120
         m_position = m_position - consumed;
×
121
      } else {
122
         m_position = 0;
23✔
123
      }
124

125
      length -= to_copy;
23✔
126
      input += to_copy;
23✔
127
   }
128
}
23✔
129

130
/*
131
* Flush buffers
132
*/
133
void Hex_Decoder::end_msg() {
23✔
134
   size_t consumed = 0;
23✔
135
   size_t written =
23✔
136
      hex_decode(m_out.data(), cast_uint8_ptr_to_char(m_in.data()), m_position, consumed, m_checking != FULL_CHECK);
23✔
137

138
   send(m_out, written);
23✔
139

140
   const bool not_full_bytes = consumed != m_position;
23✔
141

142
   m_position = 0;
23✔
143

144
   if(not_full_bytes) {
23✔
145
      throw Invalid_Argument("Hex_Decoder: Input not full bytes");
×
146
   }
147
}
23✔
148

149
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc