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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
|
// LzhHandler.cpp
#include "StdAfx.h"
#include "../../../C/CpuArch.h"
#include "../../Common/ComTry.h"
#include "../../Common/MyBuffer.h"
#include "../../Common/StringConvert.h"
#include "../../Windows/PropVariant.h"
#include "../../Windows/TimeUtils.h"
#include "../ICoder.h"
#include "../Common/LimitedStreams.h"
#include "../Common/ProgressUtils.h"
#include "../Common/RegisterArc.h"
#include "../Common/StreamUtils.h"
#include "../Compress/CopyCoder.h"
#include "../Compress/LzhDecoder.h"
#include "IArchive.h"
#include "Common/ItemNameUtils.h"
using namespace NWindows;
using namespace NTime;
#define Get16(p) GetUi16(p)
#define Get32(p) GetUi32(p)
namespace NArchive {
namespace NLzh{
const int kMethodIdSize = 5;
const Byte kExtIdFileName = 0x01;
const Byte kExtIdDirName = 0x02;
const Byte kExtIdUnixTime = 0x54;
struct CExtension
{
Byte Type;
CByteBuffer Data;
AString GetString() const
{
AString s;
for (size_t i = 0; i < Data.Size(); i++)
{
char c = (char)Data[i];
if (c == 0)
break;
s += c;
}
return s;
}
};
const UInt32 kBasicPartSize = 22;
API_FUNC_static_IsArc IsArc_Lzh(const Byte *p, size_t size)
{
if (size < 2 + kBasicPartSize)
return k_IsArc_Res_NEED_MORE;
if (p[2] != '-' || p[3] != 'l' || p[4] != 'h' || p[6] != '-')
return k_IsArc_Res_NO;
Byte n = p[5];
if (n != 'd')
if (n < '0' || n > '7')
return k_IsArc_Res_NO;
return k_IsArc_Res_YES;
}
}
struct CItem
{
AString Name;
Byte Method[kMethodIdSize];
Byte Attributes;
Byte Level;
Byte OsId;
UInt32 PackSize;
UInt32 Size;
UInt32 ModifiedTime;
UInt16 CRC;
CObjectVector<CExtension> Extensions;
bool IsValidMethod() const { return (Method[0] == '-' && Method[1] == 'l' && Method[4] == '-'); }
bool IsLhMethod() const {return (IsValidMethod() && Method[2] == 'h'); }
bool IsDir() const {return (IsLhMethod() && Method[3] == 'd'); }
bool IsCopyMethod() const
{
return (IsLhMethod() && Method[3] == '0') ||
(IsValidMethod() && Method[2] == 'z' && Method[3] == '4');
}
bool IsLh1GroupMethod() const
{
if (!IsLhMethod())
return false;
switch (Method[3])
{
case '1':
return true;
}
return false;
}
bool IsLh4GroupMethod() const
{
if (!IsLhMethod())
return false;
switch (Method[3])
{
case '4':
case '5':
case '6':
case '7':
return true;
}
return false;
}
int GetNumDictBits() const
{
if (!IsLhMethod())
return 0;
switch (Method[3])
{
case '1': return 12;
case '2': return 13;
case '3': return 13;
case '4': return 12;
case '5': return 13;
case '6': return 15;
case '7': return 16;
}
return 0;
}
int FindExt(Byte type) const
{
FOR_VECTOR (i, Extensions)
if (Extensions[i].Type == type)
return i;
return -1;
}
bool GetUnixTime(UInt32 &value) const
{
value = 0;
int index = FindExt(kExtIdUnixTime);
if (index < 0)
{
if (Level == 2)
{
value = ModifiedTime;
return true;
}
return false;
}
const Byte *data = (const Byte *)(Extensions[index].Data);
value = GetUi32(data);
return true;
}
AString GetDirName() const
{
int index = FindExt(kExtIdDirName);
if (index < 0)
return AString();
return Extensions[index].GetString();
}
AString GetFileName() const
{
int index = FindExt(kExtIdFileName);
if (index < 0)
return Name;
return Extensions[index].GetString();
}
AString GetName() const
{
AString dirName = GetDirName();
const char kDirSeparator = '\\';
// check kDirSeparator in Linux
dirName.Replace((char)(unsigned char)0xFF, kDirSeparator);
if (!dirName.IsEmpty() && dirName.Back() != kDirSeparator)
dirName += kDirSeparator;
return dirName + GetFileName();
}
};
static const Byte *ReadUInt16(const Byte *p, UInt16 &v)
{
v = Get16(p);
return p + 2;
}
static const Byte *ReadString(const Byte *p, size_t size, AString &s)
{
s.Empty();
for (size_t i = 0; i < size; i++)
{
char c = p[i];
if (c == 0)
break;
s += c;
}
return p + size;
}
static Byte CalcSum(const Byte *data, size_t size)
{
Byte sum = 0;
for (size_t i = 0; i < size; i++)
sum = (Byte)(sum + data[i]);
return sum;
}
static HRESULT GetNextItem(ISequentialInStream *stream, bool &filled, CItem &item)
{
filled = false;
size_t processedSize = 2;
Byte startHeader[2];
RINOK(ReadStream(stream, startHeader, &processedSize))
if (processedSize == 0)
return S_OK;
if (processedSize == 1)
return (startHeader[0] == 0) ? S_OK: S_FALSE;
if (startHeader[0] == 0 && startHeader[1] == 0)
return S_OK;
Byte header[256];
processedSize = kBasicPartSize;
RINOK(ReadStream(stream, header, &processedSize));
if (processedSize != kBasicPartSize)
return (startHeader[0] == 0) ? S_OK: S_FALSE;
const Byte *p = header;
memcpy(item.Method, p, kMethodIdSize);
if (!item.IsValidMethod())
return S_OK;
p += kMethodIdSize;
item.PackSize = Get32(p);
item.Size = Get32(p + 4);
item.ModifiedTime = Get32(p + 8);
item.Attributes = p[12];
item.Level = p[13];
p += 14;
if (item.Level > 2)
return S_FALSE;
UInt32 headerSize;
if (item.Level < 2)
{
headerSize = startHeader[0];
if (headerSize < kBasicPartSize)
return S_FALSE;
RINOK(ReadStream_FALSE(stream, header + kBasicPartSize, headerSize - kBasicPartSize));
if (startHeader[1] != CalcSum(header, headerSize))
return S_FALSE;
size_t nameLength = *p++;
if ((p - header) + nameLength + 2 > headerSize)
return S_FALSE;
p = ReadString(p, nameLength, item.Name);
}
else
headerSize = startHeader[0] | ((UInt32)startHeader[1] << 8);
p = ReadUInt16(p, item.CRC);
if (item.Level != 0)
{
if (item.Level == 2)
{
RINOK(ReadStream_FALSE(stream, header + kBasicPartSize, 2));
}
if ((size_t)(p - header) + 3 > headerSize)
return S_FALSE;
item.OsId = *p++;
UInt16 nextSize;
p = ReadUInt16(p, nextSize);
while (nextSize != 0)
{
if (nextSize < 3)
return S_FALSE;
if (item.Level == 1)
{
if (item.PackSize < nextSize)
return S_FALSE;
item.PackSize -= nextSize;
}
if (item.Extensions.Size() >= (1 << 8))
return S_FALSE;
CExtension ext;
RINOK(ReadStream_FALSE(stream, &ext.Type, 1))
nextSize -= 3;
ext.Data.Alloc(nextSize);
RINOK(ReadStream_FALSE(stream, (Byte *)ext.Data, nextSize))
item.Extensions.Add(ext);
Byte hdr2[2];
RINOK(ReadStream_FALSE(stream, hdr2, 2));
ReadUInt16(hdr2, nextSize);
}
}
filled = true;
return S_OK;
}
struct COsPair
{
Byte Id;
const char *Name;
};
static const COsPair g_OsPairs[] =
{
{ 0, "MS-DOS" },
{ 'M', "MS-DOS" },
{ '2', "OS/2" },
{ '9', "OS9" },
{ 'K', "OS/68K" },
{ '3', "OS/386" },
{ 'H', "HUMAN" },
{ 'U', "UNIX" },
{ 'C', "CP/M" },
{ 'F', "FLEX" },
{ 'm', "Mac" },
{ 'R', "Runser" },
{ 'T', "TownsOS" },
{ 'X', "XOSK" },
{ 'w', "Windows 95" },
{ 'W', "Windows NT" },
{ 'J', "Java VM" }
};
static const char *kUnknownOS = "Unknown";
static const char *GetOS(Byte osId)
{
for (unsigned i = 0; i < ARRAY_SIZE(g_OsPairs); i++)
if (g_OsPairs[i].Id == osId)
return g_OsPairs[i].Name;
return kUnknownOS;
}
static const Byte kProps[] =
{
kpidPath,
kpidIsDir,
kpidSize,
kpidPackSize,
kpidMTime,
// kpidAttrib,
kpidCRC,
kpidMethod,
kpidHostOS
};
class CCRC
{
UInt16 _value;
public:
static UInt16 Table[256];
static void InitTable();
CCRC(): _value(0) {}
void Init() { _value = 0; }
void Update(const void *data, size_t size);
UInt16 GetDigest() const { return _value; }
};
static const UInt16 kCRCPoly = 0xA001;
UInt16 CCRC::Table[256];
void CCRC::InitTable()
{
for (UInt32 i = 0; i < 256; i++)
{
UInt32 r = i;
for (int j = 0; j < 8; j++)
if (r & 1)
r = (r >> 1) ^ kCRCPoly;
else
r >>= 1;
CCRC::Table[i] = (UInt16)r;
}
}
class CCRCTableInit
{
public:
CCRCTableInit() { CCRC::InitTable(); }
} g_CRCTableInit;
void CCRC::Update(const void *data, size_t size)
{
UInt16 v = _value;
const Byte *p = (const Byte *)data;
for (; size > 0; size--, p++)
v = (UInt16)(Table[((Byte)(v)) ^ *p] ^ (v >> 8));
_value = v;
}
class COutStreamWithCRC:
public ISequentialOutStream,
public CMyUnknownImp
{
public:
MY_UNKNOWN_IMP
STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
private:
CCRC _crc;
CMyComPtr<ISequentialOutStream> _stream;
public:
void Init(ISequentialOutStream *stream)
{
_stream = stream;
_crc.Init();
}
void ReleaseStream() { _stream.Release(); }
UInt32 GetCRC() const { return _crc.GetDigest(); }
void InitCRC() { _crc.Init(); }
};
STDMETHODIMP COutStreamWithCRC::Write(const void *data, UInt32 size, UInt32 *processedSize)
{
UInt32 realProcessedSize;
HRESULT result;
if (!_stream)
{
realProcessedSize = size;
result = S_OK;
}
else
result = _stream->Write(data, size, &realProcessedSize);
_crc.Update(data, realProcessedSize);
if (processedSize != NULL)
*processedSize = realProcessedSize;
return result;
}
struct CItemEx: public CItem
{
UInt64 DataPosition;
};
class CHandler:
public IInArchive,
public CMyUnknownImp
{
CObjectVector<CItemEx> _items;
CMyComPtr<IInStream> _stream;
UInt64 _phySize;
UInt32 _errorFlags;
bool _isArc;
public:
MY_UNKNOWN_IMP1(IInArchive)
INTERFACE_IInArchive(;)
CHandler();
};
IMP_IInArchive_Props
IMP_IInArchive_ArcProps_NO_Table
CHandler::CHandler() {}
STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems)
{
*numItems = _items.Size();
return S_OK;
}
STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
{
NCOM::CPropVariant prop;
switch (propID)
{
case kpidPhySize: prop = _phySize; break;
case kpidErrorFlags:
UInt32 v = _errorFlags;
if (!_isArc) v |= kpv_ErrorFlags_IsNotArc;
prop = v;
break;
}
prop.Detach(value);
return S_OK;
}
STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NCOM::CPropVariant prop;
const CItemEx &item = _items[index];
switch (propID)
{
case kpidPath:
{
UString s = NItemName::WinNameToOSName(MultiByteToUnicodeString(item.GetName(), CP_OEMCP));
if (!s.IsEmpty())
{
if (s.Back() == WCHAR_PATH_SEPARATOR)
s.DeleteBack();
prop = s;
}
break;
}
case kpidIsDir: prop = item.IsDir(); break;
case kpidSize: prop = item.Size; break;
case kpidPackSize: prop = item.PackSize; break;
case kpidCRC: prop = (UInt32)item.CRC; break;
case kpidHostOS: prop = GetOS(item.OsId); break;
case kpidMTime:
{
FILETIME utc;
UInt32 unixTime;
if (item.GetUnixTime(unixTime))
NTime::UnixTimeToFileTime(unixTime, utc);
else
{
FILETIME localFileTime;
if (DosTimeToFileTime(item.ModifiedTime, localFileTime))
{
if (!LocalFileTimeToFileTime(&localFileTime, &utc))
utc.dwHighDateTime = utc.dwLowDateTime = 0;
}
else
utc.dwHighDateTime = utc.dwLowDateTime = 0;
}
prop = utc;
break;
}
// case kpidAttrib: prop = (UInt32)item.Attributes; break;
case kpidMethod:
{
char method2[kMethodIdSize + 1];
method2[kMethodIdSize] = 0;
memcpy(method2, item.Method, kMethodIdSize);
prop = method2;
break;
}
}
prop.Detach(value);
return S_OK;
COM_TRY_END
}
STDMETHODIMP CHandler::Open(IInStream *stream,
const UInt64 * /* maxCheckStartPosition */, IArchiveOpenCallback *callback)
{
COM_TRY_BEGIN
Close();
try
{
_items.Clear();
UInt64 endPos = 0;
bool needSetTotal = true;
RINOK(stream->Seek(0, STREAM_SEEK_END, &endPos));
RINOK(stream->Seek(0, STREAM_SEEK_SET, NULL));
for (;;)
{
CItemEx item;
bool filled;
HRESULT result = GetNextItem(stream, filled, item);
RINOK(stream->Seek(0, STREAM_SEEK_CUR, &item.DataPosition));
if (result == S_FALSE)
{
_errorFlags = kpv_ErrorFlags_HeadersError;
break;
}
if (result != S_OK)
return S_FALSE;
_phySize = item.DataPosition;
if (!filled)
break;
_items.Add(item);
_isArc = true;
UInt64 newPostion;
RINOK(stream->Seek(item.PackSize, STREAM_SEEK_CUR, &newPostion));
if (newPostion > endPos)
{
_phySize = endPos;
_errorFlags = kpv_ErrorFlags_UnexpectedEnd;
break;
}
_phySize = newPostion;
if (callback)
{
if (needSetTotal)
{
RINOK(callback->SetTotal(NULL, &endPos));
needSetTotal = false;
}
if (_items.Size() % 100 == 0)
{
UInt64 numFiles = _items.Size();
UInt64 numBytes = item.DataPosition;
RINOK(callback->SetCompleted(&numFiles, &numBytes));
}
}
}
if (_items.IsEmpty())
return S_FALSE;
_stream = stream;
}
catch(...)
{
return S_FALSE;
}
COM_TRY_END
return S_OK;
}
STDMETHODIMP CHandler::Close()
{
_isArc = false;
_phySize = 0;
_errorFlags = 0;
_items.Clear();
_stream.Release();
return S_OK;
}
STDMETHODIMP CHandler::Extract(const UInt32 *indices, UInt32 numItems,
Int32 testModeSpec, IArchiveExtractCallback *extractCallback)
{
COM_TRY_BEGIN
bool testMode = (testModeSpec != 0);
UInt64 totalUnPacked = 0, totalPacked = 0;
bool allFilesMode = (numItems == (UInt32)(Int32)-1);
if (allFilesMode)
numItems = _items.Size();
if (numItems == 0)
return S_OK;
UInt32 i;
for (i = 0; i < numItems; i++)
{
const CItemEx &item = _items[allFilesMode ? i : indices[i]];
totalUnPacked += item.Size;
totalPacked += item.PackSize;
}
RINOK(extractCallback->SetTotal(totalUnPacked));
UInt64 currentTotalUnPacked = 0, currentTotalPacked = 0;
UInt64 currentItemUnPacked, currentItemPacked;
NCompress::NLzh::NDecoder::CCoder *lzhDecoderSpec = 0;
CMyComPtr<ICompressCoder> lzhDecoder;
CMyComPtr<ICompressCoder> lzh1Decoder;
CMyComPtr<ICompressCoder> arj2Decoder;
NCompress::CCopyCoder *copyCoderSpec = new NCompress::CCopyCoder();
CMyComPtr<ICompressCoder> copyCoder = copyCoderSpec;
CLocalProgress *lps = new CLocalProgress;
CMyComPtr<ICompressProgressInfo> progress = lps;
lps->Init(extractCallback, false);
CLimitedSequentialInStream *streamSpec = new CLimitedSequentialInStream;
CMyComPtr<ISequentialInStream> inStream(streamSpec);
streamSpec->SetStream(_stream);
for (i = 0; i < numItems; i++, currentTotalUnPacked += currentItemUnPacked,
currentTotalPacked += currentItemPacked)
{
currentItemUnPacked = 0;
currentItemPacked = 0;
lps->InSize = currentTotalPacked;
lps->OutSize = currentTotalUnPacked;
RINOK(lps->SetCur());
CMyComPtr<ISequentialOutStream> realOutStream;
Int32 askMode;
askMode = testMode ? NExtract::NAskMode::kTest :
NExtract::NAskMode::kExtract;
Int32 index = allFilesMode ? i : indices[i];
const CItemEx &item = _items[index];
RINOK(extractCallback->GetStream(index, &realOutStream, askMode));
if (item.IsDir())
{
// if (!testMode)
{
RINOK(extractCallback->PrepareOperation(askMode));
RINOK(extractCallback->SetOperationResult(NExtract::NOperationResult::kOK));
}
continue;
}
if (!testMode && !realOutStream)
continue;
RINOK(extractCallback->PrepareOperation(askMode));
currentItemUnPacked = item.Size;
currentItemPacked = item.PackSize;
{
COutStreamWithCRC *outStreamSpec = new COutStreamWithCRC;
CMyComPtr<ISequentialOutStream> outStream(outStreamSpec);
outStreamSpec->Init(realOutStream);
realOutStream.Release();
UInt64 pos;
_stream->Seek(item.DataPosition, STREAM_SEEK_SET, &pos);
streamSpec->Init(item.PackSize);
HRESULT result = S_OK;
Int32 opRes = NExtract::NOperationResult::kOK;
if (item.IsCopyMethod())
{
result = copyCoder->Code(inStream, outStream, NULL, NULL, progress);
if (result == S_OK && copyCoderSpec->TotalSize != item.PackSize)
result = S_FALSE;
}
else if (item.IsLh4GroupMethod())
{
if (!lzhDecoder)
{
lzhDecoderSpec = new NCompress::NLzh::NDecoder::CCoder;
lzhDecoder = lzhDecoderSpec;
}
lzhDecoderSpec->SetDictionary(item.GetNumDictBits());
result = lzhDecoder->Code(inStream, outStream, NULL, ¤tItemUnPacked, progress);
}
/*
else if (item.IsLh1GroupMethod())
{
if (!lzh1Decoder)
{
lzh1DecoderSpec = new NCompress::NLzh1::NDecoder::CCoder;
lzh1Decoder = lzh1DecoderSpec;
}
lzh1DecoderSpec->SetDictionary(item.GetNumDictBits());
result = lzh1Decoder->Code(inStream, outStream, NULL, ¤tItemUnPacked, progress);
}
*/
else
opRes = NExtract::NOperationResult::kUnsupportedMethod;
if (opRes == NExtract::NOperationResult::kOK)
{
if (result == S_FALSE)
opRes = NExtract::NOperationResult::kDataError;
else
{
RINOK(result);
if (outStreamSpec->GetCRC() != item.CRC)
opRes = NExtract::NOperationResult::kCRCError;
}
}
outStream.Release();
RINOK(extractCallback->SetOperationResult(opRes));
}
}
return S_OK;
COM_TRY_END
}
IMP_CreateArcIn
static CArcInfo g_ArcInfo =
{ "Lzh", "lzh lha", 0, 6,
3, { '-', 'l', 'h' },
2,
0,
CreateArc, NULL, IsArc_Lzh };
REGISTER_ARC(Lzh)
}}
|