-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
StaticMemoryStream.pas
267 lines (214 loc) · 9.29 KB
/
StaticMemoryStream.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
{-------------------------------------------------------------------------------
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/.
-------------------------------------------------------------------------------}
{===============================================================================
Static memory stream
Very simple classes intended to provide a way of accessing general memory
location using usual streams.
They are designed to be safe for use on pointers from external sources
(libraries, OS, ...).
Version 1.1.2 (2024-04-14)
Last change 2024-04-14
©2017-2024 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.StaticMemoryStream
Dependencies:
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
StrRect - github.com/TheLazyTomcat/Lib.StrRect
Library AuxExceptions is required only when rebasing local exception classes
(see symbol StaticMemoryStream_UseAuxExceptions for details).
Indirect dependencies:
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit StaticMemoryStream;
{
StaticMemoryStream_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
StaticMemoryStream_UseAuxExceptions to achieve this.
}
{$IF Defined(StaticMemoryStream_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IFDEF FPC}
{$MODE ObjFPC}
{$MODESWITCH DuplicateLocals+}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes,
AuxTypes{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
type
ESMSException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ESMSCannotWrite = class(ESMSException);
{===============================================================================
--------------------------------------------------------------------------------
TStaticMemoryStream
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TStaticMemoryStream - declaration
===============================================================================}
type
TStaticMemoryStream = class(TCustomMemoryStream)
protected
Function GetAddress: Pointer; virtual;
procedure SetAddress(Value: Pointer); virtual;
public
constructor Create(Memory: Pointer; Size: TMemSize); overload;
Function Write(const Buffer; Count: LongInt): LongInt; override;
procedure LoadFromStream(Stream: TStream); virtual;
procedure LoadFromFile(const FileName: String); virtual;
property Address: Pointer read GetAddress write SetAddress;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWritableStaticMemoryStream
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TWritableStaticMemoryStream - declaration
===============================================================================}
TWritableStaticMemoryStream = class(TStaticMemoryStream)
public
Function Write(const Buffer; Count: LongInt): LongInt; override;
{
LoadFromStream loads as many bytes as can fit into the internal static
memory.
If less bytes is loaded than is the size of internal memory, then any bytes
in the internal memory above this count are left unchanged.
}
procedure LoadFromStream(Stream: TStream); override;
end;
implementation
uses
StrRect;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TStaticMemoryStream
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TStaticMemoryStream - implementation
===============================================================================}
{-------------------------------------------------------------------------------
TStaticMemoryStream - protected methods
-------------------------------------------------------------------------------}
Function TStaticMemoryStream.GetAddress: Pointer;
begin
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
Result := Pointer(PtrUInt(Memory) + PtrUInt(Position));
{$IFDEF FPCDWM}{$POP}{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure TStaticMemoryStream.SetAddress(Value: Pointer);
begin
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
If PtrUInt(Value) <= PtrUInt(Memory) then
Seek(0,soBeginning)
else If PtrUInt(Value) >= (PtrUInt(Memory) + PtrUInt(Size)) then
Seek(0,soEnd)
else
Seek(Int64(PtrUInt(Value) - PtrUInt(Memory)),soBeginning);
{$IFDEF FPCDWM}{$POP}{$ENDIF}
end;
{-------------------------------------------------------------------------------
TStaticMemoryStream - public methods
-------------------------------------------------------------------------------}
constructor TStaticMemoryStream.Create(Memory: Pointer; Size: TMemSize);
begin
inherited Create;
SetPointer(Memory,Size);
Position := 0;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
Function TStaticMemoryStream.Write(const Buffer; Count: LongInt): LongInt;
begin
{$IFDEF FPC}
Result := 0;
{$ENDIF}
raise ESMSCannotWrite.Create('TStaticMemoryStream.Write: Write operation not allowed on static memory.');
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TStaticMemoryStream.LoadFromStream(Stream: TStream);
begin
raise ESMSCannotWrite.Create('TStaticMemoryStream.LoadFromStream: Cannot load into static memory.');
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TStaticMemoryStream.LoadFromFile(const FileName: String);
var
FileStream: TFileStream;
begin
FileStream := TFileStream.Create(StrToRTL(FileName),fmOpenRead or fmShareDenyWrite);
try
FileStream.Seek(0,soBeginning);
LoadFromStream(FileStream);
finally
FileStream.Free;
end;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWritableStaticMemoryStream
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TWritableStaticMemoryStream - implementation
===============================================================================}
{-------------------------------------------------------------------------------
TWritableStaticMemoryStream - public methods
-------------------------------------------------------------------------------}
Function TWritableStaticMemoryStream.Write(const Buffer; Count: LongInt): LongInt;
begin
If (Count > 0) and (Position >= 0) then
begin
Result := Size - Position;
If Result > 0 then
begin
If Result > Count then
Result := Count;
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
Move(Buffer,Pointer(PtrUInt(Memory) + PtrUInt(Position))^,Result);
Position := Position + Result;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
end
else Result := 0;
end
else Result := 0;
end;
//------------------------------------------------------------------------------
procedure TWritableStaticMemoryStream.LoadFromStream(Stream: TStream);
begin
Stream.Seek(0,soBeginning);
If (Stream.Size - Stream.Position) > Size then
Stream.ReadBuffer(Memory^,Size)
else
Stream.ReadBuffer(Memory^,LongInt(Stream.Size - Stream.Position));
end;
end.