KiCad PCB EDA Suite
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * @author Seth Hillbrand <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22
23#include <core/base64.h>
24
25#include <cstdint>
26#include <cstddef>
27#include <limits>
28#include <vector>
29
30namespace {
31
32static const uint8_t ENCODE_BASE64[64] = {
33 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
34 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
35 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
36 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
37 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
38 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
39 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
40 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F,
41};
42
43static const uint8_t E = -1;
44
45static const uint8_t DECODE_BASE64[128] = {
46/* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF */
47 E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
48 E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
49 E, E, E, E, E, E, E, E, E, E, E, 62, E, E, E, 63,
50 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, E, E, E, E, E, E,
51 E, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
52 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, E, E, E, E, E,
53 E, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
54 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, E, E, E, E, E,
55};
56
57} // namespace
58
59namespace base64
60{
61 size_t encode_length( size_t aInputLen )
62 {
63 return 4 * ( ( aInputLen + 2 ) / 3 ) + ( aInputLen + 2 ) % 3 - 2;
64 }
65
66
67 size_t decode_length( size_t aInputLen )
68 {
69 if( aInputLen % 4 == 1 )
70 return 0;
71 else
72 return 3 * ( ( aInputLen + 2 ) / 4 ) + ( aInputLen + 2 ) % 4 - 2;
73 }
74
75
76 void encode( const std::vector<uint8_t>& aInput, std::vector<uint8_t>& aOutput )
77 {
78 size_t end = ( aInput.size() / 3 ) * 3;
79 aOutput.reserve( encode_length( aInput.size() ) );
80
81 for( size_t i = 0; i < end; i += 3 )
82 {
83 unsigned value = ( aInput[i] << 16 ) | ( aInput[i + 1] << 8 ) | aInput[i + 2] ;
84
85 aOutput.emplace_back( ENCODE_BASE64[( value >> 18 ) & 0x3F] );
86 aOutput.emplace_back( ENCODE_BASE64[( value >> 12 ) & 0x3F] );
87 aOutput.emplace_back( ENCODE_BASE64[( value >> 6 ) & 0x3F] );
88 aOutput.emplace_back( ENCODE_BASE64[value & 0x3F] );
89 }
90
91 size_t remainder = aInput.size() - end;
92
93 if( remainder )
94 {
95 unsigned value = aInput[end];
96
97 if( remainder == 2 )
98 {
99 value = ( value << 10 ) | ( aInput[end + 1] << 2 );
100 aOutput.emplace_back( ENCODE_BASE64[( value >> 12 ) & 0x3F] );
101 }
102 else
103 {
104 value <<= 4;
105 }
106
107 aOutput.emplace_back( ENCODE_BASE64[( value >> 6 ) & 0x3F] );
108 aOutput.emplace_back( ENCODE_BASE64[value & 0x3F] );
109 }
110 }
111
112
113 void decode(const std::vector<uint8_t>& aInput, std::vector<uint8_t>& aOutput )
114 {
115 size_t end = ( aInput.size() / 4 ) * 4;
116 size_t decode_size = decode_length( aInput.size() );
117
118 if( !decode_size )
119 return;
120
121 aOutput.reserve( decode_size );
122
123 for( size_t i = 0; i < end; i++ )
124 {
125 unsigned value = ( DECODE_BASE64[aInput[i] & 0x7F] << 18 ) |
126 ( DECODE_BASE64[aInput[i + 1] & 0x7F] << 12 ) |
127 ( DECODE_BASE64[aInput[i + 2] & 0x7F] << 6 ) |
128 DECODE_BASE64[aInput[i + 3] & 0x7F];
129
130 aOutput.emplace_back( ( value >> 16 ) & 0xff );
131 aOutput.emplace_back( ( value >> 8 ) & 0xff );
132 aOutput.emplace_back( value & 0xff );
133 }
134
135 size_t remainder = aInput.size() - end;
136
137 if( remainder )
138 {
139 unsigned value = ( ( DECODE_BASE64[aInput[end] & 0x7F] ) << 6 ) |
140 ( DECODE_BASE64[aInput[end + 1] & 0x7F] );
141
142 if( remainder == 3 )
143 {
144 value = ( value << 6 ) | ( DECODE_BASE64[aInput[end + 2] & 0x7F] );
145 value >>= 2;
146
147 aOutput.emplace_back( ( value >> 8 ) & 0xff );
148 }
149 else
150 {
151 value >>= 4;
152 }
153 aOutput[0] = value & 0xff ;
154 }
155 }
156
157} // base64
void encode(const std::vector< uint8_t > &aInput, std::vector< uint8_t > &aOutput)
Definition: base64.cpp:76
size_t decode_length(size_t aInputLen)
Definition: base64.cpp:67
size_t encode_length(size_t aInputLen)
Definition: base64.cpp:61
void decode(const std::vector< uint8_t > &aInput, std::vector< uint8_t > &aOutput)
Definition: base64.cpp:113