1 | unit BinarySerializer;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, DateUtils, SpecializedList, SpecializedStream;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TEndianness = (enBig, enLittle);
|
---|
10 |
|
---|
11 | { TBinarySerializer }
|
---|
12 |
|
---|
13 | TBinarySerializer = class
|
---|
14 | private
|
---|
15 | FGrow: Boolean;
|
---|
16 | FList: TListByte;
|
---|
17 | FEndianness: TEndianness;
|
---|
18 | SwapData: Boolean;
|
---|
19 | procedure SetList(const AValue: TListByte);
|
---|
20 | procedure SetEndianness(const AValue: TEndianness);
|
---|
21 | procedure ReverseByteOrder(var Buffer; Count: Integer);
|
---|
22 | public
|
---|
23 | Position: Integer;
|
---|
24 | OwnsList: Boolean;
|
---|
25 | procedure Write(var Buffer; Count: Integer); //inline;
|
---|
26 | procedure Read(var Buffer; Count: Integer); //inline;
|
---|
27 |
|
---|
28 | procedure Assign(Source: TBinarySerializer);
|
---|
29 | procedure WriteByte(Data: Byte);
|
---|
30 | procedure WriteWord(Data: Word);
|
---|
31 | procedure WriteCardinal(Data: Cardinal);
|
---|
32 | procedure WriteInt64(Data: Int64);
|
---|
33 | procedure WriteString(Data: string);
|
---|
34 | procedure WriteShortString(Data: ShortString);
|
---|
35 | procedure WriteAnsiString(Data: string);
|
---|
36 | procedure WriteUnixTime(Data: TDateTime);
|
---|
37 | procedure WriteDouble(Value: Double);
|
---|
38 | procedure WriteSingle(Value: Single);
|
---|
39 | procedure WriteStream(AStream: TStreamByte; Count: Integer);
|
---|
40 | procedure WriteStreamPart(AStream: TStreamByte; Count: Integer);
|
---|
41 | procedure WriteList(List: TListByte; StartIndex, Count: Integer);
|
---|
42 | function ReadByte: Byte;
|
---|
43 | function ReadWord: Word;
|
---|
44 | function ReadCardinal: Cardinal;
|
---|
45 | function ReadInt64: Int64;
|
---|
46 | function ReadString(Length: Integer): string;
|
---|
47 | function ReadShortString: string;
|
---|
48 | function ReadAnsiString: string;
|
---|
49 | function ReadStringTerminated(Terminator: string = #0): string;
|
---|
50 | function ReadUnixTime: TDateTime;
|
---|
51 | function ReadDouble: Double;
|
---|
52 | function ReadSingle: Single;
|
---|
53 | procedure ReadStream(AStream: TStream; Count: Integer);
|
---|
54 | procedure ReadStreamPart(AStream: TStream; Count: Integer);
|
---|
55 | procedure ReadList(List: TListByte; StartIndex, Count: Integer);
|
---|
56 | constructor Create; overload;
|
---|
57 | procedure Clear;
|
---|
58 | destructor Destroy; override;
|
---|
59 | property Endianness: TEndianness read FEndianness write SetEndianness;
|
---|
60 | property List: TListByte read FList write SetList;
|
---|
61 | property Grow: Boolean read FGrow write FGrow;
|
---|
62 | end;
|
---|
63 |
|
---|
64 |
|
---|
65 | implementation
|
---|
66 |
|
---|
67 | { TBinarySerializer }
|
---|
68 |
|
---|
69 | function TBinarySerializer.ReadAnsiString: string;
|
---|
70 | var
|
---|
71 | StringLength: Longint;
|
---|
72 | begin
|
---|
73 | StringLength := 0;
|
---|
74 | Read(StringLength, SizeOf(StringLength));
|
---|
75 | Result := ReadString(StringLength);
|
---|
76 | end;
|
---|
77 |
|
---|
78 | function TBinarySerializer.ReadStringTerminated(Terminator: string = #0): string;
|
---|
79 | var
|
---|
80 | Data: Char;
|
---|
81 | I: Integer;
|
---|
82 | OldPosition: Integer;
|
---|
83 | begin
|
---|
84 | OldPosition := Position;
|
---|
85 | Result := '';
|
---|
86 | I := 1;
|
---|
87 | repeat
|
---|
88 | if Position >= FList.Count then Break;
|
---|
89 | Data := Chr(ReadByte);
|
---|
90 | if Data <> Terminator[I] then begin
|
---|
91 | Result := Result + Data;
|
---|
92 | I := 1;
|
---|
93 | end else Inc(I);
|
---|
94 | until I > Length(Terminator);
|
---|
95 | if not (I > Length(Terminator)) then begin
|
---|
96 | Result := '';
|
---|
97 | Position := OldPosition;
|
---|
98 | end;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | function TBinarySerializer.ReadByte: Byte;
|
---|
102 | begin
|
---|
103 | Result := 0;
|
---|
104 | Read(Result, SizeOf(Byte));
|
---|
105 | end;
|
---|
106 |
|
---|
107 | function TBinarySerializer.ReadCardinal: Cardinal;
|
---|
108 | begin
|
---|
109 | Result := 0;
|
---|
110 | Read(Result, SizeOf(Cardinal));
|
---|
111 | if SwapData then Result := SwapEndian(Result);
|
---|
112 | end;
|
---|
113 |
|
---|
114 | function TBinarySerializer.ReadInt64: Int64;
|
---|
115 | begin
|
---|
116 | Result := 0;
|
---|
117 | Read(Result, SizeOf(Int64));
|
---|
118 | if SwapData then Result := SwapEndian(Result);
|
---|
119 | end;
|
---|
120 |
|
---|
121 | function TBinarySerializer.ReadString(Length: Integer): string;
|
---|
122 | begin
|
---|
123 | if Length > 0 then begin
|
---|
124 | SetLength(Result, Length);
|
---|
125 | Read(Result[1], Length);
|
---|
126 | end else Result := '';
|
---|
127 | end;
|
---|
128 |
|
---|
129 | function TBinarySerializer.ReadShortString: string;
|
---|
130 | var
|
---|
131 | Count: Byte;
|
---|
132 | begin
|
---|
133 | Count := 0;
|
---|
134 | Read(Count, 1);
|
---|
135 | Result := ReadString(Count);
|
---|
136 | end;
|
---|
137 |
|
---|
138 | procedure TBinarySerializer.ReadStream(AStream: TStream; Count: Integer);
|
---|
139 | var
|
---|
140 | Buffer: array of Byte;
|
---|
141 | begin
|
---|
142 | if Count > 0 then begin
|
---|
143 | SetLength(Buffer, Count);
|
---|
144 | Read(Buffer[0], Count);
|
---|
145 | AStream.Size := Count;
|
---|
146 | AStream.Position := 0;
|
---|
147 | AStream.Write(Buffer[0], Count);
|
---|
148 | end;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | procedure TBinarySerializer.ReadStreamPart(AStream: TStream; Count: Integer);
|
---|
152 | var
|
---|
153 | Buffer: array of Byte;
|
---|
154 | begin
|
---|
155 | if Count > 0 then begin
|
---|
156 | SetLength(Buffer, Count);
|
---|
157 | Read(Buffer[0], Count);
|
---|
158 | if AStream.Size < (AStream.Position + Count) then
|
---|
159 | AStream.Size := AStream.Position + Count;
|
---|
160 | Write(Buffer[0], Count);
|
---|
161 | end;
|
---|
162 | end;
|
---|
163 |
|
---|
164 | procedure TBinarySerializer.ReadList(List: TListByte; StartIndex, Count: Integer
|
---|
165 | );
|
---|
166 | var
|
---|
167 | Buffer: array of Byte;
|
---|
168 | begin
|
---|
169 | if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size
|
---|
170 | if Count > 0 then begin
|
---|
171 | SetLength(Buffer, Count);
|
---|
172 | Read(Pointer(Buffer)^, Count);
|
---|
173 | List.ReplaceBuffer(StartIndex, Pointer(Buffer)^, Count);
|
---|
174 | end;
|
---|
175 | end;
|
---|
176 |
|
---|
177 | procedure TBinarySerializer.WriteStreamPart(AStream: TStreamByte; Count: Integer);
|
---|
178 | var
|
---|
179 | Buffer: array of Byte;
|
---|
180 | begin
|
---|
181 | if Count > AStream.Size then Count := AStream.Size; // Limit max. stream size
|
---|
182 | if Count > 0 then begin
|
---|
183 | SetLength(Buffer, Count);
|
---|
184 | AStream.ReadBuffer(Pointer(Buffer)^, Count);
|
---|
185 | Write(Pointer(Buffer)^, Count);
|
---|
186 | end;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | procedure TBinarySerializer.WriteList(List: TListByte; StartIndex, Count: Integer);
|
---|
190 | var
|
---|
191 | Buffer: array of Byte;
|
---|
192 | begin
|
---|
193 | // if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size
|
---|
194 | if Count > 0 then begin
|
---|
195 | SetLength(Buffer, Count);
|
---|
196 | List.GetBuffer(StartIndex, PByte(Buffer)^, Count);
|
---|
197 | Write(Pointer(Buffer)^, Count);
|
---|
198 | end;
|
---|
199 | end;
|
---|
200 |
|
---|
201 | constructor TBinarySerializer.Create;
|
---|
202 | begin
|
---|
203 | inherited;
|
---|
204 | Endianness := enLittle;
|
---|
205 | FList := nil;
|
---|
206 | FGrow := True;
|
---|
207 | end;
|
---|
208 |
|
---|
209 | procedure TBinarySerializer.Clear;
|
---|
210 | begin
|
---|
211 | FList.Count := 0;
|
---|
212 | Position := 0;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | destructor TBinarySerializer.Destroy;
|
---|
216 | begin
|
---|
217 | if OwnsList then FreeAndNil(FList);
|
---|
218 | inherited;
|
---|
219 | end;
|
---|
220 |
|
---|
221 | function TBinarySerializer.ReadUnixTime: TDateTime;
|
---|
222 | begin
|
---|
223 | Result := UnixToDateTime(ReadCardinal);
|
---|
224 | end;
|
---|
225 |
|
---|
226 | function TBinarySerializer.ReadDouble: Double;
|
---|
227 | begin
|
---|
228 | Result := 0;
|
---|
229 | Read(Result, SizeOf(Double));
|
---|
230 | end;
|
---|
231 |
|
---|
232 | function TBinarySerializer.ReadSingle: Single;
|
---|
233 | begin
|
---|
234 | Result := 0;
|
---|
235 | Read(Result, SizeOf(Single));
|
---|
236 | end;
|
---|
237 |
|
---|
238 | function TBinarySerializer.ReadWord: Word;
|
---|
239 | begin
|
---|
240 | Result := 0;
|
---|
241 | Read(Result, SizeOf(Word));
|
---|
242 | if SwapData then Result := SwapEndian(Result);
|
---|
243 | end;
|
---|
244 |
|
---|
245 | procedure TBinarySerializer.SetList(const AValue: TListByte);
|
---|
246 | begin
|
---|
247 | if OwnsList then FList.Free;
|
---|
248 | FList := AValue;
|
---|
249 | end;
|
---|
250 |
|
---|
251 | procedure TBinarySerializer.SetEndianness(const AValue: TEndianness);
|
---|
252 | begin
|
---|
253 | FEndianness := AValue;
|
---|
254 | {$if defined(FPC_LITTLE_ENDIAN)}
|
---|
255 | SwapData := FEndianness = enBig;
|
---|
256 | {$elseif defined(FPC_BIG_ENDIAN)}
|
---|
257 | SwapData := FEndianness = enLittle;
|
---|
258 | {$endif}
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TBinarySerializer.ReverseByteOrder(var Buffer; Count: Integer);
|
---|
262 | var
|
---|
263 | I: Integer;
|
---|
264 | Temp: Byte;
|
---|
265 | type
|
---|
266 | TBytes = array of Byte;
|
---|
267 | begin
|
---|
268 | I := 0;
|
---|
269 | while I < (Count div 2) do begin
|
---|
270 | Temp := TBytes(Buffer)[Count - 1 - I];
|
---|
271 | TBytes(Buffer)[Count - 1 - I] := TBytes(Buffer)[I];
|
---|
272 | TBytes(Buffer)[I] := Temp;
|
---|
273 | I := I + 1;
|
---|
274 | end;
|
---|
275 | end;
|
---|
276 |
|
---|
277 | procedure TBinarySerializer.Write(var Buffer; Count: Integer);
|
---|
278 | var
|
---|
279 | NewCount: Integer;
|
---|
280 | begin
|
---|
281 | if FGrow then begin
|
---|
282 | NewCount := Position + Count;
|
---|
283 | if FList.Count < NewCount then
|
---|
284 | FList.Count := NewCount;
|
---|
285 | end;
|
---|
286 | FList.ReplaceBuffer(Position, Buffer, Count);
|
---|
287 | Inc(Position, Count);
|
---|
288 | end;
|
---|
289 |
|
---|
290 | procedure TBinarySerializer.Read(var Buffer; Count: Integer);
|
---|
291 | begin
|
---|
292 | FList.GetBuffer(Position, Buffer, Count);
|
---|
293 | Inc(Position, Count);
|
---|
294 | end;
|
---|
295 |
|
---|
296 | procedure TBinarySerializer.Assign(Source: TBinarySerializer);
|
---|
297 | begin
|
---|
298 | FList := Source.FList;
|
---|
299 | end;
|
---|
300 |
|
---|
301 | procedure TBinarySerializer.WriteAnsiString(Data: string);
|
---|
302 | var
|
---|
303 | StringLength: Longint;
|
---|
304 | begin
|
---|
305 | StringLength := Length(Data);
|
---|
306 | Write(StringLength, SizeOf(StringLength));
|
---|
307 | Write(Data[1], StringLength);
|
---|
308 | end;
|
---|
309 |
|
---|
310 | procedure TBinarySerializer.WriteByte(Data: Byte);
|
---|
311 | begin
|
---|
312 | Write(Data, SizeOf(Byte));
|
---|
313 | end;
|
---|
314 |
|
---|
315 | procedure TBinarySerializer.WriteCardinal(Data: Cardinal);
|
---|
316 | begin
|
---|
317 | if SwapData then Data := SwapEndian(Data);
|
---|
318 | Write(Data, SizeOf(Cardinal));
|
---|
319 | end;
|
---|
320 |
|
---|
321 | procedure TBinarySerializer.WriteInt64(Data: Int64);
|
---|
322 | begin
|
---|
323 | if SwapData then Data := SwapEndian(Data);
|
---|
324 | Write(Data, SizeOf(Int64));
|
---|
325 | end;
|
---|
326 |
|
---|
327 | procedure TBinarySerializer.WriteString(Data:string);
|
---|
328 | begin
|
---|
329 | if Length(Data) > 0 then
|
---|
330 | Write(Data[1], Length(Data));
|
---|
331 | end;
|
---|
332 |
|
---|
333 | procedure TBinarySerializer.WriteShortString(Data: ShortString);
|
---|
334 | begin
|
---|
335 | WriteByte(Length(Data));
|
---|
336 | Write(Data[1], Length(Data));
|
---|
337 | end;
|
---|
338 |
|
---|
339 | procedure TBinarySerializer.WriteStream(AStream: TStreamByte; Count: Integer);
|
---|
340 | var
|
---|
341 | Buffer: array of Byte;
|
---|
342 | begin
|
---|
343 | if Count > AStream.Size then Count := AStream.Size; // Limit max. stream size
|
---|
344 | AStream.Position := 0;
|
---|
345 | if Count > 0 then begin
|
---|
346 | SetLength(Buffer, Count);
|
---|
347 | AStream.ReadBuffer(Pointer(Buffer)^, Count);
|
---|
348 | Write(Pointer(Buffer)^, Count);
|
---|
349 | end;
|
---|
350 | end;
|
---|
351 |
|
---|
352 | procedure TBinarySerializer.WriteDouble(Value: Double);
|
---|
353 | begin
|
---|
354 | Write(Value, SizeOf(Double));
|
---|
355 | end;
|
---|
356 |
|
---|
357 | procedure TBinarySerializer.WriteSingle(Value: Single);
|
---|
358 | begin
|
---|
359 | Write(Value, SizeOf(Single));
|
---|
360 | end;
|
---|
361 |
|
---|
362 | procedure TBinarySerializer.WriteUnixTime(Data: TDateTime);
|
---|
363 | var
|
---|
364 | DataUnix: Int64;
|
---|
365 | begin
|
---|
366 | DataUnix := DateTimeToUnix(Data);
|
---|
367 | WriteCardinal(DataUnix);
|
---|
368 | end;
|
---|
369 |
|
---|
370 | procedure TBinarySerializer.WriteWord(Data: Word);
|
---|
371 | begin
|
---|
372 | if SwapData then Data := SwapEndian(Data);
|
---|
373 | Write(Data, SizeOf(Word));
|
---|
374 | end;
|
---|
375 |
|
---|
376 | end.
|
---|
377 |
|
---|