sample for base64 encoding in c language

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aruna.eies.eng@gmail.com

    sample for base64 encoding in c language

    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.

  • aruna.eies.eng@gmail.com

    #2
    Re: sample for base64 encoding in c language

    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.

    Comment

    • osmium

      #3
      Re: sample for base64 encoding in c language

      <aruna.eies.eng @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.




      Comment

      • Chris Dollin

        #4
        Re: sample for base64 encoding in c language

        aruna.eies.eng@ 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

        Comment

        • Mark McIntyre

          #5
          Re: sample for base64 encoding in c language

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

          Comment

          • Mark McIntyre

            #6
            Re: sample for base64 encoding in c language

            On Fri, 22 Jun 2007 11:56:22 -0000, in comp.lang.c ,
            aruna.eies.eng@ 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

            Comment

            • Lew Pitcher

              #7
              Re: sample for base64 encoding in c language

              aruna.eies.eng@ 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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
              "abcdefghijklmn opqrstuvwxyz"
              "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)&&(tria d+byte<s_len); ++byte)
              {
              sr <<= 8;
              sr |= (*(src+triad+by te) & 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+i x))) < 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_arra y,0,64);
              if (encode(binsize ,Bin_array,64,S tr_array) == 0)
              {
              int Strsize;

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

              Strsize = strlen(Str_arra y);
              if (decode(Strsize ,Str_array,bins ize,Raw_array) == 0)
              {
              if (memcmp(Bin_arr ay,Raw_array,bi nsize) == 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",bufsi ze);
              for (binsize = 0; binsize < 7; ++binsize)
              {
              memset(Str_arra y,0,64);
              if (encode(binsize ,Bin_array,bufs ize,Str_array) == 0)
              {
              int Strsize;

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

              Strsize = strlen(Str_arra y);
              if (decode(Strsize ,Str_array,bins ize,Raw_array) == 0)
              {
              if (memcmp(Bin_arr ay,Raw_array,bi nsize) == 0)
              printf("values match\n");
              else printf("values differ\n");
              }
              else printf("decode failed\n");
              }
              else printf("%d bytes failed to encode\n",binsi ze);
              }
              }
              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. ------

              Comment

              • websnarf@gmail.com

                #8
                Re: sample for base64 encoding in c language

                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:



                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
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



                Comment

                • Lew Pitcher

                  #9
                  Re: sample for base64 encoding in c language

                  websnarf@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:
                  >

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

                  Comment

                  • websnarf@gmail.com

                    #10
                    Re: sample for base64 encoding in c language

                    On Jun 26, 7:00 pm, Lew Pitcher <lpitc...@teksa vvy.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
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.



                    Comment

                    • aruna.eies.eng@gmail.com

                      #11
                      Re: sample for base64 encoding in c language

                      On Jun 22, 5:11 pm, Chris Dollin <chris.dol...@h p.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(Internation al
                      Telecomunicatio n 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

                      Comment

                      • aruna.eies.eng@gmail.com

                        #12
                        Re: sample for base64 encoding in c language

                        On Jun 22, 5:11 pm, Chris Dollin <chris.dol...@h p.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

                        Comment

                        • aruna.eies.eng@gmail.com

                          #13
                          Re: sample for base64 encoding in c language

                          On Jun 27, 4:35 am, Lew Pitcher <lpitc...@teksa vvy.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[] = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
                          "abcdefghijklmn opqrstuvwxyz"
                          "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)&&(tria d+byte<s_len); ++byte)
                          {
                          sr <<= 8;
                          sr |= (*(src+triad+by te) & 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+i x))) < 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_arra y,0,64);
                          if (encode(binsize ,Bin_array,64,S tr_array) == 0)
                          {
                          int Strsize;
                          >
                          printf("%d bytes becomes >%s<\n",binsize ,Str_array);
                          >
                          Strsize = strlen(Str_arra y);
                          if (decode(Strsize ,Str_array,bins ize,Raw_array) == 0)
                          {
                          if (memcmp(Bin_arr ay,Raw_array,bi nsize) == 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",bufsi ze);
                          for (binsize = 0; binsize < 7; ++binsize)
                          {
                          memset(Str_arra y,0,64);
                          if (encode(binsize ,Bin_array,bufs ize,Str_array) == 0)
                          {
                          int Strsize;
                          >
                          printf("%d bytes becomes >%s<\n",binsize ,Str_array);
                          >
                          Strsize = strlen(Str_arra y);
                          if (decode(Strsize ,Str_array,bins ize,Raw_array) == 0)
                          {
                          if (memcmp(Bin_arr ay,Raw_array,bi nsize) == 0)
                          printf("values match\n");
                          else printf("values differ\n");
                          }
                          else printf("decode failed\n");
                          }
                          else printf("%d bytes failed to encode\n",binsi ze);
                          }
                          }
                          return EXIT_SUCCESS;
                          >
                          }
                          >
                          --
                          Lew Pitcher
                          >
                          Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digital freehold.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

                          Comment

                          • Lew Pitcher

                            #14
                            Re: sample for base64 encoding in c language

                            On Jul 5, 7:48 am, aruna.eies....@ gmail.com wrote:
                            On Jun 27, 4:35 am, Lew Pitcher <lpitc...@teksa vvy.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.


                            Comment

                            Working...