| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Original source taken from: |
|---|
| 31 |
// This is part of the libb64 project, and has been placed |
|---|
| 32 |
// in the public domain. |
|---|
| 33 |
// For details, see http://sourceforge.net/projects/libb64 |
|---|
| 34 |
// |
|---|
| 35 |
// Modified 2009 WhisperSoft s.r.l. |
|---|
| 36 |
// |
|---|
| 37 |
|
|---|
| 38 |
#ifndef __NET_UTIL_BASE64_H__ |
|---|
| 39 |
#define __NET_UTIL_BASE64_H__ |
|---|
| 40 |
|
|---|
| 41 |
#include <string> |
|---|
| 42 |
#include <whisperlib/common/base/types.h> |
|---|
| 43 |
|
|---|
| 44 |
namespace base64 { |
|---|
| 45 |
|
|---|
| 46 |
////////////////////////////////////////////////////////////////////// |
|---|
| 47 |
// |
|---|
| 48 |
// BASE64 Encoding |
|---|
| 49 |
// |
|---|
| 50 |
struct EncodeState { |
|---|
| 51 |
enum EncodeStep { |
|---|
| 52 |
STEP_A, STEP_B, STEP_C |
|---|
| 53 |
}; |
|---|
| 54 |
EncodeStep step; |
|---|
| 55 |
char result; |
|---|
| 56 |
int stepcount; |
|---|
| 57 |
}; |
|---|
| 58 |
|
|---|
| 59 |
void InitEncodeState(EncodeState* state_in); |
|---|
| 60 |
|
|---|
| 61 |
static const char kEncoding[] = |
|---|
| 62 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|---|
| 63 |
"abcdefghijklmnopqrstuvwxyz" |
|---|
| 64 |
"0123456789+/"; |
|---|
| 65 |
|
|---|
| 66 |
inline char EncodeValue(char value_in) { |
|---|
| 67 |
if ( value_in >= sizeof(kEncoding) ) |
|---|
| 68 |
return '='; |
|---|
| 69 |
return kEncoding[(int)value_in]; |
|---|
| 70 |
} |
|---|
| 71 |
static const int kCharsPerLine = 72; |
|---|
| 72 |
|
|---|
| 73 |
int EncodeBlock(const char* plaintext_in, int length_in, |
|---|
| 74 |
char* code_out, EncodeState* state_in, |
|---|
| 75 |
int chars_per_line); |
|---|
| 76 |
int EncodeBlockEnd(char* code_out, EncodeState* state_in); |
|---|
| 77 |
|
|---|
| 78 |
struct Encoder { |
|---|
| 79 |
EncodeState state_; |
|---|
| 80 |
int buffersize_; |
|---|
| 81 |
Encoder(int buffersize_in = 4096) |
|---|
| 82 |
: buffersize_(buffersize_in) { |
|---|
| 83 |
InitEncodeState(&state_); |
|---|
| 84 |
} |
|---|
| 85 |
int Encode(char value_in) { |
|---|
| 86 |
return EncodeValue(value_in); |
|---|
| 87 |
} |
|---|
| 88 |
// IMPORTANT - you need twice length_in available at plaintext_out !! |
|---|
| 89 |
int Encode(const char* code_in, const int length_in, |
|---|
| 90 |
char* plaintext_out, int chars_per_line = kCharsPerLine) { |
|---|
| 91 |
return EncodeBlock(code_in, length_in, plaintext_out, |
|---|
| 92 |
&state_, chars_per_line); |
|---|
| 93 |
} |
|---|
| 94 |
int EncodeEnd(char* plaintext_out) { |
|---|
| 95 |
return EncodeBlockEnd(plaintext_out, &state_); |
|---|
| 96 |
} |
|---|
| 97 |
}; |
|---|
| 98 |
|
|---|
| 99 |
string EncodeString(const string& s); |
|---|
| 100 |
|
|---|
| 101 |
////////////////////////////////////////////////////////////////////// |
|---|
| 102 |
// |
|---|
| 103 |
// BASE64 - Decoding |
|---|
| 104 |
|
|---|
| 105 |
struct DecodeState { |
|---|
| 106 |
enum DecodeStep { |
|---|
| 107 |
STEP_A, STEP_B, STEP_C, STEP_D |
|---|
| 108 |
}; |
|---|
| 109 |
DecodeStep step; |
|---|
| 110 |
char plainchar; |
|---|
| 111 |
}; |
|---|
| 112 |
|
|---|
| 113 |
void InitDecodeState(DecodeState* state_in); |
|---|
| 114 |
|
|---|
| 115 |
static const char kDecoding[] = { |
|---|
| 116 |
62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, |
|---|
| 117 |
-1, -1, -1, -2, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
|---|
| 118 |
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, |
|---|
| 119 |
-1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, |
|---|
| 120 |
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 |
|---|
| 121 |
}; |
|---|
| 122 |
|
|---|
| 123 |
inline int DecodeValue(char value_in) { |
|---|
| 124 |
value_in -= 43; |
|---|
| 125 |
if (value_in < 0 || value_in >= sizeof(kDecoding)) |
|---|
| 126 |
return -1; |
|---|
| 127 |
return kDecoding[(int)value_in]; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
int DecodeValue(char value_in); |
|---|
| 131 |
|
|---|
| 132 |
int DecodeBlock(const char* code_in, const int length_in, |
|---|
| 133 |
char* plaintext_out, DecodeState* state_in); |
|---|
| 134 |
|
|---|
| 135 |
struct Decoder { |
|---|
| 136 |
DecodeState state_; |
|---|
| 137 |
Decoder() { |
|---|
| 138 |
InitDecodeState(&state_); |
|---|
| 139 |
} |
|---|
| 140 |
int Decode(char value_in) { |
|---|
| 141 |
return DecodeValue(value_in); |
|---|
| 142 |
} |
|---|
| 143 |
int Decode(const char* code_in, const int length_in, |
|---|
| 144 |
char* plaintext_out) { |
|---|
| 145 |
return DecodeBlock(code_in, length_in, plaintext_out, &state_); |
|---|
| 146 |
} |
|---|
| 147 |
}; |
|---|
| 148 |
} // namespace base64 |
|---|
| 149 |
|
|---|
| 150 |
#endif // BASE64_ENCODE_H |
|---|