Need Help with base64

Tim Roberts timr at probo.com
Tue Jul 24 08:47:23 CEST 2007


pycraze <dennis.varghese at wipro.com> wrote:
>
>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);

Those last 7 lines must have been leftover; pcstring is not used in this
routine.

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

"cout" is an array of 3 characters.  Where do you think the memcpy is going
to find 160 bytes?  I suspect you intended to have this loop inside a
SECOND loop, copying 3 characters at a time into *ppcdest.
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list