천객만래 [千客萬來] (It has an interminable succession of visitors)

[개발/VC++] URLEncode, URLDecode, 유니코드 변환 소스



[개발/VC++] URLEncode, URLDecode, UTF8 변환 소스


[유니코드 문자집합용]


CString Unicode_URLDecode( CString strEncodedText )
{
 CString strResult;
 wchar_t ch0, ch1, ch2;
 wchar_t wch;
 TCHAR tch;
 int i = 0;


 while( i<strEncodedText.GetLength() )
 {
  tch = strEncodedText.GetAt(i);
  
  if( tch != _T('%') )
  {
   // a character not encoded
   strResult += tch;
   i++;
  }
  else
  {
   // a character encoded !!
   ch0 = _tcstol( strEncodedText.Mid( i+1, 2 ), NULL, 16 );
   i += 3;


   if( ch0 < 0x80 )
    // 1 byte for UTF-8
    // 0xxx xxxx
    wch = ch0;
   else
   {
    if( strEncodedText.GetAt(i)!= _T('%') ) // Error!
     continue;


    ch1 = _tcstol( strEncodedText.Mid( i+1, 2 ), NULL, 16 );
    i += 3;


    if( ch0 < 0xe0 )
    {
     // 2 byte for UTF-8
     // 110x xxxx 10xx xxxx
     wch = ((ch0&0x1f)<<6)
      | (ch1&0x3f);
    }
    else
    {
     // 3 byte for UTF-8
     if( strEncodedText.GetAt(i)!= _T('%') ) // Error!
      continue;


     ch2 = _tcstol( strEncodedText.Mid( i+1, 2 ), NULL, 16 );
     i += 3;


     // 1110 xxxx 10xx xxxx 10xx xxxx
     wch = ((ch0&0x0f)<<12)
      | ((ch1&0x3f)<<6)
      | (ch2&0x3f);
    }
   }
   strResult += wch;
  }
 }


 return strResult;
}

 

-------------------------------------------------------------------------------------------
[멀티바이트 문자집합용]


inline BYTE CURLEncode::toHex(const BYTE &x) {
 return x > 9 ? x + 55: x + 48;
}


inline BYTE CURLEncode::toByte(const BYTE &x) {
 return x > 57? x - 55: x - 48;
}

CString CURLEncode::URLDecode(CString sIn)
{
    CString sOut;
    const int nLen = sIn.GetLength() + 1;
    register LPBYTE pOutTmp = NULL;
    LPBYTE pOutBuf = NULL;
    register LPBYTE pInTmp = NULL;
    LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
    //alloc out buffer
    pOutBuf = (LPBYTE)sOut.GetBuffer(nLen);
    
    if(pOutBuf)
    {
        pInTmp   = pInBuf;
        pOutTmp = pOutBuf;
        // do encoding
        while (*pInTmp)
        {
            if('%'==*pInTmp)
            {
                pInTmp++;
                *pOutTmp++ = (toByte(*pInTmp)%16<<4) + toByte(*(pInTmp+1))%16;
                pInTmp++;
            }
            else if('+'==*pInTmp)
                *pOutTmp++ = ' ';
            else
                *pOutTmp++ = *pInTmp;
            pInTmp++;
        }
        *pOutTmp = '\0';
        sOut.ReleaseBuffer();
    }
    sIn.ReleaseBuffer();
    
    return sOut;
}


CString CURLEncode::URLEncode(CString sIn)
{
    CString sOut;
    const int nLen = sIn.GetLength() + 1;
    register LPBYTE pOutTmp = NULL;
    LPBYTE pOutBuf = NULL;
    register LPBYTE pInTmp = NULL;
    LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
    //alloc out buffer
    pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3);
    
    if(pOutBuf)
    {
        pInTmp   = pInBuf;
        pOutTmp = pOutBuf;
        // do encoding
        while (*pInTmp)
        {
            if(isalnum(*pInTmp) || '-'==*pInTmp || '_'==*pInTmp || '.'==*pInTmp)
                *pOutTmp++ = *pInTmp;
            else if(isspace(*pInTmp))
                *pOutTmp++ = '+';
            else
            {
                *pOutTmp++ = '%';
                *pOutTmp++ = toHex(*pInTmp>>4);
                *pOutTmp++ = toHex(*pInTmp%16);
            }
            pInTmp++;
        }
        *pOutTmp = '\0';
        sOut.ReleaseBuffer();
    }
    sIn.ReleaseBuffer();
    
    return sOut;
}


 





Posted by SB패밀리

[개발/VC++] URLEncode, URLDecode, UTF8 변환 소스


[유니코드 문자집합용]


CString Unicode_URLDecode( CString strEncodedText )
{
 CString strResult;
 wchar_t ch0, ch1, ch2;
 wchar_t wch;
 TCHAR tch;
 int i = 0;


 while( i<strEncodedText.GetLength() )
 {
  tch = strEncodedText.GetAt(i);
  
  if( tch != _T('%') )
  {
   // a character not encoded
   strResult += tch;
   i++;
  }
  else
  {
   // a character encoded !!
   ch0 = _tcstol( strEncodedText.Mid( i+1, 2 ), NULL, 16 );
   i += 3;


   if( ch0 < 0x80 )
    // 1 byte for UTF-8
    // 0xxx xxxx
    wch = ch0;
   else
   {
    if( strEncodedText.GetAt(i)!= _T('%') ) // Error!
     continue;


    ch1 = _tcstol( strEncodedText.Mid( i+1, 2 ), NULL, 16 );
    i += 3;


    if( ch0 < 0xe0 )
    {
     // 2 byte for UTF-8
     // 110x xxxx 10xx xxxx
     wch = ((ch0&0x1f)<<6)
      | (ch1&0x3f);
    }
    else
    {
     // 3 byte for UTF-8
     if( strEncodedText.GetAt(i)!= _T('%') ) // Error!
      continue;


     ch2 = _tcstol( strEncodedText.Mid( i+1, 2 ), NULL, 16 );
     i += 3;


     // 1110 xxxx 10xx xxxx 10xx xxxx
     wch = ((ch0&0x0f)<<12)
      | ((ch1&0x3f)<<6)
      | (ch2&0x3f);
    }
   }
   strResult += wch;
  }
 }


 return strResult;
}

 

-------------------------------------------------------------------------------------------
[멀티바이트 문자집합용]


inline BYTE CURLEncode::toHex(const BYTE &x) {
 return x > 9 ? x + 55: x + 48;
}


inline BYTE CURLEncode::toByte(const BYTE &x) {
 return x > 57? x - 55: x - 48;
}

CString CURLEncode::URLDecode(CString sIn)
{
    CString sOut;
    const int nLen = sIn.GetLength() + 1;
    register LPBYTE pOutTmp = NULL;
    LPBYTE pOutBuf = NULL;
    register LPBYTE pInTmp = NULL;
    LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
    //alloc out buffer
    pOutBuf = (LPBYTE)sOut.GetBuffer(nLen);
   
    if(pOutBuf)
    {
        pInTmp   = pInBuf;
        pOutTmp = pOutBuf;
        // do encoding
        while (*pInTmp)
        {
            if('%'==*pInTmp)
            {
                pInTmp++;
                *pOutTmp++ = (toByte(*pInTmp)%16<<4) + toByte(*(pInTmp+1))%16;
                pInTmp++;
            }
            else if('+'==*pInTmp)
                *pOutTmp++ = ' ';
            else
                *pOutTmp++ = *pInTmp;
            pInTmp++;
        }
        *pOutTmp = '\0';
        sOut.ReleaseBuffer();
    }
    sIn.ReleaseBuffer();
   
    return sOut;
}


CString CURLEncode::URLEncode(CString sIn)
{
    CString sOut;
    const int nLen = sIn.GetLength() + 1;
    register LPBYTE pOutTmp = NULL;
    LPBYTE pOutBuf = NULL;
    register LPBYTE pInTmp = NULL;
    LPBYTE pInBuf =(LPBYTE)sIn.GetBuffer(nLen);
    //alloc out buffer
    pOutBuf = (LPBYTE)sOut.GetBuffer(nLen*3);
   
    if(pOutBuf)
    {
        pInTmp   = pInBuf;
        pOutTmp = pOutBuf;
        // do encoding
        while (*pInTmp)
        {
            if(isalnum(*pInTmp) || '-'==*pInTmp || '_'==*pInTmp || '.'==*pInTmp)
                *pOutTmp++ = *pInTmp;
            else if(isspace(*pInTmp))
                *pOutTmp++ = '+';
            else
            {
                *pOutTmp++ = '%';
                *pOutTmp++ = toHex(*pInTmp>>4);
                *pOutTmp++ = toHex(*pInTmp%16);
            }
            pInTmp++;
        }
        *pOutTmp = '\0';
        sOut.ReleaseBuffer();
    }
    sIn.ReleaseBuffer();
   
    return sOut;
}


 

Posted by SB패밀리