root/trunk/whisperlib/net/util/base64.cc

Revision 7, 6.6 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
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 //
32 // This is part of the libb64 project, and has been placed
33 // in the public domain.
34 // For details, see http://sourceforge.net/projects/libb64
35 //
36 // Modified 2009 WhisperSoft s.r.l.
37 //
38 #include "net/util/base64.h"
39
40 namespace base64 {
41
42 string EncodeString(const string& s) {
43   const int32 buflen = 2 * s.size() + 4;
44   char* encbuf = new char[buflen];
45   base64::Encoder encoder;
46   const int32 len = encoder.Encode(s.c_str(), s.size(), encbuf, buflen);
47   const int32 len2 = encoder.EncodeEnd(encbuf + len);
48   *(encbuf + len + len2) = '\0';
49   const string ret(encbuf, len + len2);
50   delete [] encbuf;
51   return ret;
52 }
53
54 void InitEncodeState(EncodeState* state_in) {
55   state_in->step = EncodeState::STEP_A;
56   state_in->result = 0;
57   state_in->stepcount = 0;
58 }
59
60
61 int EncodeBlock(const char* plaintext_in, int length_in,
62                 char* code_out, EncodeState* state_in,
63                 int chars_per_line) {
64   const char* plainchar = plaintext_in;
65   const char* const plaintextend = plaintext_in + length_in;
66   char* codechar = code_out;
67   char result;
68   char fragment;
69
70   result = state_in->result;
71
72   switch (state_in->step) {
73     while (1) {
74       case EncodeState::STEP_A:
75         if (plainchar == plaintextend) {
76           state_in->result = result;
77           state_in->step = EncodeState::STEP_A;
78           return codechar - code_out;
79         }
80         fragment = *plainchar++;
81         result = (fragment & 0x0fc) >> 2;
82         *codechar++ = EncodeValue(result);
83         result = (fragment & 0x003) << 4;
84       case EncodeState::STEP_B:
85         if (plainchar == plaintextend) {
86           state_in->result = result;
87           state_in->step = EncodeState::STEP_B;
88           return codechar - code_out;
89         }
90         fragment = *plainchar++;
91         result |= (fragment & 0x0f0) >> 4;
92         *codechar++ = EncodeValue(result);
93         result = (fragment & 0x00f) << 2;
94       case EncodeState::STEP_C:
95         if (plainchar == plaintextend) {
96           state_in->result = result;
97           state_in->step = EncodeState::STEP_C;
98           return codechar - code_out;
99         }
100         fragment = *plainchar++;
101         result |= (fragment & 0x0c0) >> 6;
102         *codechar++ = EncodeValue(result);
103         result  = (fragment & 0x03f) >> 0;
104         *codechar++ = EncodeValue(result);
105
106         ++(state_in->stepcount);
107         if (chars_per_line > 0 && state_in->stepcount == chars_per_line/4) {
108           *codechar++ = '\n';
109           state_in->stepcount = 0;
110         }
111     }
112   }
113   // control should not reach here
114   return codechar - code_out;
115 }
116
117 int EncodeBlockEnd(char* code_out, EncodeState* state_in) {
118   char* codechar = code_out;
119   switch (state_in->step) {
120     case EncodeState::STEP_B:
121       *codechar++ = EncodeValue(state_in->result);
122       *codechar++ = '=';
123       *codechar++ = '=';
124       break;
125     case EncodeState::STEP_C:
126       *codechar++ = EncodeValue(state_in->result);
127       *codechar++ = '=';
128       break;
129     case EncodeState::STEP_A:
130       break;
131   }
132   return codechar - code_out;
133 }
134
135 //////////////////////////////////////////////////////////////////////
136
137 void InitDecodeState(DecodeState* state_in) {
138   state_in->step = DecodeState::STEP_A;
139   state_in->plainchar = 0;
140 }
141
142 int DecodeBlock(const char* code_in, const int length_in,
143                 char* plaintext_out, DecodeState* state_in) {
144   const char* codechar = code_in;
145   char* plainchar = plaintext_out;
146   char fragment;
147
148   *plainchar = state_in->plainchar;
149
150   switch (state_in->step) {
151     while (1) {
152       case DecodeState::STEP_A:
153         do {
154           if (codechar == code_in+length_in) {
155             state_in->step = DecodeState::STEP_A;
156             state_in->plainchar = *plainchar;
157             return plainchar - plaintext_out;
158           }
159           fragment = (char)DecodeValue(*codechar++);
160         } while (fragment < 0);
161         *plainchar    = (fragment & 0x03f) << 2;
162       case DecodeState::STEP_B:
163         do {
164           if (codechar == code_in+length_in) {
165             state_in->step = DecodeState::STEP_B;
166             state_in->plainchar = *plainchar;
167             return plainchar - plaintext_out;
168           }
169           fragment = (char)DecodeValue(*codechar++);
170         } while (fragment < 0);
171         *plainchar++ |= (fragment & 0x030) >> 4;
172         *plainchar    = (fragment & 0x00f) << 4;
173       case DecodeState::STEP_C:
174         do {
175           if (codechar == code_in+length_in) {
176             state_in->step = DecodeState::STEP_C;
177             state_in->plainchar = *plainchar;
178             return plainchar - plaintext_out;
179           }
180           fragment = (char)DecodeValue(*codechar++);
181         } while (fragment < 0);
182         *plainchar++ |= (fragment & 0x03c) >> 2;
183         *plainchar    = (fragment & 0x003) << 6;
184       case DecodeState::STEP_D:
185         do {
186           if (codechar == code_in+length_in) {
187             state_in->step = DecodeState::STEP_D;
188             state_in->plainchar = *plainchar;
189             return plainchar - plaintext_out;
190           }
191           fragment = (char)DecodeValue(*codechar++);
192         } while (fragment < 0);
193         *plainchar++   |= (fragment & 0x03f);
194     }
195   }
196   // control should not reach here
197   return plainchar - plaintext_out;
198 }
199
200 }
Note: See TracBrowser for help on using the browser.