DEADSOFTWARE

c59302102436e38a1ad715ec74df08c7e908d315
[d2df-sdl.git] / src / engine / e_fixedbuffer.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE e_amodes.inc}
17 unit e_fixedbuffer;
19 interface
21 uses md5;
23 const
24 BUF_SIZE = 65536;
26 type
27 TBuffer = record
28 Data: array [0..BUF_SIZE] of Byte; // îäèí áàéò ñâåðõó íà âñÿêèé ñëó÷àé
29 ReadPos: Cardinal;
30 WritePos: Cardinal;
31 Len: Cardinal;
32 end;
33 pTBuffer = ^TBuffer;
35 var
36 RawPos: Cardinal = 0;
38 procedure e_Buffer_Clear(B: pTBuffer);
41 procedure e_Buffer_Write_Generic(B: pTBuffer; var V; N: Cardinal);
42 procedure e_Buffer_Read_Generic(B: pTBuffer; var V; N: Cardinal);
45 procedure e_Buffer_Write(B: pTBuffer; V: Char); overload;
47 procedure e_Buffer_Write(B: pTBuffer; V: Byte); overload;
48 procedure e_Buffer_Write(B: pTBuffer; V: Word); overload;
49 procedure e_Buffer_Write(B: pTBuffer; V: LongWord); overload;
51 procedure e_Buffer_Write(B: pTBuffer; V: ShortInt); overload;
52 procedure e_Buffer_Write(B: pTBuffer; V: SmallInt); overload;
53 procedure e_Buffer_Write(B: pTBuffer; V: LongInt); overload;
54 procedure e_Buffer_Write(B: pTBuffer; V: Int64); overload;
56 procedure e_Buffer_Write(B: pTBuffer; V: string); overload;
58 procedure e_Buffer_Write(B: pTBuffer; V: TMD5Digest); overload;
61 function e_Buffer_Read_Char(B: pTBuffer): Char;
63 function e_Buffer_Read_Byte(B: pTBuffer): Byte;
64 function e_Buffer_Read_Word(B: pTBuffer): Word;
65 function e_Buffer_Read_LongWord(B: pTBuffer): LongWord;
67 function e_Buffer_Read_ShortInt(B: pTBuffer): ShortInt;
68 function e_Buffer_Read_SmallInt(B: pTBuffer): SmallInt;
69 function e_Buffer_Read_LongInt(B: pTBuffer): LongInt;
70 function e_Buffer_Read_Int64(B: pTBuffer): Int64;
72 function e_Buffer_Read_String(B: pTBuffer): string;
74 function e_Buffer_Read_MD5(B: pTBuffer): TMD5Digest;
77 procedure e_Raw_Read_Generic(P: Pointer; var V; N: Cardinal);
79 function e_Raw_Read_Char(P: Pointer): Char;
81 function e_Raw_Read_Byte(P: Pointer): Byte;
82 function e_Raw_Read_Word(P: Pointer): Word;
83 function e_Raw_Read_LongWord(P: Pointer): LongWord;
85 function e_Raw_Read_ShortInt(P: Pointer): ShortInt;
86 function e_Raw_Read_SmallInt(P: Pointer): SmallInt;
87 function e_Raw_Read_LongInt(P: Pointer): LongInt;
89 function e_Raw_Read_String(P: Pointer): string;
91 function e_Raw_Read_MD5(P: Pointer): TMD5Digest;
93 procedure e_Raw_Seek(I: Cardinal);
95 implementation
97 uses SysUtils, BinEditor;
99 procedure e_Buffer_Clear(B: pTBuffer);
100 begin
101 B^.WritePos := 0;
102 B^.ReadPos := 0;
103 B^.Len := 0;
104 end;
107 procedure e_Buffer_Write_Generic(B: pTBuffer; var V; N: Cardinal);
108 begin
109 if (B^.WritePos + N >= BUF_SIZE) then Exit;
110 if (B^.WritePos + N > B^.Len) then
111 B^.Len := B^.WritePos + N + 1;
113 CopyMemory(Pointer(NativeUInt(Addr(B^.Data)) + B^.WritePos),
114 @V, N);
116 B^.WritePos := B^.WritePos + N;
117 end;
118 procedure e_Buffer_Read_Generic(B: pTBuffer; var V; N: Cardinal);
119 begin
120 if (B^.ReadPos + N >= BUF_SIZE) then Exit;
122 CopyMemory(@V, Pointer(NativeUInt(Addr(B^.Data)) + B^.ReadPos), N);
124 B^.ReadPos := B^.ReadPos + N;
125 end;
128 procedure e_Buffer_Write(B: pTBuffer; V: Char); overload;
129 begin
130 e_Buffer_Write_Generic(B, V, 1);
131 end;
133 procedure e_Buffer_Write(B: pTBuffer; V: Byte); overload;
134 begin
135 e_Buffer_Write_Generic(B, V, 1);
136 end;
137 procedure e_Buffer_Write(B: pTBuffer; V: Word); overload;
138 begin
139 e_Buffer_Write_Generic(B, V, 2);
140 end;
141 procedure e_Buffer_Write(B: pTBuffer; V: LongWord); overload;
142 begin
143 e_Buffer_Write_Generic(B, V, 4);
144 end;
146 procedure e_Buffer_Write(B: pTBuffer; V: ShortInt); overload;
147 begin
148 e_Buffer_Write_Generic(B, V, 1);
149 end;
150 procedure e_Buffer_Write(B: pTBuffer; V: SmallInt); overload;
151 begin
152 e_Buffer_Write_Generic(B, V, 2);
153 end;
154 procedure e_Buffer_Write(B: pTBuffer; V: LongInt); overload;
155 begin
156 e_Buffer_Write_Generic(B, V, 4);
157 end;
158 procedure e_Buffer_Write(B: pTBuffer; V: Int64); overload;
159 begin
160 e_Buffer_Write_Generic(B, V, 8);
161 end;
163 procedure e_Buffer_Write(B: pTBuffer; V: string); overload;
164 var
165 Len: Byte;
166 P: Cardinal;
167 begin
168 Len := Length(V);
169 e_Buffer_Write_Generic(B, Len, 1);
171 if (Len = 0) then Exit;
173 P := B^.WritePos + Len;
174 if (P >= BUF_SIZE) then
175 begin
176 Len := BUF_SIZE - B^.WritePos;
177 P := BUF_SIZE;
178 end;
180 if (P > B^.Len) then B^.Len := P;
182 CopyMemory(Pointer(NativeUInt(Addr(B^.Data)) + B^.WritePos),
183 @V[1], Len);
185 B^.WritePos := P;
186 end;
188 procedure e_Buffer_Write(B: pTBuffer; V: TMD5Digest); overload;
189 var
190 I: Integer;
191 begin
192 for I := 0 to 15 do
193 e_Buffer_Write(B, V[I]);
194 end;
197 function e_Buffer_Read_Char(B: pTBuffer): Char;
198 begin
199 e_Buffer_Read_Generic(B, Result, 1);
200 end;
202 function e_Buffer_Read_Byte(B: pTBuffer): Byte;
203 begin
204 e_Buffer_Read_Generic(B, Result, 1);
205 end;
206 function e_Buffer_Read_Word(B: pTBuffer): Word;
207 begin
208 e_Buffer_Read_Generic(B, Result, 2);
209 end;
210 function e_Buffer_Read_LongWord(B: pTBuffer): LongWord;
211 begin
212 e_Buffer_Read_Generic(B, Result, 4);
213 end;
215 function e_Buffer_Read_ShortInt(B: pTBuffer): ShortInt;
216 begin
217 e_Buffer_Read_Generic(B, Result, 1);
218 end;
219 function e_Buffer_Read_SmallInt(B: pTBuffer): SmallInt;
220 begin
221 e_Buffer_Read_Generic(B, Result, 2);
222 end;
223 function e_Buffer_Read_LongInt(B: pTBuffer): LongInt;
224 begin
225 e_Buffer_Read_Generic(B, Result, 4);
226 end;
227 function e_Buffer_Read_Int64(B: pTBuffer): Int64;
228 begin
229 e_Buffer_Read_Generic(B, Result, 8);
230 end;
232 function e_Buffer_Read_String(B: pTBuffer): string;
233 var
234 Len: Byte;
235 begin
236 Len := e_Buffer_Read_Byte(B);
237 Result := '';
238 if Len = 0 then Exit;
240 if B^.ReadPos + Len > B^.Len then
241 Len := B^.Len - B^.ReadPos;
243 SetLength(Result, Len);
244 CopyMemory(@Result[1], Pointer(NativeUInt(Addr(B^.Data)) + B^.ReadPos), Len);
246 B^.ReadPos := B^.ReadPos + Len;
247 end;
249 function e_Buffer_Read_MD5(B: pTBuffer): TMD5Digest;
250 var
251 I: Integer;
252 begin
253 for I := 0 to 15 do
254 Result[I] := e_Buffer_Read_Byte(B);
255 end;
257 procedure e_Raw_Read_Generic(P: Pointer; var V; N: Cardinal);
258 begin
259 CopyMemory(@V, Pointer(NativeUInt(P) + RawPos), N);
261 RawPos := RawPos + N;
262 end;
264 function e_Raw_Read_Char(P: Pointer): Char;
265 begin
266 e_Raw_Read_Generic(P, Result, 1);
267 end;
269 function e_Raw_Read_Byte(P: Pointer): Byte;
270 begin
271 e_Raw_Read_Generic(P, Result, 1);
272 end;
273 function e_Raw_Read_Word(P: Pointer): Word;
274 begin
275 e_Raw_Read_Generic(P, Result, 2);
276 end;
277 function e_Raw_Read_LongWord(P: Pointer): LongWord;
278 begin
279 e_Raw_Read_Generic(P, Result, 4);
280 end;
282 function e_Raw_Read_ShortInt(P: Pointer): ShortInt;
283 begin
284 e_Raw_Read_Generic(P, Result, 1);
285 end;
286 function e_Raw_Read_SmallInt(P: Pointer): SmallInt;
287 begin
288 e_Raw_Read_Generic(P, Result, 2);
289 end;
290 function e_Raw_Read_LongInt(P: Pointer): LongInt;
291 begin
292 e_Raw_Read_Generic(P, Result, 4);
293 end;
295 function e_Raw_Read_String(P: Pointer): string;
296 var
297 Len: Byte;
298 begin
299 Len := e_Raw_Read_Byte(P);
300 Result := '';
301 if Len = 0 then Exit;
303 SetLength(Result, Len);
304 CopyMemory(@Result[1], Pointer(NativeUInt(P) + RawPos), Len);
306 RawPos := RawPos + Len;
307 end;
309 function e_Raw_Read_MD5(P: Pointer): TMD5Digest;
310 var
311 I: Integer;
312 begin
313 for I := 0 to 15 do
314 Result[I] := e_Raw_Read_Byte(P);
315 end;
317 procedure e_Raw_Seek(I: Cardinal);
318 begin
319 RawPos := I;
320 end;
322 end.