Debian Codegraph @

plugins/wcx/cpio/src/cpio_io.pas

raw
1 //***************************************************************
2 // This file is part of RPMWCX, a archiver plugin for
3 // Windows Commander.
4 // Copyright (C) 2000 Mandryka Yurij e-mail:[email protected]
5 //***************************************************************
6 {
7 Add some changes for Lazarus and Linux compability
8 Copyright (C) 2007 Koblov Alexander ([email protected])
9 }
10 //***************************************************************
11 // Part of code (functions DirectoryExists and ForceDirectories)
12 // got from Delphi source code
13 //***************************************************************
14
15 //***************************************************************
16 // This code based on Christian Ghisler ([email protected]) sources
17 //***************************************************************
18
19 {$A-,I-}
20 unit cpio_io;
21
22 interface
23
24 uses
25 cpio_def, Classes;
26
27 type
28 TStrBuf = array[1..260] of Char;
29
30 function CPIO_ReadHeader(var f : file; var header : CPIO_Header) : Boolean;
31 function IsCPIOArchive(FileName: String): Boolean;
32
33 function AlignFilePointer(var f : file; align : Integer) : Boolean;
34 procedure copy_str2buf(var buf : TStrBuf; s : AnsiString);
35 function CreateDirectories(Dir : String) : Boolean;
36 function correct_filename(oldname : AnsiString) : AnsiString;
37
38 implementation
39
40 uses
41 SysUtils;
42
43
44 {$IFNDEF FPC} // for compiling under Delphi
45 Const
46 DirSeparators : set of char = ['/','\'];
47
48 Procedure DoDirSeparators (Var FileName : String);
49
50 VAr I : longint;
51
52 begin
53 For I:=1 to Length(FileName) do
54 If FileName[I] in DirSeparators then
55 FileName[i]:=PathDelim;
56 end;
57 {$ENDIF}
58
59 procedure copy_str2buf(var buf : TStrBuf; s : AnsiString);
60 var
61 i_char : Integer;
62 begin
63 FillChar(buf, Sizeof(buf), 0);
64 if Length(s) = 0 then Exit;
65 if Length(s) > 259 then
66 SetLength(s, 259);
67 s := s + #0;
68 for i_char := 1 to Length(s) do
69 buf[i_char] := s[i_char];
70 end;
71
72 function AlignFilePointer;
73 var
74 start : Integer;
75 mul : LongWord;
76 begin
77 Result := False;
78 start := FilePos(f);
79 case align of
80 2 : mul := $FFFFFFFE;
81 4 : mul := $FFFFFFFC;
82 8 : mul := $FFFFFFF8;
83 else
84 Exit;
85 end;{case}
86 if (start mod align) <> 0 then begin
87 start := start and mul;
88 Inc(start, align);
89 end;
90 Seek(f, start);
91 if IOResult = 0 then Result := True;
92 end;
93
94 function ExcludeTrailingBackslash(Dir:string):string;
95 begin
96 if (length(dir)>0) and (dir[length(dir)]='\') then
97 result:=copy(dir,1,length(dir)-1)
98 else
99 result:=dir;
100 end;
101
102 function CreateDirectories(Dir : String) : Boolean;
103 begin
104 Result := True;
105 if Length(Dir) = 0 then
106 Result := False
107 else begin
108 Dir := ExcludeTrailingBackslash(Dir);
109 if (Length(Dir) < 3) or DirectoryExists(Dir)
110 or (ExtractFilePath(Dir) = Dir) then Exit;
111 Result := CreateDirectories(ExtractFilePath(Dir)) and CreateDir(Dir);
112 end;
113 end;
114
115 function OctalToDec(Octal: String): Longword;
116 var
117 i: Integer;
118 begin
119 Result := 0;
120
121 for i := 1 to Length(Octal) do
122 begin
123 Result := Result shl 3;
124 case Octal[i] of
125 '0'..'7':
126 Result := Result + Longword(Ord(Octal[i]) - Ord('0'));
127 end;
128 end;
129 end;
130
131 function HexToDec(Hex: String): Longword;
132 var
133 i: Integer;
134 begin
135 Result := 0;
136 for i := 1 to Length(Hex) do
137 begin
138 Result := Result shl 4;
139 case Hex[i] of
140 '0'..'9':
141 Result := Result + LongWord(Ord(Hex[i]) - Ord('0'));
142 'A'..'F':
143 Result := Result + LongWord(Ord(Hex[i]) - Ord('A')) + 10;
144 'a'..'f':
145 Result := Result + LongWord(Ord(Hex[i]) - Ord('a')) + 10;
146 end;
147 end;
148 end;
149
150 function CPIO_ReadHeader(var f : file; var header : CPIO_Header): Boolean;
151 var
152 Buffer : array [0..259] of AnsiChar;
153 OldHdr : TOldBinaryHeader absolute Buffer;
154 OdcHdr : TOldCharHeader absolute Buffer;
155 NewHdr : TNewCharHeader absolute Buffer;
156 ofs : Integer;
157 begin
158 Result := False;
159
160 {First, check the type of header}
161 BlockRead(f, Buffer[0], 6);
162 if IOResult <> 0 then Exit;
163 header.IsOldHeader := False;
164
165 // Old binary format.
166 if PWord(@Buffer[0])^ = $71C7 then
167 begin
168 header.IsOldHeader := True;
169 BlockRead(f, Buffer[6], SizeOf(TOldBinaryHeader) - 6);
170 if IOResult <> 0 then Exit;
171 with header, OldHdr do
172 begin
173 magic := c_magic;
174 dev_major := c_dev;
175 dev_minor := 0;
176 inode := c_ino;
177 mode := c_mode;
178 uid := c_uid;
179 gid := c_gid;
180 nlink := c_nlink;
181 mtime := 65536 * c_mtime1 + c_mtime2;
182 filesize := 65536 * c_filesize1 + c_filesize2;
183 namesize := c_namesize;
184 end;
185 end
186
187 // Old Ascii format.
188 else if strlcomp(Buffer, '070707', 6) = 0 then
189 begin
190 BlockRead(f, Buffer[6], SizeOf(TOldCharHeader) - 6);
191 if IOResult <> 0 then Exit;
192 with header, OdcHdr do
193 begin
194 magic := OctalToDec(c_magic);
195 dev_major := OctalToDec(c_dev);
196 dev_minor := 0;
197 inode := OctalToDec(c_ino);
198 mode := OctalToDec(c_mode);
199 uid := OctalToDec(c_uid);
200 gid := OctalToDec(c_gid);
201 nlink := OctalToDec(c_nlink);
202 mtime := OctalToDec(c_mtime);
203 filesize := OctalToDec(c_filesize);
204 namesize := OctalToDec(c_namesize);
205 end;
206 end
207
208 // New Ascii format.
209 else if (strlcomp(Buffer, '070701', 6) = 0) or
210 (strlcomp(Buffer, '070702', 6) = 0) then
211 begin
212 BlockRead(f, Buffer[6], SizeOf(TNewCharHeader) - 6);
213 if IOResult <> 0 then Exit;
214 with header, NewHdr do
215 begin
216 magic := HexToDec(c_magic);
217 dev_major := HexToDec(c_devmajor);
218 dev_minor := HexToDec(c_devminor);
219 inode := HexToDec(c_ino);
220 mode := HexToDec(c_mode);
221 uid := HexToDec(c_uid);
222 gid := HexToDec(c_gid);
223 nlink := HexToDec(c_nlink);
224 mtime := HexToDec(c_mtime);
225 filesize := HexToDec(c_filesize);
226 namesize := HexToDec(c_namesize);
227 end;
228 end
229 else
230 Exit;
231
232 with header do
233 begin
234 if namesize = 0 then exit; {Error!}
235 {Read name}
236 ofs:=0;
237 if namesize > 259 then
238 begin
239 ofs := namesize - 259;
240 namesize := 259;
241 end;
242 FillChar(Buffer, SizeOf(Buffer), #0);
243 BlockRead(f, Buffer, namesize);
244 if IOResult <> 0 then Exit;
245 SetString(filename, Buffer, namesize);
246 if ofs <> 0 then
247 Seek(f, FilePos(f) + ofs);
248 origname := filename;
249 DoDirSeparators(filename);
250 if IsOldHeader then begin
251 if not AlignFilePointer(f, 2) then Exit;
252 end else
253 if not AlignFilePointer(f, 4) then Exit;
254
255 //Correct file name started with "./" or "/"
256 filename := correct_filename(filename);
257 end;
258
259 Result := True;
260 end;
261
262 function IsCPIOArchive(FileName: String): Boolean;
263 type
264 TAsciiHeader = array[0..5] of AnsiChar;
265 const
266 sOld: TAsciiHeader = ('0', '7', '0', '7', '0', '7');
267 sNew: TAsciiHeader = ('0', '7', '0', '7', '0', '1');
268 sCrc: TAsciiHeader = ('0', '7', '0', '7', '0', '2');
269 var
270 Buf: TAsciiHeader;
271 Stream: TFileStream;
272 begin
273 Result := False;
274 Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
275 try
276 if (Stream.Size >= 6) and (Stream.Read(Buf[0], 6) = 6) then
277 begin
278 Result := // Binary format
279 (PWord(@Buf[0])^ = $71C7) or
280 // Ascii formats
281 CompareMem(@Buf[0], @sOld[0], 6) or
282 CompareMem(@Buf[0], @sNew[0], 6) or
283 CompareMem(@Buf[0], @sCrc[0], 6);
284 end;
285 finally
286 Stream.Free;
287 end;
288 end;
289
290 function correct_filename(oldname : AnsiString) : AnsiString;
291 begin
292 Result := oldname;
293 if Length(oldname) > 1 then begin
294 case oldname[1] of
295 '.' :
296 case oldname[2] of
297 '/', '\' : System.Delete(oldname, 1, 2);
298 end;{case}
299 '/', '\' : System.Delete(oldname, 1, 1);
300 end;{case}
301 end;
302 Result := oldname;
303 end;
304
305 end.