annotate Resources/base64/base64.cpp @ 596:4e7cd90e982c Orthanc-0.6.2

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