LifeV
EncoderBase64.cpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3  *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23  *******************************************************************************
24  */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief An algorithm to encode / decode a string into base64 format
30 
31  The code was written by RenŽ Nyffenegger (original Copyright notice follows)
32 
33  @date 3-9-2011
34 
35  @maintainer Tiziano Passerini <tiziano@mathcs.emory.edu>
36  */
37 
38 /*
39  base64.cpp and base64.h
40 
41  Copyright (C) 2004-2008 RenŽ Nyffenegger
42 
43  This source code is provided 'as-is', without any express or implied
44  warranty. In no event will the author be held liable for any damages
45  arising from the use of this software.
46 
47  Permission is granted to anyone to use this software for any purpose,
48  including commercial applications, and to alter it and redistribute it
49  freely, subject to the following restrictions:
50 
51  1. The origin of this source code must not be misrepresented; you must not
52  claim that you wrote the original source code. If you use this source code
53  in a product, an acknowledgment in the product documentation would be
54  appreciated but is not required.
55 
56  2. Altered source versions must be plainly marked as such, and must not be
57  misrepresented as being the original source code.
58 
59  3. This notice may not be removed or altered from any source distribution.
60 
61  RenŽ Nyffenegger rene.nyffenegger@adp-gmbh.ch
62 
63  */
64 
65 #include <lifev/core/util/EncoderBase64.hpp>
66 #include <iostream>
67 
68 namespace LifeV
69 {
70 
71 static const std::string base64_chars =
72  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
73  "abcdefghijklmnopqrstuvwxyz"
74  "0123456789+/";
75 
76 
77 static inline bool is_base64 (unsigned char c)
78 {
79  return (isalnum (c) || (c == '+') || (c == '/') );
80 }
81 
82 std::string base64_encode (unsigned char const* bytes_to_encode, UInt in_len)
83 {
84  std::string ret;
85  int i = 0;
86  int j = 0;
87  unsigned char char_array_3[3];
88  unsigned char char_array_4[4];
89 
90  while (in_len--)
91  {
92  char_array_3[i++] = * (bytes_to_encode++);
93  if (i == 3)
94  {
95  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
96  char_array_4[1] = ( (char_array_3[0] & 0x03) << 4) + ( (char_array_3[1] & 0xf0) >> 4);
97  char_array_4[2] = ( (char_array_3[1] & 0x0f) << 2) + ( (char_array_3[2] & 0xc0) >> 6);
98  char_array_4[3] = char_array_3[2] & 0x3f;
99 
100  for (i = 0; (i < 4) ; i++)
101  {
102  ret += base64_chars[char_array_4[i]];
103  }
104  i = 0;
105  }
106  }
107 
108  if (i)
109  {
110  for (j = i; j < 3; j++)
111  {
112  char_array_3[j] = '\0';
113  }
114 
115  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
116  char_array_4[1] = ( (char_array_3[0] & 0x03) << 4) + ( (char_array_3[1] & 0xf0) >> 4);
117  char_array_4[2] = ( (char_array_3[1] & 0x0f) << 2) + ( (char_array_3[2] & 0xc0) >> 6);
118  char_array_4[3] = char_array_3[2] & 0x3f;
119 
120  for (j = 0; (j < i + 1); j++)
121  {
122  ret += base64_chars[char_array_4[j]];
123  }
124 
125  while ( (i++ < 3) )
126  {
127  ret += '=';
128  }
129 
130  }
131 
132  return ret;
133 
134 }
135 
136 std::string base64_decode (std::string const& encoded_string)
137 {
138  int in_len = encoded_string.size();
139  int i = 0;
140  int j = 0;
141  int in_ = 0;
142  unsigned char char_array_4[4], char_array_3[3];
143  std::string ret;
144 
145  while (in_len-- && ( encoded_string[in_] != '=') && is_base64 (encoded_string[in_]) )
146  {
147  char_array_4[i++] = encoded_string[in_];
148  in_++;
149  if (i == 4)
150  {
151  for (i = 0; i < 4; i++)
152  {
153  char_array_4[i] = base64_chars.find (char_array_4[i]);
154  }
155 
156  char_array_3[0] = (char_array_4[0] << 2) + ( (char_array_4[1] & 0x30) >> 4);
157  char_array_3[1] = ( (char_array_4[1] & 0xf) << 4) + ( (char_array_4[2] & 0x3c) >> 2);
158  char_array_3[2] = ( (char_array_4[2] & 0x3) << 6) + char_array_4[3];
159 
160  for (i = 0; (i < 3); i++)
161  {
162  ret += char_array_3[i];
163  }
164  i = 0;
165  }
166  }
167 
168  if (i)
169  {
170  for (j = i; j < 4; j++)
171  {
172  char_array_4[j] = 0;
173  }
174 
175  for (j = 0; j < 4; j++)
176  {
177  char_array_4[j] = base64_chars.find (char_array_4[j]);
178  }
179 
180  char_array_3[0] = (char_array_4[0] << 2) + ( (char_array_4[1] & 0x30) >> 4);
181  char_array_3[1] = ( (char_array_4[1] & 0xf) << 4) + ( (char_array_4[2] & 0x3c) >> 2);
182  char_array_3[2] = ( (char_array_4[2] & 0x3) << 6) + char_array_4[3];
183 
184  for (j = 0; (j < i - 1); j++)
185  {
186  ret += char_array_3[j];
187  }
188  }
189 
190  return ret;
191 }
192 
193 }
void updateInverseJacobian(const UInt &iQuadPt)
static bool is_base64(unsigned char c)
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191
static const std::string base64_chars