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