Coverage Report

Created: 2026-07-25 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/Foundation/src/Base64Decoder.cpp
Line
Count
Source
1
//
2
// Base64Decoder.cpp
3
//
4
// Library: Foundation
5
// Package: Streams
6
// Module:  Base64
7
//
8
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// and Contributors.
10
//
11
// SPDX-License-Identifier: BSL-1.0
12
//
13
14
15
#include "Poco/Base64Decoder.h"
16
#include "Poco/Base64Encoder.h"
17
#include "Poco/Exception.h"
18
#include "Poco/Mutex.h"
19
20
21
namespace Poco {
22
23
24
unsigned char Base64DecoderBuf::IN_ENCODING[256];
25
bool Base64DecoderBuf::IN_ENCODING_INIT = false;
26
unsigned char Base64DecoderBuf::IN_ENCODING_URL[256];
27
bool Base64DecoderBuf::IN_ENCODING_URL_INIT = false;
28
29
30
namespace
31
{
32
  static FastMutex mutex;
33
}
34
35
36
Base64DecoderBuf::Base64DecoderBuf(std::istream& istr, int options):
37
126k
  _options(options),
38
126k
  _groupLength(0),
39
126k
  _groupIndex(0),
40
126k
  _buf(*istr.rdbuf()),
41
126k
  _pInEncoding((options & BASE64_URL_ENCODING) ? IN_ENCODING_URL : IN_ENCODING)
42
126k
{
43
126k
  FastMutex::ScopedLock lock(mutex);
44
126k
  if (options & BASE64_URL_ENCODING)
45
92.7k
  {
46
92.7k
    if (!IN_ENCODING_URL_INIT)
47
1
    {
48
257
      for (unsigned i = 0; i < sizeof(IN_ENCODING_URL); i++)
49
256
      {
50
256
        IN_ENCODING_URL[i] = 0xFF;
51
256
      }
52
65
      for (unsigned i = 0; i < sizeof(Base64EncoderBuf::OUT_ENCODING_URL); i++)
53
64
      {
54
64
        IN_ENCODING_URL[Base64EncoderBuf::OUT_ENCODING_URL[i]] = static_cast<UInt8>(i);
55
64
      }
56
1
      IN_ENCODING_URL[static_cast<unsigned char>('=')] = '\0';
57
1
      IN_ENCODING_URL_INIT = true;
58
1
    }
59
92.7k
  }
60
33.5k
  else
61
33.5k
  {
62
33.5k
    if (!IN_ENCODING_INIT)
63
2
    {
64
514
      for (unsigned i = 0; i < sizeof(IN_ENCODING); i++)
65
512
      {
66
512
        IN_ENCODING[i] = 0xFF;
67
512
      }
68
130
      for (unsigned i = 0; i < sizeof(Base64EncoderBuf::OUT_ENCODING); i++)
69
128
      {
70
128
        IN_ENCODING[Base64EncoderBuf::OUT_ENCODING[i]] = static_cast<UInt8>(i);
71
128
      }
72
2
      IN_ENCODING[static_cast<unsigned char>('=')] = '\0';
73
2
      IN_ENCODING_INIT = true;
74
2
    }
75
33.5k
  }
76
126k
}
77
78
79
Base64DecoderBuf::~Base64DecoderBuf()
80
126k
{
81
126k
}
82
83
84
int Base64DecoderBuf::readFromDevice()
85
617M
{
86
617M
  if (_groupIndex < _groupLength)
87
411M
  {
88
411M
    return _group[_groupIndex++];
89
411M
  }
90
206M
  else
91
206M
  {
92
206M
    unsigned char buffer[4];
93
206M
    int c;
94
206M
    if ((c = readOne()) == -1) return -1;
95
206M
    buffer[0] = static_cast<UInt8>(c);
96
206M
    if (_pInEncoding[buffer[0]] == 0xFF) throw DataFormatException();
97
206M
    if ((c = readOne()) == -1) return -1;
98
206M
    buffer[1] = static_cast<UInt8>(c);
99
206M
    if (_pInEncoding[buffer[1]] == 0xFF) throw DataFormatException();
100
206M
    if (_options & BASE64_NO_PADDING)
101
205M
    {
102
205M
      if ((c = readOne()) != -1)
103
205M
        buffer[2] = static_cast<UInt8>(c);
104
18.4k
      else
105
18.4k
        buffer[2] = '=';
106
205M
      if (_pInEncoding[buffer[2]] == 0xFF) throw DataFormatException();
107
205M
      if ((c = readOne()) != -1)
108
205M
        buffer[3] = static_cast<UInt8>(c);
109
66.8k
      else
110
66.8k
        buffer[3] = '=';
111
205M
      if (_pInEncoding[buffer[3]] == 0xFF) throw DataFormatException();
112
205M
    }
113
78.8k
    else
114
78.8k
    {
115
78.8k
      if ((c = readOne()) == -1) throw DataFormatException();
116
77.9k
      buffer[2] = static_cast<UInt8>(c);
117
77.9k
      if (_pInEncoding[buffer[2]] == 0xFF) throw DataFormatException();
118
77.4k
      if ((c = readOne()) == -1) throw DataFormatException();
119
76.3k
      buffer[3] = static_cast<UInt8>(c);
120
76.3k
      if (_pInEncoding[buffer[3]] == 0xFF) throw DataFormatException();
121
76.3k
    }
122
123
206M
    _group[0] = (_pInEncoding[buffer[0]] << 2) | (_pInEncoding[buffer[1]] >> 4);
124
206M
    _group[1] = ((_pInEncoding[buffer[1]] & 0x0F) << 4) | (_pInEncoding[buffer[2]] >> 2);
125
206M
    _group[2] = (_pInEncoding[buffer[2]] << 6) | _pInEncoding[buffer[3]];
126
127
206M
    if (buffer[2] == '=')
128
390k
      _groupLength = 1;
129
205M
    else if (buffer[3] == '=')
130
150k
      _groupLength = 2;
131
205M
    else
132
205M
      _groupLength = 3;
133
206M
    _groupIndex = 1;
134
206M
    return _group[0];
135
206M
  }
136
617M
}
137
138
139
int Base64DecoderBuf::readOne()
140
824M
{
141
824M
  int ch = _buf.sbumpc();
142
824M
  if (!(_options & BASE64_URL_ENCODING))
143
350k
  {
144
360k
    while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
145
10.4k
      ch = _buf.sbumpc();
146
350k
  }
147
824M
  return ch;
148
824M
}
149
150
151
126k
Base64DecoderIOS::Base64DecoderIOS(std::istream& istr, int options): _buf(istr, options)
152
126k
{
153
126k
  poco_ios_init(&_buf);
154
126k
}
Poco::Base64DecoderIOS::Base64DecoderIOS(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int)
Line
Count
Source
151
126k
Base64DecoderIOS::Base64DecoderIOS(std::istream& istr, int options): _buf(istr, options)
152
126k
{
153
126k
  poco_ios_init(&_buf);
154
126k
}
Unexecuted instantiation: Poco::Base64DecoderIOS::Base64DecoderIOS(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int)
155
156
157
Base64DecoderIOS::~Base64DecoderIOS()
158
126k
{
159
126k
}
160
161
162
Base64DecoderBuf* Base64DecoderIOS::rdbuf()
163
0
{
164
0
  return &_buf;
165
0
}
166
167
168
126k
Base64Decoder::Base64Decoder(std::istream& istr, int options): Base64DecoderIOS(istr, options), std::istream(&_buf)
169
126k
{
170
126k
}
Unexecuted instantiation: Poco::Base64Decoder::Base64Decoder(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int)
Poco::Base64Decoder::Base64Decoder(std::__1::basic_istream<char, std::__1::char_traits<char> >&, int)
Line
Count
Source
168
126k
Base64Decoder::Base64Decoder(std::istream& istr, int options): Base64DecoderIOS(istr, options), std::istream(&_buf)
169
126k
{
170
126k
}
171
172
173
Base64Decoder::~Base64Decoder()
174
126k
{
175
126k
}
176
177
178
} // namespace Poco