sample for base64 encoding in c language

A

aruna.eies.eng

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.
 
A

aruna.eies.eng

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.
 
O

osmium

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
 
M

Mark McIntyre

On Fri, 22 Jun 2007 11:13:23 -0000, in comp.lang.c ,
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
 
M

Mark McIntyre

On Fri, 22 Jun 2007 11:56:22 -0000, in comp.lang.c ,
#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
 
L

Lew Pitcher

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. ------
 
W

websnarf

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.
 
L

Lew Pitcher

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. ------
 
W

websnarf

[...] 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.
 
A

aruna.eies.eng

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

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
 
A

aruna.eies.eng

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

oh sorry that the sample code sent by Low Pithcher, that should be
revised .
aruna.
faculty of engineering
galle
srilanka
 
A

aruna.eies.eng

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
 
L

Lew Pitcher

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.
 
A

aruna

(e-mail address removed) 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.


I'm glad to hear that.
I'm aruna and an intern of WSO2.inc (http://wso2.org/).with in that
internship we had a project which fast infoset in c.The project is
specified at http://www.itu.int/ITU-T/studygroups/com17/languages/X680cor1.pdf
As the WSO2 is an open source company our project is also an open
source one. XML infoset(http://www.w3.org/TR/xml-infoset/) have to be
encoded and ultimate result is a light weight document. so i m
currently doing the encoding part and that the specification says
about ten built in algorithms which is to used at the fast infoset.
they are hexadecimal , base64 , short , long, boolean, float, double,
int, uuid, cdata algorithms. these are just for your information.
Actually i need to read the specification document more. or im forgot
something. that the fast infoset has been implemented in java by sun.
there paper can be referred at http://idealliance.org/proceedings/xtech05/papers/04-01-01/
i believe that will be more simpler than referring the the spec at the
above link. so we can take the advantage from that too as that project
is an open source.
aruna
intern WSO2.inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top