Audacity 3.2.0
Base64.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file Base64.cpp
6
7 @brief Base64 encode/decode (extracted from Audacity sources)
8
9 **********************************************************************/
10
11#include "Base64.h"
12#include <wx/string.h>
13
15// Base64 en/decoding
16//
17// Original routines marked as public domain and found at:
18//
19// http://en.wikibooks.org/wiki/Algorithm_implementation/Miscellaneous/Base64
20//
22
23// Lookup table for encoding
24const static wxChar cset[] = wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
25const static char padc = wxT('=');
26
27wxString Base64::Encode(const void *in, int len)
28{
29 auto p = static_cast<const unsigned char*>(in);
30 wxString out;
31
32 unsigned long temp;
33 for (int i = 0; i < len / 3; i++)
34 {
35 temp = (*p++) << 16; //Convert to big endian
36 temp += (*p++) << 8;
37 temp += (*p++);
38 out += cset[(temp & 0x00FC0000) >> 18];
39 out += cset[(temp & 0x0003F000) >> 12];
40 out += cset[(temp & 0x00000FC0) >> 6];
41 out += cset[(temp & 0x0000003F)];
42 }
43
44 switch (len % 3)
45 {
46 case 1:
47 temp = (*p++) << 16; //Convert to big endian
48 out += cset[(temp & 0x00FC0000) >> 18];
49 out += cset[(temp & 0x0003F000) >> 12];
50 out += padc;
51 out += padc;
52 break;
53
54 case 2:
55 temp = (*p++) << 16; //Convert to big endian
56 temp += (*p++) << 8;
57 out += cset[(temp & 0x00FC0000) >> 18];
58 out += cset[(temp & 0x0003F000) >> 12];
59 out += cset[(temp & 0x00000FC0) >> 6];
60 out += padc;
61 break;
62 }
63
64 return out;
65}
66
67int Base64::Decode(const wxString &in, void *out)
68{
69 const auto len = in.length();
70 auto p = static_cast<unsigned char*>(out);
71
72 if (len % 4) //Sanity check
73 {
74 return 0;
75 }
76
77 //const char *a = in.mb_str();
78 //Setup a vector to hold the result
79 unsigned long temp = 0; //Holds decoded quanta
80 size_t i = 0;
81 while (i < len)
82 {
83 for (int quantumPosition = 0; quantumPosition < 4; quantumPosition++)
84 {
85 unsigned char c = in[i];
86 temp <<= 6;
87
88 if (c >= 0x41 && c <= 0x5A)
89 {
90 temp |= c - 0x41;
91 }
92 else if (c >= 0x61 && c <= 0x7A)
93 {
94 temp |= c - 0x47;
95 }
96 else if (c >= 0x30 && c <= 0x39)
97 {
98 temp |= c + 0x04;
99 }
100 else if (c == 0x2B)
101 {
102 temp |= 0x3E;
103 }
104 else if (c == 0x2F)
105 {
106 temp |= 0x3F;
107 }
108 else if (c == padc)
109 {
110 switch (len - i)
111 {
112 case 1: //One pad character
113 *p++ = (temp >> 16) & 0x000000FF;
114 *p++ = (temp >> 8) & 0x000000FF;
115 return p - static_cast<unsigned char*>(out);
116 case 2: //Two pad characters
117 *p++ = (temp >> 10) & 0x000000FF;
118 return p - static_cast<unsigned char*>(out);
119 }
120 }
121 i++;
122 }
123 *p++ = (temp >> 16) & 0x000000FF;
124 *p++ = (temp >> 8) & 0x000000FF;
125 *p++ = temp & 0x000000FF;
126 }
127
128 return p - static_cast<unsigned char*>(out);
129}
wxT("CloseDown"))
static const wxChar cset[]
Definition: Base64.cpp:24
static const char padc
Definition: Base64.cpp:25
STRINGS_API wxString Encode(const void *in, int len)
Definition: Base64.cpp:27
STRINGS_API int Decode(const wxString &in, void *out)
Definition: Base64.cpp:67