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
[11725] Re:Base64 디코딩
방태윤 [nabty] 1283 읽음    2001-10-11 14:33
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;
}




매댐덜 님이 쓰신 글 :
: VC++ 밖엔 없는데...제 실력으론 포팅이 안되네요...
:
: 소스 올려 드리면 포팅을 좀 해주실수 있는지..흑흑 급한건데..여기서 막히네요...
:
:
: void CEmailChecker::Base64Decoding(CString &strSource)
: {
:     LPSTR    szDecode;
:     char    szTemp[10];
:     CString    strDecode = "", strTemp;
:     int        i, j, nSize, nPadding, nSum, test;
:     int        *pData;
:
:     m_strDecoded = "";
:     strSource.Remove('=');
:     szDecode = (LPSTR)(LPCTSTR)strSource;
:     nSize = strSource.GetLength();
:
:     pData = new int[nSize];
:
:     // 문자열에서 6비트씩 추출
:     for(i = 0 ; i < nSize ; i++)
:     {
:         if(szDecode[i] >= 'A' && szDecode[i] <= 'Z')
:             pData[i] = szDecode[i] - 'A';
:         else if(szDecode[i] >= 'a' && szDecode[i] <= 'z')
:             pData[i] = szDecode[i] - 'a' + 26;
:         else if(szDecode[i] >= '0' && szDecode[i] <= '9')
:             pData[i] = szDecode[i] - '0' + 52;
:         else if(szDecode[i] == '+')
:             pData[i] = 62;
:         else if(szDecode[i] == '/')
:             pData[i] = 63;
:
:         // 이 부분이 6비트로 나와야 함...
:         itoa(pData[i], szTemp, 2);
:         nPadding = 6 - strlen(szTemp);
:         for(j = 0 ; j < nPadding ; j++)
:             strDecode += '0';
:
:         strDecode += szTemp;
:     }
:
:     // 6비트씩으로 맞추면서 들어간 '0'패딩부분을 없애줌
:     nSize = strDecode.GetLength();
:     nPadding = nSize % 8;
:     if(nPadding > 0)
:     {
:         strTemp = strDecode.Left(nSize-nPadding);
:         nSize = strTemp.GetLength();
:     }
:     else
:         strTemp = strDecode;
:
:     // 다시 8비트씩으로 묶음
:     nSize /= 8;
:
:     for(j = 0 ; j < nSize ; j++)
:     {
:         strDecode = strTemp.Left(8);
:         szDecode = (LPSTR)(LPCTSTR)strDecode;
:
:         for(nSum = 0, i = 0 ; i < 8 ; i++)
:         {
:             if(szDecode[i] == '0')
:                 continue;
:             nSum += (int)pow(2, 7-i);
:         }
:         m_strDecoded += (char)nSum;
:         test = strTemp.GetLength();
:         strTemp = strTemp.Right(test-8);
:     }
:
:     delete [] pData;
: }
:
: /*******************************************************************************
:         KS C 5601 에서 Quoted-printable로 인코딩된 것을 디코딩하는 함수
: *******************************************************************************/
: void CEmailChecker::QPDecoding(CString &strSource)
: {
:     LPSTR    szDecode;
:     CString    strDecode = "", strTemp;
:     int        i, nSize, nSum, nCount = 0, nValue;
:
:     m_strDecoded = "";
:     strSource.Remove('=');
:     szDecode = (LPSTR)(LPCTSTR)strSource;
:     nSize = strSource.GetLength();
:
:     for(nSum = 0, i = 0 ; i < nSize ; i++)
:     {
:         if(szDecode[i] >= '0' && szDecode[i] <= '9')
:             nValue = (int)(szDecode[i] - '0');
:         else if(szDecode[i] >= 'A' && szDecode[i] <= 'F')
:             nValue = (int)(szDecode[i] - 'A') + 10;
:         else
:         {
:             m_strDecoded += szDecode[i];
:             continue;
:         }
:         if(nCount == 0)
:         {
:             nSum += nValue * 16;
:             nCount++;
:             continue;
:         }
:         else if(nCount == 1)
:         {
:             nSum += nValue;
:             nCount = 0;
:             m_strDecoded += (char)nSum;
:             nSum = 0;
:         }
:     }
: }
:

+ -

관련 글 리스트
11724 Base64 및 QP 디코딩 하는 알고리즘이나 소스 있으신분 점 알려주세요... 매댐덜 1256 2001/10/11
11725     Re:Base64 디코딩 방태윤 1283 2001/10/11
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.