Debian Codegraph @

plugins/wdx/rpm_wdx/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 {$MODE Delphi}
19
20 interface
21
22 uses
23 SysUtils,
24 rpm_def;
25
26 type
27 TStrBuf = array[1..260] of Char;
28
29 function RPM_ReadLead(var f : file; var lead : RPM_Lead) : Boolean;
30 function RPM_ReadSignature(var f : file; sig_type : Word; var signature : RPM_Header) : Boolean;
31 function RPM_ReadHeader(var f : file; align_data : Boolean; var header : RPM_Header; var info : RPM_InfoRec) : Boolean;
32 function RPM_ReadEntry(var f : file; data_start : LongInt; var entry : RPM_EntryInfo) : Boolean;
33 function RPM_ProcessEntry(var f : file; data_start : LongInt; var entry : RPM_EntryInfo; var info : RPM_InfoRec) : Boolean;
34
35 procedure swap_value(var value; size : Integer);
36 procedure copy_str2buf(var buf : TStrBuf; s : AnsiString);
37 function get_archivename(var fname : String;is_bz2file:boolean) : String;
38 function read_string(var f : file; var s : AnsiString) : Boolean;
39 function read_int32(var f : file; var int32 : LongWord) : Boolean;
40
41 implementation
42
43 uses
44 Classes;
45
46 procedure swap_value(var value; size:Integer);
47 type
48 byte_array = array[1..MaxInt] of Byte;
49 var
50 i : Integer;
51 avalue : Byte;
52 begin
53 for i:=1 to size div 2 do
54 begin
55 avalue := byte_array(value)[i];
56 byte_array(value)[i] := byte_array(value)[size + 1 - i];
57 byte_array(value)[size + 1 - i] := avalue;
58 end;
59 end;
60
61 procedure copy_str2buf(var buf : TStrBuf; s : AnsiString);
62 var
63 i_char : Integer;
64 begin
65 FillChar(buf, Sizeof(buf), 0);
66 if Length(s) = 0 then Exit;
67 if Length(s) > 259 then
68 SetLength(s, 259);
69 s := s + #0;
70 for i_char := 1 to Length(s) do
71 buf[i_char] := s[i_char];
72 end;
73
74 function get_archivename(var fname : String;is_bz2file:boolean) : String;
75 var
76 tmp_str : String;
77 i_char : Integer;
78 fgFound : Boolean;
79 begin
80 tmp_str := ExtractFileName(fname);
81 fgFound := False;
82 for i_char := Length(tmp_str) downto 1 do
83 if tmp_str[i_char] = '.' then begin
84 fgFound := True;
85 Break;
86 end;
87 if fgFound then
88 SetLength(tmp_str, i_char - 1);
89 if is_bz2file then
90 tmp_str := tmp_str + '.cpio.bz2'
91 else
92 tmp_str := tmp_str + '.cpio.gz';
93 Result := tmp_str;
94 end;
95
96 function RPM_ReadLead;
97 begin
98 Result := False;
99 BlockRead(f, lead, Sizeof(Lead));
100 if IOResult = 0 then Result := True;
101 with lead do begin
102 swap_value(rpmtype, 2);
103 swap_value(archnum, 2);
104 swap_value(osnum, 2);
105 swap_value(signature_type, 2);
106 end;
107 end;
108
109 function RPM_ReadHeader;
110 var
111 i_entry : LongWord;
112 start : Integer;
113 entry : RPM_EntryInfo;
114 begin
115 Result := False;
116 BlockRead(f, header, Sizeof(header));
117 if IOResult = 0 then begin
118 with header do begin
119 swap_value(count, 4);
120 swap_value(data_size, 4);
121 start := FilePos(f) + LongInt(count) * Sizeof(entry);
122 for i_entry := 0 to count - 1 do begin
123 if not RPM_ReadEntry(f, start, entry) then Exit
124 else
125 if not RPM_ProcessEntry(f, start, entry, info) then Exit;
126 end;
127 end;
128 start := start + LongInt(header.data_size);
129 // Move file pointer on padded to a multiple of 8 bytes position
130 if align_data then
131 if (start mod 8) <> 0 then begin
132 start := start and $FFFFFFF8;
133 Inc(start, 8);
134 end;
135 Seek(f, start);
136 Result := True;
137 end;
138 end;
139
140 function RPM_ReadEntry;
141 begin
142 Result := False;
143 BlockRead(f, entry, Sizeof(entry));
144 if IOResult = 0 then Result := True;
145 with entry do begin
146 swap_value(tag, 4);
147 swap_value(etype, 4);
148 swap_value(offset, 4);
149 offset := data_start + LongInt(offset);
150 swap_value(count, 4);
151 end;
152 end;
153
154 function RPM_ReadSignature;
155 var
156 info : RPM_InfoRec;
157 begin
158 Result := False;
159 case sig_type of
160 RPMSIG_PGP262_1024 : ; // Old PGP signature
161 RPMSIG_MD5 : ; //
162 RPMSIG_MD5_PGP : ; //
163 RPMSIG_HEADERSIG : // New header signature
164 begin
165 if RPM_ReadHeader(f, True, signature, info) then Result := True;
166 end;
167 end;{case signature type}
168 end;
169
170 procedure CRtoCRLF(var instr:string);
171 var s:string;
172 i,l:integer;
173 ch,ch2:char;
174 begin
175 instr:=instr+' '; {Avoid overflow}
176 l:=length(instr)-1;
177 for i:=1 to l do begin
178 ch:=instr[i];
179 ch2:=instr[i+1];
180 if ((ch=#13) and (ch2<>#10)) or ((ch=#10) and (ch2<>#13)) then
181 s:=s+#13#10
182 else
183 s:=s+ch;
184 end;
185 instr:=s;
186 end;
187
188 function RPM_ProcessEntry;
189 var
190 save_pos : Integer;
191 fgError : Boolean;
192 begin
193 result:=true;
194 if entry.tag = RPMTAG_FILENAMES then exit;
195 fgError := False;
196 save_pos := FilePos(f);
197 Seek(f, entry.offset);
198 if IOResult = 0 then begin
199 case entry.tag of
200 RPMTAG_NAME :
201 if entry.etype = 6 then fgError := not read_string(f, info.name);
202 RPMTAG_VERSION :
203 if entry.etype = 6 then fgError := not read_string(f, info.version);
204 RPMTAG_RELEASE :
205 if entry.etype = 6 then fgError := not read_string(f, info.release);
206 RPMTAG_SUMMARY :
207 if entry.etype = 9 then fgError := not read_string(f, info.summary);
208 RPMTAG_DESCRIPTION :
209 if entry.etype = 9 then begin
210 fgError := not read_string(f, info.description);
211 if not fgError then
212 CRtoCRLF(info.description);
213 end;
214 RPMTAG_BUILDTIME :
215 if entry.etype = 4 then fgError := not read_int32(f, info.buildtime);
216 RPMTAG_DISTRIBUTION :
217 if entry.etype = 6 then fgError := not read_string(f, info.distribution);
218 RPMTAG_VENDOR :
219 if entry.etype = 6 then fgError := not read_string(f, info.vendor);
220 RPMTAG_LICENSE :
221 if entry.etype = 6 then fgError := not read_string(f, info.license);
222 RPMTAG_PACKAGER :
223 if entry.etype = 6 then fgError := not read_string(f, info.packager);
224 RPMTAG_GROUP :
225 if entry.etype = 9 then fgError := not read_string(f, info.group);
226 RPMTAG_OS :
227 if entry.etype = 6 then fgError := not read_string(f, info.os);
228 RPMTAG_ARCH :
229 if entry.etype = 6 then fgError := not read_string(f, info.arch);
230 RPMTAG_SOURCERPM :
231 if entry.etype = 6 then fgError := not read_string(f, info.sourcerpm);
232 end;{case}
233 end
234 else fgError := True;
235 Result := not fgError;
236 Seek(f, save_pos);
237 end;
238
239 function read_string(var f : file; var s : AnsiString) : Boolean;
240 var
241 i_char : Char;
242 fgError : Boolean;
243 begin
244 fgError := False;
245 SetLength(s, 0);
246 while not eof(f) do begin
247 BlockRead(f, i_char, 1);
248 if IOResult <> 0 then begin
249 fgError := True;
250 Break;
251 end;
252 if i_char = #0
253 then Break
254 else s := s + i_char;
255 end;
256 Result := not fgError;
257 end;
258
259 function read_int32(var f : file; var int32 : LongWord) : Boolean;
260 begin
261 BlockRead(f, int32, Sizeof(LongWord));
262 swap_value(int32, Sizeof(LongWord));
263 if IOResult = 0 then Result := True else Result := False;
264 end;
265
266 procedure RPM_CreateInfoRec(var info : RPM_InfoRec);
267 begin
268 end;
269
270 procedure RPM_DeleteInfoRec(var info : RPM_InfoRec);
271 begin
272 end;
273
274 end.