1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive Library Open Source distribution
// and is Copyrighted 2000 - 2022 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: https://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef _ZIP_SYSTEM_WIN
#define _ZIP_SAFE_WINDOWS_API
#include "ZipPlatform.h"
#include "ZipFileHeader.h"
#include "ZipException.h"
#include "ZipAutoBuffer.h"
#include <sys/stat.h>
#ifndef __BORLANDC__
#include <sys/utime.h>
#else
#ifndef _UTIMBUF_DEFINED
#define _utimbuf utimbuf
#define _UTIMBUF_DEFINED
#endif
#include <utime.h>
#endif
#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
#endif
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
#include <direct.h>
#include <io.h>
#include <share.h>
#include <time.h>
#include <locale>
#include "ZipPathComponent.h"
#include "ZipCompatibility.h"
const TCHAR CZipPathComponent::m_cSeparator = _T('\\');
ULONGLONG ZipPlatform::GetDeviceFreeSpace(LPCTSTR lpszPath)
{
ULONGLONG uFreeBytesToCaller = 0, uTotalBytes = 0, uFreeBytes = 0;
CZipPathComponent zpc (lpszPath);
CZipString szDrive = zpc.GetFileDrive();
if (!GetDiskFreeSpaceEx(
szDrive,
(PULARGE_INTEGER)&uFreeBytesToCaller,
(PULARGE_INTEGER)&uTotalBytes,
(PULARGE_INTEGER)&uFreeBytes))
{
CZipPathComponent::AppendSeparator(szDrive); // in spite of what is written in MSDN it is sometimes needed (on fixed disks)
if (!GetDiskFreeSpaceEx(
szDrive,
(PULARGE_INTEGER)&uFreeBytesToCaller,
(PULARGE_INTEGER)&uTotalBytes,
(PULARGE_INTEGER)&uFreeBytes))
return 0;
}
return uFreeBytes;
}
CZipString ZipPlatform::GetTmpFileName(LPCTSTR lpszPath, ZIP_SIZE_TYPE uSizeNeeded)
{
TCHAR empty[] = _T("");
CZipString tempPath;
bool bCheckTemp = true;
if (lpszPath)
{
tempPath = lpszPath;
bCheckTemp = uSizeNeeded > 0 && GetDeviceFreeSpace(tempPath) < uSizeNeeded;
}
if (bCheckTemp)
{
DWORD size = GetTempPath(0, empty);
if (size == 0)
return (CZipString)empty;
GetTempPath(size, tempPath.GetBuffer(size));
tempPath.ReleaseBuffer();
if (GetDeviceFreeSpace(tempPath) < uSizeNeeded)
{
if (!GetCurrentDirectory(tempPath) || (uSizeNeeded > 0 && GetDeviceFreeSpace(tempPath) < uSizeNeeded))
return (CZipString)empty;
}
}
CZipPathComponent::AppendSeparator(tempPath);
tempPath += _T("ZAR");
tempPath += _T("XXXXXX");
int length = tempPath.GetLength() + 1;
#if _MSC_VER >= 1400
errno_t err = _tmktemp_s(tempPath.GetBuffer(length), (size_t)length);
#else // defined __MINGW32__ || _MSC_VER >= 1200
LPTSTR err = _tmktemp(tempPath.GetBuffer(length));
#endif
tempPath.ReleaseBuffer();
#if _MSC_VER >= 1400
if (err != 0)
#else
if (err == NULL)
#endif
{
return (CZipString)empty;
}
return tempPath;
}
bool ZipPlatform::GetCurrentDirectory(CZipString& sz)
{
DWORD i = ::GetCurrentDirectory(0, NULL);
if (!i)
return false;
TCHAR* pBuf = new TCHAR[i];
bool b = true;
if (!::GetCurrentDirectory(i, pBuf))
b = false;
else
sz = pBuf;
delete[] pBuf;
return b;
}
bool ZipPlatform::SetFileAttr(LPCTSTR lpFileName, DWORD uAttr)
{
return ::SetFileAttributes(CZipPathComponent::AddPrefix(lpFileName), uAttr) != 0;
}
bool ZipPlatform::GetFileAttr(LPCTSTR lpFileName, DWORD& uAttr)
{
DWORD temp = ::GetFileAttributes(CZipPathComponent::AddPrefix(lpFileName));
if (temp == INVALID_FILE_ATTRIBUTES)
return false;
uAttr = temp;
return true;
}
bool ZipPlatform::GetFileTimes(LPCTSTR lpFileName, time_t* tModificationTime, time_t* tCreationTime, time_t* tLastAccessTime)
{
WIN32_FIND_DATA findData = {0};
CZipString szPath = CZipPathComponent::AddPrefix(lpFileName);
HANDLE handle = ::FindFirstFile(szPath, &findData);
if (tModificationTime != NULL)
{
*tModificationTime = 0;
}
if (tCreationTime != NULL)
{
*tCreationTime = 0;
}
if (tLastAccessTime != NULL)
{
*tLastAccessTime = 0;
}
bool ret = false;
if (handle != INVALID_HANDLE_VALUE)
{
FindClose(handle);
ret = true;
}
else
{
// handle the situation when there are no permissions for reading the list of files, but there are permissions for reading the file
handle = ::CreateFile(szPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle != INVALID_HANDLE_VALUE)
{
ZBOOL bResult = ::GetFileTime(handle, &findData.ftCreationTime, &findData.ftLastAccessTime, &findData.ftLastWriteTime);
CloseHandle(handle);
if (bResult != FALSE)
{
ret = true;
}
}
}
if (ret)
{
if (tModificationTime != NULL && !ConvertFileTimeToTime(findData.ftLastWriteTime, *tModificationTime))
ret = false;
if (tCreationTime != NULL && !ConvertFileTimeToTime(findData.ftCreationTime, *tCreationTime))
ret = false;
if (tLastAccessTime != NULL && !ConvertFileTimeToTime(findData.ftLastAccessTime, *tLastAccessTime))
ret = false;
}
if (!ret)
{
time_t tNow = time(NULL);
if (tModificationTime != NULL && *tModificationTime <= 0)
{
*tModificationTime = tNow;
}
if (tCreationTime != NULL && *tCreationTime <= 0)
{
*tCreationTime = tNow;
}
if (tLastAccessTime != NULL && *tLastAccessTime <= 0)
{
*tLastAccessTime = tNow;
}
}
return ret;
}
bool ZipPlatform::SetFileTimes(LPCTSTR lpFileName, const time_t* tModificationTime, const time_t* tCreationTime, const time_t* tLastAccessTime)
{
HANDLE handle = ::CreateFile(CZipPathComponent::AddPrefix(lpFileName), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (handle == INVALID_HANDLE_VALUE)
{
return false;
}
bool ret = ZipPlatform::SetFileTimes(handle, tModificationTime, tCreationTime, tLastAccessTime);
CloseHandle(handle);
return ret;
}
bool ZipPlatform::SetFileTimes(HANDLE handle, const time_t* tModificationTime, const time_t* tCreationTime, const time_t* tLastAccessTime)
{
ZFILETIME ftModTime = {0};
ZFILETIME* pftModTime;
if (tModificationTime != NULL)
{
ZipPlatform::ConvertTimeToFileTime(*tModificationTime, ftModTime);
pftModTime = &ftModTime;
}
else
{
pftModTime = NULL;
}
ZFILETIME ftCreationTime = {0};
ZFILETIME* pftCreationTime;
if (tCreationTime != NULL)
{
ZipPlatform::ConvertTimeToFileTime(*tCreationTime, ftCreationTime);
pftCreationTime = &ftCreationTime;
}
else
{
pftCreationTime = NULL;
}
ZFILETIME ftLastAccessTime = {0};
ZFILETIME* pftLastAccessTime;
if (tLastAccessTime != NULL)
{
ZipPlatform::ConvertTimeToFileTime(*tLastAccessTime, ftLastAccessTime);
pftLastAccessTime = &ftLastAccessTime;
}
else
{
pftLastAccessTime = NULL;
}
return ::SetFileTime(handle, pftCreationTime, pftLastAccessTime , pftModTime) != 0;
}
bool ZipPlatform::ChangeDirectory(LPCTSTR lpDirectory)
{
return _tchdir(lpDirectory) == 0; // returns 0 if ok
}
int ZipPlatform::FileExists(LPCTSTR lpszName)
{
DWORD attributes = ::GetFileAttributes(CZipPathComponent::AddPrefix(lpszName));
if (attributes == INVALID_FILE_ATTRIBUTES)
return 0;
return ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) ? -1 : 1;
}
ZIPINLINE bool ZipPlatform::IsDriveRemovable(LPCTSTR lpszFilePath)
{
CZipPathComponent zpc(lpszFilePath);
return ::GetDriveType(zpc.GetFileDrive()) == DRIVE_REMOVABLE;
}
ZIPINLINE bool ZipPlatform::SetVolLabel(LPCTSTR lpszPath, LPCTSTR lpszLabel)
{
CZipPathComponent zpc(lpszPath);
CZipString szDrive = zpc.GetFileDrive();
CZipPathComponent::AppendSeparator(szDrive);
return ::SetVolumeLabel(szDrive, lpszLabel) != 0;
}
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
}
ZIPINLINE bool ZipPlatform::RemoveFile(LPCTSTR lpszFileName, bool bThrow, int iMode)
{
if ((iMode & ZipPlatform::fomRemoveReadOnly) != 0)
{
DWORD attr;
if (ZipPlatform::GetFileAttr(lpszFileName, attr)
&& (ZipCompatibility::GetAsInternalAttributes(attr, ZipPlatform::GetSystemID()) & ZipCompatibility::attROnly) != 0)
{
ZipPlatform::SetFileAttr(lpszFileName, ZipPlatform::GetDefaultAttributes());
}
}
#ifdef SHFileOperation
if ((iMode & ZipPlatform::fomRecycleBin) == 0)
{
#endif
if (::DeleteFile((LPTSTR)(LPCTSTR)CZipPathComponent::AddPrefix(lpszFileName, false)))
return true;
#ifdef SHFileOperation
}
else
{
CZipString file = lpszFileName;
#if defined _UNICODE && _MSC_VER >= 1400
if (file.GetLength() >= MAX_PATH)
{
// cannot prefix for SHFileOperation, use short path
CZipString temp = CZipPathComponent::AddPrefix(lpszFileName, false);
DWORD size = ::GetShortPathName(temp, NULL, 0);
if (size != 0)
{
size = ::GetShortPathName(temp, file.GetBuffer(size), size);
file.ReleaseBuffer();
}
if (size == 0)
{
if (bThrow)
CZipException::Throw(CZipException::notRemoved, lpszFileName);
return false;
}
// GetShortPathName keeps the prefix - remove it
int prefixLength = CZipPathComponent::IsPrefixed(file);
if (prefixLength > 0)
{
file = file.Mid(prefixLength);
}
}
#endif
int length = file.GetLength();
CZipAutoBuffer buffer((length + 2) * sizeof(TCHAR), true); // double NULL is required
memcpy(buffer, (LPCTSTR)file, length * sizeof(TCHAR));
SHFILEOPSTRUCT shFileOp = {0};
shFileOp.wFunc = FO_DELETE;
shFileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
shFileOp.pFrom = (LPCTSTR)(char*)buffer;
if (SHFileOperation(&shFileOp) == 0 && !shFileOp.fAnyOperationsAborted)
return true;
}
#endif
if (bThrow)
CZipException::Throw(CZipException::notRemoved, lpszFileName);
return false;
}
ZIPINLINE bool ZipPlatform::RenameFile( LPCTSTR lpszOldName, LPCTSTR lpszNewName, bool bThrow)
{
if (!::MoveFile((LPTSTR)(LPCTSTR)CZipPathComponent::AddPrefix(lpszOldName, false),
(LPTSTR)(LPCTSTR)CZipPathComponent::AddPrefix(lpszNewName, false)))
if (bThrow)
CZipException::Throw(CZipException::notRenamed, lpszOldName);
else
return false;
return true;
}
ZIPINLINE bool ZipPlatform::IsDirectory(DWORD uAttr)
{
return (uAttr & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
ZIPINLINE bool ZipPlatform::CreateNewDirectory(LPCTSTR lpDirectory)
{
return ::CreateDirectory(CZipPathComponent::AddPrefix(lpDirectory), NULL) != 0;
}
ZIPINLINE DWORD ZipPlatform::GetDefaultAttributes()
{
return FILE_ATTRIBUTE_ARCHIVE;
}
ZIPINLINE DWORD ZipPlatform::GetDefaultDirAttributes()
{
return FILE_ATTRIBUTE_DIRECTORY;
}
ZIPINLINE int ZipPlatform::GetSystemID()
{
return ZipCompatibility::zcDosFat;
}
ZIPINLINE bool ZipPlatform::GetSystemCaseSensitivity()
{
return false;
}
#ifdef _UNICODE
#ifdef _ZIP_UNICODE_NORMALIZE
#pragma comment(lib, "Normaliz.lib")
#endif
int ZipPlatform::WideToMultiByte(LPCWSTR lpszIn, CZipAutoBuffer &szOut, bool bAddNull, 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(bAddNull ? iLen + 1 : iLen, true);
iLen = ::WideCharToMultiByte(uCodePage, 0, lpszIn, (int)wideLen, szOut,
iLen, NULL, NULL);
ASSERT(iLen > 0);
if (bAddNull)
{
szOut[iLen] = 0;
}
return iLen > 0 ? iLen : -1;
}
else // here it means error
{
szOut.Release();
return -1;
}
}
int ZipPlatform::MultiByteToWide(const char* szIn, int iInSize, CZipString& szOut, UINT uCodePage)
{
szOut.Empty();
if (iInSize < 0)
{
iInSize = (int)std::string(szIn).length();
}
if (iInSize == 0)
{
return 0;
}
// iLen doesn't include terminating character
DWORD dwFlags = uCodePage <= CP_OEMCP ? MB_PRECOMPOSED : 0;
int iLen = ::MultiByteToWideChar(uCodePage, dwFlags, szIn, iInSize, NULL, 0);
if (iLen > 0)
{
iLen = ::MultiByteToWideChar(uCodePage, dwFlags, szIn, iInSize, 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) != FALSE)
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
{
return -1;
}
}
#endif
#if defined _ZIP_IMPL_STL || _ZIP_FILE_IMPLEMENTATION == ZIP_ZFI_STL
#if _MSC_VER > 1000
#pragma warning( push )
#pragma warning (disable : 4702) // unreachable code
#endif
bool ZipPlatform::TruncateFile(int iDes, ULONGLONG uSize)
{
#if (_MSC_VER >= 1400)
return _chsize_s(iDes, uSize) == 0;
#else
if (uSize > _I64_MAX)
CZipException::Throw(CZipException::tooBigSize);
else
{
HANDLE handle = (HANDLE)GetFileSystemHandle(iDes);
ULARGE_INTEGER li;
li.QuadPart = uSize;
li.LowPart = ::SetFilePointer(handle, li.LowPart, (LONG*)&li.HighPart, FILE_BEGIN);
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
return false;
return ::SetEndOfFile(handle) != FALSE;
}
return false; // for the compiler
#endif
}
#if _MSC_VER > 1000
#pragma warning( pop )
#endif
int ZipPlatform::OpenFile(LPCTSTR lpszFileName, UINT iMode, int iShareMode)
{
switch (iShareMode)
{
case (CZipFile::shareExclusive):
iShareMode = SH_DENYRW;
break;
case (CZipFile::shareDenyRead):
iShareMode = SH_DENYRD;
break;
case (CZipFile::shareDenyWrite):
iShareMode = SH_DENYWR;
break;
default:
iShareMode = SH_DENYNO;
}
#if _MSC_VER >= 1400
int handle;
if (_tsopen_s(&handle, lpszFileName, iMode, iShareMode, S_IREAD | S_IWRITE /*required only when O_CREAT mode*/) != 0)
return -1;
else
return handle;
#else
return _tsopen(lpszFileName, iMode, iShareMode, S_IREAD | S_IWRITE /*required only when O_CREAT mode*/);
#endif
}
bool ZipPlatform::FlushFile(int iDes)
{
return _commit(iDes) == 0;
}
intptr_t ZipPlatform::GetFileSystemHandle(int iDes)
{
return _get_osfhandle(iDes);
}
#endif // _ZIP_IMPL_STL
#endif // _ZIP_SYSTEM_WIN
|