-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPV_Arj.pas
318 lines (252 loc) · 6.94 KB
/
PV_Arj.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
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
unit PV_Arj;
{$mode objfpc}{$H+}
//PV Pack
//https://github.com/PascalVault
//Licence: MIT
//Last update: 2023-10-04
//ARJ
interface
uses
Classes, SysUtils, Dialogs, CRC32_ISOHDLC, PV_Pack;
type
{ TArj }
TArj = class(TPack)
private
FStream: TStream;
FList: TFileList;
FComment: String;
procedure Start;
public
constructor Create(Str: TStream); override;
destructor Destroy; override;
function AddFile(Str: TStream; Name: String): Boolean; override;
procedure SetComment(Comment: String); override;
end;
implementation
{ TArj }
constructor TArj.Create(Str: TStream);
begin
FStream := Str;
FList := TFileList.Create;
Start;
end;
procedure TArj.Start;
type THead = packed record
Magic: Word; //=$ea60
HeadSize: Word;
HeadSize2: Byte;
Version: Byte; //4
MinVersion: Byte; //1
Host: Byte; //0= dos, 2=unix, 4=mac
Flags: Byte; //0=no pass
Security: Byte;
Typee: Byte; //0=binary
Reserved: Byte;
CreateTime: Word;//msdos
CreateDate: Word; //msdos
ModifyTime: Word;//msdos
ModifyDate: Word; //msdos
ArchiveSize: Cardinal;
Security2: Cardinal;
SpecOffset: Word;
SpecSize: Word;
Encryption: Byte;
LastChapter: Byte;
Protection: Byte;
ArjFlags: Byte;
SpareBytes: Word;
end;
var Head: THead;
HeadLen: Integer;
Hasher: TCRC32_ISO;
CRC: Cardinal;
ArcName: String;
ExtHeadSize: Cardinal;
HeadCRC: Cardinal;
Null: Byte;
Mem: TMemoryStream;
Tmp: Byte;
i: Integer;
begin
ArcName := 'ARCHIVE.ARJ';
HeadLen := SizeOf(Head) + Length(ArcName) + 8; //8 = 2*null + CRC + extra
with Head do begin
Magic := $ea60;
HeadSize := 36+Length(ArcName);
HeadSize2 := 34; //SizeOf(Head)-4(Magic)
Version := 11;
MinVersion:= 1;
Host := 0;
Flags := $10;
Security := 0;
Typee := 2;
Reserved := $1a;
CreateTime := DosTime;
CreateDate := DosDate;
ModifyTime := DosTime;
ModifyDate := DosDate;
ArchiveSize := 0;
Security2 := 0;
SpecOffset := 0;
SpecSize := 0;
Encryption := 0;
LastChapter := 0;
Protection := 0;
ArjFlags := 0;
SpareBytes := 0;
end;
//head crc
Mem := TMemoryStream.Create;
Mem.Write(Head, SizeOf(Head));
Mem.Write(ArcName[1], Length(ArcName));
Null := 0;
Mem.Write(Null, 1); //null ending filename
Mem.Write(Null, 1); //null ending comment
Mem.Position := 4;
Hasher := TCRC32_ISO.Create;
for i:=4 to Mem.Size-1 do begin
Mem.Read(Tmp, 1);
Hasher.Update(@Tmp, 1);
end;
Mem.Free;
HeadCRC := Hasher.Final;
Hasher.Free;
FStream.Write(Head, SizeOf(Head));
FStream.Write(ArcName[1], Length(ArcName));
//archive comment, if any
Null := 0;
FStream.Write(Null, 1); //null ending filename
FStream.Write(Null, 1); //null ending comment
FStream.Write(HeadCRC, 4);
ExtHeadSize := 00;
FStream.Write(ExtHeadSize, 2);
end;
destructor TArj.Destroy;
var EOF: Cardinal;
begin
EOF := $Ea60;
FStream.Write(EOF, 4);
end;
function TArj.AddFile(Str: TStream; Name: String): Boolean;
const BufLen = 4096;
type TEntryHead = packed record
Magic: Word; //=$ea60
HeadSize: Word;
end;
TEntry = packed record
HeadSize2: Byte;
Version: Byte; //4
MinVersion: Byte; //1
Host: Byte; //0= dos, 2=unix, 4=mac
Flags: Byte; //0=no pass
Compression: Byte;//0=stored
Typee: Byte; //0=binary
Reserved: Byte;
Time: Word;//msdos
Date: Word; //msdos
PackedSize: Cardinal;
UnpackedSize: Cardinal;
CRC32: Cardinal; //CRC32_ISOHDLC
Offset: Word; //11
Attributes: Word; //868
HostData: Word;
//34 bytes all the above
//file name null terminated
//comment null terminated
//crc32 of basic header
//packed file here
end;
var Entry: TEntry;
EntryHead: TEntryHead;
HeadLen: Integer;
HeadOffset: Integer;
DataOffset: Integer;
PackSize: Cardinal;
Hasher: TCRC32_ISO;
Buf: array of Byte;
ReadLen: Integer;
FinalOffset: Cardinal;
CRC: Cardinal;
ArcName: String;
What: Cardinal;
ExtHeadSize: Cardinal;
HeadCRC: Cardinal;
Null: Byte;
Mem: TMemoryStream;
Tmp: Byte;
i: Integer;
begin
Name := Uppercase(Name);
HeadLen := SizeOf(EntryHead) + SizeOf(Entry) + Length(Name) + 8; //8 = 2*null + CRC + extra
//skip head for now
HeadOffset := FStream.Position;
DataOffset := HeadOffset + HeadLen;
FStream.Position := DataOffset;
//copy data and calculate checksum
Hasher := TCRC32_ISO.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;
CRC := Hasher.Final;
Hasher.Free;
PackSize := FStream.Position - DataOffset;
FinalOffset := FStream.Position;
//rewind to write head
FStream.Position := HeadOffset;
with EntryHead do begin
Magic := $ea60;
HeadSize := 30 + 2 + Length(Name); //2 = 2*null
end;
FStream.Write(EntryHead, SizeOf(EntryHead));
with Entry do begin
HeadSize2 := 30;
Version := $0b;
MinVersion := 1;
Host := 0;
Flags := $10; //(0x10 = PATHSYM_FLAG) indicates filename translated ("\" changed to "/")
Compression:= 0; //store
Typee := 0; //binary
Reserved := $1a;
Time := DosTime;
Date := DosDate;
PackedSize := PackSize;
UnpackedSize:= Str.Size;
CRC32 := CRC;
Offset := 0;
Attributes := $20;
Hostdata := 0;
end;
//head crc
Mem := TMemoryStream.Create;
Mem.Write(Entry, SizeOf(Entry));
Mem.Write(Name[1], Length(Name));
Null := 0;
Mem.Write(Null, 1); //null ending filename
Mem.Write(Null, 1); //null ending comment
Mem.Position := 0;
Hasher := TCRC32_ISO.Create;
for i:=0 to Mem.Size-1 do begin
Mem.Read(Tmp, 1);
Hasher.Update(@Tmp, 1);
end;
Mem.Free;
HeadCRC := Hasher.Final; //CRC32 ISO HDLC of 38 bytes, that is from HeadSize2 to the last null above
Hasher.Free;
FStream.Write(Entry, SizeOf(Entry));
FStream.Write(Name[1], Length(Name));
Null := 0;
FStream.Write(Null, 1); //null ending filename
FStream.Write(Null, 1); //null ending comment
FStream.Write(HeadCRC, 4);
ExtHeadSize := 0;
FStream.Write(ExtHeadSize, 2);
FStream.Position := FinalOffset;
end;
procedure TArj.SetComment(Comment: String);
begin
FComment := Comment;
end;
end.