OpenCPN Partial API docs
Loading...
Searching...
No Matches
base64.h
Go to the documentation of this file.
1
2// Name: base64.h
3// Author: Angelo Mandato
4// Created: 2005/08/10
5// Copyright: (c) 2005 Angelo Mandato (http://www.spaceblue.com)
6// Licence: wxWidgets licence
8
16#ifndef WX_BASE64_H_
17#define WX_BASE64_H_
18
19// optimization for GCC
20#if defined(__GNUG__) && !defined(__APPLE__)
21#pragma interface "base64.h"
22#endif
23
24#include <wx/string.h>
25
26static const char base64_table[] = {
27 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
28 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
29 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
30 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
31 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'};
32
33static const char base64_pad = '=';
34
35static const short base64_reverse_table[256] = {
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60,
39 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
40 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1,
41 -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
42 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1};
50
52// Based on code from PHP library:
53WXDLLIMPEXP_HTTPENGINE wxString wxBase64Encode(const wxString &str) {
54 wxString szToReturn;
55 int length = str.Length();
56 int current = 0;
57
58 while (length > 2)
59 // keep going until we have less than 24 bits (each item is 8 bits)
60 {
61 szToReturn.Append(base64_table[str.GetChar(current) >> 2]);
62 szToReturn.Append(base64_table[((str.GetChar(current) & 0x03) << 4) +
63 (str.GetChar(current + 1) >> 4)]);
64 szToReturn.Append(base64_table[((str.GetChar(current + 1) & 0x0f) << 2) +
65 (str.GetChar(current + 2) >> 6)]);
66 szToReturn.Append(base64_table[str.GetChar(current + 2) & 0x3f]);
67
68 current += 3;
69 length -= 3; // we just handle 3 octets of data
70 }
71
72 // now deal with the tail end of things
73 if (length != 0) {
74 szToReturn.Append(base64_table[str.GetChar(current) >> 2]);
75
76 //*p++ = base64_table[current[0] >> 2];
77 if (length > 1) {
78 szToReturn.Append(base64_table[((str.GetChar(current) & 0x03) << 4) +
79 (str.GetChar(current + 1) >> 4)]);
80 szToReturn.Append(base64_table[(str.GetChar(current + 1) & 0x0f) << 2]);
81 szToReturn.Append(base64_pad);
82 } else {
83 szToReturn.Append(base64_table[(str.GetChar(current) & 0x03) << 4]);
84 szToReturn.Append(base64_pad);
85 szToReturn.Append(base64_pad);
86 }
87 }
88
89 return szToReturn;
90}
91
93// Based on code from PHP library:
94WXDLLIMPEXP_HTTPENGINE wxString wxBase64Decode(const wxString &str) {
95 wxString szToReturn;
96
97 int length = str.Length();
98 unsigned int current = 0;
99 int i = 0, j = 0, k;
100 wxChar ch = ' ';
101
102 while (current != str.Length() && length-- > 0) {
103 ch = str.GetChar(current++);
104
105 if (ch == base64_pad) break;
106
107 // When Base64 gets POSTed, all pluses are interpreted as spaces.
108 // This line changes them back. It's not exactly the Base64 spec,
109 // but it is completely compatible with it (the spec says that
110 // spaces are invalid). This will also save many people considerable
111 // headache. - Turadg Aleahmad <turadg@wise.berkeley.edu>
112
113 if (ch == ' ') ch = '+';
114
115 ch = base64_reverse_table[(int)ch]; // CHECK
116 if (ch < 0) continue;
117
118 switch (i % 4) {
119 case 0:
120
121 szToReturn.Append(ch << 2);
122 break;
123 case 1:
124 szToReturn.SetChar(j, szToReturn.GetChar(j) | ch >> 4);
125 j++;
126 szToReturn.Append((ch & 0x0f) << 4);
127 break;
128 case 2:
129 szToReturn.SetChar(j, szToReturn.GetChar(j) | ch >> 2);
130 j++;
131 szToReturn.Append((ch & 0x03) << 6);
132 break;
133 case 3:
134 szToReturn.SetChar(j, szToReturn.GetChar(j) | ch);
135 j++;
136 break;
137 }
138 i++;
139 }
140
141 k = j;
142 // mop things up if we ended on a boundary
143 if (ch == base64_pad) {
144 switch (i % 4) {
145 case 0:
146 case 1:
147 return "";
148 }
149 }
150
151 return szToReturn;
152}
153
154#endif // WX_BASE64_H_
WXDLLIMPEXP_HTTPENGINE wxString wxBase64Decode(const wxString &str)
Returns string base64 decoded.
Definition base64.h:94
WXDLLIMPEXP_HTTPENGINE wxString wxBase64Encode(const wxString &str)
Returns string base64 encoded.
Definition base64.h:53