-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuMemReader.pas
171 lines (140 loc) · 3.53 KB
/
uMemReader.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
{
******************************************************
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 uMemReader;
interface
uses
Classes, SysUtils;
type
TExplorerMemoryStream = class (TMemoryStream)
private
public
function ReadByte: byte;
function ReadWord: word;
function ReadWordBE: word;
function ReadDWord: longword;
function ReadDWordBE: longword;
function ReadBlockName: string;
function ReadBuffer(var Buffer; Count: longint): longint;
function ReadString(Length: integer): string; inline;
function ReadAnsiString(Length: integer): ansistring; inline;
function ReadStringAlt(Length: integer): string; inline;
function FindFileHeader(Header: string; StartSearchAt, EndSearchAt: cardinal): longint;
constructor Create;
destructor Destroy; override;
end;
implementation
function TExplorerMemoryStream.ReadByte: byte;
begin
Read(result,1);
end;
function TExplorerMemoryStream.ReadWord: word;
begin
Read(result,2);
end;
function TExplorerMemoryStream.ReadWordBE: word;
begin
result:=ReadByte shl 8
+ReadByte;
end;
function TExplorerMemoryStream.ReadDWord: longword;
begin
Read(result,4);
end;
function TExplorerMemoryStream.ReadDWordBE: longword;
begin
result:=ReadByte shl 24
+ReadByte shl 16
+ReadByte shl 8
+ReadByte;
end;
function TExplorerMemoryStream.ReadBlockName: string;
begin
result:=chr(ReadByte)+chr(ReadByte)+chr(ReadByte)+chr(ReadByte);
end;
function TExplorerMemoryStream.ReadBuffer(var Buffer; Count:longint): longint;
begin
result:=Read(Buffer,Count);
end;
function TExplorerMemoryStream.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 TExplorerMemoryStream.ReadAnsiString(Length: integer): ansistring;
var
n: longword;
begin
SetLength(result,length);
for n:=1 to length do
begin
result[n]:=Ansichar(Chr(ReadByte));
end;
end;
function TExplorerMemoryStream.ReadStringAlt(Length: integer): string;
var //Replaces #0 chars
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;
constructor TExplorerMemoryStream.Create;
begin
inherited Create;
end;
destructor TExplorerMemoryStream.Destroy;
begin
inherited;
end;
function TExplorerMemoryStream.FindFileHeader(Header: string; StartSearchAt,
EndSearchAt: cardinal): longint;
var
HeaderLength, Index: integer;
begin
Result:=-1;
Index:=1;
if EndSearchAt > self.Size then
EndSearchAt:=self.Size;
HeaderLength:=Length(Header);
if HeaderLength <= 0 then exit;
Self.Position:=StartSearchAt;
while Self.Position < EndSearchAt do
begin
if Chr(ReadByte) <> Header[Index] then
begin
if Index > 1 then
Self.Position := Self.Position -1;
Index:=1;
continue;
end;
inc(Index);
if index > HeaderLength then
begin
Result:=Self.Position - HeaderLength;
exit;
end;
end;
end;
end.