Lolly 1.4.27
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : base64.hpp
4 * DESCRIPTION: Implementation of the base64 coding as described by RFC-3548.
5 * COPYRIGHT : (C) 2013 Francois Poulain
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "base64.hpp"
13#include "array.hpp"
14#include "string.hpp"
15
16namespace lolly {
17namespace data {
18
19// 0 maps to 'A', 1 maps to 'B', and so on ...
20static const char int_to_b64[]=
21 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
22
23inline static string
24encode_base64 (char c1, char c2, char c3) {
25 string r (4);
26 int idx0, idx1, idx2, idx3;
27
28 idx0= ((c1 >> 2) & 0x3F);
29 idx1= (((c1 << 4) & 0x30) + ((c2 >> 4) & 0x0F));
30 idx2= (((c2 << 2) & 0x3C) + ((c3 >> 6) & 0x03));
31 idx3= (c3 & 0x3F);
32
33 r[0]= int_to_b64[idx0];
34 r[1]= int_to_b64[idx1];
35 r[2]= int_to_b64[idx2];
36 r[3]= int_to_b64[idx3];
37 return r;
38}
39
40string
41encode_base64 (string s) {
42 string r;
43 int i, n= N (s);
44 for (i= 0; i + 2 < n; i+= 3) {
45 if (i > 0 && i % 60 == 0) r << "\n";
46 r << encode_base64 (s[i], s[i + 1], s[i + 2]);
47 }
48
49 if (i == n - 1) {
50 if (i > 0 && i % 60 == 0) r << "\n";
51 r << encode_base64 (s[i], '\0', '\0') (0, 2) << "==";
52 }
53 else if (i == n - 2) {
54 if (i > 0 && i % 60 == 0) r << "\n";
55 r << encode_base64 (s[i], s[i + 1], '\0') (0, 3) << "=";
56 }
57 return r;
58}
59
60// 'A' maps to (64+0), 'B' maps to (64+1), and so on, until '/' maps to (64+63)
61// Others maps to 43 ('?')
62static const char b64_to_int[]=
63 "???????????????????????????????????????????~???\177tuvwxyz{|}??\
64?????@ABCDEFGHIJKLMNOPQRSTUVWXY??????Z[\\]^_`abcdefghijklmnopqrs?????";
65
66string
68 string r (3);
69 int n= N (ac), n1, n2, n3, n4;
70
71 n1= b64_to_int[(int) ac[0]] - 64;
72 n2= b64_to_int[(int) ac[1]] - 64;
73 n3= (n > 2) ? b64_to_int[(int) ac[2]] - 64 : 0;
74 n4= (n > 3) ? b64_to_int[(int) ac[3]] - 64 : 0;
75
76 r[0]= ((n1 << 2) & 0xFC) + ((n2 >> 4) & 0x03);
77 r[1]= ((n2 << 4) & 0xF0) + ((n3 >> 2) & 0x0F);
78 r[2]= ((n3 << 6) & 0xC0) + (n4 & 0x3F);
79
80 return r (0, n - 1);
81}
82
83string
84decode_base64 (string s) {
85 string r;
86 bool end= false;
88 int i, n= N (s), cnt= 0;
89 for (i= 0; i < n && !end; i++) {
90 if (b64_to_int[(int) s[i]] != '?') {
91 tmp << (int) s[i];
92 cnt++;
93 }
94 else if (s[i] == '=') {
95 end= true;
96 }
97
98 if (cnt == 4 || end) {
99 r << decode_base64 (tmp);
100 cnt= 0;
101 tmp= array<int> ();
102 }
103 }
104 return r;
105}
106
107} // namespace data
108} // namespace lolly
The list class represents a linked list.
Definition list.hpp:48
string decode_base64(array< int > ac)
Definition base64.cpp:67
static const char int_to_b64[]
Definition base64.cpp:20
static string encode_base64(char c1, char c2, char c3)
Definition base64.cpp:24
int N(lolly_tree< T > t)
static const char b64_to_int[]
Definition base64.cpp:62