int ZipPlatform::WideToMultiByte(LPCWSTR lpszIn, CZipAutoBuffer &szOut, UINT uCodePage) { size_t wideLen = wcslen(lpszIn); if (wideLen == 0) { szOut.Release(); return 0; } // iLen does not include terminating character int iLen = WideCharToMultiByte(uCodePage, 0, lpszIn, (int)wideLen, szOut, 0, NULL, NULL); if (iLen > 0) { szOut.Allocate(iLen, true); iLen = WideCharToMultiByte(uCodePage, 0, lpszIn , (int)wideLen, szOut, iLen, NULL, NULL); ASSERT(iLen > 0); return iLen > 0 ? iLen : -1; } else // here it means error { szOut.Release(); return -1; } }
bool CZipExtraField::Read(CZipStorage *pStorage, WORD uSize) { if (uSize == 0) return true; Clear(); CZipAutoBuffer buffer; buffer.Allocate(uSize); pStorage->Read(buffer, uSize, true); char* position = (char*) buffer; do { CZipExtraData* pExtra = new CZipExtraData(); if (!pExtra->Read(position, uSize)) { delete pExtra; return false; } int totalSize = pExtra->GetTotalSize(); if (totalSize > uSize || totalSize < 0) return false; position += totalSize; uSize = (WORD)(uSize - totalSize); Add(pExtra); } while (uSize > 0); return true; }
int ZipPlatform::WideToSingle(LPCTSTR lpWide, CZipAutoBuffer &szSingle) { size_t wideLen = wcslen(lpWide); if (wideLen == 0) { szSingle.Release(); return 0; } // iLen does not include terminating character int iLen = WideCharToMultiByte(CP_ACP,0, lpWide, wideLen, szSingle, 0, NULL, NULL); if (iLen > 0) { szSingle.Allocate(iLen, true); iLen = WideCharToMultiByte(CP_ACP,0, lpWide , wideLen, szSingle, iLen, NULL, NULL); ASSERT(iLen != 0); } else // here it means error { szSingle.Release(); iLen --; } return iLen; }
ZIPINLINE void ZipPlatform::AnsiOem(CZipAutoBuffer& buffer, bool bAnsiToOem) { if (bAnsiToOem) CharToOemBuffA(buffer, buffer, buffer.GetSize()); else OemToCharBuffA(buffer, buffer, buffer.GetSize()); }
int ZipPlatform::SingleToWide(const CZipAutoBuffer &szSingle, CZipString& szWide, bool bUseAnsi) { int singleLen = szSingle.GetSize(); // iLen doesn't include terminating character UINT uCodePage; DWORD dwFlags; if (bUseAnsi) { uCodePage = CP_ACP; dwFlags = MB_PRECOMPOSED; } else { uCodePage = CP_UTF8; dwFlags = 0; } int iLen = MultiByteToWideChar(uCodePage, dwFlags, szSingle.GetBuffer(), singleLen, NULL, 0); if (iLen > 0) { iLen = MultiByteToWideChar(uCodePage, dwFlags, szSingle.GetBuffer(), singleLen, szWide.GetBuffer(iLen) , iLen); szWide.ReleaseBuffer(iLen); ASSERT(iLen != 0); } else { szWide.Empty(); iLen --; // return -1 } return iLen; }
int ZipPlatform::MultiByteToWide(const CZipAutoBuffer &szIn, CZipString& szOut, UINT uCodePage) { int singleLen = szIn.GetSize(); if (singleLen == 0) { szOut.Empty(); return 0; } // iLen doesn't include terminating character DWORD dwFlags = uCodePage <= CP_OEMCP ? MB_PRECOMPOSED : 0; int iLen = MultiByteToWideChar(uCodePage, dwFlags, szIn.GetBuffer(), singleLen, NULL, 0); if (iLen > 0) { iLen = MultiByteToWideChar(uCodePage, dwFlags, szIn.GetBuffer(), singleLen, szOut.GetBuffer(iLen) , iLen); szOut.ReleaseBuffer(iLen); ASSERT(iLen > 0); if (iLen == 0) return -1; #ifdef _ZIP_UNICODE_NORMALIZE // if there is a problem with compilation here, you may need uncomment the block defining WINVER = 0x0600 at the bottom of _features.h file if (IsNormalizedString(NormalizationC, szOut, iLen + 1) == TRUE) return iLen; int iNewLen = NormalizeString(NormalizationC, szOut, iLen, NULL, 0); if (iNewLen <= 0) { return iLen; } CZipString szNormalized; iNewLen = NormalizeString(NormalizationC, szOut, iLen, szNormalized.GetBuffer(iNewLen), iNewLen); if (iNewLen <= 0) { szNormalized.ReleaseBuffer(0); return iLen; } szNormalized.ReleaseBuffer(iNewLen); szOut = szNormalized; return iNewLen; #else return iLen; #endif } else { szOut.Empty(); return -1; } }
int ZipPlatform::SingleToWide(const CZipAutoBuffer &szSingle, CZipString& szWide) { int singleLen = szSingle.GetSize(); // iLen doesn't include terminating character int iLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szSingle.GetBuffer(), singleLen, NULL, 0); if (iLen > 0) { iLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szSingle.GetBuffer(), singleLen, szWide.GetBuffer(iLen) , iLen); szWide.ReleaseBuffer(iLen); ASSERT(iLen != 0); } else { szWide.Empty(); iLen --; // return -1 } return iLen; }
void ZipCompatibility::ConvertStringToBuffer(LPCTSTR lpszString, CZipAutoBuffer& buffer, UINT uCodePage) { #ifdef _UNICODE ZipPlatform::WideToMultiByte(lpszString, buffer, uCodePage); #else int iLen = (int)strlen(lpszString); // iLen does not include the NULL character buffer.Allocate(iLen); memcpy(buffer, lpszString, (size_t)iLen); if (uCodePage == CP_OEMCP) ZipPlatform::AnsiOem(buffer, true); #endif }
void ZipCompatibility::ConvertBufferToString(CZipString& szString, const CZipAutoBuffer& buffer, UINT uCodePage) { #ifdef _UNICODE ZipPlatform::MultiByteToWide(buffer, szString, uCodePage); #else // iLen does not include the NULL character int iLen; if (uCodePage == CP_OEMCP) { CZipAutoBuffer buf; buf = buffer; ZipPlatform::AnsiOem(buf, false); iLen = buf.GetSize(); memcpy(szString.GetBuffer(iLen), buf.GetBuffer(), iLen); } else { iLen = buffer.GetSize(); memcpy(szString.GetBuffer(iLen), buffer.GetBuffer(), iLen); } szString.ReleaseBuffer(iLen); #endif }
void ZipCompatibility::SlashBackslashChg(CZipAutoBuffer& buffer, bool bReplaceSlash) { char t1 = '\\' /*backslash*/, t2 = '/', c1, c2; if (bReplaceSlash) { c1 = t1; c2 = t2; } else { c1 = t2; c2 = t1; } for (DWORD i = 0; i < buffer.GetSize(); i++) { if (buffer[i] == c2) buffer[i] = c1; } }
ZIPINLINE void ZipPlatform::AnsiOem(CZipAutoBuffer& buffer, bool bAnsiToOem) { #ifdef _ZIP_SAFE_WINDOWS_API UINT cpIn, cpOut; if (bAnsiToOem) { cpIn = CP_ACP; cpOut = CP_OEMCP; } else { cpIn = CP_OEMCP; cpOut = CP_ACP; } CZipAutoBuffer interBuffer; int size = buffer.GetSize(); // iLen doesn't include terminating character int iLen = MultiByteToWideChar(cpIn, MB_PRECOMPOSED, buffer, size, NULL, 0); if (iLen <= 0) return; interBuffer.Allocate(iLen * sizeof(wchar_t)); LPWSTR lpszWide = (LPWSTR)(char*)interBuffer; iLen = MultiByteToWideChar(cpIn, MB_PRECOMPOSED, buffer, size, lpszWide, iLen); ASSERT(iLen != 0); // iLen does not include terminating character size = WideCharToMultiByte(cpOut, 0, lpszWide, iLen, NULL, 0, NULL, NULL); if (size <= 0) return; buffer.Allocate(size); size = WideCharToMultiByte(cpOut, 0, lpszWide, iLen, buffer, size, NULL, NULL); ASSERT(size != 0); #else if (bAnsiToOem) CharToOemBuffA(buffer, buffer, buffer.GetSize()); else OemToCharBuffA(buffer, buffer, buffer.GetSize()); #endif }