• 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

82.72
/src/lib/filters/b64_filt.cpp
1
/*
2
* Base64 Encoder/Decoder
3
* (C) 1999-2010 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/base64.h>
11
#include <botan/exceptn.h>
12
#include <algorithm>
13

14
namespace Botan {
15

16
/*
17
* Base64_Encoder Constructor
18
*/
19
Base64_Encoder::Base64_Encoder(bool line_breaks, size_t line_length, bool trailing_newline) :
5✔
20
      m_line_length(line_breaks ? line_length : 0),
10✔
21
      m_trailing_newline(trailing_newline && line_breaks),
5✔
22
      m_in(48),
5✔
23
      m_out(64),
5✔
24
      m_position(0),
5✔
25
      m_out_position(0) {}
10✔
26

27
/*
28
* Encode and send a block
29
*/
30
void Base64_Encoder::encode_and_send(const uint8_t input[], size_t length, bool final_inputs) {
8✔
31
   while(length) {
16✔
32
      const size_t proc = std::min(length, m_in.size());
8✔
33

34
      size_t consumed = 0;
8✔
35
      size_t produced = base64_encode(cast_uint8_ptr_to_char(m_out.data()), input, proc, consumed, final_inputs);
8✔
36

37
      do_output(m_out.data(), produced);
8✔
38

39
      // FIXME: s/proc/consumed/?
40
      input += proc;
8✔
41
      length -= proc;
8✔
42
   }
43
}
8✔
44

45
/*
46
* Handle the output
47
*/
48
void Base64_Encoder::do_output(const uint8_t input[], size_t length) {
8✔
49
   if(m_line_length == 0) {
8✔
50
      send(input, length);
6✔
51
   } else {
52
      size_t remaining = length, offset = 0;
2✔
53
      while(remaining) {
8✔
54
         size_t sent = std::min(m_line_length - m_out_position, remaining);
6✔
55
         send(input + offset, sent);
6✔
56
         m_out_position += sent;
6✔
57
         remaining -= sent;
6✔
58
         offset += sent;
6✔
59
         if(m_out_position == m_line_length) {
6✔
60
            send('\n');
5✔
61
            m_out_position = 0;
5✔
62
         }
63
      }
64
   }
65
}
8✔
66

67
/*
68
* Convert some data into Base64
69
*/
70
void Base64_Encoder::write(const uint8_t input[], size_t length) {
8✔
71
   const size_t initial_fill = std::min(m_in.size() - m_position, length);
8✔
72
   copy_mem(&m_in[m_position], input, initial_fill);
8✔
73

74
   if(m_position + length >= m_in.size()) {
8✔
75
      encode_and_send(m_in.data(), m_in.size());
×
76
      input += (m_in.size() - m_position);
×
77
      length -= (m_in.size() - m_position);
×
78
      while(length >= m_in.size()) {
×
79
         encode_and_send(input, m_in.size());
×
80
         input += m_in.size();
×
81
         length -= m_in.size();
×
82
      }
83
      copy_mem(m_in.data(), input, length);
×
84
      m_position = 0;
×
85
   }
86
   m_position += length;
8✔
87
}
8✔
88

89
/*
90
* Flush buffers
91
*/
92
void Base64_Encoder::end_msg() {
8✔
93
   encode_and_send(m_in.data(), m_position, true);
8✔
94

95
   if(m_trailing_newline || (m_out_position && m_line_length)) {
8✔
96
      send('\n');
2✔
97
   }
98

99
   m_out_position = m_position = 0;
8✔
100
}
8✔
101

102
/*
103
* Base64_Decoder Constructor
104
*/
105
Base64_Decoder::Base64_Decoder(Decoder_Checking c) : m_checking(c), m_in(64), m_out(48), m_position(0) {}
1✔
106

107
/*
108
* Convert some data from Base64
109
*/
110
void Base64_Decoder::write(const uint8_t input[], size_t length) {
1✔
111
   while(length) {
2✔
112
      size_t to_copy = std::min<size_t>(length, m_in.size() - m_position);
1✔
113
      if(to_copy == 0) {
1✔
114
         m_in.resize(m_in.size() * 2);
×
115
         m_out.resize(m_out.size() * 2);
×
116
      }
117
      copy_mem(&m_in[m_position], input, to_copy);
1✔
118
      m_position += to_copy;
1✔
119

120
      size_t consumed = 0;
1✔
121
      size_t written = base64_decode(
2✔
122
         m_out.data(), cast_uint8_ptr_to_char(m_in.data()), m_position, consumed, false, m_checking != FULL_CHECK);
1✔
123

124
      send(m_out, written);
1✔
125

126
      if(consumed != m_position) {
1✔
127
         copy_mem(m_in.data(), m_in.data() + consumed, m_position - consumed);
×
128
         m_position = m_position - consumed;
×
129
      } else {
130
         m_position = 0;
1✔
131
      }
132

133
      length -= to_copy;
1✔
134
      input += to_copy;
1✔
135
   }
136
}
1✔
137

138
/*
139
* Flush buffers
140
*/
141
void Base64_Decoder::end_msg() {
1✔
142
   size_t consumed = 0;
1✔
143
   size_t written = base64_decode(
2✔
144
      m_out.data(), cast_uint8_ptr_to_char(m_in.data()), m_position, consumed, true, m_checking != FULL_CHECK);
1✔
145

146
   send(m_out, written);
1✔
147

148
   const bool not_full_bytes = consumed != m_position;
1✔
149

150
   m_position = 0;
1✔
151

152
   if(not_full_bytes) {
1✔
153
      throw Invalid_Argument("Base64_Decoder: Input not full bytes");
×
154
   }
155
}
1✔
156

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