|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sflmime.h" size_t decode_base64 (const byte *source, byte *target, size_t source_size)
Decodes a block of Base 64 data and stores the resulting binary data in a target buffer. The target buffer must be at least 3/4 the size of the base 64 data. Returns the number of characters output into the target buffer.
{
size_t
target_size = 0; /* Length of target buffer */
int
nb_block; /* Total number of block */
byte
value, /* Value of Base64 byte */
*p_source, /* Pointer in source buffer */
*p_target; /* Pointer in target buffer */
ASSERT (source);
ASSERT (target);
if (source_size == 0)
return (0);
if (!tables_initialised)
init_conversion_tables ();
/* Bit positions
| byte 1 | byte 2 | byte 3 | byte 4 |
Encoded block 654321 654321 654321 654321 -> 4 bytes of 6 bits
| byte 1 | byte 2 | byte 3 |
Decoded block 65432165 43216543 21654321 -> 3 bytes of 8 bits
*/
nb_block = source_size / 4;
target_size = (size_t) nb_block * 3;
target [target_size] = '\0';
p_source = (byte *) source; /* Point to start of buffers */
p_target = target;
while (nb_block--)
{
/* Byte 1 */
*p_target = char_to_base64 [(byte) *p_source++] << 2;
value = char_to_base64 [(byte) *p_source++];
*p_target++ += ((value & 0x30) >> 4);
/* Byte 2 */
*p_target = ((value & 0x0F) << 4);
value = char_to_base64 [(byte) *p_source++];
*p_target++ += ((value & 0x3C) >> 2);
/* Byte 3 */
*p_target = (value & 0x03) << 6;
value = char_to_base64 [(byte) *p_source++];
*p_target++ += value;
}
return (target_size);
}
| | << | < | > | >> |
|