|
Packit Service |
7770af |
/*
|
|
Packit Service |
7770af |
cencoder.c - c source to a base64 encoding algorithm implementation
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
This is part of the libb64 project, and has been placed in the public domain.
|
|
Packit Service |
7770af |
For details, see http://sourceforge.net/projects/libb64
|
|
Packit Service |
7770af |
*/
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
#include "b64/cencode.h"
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
void base64_init_encodestate(base64_encodestate* state_in)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
state_in->step = step_A;
|
|
Packit Service |
7770af |
state_in->result = 0;
|
|
Packit Service |
7770af |
state_in->stepcount = 0;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
char base64_encode_value(char value_in)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
Packit Service |
7770af |
if (value_in > 63) return '=';
|
|
Packit Service |
7770af |
return encoding[(int)value_in];
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
const char* plainchar = plaintext_in;
|
|
Packit Service |
7770af |
const char* const plaintextend = plaintext_in + length_in;
|
|
Packit Service |
7770af |
char* codechar = code_out;
|
|
Packit Service |
7770af |
char result;
|
|
Packit Service |
7770af |
char fragment;
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
result = state_in->result;
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
switch (state_in->step)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
while (1)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
case step_A:
|
|
Packit Service |
7770af |
if (plainchar == plaintextend)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
state_in->result = result;
|
|
Packit Service |
7770af |
state_in->step = step_A;
|
|
Packit Service |
7770af |
return (int)(codechar - code_out);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
fragment = *plainchar++;
|
|
Packit Service |
7770af |
result = (fragment & 0x0fc) >> 2;
|
|
Packit Service |
7770af |
*codechar++ = base64_encode_value(result);
|
|
Packit Service |
7770af |
result = (fragment & 0x003) << 4;
|
|
Packit Service |
7770af |
case step_B:
|
|
Packit Service |
7770af |
if (plainchar == plaintextend)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
state_in->result = result;
|
|
Packit Service |
7770af |
state_in->step = step_B;
|
|
Packit Service |
7770af |
return (int)(codechar - code_out);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
fragment = *plainchar++;
|
|
Packit Service |
7770af |
result |= (fragment & 0x0f0) >> 4;
|
|
Packit Service |
7770af |
*codechar++ = base64_encode_value(result);
|
|
Packit Service |
7770af |
result = (fragment & 0x00f) << 2;
|
|
Packit Service |
7770af |
case step_C:
|
|
Packit Service |
7770af |
if (plainchar == plaintextend)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
state_in->result = result;
|
|
Packit Service |
7770af |
state_in->step = step_C;
|
|
Packit Service |
7770af |
return (int)(codechar - code_out);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
fragment = *plainchar++;
|
|
Packit Service |
7770af |
result |= (fragment & 0x0c0) >> 6;
|
|
Packit Service |
7770af |
*codechar++ = base64_encode_value(result);
|
|
Packit Service |
7770af |
result = (fragment & 0x03f) >> 0;
|
|
Packit Service |
7770af |
*codechar++ = base64_encode_value(result);
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
++(state_in->stepcount);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
/* control should not reach here */
|
|
Packit Service |
7770af |
return (int)(codechar - code_out);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
int base64_encode_blockend(char* code_out, base64_encodestate* state_in)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
char* codechar = code_out;
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
switch (state_in->step)
|
|
Packit Service |
7770af |
{
|
|
Packit Service |
7770af |
case step_B:
|
|
Packit Service |
7770af |
*codechar++ = base64_encode_value(state_in->result);
|
|
Packit Service |
7770af |
*codechar++ = '=';
|
|
Packit Service |
7770af |
*codechar++ = '=';
|
|
Packit Service |
7770af |
break;
|
|
Packit Service |
7770af |
case step_C:
|
|
Packit Service |
7770af |
*codechar++ = base64_encode_value(state_in->result);
|
|
Packit Service |
7770af |
*codechar++ = '=';
|
|
Packit Service |
7770af |
break;
|
|
Packit Service |
7770af |
case step_A:
|
|
Packit Service |
7770af |
break;
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
*codechar++ = '\n';
|
|
Packit Service |
7770af |
|
|
Packit Service |
7770af |
return (int)(codechar - code_out);
|
|
Packit Service |
7770af |
}
|
|
Packit Service |
7770af |
|