KiCad PCB EDA Suite
base64 Namespace Reference

Functions

size_t encode_length (size_t aInputLen)
 
size_t decode_length (size_t aInputLen)
 
void encode (const std::vector< uint8_t > &aInput, std::vector< uint8_t > &aOutput)
 
void decode (const std::vector< uint8_t > &aInput, std::vector< uint8_t > &aOutput)
 

Function Documentation

◆ decode()

void base64::decode ( const std::vector< uint8_t > &  aInput,
std::vector< uint8_t > &  aOutput 
)

Definition at line 113 of file base64.cpp.

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  }
size_t decode_length(size_t aInputLen)
Definition: base64.cpp:67

References decode_length().

◆ decode_length()

size_t base64::decode_length ( size_t  aInputLen)

Definition at line 67 of file base64.cpp.

68  {
69  if( aInputLen % 4 == 1 )
70  return 0;
71  else
72  return 3 * ( ( aInputLen + 2 ) / 4 ) + ( aInputLen + 2 ) % 4 - 2;
73  }

Referenced by decode().

◆ encode()

void base64::encode ( const std::vector< uint8_t > &  aInput,
std::vector< uint8_t > &  aOutput 
)

Definition at line 76 of file base64.cpp.

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  }
size_t encode_length(size_t aInputLen)
Definition: base64.cpp:61

References encode_length().

Referenced by SVG_PLOTTER::PlotImage().

◆ encode_length()

size_t base64::encode_length ( size_t  aInputLen)

Definition at line 61 of file base64.cpp.

62  {
63  return 4 * ( ( aInputLen + 2 ) / 3 ) + ( aInputLen + 2 ) % 3 - 2;
64  }

Referenced by encode().