Code::Blocks  SVN r11506
base64.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 10769 $
6  * $Id: base64.cpp 10769 2016-02-06 14:26:58Z mortenmacfly $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/base64.cpp $
8  */
9 
10 //*********************************************************************
11 //* Base64 - a simple base64 encoder and decoder.
12 //*
13 //* Copyright (c) 1999, Bob Withers - bwit@pobox.com
14 //*
15 //* This code may be freely used for any purpose, either personal
16 //* or commercial, provided the authors copyright notice remains
17 //* intact.
18 //*********************************************************************
19 //
20 // converted to wxWindows by Frank Bu?
21 //
22 
23 #include "base64.h"
24 
25 #if wxCHECK_VERSION(3, 0, 0)
26 #include <wx/unichar.h>
27 #endif
28 
29 const wxChar fillchar = '=';
30 
31  // 00000000001111111111222222
32  // 01234567890123456789012345
33 static wxString cvt = _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34 
35  // 22223333333333444444444455
36  // 67890123456789012345678901
37  "abcdefghijklmnopqrstuvwxyz"
38 
39  // 555555556666
40  // 234567890123
41  "0123456789+/");
42 
44 {
45  return wxBase64::Encode((const wxUint8*)data.c_str(), data.Length());
46 }
47 
48 wxString wxBase64::Encode(const wxUint8* pData, size_t len)
49 {
50  size_t c;
51  wxString ret;
52  ret.Alloc(len * 4 / 3 + len * 2);
53 
54  for (size_t i = 0; i < len; ++i)
55  {
56  c = (pData[i] >> 2) & 0x3f;
57  ret.Append(cvt[c], 1);
58  c = (pData[i] << 4) & 0x3f;
59  if (++i < len)
60  c |= (pData[i] >> 4) & 0x0f;
61 
62  ret.Append(cvt[c], 1);
63  if (i < len)
64  {
65  c = (pData[i] << 2) & 0x3f;
66  if (++i < len)
67  c |= (pData[i] >> 6) & 0x03;
68 
69  ret.Append(cvt[c], 1);
70  }
71  else
72  {
73  ++i;
74  ret.Append(fillchar, 1);
75  }
76 
77  if (i < len)
78  {
79  c = pData[i] & 0x3f;
80  ret.Append(cvt[c], 1);
81  }
82  else
83  {
84  ret.Append(fillchar, 1);
85  }
86  }
87 
88  return ret;
89 }
90 
92 {
93  int c;
94  int c1;
95  size_t len = data.Length();
96  wxString ret;
97  ret.Alloc(data.Length() * 3 / 4);
98 
99  for (size_t i = 0; i < len; ++i)
100  {
101  // TODO: check all Find results for -1 as result of wrong input data for release build
102  c = cvt.Find(data[i]);
103  wxASSERT_MSG(c >= 0, _T("invalid base64 input"));
104  ++i;
105  c1 = cvt.Find(data[i]);
106  wxASSERT_MSG(c1 >= 0, _T("invalid base64 input"));
107  c = (c << 2) | ((c1 >> 4) & 0x3);
108  #if wxCHECK_VERSION(3, 0, 0)
109  ret.Append(static_cast<wxUniChar>(c), 1);
110  #else
111  ret.Append(c, 1);
112  #endif
113  if (++i < len)
114  {
115  c = data[i];
116  if ((char)fillchar == c)
117  break;
118 
119  #if wxCHECK_VERSION(3, 0, 0)
120  c = cvt.Find(static_cast<wxUniChar>(c));
121  #else
122  c = cvt.Find(c);
123  #endif
124  wxASSERT_MSG(c >= 0, _T("invalid base64 input"));
125  c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
126  #if wxCHECK_VERSION(3, 0, 0)
127  ret.Append(static_cast<wxUniChar>(c1), 1);
128  #else
129  ret.Append(c1, 1);
130  #endif
131  }
132 
133  if (++i < len)
134  {
135  c1 = data[i];
136  if ((char)fillchar == c1)
137  break;
138 
139  #if wxCHECK_VERSION(3, 0, 0)
140  c1 = cvt.Find(static_cast<wxUniChar>(c1));
141  #else
142  c1 = cvt.Find(c1);
143  #endif
144  wxASSERT_MSG(c1 >= 0, _T("invalid base64 input"));
145  c = ((c << 6) & 0xc0) | c1;
146  #if wxCHECK_VERSION(3, 0, 0)
147  ret.Append(static_cast<wxUniChar>(c), 1);
148  #else
149  ret.Append(c, 1);
150  #endif
151  }
152  }
153 
154  return ret;
155 }
#define wxASSERT_MSG(condition, message)
const wxChar fillchar
Definition: base64.cpp:29
size_t Length() const
wxCStrData c_str() const
#define _T(string)
wxUSE_UNICODE_dependent wxChar
bool Alloc(size_t nLen)
DLLIMPORT wxString Encode(const wxUint8 *pData, size_t len)
Definition: base64.cpp:48
wxString & Append(const char *psz)
DLLIMPORT wxString Decode(const wxString &data)
Definition: base64.cpp:91
int Find(wxUniChar ch, bool fromEnd=false) const
unsigned char wxUint8