OSDN Git Service

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