Files
harbour-core/harbour/contrib/hbziparch/ZipCompatibility.cpp
Viktor Szakats 40338a87d6 2008-06-24 00:35 UTC+0200 Viktor Szakats (harbour.01 syenar hu)
* contrib/hbziparch/_features.h
   * contrib/hbziparch/_platform.h
   * contrib/hbziparch/Aes.cpp
   * contrib/hbziparch/Aes.h
   * contrib/hbziparch/BaseLibCompressor.cpp
   * contrib/hbziparch/BaseLibCompressor.h
   * contrib/hbziparch/BytesWriter.h
   * contrib/hbziparch/Bzip2Compressor.cpp
   * contrib/hbziparch/Bzip2Compressor.h
   * contrib/hbziparch/DeflateCompressor.cpp
   * contrib/hbziparch/DeflateCompressor.h
   * contrib/hbziparch/DirEnumerator.cpp
   * contrib/hbziparch/DirEnumerator.h
   * contrib/hbziparch/FileFilter.cpp
   * contrib/hbziparch/FileFilter.h
   * contrib/hbziparch/FileInfo.h
   * contrib/hbziparch/Hmac.cpp
   * contrib/hbziparch/Hmac.h
   * contrib/hbziparch/RandomPool.cpp
   * contrib/hbziparch/RandomPool.h
   * contrib/hbziparch/Sha1.cpp
   * contrib/hbziparch/Sha1.h
   * contrib/hbziparch/std_mfc.h
   * contrib/hbziparch/std_stl.h
   * contrib/hbziparch/Wildcard.cpp
   * contrib/hbziparch/Wildcard.h
   * contrib/hbziparch/ZipAbstractFile.h
   * contrib/hbziparch/ZipAesCryptograph.cpp
   * contrib/hbziparch/ZipAesCryptograph.h
   * contrib/hbziparch/ZipArchive.cpp
   * contrib/hbziparch/ZipArchive.h
   * contrib/hbziparch/ZipAutoBuffer.cpp
   * contrib/hbziparch/ZipAutoBuffer.h
   * contrib/hbziparch/ZipBaseException.h
   * contrib/hbziparch/ZipCallback.h
   * contrib/hbziparch/ZipCallbackProvider.h
   * contrib/hbziparch/ZipCentralDir.cpp
   * contrib/hbziparch/ZipCentralDir.h
   * contrib/hbziparch/ZipCollections.h
   * contrib/hbziparch/ZipCollections_mfc.h
   * contrib/hbziparch/ZipCollections_stl.h
   * contrib/hbziparch/ZipCompatibility.cpp
   * contrib/hbziparch/ZipCompatibility.h
   * contrib/hbziparch/ZipCompressor.cpp
   * contrib/hbziparch/ZipCompressor.h
   * contrib/hbziparch/ZipCrc32Cryptograph.cpp
   * contrib/hbziparch/ZipCrc32Cryptograph.h
   * contrib/hbziparch/ZipCryptograph.cpp
   * contrib/hbziparch/ZipCryptograph.h
   * contrib/hbziparch/ZipException.cpp
   * contrib/hbziparch/ZipException.h
   * contrib/hbziparch/ZipExtraData.cpp
   * contrib/hbziparch/ZipExtraData.h
   * contrib/hbziparch/ZipExtraField.cpp
   * contrib/hbziparch/ZipExtraField.h
   * contrib/hbziparch/ZipFile.h
   * contrib/hbziparch/ZipFile_mfc.cpp
   * contrib/hbziparch/ZipFile_mfc.h
   * contrib/hbziparch/ZipFile_stl.cpp
   * contrib/hbziparch/ZipFile_stl.h
   * contrib/hbziparch/ZipFileHeader.cpp
   * contrib/hbziparch/ZipFileHeader.h
   * contrib/hbziparch/ZipFileMapping.h
   * contrib/hbziparch/ZipFileMapping_lnx.h
   * contrib/hbziparch/ZipFileMapping_win.h
   * contrib/hbziparch/ZipMemFile.cpp
   * contrib/hbziparch/ZipMemFile.h
   * contrib/hbziparch/ZipMutex.h
   * contrib/hbziparch/ZipMutex_lnx.h
   * contrib/hbziparch/ZipMutex_win.h
   * contrib/hbziparch/ZipPathComponent.h
   * contrib/hbziparch/ZipPlatform.h
   * contrib/hbziparch/ZipPlatformComm.cpp
   * contrib/hbziparch/ZipStorage.cpp
   * contrib/hbziparch/ZipStorage.h
   * contrib/hbziparch/ZipString.h
   * contrib/hbziparch/ZipString_mfc.h
   * contrib/hbziparch/ZipString_stl.h
   * contrib/hbziparch/ZipStringStoreSettings.h
     + ZipArchive lib update finished.
     ; Pass 2/2
     ; Please test.
2008-06-23 22:41:33 +00:00

220 lines
5.3 KiB
C++

////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2007 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ZipCompatibility.h"
#include "ZipPlatform.h"
#include "ZipException.h"
#include "ZipAutoBuffer.h"
#include "ZipFileHeader.h"
enum iInternalAttr
{
attROnly = 0x01,
attHidd = 0x02,
attSys = 0x04,
attDir = 0x10,
attArch = 0x20
};
// *********************** WINDOWS **************************
#ifndef _WIN32
#define FILE_ATTRIBUTE_READONLY 0x00000001
#define FILE_ATTRIBUTE_HIDDEN 0x00000002
#define FILE_ATTRIBUTE_SYSTEM 0x00000004
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
#define FILE_ATTRIBUTE_ARCHIVE 0x00000020
#endif
// *********************** UINX **************************
#define USER_PERMISSIONS_MASK 0x01C0
#define EXTRACT_USER_PERMISSIONS(x) ((x & USER_PERMISSIONS_MASK) >> 6)
#define CREATE_USER_PERMISSIONS(x) ((x & 0x0007) << 6)
#define GROUP_PERMISSIONS_MASK 0x0038
#define CREATE_GROUP_PERMISSIONS(x) ((x & 0x0007) << 3)
#define OTHER_PERMISSIONS_MASK 0x0007
#define CREATE_OTHER_PERMISSIONS(x) (x & 0x0007)
#define UNIX_DIRECTORY_ATTRIBUTE 0x4000
#define UNIX_FILE_ATTRIBUTE 0x8000
#define UNIX_EXEC 1
#define UNIX_WRITE 2
#define UNIX_READ 4
using namespace ZipCompatibility;
typedef DWORD(*conv_func)(DWORD , bool );
DWORD AttrDos(DWORD , bool );
DWORD AttrUnix(DWORD, bool);
DWORD AttrMac(DWORD , bool );
conv_func conv_funcs[11] = {AttrDos,
NULL,
NULL,
AttrUnix,
NULL,
NULL,
AttrDos,
AttrMac,
NULL,
NULL,
AttrDos
};
DWORD ZipCompatibility::ConvertToSystem(DWORD uAttr, int iFromSystem, int iToSystem)
{
if (iToSystem != iFromSystem && iFromSystem < 11 && iToSystem < 11)
{
conv_func p = conv_funcs[iFromSystem], q = conv_funcs[iToSystem];
if (p && q)
uAttr = q( p(uAttr, true), false);
else
CZipException::Throw(CZipException::platfNotSupp);
}
return uAttr;
}
DWORD AttrDos(DWORD uAttr, bool )
{
return uAttr;
}
DWORD AttrUnix(DWORD uAttr, bool bFrom)
{
DWORD uNewAttr = 0;
if (bFrom)
{
bool isDir = (uAttr & UNIX_DIRECTORY_ATTRIBUTE) != 0;
if (isDir)
uNewAttr = attDir;
uAttr = EXTRACT_USER_PERMISSIONS (uAttr);
// we may set archive attribute if the file hasn't got the execute permissions
// and is not a directory
if (!isDir && !(uAttr & UNIX_EXEC))
uNewAttr |= attArch ;
if (!(uAttr & UNIX_WRITE))
uNewAttr |= attROnly;
if (!(uAttr & UNIX_READ))
uNewAttr |= attHidd;
}
else
{
uNewAttr = 0;
// we cannot assume that if the file hasn't the archive attribute set
// then it is executable and set execute permissions
if (!(uAttr & attHidd))
uNewAttr |= (CREATE_OTHER_PERMISSIONS (UNIX_READ) | CREATE_GROUP_PERMISSIONS (UNIX_READ)) |
CREATE_USER_PERMISSIONS (UNIX_READ);
if (!(uAttr & attROnly))
uNewAttr |= (CREATE_GROUP_PERMISSIONS (UNIX_WRITE) | CREATE_USER_PERMISSIONS (UNIX_WRITE));
if (uAttr & attDir)
{
uNewAttr |= UNIX_DIRECTORY_ATTRIBUTE;
uNewAttr |= (CREATE_OTHER_PERMISSIONS (UNIX_EXEC) | CREATE_GROUP_PERMISSIONS (UNIX_EXEC)) |
CREATE_USER_PERMISSIONS (UNIX_EXEC);
}
else
uNewAttr |= UNIX_FILE_ATTRIBUTE;
}
return uNewAttr;
}
DWORD AttrMac(DWORD uAttr, bool )
{
return uAttr & (attDir | attROnly);
}
// ************************************************************************
ZIPINLINE bool ZipCompatibility::IsPlatformSupported(int iCode)
{
return iCode == zcDosFat || iCode == zcUnix || iCode == zcMacintosh
|| iCode == zcNtfs || iCode == zcOs2Hpfs;
}
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::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::SlashBackslashChg(CZipString& szFileName, bool bReplaceSlash)
{
TCHAR t1 = _T('\\') /*backslash*/, t2 = _T('/'), c1, c2;
if (bReplaceSlash)
{
c1 = t1;
c2 = t2;
}
else
{
c1 = t2;
c2 = t1;
}
szFileName.Replace(c2, c1);
}