📄 cmemory.cpp
字号:
}
pszDes[j] = 0;
memcpy( (char *)pszSrc, (const char *)pszDes, j );
delete [] pszDes;
return j;
}
/* コード変換 JIS→SJIS */
void CMemory::JIStoSJIS( bool bMIMEdecode )
{
int i;
int j;
unsigned char* pszDes;
unsigned char* pszSrc = (unsigned char*)m_pData;
long nSrcLen = m_nDataLen;
BOOL bMIME = FALSE;
int nMEME_Selected;
long nWorkBgn;
long nWorkLen;
unsigned char* pszWork;
int nISOCode = CHAR_ASCII;
int nOldISOCode = nISOCode;
BOOL bFindESCSeq = FALSE;
int nESCSeqLen = - 1; // エスケープシーケンス長 - 1
pszDes = new unsigned char [nSrcLen + 1];
memset( pszDes, 0, nSrcLen + 1 );
j = 0;
if( false != bMIMEdecode ){
for( i = 0; i < nSrcLen; i++ ){
if( i <= nSrcLen - 16 && '=' == pszSrc[i] ){
if( 0 == _memicmp( "=?ISO-2022-JP?B?", &pszSrc[i], 16 ) ){
nMEME_Selected = MIME_BASE64;
bMIME = TRUE;
i += 15;
nWorkBgn = i + 1;
continue;
}
if( 0 == _memicmp( "=?ISO-2022-JP?Q?", &pszSrc[i], 16 ) ){
nMEME_Selected = MIME_QUOTED;
bMIME = TRUE;
i += 15;
nWorkBgn = i + 1;
continue;
}
}
if( bMIME == TRUE ){
if( i <= nSrcLen - 2 &&
0 == memcmp( "?=", &pszSrc[i], 2 ) ){
nWorkLen = i - nWorkBgn;
pszWork = new unsigned char [nWorkLen + 1];
// memset( pszWork, 0, nWorkLen + 1 );
memcpy( pszWork, &pszSrc[nWorkBgn], nWorkLen );
pszWork[nWorkLen] = '\0';
switch( nMEME_Selected ){
case MIME_BASE64:
// Base64デコード
nWorkLen = MemBASE64_Decode(pszWork, nWorkLen);
break;
case MIME_QUOTED:
// Quoted-Printableデコード
nWorkLen = QuotedPrintable_Decode( (char*)pszWork, nWorkLen );
break;
}
memcpy( &pszDes[j], pszWork, nWorkLen );
bMIME = FALSE;
j += nWorkLen;
++i;
delete [] pszWork;
continue;
}else{
continue;
}
}
pszDes[j] = pszSrc[i];
j++;
}
if( bMIME ){
nWorkBgn -= 16; // MIMEヘッダをそのままコピー
nWorkLen = i - nWorkBgn;
memcpy( &pszDes[j], &pszSrc[nWorkBgn], nWorkLen );
j += nWorkLen;
}
// 非ASCIIテキスト対応メッセージヘッダのMIMEコード
memcpy( (char *)pszSrc, (const char *)pszDes, j );
nSrcLen = j;
}
nWorkBgn = 0;
j = 0;
for( i = 0; i < nSrcLen; i++ ){
if( i <= nSrcLen - 3 &&
pszSrc[i + 0] == 0x1b &&
pszSrc[i + 1] == '$' &&
(pszSrc[i + 2] == 'B' || pszSrc[i + 2] == '@') ){
bFindESCSeq = TRUE;
nOldISOCode = nISOCode;
nISOCode = CHAR_ZENKAKU;
nESCSeqLen = 2;
}else
if( i <= nSrcLen - 3 &&
pszSrc[i + 0] == 0x1b &&
pszSrc[i + 1] == '(' &&
pszSrc[i + 2] == 'I' ){
bFindESCSeq = TRUE;
nOldISOCode = nISOCode;
nISOCode = CHAR_8BITCODE;
nESCSeqLen = 2;
}else
if( i <= nSrcLen - 3 &&
pszSrc[i + 0] == 0x1b &&
pszSrc[i + 1] == '(' &&
(pszSrc[i + 2] == 'B' || pszSrc[i + 2] == 'J') ){
bFindESCSeq = TRUE;
nOldISOCode = nISOCode;
nISOCode = CHAR_ASCII;
nESCSeqLen = 2;
}
// else{}
if( bFindESCSeq ){
if( 0 < i - nWorkBgn ){
if( CHAR_ZENKAKU == nOldISOCode ){
nWorkLen = i - nWorkBgn;
pszWork = new unsigned char [nWorkLen + 1];
memcpy( pszWork, &pszSrc[nWorkBgn], nWorkLen );
pszWork[nWorkLen] = '\0';
// JIS→SJIS変換
nWorkLen = MemJIStoSJIS( (unsigned char*)pszWork, nWorkLen );
memcpy( &pszDes[j], pszWork, nWorkLen );
j += nWorkLen;
delete [] pszWork;
}
}
i += nESCSeqLen;
nWorkBgn = i + 1;
bFindESCSeq = FALSE;
continue;
}else
if( CHAR_ASCII == nISOCode ){
pszDes[j] = pszSrc[i];
j++;
continue;
}else
if( CHAR_8BITCODE == nISOCode ){
pszDes[j] = (unsigned char)0x80 | pszSrc[i];
j++;
continue;
}
}
// ESCSeqがASCIIに戻らなかったときに,データを失わないように
if( CHAR_ZENKAKU == nISOCode ){
if( 0 < i - nWorkBgn ){
nWorkBgn -= nESCSeqLen + 1; // ESCSeqも残しておきたい
nWorkLen = i - nWorkBgn;
memcpy( &pszDes[j], &pszSrc[nWorkBgn], nWorkLen );
j += nWorkLen;
}
}
memcpy( pszSrc, pszDes, j );
m_nDataLen = j;
delete [] pszDes;
return;
}
/* 文字がBase64のデータか */
int CMemory::IsBASE64Char( char cData )
{
#if 0
static const char szBASE64CODE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int nBASE64CODE_Num = sizeof( szBASE64CODE ) / sizeof( szBASE64CODE[0] );
int k;
for( k = 0; k < nBASE64CODE_Num; ++k ){
if( cData == szBASE64CODE[k] ){
break;
}
}
if( k < nBASE64CODE_Num ){
return k;
}else{
return -1;
}
#else
static const int NA = -1;
static const int nBASE64Table[] = {
62, NA, NA, NA, 63, // +,-./
52, 53, 54, 55, 56, 57, 58, 59, // 01234567
60, 61, NA, NA, NA, NA, NA, NA, // 89:;<=>?
NA, 0, 1, 2, 3, 4, 5, 6, // @ABCDEFG
7, 8, 9, 10, 11, 12, 13, 14, // HIJKLMNO
15, 16, 17, 18, 19, 20, 21, 22, // PQRSTUVW
23, 24, 25, NA, NA, NA, NA, NA, // XYZ[\]^_
NA, 26, 27, 28, 29, 30, 31, 32, // `abcdefg
33, 34, 35, 36, 37, 38, 39, 40, // hijklmno
41, 42, 43, 44, 45, 46, 47, 48, // pqrstuvw
49, 50, 51 // xyz
};
// Oct. 10, 2000 genta
if( cData < '+' || 'z' < cData ){
return -1;
}
return nBASE64Table[cData - '+'];
#endif
}
// BASE64 => デコード後
// 4文字 => 3文字
// Base64デコード
long CMemory::MemBASE64_Decode( unsigned char* pszSrc, long nSrcLen )
{
int i, j, k;
long nSrcSize;
long lData;
unsigned char* pszDes;
long nDesLen;
long lDesSize;
int sMax;
//Srcの有効長の算出
for( i = 0; i < nSrcLen; i++ ){
if( pszSrc[i] == '=' ){
break;
}
}
nSrcSize = i;
nDesLen = (nSrcSize / 4) * 3;
nDesLen += ((nSrcSize % 4) * 6) / 8;
pszDes = new unsigned char[nDesLen + 1];
memset( pszDes, 0, nDesLen + 1 );
lDesSize = 0;
for( i = 0; i < nSrcSize; i++ ){
if( i < nSrcSize - (nSrcSize % 4) ){
sMax = 4;
}else{
sMax = (nSrcSize % 4);
}
lData = 0;
for( j = 0; j < sMax; j++ ){
k = IsBASE64Char( pszSrc[i + j] );
if( k >= 0 ){
lData |= (((long)k) << ((4 - j - 1) * 6));
}else{
}
}
for( j = 0; j < (sMax * 6)/ 8 ; j++ ){
pszDes[lDesSize] = (unsigned char)((lData >> (8 * (2 - j))) & 0x0000ff);
lDesSize++;
}
i+= 3;
}
pszDes[lDesSize] = 0;
memcpy( (char *)pszSrc, (const char *)pszDes, lDesSize );
delete [] pszDes;
return lDesSize;
}
/*
* Base64エンコード
*
* 符号化したデータは、新たに確保したメモリに格納されます
* 終了時に、そのメモリハンドルを指定されたアドレスに格納します
* 符号化されたデータ列は、一応NULL終端文字列になっています
*
*/
int CMemory::MemBASE64_Encode(
const char* pszSrc , // エンコード対象データ
int nSrcLen , // エンコード対象データ長
char** ppszDes , // 結果データ格納メモリポインタのアドレス
// HGLOBAL* phgDes , // 結果データのメモリハンドル格納場所
// long* pnDesLen , // 結果データのデータ長格納場所
int nWrap , // エンコード後のデータを自動的にCRLFで折り返す場合の1行最大文字数 (-1が指定された場合は折り返さない)
int bPadding // パディングするか
)
{
int i, j, k, m, n;
long nDesIdx;
// char* pszDes;
long nDataDes;
long nDataSrc;
long nLineLen;
char cw;
const char szBASE64CODE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int nBASE64CODE_Num = sizeof( szBASE64CODE ) / sizeof( szBASE64CODE[0] );
int nDesLen;
// 符号化後の長さを算出(ヘッダー?フッターを除く)
//=?ISO-2022-JP?B? GyRC QXc/ Lkgi JE4/ NiRq Siwk MRso Qg== ?=
// if( bPadding ){
nDesLen = ((nSrcLen / 3) + ((nSrcLen % 3)? 1:0)) * 4;
if( -1 != nWrap ){
nDesLen += 2 * ( nDesLen / nWrap + ((nDesLen % nWrap)? 1:0 ));
}
// }else{
// nDesLen = ?????????????????????????????????????????;
// }
// *phgDes = GlobalAlloc( GHND, nDesLen + 1 );
// pszDes = (char *)GlobalLock( *phgDes );
(*ppszDes) = new char[nDesLen + 1];
memset( (*ppszDes), 0, nDesLen + 1 );
nDesIdx = 0;
nLineLen = 0;
for( i = 0; i < nSrcLen; i += 3 ){
memcpy( (void *)&nDataDes, "====", 4 );
nDataSrc = 0;
if( nSrcLen - i < 3 ){
k = (nSrcLen % 3) * 8 / 6 + 1;
for( m = 0; m < nSrcLen % 3; m++ ){
((char*)&nDataSrc)[3 - m] = pszSrc[i + m];
}
}else{
k = 4;
for( m = 0; m < 3; m++ ){
((char*)&nDataSrc)[3 - m] = pszSrc[i + m];
}
}
for( j = 0; j < k; j++ ){
cw = (char)((nDataSrc >> (6 * (3 - j) + 8)) & 0x0000003f);
((char*)&nDataDes)[j] = szBASE64CODE[(int)cw];
}
if( bPadding ){ // パディングするか
k = 4;
}else{
nDesLen -= (4 - k);
}
for( n = 0; n < k; n++ ){
// 自動折り返しの処理
if( nWrap != -1 && nLineLen == nWrap ){
(*ppszDes)[nDesIdx + 0] = CR;
(*ppszDes)[nDesIdx + 1] = LF;
nDesIdx += 2;
nLineLen = 0;
}
(*ppszDes)[nDesIdx] = ((char*)&nDataDes)[n];
nDesIdx++;
nLineLen++;
}
}
// GlobalUnlock( *phgDes );
return nDesLen;
}
/* Base64デコード */
void CMemory::BASE64Decode( void )
{
int nBgn;
int nPos;
char* pszSrc = m_pData;
long nSrcLen = m_nDataLen;
CMemory cmemBuf;
nBgn = 0;
nPos = 0;
while( nBgn < nSrcLen ){
while( nBgn < nSrcLen && 0 > IsBASE64Char( pszSrc[nBgn] ) ){
nBgn++;
}
nPos = nBgn + 1;
while( nPos < nSrcLen && 0 <= IsBASE64Char( pszSrc[nPos] ) ){
nPos++;
}
if( nBgn < nSrcLen && nPos - nBgn > 0 ){
char* pData;
int nDesLen;
pData = new char[nPos - nBgn];
memcpy( pData, &pszSrc[nBgn], nPos - nBgn );
// Base64デコード
nDesLen = MemBASE64_Decode( (unsigned char *)pData, nPos - nBgn );
cmemBuf.Append( pData, nDesLen );
delete [] pData;
}
nBgn = nPos;
}
SetData( cmemBuf.GetPtr(), cmemBuf.m_nDataLen );
return;
}
/* Uudecode (デコード)*/
void CMemory::UUDECODE( char* pszFileName )
{
unsigned char* pszSrc = (unsigned char *)m_pData;
long nSrcLen = m_nDataLen;
CMemory cmemBuf;
int nBgn;
int nPos;
BOOL bBEGIN;
unsigned char uchDecode[64];
bBEGIN = FALSE;
nBgn = 0;
while( nBgn < nSrcLen ){
nPos = nBgn;
while( nPos < nSrcLen && pszSrc[nPos] != CR && pszSrc[nPos] != LF ){
nPos++;
}
if( nBgn < nSrcLen && nPos - nBgn > 0 ){
int nBufLen = nPos - nBgn;
unsigned char* pBuf = new unsigned char[ nBufLen + 1];
memset( pBuf, 0, nBufLen + 1 );
memcpy( pBuf, &pszSrc[nBgn], nBufLen );
if( !bBEGIN ){
if( 0 == memcmp( pBuf, "begin ", 6 ) ){
bBEGIN = TRUE;
char* pTok;
/* 最初のトークンを取得します。*/
pTok = strtok( (char*)pBuf, " " );
/* 次のトークンを取得します。*/
pTok = strtok( NULL, " " );
/* 更に次のトークンを取得します。*/
pTok = strtok( NULL, "\0" );
if( NULL != pszFileName ){
strcpy( pszFileName, pTok );
}
}
}else{
if( 0 == memcmp( pBuf, "end", 3 ) ){
break;
}else{
int nBytes;
nBytes = UUDECODE_CHAR( pBuf[0] );
if( 0 == nBytes ){
break;
}
int iByteIndex = 0;
int iCharIndex = 1;
while ( iByteIndex < nBytes ) {
// Note that nBytes may not be a multiple of 3, but the
// number of characters on the line should be a multiple of 4.
// If the number of encoded bytes is not a multiple of 3 then
// extra pad characters should have been added to the text
// by the encoder to make the character count a multiple of 4.
// if( pBuf[iCharIndex] == '\0' ||
// pBuf[iCharIndex + 1] == '\0' ||
// pBuf[iCharIndex + 2] == '\0' ||
// pBuf[iCharIndex + 3] == '\0') {
// //break;
// }
// Decode the line in 3-byte (=4-character) jumps.
if( iByteIndex < nBytes ){
uchDecode[iByteIndex] =
(unsigned char)((UUDECODE_CHAR(pBuf[iCharIndex]) << 2) |
(UUDECODE_CHAR(pBuf[iCharIndex + 1]) >> 4));
iByteIndex++;
}
if( iByteIndex < nBytes ){
uchDecode[iByteIndex] =
(unsigned char)((UUDECODE_CHAR(pBuf[iCharIndex + 1]) << 4) |
(UUDECODE_CHAR(pBuf[iCharIndex + 2]) >> 2));
iByteIndex++;
}
if( iByteIndex < nBytes ){
uchDecode[iByteIndex] =
(unsigned char)((UUDECODE_CHAR(pBuf[iCharIndex + 2]) << 6) |
UUDECODE_CHAR(pBuf[iCharIndex + 3]));
iByteIndex++;
}
iCharIndex += 4;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -