-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.cpp
More file actions
111 lines (89 loc) · 3.65 KB
/
base64.cpp
File metadata and controls
111 lines (89 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// base64.cpp
// ncc
//
// Created by yuki on 2022/05/27.
//
#include "base64.hpp"
bool algorithm::encode_base64(const std::vector<unsigned char>& src, std::string& dst) {
const std::string table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
std::string cdst;
for (std::size_t i = 0; i < src.size(); ++i) {
switch (i % 3) {
case 0:
cdst.push_back(table[(src[i] & 0xFC) >> 2]);
if (i + 1 == src.size()) {
cdst.push_back(table[(src[i] & 0x03) << 4]);
cdst.push_back('=');
cdst.push_back('=');
}
break;
case 1:
cdst.push_back(table[((src[i - 1] & 0x03) << 4) | ((src[i + 0] & 0xF0) >> 4)]);
if (i + 1 == src.size()) {
cdst.push_back(table[(src[i] & 0x0F) << 2]);
cdst.push_back('=');
}
break;
case 2:
cdst.push_back(table[((src[i - 1] & 0x0F) << 2) | ((src[i + 0] & 0xC0) >> 6)]);
cdst.push_back(table[src[i] & 0x3F]);
break;
}
}
dst.swap(cdst);
return true;
}
// base64 デコード
bool algorithm::decode_base64(const std::string& src, std::vector<unsigned char>& dst)
{
if (src.size() & 0x00000003) {
return false;
}
else {
const std::string table("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
std::vector<unsigned char> cdst;
for (std::size_t i = 0; i < src.size(); i += 4) {
if (src[i + 0] == '=') {
return false;
}
else if (src[i + 1] == '=') {
return false;
}
else if (src[i + 2] == '=') {
const std::string::size_type s1 = table.find(src[i + 0]);
const std::string::size_type s2 = table.find(src[i + 1]);
if (s1 == std::string::npos || s2 == std::string::npos) {
return false;
}
cdst.push_back(static_cast<unsigned char>(((s1 & 0x3F) << 2) | ((s2 & 0x30) >> 4)));
break;
}
else if (src[i + 3] == '=') {
const std::string::size_type s1 = table.find(src[i + 0]);
const std::string::size_type s2 = table.find(src[i + 1]);
const std::string::size_type s3 = table.find(src[i + 2]);
if (s1 == std::string::npos || s2 == std::string::npos || s3 == std::string::npos) {
return false;
}
cdst.push_back(static_cast<unsigned char>(((s1 & 0x3F) << 2) | ((s2 & 0x30) >> 4)));
cdst.push_back(static_cast<unsigned char>(((s2 & 0x0F) << 4) | ((s3 & 0x3C) >> 2)));
break;
}
else {
const std::string::size_type s1 = table.find(src[i + 0]);
const std::string::size_type s2 = table.find(src[i + 1]);
const std::string::size_type s3 = table.find(src[i + 2]);
const std::string::size_type s4 = table.find(src[i + 3]);
if (s1 == std::string::npos || s2 == std::string::npos || s3 == std::string::npos || s4 == std::string::npos) {
return false;
}
cdst.push_back(static_cast<unsigned char>(((s1 & 0x3F) << 2) | ((s2 & 0x30) >> 4)));
cdst.push_back(static_cast<unsigned char>(((s2 & 0x0F) << 4) | ((s3 & 0x3C) >> 2)));
cdst.push_back(static_cast<unsigned char>(((s3 & 0x03) << 6) | ((s4 & 0x3F) >> 0)));
}
}
dst.swap(cdst);
return true;
}
}