C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[18443] [참고]base64 디코딩.
유부남 [] 1530 읽음    2002-05-13 23:15
base64 디코딩이 필요하여 Q&A에서 방태윤님의 자료를 찾아 사용해보니 약간의 문제(가끔 뒤에 쓰레기가 붙는 문제)가 있어 조금 수정해보았습니다.

const char Base64Table[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int Base64Decod(const char *buftodec,int bufsize,char *decbuf)
{
    int i=0;
    unsigned char binbyte[4];
    int cpos[5];
    unsigned char *buftemp;

    //Allocate space for the temporary string
    buftemp=(unsigned char *)malloc(bufsize+4);
    strcpy(buftemp,buftodec);
    if (fmod(bufsize,4)==1){
        buftemp[bufsize]='\0';
        buftemp[bufsize+1]='\0';
        buftemp[bufsize+2]='\0';
    }
    if (fmod(bufsize,4)==2){
        buftemp[bufsize]='\0';
        buftemp[bufsize+1]='\0';
    }
    if (fmod(bufsize,4)==3)buftemp[bufsize]='\0';
    while (i<bufsize){
        cpos[0]=strchr(Base64Table,buftemp[i+0])-Base64Table;
        cpos[1]=strchr(Base64Table,buftemp[i+1])-Base64Table;

        // 수정한 부분 시작 .이부분에 오류가 있어 가끔 쓰레기가 붙더군요.//
        if(strchr(Base64Table,buftemp[i+2])==NULL) cpos[2]=0;
        else cpos[2]=strchr(Base64Table,buftemp[i+2])-Base64Table;
        if(strchr(Base64Table,buftemp[i+3])==NULL) cpos[3]=0;
        else cpos[3]=strchr(Base64Table,buftemp[i+3])-Base64Table;
        // 수정한 부분 끝. //

        binbyte[0]=((cpos[0]<<2)|(cpos[1]>>4));
        binbyte[1]=((cpos[1]<<4)|(cpos[2]>>2));
        binbyte[2]=(((cpos[2]&0x03)<<6)|(cpos[3]&0x3f));
        decbuf[i-(i/4)]=binbyte[0];
        decbuf[i-(i/4)+1]=binbyte[1];
        decbuf[i-(i/4)+2]=binbyte[2];
        i+=4;
    }
    free(buftemp);
    if(fmod(bufsize,4)==0)return bufsize*6/8;
    if(fmod(bufsize,4)==1)return((bufsize+3)*6/8)-3;
    if(fmod(bufsize,4)==2)return((bufsize+2)*6/8)-2;
    if(fmod(bufsize,4)==3)return((bufsize+1)*6/8)-1;
    return -1;
}

방태윤 님이 쓰신 글 :
: http://www.delphi.co.kr/cgi-bin/cwb/CrazyWWWBoard.cgi?mode=read&num=47&db=cbtip&backdepth=1
: Black}{ole 이라는 분이 올린것입니다.
:
: Base64 인코딩/디코딩
:
: //Base64Table contains the 64 printable characters
: const char Base64Table[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
: int  Base64Encod(const char *buftoenc,int bufsize,char *encbuf)
: {
:   int i=0;
:   int b64byte[5];
:   unsigned char *buftemp;
:
:   //Allocate space for the temporary string
:   buftemp=(unsigned char *)malloc(bufsize+2);
:   strcpy(buftemp,buftoenc);
:   if (fmod(bufsize,3)==1)
:   {
:      buftemp[bufsize]='\0';
:      buftemp[bufsize+1]='\0';
:   }
:   if (fmod(bufsize,3)==2)buftemp[bufsize]='\0';
:   while (i<bufsize)
:   {
:      b64byte[0]=buftemp[i]>>2;
:      b64byte[1]=((buftemp[i]&3)<<4)|(buftemp[i+1]>>4);
:      b64byte[2]=((buftemp[i+1]&0x0F)<<2)|(buftemp[i+2]>>6);
:      b64byte[3]=buftemp[i+2]&0x3F;
:      encbuf[i+(i/3)]=Base64Table[b64byte[0]];
:      encbuf[i+(i/3)+1]=Base64Table[b64byte[1]];
:      encbuf[i+(i/3)+2]=Base64Table[b64byte[2]];
:      encbuf[i+(i/3)+3]=Base64Table[b64byte[3]];
:      i+=3;
:   }
:   free(buftemp);
:   if (fmod(bufsize,3)==0)return bufsize*8/6;
:   if (fmod(bufsize,3)==1)return((bufsize+2)*8/6)-2;
:   if (fmod(bufsize,3)==2)return((bufsize+1)*8/6)-1;
:   return -1;
: }
:
:
:
:
: Base64 Decoding Décodage Base64
:
:


: Decodes the string buftodec in base64 format. You have to pass to the function the size of the string and a pointer to the string where to put the decoded one. This function returns the number of characters contained in the decoded string.
: Cette fonction décode un string codé en base64. Passez à la fonction le string à décoder et sa taille ainsi qu'un pointeur vers le string destiné à contenir le string décodé. La fonction retourne le nombre de caractères contenu dans le string décodé.
:
: //Base64Table contains the 64 printable characters
: const char Base64Table[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
: int Base64Decod(const char *buftodec,int bufsize,char *decbuf)
: {
:   int i=0;
:   unsigned char binbyte[4];
:   int cpos[5];
:   unsigned char *buftemp;
:
:
:
:   //Allocate space for the temporary string
:   buftemp=(unsigned char *)malloc(bufsize);
:   strcpy(buftemp,buftodec);
:   if (fmod(bufsize,4)==1)
:   {
:      buftemp[bufsize]='\0';
:      buftemp[bufsize+1]='\0';
:      buftemp[bufsize+2]='\0';
:   }
:   if (fmod(bufsize,4)==2)
:   {
:      buftemp[bufsize]='\0';
:      buftemp[bufsize+1]='\0';
:   }
:   if (fmod(bufsize,4)==3)buftemp[bufsize]='\0';
:   while (i<bufsize)
:   {
:      cpos[0]=strchr(Base64Table,buftemp[i])-Base64Table;
:      cpos[1]=strchr(Base64Table,buftemp[i+1])-Base64Table;
:      cpos[2]=strchr(Base64Table,buftemp[i+2])-Base64Table;
:      cpos[3]=strchr(Base64Table,buftemp[i+3])-Base64Table;
:      binbyte[0]=((cpos[0]<<2)|(cpos[1]>>4));
:      binbyte[1]=((cpos[1]<<4)|(cpos[2]>>2));
:      binbyte[2]=(((cpos[2]&0x03)<<6)|(cpos[3]&0x3f));
:      decbuf[i-(i/4)]=binbyte[0];
:      decbuf[i-(i/4)+1]=binbyte[1];
:      decbuf[i-(i/4)+2]=binbyte[2];
:      i+=4;
:   }
:   free(buftemp);
:   if (fmod(bufsize,4)==0)return bufsize*6/8;
:   if (fmod(bufsize,4)==1)return((bufsize+3)*6/8)-3;
:   if (fmod(bufsize,4)==2)return((bufsize+2)*6/8)-2;
:   if (fmod(bufsize,4)==3)return((bufsize+1)*6/8)-1;
:   return -1;
: }
:

+ -

관련 글 리스트
18443 [참고]base64 디코딩. 유부남 1530 2002/05/13
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.