1 | unit VarBlockSerializer;
|
---|
2 |
|
---|
3 | // One level of recursive VarInt size supported
|
---|
4 | // Biggest UInt type is QWord (64-bit)
|
---|
5 |
|
---|
6 | interface
|
---|
7 |
|
---|
8 | uses
|
---|
9 | Classes, DateUtils, StreamHelper, Math, SysUtils, SubStream, LazUTF8,
|
---|
10 | SpecializedList, LCLProc;
|
---|
11 |
|
---|
12 | const
|
---|
13 | BitAlignment = 8;
|
---|
14 |
|
---|
15 | type
|
---|
16 |
|
---|
17 | { TVarBlockSerializer }
|
---|
18 |
|
---|
19 | TVarBlockSerializer = class
|
---|
20 | private
|
---|
21 | FStream: TStream;
|
---|
22 | procedure SetStream(const AValue: TStream);
|
---|
23 | procedure TrimLeft;
|
---|
24 | function GetUnaryLengthMask(Length: Integer): Byte;
|
---|
25 | function DecodeUnaryLength(Data: Byte): Integer;
|
---|
26 | public
|
---|
27 | OwnsStream: Boolean;
|
---|
28 |
|
---|
29 | // Base
|
---|
30 | procedure WriteVarUInt(Value: QWord);
|
---|
31 | function ReadVarUInt: QWord;
|
---|
32 | procedure WriteVarBlock(Block: TVarBlockSerializer);
|
---|
33 | procedure ReadVarBlock(Block: TVarBlockSerializer);
|
---|
34 | procedure WriteVarStream(AStream: TStream);
|
---|
35 | procedure ReadVarStream(AStream: TStream);
|
---|
36 | procedure WriteVarList(List: TListByte);
|
---|
37 | procedure ReadVarList(List: TListByte);
|
---|
38 | procedure WriteVarBuffer(var Buffer; Count: Integer);
|
---|
39 | procedure ReadVarBuffer(var Buffer; Count: Integer);
|
---|
40 | function GetVarSize: Integer;
|
---|
41 | function GetVarCount: Integer;
|
---|
42 | function TryVarBlock: Boolean;
|
---|
43 |
|
---|
44 | // Advanced data types
|
---|
45 | procedure WriteVarSInt(Value: Int64);
|
---|
46 | function ReadVarSInt: Int64;
|
---|
47 | procedure WriteVarFloat(Value: Double; Base: Integer = 2);
|
---|
48 | function ReadVarFloat(Base: Integer = 2): Double;
|
---|
49 | procedure WriteVarString(Value: string);
|
---|
50 | function ReadVarString: string;
|
---|
51 | procedure WriteVarDouble(Value: Double);
|
---|
52 | function ReadVarDouble: Double;
|
---|
53 |
|
---|
54 | // Misc methods
|
---|
55 | function TestMask(Mask: QWord; BitIndex: Byte): Boolean;
|
---|
56 | function BuildMask(Bits: array of Integer): Integer;
|
---|
57 | procedure ReadItemByMaskIndex(Index: Integer; Data: TVarBlockSerializer);
|
---|
58 | procedure ReadItemRefByMaskIndex(Index: Integer; Data: TSubStream);
|
---|
59 | procedure BlockEnclose;
|
---|
60 | procedure BlockUnclose;
|
---|
61 | procedure Assign(Source: TVarBlockSerializer);
|
---|
62 | constructor Create;
|
---|
63 | destructor Destroy; override;
|
---|
64 | property Stream: TStream read FStream write SetStream;
|
---|
65 | end;
|
---|
66 |
|
---|
67 | { TVarBlockIndexed }
|
---|
68 |
|
---|
69 | TVarBlockIndexed = class
|
---|
70 | private
|
---|
71 | public
|
---|
72 | Items: TListObject; // TListObject<TVarBlockSerializer>
|
---|
73 | Enclose: Boolean;
|
---|
74 | procedure CheckItem(Index: Integer);
|
---|
75 | procedure Assign(Source: TVarBlockIndexed);
|
---|
76 |
|
---|
77 | // Base
|
---|
78 | procedure WriteVarUInt(Index: Integer; Value: QWord);
|
---|
79 | function ReadVarUInt(Index: Integer): QWord;
|
---|
80 | procedure WriteVarBlock(Index: Integer; Block: TVarBlockSerializer);
|
---|
81 | procedure ReadVarBlock(Index: Integer; Block: TVarBlockSerializer);
|
---|
82 | procedure WriteVarStream(Index: Integer; Stream: TStream);
|
---|
83 | procedure ReadVarStream(Index: Integer; Stream: TStream);
|
---|
84 | procedure WriteVarList(Index: Integer; List: TListByte);
|
---|
85 | procedure ReadVarList(Index: Integer; List: TListByte);
|
---|
86 | procedure WriteVarIndexedBlock(Index: Integer; Block: TVarBlockIndexed);
|
---|
87 | procedure ReadVarIndexedBlock(Index: Integer; Block: TVarBlockIndexed);
|
---|
88 | procedure WriteVarBuffer(Index: Integer; var Buffer; Count: Integer);
|
---|
89 | procedure ReadVarBuffer(Index: Integer; var Buffer; Count: Integer);
|
---|
90 |
|
---|
91 | // Advanced data types
|
---|
92 | procedure WriteVarSInt(Index: Integer; Value: Int64);
|
---|
93 | function ReadVarSInt(Index: Integer): Int64;
|
---|
94 | procedure WriteVarFloat(Index: Integer; Value: Double; Base: Integer = 2);
|
---|
95 | function ReadVarFloat(Index: Integer; Base: Integer = 2): Double;
|
---|
96 | procedure WriteVarDouble(Index: Integer; Value: Double);
|
---|
97 | function ReadVarDouble(Index: Integer): Double;
|
---|
98 | procedure WriteVarString(Index: Integer; Value: string);
|
---|
99 | function ReadVarString(Index: Integer): string;
|
---|
100 | procedure WriteVarUIntArray(Index: Integer; List: TListInteger);
|
---|
101 | procedure ReadVarUIntArray(Index: Integer; List: TListInteger);
|
---|
102 | procedure WriteVarStringArray(Index: Integer; List: TListString);
|
---|
103 | procedure ReadVarStringArray(Index: Integer; List: TListString);
|
---|
104 |
|
---|
105 | procedure Clear;
|
---|
106 | function TestIndex(Index: Integer): Boolean;
|
---|
107 | procedure WriteToVarBlock(VarBlock: TVarBlockSerializer);
|
---|
108 | procedure ReadFromVarBlock(VarBlock: TVarBlockSerializer);
|
---|
109 | procedure WriteToStream(Stream: TStream);
|
---|
110 | procedure ReadFromStream(Stream: TStream);
|
---|
111 | procedure WriteToList(List: TListByte);
|
---|
112 | procedure ReadFromList(List: TListByte);
|
---|
113 | constructor Create;
|
---|
114 | destructor Destroy; override;
|
---|
115 | end;
|
---|
116 |
|
---|
117 |
|
---|
118 | implementation
|
---|
119 |
|
---|
120 | resourcestring
|
---|
121 | SMaskedValueReadError = 'Error reading masked variable length block.';
|
---|
122 | SUInt64Overflow = '64-bit UInt read overflow.';
|
---|
123 | SReadError = 'Stream read error. Expected length %d, read %d. Source stream size %d.';
|
---|
124 | SErrorGetVarSize = 'Error reading variable block size';
|
---|
125 |
|
---|
126 | { TVarBlockSerializer }
|
---|
127 |
|
---|
128 | procedure TVarBlockSerializer.TrimLeft;
|
---|
129 | var
|
---|
130 | Temp: TVarBlockSerializer;
|
---|
131 | Length: Integer;
|
---|
132 | Data: Byte;
|
---|
133 | StreamHelper: TStreamHelper;
|
---|
134 | begin
|
---|
135 | try
|
---|
136 | Temp := TVarBlockSerializer.Create;
|
---|
137 | Stream.Position := 0;
|
---|
138 | Length := Stream.Size * 8;
|
---|
139 | Data := 0;
|
---|
140 | while (Length > 0) and
|
---|
141 | (((Data shr (Length and 7)) and 1) = 0) do begin
|
---|
142 | Data := Stream.ReadByte;
|
---|
143 | Dec(Length); // set 7. bit in byte
|
---|
144 | while (((Data shr (Length and 7)) and 1) = 0) and ((Length and 7) > 0) do
|
---|
145 | Dec(Length);
|
---|
146 | end;
|
---|
147 | Inc(Length);
|
---|
148 | Length := Ceil(Length / 8);
|
---|
149 | Stream.Position := Stream.Size - Length;
|
---|
150 | StreamHelper := TStreamHelper.Create(Stream);
|
---|
151 | StreamHelper.ReadStream(Temp.Stream, Length);
|
---|
152 | Temp.Stream.Size := 0;
|
---|
153 | Stream.Position := 0;
|
---|
154 | StreamHelper.WriteStream(Temp.Stream, Temp.Stream.Size);
|
---|
155 | finally
|
---|
156 | StreamHelper.Free;
|
---|
157 | Temp.Free;
|
---|
158 | end;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TVarBlockSerializer.SetStream(const AValue: TStream);
|
---|
162 | begin
|
---|
163 | if OwnsStream and Assigned(FStream) then
|
---|
164 | FStream.Free;
|
---|
165 | OwnsStream := False;
|
---|
166 | FStream := AValue;
|
---|
167 | end;
|
---|
168 |
|
---|
169 | function TVarBlockSerializer.GetUnaryLengthMask(Length: Integer): Byte;
|
---|
170 | begin
|
---|
171 | Result := ((1 shl (BitAlignment - Length)) - 1) xor $ff;
|
---|
172 | end;
|
---|
173 |
|
---|
174 | function TVarBlockSerializer.DecodeUnaryLength(Data:Byte):Integer;
|
---|
175 | begin
|
---|
176 | Result := 1;
|
---|
177 | while (((Data shr (BitAlignment - Result)) and 1) = 1) and
|
---|
178 | (Result < (BitAlignment + 1)) do Inc(Result);
|
---|
179 | end;
|
---|
180 |
|
---|
181 | procedure TVarBlockSerializer.WriteVarUInt(Value: QWord);
|
---|
182 | var
|
---|
183 | Length: Byte;
|
---|
184 | Data: Byte;
|
---|
185 | I: Integer;
|
---|
186 | LengthMask: Byte;
|
---|
187 | begin
|
---|
188 | // Get bit length
|
---|
189 | Length := SizeOf(QWord) * BitAlignment;
|
---|
190 | while ((Value and (QWord(1) shl (Length - 1))) = 0) and (Length > 0) do
|
---|
191 | Dec(Length);
|
---|
192 | Inc(Length);
|
---|
193 | Length := Ceil(Length / (BitAlignment - 1));
|
---|
194 | LengthMask := GetUnaryLengthMask(Length);
|
---|
195 |
|
---|
196 | // Copy data
|
---|
197 | for I := Length downto 1 do begin
|
---|
198 | Data := (Value shr (BitAlignment * (I - 1))) and $ff;
|
---|
199 | if I = Length then Data := (Data and
|
---|
200 | (LengthMask xor $ff)) or ((LengthMask shl 1) and $ff);
|
---|
201 | Stream.WriteByte(Data);
|
---|
202 | end;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | function TVarBlockSerializer.ReadVarUInt: QWord;
|
---|
206 | var
|
---|
207 | Data: Byte;
|
---|
208 | Length: Integer;
|
---|
209 | I: Integer;
|
---|
210 | LengthMask: Byte;
|
---|
211 | begin
|
---|
212 | Result := 0;
|
---|
213 | Length := 1;
|
---|
214 | I := 0;
|
---|
215 | while I < Length do begin
|
---|
216 | Data := Stream.ReadByte;
|
---|
217 | if I = 0 then begin
|
---|
218 | if Data = $ff then begin
|
---|
219 | // Read recursive length
|
---|
220 | Length := ReadVarUInt;
|
---|
221 | if Length > BitAlignment then
|
---|
222 | raise Exception.Create(SUInt64Overflow);
|
---|
223 | if Length > 0 then Data := Stream.ReadByte else
|
---|
224 | Data := 0;
|
---|
225 | end else begin
|
---|
226 | Length := DecodeUnaryLength(Data);
|
---|
227 | LengthMask := GetUnaryLengthMask(Length);
|
---|
228 | Data := Data and (LengthMask xor $ff);
|
---|
229 | end;
|
---|
230 | end;
|
---|
231 | Result := Result or (QWord(Data) shl ((Length - I - 1) * BitAlignment));
|
---|
232 | Inc(I);
|
---|
233 | end;
|
---|
234 | end;
|
---|
235 |
|
---|
236 | procedure TVarBlockSerializer.WriteVarBlock(Block: TVarBlockSerializer);
|
---|
237 | begin
|
---|
238 | WriteVarStream(Block.Stream);
|
---|
239 | end;
|
---|
240 |
|
---|
241 | procedure TVarBlockSerializer.ReadVarBlock(Block: TVarBlockSerializer);
|
---|
242 | begin
|
---|
243 | ReadVarStream(Block.Stream);
|
---|
244 | end;
|
---|
245 |
|
---|
246 | procedure TVarBlockSerializer.WriteVarFloat(Value: Double; Base: Integer = 2);
|
---|
247 | var
|
---|
248 | Exponent: Integer;
|
---|
249 | Block: TVarBlockSerializer;
|
---|
250 | begin
|
---|
251 | try
|
---|
252 | Block := TVarBlockSerializer.Create;
|
---|
253 |
|
---|
254 | // Normalize to integer number with base 10 exponent
|
---|
255 | Exponent := 0;
|
---|
256 | if Value <> 0 then begin
|
---|
257 | if Frac(Value) > 0 then begin
|
---|
258 | while Frac(Value) > 0 do begin
|
---|
259 | Value := Value * Base;
|
---|
260 | Dec(Exponent);
|
---|
261 | end;
|
---|
262 | end else
|
---|
263 | while Frac(Value / Base) = 0 do begin
|
---|
264 | Value := Value / Base;
|
---|
265 | Inc(Exponent);
|
---|
266 | end;
|
---|
267 | end;
|
---|
268 | Block.WriteVarSInt(Trunc(Value));
|
---|
269 | Block.WriteVarSInt(Exponent);
|
---|
270 | WriteVarBlock(Block);
|
---|
271 | finally
|
---|
272 | Block.Free;
|
---|
273 | end;
|
---|
274 | end;
|
---|
275 |
|
---|
276 | function TVarBlockSerializer.ReadVarFloat(Base: Integer = 2): Double;
|
---|
277 | var
|
---|
278 | Significant: Int64;
|
---|
279 | Exponent: Integer;
|
---|
280 | Block: TVarBlockSerializer;
|
---|
281 | begin
|
---|
282 | try
|
---|
283 | Block := TVarBlockSerializer.Create;
|
---|
284 | ReadVarBlock(Block);
|
---|
285 | Significant := Block.ReadVarSInt;
|
---|
286 | Exponent := Block.ReadVarSInt;
|
---|
287 | Result := Significant * IntPower(Base, Exponent);
|
---|
288 | finally
|
---|
289 | Block.Free;
|
---|
290 | end;
|
---|
291 | end;
|
---|
292 |
|
---|
293 | procedure TVarBlockSerializer.WriteVarString(Value: string);
|
---|
294 | var
|
---|
295 | Stream: TVarBlockSerializer;
|
---|
296 | I: Integer;
|
---|
297 | P: PChar;
|
---|
298 | Unicode: Cardinal;
|
---|
299 | CharLen: Integer;
|
---|
300 | begin
|
---|
301 | try
|
---|
302 | Stream := TVarBlockSerializer.Create;
|
---|
303 | P := PChar(Value);
|
---|
304 | for I := 0 to UTF8Length(Value) - 1 do begin
|
---|
305 | Unicode := UTF8CharacterToUnicode(P, CharLen);
|
---|
306 | Stream.WriteVarUInt(Unicode);
|
---|
307 | Inc(P, CharLen);
|
---|
308 | end;
|
---|
309 | WriteVarBlock(Stream);
|
---|
310 | finally
|
---|
311 | Stream.Free;
|
---|
312 | end;
|
---|
313 | end;
|
---|
314 |
|
---|
315 | function TVarBlockSerializer.ReadVarString: string;
|
---|
316 | var
|
---|
317 | Block: TVarBlockSerializer;
|
---|
318 | Character: Integer;
|
---|
319 | begin
|
---|
320 | try
|
---|
321 | Block := TVarBlockSerializer.Create;
|
---|
322 | ReadVarBlock(Block);
|
---|
323 | Block.Stream.Position := 0;
|
---|
324 | while Block.Stream.Position < Block.Stream.Size do begin
|
---|
325 | Character := Block.ReadVarUInt;
|
---|
326 | Result := Result + UnicodeToUTF8(Character);
|
---|
327 | end;
|
---|
328 | finally
|
---|
329 | Block.Free;
|
---|
330 | end;
|
---|
331 | end;
|
---|
332 |
|
---|
333 | procedure TVarBlockSerializer.WriteVarDouble(Value: Double);
|
---|
334 | begin
|
---|
335 | WriteVarBuffer(Value, 8);
|
---|
336 | end;
|
---|
337 |
|
---|
338 | function TVarBlockSerializer.ReadVarDouble: Double;
|
---|
339 | begin
|
---|
340 | Result := 0;
|
---|
341 | ReadVarBuffer(Result, 8);
|
---|
342 | end;
|
---|
343 |
|
---|
344 | procedure TVarBlockSerializer.WriteVarStream(AStream: TStream);
|
---|
345 | var
|
---|
346 | Length: Integer; // Count of data bytes
|
---|
347 | Data: Byte;
|
---|
348 | I: Integer;
|
---|
349 | LengthMask: Byte;
|
---|
350 | begin
|
---|
351 | AStream.Position := 0;
|
---|
352 | Length := AStream.Size;
|
---|
353 |
|
---|
354 | // Copy data
|
---|
355 | if Length = 0 then Stream.WriteByte(0)
|
---|
356 | else begin
|
---|
357 | if AStream.Size > 0 then Data := AStream.ReadByte
|
---|
358 | else Data := 0;
|
---|
359 | if (Length < BitAlignment) then begin
|
---|
360 | LengthMask := GetUnaryLengthMask(Length);
|
---|
361 | if ((Data and (LengthMask xor $ff)) <> Data) or (Data = 0) then begin
|
---|
362 | // First data starts by zero or
|
---|
363 | // first data byte not fit to length byte
|
---|
364 | Inc(Length);
|
---|
365 | if Length < 8 then begin
|
---|
366 | LengthMask := GetUnaryLengthMask(Length);
|
---|
367 | Stream.WriteByte((LengthMask shl 1) and $ff);
|
---|
368 | Stream.WriteByte(Data);
|
---|
369 | end;
|
---|
370 | end else begin
|
---|
371 | // First data byte fit to length byte
|
---|
372 | Stream.WriteByte((Data and (LengthMask xor $ff)) or ((LengthMask shl 1) and $ff));
|
---|
373 | end;
|
---|
374 | end;
|
---|
375 | if Length >= BitAlignment then begin
|
---|
376 | // Recursive length
|
---|
377 | Stream.WriteByte($ff);
|
---|
378 | WriteVarUInt(AStream.Size);
|
---|
379 | Stream.WriteByte(Data);
|
---|
380 | end;
|
---|
381 |
|
---|
382 | // Copy rest of data
|
---|
383 | for I := 1 to AStream.Size - 1 do begin
|
---|
384 | if I < AStream.Size then Data := AStream.ReadByte
|
---|
385 | else Data := 0;
|
---|
386 | Stream.WriteByte(Data);
|
---|
387 | end;
|
---|
388 | end;
|
---|
389 | end;
|
---|
390 |
|
---|
391 | procedure TVarBlockSerializer.ReadVarStream(AStream: TStream);
|
---|
392 | var
|
---|
393 | Data: Byte;
|
---|
394 | Length: Cardinal;
|
---|
395 | RealLength: Cardinal;
|
---|
396 | LengthMask: Byte;
|
---|
397 | begin
|
---|
398 | AStream.Size := 0;
|
---|
399 | Length := 1;
|
---|
400 |
|
---|
401 | Data := Stream.ReadByte;
|
---|
402 | if Data = $ff then begin
|
---|
403 | // Read recursive length
|
---|
404 | Length := ReadVarUInt;
|
---|
405 | AStream.Size := Length;
|
---|
406 | if Length > 0 then begin
|
---|
407 | Data := Stream.ReadByte;
|
---|
408 | AStream.WriteByte(Data);
|
---|
409 | end;
|
---|
410 | end else begin
|
---|
411 | // Read unary length
|
---|
412 | Length := DecodeUnaryLength(Data);
|
---|
413 | AStream.Size := Length;
|
---|
414 | LengthMask := GetUnaryLengthMask(Length);
|
---|
415 | Data := Data and (LengthMask xor $ff);
|
---|
416 | // Drop first byte if first data is zero
|
---|
417 | if Data <> 0 then AStream.WriteByte(Data)
|
---|
418 | else begin
|
---|
419 | Dec(Length);
|
---|
420 | AStream.Size := Length;
|
---|
421 | if Length > 0 then begin
|
---|
422 | Data := Stream.ReadByte;
|
---|
423 | AStream.WriteByte(Data);
|
---|
424 | end;
|
---|
425 | end;
|
---|
426 | end;
|
---|
427 |
|
---|
428 | // If CopyFrom parameter count is zero then whole source is copied
|
---|
429 | if Length > 1 then begin
|
---|
430 | RealLength := AStream.CopyFrom(Stream, Length - 1);
|
---|
431 | if RealLength < (Length - 1) then
|
---|
432 | raise EReadError.Create(Format(SReadError, [Length - 1, RealLength, Stream.Size]));
|
---|
433 | end;
|
---|
434 | AStream.Position := 0;
|
---|
435 | end;
|
---|
436 |
|
---|
437 | procedure TVarBlockSerializer.WriteVarList(List: TListByte);
|
---|
438 | var
|
---|
439 | Mem: TMemoryStream;
|
---|
440 | begin
|
---|
441 | try
|
---|
442 | Mem := TMemoryStream.Create;
|
---|
443 | List.WriteToStream(Mem);
|
---|
444 | WriteVarStream(Mem);
|
---|
445 | finally
|
---|
446 | Mem.Free;
|
---|
447 | end;
|
---|
448 | end;
|
---|
449 |
|
---|
450 | procedure TVarBlockSerializer.ReadVarList(List: TListByte);
|
---|
451 | var
|
---|
452 | Mem: TMemoryStream;
|
---|
453 | begin
|
---|
454 | try
|
---|
455 | Mem := TMemoryStream.Create;
|
---|
456 | ReadVarStream(Mem);
|
---|
457 | Mem.Position := 0;
|
---|
458 | List.Count := Mem.Size;
|
---|
459 | List.ReplaceStream(Mem);
|
---|
460 | finally
|
---|
461 | Mem.Free;
|
---|
462 | end;
|
---|
463 | end;
|
---|
464 |
|
---|
465 | procedure TVarBlockSerializer.WriteVarBuffer(var Buffer; Count: Integer);
|
---|
466 | var
|
---|
467 | Mem: TMemoryStream;
|
---|
468 | begin
|
---|
469 | try
|
---|
470 | Mem := TMemoryStream.Create;
|
---|
471 | Mem.WriteBuffer(Buffer, Count);
|
---|
472 | WriteVarStream(Mem);
|
---|
473 | finally
|
---|
474 | Mem.Free;
|
---|
475 | end;
|
---|
476 | end;
|
---|
477 |
|
---|
478 | procedure TVarBlockSerializer.ReadVarBuffer(var Buffer; Count: Integer);
|
---|
479 | var
|
---|
480 | Mem: TMemoryStream;
|
---|
481 | begin
|
---|
482 | try
|
---|
483 | Mem := TMemoryStream.Create;
|
---|
484 | ReadVarStream(Mem);
|
---|
485 | Mem.Position := 0;
|
---|
486 | Mem.ReadBuffer(Buffer, Count);
|
---|
487 | finally
|
---|
488 | Mem.Free;
|
---|
489 | end;
|
---|
490 | end;
|
---|
491 |
|
---|
492 | function TVarBlockSerializer.GetVarSize: Integer;
|
---|
493 | var
|
---|
494 | Data: Byte;
|
---|
495 | StoredPosition: Integer;
|
---|
496 | begin
|
---|
497 | try
|
---|
498 | StoredPosition := Stream.Position;
|
---|
499 | Result := 1; // Byte block length
|
---|
500 | Data := Stream.ReadByte;
|
---|
501 |
|
---|
502 | if Data = $ff then Result := GetVarSize + ReadVarUInt + 1
|
---|
503 | else begin
|
---|
504 | Result := DecodeUnaryLength(Data);
|
---|
505 | end;
|
---|
506 | Stream.Position := StoredPosition;
|
---|
507 | except
|
---|
508 | raise Exception.Create(SErrorGetVarSize);
|
---|
509 | end;
|
---|
510 | end;
|
---|
511 |
|
---|
512 | function TVarBlockSerializer.GetVarCount: Integer;
|
---|
513 | var
|
---|
514 | Data: Byte;
|
---|
515 | StoredPosition: Integer;
|
---|
516 | Skip: Integer;
|
---|
517 | begin
|
---|
518 | StoredPosition := Stream.Position;
|
---|
519 | Result := 0;
|
---|
520 | while Stream.Position < Stream.Size do begin
|
---|
521 | Data := Stream.ReadByte;
|
---|
522 | if Data = $ff then Skip := ReadVarUInt
|
---|
523 | else Skip := DecodeUnaryLength(Data) - 1;
|
---|
524 | Stream.Seek(Skip, soCurrent);
|
---|
525 | Inc(Result);
|
---|
526 | end;
|
---|
527 | Stream.Position := StoredPosition;
|
---|
528 | end;
|
---|
529 |
|
---|
530 | function TVarBlockSerializer.TryVarBlock: Boolean;
|
---|
531 | var
|
---|
532 | Data: Byte;
|
---|
533 | StoredPosition: Integer;
|
---|
534 | Count: Integer;
|
---|
535 | begin
|
---|
536 | if Stream.Position < Stream.Size then
|
---|
537 | try
|
---|
538 | StoredPosition := Stream.Position;
|
---|
539 | Data := Stream.ReadByte;
|
---|
540 | if Data = $ff then begin
|
---|
541 | if TryVarBlock then begin
|
---|
542 | Count := ReadVarUInt;
|
---|
543 | Result := Count <= Stream.Size - Stream.Position;
|
---|
544 | end else Result := False;
|
---|
545 | end else begin
|
---|
546 | Count := DecodeUnaryLength(Data) - 1;
|
---|
547 | Result := Count <= Stream.Size - Stream.Position;
|
---|
548 | end;
|
---|
549 | Stream.Position := StoredPosition;
|
---|
550 | except
|
---|
551 | raise Exception.Create(SErrorGetVarSize);
|
---|
552 | end else Result := False;
|
---|
553 | end;
|
---|
554 |
|
---|
555 | procedure TVarBlockSerializer.WriteVarSInt(Value: Int64);
|
---|
556 | begin
|
---|
557 | if Value < 0 then WriteVarUInt(((-Value) shl 1) - 1)
|
---|
558 | else WriteVarUInt((Value shl 1))
|
---|
559 | end;
|
---|
560 |
|
---|
561 | function TVarBlockSerializer.ReadVarSInt: Int64;
|
---|
562 | begin
|
---|
563 | Result := ReadVarUInt;
|
---|
564 | if (Result and 1) = 0 then Result := Result shr 1
|
---|
565 | else Result := -((Result + 1) shr 1);
|
---|
566 | end;
|
---|
567 |
|
---|
568 | function TVarBlockSerializer.TestMask(Mask: QWord; BitIndex: Byte): Boolean;
|
---|
569 | begin
|
---|
570 | Result := ((Mask shr BitIndex) and 1) = 1;
|
---|
571 | end;
|
---|
572 |
|
---|
573 | function TVarBlockSerializer.BuildMask(Bits:array of Integer):Integer;
|
---|
574 | var
|
---|
575 | I: Integer;
|
---|
576 | begin
|
---|
577 | Result := 0;
|
---|
578 | for I := 0 to High(Bits) do
|
---|
579 | Result := Result or (1 shl Bits[I]);
|
---|
580 | end;
|
---|
581 |
|
---|
582 | procedure TVarBlockSerializer.ReadItemByMaskIndex(Index:Integer; Data:
|
---|
583 | TVarBlockSerializer);
|
---|
584 | var
|
---|
585 | Mask: Integer;
|
---|
586 | I: Integer;
|
---|
587 | StreamHelper: TStreamHelper;
|
---|
588 | begin
|
---|
589 | try
|
---|
590 | StreamHelper := TStreamHelper.Create(Stream);
|
---|
591 | try
|
---|
592 | Stream.Position := 0;
|
---|
593 | Data.Stream.Size := 0;
|
---|
594 | Mask := ReadVarUInt;
|
---|
595 | I := 0;
|
---|
596 | while (Stream.Position < Stream.Size) and (I < Index) do begin
|
---|
597 | if TestMask(Mask, I) then
|
---|
598 | Stream.Position := Stream.Position + GetVarSize;
|
---|
599 | Inc(I);
|
---|
600 | end;
|
---|
601 | if TestMask(Mask, Index) then
|
---|
602 | StreamHelper.ReadStream(Data.Stream, GetVarSize);
|
---|
603 | except
|
---|
604 | //raise Exception.Create(SMaskedValueReadError);
|
---|
605 | // Error recovery for not enough source data in stream
|
---|
606 | Data.Stream.Size := 0;
|
---|
607 | Data.Stream.WriteByte(0);
|
---|
608 | end;
|
---|
609 | finally
|
---|
610 | StreamHelper.Free;
|
---|
611 | Data.Stream.Position := 0;
|
---|
612 | end;
|
---|
613 | end;
|
---|
614 |
|
---|
615 | procedure TVarBlockSerializer.ReadItemRefByMaskIndex(Index:Integer;Data:TSubStream
|
---|
616 | );
|
---|
617 | var
|
---|
618 | Mask: Integer;
|
---|
619 | I: Integer;
|
---|
620 | begin
|
---|
621 | try
|
---|
622 | Stream.Position := 0;
|
---|
623 | Data.Size := 0;
|
---|
624 | Mask := ReadVarUInt;
|
---|
625 | I := 0;
|
---|
626 | while (Stream.Position < Stream.Size) and (I < Index) do begin
|
---|
627 | if TestMask(Mask, I) then Stream.Position := Stream.Position + GetVarSize;
|
---|
628 | Inc(I);
|
---|
629 | end;
|
---|
630 | if TestMask(Mask, Index) then begin
|
---|
631 | if Stream is TSubStream then begin
|
---|
632 | // Recalculate substream
|
---|
633 | Data.Source := TSubStream(Stream).Source;
|
---|
634 | Data.SourcePosition := TSubStream(Stream).SourcePosition + Stream.Position;
|
---|
635 | end else begin
|
---|
636 | Data.Source := Self.Stream;
|
---|
637 | Data.SourcePosition := Stream.Position;
|
---|
638 | end;
|
---|
639 | Data.Size := GetVarSize;
|
---|
640 | end;
|
---|
641 | Data.Position := 0;
|
---|
642 | except
|
---|
643 | raise Exception.Create(SMaskedValueReadError);
|
---|
644 | end;
|
---|
645 | end;
|
---|
646 |
|
---|
647 | procedure TVarBlockSerializer.BlockEnclose;
|
---|
648 | var
|
---|
649 | Temp: TVarBlockSerializer;
|
---|
650 | StreamHelper: TStreamHelper;
|
---|
651 | begin
|
---|
652 | try
|
---|
653 | Temp := TVarBlockSerializer.Create;
|
---|
654 | StreamHelper := TStreamHelper.Create(Temp.Stream);
|
---|
655 | StreamHelper.WriteStream(Stream, Stream.Size);
|
---|
656 | Stream.Size := 0;
|
---|
657 | WriteVarBlock(Temp);
|
---|
658 | finally
|
---|
659 | StreamHelper.Free;
|
---|
660 | Temp.Free;
|
---|
661 | end;
|
---|
662 | end;
|
---|
663 |
|
---|
664 | procedure TVarBlockSerializer.BlockUnclose;
|
---|
665 | var
|
---|
666 | Temp: TVarBlockSerializer;
|
---|
667 | StreamHelper: TStreamHelper;
|
---|
668 | begin
|
---|
669 | try
|
---|
670 | Temp := TVarBlockSerializer.Create;
|
---|
671 | StreamHelper := TStreamHelper.Create(Stream);
|
---|
672 | Stream.Position := 0;
|
---|
673 | ReadVarBlock(Temp);
|
---|
674 | Stream.Size := 0;
|
---|
675 | StreamHelper.WriteStream(Temp.Stream, Temp.Stream.Size);
|
---|
676 | finally
|
---|
677 | Stream.Position := 0;
|
---|
678 | StreamHelper.Free;
|
---|
679 | Temp.Free;
|
---|
680 | end;
|
---|
681 | end;
|
---|
682 |
|
---|
683 | procedure TVarBlockSerializer.Assign(Source: TVarBlockSerializer);
|
---|
684 | var
|
---|
685 | OldPos: Integer;
|
---|
686 | begin
|
---|
687 | OwnsStream := Source.OwnsStream;
|
---|
688 | FStream.Size := 0;
|
---|
689 | OldPos := Source.FStream.Position;
|
---|
690 | FStream.CopyFrom(Source.FStream, Source.FStream.Size);
|
---|
691 | Source.FStream.Position := OldPos;
|
---|
692 | FStream.Position := OldPos;
|
---|
693 | end;
|
---|
694 |
|
---|
695 | constructor TVarBlockSerializer.Create;
|
---|
696 | begin
|
---|
697 | inherited;
|
---|
698 | Stream := TStreamHelper.Create;
|
---|
699 | OwnsStream := True;
|
---|
700 | TStreamHelper(Stream).Endianness := enBig;
|
---|
701 | end;
|
---|
702 |
|
---|
703 | destructor TVarBlockSerializer.Destroy;
|
---|
704 | begin
|
---|
705 | if OwnsStream then begin
|
---|
706 | FreeAndNil(FStream);
|
---|
707 | end;
|
---|
708 | inherited;
|
---|
709 | end;
|
---|
710 |
|
---|
711 | { TVarBlockIndexed }
|
---|
712 |
|
---|
713 | procedure TVarBlockIndexed.CheckItem(Index:Integer);
|
---|
714 | begin
|
---|
715 | if Items.Count > Index then begin
|
---|
716 | if not Assigned(Items[Index]) then
|
---|
717 | Items[Index] := TVarBlockSerializer.Create;
|
---|
718 | TVarBlockSerializer(Items[Index]).Stream.Size := 0;
|
---|
719 | end else begin
|
---|
720 | Items.Count := Index + 1;
|
---|
721 | Items[Index] := TVarBlockSerializer.Create;
|
---|
722 | end;
|
---|
723 | end;
|
---|
724 |
|
---|
725 | procedure TVarBlockIndexed.Assign(Source: TVarBlockIndexed);
|
---|
726 | var
|
---|
727 | I: Integer;
|
---|
728 | begin
|
---|
729 | Enclose := Source.Enclose;
|
---|
730 | Items.Count := 0;
|
---|
731 | Items.Count := Source.Items.Count;
|
---|
732 | for I := 0 to Items.Count - 1 do begin
|
---|
733 | Items[I] := TVarBlockSerializer.Create;
|
---|
734 | TVarBlockSerializer(Items[I]).Assign(TVarBlockSerializer(Source.Items[I]));
|
---|
735 | end;
|
---|
736 | end;
|
---|
737 |
|
---|
738 | procedure TVarBlockIndexed.WriteVarUInt(Index:Integer;Value:QWord);
|
---|
739 | begin
|
---|
740 | CheckItem(Index);
|
---|
741 | TVarBlockSerializer(Items[Index]).WriteVarUInt(Value);
|
---|
742 | end;
|
---|
743 |
|
---|
744 | function TVarBlockIndexed.ReadVarUInt(Index:Integer):QWord;
|
---|
745 | begin
|
---|
746 | TVarBlockSerializer(Items[Index]).Stream.Position := 0;
|
---|
747 | Result := TVarBlockSerializer(Items[Index]).ReadVarUInt;
|
---|
748 | end;
|
---|
749 |
|
---|
750 | procedure TVarBlockIndexed.WriteVarBlock(Index: Integer; Block: TVarBlockSerializer);
|
---|
751 | begin
|
---|
752 | CheckItem(Index);
|
---|
753 | TVarBlockSerializer(Items[Index]).WriteVarBlock(Block);
|
---|
754 | end;
|
---|
755 |
|
---|
756 | procedure TVarBlockIndexed.ReadVarBlock(Index: Integer; Block: TVarBlockSerializer);
|
---|
757 | begin
|
---|
758 | with TVarBlockSerializer(Items[Index]) do begin
|
---|
759 | Stream.Position := 0;
|
---|
760 | ReadVarBlock(Block);
|
---|
761 | end;
|
---|
762 | end;
|
---|
763 |
|
---|
764 | procedure TVarBlockIndexed.WriteVarStream(Index: Integer; Stream: TStream);
|
---|
765 | begin
|
---|
766 | CheckItem(Index);
|
---|
767 | TVarBlockSerializer(Items[Index]).WriteVarStream(Stream);
|
---|
768 | end;
|
---|
769 |
|
---|
770 | procedure TVarBlockIndexed.ReadVarStream(Index: Integer; Stream: TStream);
|
---|
771 | begin
|
---|
772 | TVarBlockSerializer(Items[Index]).Stream.Position := 0;
|
---|
773 | TVarBlockSerializer(Items[Index]).ReadVarStream(Stream);
|
---|
774 | end;
|
---|
775 |
|
---|
776 | procedure TVarBlockIndexed.WriteVarList(Index: Integer; List: TListByte);
|
---|
777 | begin
|
---|
778 | CheckItem(Index);
|
---|
779 | TVarBlockSerializer(Items[Index]).WriteVarList(List);
|
---|
780 | end;
|
---|
781 |
|
---|
782 | procedure TVarBlockIndexed.ReadVarList(Index: Integer; List: TListByte);
|
---|
783 | begin
|
---|
784 | TVarBlockSerializer(Items[Index]).Stream.Position := 0;
|
---|
785 | TVarBlockSerializer(Items[Index]).ReadVarList(List);
|
---|
786 | end;
|
---|
787 |
|
---|
788 | procedure TVarBlockIndexed.WriteVarIndexedBlock(Index: Integer;
|
---|
789 | Block: TVarBlockIndexed);
|
---|
790 | var
|
---|
791 | Temp: TStreamHelper;
|
---|
792 | begin
|
---|
793 | try
|
---|
794 | Temp := TStreamHelper.Create;
|
---|
795 | Block.Enclose := False;
|
---|
796 | Block.WriteToStream(Temp);
|
---|
797 | WriteVarStream(Index, Temp);
|
---|
798 | finally
|
---|
799 | Temp.Free;
|
---|
800 | end;
|
---|
801 | end;
|
---|
802 |
|
---|
803 | procedure TVarBlockIndexed.ReadVarIndexedBlock(Index: Integer;
|
---|
804 | Block: TVarBlockIndexed);
|
---|
805 | var
|
---|
806 | Temp: TStreamHelper;
|
---|
807 | begin
|
---|
808 | try
|
---|
809 | Temp := TStreamHelper.Create;
|
---|
810 | Block.Enclose := False;
|
---|
811 | ReadVarStream(Index, Temp);
|
---|
812 | Block.ReadFromStream(Temp);
|
---|
813 | finally
|
---|
814 | Temp.Free;
|
---|
815 | end;
|
---|
816 | end;
|
---|
817 |
|
---|
818 | procedure TVarBlockIndexed.WriteVarBuffer(Index: Integer; var Buffer;
|
---|
819 | Count: Integer);
|
---|
820 | begin
|
---|
821 | CheckItem(Index);
|
---|
822 | TVarBlockSerializer(Items[Index]).WriteVarBuffer(Buffer, Count);
|
---|
823 | end;
|
---|
824 |
|
---|
825 | procedure TVarBlockIndexed.ReadVarBuffer(Index: Integer; var Buffer;
|
---|
826 | Count: Integer);
|
---|
827 | begin
|
---|
828 | CheckItem(Index);
|
---|
829 | TVarBlockSerializer(Items[Index]).ReadVarBuffer(Buffer, Count);
|
---|
830 | end;
|
---|
831 |
|
---|
832 | procedure TVarBlockIndexed.WriteVarSInt(Index: Integer; Value:Int64);
|
---|
833 | begin
|
---|
834 | CheckItem(Index);
|
---|
835 | TVarBlockSerializer(Items[Index]).WriteVarSInt(Value);
|
---|
836 | end;
|
---|
837 |
|
---|
838 | function TVarBlockIndexed.ReadVarSInt(Index: Integer): Int64;
|
---|
839 | begin
|
---|
840 | TVarBlockSerializer(Items[Index]).Stream.Position := 0;
|
---|
841 | Result := TVarBlockSerializer(Items[Index]).ReadVarSInt;
|
---|
842 | end;
|
---|
843 |
|
---|
844 | procedure TVarBlockIndexed.WriteVarFloat(Index: Integer; Value:Double; Base: Integer = 2);
|
---|
845 | begin
|
---|
846 | CheckItem(Index);
|
---|
847 | TVarBlockSerializer(Items[Index]).WriteVarFloat(Value, Base);
|
---|
848 | end;
|
---|
849 |
|
---|
850 | function TVarBlockIndexed.ReadVarFloat(Index: Integer; Base: Integer = 2):Double;
|
---|
851 | begin
|
---|
852 | TVarBlockSerializer(Items[Index]).Stream.Position := 0;
|
---|
853 | Result := TVarBlockSerializer(Items[Index]).ReadVarFloat(Base);
|
---|
854 | end;
|
---|
855 |
|
---|
856 | procedure TVarBlockIndexed.WriteVarDouble(Index: Integer; Value: Double);
|
---|
857 | begin
|
---|
858 | CheckItem(Index);
|
---|
859 | TVarBlockSerializer(Items[Index]).WriteVarDouble(Value);
|
---|
860 | end;
|
---|
861 |
|
---|
862 | function TVarBlockIndexed.ReadVarDouble(Index: Integer): Double;
|
---|
863 | begin
|
---|
864 | TVarBlockSerializer(Items[Index]).Stream.Position := 0;
|
---|
865 | Result := TVarBlockSerializer(Items[Index]).ReadVarDouble;
|
---|
866 | end;
|
---|
867 |
|
---|
868 | procedure TVarBlockIndexed.WriteVarString(Index: Integer; Value:string);
|
---|
869 | begin
|
---|
870 | CheckItem(Index);
|
---|
871 | TVarBlockSerializer(Items[Index]).WriteVarString(Value);
|
---|
872 | end;
|
---|
873 |
|
---|
874 | function TVarBlockIndexed.ReadVarString(Index: Integer):string;
|
---|
875 | begin
|
---|
876 | with TVarBlockSerializer(Items[Index]) do begin
|
---|
877 | Stream.Position := 0;
|
---|
878 | Result := ReadVarString;
|
---|
879 | end;
|
---|
880 | end;
|
---|
881 |
|
---|
882 | procedure TVarBlockIndexed.WriteVarUIntArray(Index: Integer;
|
---|
883 | List: TListInteger);
|
---|
884 | var
|
---|
885 | I: Integer;
|
---|
886 | Temp: TVarBlockSerializer;
|
---|
887 | begin
|
---|
888 | try
|
---|
889 | Temp := TVarBlockSerializer.Create;
|
---|
890 | for I := 0 to List.Count - 1 do
|
---|
891 | Temp.WriteVarUInt(Integer(List[I]));
|
---|
892 | WriteVarBlock(Index, Temp);
|
---|
893 | finally
|
---|
894 | Temp.Free;
|
---|
895 | end;
|
---|
896 | end;
|
---|
897 |
|
---|
898 | procedure TVarBlockIndexed.ReadVarUIntArray(Index: Integer;
|
---|
899 | List: TListInteger);
|
---|
900 | var
|
---|
901 | Temp: TVarBlockSerializer;
|
---|
902 | begin
|
---|
903 | try
|
---|
904 | Temp := TVarBlockSerializer.Create;
|
---|
905 | List.Clear;
|
---|
906 | ReadVarBlock(Index, Temp);
|
---|
907 | while Temp.Stream.Position < Temp.Stream.Size do begin
|
---|
908 | List.Add(Temp.ReadVarUInt);
|
---|
909 | end;
|
---|
910 | finally
|
---|
911 | Temp.Free;
|
---|
912 | end;
|
---|
913 | end;
|
---|
914 |
|
---|
915 | procedure TVarBlockIndexed.WriteVarStringArray(Index: Integer;
|
---|
916 | List: TListString);
|
---|
917 | var
|
---|
918 | I: Integer;
|
---|
919 | Temp: TVarBlockSerializer;
|
---|
920 | begin
|
---|
921 | try
|
---|
922 | Temp := TVarBlockSerializer.Create;
|
---|
923 | for I := 0 to List.Count - 1 do
|
---|
924 | Temp.WriteVarString(List[I]);
|
---|
925 | WriteVarBlock(Index, Temp);
|
---|
926 | finally
|
---|
927 | Temp.Free;
|
---|
928 | end;
|
---|
929 | end;
|
---|
930 |
|
---|
931 | procedure TVarBlockIndexed.ReadVarStringArray(Index: Integer; List: TListString
|
---|
932 | );
|
---|
933 | var
|
---|
934 | Temp: TVarBlockSerializer;
|
---|
935 | begin
|
---|
936 | try
|
---|
937 | Temp := TVarBlockSerializer.Create;
|
---|
938 | List.Clear;
|
---|
939 | ReadVarBlock(Index, Temp);
|
---|
940 | while Temp.Stream.Position < Temp.Stream.Size do begin
|
---|
941 | List.Add(Temp.ReadVarString);
|
---|
942 | end;
|
---|
943 | finally
|
---|
944 | Temp.Free;
|
---|
945 | end;
|
---|
946 | end;
|
---|
947 |
|
---|
948 | procedure TVarBlockIndexed.Clear;
|
---|
949 | begin
|
---|
950 | Items.Clear;
|
---|
951 | end;
|
---|
952 |
|
---|
953 | function TVarBlockIndexed.TestIndex(Index: Integer):Boolean;
|
---|
954 | begin
|
---|
955 | if (Index >= 0) and (Index < Items.Count) then
|
---|
956 | Result := Assigned(Items[Index])
|
---|
957 | else Result := False
|
---|
958 | end;
|
---|
959 |
|
---|
960 | procedure TVarBlockIndexed.WriteToVarBlock(VarBlock: TVarBlockSerializer);
|
---|
961 | var
|
---|
962 | Mask: Integer;
|
---|
963 | I: Integer;
|
---|
964 | StreamHelper: TStreamHelper;
|
---|
965 | Temp: TVarBlockSerializer;
|
---|
966 | Output: TVarBlockSerializer;
|
---|
967 | begin
|
---|
968 | try
|
---|
969 | if Enclose then begin
|
---|
970 | Temp := TVarBlockSerializer.Create;
|
---|
971 | Output := Temp;
|
---|
972 | end else begin
|
---|
973 | Temp := nil;
|
---|
974 | Output := VarBlock;
|
---|
975 | end;
|
---|
976 | StreamHelper := TStreamHelper.Create(Output.Stream);
|
---|
977 |
|
---|
978 | Output.Stream.Size := 0;
|
---|
979 | Mask := 0;
|
---|
980 | for I := 0 to Items.Count - 1 do
|
---|
981 | if Assigned(Items[I]) then Mask := Mask or (1 shl I);
|
---|
982 | Output.WriteVarUInt(Mask);
|
---|
983 | for I := 0 to Items.Count - 1 do
|
---|
984 | if Assigned(Items[I]) then
|
---|
985 | StreamHelper.WriteStream(TVarBlockSerializer(Items[I]).Stream,
|
---|
986 | TVarBlockSerializer(Items[I]).Stream.Size);
|
---|
987 |
|
---|
988 | if Enclose then VarBlock.WriteVarBlock(Temp);
|
---|
989 | finally
|
---|
990 | if Assigned(Temp) then Temp.Free;
|
---|
991 | StreamHelper.Free;
|
---|
992 | end;
|
---|
993 | end;
|
---|
994 |
|
---|
995 | procedure TVarBlockIndexed.ReadFromVarBlock(VarBlock: TVarBlockSerializer);
|
---|
996 | var
|
---|
997 | Mask: Integer;
|
---|
998 | I: Integer;
|
---|
999 | Temp: TVarBlockSerializer;
|
---|
1000 | Input: TVarBlockSerializer;
|
---|
1001 | StreamHelper: TStreamHelper;
|
---|
1002 | begin
|
---|
1003 | try
|
---|
1004 | StreamHelper := TStreamHelper.Create;
|
---|
1005 | if Enclose then begin
|
---|
1006 | Temp := TVarBlockSerializer.Create;
|
---|
1007 | Temp.ReadVarBlock(VarBlock);
|
---|
1008 | Input := Temp;
|
---|
1009 | end else begin
|
---|
1010 | Temp := nil;
|
---|
1011 | Input := VarBlock;
|
---|
1012 | end;
|
---|
1013 | StreamHelper.Stream := Input.Stream;
|
---|
1014 |
|
---|
1015 | Input.Stream.Position := 0;
|
---|
1016 | Mask := Input.ReadVarUInt;
|
---|
1017 | Items.Clear;
|
---|
1018 | I := 0;
|
---|
1019 | while (Mask <> 0) and (Input.Stream.Position < Input.Stream.Size) do begin
|
---|
1020 | if Input.TestMask(Mask, I) then begin
|
---|
1021 | CheckItem(I);
|
---|
1022 | TVarBlockSerializer(Items[I]).Stream.Size := 0;
|
---|
1023 | StreamHelper.ReadStream(TVarBlockSerializer(Items[I]).Stream, Input.GetVarSize);
|
---|
1024 | //Input.ReadItemByMaskIndex(I, TVarBlockSerializer(Items[I]));
|
---|
1025 | Mask := Mask xor (1 shl I); // Clear bit on current index
|
---|
1026 | end;
|
---|
1027 | Inc(I);
|
---|
1028 | end;
|
---|
1029 | finally
|
---|
1030 | if Assigned(Temp) then Temp.Free;
|
---|
1031 | StreamHelper.Free;
|
---|
1032 | end;
|
---|
1033 | end;
|
---|
1034 |
|
---|
1035 | procedure TVarBlockIndexed.WriteToStream(Stream: TStream);
|
---|
1036 | var
|
---|
1037 | Temp: TVarBlockSerializer;
|
---|
1038 | StreamHelper: TStreamHelper;
|
---|
1039 | begin
|
---|
1040 | try
|
---|
1041 | Temp := TVarBlockSerializer.Create;
|
---|
1042 | StreamHelper := TStreamHelper.Create(Stream);
|
---|
1043 | WriteToVarBlock(Temp);
|
---|
1044 | StreamHelper.WriteStream(Temp.Stream, Temp.Stream.Size);
|
---|
1045 | finally
|
---|
1046 | StreamHelper.Free;
|
---|
1047 | Temp.Free;
|
---|
1048 | end;
|
---|
1049 | end;
|
---|
1050 |
|
---|
1051 | procedure TVarBlockIndexed.ReadFromStream(Stream: TStream);
|
---|
1052 | var
|
---|
1053 | VarBlock: TVarBlockSerializer;
|
---|
1054 | begin
|
---|
1055 | try
|
---|
1056 | VarBlock := TVarBlockSerializer.Create;
|
---|
1057 | VarBlock.Stream := Stream;
|
---|
1058 | ReadFromVarBlock(VarBlock);
|
---|
1059 | finally
|
---|
1060 | VarBlock.Free;
|
---|
1061 | end;
|
---|
1062 | end;
|
---|
1063 |
|
---|
1064 | procedure TVarBlockIndexed.WriteToList(List: TListByte);
|
---|
1065 | var
|
---|
1066 | Mem: TMemoryStream;
|
---|
1067 | begin
|
---|
1068 | try
|
---|
1069 | Mem := TMemoryStream.Create;
|
---|
1070 | WriteToStream(Mem);
|
---|
1071 | List.Count := Mem.Size;
|
---|
1072 | List.ReplaceStream(Mem);
|
---|
1073 | finally
|
---|
1074 | Mem.Free;
|
---|
1075 | end;
|
---|
1076 | end;
|
---|
1077 |
|
---|
1078 | procedure TVarBlockIndexed.ReadFromList(List: TListByte);
|
---|
1079 | var
|
---|
1080 | Mem: TMemoryStream;
|
---|
1081 | begin
|
---|
1082 | try
|
---|
1083 | Mem := TMemoryStream.Create;
|
---|
1084 | List.WriteToStream(Mem);
|
---|
1085 | ReadFromStream(Mem);
|
---|
1086 | finally
|
---|
1087 | Mem.Free;
|
---|
1088 | end;
|
---|
1089 | end;
|
---|
1090 |
|
---|
1091 | constructor TVarBlockIndexed.Create;
|
---|
1092 | begin
|
---|
1093 | Items := TListObject.Create;
|
---|
1094 | Enclose := True;
|
---|
1095 | end;
|
---|
1096 |
|
---|
1097 | destructor TVarBlockIndexed.Destroy;
|
---|
1098 | begin
|
---|
1099 | FreeAndNil(Items);
|
---|
1100 | inherited;
|
---|
1101 | end;
|
---|
1102 |
|
---|
1103 | end.
|
---|