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