-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTaggedBinaryData.pas
More file actions
790 lines (653 loc) · 29.1 KB
/
Copy pathTaggedBinaryData.pas
File metadata and controls
790 lines (653 loc) · 29.1 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
TaggedBinaryData
Set of very simple classes (descendants of TStream) intended for
serialization and deserialization of binary data into/from streams.
Each data point is stored with its tag and the tagged values are split
into groups called contexts.
Tag is an 8bit unsigned integer (byte) that can have value between 0 and
254. Value of 255 is reserved for a context tag - this tag signals that
next byte(s) are not data, but a new context.
Context is a 16bit unsigned integer that can be set to any value (none
is reserved). Since the tag has very limited range, contexts are here to
allow for more unique identification of data points.
Unlike tags, which are stored before each data point, context is stored
only when it changes. You should refrain from changing it too often, as
each context change stores 4 more bytes into the resulting stream.
General structure of taged binary data can be described as this:
TaggedBinaryData
Signature - four byte signature ($54, $42, $44, $53)
ContextGroup[] - array of context groups
ClosingSequence - bytes $FF, $80 (see further for details)
There can be no context group written, in which case absolutely nothing
is written in the stream (not even signature or closing sequence - but
see property WriteEmptyStream of the writer class).
The closing sequence consinsts of context tag ($FF), which marks a
context change, followed by context flags without an actual new context
ID. In the context flags, a close flag is set - this actually marks the
end of tagged data (usually no other flag is set, so the value $80).
Context group is a sequence of context tag, context flags, context ID
and an array of tagged data points:
ContextGroup
ContextTag - tag of value $FF
ContextFlags - flags for this context group
ContextID - ID of this context group
TaggedDataPoint[] - array of tagged data points
... where TaggedDataPoint can be seen as:
TaggedDataPoint
Tag - tag for this data point
Data - actual data (of variable size)
Note that the first context group might not start with a ContextGroup
pseudostructure. Instead, it can only contain and array of tagged data
points. In such case, this group has an implicit ID of 0 and flags are
empty.
All metadata (signature, context ID) are written with little endianess.
Version 1.0.4 (2026-08-10)
Last change 2026-08-10
©2022-2026 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.TaggedBinaryData
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
Library AuxExceptions is required only when rebasing local exception classes
(see symbol TaggedBinaryData_UseAuxExceptions for details).
BinaryStreamingLite can be replaced by full BinaryStreaming.
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
ListUtils - github.com/TheLazyTomcat/Lib.ListUtils
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit TaggedBinaryData;
{
TaggedBinaryData_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
TaggedBinaryData_UseAuxExceptions to achieve this.
}
{$IF Defined(TaggedBinaryData_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IFDEF FPC}
{$MODE ObjFPC}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes,
AuxTypes, AuxClasses{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ETBDException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ETBDInvalidValue = class(ETBDException);
ETBDReadError = class(ETBDException);
ETBDWriteError = class(ETBDException);
{===============================================================================
Common types and constants
===============================================================================}
type
TTBDContextFlags = UInt8;
TTBDContextID = UInt16;
TTBDTag = UInt8;
const
TBD_SIGNATURE = UInt32($53444254); // TBDS when read as a string
TBD_CTXFLAGS_FLAG_CLOSE = TTBDContextFlags($80);
TBD_TAG_CONTEXT = TTBDTag(-1);
{===============================================================================
--------------------------------------------------------------------------------
TTaggedBinaryDataWriter
--------------------------------------------------------------------------------
===============================================================================}
{
Reading is not allowed in writer, a call to method Read (be it explicit or
implicit) will raise an ETBDReadError exception.
Seeking is directly passed to destination stream.
All writes of metadata (context changes, tags and a signature) are deferred
to a first call to Write method. Meaning no matter how many times you call
SetContext or SetTag, nothing will be written into destination stream until
you write some actual data, at which point only the last set context and tag
will be written (and possibly the signature if at the start of stream).
At the start, no context is written into destination, unless you explicitly
change it (first, implicit, context has ID of 0) - first written thing after
signature will be tag of the first data. If you do not set the tag before
writing data, it will be 0.
If current context matches the one last stored, then no context will be
written, even if you explicitly set it.
When property WriteEmptyStream is set to false (default value), then, if you
do not write any data, nothing is stored in the destination, not even the
signature or closing sequence will be written.
WriteEmptyStream of true ensures that at least signature and closing sequence
is written into destination stream, even if you do not write any actual data.
It is possible to store compound data via multiple calls to write, only the
first write after SetTag will actually write the tag (and other metadata).
If context change is written, it also means that a tag is written, even if
you do not explicitly set it before. This ensures that there are no bare
(tag-less) data just after the context.
An example on how to use the writer could be something like this (uses
BinaryStreaming library):
Writer := TTaggedBinaryDataWriter.Create(DestinationStream);
try
(* implicit context ID (0) *)
Stream_WriteInt16(Writer.SetTag(0),<value_C0_T0>);
Stream_WriteFloat32(Writer.SetTag(1),<value_C0_T1>);
Stream_WriteString(Writer.SetTag(2),<value_C0_T2>);
Writer.SetContext(1);
Stream_WriteAnsiChar(Writer.SetTag(0),<value_C1_T0>);
Writer.SetContext(22);
Stream_WriteInt64(Writer.SetTag(100),<value_C22_T100>);
Stream_WriteInt64(Writer.SetTag(200),<value_C22_T200>);
finally
Writer.Free;
end;
This will produce a following byte sequence in the destination stream:
5442445300<v0>01<v1>02<v3>FF00010000<v4>FF00160064<v5>C8<v6>FF80
...where the bytes have following meanings:
54424453 - signature
00 - tag 0
<v0> - value_C0_T0
01 - tag 1
<v1> - value_C0_T1
02 - tag 2
<v3> - value_C0_T2
FF - context tag
00 - context flags
0100 - context ID 1
00 - tag 0
<v4> - value_C1_T0
FF - context tag
00 - context flags
1600 - context ID 22
64 - tag 100
<v5> - value_C22_T100
C8 - tag 200
<v6> - value_C22_T200
FF - context tag
80 - context flags with close flag set
}
{===============================================================================
TTaggedBinaryDataWriter - class declaration
===============================================================================}
type
TTaggedBinaryDataWriter = class(TStream)
protected
fDestination: TStream;
fDeferredActions: set of (daWriteSignature,daWriteContext,daWriteTag);
fPreviousContext: TTBDContextID;
fCurrentContext: TTBDContextID;
fCurrentTag: TTBDTag;
fWriteEmptyStream: Boolean;
fMetaBytes: Int64;
fDataBytes: Int64;
Function GetUtilization: Double; virtual;
procedure Initialize(Destination: TStream); virtual;
procedure Finalize; virtual;
procedure WriteSignature; virtual;
procedure WriteContext; virtual;
procedure WriteTag; virtual;
procedure WriteClose; virtual;
public
constructor Create(Destination: TStream);
destructor Destroy; override;
Function Read(var Buffer; Count: LongInt): LongInt; override;
Function Write(const Buffer; Count: LongInt): LongInt; override;
Function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
procedure SetContext(Context: TTBDContextID); virtual;
{
SetTag
Returns reference to self, so it can be used for inline tag set and write,
for example:
Writer.SetTag(15).WriteBuffer(Buff,SizeOf(Buff));
Of course, if you hate such constructs, do not use it ;)
}
Function SetTag(Tag: TTBDTag): TStream; virtual;
property Destination: TStream read fDestination;
property CurrentContext: TTBDContextID read fCurrentContext;
property CurrentTag: TTBDTag read fCurrentTag;
property WriteEmptyStream: Boolean read fWriteEmptyStream write fWriteEmptyStream;
{
MetaBytes
Number of bytes written that are not actual data - this includes signature,
contexts and tags.
Overwrites (eg. after back-seeking) are not catched - this number counts
all meta data written, not what eventually ends-up in the stream.
}
property MetaBytes: Int64 read fMetaBytes;
{
DataBytes
Number of bytes written that belongs to the actual user-written data, not
meta data (tags, contexts, ...).
Overwrites are not catched - this number counts all actual data written,
not what eventually ends-up in the stream.
}
property DataBytes: Int64 read fDataBytes;
{
Utilization
Indicates how many of written bytes were actual data, not metadata (tags,
contexts, ...). It is given as normalized value in range [0,1], where 0
means that all written bytes were metadata, 1 means all written bytes were
actual usefull data. It is calculated like:
Utilization := DataBytes / (DataBytes + MetaBytes)
If no data were writen, then 0 is returned.
}
property Utilization: Double read GetUtilization;
end;
// shorter alias
TTBDWriter = TTaggedBinaryDataWriter;
{===============================================================================
--------------------------------------------------------------------------------
TTaggedBinaryDataReader
--------------------------------------------------------------------------------
===============================================================================}
{
Writing is not allowed in reader, a call to method Write (be it explicit or
implicit) will raise an ETBDWriteError exception.
Both seeking and reading are directly passed to source stream.
To properly use the reader, call method GetTag once (and only once) before
every data point. This method will try to load next stored tag and, if
necessary, a new context information.
When GetTag returns true, it indicates that a tag was read and properties
CurrentContext and CurrentTag now contains proper values. Use those values
to discern which data point to read next. Size of the data point is not
managed by this library so you are responsible to read proper number of
bytes (if you fail to do so, you will damage the reading process and further
behavior of the reader is completely undefined).
When it returns false, it indicates that either the end of source stream or
end of tagged binary data stream pseudostructure (also indicated by property
EndOfDataReached) was reached. In any case, you should stop reading any
further data points. Also, in this situation, values stored in properties
CurrentContext and CurrentTag are undefined.
An example how to use the reader could be (note that it is reading the same
data that would be stored in the example for writer - see above):
Reader := TTaggedBinaryDataReader.Create(SourceStream);
try
while Reader.GetTag do
case Reader.CurrentContext of
0: case Reader.CurrentTag of
0: <value_C0_T0> := Stream_GetInt16(Reader);
1: <value_C0_T1> := Stream_GetFloat32(Reader);
2: <value_C0_T2> := Stream_GetString(Reader);
end;
1: If Reader.CurrentTag = 0 then
<value_C1_T0> := Stream_GetAnsiChar(Reader);
22: case Reader.CurrentTag of
100: <value_C22_T100> := Stream_GetInt64(Reader);
200: <value_C22_T200> := Stream_GetInt64(Reader);
end;
end;
finally
Reader.Free;
end;
This is just one possible approach, you can create your own implementation,
for example using provided events fired on context and tag change...
NOTE - OnTagChange* is called even if the actual tag does not change
from last occurence. This is to account for situation where
two or more datapoints of the same tag are stored together.
}
{===============================================================================
TTaggedBinaryDataReader - class declaration
===============================================================================}
type
TTaggedBinaryDataReader = class(TStream)
protected
fSource: TStream;
fEndOfDataReached: Boolean;
fCurrentContext: TTBDContextID;
fCurrentTag: TTBDTag;
fInitialContext: Boolean;
fMetaBytes: Int64;
fDataBytes: Int64;
fContextChangeEvent: TNotifyEvent;
fContextChangeCallback: TNotifyCallback;
fTagChangeEvent: TNotifyEvent;
fTagChangeCallback: TNotifyCallback;
Function GetUtilization: Double; virtual;
procedure Initialize(Source: TStream); virtual;
procedure Finalize; virtual;
procedure DoContextChange; virtual;
procedure DoTagChange; virtual;
public
constructor Create(Source: TStream);
destructor Destroy; override;
Function Read(var Buffer; Count: LongInt): LongInt; override;
Function Write(const Buffer; Count: LongInt): LongInt; override;
Function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
Function GetTag: Boolean; virtual;
property Source: TStream read fSource;
property EndOfDataReached: Boolean read fEndOfDataReached;
property CurrentContext: TTBDContextID read fCurrentContext;
property CurrentTag: TTBDTag read fCurrentTag;
property MetaBytes: Int64 read fMetaBytes;
property DataBytes: Int64 read fDataBytes;
property Utilization: Double read GetUtilization;
property OnContextChangeEvent: TNotifyEvent read fContextChangeEvent write fContextChangeEvent;
property OnContextChangeCallback: TNotifyCallback read fContextChangeCallback write fContextChangeCallback;
property OnContextChange: TNotifyEvent read fContextChangeEvent write fContextChangeEvent;
property OnTagChangeEvent: TNotifyEvent read fTagChangeEvent write fTagChangeEvent;
property OnTagChangeCallback: TNotifyCallback read fTagChangeCallback write fTagChangeCallback;
property OnTagChange: TNotifyEvent read fTagChangeEvent write fTagChangeEvent;
end;
TTBDReader = TTaggedBinaryDataReader;
implementation
uses
BinaryStreamingLite;
{===============================================================================
Auxiliary routines
===============================================================================}
Function ConsumeArgs(const Args: array of const): Integer;
begin
Result := Length(Args) * 0;
end;
{===============================================================================
--------------------------------------------------------------------------------
TTaggedBinaryDataWriter
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TTaggedBinaryDataWriter - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TTaggedBinaryDataWriter - protected methods
-------------------------------------------------------------------------------}
Function TTaggedBinaryDataWriter.GetUtilization: Double;
begin
If (fMetaBytes + fDataBytes) <> 0 then
Result := fDataBytes / (fDataBytes + fMetaBytes)
else
Result := 0.0;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataWriter.Initialize(Destination: TStream);
begin
If Assigned(Destination) then
fDestination := Destination
else
raise ETBDInvalidValue.Create('TTaggedBinaryDataWriter.Initialize: Destination stream not assigned.');
fDeferredActions := [daWriteSignature,daWriteContext,daWriteTag];
fPreviousContext := 0;
fCurrentContext := 0;
fCurrentTag := 0;
fWriteEmptyStream := False;
fMetaBytes := 0;
fDataBytes := 0;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataWriter.Finalize;
begin
If Assigned(fDestination) then
WriteClose;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataWriter.WriteSignature;
begin
Stream_WriteUInt32(fDestination,TBD_SIGNATURE);
Exclude(fDeferredActions,daWriteSignature);
Inc(fMetaBytes,SizeOf(UInt32));
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataWriter.WriteContext;
begin
// write context only if it really changed
If fCurrentContext <> fPreviousContext then
begin
Stream_WriteUInt8(fDestination,TBD_TAG_CONTEXT);
Stream_WriteUInt8(fDestination,0{flags, nothing implemented atm});
Stream_WriteUInt16(fDestination,fCurrentContext);
// make sure bare data are not written directly after the context
Include(fDeferredActions,daWriteTag);
Inc(fMetaBytes,SizeOf(TTBDTag) + SizeOf(TTBDContextFlags) + SizeOf(TTBDContextID));
end;
Exclude(fDeferredActions,daWriteContext);
fPreviousContext := fCurrentContext;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataWriter.WriteTag;
begin
Stream_WriteUInt8(fDestination,fCurrentTag);
Exclude(fDeferredActions,daWriteTag);
Inc(fMetaBytes,SizeOf(TTBDTag));
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataWriter.WriteClose;
begin
If not(daWriteSignature in fDeferredActions){something was written} or fWriteEmptyStream then
begin
If daWriteSignature in fDeferredActions then
WriteSignature;
// closing tag
Stream_WriteUInt8(fDestination,TBD_TAG_CONTEXT);
// terminating context flags without context id
Stream_WriteUInt8(fDestination,TBD_CTXFLAGS_FLAG_CLOSE);
Inc(fMetaBytes,SizeOf(TTBDTag) + SizeOf(TTBDContextFlags));
end;
end;
{-------------------------------------------------------------------------------
TTaggedBinaryDataWriter - public methods
-------------------------------------------------------------------------------}
constructor TTaggedBinaryDataWriter.Create(Destination: TStream);
begin
inherited Create;
Initialize(Destination);
end;
//------------------------------------------------------------------------------
destructor TTaggedBinaryDataWriter.Destroy;
begin
Finalize;
inherited;
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataWriter.Read(var Buffer; Count: LongInt): LongInt;
begin
{$IFDEF FPC}Result := {$ENDIF}ConsumeArgs([@Buffer,Count]);
raise ETBDReadError.Create('TTaggedBinaryDataWriter.Read: Reading not allowed.');
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataWriter.Write(const Buffer; Count: LongInt): LongInt;
begin
If daWritesignature in fDeferredActions then
WriteSignature;
If daWriteContext in fDeferredActions then
WriteContext;
If daWriteTag in fDeferredActions then
WriteTag;
Result := fDestination.Write(Buffer,Count);
Inc(fDataBytes,Int64(Result));
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataWriter.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Result := fDestination.Seek(Offset,Origin);
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataWriter.SetContext(Context: TTBDContextID);
begin
Include(fDeferredActions,daWriteContext);
fCurrentContext := Context;
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataWriter.SetTag(Tag: TTBDTag): TStream;
begin
If Tag <> TBD_TAG_CONTEXT then
begin
Include(fDeferredActions,daWriteTag);
fCurrentTag := Tag;
Result := Self;
end
else raise ETBDInvalidValue.CreateFmt('TTaggedBinaryDataWriter.SetTag: Invalid tag (0x%.2x).',[Tag]);
end;
{===============================================================================
--------------------------------------------------------------------------------
TTaggedBinaryDataReader
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TTaggedBinaryDataReader - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TTaggedBinaryDataReader - protected methods
-------------------------------------------------------------------------------}
Function TTaggedBinaryDataReader.GetUtilization: Double;
begin
If (fMetaBytes + fDataBytes) <> 0 then
Result := fDataBytes / (fDataBytes + fMetaBytes)
else
Result := 0.0;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataReader.Initialize(Source: TStream);
var
Signature: UInt32;
begin
If Assigned(Source) then
fSource := Source
else
raise ETBDInvalidValue.Create('TTaggedBinaryDataReader.Initialize: Source stream not assigned.');
{
Check if the source can contain a valid TBD stream, and if so whether it
starts with a proper signature.
}
fMetaBytes := 0; // must be here since it is used
If (fSource.Size - fSource.Position) >= 6 {4B signature, 2B closing sequence} then
begin
Signature := Stream_GetUInt32(fSource);
fEndOfDataReached := Signature <> TBD_SIGNATURE;
If fEndOfDataReached then
fSource.Seek(-SizeOf(UInt32),soCurrent)
else
Inc(fMetaBytes,SizeOf(UInt32));
end
else fEndOfDataReached := True;
fInitialContext := True;
// init other fields
fCurrentContext := 0;
fCurrentTag := 0;
fDataBytes := 0;
fContextChangeEvent := nil;
fContextChangeCallback := nil;
fTagChangeEvent := nil;
fTagChangeCallback := nil;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataReader.Finalize;
begin
fContextChangeEvent := nil;
fContextChangeCallback := nil;
fTagChangeEvent := nil;
fTagChangeCallback := nil;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataReader.DoContextChange;
begin
If Assigned(fContextChangeEvent) then
fContextChangeEvent(Self)
else If Assigned(fContextChangeCallback) then
fContextChangeCallback(Self);
fInitialContext := False;
end;
//------------------------------------------------------------------------------
procedure TTaggedBinaryDataReader.DoTagChange;
begin
If Assigned(fTagChangeEvent) then
fTagChangeEvent(Self)
else If Assigned(fTagChangeCallback) then
fTagChangeCallback(Self);
end;
{-------------------------------------------------------------------------------
TTaggedBinaryDataReader - public methods
-------------------------------------------------------------------------------}
constructor TTaggedBinaryDataReader.Create(Source: TStream);
begin
inherited Create;
Initialize(Source);
end;
//------------------------------------------------------------------------------
destructor TTaggedBinaryDataReader.Destroy;
begin
Finalize;
inherited;
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataReader.Read(var Buffer; Count: LongInt): LongInt;
begin
Result := fSource.Read(Buffer,Count);
Inc(fDataBytes,Int64(Result));
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataReader.Write(const Buffer; Count: LongInt): LongInt;
begin
{$IFDEF FPC}Result := {$ENDIF}ConsumeArgs([@Buffer,Count]);
raise ETBDWriteError.Create('TTaggedBinaryDataReader.Write: Writing not allowed.');
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataReader.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Result := fSource.Seek(Offset,Origin);
end;
//------------------------------------------------------------------------------
Function TTaggedBinaryDataReader.GetTag: Boolean;
begin
Result := False;
If not fEndOfDataReached then
begin
If (fSource.Size - fSource.Position) >= SizeOf(TTBDTag) then
begin
// tag can fit in the rest of the stream after current position
fCurrentTag := Stream_GetUInt8(fSource);
Inc(fMetaBytes,SizeOf(TTBDTag));
If fCurrentTag = TBD_TAG_CONTEXT then
begin
If (fSource.Size - fSource.Position) >= SizeOf(TTBDContextFlags) then
begin
Inc(fMetaBytes,SizeOf(TTBDContextFlags));
If (Stream_GetUInt8(fSource) and TBD_CTXFLAGS_FLAG_CLOSE) = 0 then
If (fSource.Size - fSource.Position) >= SizeOf(TTBDContextID) then
begin
fCurrentContext := Stream_GetUInt16(fSource);
Inc(fMetaBytes,SizeOf(TTBDContextID));
DoContextChange;
{
Recursively call GetTag again to read next thing after
the context change (whatever it will be).
Note the brackets must be there for FPC - otherwise
GetTag is parsed as a result of this function (Boolean),
not as a call to it.
}
Result := GetTag();
Exit;
end;
end;
fEndOfDataReached := True;
end
else
begin
// non-context tag read, signal context change if at the start of stream
If fInitialContext then
DoContextChange;
DoTagChange;
Result := True;
end;
end
// tag cannot fit
else fEndOfDataReached := True;
end;
end;
end.