-
Notifications
You must be signed in to change notification settings - Fork 0
/
PV_Lzh.pas
156 lines (126 loc) · 3.25 KB
/
PV_Lzh.pas
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
unit PV_Lzh;
{$mode objfpc}{$H+}
//PV Pack
//https://github.com/PascalVault
//Licence: MIT
//Last update: 2023-10-04
//LZH
interface
uses
Classes, SysUtils, Dialogs, CRC16_ARC, PV_Pack;
type
{ TLzh }
TLzh = class(TPack)
private
FStream: TStream;
FList: TFileList;
FComment: String;
public
constructor Create(Str: TStream); override;
destructor Destroy; override;
function AddFile(Str: TStream; Name: String): Boolean; override;
procedure SetComment(Comment: String); override;
end;
implementation
{ TLzh }
constructor TLzh.Create(Str: TStream);
begin
FStream := Str;
FList := TFileList.Create;
end;
destructor TLzh.Destroy;
var EOF: Byte;
begin
EOF := 0;
FStream.Write(EOF, 1);
end;
function TLzh.AddFile(Str: TStream; Name: String): Boolean;
const BufLen = 4096;
type THead = packed record
HeadLen: Byte; // 0=end of file
HeadCRC: Byte; //CRC-16 ARC
SignBegin: Char; // '-'
l: Char; // 'l'
H: Char; // 'h'
Compression: Char;
SignEnd: Char; // '-'
PackedSize: Cardinal;
UnpackedSize: Cardinal;
FileTime: Word;
FileDate: Word;
ExternalAttr: Byte;
Level: Byte;
FNameLen: Byte;
end;
var Head: THead;
HeadLen: Integer;
HeadOffset: Integer;
DataOffset: Integer;
PackSize: Cardinal;
Hasher: TCRC16_ARC;
Buf: array of Byte;
ReadLen: Integer;
FinalOffset: Cardinal;
CRC: Cardinal;
Mem: TMemoryStream;
HeadCRC: Byte;
Tmp: Byte;
i: Integer;
begin
HeadLen := SizeOf(Head) + Length(Name)+2;
//skip head for now
HeadOffset := FStream.Position;
DataOffset := HeadOffset + HeadLen;
FStream.Position := DataOffset;
//copy data and calculate checksum
Hasher := TCRC16_ARC.Create;
SetLength(Buf, BufLen);
while Str.Position < Str.Size do begin
ReadLen := Str.Read(Buf[0], BufLen);
Hasher.Update(@Buf[0], ReadLen);
FStream.Write(Buf[0], ReadLen);
end;
PackSize := FStream.Position - DataOffset;
FinalOffset := FStream.Position;
//rewind to write head
FStream.Position := HeadOffset;
CRC := Hasher.Final;
Hasher.Free;
with Head do begin
HeadLen := SizeOf(Head) + Length(Name); // 0=end of file
HeadCRC := 0;
SignBegin := '-';
l := 'l';
H := 'h';
Compression := '0';
SignEnd := '-';
PackedSize := PackSize;
UnpackedSize := Str.Size;
FileTime := DosTime;
FileDate := DosDate;
ExternalAttr := $20;
Level := 0;
FNameLen := Length(Name);
end;
Mem := TMemoryStream.Create;
Mem.Write(Head, SizeOf(Head));
Mem.Write(Name[1], Length(Name));
Mem.Write(CRC, 2);
Mem.Position := 2;
HeadCRC := 0;
for i:=2 to Mem.Size-1 do begin
Mem.Read(Tmp, 1);
HeadCRC := HeadCRC + Tmp;
end;
Mem.Free;
Head.HeadCRC := HeadCRC;
FStream.Write(Head, SizeOf(Head));
FStream.Write(Name[1], Length(Name));
FStream.Write(CRC, 2);
FStream.Position := FinalOffset;
end;
procedure TLzh.SetComment(Comment: String);
begin
FComment := Comment;
end;
end.