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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

77.63
/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
   buffer_insert(m_in, m_position, input, length);
37✔
68
   if(m_position + length >= m_in.size()) {
37✔
69
      encode_and_send(m_in.data(), m_in.size());
×
70
      input += (m_in.size() - m_position);
×
71
      length -= (m_in.size() - m_position);
×
72
      while(length >= m_in.size()) {
×
73
         encode_and_send(input, m_in.size());
×
74
         input += m_in.size();
×
75
         length -= m_in.size();
×
76
      }
77
      copy_mem(m_in.data(), input, length);
×
78
      m_position = 0;
×
79
   }
80
   m_position += length;
37✔
81
}
37✔
82

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

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

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

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

115
      send(m_out, written);
23✔
116

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

123
      length -= to_copy;
23✔
124
      input += to_copy;
23✔
125
   }
126
}
23✔
127

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

136
   send(m_out, written);
23✔
137

138
   const bool not_full_bytes = consumed != m_position;
23✔
139

140
   m_position = 0;
23✔
141

142
   if(not_full_bytes)
23✔
143
      throw Invalid_Argument("Hex_Decoder: Input not full bytes");
×
144
}
23✔
145

146
}
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