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

randombit / botan / 6513140094

13 Oct 2023 09:12PM UTC coverage: 91.704% (-0.003%) from 91.707%
6513140094

push

github

web-flow
Merge pull request #3757 from randombit/jack/remove-buffer-insert

Remove use of buffer_insert from filters, MACs, stream ciphers

80025 of 87264 relevant lines covered (91.7%)

8532652.1 hits per line

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

77.92
/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 <algorithm>
13

14
namespace Botan {
15

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

21
/*
22
* Hex_Encoder Constructor
23
*/
24
Hex_Encoder::Hex_Encoder(bool breaks, size_t length, Case c) : m_casing(c), m_line_length(breaks ? length : 0) {
18✔
25
   m_in.resize(HEX_CODEC_BUFFER_SIZE);
10✔
26
   m_out.resize(2 * m_in.size());
10✔
27
   m_counter = m_position = 0;
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
   m_counter = m_position = 0;
×
37
}
×
38

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

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

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

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

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

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

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

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

118
      send(m_out, written);
23✔
119

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

127
      length -= to_copy;
23✔
128
      input += to_copy;
23✔
129
   }
130
}
23✔
131

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

140
   send(m_out, written);
23✔
141

142
   const bool not_full_bytes = consumed != m_position;
23✔
143

144
   m_position = 0;
23✔
145

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

151
}  // 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