423,473 Members | 2,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,473 IT Pros & Developers. It's quick & easy.

sample for base64 encoding in c language

P: n/a
i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.

Jun 22 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Jun 22, 4:13 pm, aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.
hey im adding more,
i tried with this

#include <stdio.h>

int main(void){
long l;
l=A64L("asdbsde");
printf(%d,l);
return 0;

}
but at the A64l it says this is Compatible with UNIX System V C.
so cud u find the error there.

Jun 22 '07 #2

P: n/a
<ar************@gmail.comwrote:
>i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
The number 6 item on this link may help. Note that the code has been
criticized and also note that the download has a lot more code than the tiny
fragments shown in situ. I did the download and it looked like real source
code. Then I lost interest. I think it is entirely possible that you might
code it yourself and come out with comparable results, timewise. I am quite
sure base64 is documented well enough on the Web that it is just a question
of writing the code. And when you get through you *know* what you have. I
am registered for downloads on this site and it hasn't had any nasty side
effects.

It is also entirely possible there is something better already on the net.

http://www.codeproject.com/info/search.asp
Jun 22 '07 #3

P: n/a
ar************@gmail.com wrote:
i am currently trying to convert data into binary data.
What is your data, and what do you mean by "binary data"?

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 22 '07 #4

P: n/a
On Fri, 22 Jun 2007 11:13:23 -0000, in comp.lang.c ,
ar************@gmail.com wrote:
>i am currently trying to convert data into binary data.
You need to clarify what you mean. Data is data is data.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 22 '07 #5

P: n/a
On Fri, 22 Jun 2007 11:56:22 -0000, in comp.lang.c ,
ar************@gmail.com wrote:
>#include <stdio.h>
int main(void){
long l;
l=A64L("asdbsde");
no definition of function "A64L" provided.
> printf(%d,l);
missing quote marks round specifier
missing \n at the end of the format string - output not guaranteed.
%d is not the specifier for a long, undefined behaviour.
>but at the A64l it says this is Compatible with UNIX System V C.
no idea what you mean by that.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jun 22 '07 #6

P: n/a
ar************@gmail.com wrote:
i am currently trying to convert data into binary data.
Assuming that your Subject: heading is an accurate indicator of what you are
looking for ("sample for base64 encoding in C language"), here's some code for
you....

/*
** MIME Base64 coding examples
**
** encode() encodes an arbitrary data block into MIME Base64 format string
** decode() decodes a MIME Base64 format string into raw data
**
** Global table base64[] carries the MIME Base64 conversion characters
*/
/*
** Global data used by both binary-to-base64 and base64-to-binary conversions
*/

static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/";

/*
** ENCODE RAW into BASE64
*/

/* Encode source from raw data into Base64 encoded string */
int encode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned triad;

for (triad = 0; triad < s_len; triad += 3)
{
unsigned long int sr;
unsigned byte;

for (byte = 0; (byte<3)&&(triad+byte<s_len); ++byte)
{
sr <<= 8;
sr |= (*(src+triad+byte) & 0xff);
}

sr <<= (6-((8*byte)%6))%6; /*shift left to next 6bit alignment*/

if (d_len < 4) return 1; /* error - dest too short */

*(dst+0) = *(dst+1) = *(dst+2) = *(dst+3) = '=';
switch(byte)
{
case 3:
*(dst+3) = base64[sr&0x3f];
sr >>= 6;
case 2:
*(dst+2) = base64[sr&0x3f];
sr >>= 6;
case 1:
*(dst+1) = base64[sr&0x3f];
sr >>= 6;
*(dst+0) = base64[sr&0x3f];
}
dst += 4; d_len -= 4;
}

return 0;
}

/*
** DECODE BASE64 into RAW
*/

/* determine which sextet value a Base64 character represents */
int tlu(int byte)
{
int index;

for (index = 0; index < 64; ++index)
if (base64[index] == byte)
break;
if (index 63) index = -1;
return index;
}

/*
** Decode source from Base64 encoded string into raw data
**
** Returns: 0 - Success
** 1 - Error - Source underflow - need more base64 data
** 2 - Error - Chunk contains half a byte of data
** 3 - Error - Decoded results will overflow output buffer
*/
int decode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned six, dix;

dix = 0;

for (six = 0; six < s_len; six += 4)
{
unsigned long sr;
unsigned ix;

sr = 0;
for (ix = 0; ix < 4; ++ix)
{
int sextet;

if (six+ix >= s_len)
return 1;
if ((sextet = tlu(*(src+six+ix))) < 0)
break;
sr <<= 6;
sr |= (sextet & 0x3f);
}

switch (ix)
{
case 0: /* end of data, no padding */
return 0;

case 1: /* can't happen */
return 2;

case 2: /* 1 result byte */
sr >>= 4;
if (dix d_len) return 3;
*(dst+dix) = (sr & 0xff);
++dix;
break;

case 3: /* 2 result bytes */
sr >>= 2;
if (dix+1 d_len) return 3;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 2;
break;

case 4: /* 3 result bytes */
if (dix+2 d_len) return 3;
*(dst+dix+2) = (sr & 0xff);
sr >>= 8;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 3;
break;
}
}
return 0;
}
/************************************************** ****
** Test bed program to encode and decode base64 data **
************************************************** ****/

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

int main(void)
{
char Bin_array[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
char Str_array[64];
char Raw_array[64];
int binsize, bufsize;

printf("Test 1 - increasing size binary data\n");
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,64,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n");
}

printf("\nTest 2 - decreasing size buffer\n");
for (bufsize = 10; bufsize 0; --bufsize)
{
printf("** %d byte buffer\n",bufsize);
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,bufsize,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n",binsize);
}
}
return EXIT_SUCCESS;
}
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Jun 26 '07 #7

P: n/a
On Jun 22, 4:13 am, aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.
Uhh ... under the assumption that you want a base64 encoder and/or
decoder (based on your subject) you may find a fairly robust one in
the bstraux module of the Better String Library from here:

http://bstring.sf.net/

Unlike the one posted by Lew Pitcher, the one in Bstrlib actually
takes binary input, not just '\0'-free C string input (which kind of
defeats the entire point of a base64 codec). Anyways, it tracks
decoder errors and allows you to stream the conversion through IO
(because you might not have the memory to do it) or whatever.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 27 '07 #8

P: n/a
we******@gmail.com wrote:
On Jun 22, 4:13 am, aruna.eies....@gmail.com wrote:
>i am currently trying to convert data into binary data.for that i need
to know how to achieve it in c language and what are the libraries
that we can use. so if any one can send me a sample code or send me
the library file which helps that is really grateful.
aruna.

Uhh ... under the assumption that you want a base64 encoder and/or
decoder (based on your subject) you may find a fairly robust one in
the bstraux module of the Better String Library from here:

http://bstring.sf.net/

Unlike the one posted by Lew Pitcher, the one in Bstrlib actually
takes binary input, not just '\0'-free C string input (which kind of
defeats the entire point of a base64 codec).
Ahem!

If you examined the code, you would have seen that the encode() function does
not "just take '\0'-free C string input". It indeed takes a char buffer of
arbitrary binary data, and transforms that into a MIME base64 string.
Similarly, the decode() function takes a MIME base64 string of specified
length (possibly terminated by a non-MIME-base64 character) and transforms
that into a buffer of arbitrary binary data.

I suggest that next time you are tempted to critique some code, you should
read it first.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------

Jun 27 '07 #9

P: n/a
On Jun 26, 7:00 pm, Lew Pitcher <lpitc...@teksavvy.comwrote:
websn...@gmail.com wrote:
[...] Unlike the one posted by Lew Pitcher, the one in Bstrlib actually
takes binary input, not just '\0'-free C string input (which kind of
defeats the entire point of a base64 codec).

Ahem!

If you examined the code, you would have seen that the encode() function
does not "just take '\0'-free C string input". It indeed takes a char
buffer of arbitrary binary data, and transforms that into a MIME base64
string. [etc]
Whoops! Sorry, you are right. I did skim your code, but just way too
quickly. Indeed, I withdraw my complaint about your code; it does
appear to be better designed and more correct than I thought.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 27 '07 #10

P: n/a
On Jun 22, 5:11 pm, Chris Dollin <chris.dol...@hp.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.

What is your data, and what do you mean by "binary data"?

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
if i say you the full story, we are currently developing a project
named 'fast infoset' which has recommended ITU(International
Telecomunication Union), this is to speed up the messaging and there
is one open source implementation named fi at sun under the glass fish
projects.so we are going to do the c implementation.so we need to
encode the xml data into binary data, according to the specification
there are several encoding methods according to the character data for
an example for integers there is int encoding algorithms, which is a
built in algorithm. so i need to start my stuff some where else , that
is why i tried to have some sample codes which encode the just
character data in to base64 encoded data. for that i tried to find a
method from the library function as a result of that i got above
mentioned function. but it did nt work.so any way i saw one sample
code is given there at the below which is sent by Mark McIntyre . I
tried with that, amaizing that is working.
so any way thanks you lot.
aruna

Jul 5 '07 #11

P: n/a
On Jun 22, 5:11 pm, Chris Dollin <chris.dol...@hp.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.

What is your data, and what do you mean by "binary data"?

--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
oh sorry that the sample code sent by Low Pithcher, that should be
revised .
aruna.
faculty of engineering
galle
srilanka

Jul 5 '07 #12

P: n/a
On Jun 27, 4:35 am, Lew Pitcher <lpitc...@teksavvy.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.

Assuming that your Subject: heading is an accurate indicator of what you are
looking for ("sample for base64 encoding in C language"), here's some code for
you....

/*
** MIME Base64 coding examples
**
** encode() encodes an arbitrary data block into MIME Base64 format string
** decode() decodes a MIME Base64 format string into raw data
**
** Global table base64[] carries the MIME Base64 conversion characters
*/

/*
** Global data used by both binary-to-base64 and base64-to-binary conversions
*/

static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/";

/*
** ENCODE RAW into BASE64
*/

/* Encode source from raw data into Base64 encoded string */
int encode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned triad;

for (triad = 0; triad < s_len; triad += 3)
{
unsigned long int sr;
unsigned byte;

for (byte = 0; (byte<3)&&(triad+byte<s_len); ++byte)
{
sr <<= 8;
sr |= (*(src+triad+byte) & 0xff);
}

sr <<= (6-((8*byte)%6))%6; /*shift left to next 6bit alignment*/

if (d_len < 4) return 1; /* error - dest too short */

*(dst+0) = *(dst+1) = *(dst+2) = *(dst+3) = '=';
switch(byte)
{
case 3:
*(dst+3) = base64[sr&0x3f];
sr >>= 6;
case 2:
*(dst+2) = base64[sr&0x3f];
sr >>= 6;
case 1:
*(dst+1) = base64[sr&0x3f];
sr >>= 6;
*(dst+0) = base64[sr&0x3f];
}
dst += 4; d_len -= 4;
}

return 0;

}

/*
** DECODE BASE64 into RAW
*/

/* determine which sextet value a Base64 character represents */
int tlu(int byte)
{
int index;

for (index = 0; index < 64; ++index)
if (base64[index] == byte)
break;
if (index 63) index = -1;
return index;

}

/*
** Decode source from Base64 encoded string into raw data
**
** Returns: 0 - Success
** 1 - Error - Source underflow - need more base64 data
** 2 - Error - Chunk contains half a byte of data
** 3 - Error - Decoded results will overflow output buffer
*/
int decode(unsigned s_len, char *src, unsigned d_len, char *dst)
{
unsigned six, dix;

dix = 0;

for (six = 0; six < s_len; six += 4)
{
unsigned long sr;
unsigned ix;

sr = 0;
for (ix = 0; ix < 4; ++ix)
{
int sextet;

if (six+ix >= s_len)
return 1;
if ((sextet = tlu(*(src+six+ix))) < 0)
break;
sr <<= 6;
sr |= (sextet & 0x3f);
}

switch (ix)
{
case 0: /* end of data, no padding */
return 0;

case 1: /* can't happen */
return 2;

case 2: /* 1 result byte */
sr >>= 4;
if (dix d_len) return 3;
*(dst+dix) = (sr & 0xff);
++dix;
break;

case 3: /* 2 result bytes */
sr >>= 2;
if (dix+1 d_len) return 3;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 2;
break;

case 4: /* 3 result bytes */
if (dix+2 d_len) return 3;
*(dst+dix+2) = (sr & 0xff);
sr >>= 8;
*(dst+dix+1) = (sr & 0xff);
sr >>= 8;
*(dst+dix) = (sr & 0xff);
dix += 3;
break;
}
}
return 0;

}

/************************************************** ****
** Test bed program to encode and decode base64 data **
************************************************** ****/

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

int main(void)
{
char Bin_array[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
char Str_array[64];
char Raw_array[64];
int binsize, bufsize;

printf("Test 1 - increasing size binary data\n");
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,64,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n");
}

printf("\nTest 2 - decreasing size buffer\n");
for (bufsize = 10; bufsize 0; --bufsize)
{
printf("** %d byte buffer\n",bufsize);
for (binsize = 0; binsize < 7; ++binsize)
{
memset(Str_array,0,64);
if (encode(binsize,Bin_array,bufsize,Str_array) == 0)
{
int Strsize;

printf("%d bytes becomes >%s<\n",binsize,Str_array);

Strsize = strlen(Str_array);
if (decode(Strsize,Str_array,binsize,Raw_array) == 0)
{
if (memcmp(Bin_array,Raw_array,binsize) == 0)
printf("values match\n");
else printf("values differ\n");
}
else printf("decode failed\n");
}
else printf("%d bytes failed to encode\n",binsize);
}
}
return EXIT_SUCCESS;

}

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
hi Lew ,
i m really grateful you and amazing that is really working .
so thanks lot .
aruna
faculty of engineering
galle
sri lanka
intern wso2.inc

Jul 5 '07 #13

P: n/a
On Jul 5, 7:48 am, aruna.eies....@gmail.com wrote:
On Jun 27, 4:35 am, Lew Pitcher <lpitc...@teksavvy.comwrote:
aruna.eies....@gmail.com wrote:
i am currently trying to convert data into binary data.
Assuming that your Subject: heading is an accurate indicator of what you are
looking for ("sample for base64 encoding in C language"), here's some code for
you....
/*
** MIME Base64 coding examples
**
[snip]
hi Lew ,
i m really grateful you and amazing that is really working .
so thanks lot .
You're welcome.

I wrote that code as an example for an XML development project I was
working on. From that C code, I wrote a corresponding COBOL program to
encode and decode in base64 (the C code being a template for the
webish side of the system, and the COBOL for the mainframe side of the
system).

I'm glad it worked for you.
Jul 5 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.