-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUTAR.PAS
200 lines (168 loc) · 5.29 KB
/
UTAR.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
{ MIT License
Copyright (c) 2024 Viacheslav Komenda
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. }
{$A-,I-,S-,R-,D+,L+,Q-,F-,G-,O-,B-}
UNIT UTAR;
INTERFACE
(* Read/Write .tar file.
File read example:
VAR f : TAR_FILE; fname : STRING; fsize : LONGINT;
BEGIN
Assign(f, 'hello.tar');
Reset(f, 1);
WHILE utar.Find(f, fname, fsize) DO BEGIN
WriteLn(fname, ' ', fsize);
Seek(f, FilePos(f) + fsize);
END;
Close(f);
END;
File write example:
VAR f : TAR_FILE; str : STRING;
BEGIN
Assign(f, 'hello.tar');
ReWrite(f, 1);
{ hello.txt }
str := 'Hello, world!';
utar.Add(f, 'hello.txt', Length(str));
BlockWrite(f, str[1], Length(str));
{ another.txt }
str := 'Hello, another world!';
utar.Add(f, 'another.txt', Length(str));
BlockWrite(f, str[1], Length(str));
{ close file }
utar.Finish(f);
Close(f);
END; *)
TYPE TAR_FILE = FILE;
(* Find next file. Before call: file position at header start. After, - at begin of content *)
FUNCTION Find(VAR f : TAR_FILE; VAR filename : STRING; VAR filesize : LONGINT) : BOOLEAN;
(* Add one more header for file. After it, you can write content *)
PROCEDURE Add(VAR f : TAR_FILE; filename : STRING; filesize : LONGINT);
(* Finallize, need before close .tar file*)
PROCEDURE Finish(VAR f : TAR_FILE);
IMPLEMENTATION
CONST
TAR_BLOCKSIZE = 512;
TAR_OWNER = 'root';
TAR_USTAR = 'ustar ';
TAR_FILEMODE = '0000640';
TAR_UGID = '0000000';
TAR_DATETIME = '00000000000';
TYPE
TAR_BLOCK = ARRAY[1..TAR_BLOCKSIZE] OF CHAR;
(* ----------------------------------------------------------------------------------- *)
PROCEDURE WriteAlignment(VAR f : TAR_FILE);
VAR buf : TAR_BLOCK;
p : INTEGER;
BEGIN
p := FilePos(f) MOD TAR_BLOCKSIZE;
IF p <> 0 THEN BEGIN
FillChar(buf, TAR_BLOCKSIZE, #0);
BlockWrite(f, buf, TAR_BLOCKSIZE - p);
END;
END;
FUNCTION Int2Oct(i : LONGINT) : STRING;
VAR r : STRING;
c : CHAR;
BEGIN
c := CHR((i mod 8) + ORD('0'));
i := i DIV 8;
IF i <> 0 THEN r := Int2Oct(i) ELSE r := '';
Int2Oct := r + c;
END;
PROCEDURE SetStrMem(VAR buf : TAR_BLOCK; ofs : INTEGER; s : STRING);
BEGIN
Move(s[1], buf[ofs], Length(s));
END;
FUNCTION GetStrMem(VAR buf : TAR_BLOCK; ofs, len : INTEGER) : STRING;
VAR s : STRING;
BEGIN
s := '';
WHILE (len > 0) AND (buf[ofs] <> #0) DO BEGIN
s := s + buf[ofs];
INC(ofs);
DEC(len);
END;
GetStrMem := s;
END;
FUNCTION lpad(s : STRING; len : INTEGER) : STRING;
BEGIN
WHILE Length(s) < len DO s := '0' + s;
lpad := s;
END;
FUNCTION CheckSum(VAR buf : TAR_BLOCK) : INTEGER;
VAR r : INTEGER;
i : INTEGER;
BEGIN
r := 0;
FOR i := 1 TO TAR_BLOCKSIZE DO INC(r, ORD(buf[i]));
CheckSum := r;
END;
(* ----------------------------------------------------------------------------------- *)
FUNCTION Find(VAR f : TAR_FILE; VAR filename : STRING; VAR filesize : LONGINT) : BOOLEAN;
VAR r : BOOLEAN;
buf : TAR_BLOCK;
str : STRING;
i : INTEGER;
p : LONGINT;
BEGIN
r := FALSE;
p := FilePos(f);
i := p MOD TAR_BLOCKSIZE;
IF i <> 0 THEN Seek(f, p + (TAR_BLOCKSIZE - i));
BlockRead(f, buf, TAR_BLOCKSIZE);
IF GetStrMem(buf, 258, 7) = TAR_USTAR THEN BEGIN
filename := GetStrMem(buf, 1, 100);
str := GetStrMem(buf, 125, 12);
filesize := 0;
i := 1;
WHILE (i <= Length(str)) AND (str[i] IN ['0'..'7']) DO BEGIN
filesize := filesize * 8;
INC(filesize, ORD(str[i]) - ORD('0'));
INC(i);
END;
r := TRUE;
END ELSE Seek(f, p);
Find := r;
END;
PROCEDURE Add(VAR f : TAR_FILE; filename : STRING; filesize : LONGINT);
VAR buf : TAR_BLOCK;
BEGIN
FillChar(buf, TAR_BLOCKSIZE, #0);
SetStrMem(buf, 1, filename);
SetStrMem(buf, 125, lpad(Int2Oct(filesize), 11));
SetStrMem(buf, 101, TAR_FILEMODE);
SetStrMem(buf, 109, TAR_UGID);
SetStrMem(buf, 117, TAR_UGID);
SetStrMem(buf, 137, TAR_DATETIME);
SetStrMem(buf, 258, TAR_USTAR);
SetStrMem(buf, 266, TAR_OWNER);
SetStrMem(buf, 298, TAR_OWNER);
FillChar(buf[149], 8, ' ');
SetStrMem(buf, 149, lpad(Int2Oct(CheckSum(buf)), 8));
WriteAlignment(f);
BlockWrite(f, buf, TAR_BLOCKSIZE);
END;
PROCEDURE Finish(VAR f : TAR_FILE);
VAR buf : TAR_BLOCK;
BEGIN
FillChar(buf, TAR_BLOCKSIZE, #0);
WriteAlignment(f);
BlockWrite(f, buf, TAR_BLOCKSIZE);
BlockWrite(f, buf, TAR_BLOCKSIZE);
END;
END.