1 (* Copyright (C) Doom 2D: Forever Developers
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, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
33 function Init(V
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
34 procedure Alloc(N
: Integer);
37 procedure CopyFrom(var From
: TMsg
; V
: Pointer; N
: Integer);
38 function Allocated(): Boolean;
39 function AssignBuffer(P
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
41 procedure BeginReading();
42 procedure Seek(Pos
: Integer);
43 procedure Skip(Size
: Integer);
44 function BytesLeft(): Integer;
45 function ReadData(V
: Pointer; N
: Integer): Integer;
46 function ReadChar(): Char;
47 function ReadByte(): Byte;
48 function ReadWord(): Word;
49 function ReadLongWord(): LongWord;
50 function ReadShortInt(): ShortInt;
51 function ReadSmallInt(): SmallInt;
52 function ReadLongInt(): LongInt;
53 function ReadInt64(): Int64;
54 function ReadString(): String;
55 function ReadMD5(): TMD5Digest
;
57 procedure WriteData(V
: Pointer; N
: Integer);
58 procedure Write(V
: Byte); overload
;
59 procedure Write(V
: Word); overload
;
60 procedure Write(V
: LongWord); overload
;
61 procedure Write(V
: ShortInt); overload
;
62 procedure Write(V
: SmallInt); overload
;
63 procedure Write(V
: LongInt); overload
;
64 procedure Write(V
: Int64); overload
;
65 procedure Write(V
: String); overload
;
66 procedure Write(V
: TMD5Digest
); overload
;
67 procedure Write(V
: TMsg
);
77 function TMsg
.Init(V
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
80 if Full
then CurSize
:= N
else CurSize
:= 0;
86 Result
:= (N
> 0) and (V
<> nil);
89 procedure TMsg
.Alloc(N
: Integer);
95 raise Exception
.Create('TMsg.Alloc: no mem');
101 procedure TMsg
.Free();
103 if not OwnMemory
then
104 raise Exception
.Create('TMsg.Free: called on borrowed memory');
112 procedure TMsg
.Clear();
120 function TMsg
.Allocated(): Boolean;
125 procedure TMsg
.CopyFrom(var From
: TMsg
; V
: Pointer; N
: Integer);
127 if N
< From
.CurSize
then
128 raise Exception
.Create('TMsg.Copy: can''t copy into a smaller TMsg');
129 Move(From
, Self
, SizeOf(TMsg
));
131 Move(From
.Data
^, Data
^, From
.CurSize
);
134 function TMsg
.AssignBuffer(P
: Pointer; N
: Integer; Full
: Boolean = False): Boolean;
136 if OwnMemory
then Self
.Free();
140 if Full
then CurSize
:= N
;
141 Result
:= (N
> 0) and (P
<> nil);
144 procedure TMsg
.WriteData(V
: Pointer; N
: Integer);
148 if CurSize
+ N
> MaxSize
then
152 NewSize
:= MaxSize
+ ((N
+ AllocStep
- 1) div AllocStep
) * AllocStep
; // round up
153 if ReAllocMem(Data
, NewSize
) = nil then
154 raise Exception
.Create('TMsg.WriteData: out of memory on realloc');
160 raise Exception
.Create('TMsg.WriteData: buffer overrun on borrowed memory!');
164 Move(V
^, (Data
+ CurSize
)^, N
);
165 CurSize
:= CurSize
+ N
;
168 procedure TMsg
.Write(V
: TMsg
);
170 WriteData(V
.Data
, V
.CurSize
);
173 procedure TMsg
.Write(V
: Byte); overload
;
178 procedure TMsg
.Write(V
: Word); overload
;
184 procedure TMsg
.Write(V
: LongWord); overload
;
190 procedure TMsg
.Write(V
: ShortInt); overload
;
196 procedure TMsg
.Write(V
: SmallInt); overload
;
202 procedure TMsg
.Write(V
: LongInt); overload
;
208 procedure TMsg
.Write(V
: Int64); overload
;
214 procedure TMsg
.Write(V
: AnsiString); overload
;
218 // TODO: Write(Word(Length(V)));
219 Write(Byte(Length(V
)));
220 for I
:= 1 to High(V
) do
224 procedure TMsg
.Write(V
: TMD5Digest
); overload
;
232 procedure TMsg
.BeginReading();
238 procedure TMsg
.Seek(Pos
: Integer);
240 if Pos
> CurSize
then
241 raise Exception
.Create('TMsg.Seek: buffer overrun!');
245 procedure TMsg
.Skip(Size
: Integer);
247 if ReadCount
+ Size
> CurSize
then
248 raise Exception
.Create('TMsg.Skip: buffer overrun!');
249 ReadCount
:= ReadCount
+ Size
;
252 function TMsg
.BytesLeft(): Integer;
254 Result
:= CurSize
- ReadCount
;
257 function TMsg
.ReadData(V
: Pointer; N
: Integer): Integer;
260 if ReadCount
+ N
> CurSize
then
262 // TODO: maybe partial reads?
263 ReadCount
:= CurSize
+ 1;
264 raise Exception
.Create('TMsg.ReadData: buffer overrun!');
267 Move((Data
+ ReadCount
)^, V
^, N
);
268 ReadCount
:= ReadCount
+ N
;
272 function TMsg
.ReadChar(): Char;
275 ReadData(@Result
, 1);
278 function TMsg
.ReadByte(): Byte;
281 ReadData(@Result
, 1);
284 function TMsg
.ReadWord(): Word;
287 ReadData(@Result
, 2);
288 Result
:= LEtoN(Result
);
291 function TMsg
.ReadLongWord(): LongWord;
294 ReadData(@Result
, 4);
295 Result
:= LEtoN(Result
);
298 function TMsg
.ReadShortInt(): ShortInt;
301 ReadData(@Result
, 1);
302 Result
:= LEtoN(Result
);
305 function TMsg
.ReadSmallInt(): SmallInt;
308 ReadData(@Result
, 2);
309 Result
:= LEtoN(Result
);
312 function TMsg
.ReadLongInt(): LongInt;
315 ReadData(@Result
, 4);
316 Result
:= LEtoN(Result
);
319 function TMsg
.ReadInt64(): Int64;
322 ReadData(@Result
, 8);
323 Result
:= LEtoN(Result
);
326 function TMsg
.ReadString(): string;
332 // TODO: L := ReadWord();
334 if (L
> 0) and (L
<> Byte(-1)) then
336 SetLength(Result
, L
);
338 Result
[I
] := ReadChar();
342 function TMsg
.ReadMD5(): TMD5Digest
;
347 Result
[I
] := ReadByte();