summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/Base64.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/Base64.cpp')
-rw-r--r--xbmc/utils/Base64.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/xbmc/utils/Base64.cpp b/xbmc/utils/Base64.cpp
new file mode 100644
index 0000000..6b41519
--- /dev/null
+++ b/xbmc/utils/Base64.cpp
@@ -0,0 +1,128 @@
1/*
2 * Copyright (C) 2011-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#include "Base64.h"
10
11#define PADDING '='
12
13const std::string Base64::m_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
14 "abcdefghijklmnopqrstuvwxyz"
15 "0123456789+/";
16
17void Base64::Encode(const char* input, unsigned int length, std::string &output)
18{
19 if (input == NULL || length == 0)
20 return;
21
22 long l;
23 output.clear();
24 output.reserve(((length + 2) / 3) * 4);
25
26 for (unsigned int i = 0; i < length; i += 3)
27 {
28 l = ((((unsigned long) input[i]) << 16) & 0xFFFFFF) |
29 ((((i + 1) < length) ? (((unsigned long) input[i + 1]) << 8) : 0) & 0xFFFF) |
30 ((((i + 2) < length) ? (((unsigned long) input[i + 2]) << 0) : 0) & 0x00FF);
31
32 output.push_back(m_characters[(l >> 18) & 0x3F]);
33 output.push_back(m_characters[(l >> 12) & 0x3F]);
34
35 if (i + 1 < length)
36 output.push_back(m_characters[(l >> 6) & 0x3F]);
37 if (i + 2 < length)
38 output.push_back(m_characters[(l >> 0) & 0x3F]);
39 }
40
41 int left = 3 - (length % 3);
42
43 if (length % 3)
44 {
45 for (int i = 0; i < left; i++)
46 output.push_back(PADDING);
47 }
48}
49
50std::string Base64::Encode(const char* input, unsigned int length)
51{
52 std::string output;
53 Encode(input, length, output);
54
55 return output;
56}
57
58void Base64::Encode(const std::string &input, std::string &output)
59{
60 Encode(input.c_str(), input.size(), output);
61}
62
63std::string Base64::Encode(const std::string &input)
64{
65 std::string output;
66 Encode(input, output);
67
68 return output;
69}
70
71void Base64::Decode(const char* input, unsigned int length, std::string &output)
72{
73 if (input == NULL || length == 0)
74 return;
75
76 long l;
77 output.clear();
78
79 for (unsigned int index = 0; index < length; index++)
80 {
81 if (input[index] == '=')
82 {
83 length = index;
84 break;
85 }
86 }
87
88 output.reserve(length - ((length + 2) / 4));
89
90 for (unsigned int i = 0; i < length; i += 4)
91 {
92 l = ((((unsigned long) m_characters.find(input[i])) & 0x3F) << 18);
93 l |= (((i + 1) < length) ? ((((unsigned long) m_characters.find(input[i + 1])) & 0x3F) << 12) : 0);
94 l |= (((i + 2) < length) ? ((((unsigned long) m_characters.find(input[i + 2])) & 0x3F) << 6) : 0);
95 l |= (((i + 3) < length) ? ((((unsigned long) m_characters.find(input[i + 3])) & 0x3F) << 0) : 0);
96
97 output.push_back((char)((l >> 16) & 0xFF));
98 if (i + 2 < length)
99 output.push_back((char)((l >> 8) & 0xFF));
100 if (i + 3 < length)
101 output.push_back((char)((l >> 0) & 0xFF));
102 }
103}
104
105std::string Base64::Decode(const char* input, unsigned int length)
106{
107 std::string output;
108 Decode(input, length, output);
109
110 return output;
111}
112
113void Base64::Decode(const std::string &input, std::string &output)
114{
115 size_t length = input.find_first_of(PADDING);
116 if (length == std::string::npos)
117 length = input.size();
118
119 Decode(input.c_str(), length, output);
120}
121
122std::string Base64::Decode(const std::string &input)
123{
124 std::string output;
125 Decode(input, output);
126
127 return output;
128}