Debian Codegraph @

plugins/wcx/rpm/src/rpm_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 //***************************************************************
8 // This code based on Christian Ghisler ([email protected]) sources
9 //***************************************************************
10
11 //***************************************************************
12 // This code was improved by Sergio Daniel Freue ([email protected])
13 //***************************************************************
14
15 {$A-,I-}
16 unit rpm_io;
17
18 interface
19
20 uses
21 SysUtils,
22 rpm_def;
23
24 type
25 TStrBuf = array[1..260] of Char;
26
27 function RPM_ReadLead(var f : file; var lead : RPM_Lead) : Boolean;
28 function RPM_ReadSignature(var f : file; sig_type : Word; var signature : RPM_Header) : Boolean;
29 function RPM_ReadHeader(var f : file; align_data : Boolean; var header : RPM_Header; var info : RPM_InfoRec; var deps : RPM_DepsRec) : Boolean;
30 function RPM_ReadEntry(var f : file; data_start : LongInt; var entry : RPM_EntryInfo) : Boolean;
31 function RPM_ProcessEntry(var f : file; data_start : LongInt; var entry : RPM_EntryInfo; var info : RPM_InfoRec; var deps : RPM_DepsRec) : Boolean;
32
33 procedure swap_value(var value; size : Integer);
34 procedure copy_str2buf(var buf : TStrBuf; s : AnsiString);
35 function get_archivename(var fname : String;datasig:RPM_DataSig) : String;
36 function read_string(var f : file; var s : AnsiString) : Boolean;
37 function read_int32(var f : file; var int32 : LongWord) : Boolean;
38
39 implementation
40
41 uses
42 Classes;
43
44 procedure swap_value(var value; size:Integer);
45 type
46 byte_array = array[1..MaxInt] of Byte;
47 var
48 i : Integer;
49 avalue : Byte;
50 begin
51 for i:=1 to size div 2 do
52 begin
53 avalue := byte_array(value)[i];
54 byte_array(value)[i] := byte_array(value)[size + 1 - i];
55 byte_array(value)[size + 1 - i] := avalue;
56 end;
57 end;
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 get_archivename(var fname : String;datasig:RPM_DataSig) : String;
73 var
74 tmp_str : String;
75 i_char : Integer;
76 fgFound : Boolean;
77 begin
78 tmp_str := ExtractFileName(fname);
79 fgFound := False;
80 for i_char := Length(tmp_str) downto 1 do
81 if tmp_str[i_char] = '.' then begin
82 fgFound := True;
83 Break;
84 end;
85 if fgFound then
86 SetLength(tmp_str, i_char - 1);
87 if (datasig[0] = #031) and (datasig[1] = #139) then
88 tmp_str := tmp_str + '.cpio.gz'
89 else if (datasig[0]='B') and (datasig[1]='Z') and (datasig[2]='h') then
90 tmp_str := tmp_str + '.cpio.bz2'
91 else if CompareByte(datasig, #253'7zXZ'#000, 6) = 0 then
92 tmp_str := tmp_str + '.cpio.xz'
93 else if CompareByte(datasig, #$28#$B5#$2F#$FD, 4) = 0 then
94 tmp_str := tmp_str + '.cpio.zst'
95 else
96 tmp_str := tmp_str + '.cpio.lzma';
97 Result := tmp_str;
98 end;
99
100 function RPM_ReadLead;
101 begin
102 Result := False;
103 BlockRead(f, lead, Sizeof(Lead));
104 if IOResult = 0 then Result := True;
105 with lead do begin
106 swap_value(rpmtype, 2);
107 swap_value(archnum, 2);
108 swap_value(osnum, 2);
109 swap_value(signature_type, 2);
110 end;
111 end;
112
113 function RPM_ReadHeader;
114 var
115 i_entry : LongWord;
116 start : Integer;
117 entry : RPM_EntryInfo;
118 begin
119 Result := False;
120 BlockRead(f, header, Sizeof(header));
121 if IOResult = 0 then begin
122 with header do begin
123 swap_value(count, 4);
124 swap_value(data_size, 4);
125 start := FilePos(f) + LongInt(count) * Sizeof(entry);
126 for i_entry := 0 to count - 1 do begin
127 if not RPM_ReadEntry(f, start, entry) then Exit
128 else
129 if not RPM_ProcessEntry(f, start, entry, info, deps) then Exit;
130 end;
131 end;
132 start := start + LongInt(header.data_size);
133 // Move file pointer on padded to a multiple of 8 bytes position
134 if align_data then
135 if (start mod 8) <> 0 then begin
136 start := start and $FFFFFFF8;
137 Inc(start, 8);
138 end;
139 Seek(f, start);
140 Result := True;
141 end;
142 end;
143
144 function RPM_ReadEntry;
145 begin
146 Result := False;
147 BlockRead(f, entry, Sizeof(entry));
148 if IOResult = 0 then Result := True;
149 with entry do begin
150 swap_value(tag, 4);
151 swap_value(etype, 4);
152 swap_value(offset, 4);
153 offset := data_start + LongInt(offset);
154 swap_value(count, 4);
155 end;
156 end;
157
158 function RPM_ReadSignature;
159 var
160 info : RPM_InfoRec;
161 deps : RPM_DepsRec;
162 begin
163 Result := False;
164 case sig_type of
165 RPMSIG_PGP262_1024 : ; // Old PGP signature
166 RPMSIG_MD5 : ; //
167 RPMSIG_MD5_PGP : ; //
168 RPMSIG_HEADERSIG : // New header signature
169 begin
170 if RPM_ReadHeader(f, True, signature, info, deps) then Result := True;
171 end;
172 end;{case signature type}
173 end;
174
175 procedure CRtoCRLF(var instr:string);
176 var s:string;
177 i,l:integer;
178 ch,ch2:char;
179 begin
180 s := '';
181 instr:=instr+' '; {Avoid overflow}
182 l:=length(instr)-1;
183 for i:=1 to l do begin
184 ch:=instr[i];
185 ch2:=instr[i+1];
186 if ((ch=#13) and (ch2<>#10)) or ((ch=#10) and (ch2<>#13)) then
187 s:=s+#13#10
188 else
189 s:=s+ch;
190 end;
191 instr:=s;
192 end;
193
194 function RPM_ProcessEntry;
195 var
196 save_pos : Integer;
197 fgError : Boolean;
198 i : Integer;
199 s : String;
200 begin
201 result:=true;
202 if entry.tag = RPMTAG_FILENAMES then exit;
203 fgError := False;
204 save_pos := FilePos(f);
205 Seek(f, entry.offset);
206 if IOResult = 0 then begin
207 case entry.tag of
208 RPMTAG_NAME :
209 if entry.etype = 6 then fgError := not read_string(f, info.name);
210 RPMTAG_VERSION :
211
212 if entry.etype = 6 then fgError := not read_string(f, info.version);
213
214 RPMTAG_RELEASE :
215
216 if entry.etype = 6 then fgError := not read_string(f, info.release);
217
218 RPMTAG_SUMMARY :
219
220 if entry.etype = 9 then fgError := not read_string(f, info.summary);
221
222 RPMTAG_DESCRIPTION :
223
224 if entry.etype = 9 then begin
225
226 fgError := not read_string(f, info.description);
227
228 if not fgError then
229
230 CRtoCRLF(info.description);
231
232 end;
233
234 RPMTAG_BUILDTIME :
235
236 if entry.etype = 4 then fgError := not read_int32(f, info.buildtime);
237
238 RPMTAG_DISTRIBUTION :
239
240 if entry.etype = 6 then fgError := not read_string(f, info.distribution);
241
242 RPMTAG_VENDOR :
243
244 if entry.etype = 6 then fgError := not read_string(f, info.vendor);
245
246 RPMTAG_LICENSE :
247
248 if entry.etype = 6 then fgError := not read_string(f, info.license);
249
250 RPMTAG_PACKAGER :
251
252 if entry.etype = 6 then fgError := not read_string(f, info.packager);
253
254 RPMTAG_GROUP :
255
256 if entry.etype = 9 then fgError := not read_string(f, info.group);
257
258 RPMTAG_OS :
259
260 if entry.etype = 6 then fgError := not read_string(f, info.os);
261
262 RPMTAG_ARCH :
263
264 if entry.etype = 6 then fgError := not read_string(f, info.arch);
265
266 RPMTAG_SOURCERPM :
267
268 if entry.etype = 6 then fgError := not read_string(f, info.sourcerpm);
269
270 RPMTAG_REQUIRENAME:
271 if entry.etype = 8 then
272 begin
273 SetLength(deps.names, entry.Count);
274 for i := 0 to entry.Count - 1 do
275 begin
276 read_string(f, s);
277 deps.names[i] := s;
278 end;
279 end;
280
281 end;{case}
282
283 end
284 else fgError := True;
285 Result := not fgError;
286 Seek(f, save_pos);
287 end;
288
289 function read_string(var f : file; var s : AnsiString) : Boolean;
290 var
291 i_char : Char;
292 fgError : Boolean;
293 begin
294 fgError := False;
295 SetLength(s, 0);
296 while not eof(f) do begin
297 BlockRead(f, i_char, 1);
298 if IOResult <> 0 then begin
299 fgError := True;
300 Break;
301 end;
302 if i_char = #0
303 then Break
304 else s := s + i_char;
305 end;
306 Result := not fgError;
307 end;
308
309 function read_int32(var f : file; var int32 : LongWord) : Boolean;
310 begin
311 BlockRead(f, int32, Sizeof(LongWord));
312 swap_value(int32, Sizeof(LongWord));
313 if IOResult = 0 then Result := True else Result := False;
314 end;
315
316 procedure RPM_CreateInfoRec(var info : RPM_InfoRec);
317 begin
318 end;
319
320 procedure RPM_DeleteInfoRec(var info : RPM_InfoRec);
321 begin
322 end;
323
324 end.