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