Need Help with base64

Discussion in 'C Programming' started by pycraze, Jul 23, 2007.

  1. pycraze

    pycraze Guest

    Hi ,

    I am currently trying to implement base64 encoding and decoding
    scheme in C . Python has a module , base64 , that will do the
    encoding and decoding with ease . I am aware of OpenSSL having support
    for base64 encoding and decoding , but i will have to now implement
    both in C without using the openssl libraries .

    I was able to download a code w.r.t. base 64 encoding and
    decoding . I am attaching the code below .

    void encodeblock( unsigned char cin[3], unsigned char cout[4], int
    nlen )
    {

    cout[0] = cb64[ cin[0] >> 2 ];
    cout[1] = cb64[ ((cin[0] & 0x03) << 4) | ((cin[1] & 0xf0) >> 4) ];
    cout[2] = (unsigned char) (nlen > 1 ? cb64[ ((cin[1] & 0x0f) << 2)
    | ((cin[2] & 0xc0) >> 6) ] : '=');
    cout[3] = (unsigned char) (nlen > 2 ? cb64[ cin[2] & 0x3f ] :
    '=');

    }

    void decodeblock( unsigned char cin[4], unsigned char cout[3] )
    {
    cout[ 0 ] = (unsigned char ) (cin[0] << 2 | cin[1] >> 4);
    cout[ 1 ] = (unsigned char ) (cin[1] << 4 | cin[2] >> 2);
    cout[ 2 ] = (unsigned char ) (((cin[2] << 6) & 0xc0) | cin[3]);

    }

    int base641_decodestring(char* pcstr,int size,char** ppcdest)
    {
    unsigned char cin[4] = {""};
    unsigned char cout[3] = {""};
    unsigned char cv = 0;
    int ni = 0;
    int nlen = 0;
    char* cptr = pcstr;
    *ppcdest = malloc(sizeof(char)*160);
    if (!*ppcdest)
    {
    return 1;
    }
    memset(*ppcdest,0,sizeof(char)*160);

    char* pcstring = malloc( sizeof(char) * SIZE);
    if (!pcstring)
    {
    aps_log("APS_log.txt","\nbae64.c:base64encode:malloc failed
    \n");
    return 1;
    }
    memset(pcstring,'\0',SIZE);

    for( nlen = 0, ni = 0; ni < 4; ni++ )
    {
    cv = 0;
    while( cv == 0 )
    {
    cv = (unsigned char) *cptr;
    cv = (unsigned char) ((cv < 43 || cv > 122) ? 0 :
    cd64[ cv - 43 ]);
    if( cv )
    {
    cv = (unsigned char) ((cv == '$') ? 0 : cv - 61);
    }
    }
    if( cptr++ )
    {
    nlen++;
    if( cv )
    {
    cin[ ni ] = (unsigned char) (cv - 1);
    }
    }
    else
    {
    cin[ni] = 0;
    }
    }
    if( nlen )
    {
    decodeblock( cin, cout );

    }
    memcpy(*ppcdest,cout,160);
    return 0;

    }/*end of base64_decode */

    char* base64_encode(char* pcstr)
    {
    unsigned char cin[3] = {""};
    unsigned char cout[4] = {""};
    int ni = 0;
    int nlen = 0;
    int flag = 1;
    char* cptr = pcstr;
    char* pcstring = malloc( sizeof(char) * SIZE);
    if (!pcstring)
    {
    aps_log("APS_log.txt","\nbae64.c:base64encode:malloc failed
    \n");
    return (void*)0;
    }
    memset(pcstring,'\0',SIZE);
    while( flag )
    {
    for( ni = 0; ni < 3; ni++ )
    {
    cin[ni] = (unsigned char)*cptr;
    if( *++cptr != '\0' )
    nlen++;
    else
    {
    cin[ni] = 0;
    flag = 0;
    break;
    }
    }
    encodeblock(cin, cout,nlen);
    strcat(pcstring,cout);
    }

    return pcstring;

    }/* end of base64_encode */

    But this code gives different hex values as compared to the
    python base64.encodestring and base64.decodestring respectively .

    I need some help on this matter . I need some pointers on
    where is the mistake for the above file .
     
    pycraze, Jul 23, 2007
    #1
    1. Advertising

  2. On Mon, 23 Jul 2007 05:37:40 -0700, pycraze wrote:
    > But this code gives different hex values as compared to the
    >python base64.encodestring and base64.decodestring respectively .
    > I need some help on this matter . I need some pointers on
    >where is the mistake for the above file .


    It's usually easier to re-use existing code than to implement (and
    debug) something from scratch. Several Base64 implementations are
    available, e.g. http://www.synesis.com.au/software/b64.html


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
     
    Roland Pibinger, Jul 23, 2007
    #2
    1. Advertising

  3. pycraze

    Mark Bluemel Guest

    pycraze wrote:
    > Hi ,
    >
    > I am currently trying to implement base64 encoding and decoding
    > scheme in C . Python has a module , base64 , that will do the
    > encoding and decoding with ease . I am aware of OpenSSL having support
    > for base64 encoding and decoding , but i will have to now implement
    > both in C without using the openssl libraries .
    >
    > I was able to download a code w.r.t. base 64 encoding and
    > decoding . I am attaching the code below .
    >

    [Snip]
    > But this code gives different hex values as compared to the
    > python base64.encodestring and base64.decodestring respectively .
    >
    > I need some help on this matter . I need some pointers on
    > where is the mistake for the above file .
    >


    On a brief test your code seemed compatible with that bundled into the
    MIME support in a Java environment I had to hand. Perhaps the Python
    support is based on a different standard/RFC? You might wish to enquire
    somewhere where Python is on topic.
     
    Mark Bluemel, Jul 23, 2007
    #3
  4. pycraze

    Mark Bluemel Guest

    Roland Pibinger wrote:
    > On Mon, 23 Jul 2007 05:37:40 -0700, pycraze wrote:
    >
    >> But this code gives different hex values as compared to the
    >>python base64.encodestring and base64.decodestring respectively .
    >> I need some help on this matter . I need some pointers on
    >>where is the mistake for the above file .

    >
    >
    > It's usually easier to re-use existing code


    He did ... He said
    >> I was able to download a code w.r.t. base 64 encoding and
    >> decoding . I am attaching the code below .


    I've downloaded and built the same code (it was easily found via Google)
    and it seems to be compatible with the implementation in the Java
    environment I have lying around, so I think the issue may be with the
    way he's trying to use the Python implementation, but what do I know
    about that?
     
    Mark Bluemel, Jul 23, 2007
    #4
  5. pycraze

    pycraze Guest

    On Jul 23, 8:29 pm, (Roland Pibinger) wrote:
    > On Mon, 23 Jul 2007 05:37:40 -0700, pycraze wrote:
    > > But this code gives different hex values as compared to the
    > >python base64.encodestring and base64.decodestring respectively .
    > > I need some help on this matter . I need some pointers on
    > >where is the mistake for the above file .

    >
    > It's usually easier to re-use existing code than to implement (and
    > debug) something from scratch. Several Base64 implementations are
    > available, e.g.http://www.synesis.com.au/software/b64.html
    >
    > --
    > Roland Pibinger
    > "The best software is simple, elegant, and full of drama" - Grady Booch


    Hi Roland ,

    Thank you for your assistance . Python uses base64 encode and decode
    schemes specified in RFC 3548 . I am looking to implement this in C .
    In the link you have sent , the base64 library is according to RFC
    1113 .
     
    pycraze, Jul 23, 2007
    #5
  6. pycraze

    Mark Bluemel Guest

    Mark Bluemel wrote:
    > pycraze wrote:
    >
    >> Hi ,
    >>
    >> I am currently trying to implement base64 encoding and decoding
    >> scheme in C . Python has a module , base64 , that will do the
    >> encoding and decoding with ease . I am aware of OpenSSL having support
    >> for base64 encoding and decoding , but i will have to now implement
    >> both in C without using the openssl libraries .
    >>
    >> I was able to download a code w.r.t. base 64 encoding and
    >> decoding . I am attaching the code below .
    >>

    > [Snip]
    >
    >> But this code gives different hex values as compared to the
    >> python base64.encodestring and base64.decodestring respectively .
    >>
    >> I need some help on this matter . I need some pointers on
    >> where is the mistake for the above file .
    >>

    >
    > On a brief test your code seemed compatible with that bundled into the
    > MIME support in a Java environment I had to hand. Perhaps the Python
    > support is based on a different standard/RFC? You might wish to enquire
    > somewhere where Python is on topic.


    I also checked with Python to a limited extent and that also seems OK.
    What problems are you having?
     
    Mark Bluemel, Jul 23, 2007
    #6
  7. pycraze

    pycraze Guest

    On Jul 23, 8:46 pm, Mark Bluemel <> wrote:
    > Mark Bluemel wrote:
    > > pycraze wrote:

    >
    > >> Hi ,

    >
    > >> I am currently trying to implement base64 encoding and decoding
    > >> scheme in C . Python has a module , base64 , that will do the
    > >> encoding and decoding with ease . I am aware of OpenSSL having support
    > >> for base64 encoding and decoding , but i will have to now implement
    > >> both in C without using the openssl libraries .

    >
    > >> I was able to download a code w.r.t. base 64 encoding and
    > >> decoding . I am attaching the code below .

    >
    > > [Snip]

    >
    > >> But this code gives different hex values as compared to the
    > >> python base64.encodestring and base64.decodestring respectively .

    >
    > >> I need some help on this matter . I need some pointers on
    > >> where is the mistake for the above file .

    >
    > > On a brief test your code seemed compatible with that bundled into the
    > > MIME support in a Java environment I had to hand. Perhaps the Python
    > > support is based on a different standard/RFC? You might wish to enquire
    > > somewhere where Python is on topic.

    >
    > I also checked with Python to a limited extent and that also seems OK.
    > What problems are you having?


    Roland ,

    The python's base64 module gives me a different string as compared
    the above C code . I am currently using OpenSSL library for the base64
    encode and decode schemes . The OpenSSL gives similar output as
    compared to python base64 when i encode the same input string .

    I do not want to use OpenSSL library and would want to implement
    now in pure C . So i am stuck with only this code . I get to see
    numerous C codes that have implemented base64 encoding and decoding
    schemes . I am looking particularly for the RFC 3548 specification of
    base64 encoding and decoding .
     
    pycraze, Jul 23, 2007
    #7
  8. pycraze

    Mark Bluemel Guest

    pycraze wrote:

    > Thank you for your assistance . Python uses base64 encode and decode
    > schemes specified in RFC 3548 . I am looking to implement this in C .
    > In the link you have sent , the base64 library is according to RFC
    > 1113 .


    So is the code you downloaded...

    A little examination shows that 3548 and 1113 are very similar. See
    http://www.faqs.org/rfcs/rfc<number>.html

    The encoding alphabet is identical between the two. I have not read the
    specs in detail, but suspect the differences are related to things like
    line length, line endings, padding rules and the like. With a little
    study these small differences should be fairly easy to understand and
    adapt your code for.
     
    Mark Bluemel, Jul 23, 2007
    #8
  9. pycraze

    Mark Bluemel Guest

    pycraze wrote:
    > On Jul 23, 8:46 pm, Mark Bluemel <> wrote:
    >
    >>Mark Bluemel wrote:

    [Snip]
    > Roland ,


    I'm not Roland

    > The python's base64 module gives me a different string as compared
    > the above C code .


    How different?

    > I am currently using OpenSSL library for the base64
    > encode and decode schemes . The OpenSSL gives similar output as
    > compared to python base64 when i encode the same input string .
    >
    > I do not want to use OpenSSL library and would want to implement
    > now in pure C . So i am stuck with only this code . I get to see
    > numerous C codes that have implemented base64 encoding and decoding
    > schemes . I am looking particularly for the RFC 3548 specification of
    > base64 encoding and decoding .


    I've posted a link elsewhere in the thread, but simply doing a Google
    search for "RFC 3548" would have found it.
     
    Mark Bluemel, Jul 23, 2007
    #9
  10. On Mon, 23 Jul 2007 16:58:46 +0100, Mark Bluemel wrote:
    >A little examination shows that 3548 and 1113 are very similar. See
    >http://www.faqs.org/rfcs/rfc<number>.html


    So, assuming they are different, which is the right one? A format
    primarily created for exchange of data can hardly be used in different
    variants.


    --
    Roland Pibinger
    "The best software is simple, elegant, and full of drama" - Grady Booch
     
    Roland Pibinger, Jul 23, 2007
    #10
  11. (Roland Pibinger) writes:
    > On Mon, 23 Jul 2007 16:58:46 +0100, Mark Bluemel wrote:
    >>A little examination shows that 3548 and 1113 are very similar. See
    >>http://www.faqs.org/rfcs/rfc<number>.html

    >
    > So, assuming they are different, which is the right one? A format
    > primarily created for exchange of data can hardly be used in different
    > variants.


    That's a good question (assuming that they really are significantly
    different), but it's not a C question.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 23, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mm

    Help with base64 encoding

    mm, Feb 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,782
  2. pycraze

    Need Help with base64

    pycraze, Jul 23, 2007, in forum: Python
    Replies:
    3
    Views:
    364
    Tim Roberts
    Jul 24, 2007
  3. d-fan

    base64 decode help needed

    d-fan, May 21, 2008, in forum: C Programming
    Replies:
    3
    Views:
    582
    David Thompson
    Jun 4, 2008
  4. help me~!about base64

    , Dec 2, 2008, in forum: Python
    Replies:
    5
    Views:
    692
    Diez B. Roggisch
    Dec 2, 2008
  5. phildog
    Replies:
    1
    Views:
    156
    phildog
    Jun 28, 2007
Loading...

Share This Page