• 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

82.5
/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
   buffer_insert(m_in, m_position, input, length);
8✔
72
   if(m_position + length >= m_in.size()) {
8✔
73
      encode_and_send(m_in.data(), m_in.size());
×
74
      input += (m_in.size() - m_position);
×
75
      length -= (m_in.size() - m_position);
×
76
      while(length >= m_in.size()) {
×
77
         encode_and_send(input, m_in.size());
×
78
         input += m_in.size();
×
79
         length -= m_in.size();
×
80
      }
81
      copy_mem(m_in.data(), input, length);
×
82
      m_position = 0;
×
83
   }
84
   m_position += length;
8✔
85
}
8✔
86

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

93
   if(m_trailing_newline || (m_out_position && m_line_length))
8✔
94
      send('\n');
2✔
95

96
   m_out_position = m_position = 0;
8✔
97
}
8✔
98

99
/*
100
* Base64_Decoder Constructor
101
*/
102
Base64_Decoder::Base64_Decoder(Decoder_Checking c) : m_checking(c), m_in(64), m_out(48), m_position(0) {}
1✔
103

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

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

121
      send(m_out, written);
1✔
122

123
      if(consumed != m_position) {
1✔
124
         copy_mem(m_in.data(), m_in.data() + consumed, m_position - consumed);
×
125
         m_position = m_position - consumed;
×
126
      } else
127
         m_position = 0;
1✔
128

129
      length -= to_copy;
1✔
130
      input += to_copy;
1✔
131
   }
132
}
1✔
133

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

142
   send(m_out, written);
1✔
143

144
   const bool not_full_bytes = consumed != m_position;
1✔
145

146
   m_position = 0;
1✔
147

148
   if(not_full_bytes)
1✔
149
      throw Invalid_Argument("Base64_Decoder: Input not full bytes");
×
150
}
1✔
151

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