DCL 4.0
Loading...
Searching...
No Matches
Base64Encoder Class Reference

#include <TransferEncoding.h>

Static Public Member Functions

static ByteString encode (const ByteString &_str)

Detailed Description

Definition at line 39 of file TransferEncoding.h.

Member Function Documentation

◆ encode()

ByteString Base64Encoder::encode ( const ByteString & _str)
static

Definition at line 193 of file TransferEncoding.cpp.

194{
195 __DCL_ASSERT(_str.length() > 0);
196
197 size_t nAllocLength = ((_str.length() + 2) / 3) * 4;
198 nAllocLength += 2 * ((nAllocLength / MAX_LINE_LENGTH) + 1); // + 1;
199
200 ByteBuffer* buf = ByteBuffer::create(nAllocLength);
201 byte_t* dest = (byte_t*) buf->data();
202
203 const byte_t* src = (byte_t*)_str.data();
204 size_t len = _str.length();
205 for(size_t i = 1; len >= 3; len -= 3, src += 3)
206 {
207 *dest++ = aBase64Table[src[0] >> 2];
208 *dest++ = aBase64Table[((src[0] & 0x03) << 4) + (src[1] >> 4)];
209 *dest++ = aBase64Table[((src[1] & 0x0f) << 2) + (src[2] >> 6)];
210 *dest++ = aBase64Table[src[2] & 0x3f];
211 if ((i++) == 19) // reached MAX_LINE_LENGTH == (i * 4) == 76
212 {
213 *dest++ = CR;
214 *dest++ = LF;
215 i = 1;
216 }
217 }
218
219 if (len)
220 {
221 *dest++ = aBase64Table[src[0] >> 2];
222 if (len > 1)
223 {
224 *dest++ = aBase64Table[((src[0] & 0x03) << 4) + (src[1] >> 4)];
225 *dest++ = aBase64Table[((src[1] & 0x0f) << 2)];
226 *dest++ = BASE64_PADDING_CHAR;
227 }
228 else
229 {
230 *dest++ = aBase64Table[((src[0] & 0x03) << 4)];
231 *dest++ = BASE64_PADDING_CHAR;
232 *dest++ = BASE64_PADDING_CHAR;
233 }
234 }
235
236 *dest++ = CR;
237 *dest++ = LF;
238
239 *dest = __T('\0');
240 __DCL_ASSERT(buf->data() <= (char*) dest);
241 buf->__dataLength = (char*) dest - buf->data();
242 __DCL_ASSERT(buf->__dataLength <= buf->__allocLength);
243
244 ByteString r = buf;
245 buf->release();
246 return r;
247}
unsigned char byte_t
Definition Config.h:274
#define LF
#define CR
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define __T(str)
Definition Object.h:44
ByteString r
size_t len
ByteBuffer * buf
#define BASE64_PADDING_CHAR
#define MAX_LINE_LENGTH

The documentation for this class was generated from the following files: