annotate Resources/ThirdParty/base64/base64.cpp @ 2448:862d943115f9

working on linux standard base
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jan 2018 18:04:11 +0100
parents f0544ab9396b
children 2e7c5c15ba25
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2102
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
1 /*
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
2 base64.cpp and base64.h
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
3
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
4 Copyright (C) 2004-2008 René Nyffenegger
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
5
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
6 This source code is provided 'as-is', without any express or implied
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
7 warranty. In no event will the author be held liable for any damages
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
8 arising from the use of this software.
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
9
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
10 Permission is granted to anyone to use this software for any purpose,
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
11 including commercial applications, and to alter it and redistribute it
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
12 freely, subject to the following restrictions:
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
13
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
14 1. The origin of this source code must not be misrepresented; you must not
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
15 claim that you wrote the original source code. If you use this source code
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
16 in a product, an acknowledgment in the product documentation would be
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
17 appreciated but is not required.
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
18
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
19 2. Altered source versions must be plainly marked as such, and must not be
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
20 misrepresented as being the original source code.
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
21
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
22 3. This notice may not be removed or altered from any source distribution.
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
23
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
24 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
25
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
26 */
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
27
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
28 #include "base64.h"
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
29 #include <string.h>
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
30
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
31 static const std::string base64_chars =
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
33 "abcdefghijklmnopqrstuvwxyz"
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
34 "0123456789+/";
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
35
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
36
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
37 static inline bool is_base64(unsigned char c) {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
38 return (isalnum(c) || (c == '+') || (c == '/'));
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
39 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
40
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
41 std::string base64_encode(const std::string& stringToEncode)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
42 {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
43 const unsigned char* bytes_to_encode = reinterpret_cast<const unsigned char*>
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
44 (stringToEncode.size() > 0 ? &stringToEncode[0] : NULL);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
45 unsigned int in_len = stringToEncode.size();
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
46
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
47 std::string ret;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
48 int i = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
49 int j = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
50 unsigned char char_array_3[3];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
51 unsigned char char_array_4[4];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
52
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
53 while (in_len--) {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
54 char_array_3[i++] = *(bytes_to_encode++);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
55 if (i == 3) {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
56 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
57 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
58 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
59 char_array_4[3] = char_array_3[2] & 0x3f;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
60
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
61 for(i = 0; (i <4) ; i++)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
62 ret += base64_chars[char_array_4[i]];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
63 i = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
64 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
65 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
66
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
67 if (i)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
68 {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
69 for(j = i; j < 3; j++)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
70 char_array_3[j] = '\0';
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
71
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
72 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
73 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
74 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
75 char_array_4[3] = char_array_3[2] & 0x3f;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
76
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
77 for (j = 0; (j < i + 1); j++)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
78 ret += base64_chars[char_array_4[j]];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
79
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
80 while((i++ < 3))
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
81 ret += '=';
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
82
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
83 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
84
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
85 return ret;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
86 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
87
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
88
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
89 std::string base64_decode(const std::string& encoded_string) {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
90 int in_len = encoded_string.size();
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
91 int i = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
92 int j = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
93 int in_ = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
94 unsigned char char_array_4[4], char_array_3[3];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
95 std::string ret;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
96
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
97 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
98 char_array_4[i++] = encoded_string[in_]; in_++;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
99 if (i ==4) {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
100 for (i = 0; i <4; i++)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
101 char_array_4[i] = base64_chars.find(char_array_4[i]);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
102
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
103 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
104 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
105 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
106
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
107 for (i = 0; (i < 3); i++)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
108 ret += char_array_3[i];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
109 i = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
110 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
111 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
112
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
113 if (i) {
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
114 for (j = i; j <4; j++)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
115 char_array_4[j] = 0;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
116
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
117 for (j = 0; j <4; j++)
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
118 char_array_4[j] = base64_chars.find(char_array_4[j]);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
119
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
120 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
121 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
122 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
123
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
124 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
125 }
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
126
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
127 return ret;
f0544ab9396b fix line endings for msvc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 950
diff changeset
128 }