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