-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuFileReader.pas
177 lines (152 loc) · 3.79 KB
/
uFileReader.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
{
******************************************************
DoubleFine Explorer
By Bennyboy
Http://quickandeasysoftware.net
******************************************************
}
{
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
}
unit uFileReader;
interface
uses
Classes, SysUtils;
type
TExplorerFileStream = class (TFileStream)
private
fBigEndian: boolean;
function Swap8 (const i: uInt64): uInt64; register;
procedure setBigEndian(const Value: boolean);
public
function ReadByte: byte; inline;
function ReadWord: word; inline;
function ReadWordBE: word; inline;
function ReadDWord: longword; inline;
function ReadDWordLE: longword; inline;
function ReadDWordBE: longword; inline;
function ReadQWord: uint64; inline;
function ReadQWordBE: uint64; inline;
function ReadTriByte: longword; inline;
function ReadTriByteBE: longword; inline;
function ReadBlockName: string; inline;
function ReadString(Length: integer): string; inline;
function ReadStringAlt(Length: integer): string; inline;
constructor Create(FileName: string);
destructor Destroy; override;
property BigEndian: boolean read fBigEndian write setBigEndian;
end;
implementation
function TExplorerFileStream.ReadByte: byte;
begin
Read(result,1);
end;
function TExplorerFileStream.ReadWord: word;
begin
if fBigEndian then
result :=ReadWordBE
else
Read(result,2);
end;
function TExplorerFileStream.ReadWordBE: word;
begin
result:=ReadByte shl 8
+ReadByte;
end;
function TExplorerFileStream.ReadDWord: longword;
begin
if fBigEndian then
result :=ReadDWordBE
else
Read(result,4);
end;
function TExplorerFileStream.ReadDWordBE: longword;
begin
result:=ReadByte shl 24
+ReadByte shl 16
+ReadByte shl 8
+ReadByte;
end;
function TExplorerFileStream.ReadDWordLE: longword;
begin
Read(result,4);
end;
function TExplorerFileStream.ReadQWord: uint64;
begin
if fBigEndian then
result := ReadQWordBE
else
Read(result,8);
end;
function TExplorerFileStream.Swap8(const i: uInt64): uInt64;
asm
mov edx, dword [i]
bswap edx
mov eax, dword [i+4]
bswap eax
end;
function TExplorerFileStream.ReadQWordBE: uint64;
var
i: uint64;
begin
read(i, 8);
result:= Swap8(i);
end;
function TExplorerFileStream.ReadBlockName: string;
begin
result:=chr(ReadByte)+chr(ReadByte)+chr(ReadByte)+chr(ReadByte);
end;
function TExplorerFileStream.ReadString(Length: integer): string;
var
n: longword;
begin
SetLength(result,length);
for n:=1 to length do
begin
result[n]:=Chr(ReadByte);
end;
end;
function TExplorerFileStream.ReadStringAlt(Length: integer): string;
var //Replaces #0 chars with character
n: longword;
Rchar: char;
begin
SetLength(result,length);
for n:=0 to length -1 do
begin
RChar:=Chr(ReadByte);
if RChar=#0 then
result[n]:='x'
else
result[n]:=rchar;
end;
end;
function TExplorerFileStream.ReadTriByte: longword;
begin
if fBigEndian then
result :=ReadTriByteBE
else
Read(result,3);
end;
function TExplorerFileStream.ReadTriByteBE: longword;
begin
result:=ReadByte shl 16
+ReadByte shl 8
+ReadByte;
end;
procedure TExplorerFileStream.setBigEndian(const Value: boolean);
begin
fBigEndian := Value;
end;
constructor TExplorerFileStream.Create(FileName: string);
begin
inherited Create(Filename, fmopenread OR fmShareDenyNone);
fBigEndian := false;
end;
destructor TExplorerFileStream.Destroy;
begin
inherited;
end;
end.