Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Re-enable unit tests
[xestiaab/.git] / source / tools / odthelpbrowser / base64.cpp
1 /* 
2    base64.cpp and base64.h
4    Copyright (C) 2004-2008 RenĂ© Nyffenegger
6    This source code is provided 'as-is', without any express or implied
7    warranty. In no event will the author be held liable for any damages
8    arising from the use of this software.
10    Permission is granted to anyone to use this software for any purpose,
11    including commercial applications, and to alter it and redistribute it
12    freely, subject to the following restrictions:
14    1. The origin of this source code must not be misrepresented; you must not
15       claim that you wrote the original source code. If you use this source code
16       in a product, an acknowledgment in the product documentation would be
17       appreciated but is not required.
19    2. Altered source versions must be plainly marked as such, and must not be
20       misrepresented as being the original source code.
22    3. This notice may not be removed or altered from any source distribution.
24    RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch
26 */
28 #include "base64.h"
29 #include <iostream>
31 static const std::string base64_chars = 
32              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33              "abcdefghijklmnopqrstuvwxyz"
34              "0123456789+/";
37 static inline bool is_base64(unsigned char c) {
38   return (isalnum(c) || (c == '+') || (c == '/'));
39 }
41 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
42   std::string ret;
43   int i = 0;
44   int j = 0;
45   unsigned char char_array_3[3];
46   unsigned char char_array_4[4];
48   while (in_len--) {
49     char_array_3[i++] = *(bytes_to_encode++);
50     if (i == 3) {
51       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
52       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
53       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
54       char_array_4[3] = char_array_3[2] & 0x3f;
56       for(i = 0; (i <4) ; i++)
57         ret += base64_chars[char_array_4[i]];
58       i = 0;
59     }
60   }
62   if (i)
63   {
64     for(j = i; j < 3; j++)
65       char_array_3[j] = '\0';
67     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
68     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
69     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
70     char_array_4[3] = char_array_3[2] & 0x3f;
72     for (j = 0; (j < i + 1); j++)
73       ret += base64_chars[char_array_4[j]];
75     while((i++ < 3))
76       ret += '=';
78   }
80   return ret;
82 }
84 std::string base64_decode(std::string const& encoded_string) {
85 /*
86   Modified change: swapped 'int in_len' to 'size_t in_len'
87  */
88   size_t in_len = encoded_string.size();
89   int i = 0;
90   int j = 0;
91   int in_ = 0;
92   unsigned char char_array_4[4], char_array_3[3];
93   std::string ret;
95   while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
96     char_array_4[i++] = encoded_string[in_]; in_++;
97     if (i ==4) {
98       for (i = 0; i <4; i++)
99         char_array_4[i] = base64_chars.find(char_array_4[i]);
101       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
102       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
103       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
105       for (i = 0; (i < 3); i++)
106         ret += char_array_3[i];
107       i = 0;
108     }
109   }
111   if (i) {
112     for (j = i; j <4; j++)
113       char_array_4[j] = 0;
115     for (j = 0; j <4; j++)
116       char_array_4[j] = base64_chars.find(char_array_4[j]);
118     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
119     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
120     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
122     for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
123   }
125   return ret;
127  
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy