Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Base64 - Buffers

72 views
Skip to first unread message

Ravi Uday

unread,
Jul 26, 2002, 2:11:29 AM7/26/02
to
Hi,

I am in my initial stages of building a IMAP for my specific board
configuration. As a part of the spec for IMAP / POP3 there seems to
be a need for Encoding the byte stream using base64 mechanism.
I found a piece of C - code which would do the job.. but the inputs
are in the form of FILES !! i.e string to be encoded has to be placed
in a file and the program upon reading from file and decoding it..it
outputs again to a FILE !!

I cant afford a file operation on my board. Can you tell me where i
can find a C-code for doing base64 encoding / decoding where the
inputs ( and decoded output ) can be specified in terms of BUFFERS (
unsigned char *)

thanks for ur support

ke...@hplb.hpl.hp.com

unread,
Jul 26, 2002, 4:05:23 AM7/26/02
to
In article <ecca3ca6.02072...@posting.google.com>,

Surely it's not *that* difficult to translate code that writes to
FILE*s into code that writes to character buffers? Is there some part of
it you can't see how to handle?

> thanks for ur support

Well, I suppose that C *is* part of the primeval soup. Doris and Frank,
and Stephen Donaldson, thank you.

--
Chris "Ursuppe, Urland, ur-vile, ur-Lord, ur .. what?" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

Kevin Easton

unread,
Jul 26, 2002, 4:34:58 AM7/26/02
to

Here's some prototype code I wrote for base64 encoding/decoding. The
algorithm is correct, but the interface to the functions leaves a bit
to be desired - like I said, it was prototype-level code.

I _think_ it's ISO C (no platform-specific stuff), and if it's not I'm
sure I'll be set right in short order... You probably also want to
rework the way errors are handled in the decoder, and at the moment the
caller must allocate destination buffers of sufficient size.

- Kevin.

PS If you make any improvements, could you post them back here?

---- begin base64.h ----

#ifndef _BASE64_H
#define _BASE64_H

int b64_encode(char *dest, const char *src, int len);
int b64_decode(char *dest, const char *src);

#endif /* BASE64_H */

---- end base64.h ----

---- begin base64.c ----

#include <string.h>
#include <stdio.h>

#include "base64.h"

static const unsigned char *b64_tbl =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const unsigned char b64_pad = '=';

/* base64 encode a group of between 1 and 3 input chars into a group of
* 4 output chars */
static void encode_group(unsigned char output[], const unsigned char
input[], int n)
{
unsigned char ingrp[3];

ingrp[0] = n > 0 ? input[0] : 0;
ingrp[1] = n > 1 ? input[1] : 0;
ingrp[2] = n > 2 ? input[2] : 0;

/* upper 6 bits of ingrp[0] */
output[0] = n > 0 ? b64_tbl[ingrp[0] >> 2] : b64_pad;
/* lower 2 bits of ingrp[0] | upper 4 bits of ingrp[1] */
output[1] = n > 0 ? b64_tbl[((ingrp[0] & 0x3) << 4) | (ingrp[1]
>> 4)]
: b64_pad;
/* lower 4 bits of ingrp[1] | upper 2 bits of ingrp[2] */
output[2] = n > 1 ? b64_tbl[((ingrp[1] & 0xf) << 2) | (ingrp[2]
>> 6)]
: b64_pad;
/* lower 6 bits of ingrp[2] */
output[3] = n > 2 ? b64_tbl[ingrp[2] & 0x3f] : b64_pad;

}

int b64_encode(char *dest, const char *src, int len)
{
int outsz = 0;

while (len > 0) {
encode_group(dest + outsz, src, len > 3 ? 3 : len);
len -= 3;
src += 3;
outsz += 4;
}

return outsz;
}

/* base64 decode a group of 4 input chars into a group of between 0 and
* 3
* output chars */
static void decode_group(unsigned char output[], const unsigned char
input[], int *n)
{
unsigned char *t1,*t2;
*n = 0;

if (input[0] == b64_pad)
return;

if (input[1] == b64_pad) {
fprintf(stderr, "Warning: orphaned bits ignored.\n");
return;
}

t1 = strchr(b64_tbl, input[0]);
t2 = strchr(b64_tbl, input[1]);

if ((t1 == NULL) || (t2 == NULL)) {
fprintf(stderr, "Warning: garbage found, giving up.\n");
return;
}

output[(*n)++] = ((t1 - b64_tbl) << 2) | ((t2 - b64_tbl) >> 4);

if (input[2] == b64_pad)
return;

t1 = strchr(b64_tbl, input[2]);

if (t1 == NULL) {
fprintf(stderr, "Warning: garbage found, giving up.\n");
return;
}

output[(*n)++] = ((t2 - b64_tbl) << 4) | ((t1 - b64_tbl) >> 2);

if (input[3] == b64_pad)
return;

t2 = strchr(b64_tbl, input[3]);

if (t2 == NULL) {
fprintf(stderr, "Warning: garbage found, giving up.\n");
return;
}

output[(*n)++] = ((t1 - b64_tbl) << 6) | (t2 - b64_tbl);

return;
}

int b64_decode(char *dest, const char *src)
{
int len;
int outsz = 0;

while (*src) {
decode_group(dest + outsz, src, &len);
src += 4;
outsz += len;
}

return outsz;
}

---- end base64.c ----

Ravi Uday

unread,
Jul 27, 2002, 8:08:04 AM7/27/02
to
Kevin Easton <ke...@tomato.pcug.org.au> wrote in message news:<ahr1ji$6v3$1...@tomato.pcug.org.au>...

Kevin:
Thanks man.. i found your code very useful as the implementation is
exact..
In one shot i did get a successful authentication message from the
server !!
I am going through the code and shll keep u informed if the same can
be improved upon

Barry Schwarz

unread,
Jul 27, 2002, 2:06:09 PM7/27/02
to
On 27 Jul 2002 05:08:04 -0700, rav...@yahoo.com (Ravi Uday) wrote:

>Kevin Easton <ke...@tomato.pcug.org.au> wrote in message news:<ahr1ji$6v3$1...@tomato.pcug.org.au>...
>> Ravi Uday <rav...@yahoo.com> wrote:

snip


>
>Kevin:
>Thanks man.. i found your code very useful as the implementation is
>exact..
>In one shot i did get a successful authentication message from the
>server !!
>I am going through the code and shll keep u informed if the same can
>be improved upon

Did you have to quote 150+ lines just to say thanks?


<<Remove the del for email>>

Ravi Uday

unread,
Jul 29, 2002, 5:14:39 AM7/29/02
to
Barry Schwarz <schw...@deloz.net> wrote in message news:<ahuneh$9b3$2...@216.39.135.206>...


Barry :

Oops sorry pal... didnt realise in that urgency !!

0 new messages