-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinarystream.pas
424 lines (383 loc) · 10.7 KB
/
binarystream.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit binarystream;
interface
uses
sysutils;
type
TBinaryStreamReader = class
private
FInput: RawByteString;
FPosition: Cardinal; // 1-based
procedure CheckCanRead(ComponentSize: Cardinal); inline;
public
constructor Create(const Input: RawByteString);
function ReadBoolean(): Boolean;
function ReadCardinal(): Cardinal;
function ReadPtrUInt(): PtrUInt;
function ReadByte(): Byte;
function ReadInt32(): Int32;
function ReadInt64(): Int64;
function ReadUInt64(): UInt64;
function ReadDouble(): Double;
function ReadString(): RawByteString;
function ReadBytes(): TBytes;
procedure ReadRawBytes(Length: Cardinal; out Output);
procedure Reset(); // returns position to start
procedure Truncate(Remainder: Cardinal); // truncates the input so that only Remainder bytes remain
property RawInput: RawByteString read FInput;
end;
TBinaryStreamWriter = class
private
const
SegmentSize = 1024;
type
PSegment = ^TSegment;
TSegment = record
Next: PSegment;
Data: array[0..SegmentSize-1] of Byte;
Length: Cardinal;
procedure Reset();
end;
var
FFirst: PSegment;
FLast: PSegment;
FPosition: Cardinal; // position in FLast, 0-based
FLength: Cardinal; // total length so far
FSkip: Cardinal; // leading bytes to skip
function GetDestination(NeededLength: Cardinal): Pointer;
public
destructor Destroy(); override;
procedure WriteBoolean(const Value: Boolean);
procedure WriteCardinal(const Value: Cardinal);
procedure WritePtrUInt(const Value: PtrUInt);
procedure WriteByte(const Value: Byte);
procedure WriteInt32(const Value: Int32);
procedure WriteInt64(const Value: Int64);
procedure WriteUInt64(const Value: UInt64);
procedure WriteDouble(const Value: Double);
procedure WriteString(const Value: RawByteString);
procedure WriteBytes(const Value: TBytes);
procedure WriteRawBytes(Buffer: Pointer; Length: Cardinal);
function Serialize(IncludeLengthPrefix: Boolean): RawByteString;
procedure Consume(Count: Cardinal); // skips the leading Count bytes in future Serialize attempts
procedure Clear(); // consume everything
property BufferLength: Cardinal read FLength;
end;
EBinaryStreamError = class(Exception)
end;
// TODO: build a version of this that uses the same API for reading and writing so that you can have just one codepath
implementation
constructor TBinaryStreamReader.Create(const Input: RawByteString);
begin
inherited Create();
FInput := Input;
FPosition := 1;
end;
procedure TBinaryStreamReader.CheckCanRead(ComponentSize: Cardinal);
begin
if (FPosition + ComponentSize - 1 > Length(FInput)) then
raise EBinaryStreamError.CreateFmt('Read past end of stream (position=%d, size=%d, length=%d).', [FPosition, ComponentSize, Length(FInput)]);
end;
function TBinaryStreamReader.ReadBoolean(): Boolean;
type
PByteBool = ^ByteBool;
begin
CheckCanRead(SizeOf(ByteBool));
Result := PByteBool(Pointer(@FInput[FPosition]))^;
Inc(FPosition, 1);
end;
function TBinaryStreamReader.ReadCardinal(): Cardinal;
type
PCardinal = ^Cardinal;
begin
CheckCanRead(SizeOf(Cardinal));
Result := PCardinal(Pointer(@FInput[FPosition]))^;
Inc(FPosition, SizeOf(Cardinal));
end;
function TBinaryStreamReader.ReadPtrUInt(): PtrUInt;
type
PPtrUInt = ^PtrUInt;
begin
CheckCanRead(SizeOf(PtrUInt));
Result := PPtrUInt(Pointer(@FInput[FPosition]))^;
Inc(FPosition, SizeOf(PtrUInt));
end;
function TBinaryStreamReader.ReadByte(): Byte;
type
PByte = ^Byte;
begin
CheckCanRead(SizeOf(Byte));
Result := PByte(Pointer(@FInput[FPosition]))^;
Inc(FPosition, SizeOf(Byte));
end;
function TBinaryStreamReader.ReadInt32(): Int32;
type
PInt32 = ^Int32;
begin
CheckCanRead(SizeOf(Int32));
Result := PInt32(Pointer(@FInput[FPosition]))^;
Inc(FPosition, SizeOf(Int32));
end;
function TBinaryStreamReader.ReadInt64(): Int64;
type
PInt64 = ^Int64;
begin
CheckCanRead(SizeOf(Int64));
Result := PInt64(Pointer(@FInput[FPosition]))^;
Inc(FPosition, SizeOf(Int64));
end;
function TBinaryStreamReader.ReadUInt64(): UInt64;
type
PUInt64 = ^UInt64;
begin
CheckCanRead(SizeOf(UInt64));
Result := PUInt64(Pointer(@FInput[FPosition]))^;
Inc(FPosition, SizeOf(UInt64));
end;
function TBinaryStreamReader.ReadDouble(): Double;
type
PDouble = ^Double;
begin
CheckCanRead(SizeOf(Double));
Result := PDouble(Pointer(@FInput[FPosition]))^;
Inc(FPosition, SizeOf(Double));
end;
function TBinaryStreamReader.ReadString(): RawByteString;
var
BufferLength: Cardinal;
begin
BufferLength := ReadCardinal();
CheckCanRead(SizeOf(BufferLength));
SetLength(Result, BufferLength); {BOGUS Hint: Function result variable of a managed type does not seem to be initialized}
if (BufferLength > 0) then
begin
Move(FInput[FPosition], Result[1], BufferLength);
Inc(FPosition, BufferLength);
end;
end;
function TBinaryStreamReader.ReadBytes(): TBytes;
var
BufferLength: Cardinal;
begin
BufferLength := ReadCardinal();
CheckCanRead(SizeOf(BufferLength));
SetLength(Result, BufferLength); {BOGUS Warning: Function result variable of a managed type does not seem to be initialized}
if (BufferLength > 0) then
begin
Move(FInput[FPosition], Result[0], BufferLength);
Inc(FPosition, BufferLength);
end;
end;
procedure TBinaryStreamReader.ReadRawBytes(Length: Cardinal; out Output);
begin
CheckCanRead(SizeOf(Length));
if (Length > 0) then
begin
Move(FInput[FPosition], Output, Length); {BOGUS Hint: Variable "Output" does not seem to be initialized}
Inc(FPosition, Length);
end;
end;
procedure TBinaryStreamReader.Reset();
begin
FPosition := 1;
end;
procedure TBinaryStreamReader.Truncate(Remainder: Cardinal);
begin
Assert((FPosition - 1) + Remainder <= Length(FInput));
SetLength(FInput, (FPosition - 1) + Remainder);
end;
procedure TBinaryStreamWriter.TSegment.Reset();
begin
Next := nil;
Length := 0;
end;
destructor TBinaryStreamWriter.Destroy();
var
Next: PSegment;
begin
while (Assigned(FFirst)) do
begin
Next := FFirst^.Next;
Dispose(FFirst);
FFirst := Next;
end;
inherited;
end;
function TBinaryStreamWriter.GetDestination(NeededLength: Cardinal): Pointer;
begin
Assert(NeededLength > 0);
Assert(NeededLength <= SegmentSize);
if (not Assigned(FLast)) then
begin
New(FFirst);
FFirst^.Reset();
FLast := FFirst;
FLast^.Next := nil;
Assert(FPosition = 0);
end
else
if (FPosition + NeededLength >= High(FLast^.Data)) then
begin
New(FLast^.Next);
FLast := FLast^.Next;
FLast^.Reset();
FPosition := 0;
end;
Result := Pointer(@FLast^.Data) + FPosition;
Inc(FLast^.Length, NeededLength);
Inc(FPosition, NeededLength);
Inc(FLength, NeededLength);
end;
procedure TBinaryStreamWriter.WriteBoolean(const Value: Boolean);
begin
if (Value) then
begin
PByte(GetDestination(1))^ := $01;
end
else
begin
PByte(GetDestination(1))^ := $00;
end;
end;
procedure TBinaryStreamWriter.WriteCardinal(const Value: Cardinal);
type
PCardinal = ^Cardinal;
begin
PCardinal(GetDestination(SizeOf(Cardinal)))^ := Value;
end;
procedure TBinaryStreamWriter.WritePtrUInt(const Value: PtrUInt);
type
PPtrUInt = ^PtrUInt;
begin
PPtrUInt(GetDestination(SizeOf(PtrUInt)))^ := Value;
end;
procedure TBinaryStreamWriter.WriteByte(const Value: Byte);
type
PByte = ^Byte;
begin
PByte(GetDestination(SizeOf(Byte)))^ := Value;
end;
procedure TBinaryStreamWriter.WriteInt32(const Value: Int32);
type
PInt32 = ^Int32;
begin
PInt32(GetDestination(SizeOf(Int32)))^ := Value;
end;
procedure TBinaryStreamWriter.WriteInt64(const Value: Int64);
type
PInt64 = ^Int64;
begin
PInt64(GetDestination(SizeOf(Int64)))^ := Value;
end;
procedure TBinaryStreamWriter.WriteUInt64(const Value: UInt64);
type
PUInt64 = ^UInt64;
begin
PUInt64(GetDestination(SizeOf(UInt64)))^ := Value;
end;
procedure TBinaryStreamWriter.WriteDouble(const Value: Double);
type
PDouble = ^Double;
begin
PDouble(GetDestination(SizeOf(Double)))^ := Value;
end;
procedure TBinaryStreamWriter.WriteRawBytes(Buffer: Pointer; Length: Cardinal);
var
ChunkSize: Cardinal;
begin
while (Length > 0) do
begin
Assert(FPosition <= SegmentSize);
ChunkSize := SegmentSize - FPosition; // $R-
if (ChunkSize = 0) then
begin
ChunkSize := SegmentSize;
end;
if (ChunkSize > Length) then
begin
ChunkSize := Length;
end;
Move(Buffer^, GetDestination(ChunkSize)^, ChunkSize);
Inc(Buffer, ChunkSize);
Dec(Length, ChunkSize);
end;
end;
procedure TBinaryStreamWriter.WriteString(const Value: RawByteString);
begin
WriteCardinal(Length(Value));
if (Value <> '') then
WriteRawBytes(@Value[1], Length(Value)); // $R-
end;
procedure TBinaryStreamWriter.WriteBytes(const Value: TBytes);
begin
WriteCardinal(Length(Value));
if (Length(Value) > 0) then
WriteRawBytes(@Value[0], Length(Value)); // $R-
end;
function TBinaryStreamWriter.Serialize(IncludeLengthPrefix: Boolean): RawByteString;
var
Buffer: RawByteString;
Index, Skip: Cardinal;
Segment: PSegment;
begin
Index := 1;
if (IncludeLengthPrefix) then
begin
SetLength(Buffer, SizeOf(Cardinal) + FLength);
Move(FLength, Buffer[Index], SizeOf(Cardinal));
Inc(Index, SizeOf(Cardinal));
end
else
begin
SetLength(Buffer, FLength);
end;
Segment := FFirst;
Skip := FSkip;
while (Assigned(Segment)) do
begin
Assert(Skip < Segment^.Length);
Move(Segment^.Data[Skip], Buffer[Index], Segment^.Length - Skip);
Inc(Index, Segment^.Length - Skip);
Segment := Segment^.Next;
Skip := 0;
end;
Assert(Index = Length(Buffer) + 1);
Result := Buffer;
end;
procedure TBinaryStreamWriter.Consume(Count: Cardinal);
var
Segment: PSegment;
begin
Assert(Count <= FLength);
Inc(FSkip, Count);
Dec(FLength, Count);
while (Assigned(FFirst) and (FFirst^.Length <= FSkip)) do
begin
Segment := FFirst;
FFirst := FFirst^.Next;
Dec(FSkip, Segment^.Length);
if (FLast = Segment) then
begin
FLast := nil;
FPosition := 0;
end;
Dispose(Segment);
end;
end;
procedure TBinaryStreamWriter.Clear();
var
Segment: PSegment;
begin
while (Assigned(FFirst)) do
begin
Segment := FFirst;
FFirst := FFirst^.Next;
Dispose(Segment);
end;
FLast := nil;
FPosition := 0;
FLength := 0;
FSkip := 0;
end;
end.