-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCR.Exif.StreamHelper.pas
375 lines (323 loc) · 12 KB
/
CCR.Exif.StreamHelper.pas
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
{**************************************************************************************}
{ }
{ CCR Exif - Delphi class library for reading and writing image metadata }
{ Version 1.5.3 }
{ }
{ The contents of this file are subject to the Mozilla Public License Version 1.1 }
{ (the "License"); you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at http://www.mozilla.org/MPL/ }
{ }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT }
{ WARRANTY OF ANY KIND, either express or implied. See the License for the specific }
{ language governing rights and limitations under the License. }
{ }
{ The Original Code is CCR.Exif.StreamHelper.pas. }
{ }
{ The Initial Developer of the Original Code is Chris Rolliston. Portions created by }
{ Chris Rolliston are Copyright (C) 2009-2014 Chris Rolliston. All Rights Reserved. }
{ }
{**************************************************************************************}
{$I CCR.Exif.inc}
unit CCR.Exif.StreamHelper;
interface
uses
SysUtils, Classes;
type
{$IF not Declared(TBytes)}
TBytes = array of Byte; //added in D2007
{$IFEND}
{$IF not Declared(UnicodeString)}
UnicodeString = WideString; //added in D2009
{$IFEND}
PDoubleArray = ^TDoubleArray;
TDoubleArray = array[0..High(TByteArray) div 8] of Double;
PShortIntArray = ^TShortIntArray;
TShortIntArray = array[0..High(TByteArray)] of ShortInt;
PSmallIntArray = ^TSmallIntArray;
TSmallIntArray = array[0..High(TWordArray)] of SmallInt;
PLongWordArray = ^TLongWordArray;
TLongWordArray = array[0..High(TWordArray) div 2] of LongWord;
PLongIntArray = ^TLongIntArray;
TLongIntArray = array[0..High(TWordArray) div 2] of LongInt;
TEndianness = (SmallEndian, BigEndian);
TStreamHelper = class helper for TStream
function ReadByte(var Value: Byte): Boolean; overload; inline;
function ReadByte: Byte; overload; inline;
function ReadSmallInt(Endianness: TEndianness; var Value: SmallInt): Boolean; overload;
function ReadSmallInt(Endianness: TEndianness): SmallInt; overload; inline;
function ReadWord(Endianness: TEndianness; var Value: Word): Boolean; overload;
function ReadWord(Endianness: TEndianness): Word; overload; inline;
function ReadLongInt(Endianness: TEndianness; var Value: LongInt): Boolean; overload;
function ReadLongInt(Endianness: TEndianness): LongInt; overload; inline;
function ReadLongWord(Endianness: TEndianness; var Value: LongWord): Boolean; overload;
function ReadLongWord(Endianness: TEndianness): LongWord; overload; inline;
function ReadDouble(Endianness: TEndianness): Double;
{$IF Declared(ShortString)}
function ReadShortString(var S: ShortString): Boolean; overload;
function ReadShortString: ShortString; overload; inline;
{$IFEND}
function TryReadBuffer(var Buffer; Count: Integer): Boolean;
function TryReadHeader(const Header; HeaderSize: Byte;
AlwaysResetPosition: Boolean = False): Boolean;
procedure WriteByte(Value: Byte); overload; inline;
{$IF Declared(AnsiChar)}
procedure WriteByte(Value: AnsiChar); overload; inline;
{$ELSE}
procedure WriteByte(Value: Char); overload;
{$IFEND}
procedure WriteWord(Value: Word; Endianness: TEndianness);
procedure WriteSmallInt(Value: SmallInt; Endianness: TEndianness);
procedure WriteLongInt(Value: LongInt; Endianness: TEndianness);
procedure WriteLongWord(Value: LongWord; Endianness: TEndianness);
procedure WriteDouble(Value: Double; Endianness: TEndianness);
{$IF Declared(UTF8String)}
procedure WriteUTF8Chars(const S: UTF8String); overload;
{$IFEND}
procedure WriteUTF8Chars(const S: TBytes); overload;
procedure WriteUTF8Chars(const S: UnicodeString); overload;
procedure WriteUTF8Chars(const S: UnicodeString; const Args: array of const); overload;
procedure WriteWideChars(const S: UnicodeString; Endianness: TEndianness);
end;
function SwapLongInt(const Value: LongInt): LongInt;
function SwapLongWord(const Value: LongWord): LongWord;
function SwapSingle(const Value: Single): Single;
function SwapDouble(const Value: Double): Double;
function UnicodeFormat(const S: UnicodeString; const Args: array of const): UnicodeString;
function UnicodeSameText(const S1, S2: UnicodeString): Boolean; inline;
implementation
uses RTLConsts, SysConst;
{$IFDEF WIN32}
function SwapLongInt(const Value: LongInt): LongInt;
asm
BSWAP EAX
end;
function SwapLongWord(const Value: LongWord): LongWord;
asm
BSWAP EAX
end;
{$ELSE}
function SwapLongInt(const Value: LongInt): LongInt;
begin
LongRec(Result).Bytes[0] := LongRec(Value).Bytes[3];
LongRec(Result).Bytes[1] := LongRec(Value).Bytes[2];
LongRec(Result).Bytes[2] := LongRec(Value).Bytes[1];
LongRec(Result).Bytes[3] := LongRec(Value).Bytes[0];
end;
function SwapLongWord(const Value: LongWord): LongWord;
begin
LongRec(Result).Bytes[0] := LongRec(Value).Bytes[3];
LongRec(Result).Bytes[1] := LongRec(Value).Bytes[2];
LongRec(Result).Bytes[2] := LongRec(Value).Bytes[1];
LongRec(Result).Bytes[3] := LongRec(Value).Bytes[0];
end;
{$ENDIF}
function SwapSingle(const Value: Single): Single;
begin
LongRec(Result).Bytes[0] := LongRec(Value).Bytes[3];
LongRec(Result).Bytes[1] := LongRec(Value).Bytes[2];
LongRec(Result).Bytes[2] := LongRec(Value).Bytes[1];
LongRec(Result).Bytes[3] := LongRec(Value).Bytes[0];
end;
function SwapDouble(const Value: Double): Double;
var
I: Integer;
begin
for I := SizeOf(Double) - 1 downto 0 do
PByteArray(@Result)[I] := PByteArray(@Value)[SizeOf(Double) - 1 - I];
end;
function UnicodeFormat(const S: UnicodeString; const Args: array of const): UnicodeString;
begin
{$IFDEF UNICODE}
Result := Format(S, Args);
{$ELSE}
Result := WideFormat(S, Args);
{$ENDIF}
end;
function UnicodeSameText(const S1, S2: UnicodeString): Boolean;
begin
{$IFDEF UNICODE}
Result := SameText(S1, S2);
{$ELSE}
Result := WideSameText(S1, S2);
{$ENDIF}
end;
{ TStreamHelper }
function TStreamHelper.ReadByte(var Value: Byte): Boolean;
begin
Result := (Read(Value, 1) = 1);
end;
function TStreamHelper.ReadByte: Byte;
begin
ReadBuffer(Result, 1);
end;
function TStreamHelper.ReadWord(Endianness: TEndianness; var Value: Word): Boolean;
begin
Result := TryReadBuffer(Value, 2);
if Result and (Endianness = BigEndian) then
Value := Swap(Value)
end;
function TStreamHelper.ReadWord(Endianness: TEndianness): Word;
begin
if not ReadWord(Endianness, Result) then
raise EReadError.CreateRes(@SReadError);
end;
function TStreamHelper.ReadDouble(Endianness: TEndianness): Double;
begin
ReadBuffer(Result, 8);
if Endianness = BigEndian then
Result := SwapDouble(Result);
end;
function TStreamHelper.ReadLongInt(Endianness: TEndianness; var Value: LongInt): Boolean;
begin
Result := TryReadBuffer(Value, 4);
if Result and (Endianness = BigEndian) then
Value := SwapLongInt(Value);
end;
function TStreamHelper.ReadLongInt(Endianness: TEndianness): LongInt;
begin
if not ReadLongInt(Endianness, Result) then
raise EReadError.CreateRes(@SReadError);
end;
function TStreamHelper.ReadLongWord(Endianness: TEndianness;
var Value: LongWord): Boolean;
begin
Result := TryReadBuffer(Value, 4);
if Result and (Endianness = BigEndian) then
Value := SwapLongWord(Value);
end;
function TStreamHelper.ReadLongWord(Endianness: TEndianness): LongWord;
begin
if not ReadLongWord(Endianness, Result) then
raise EReadError.CreateRes(@SReadError);
end;
{$IF Declared(ShortString)}
function TStreamHelper.ReadShortString(var S: ShortString): Boolean;
begin
Result := (Read(S[0], 1) = 1) and (Read(S[1], Byte(S[0])) = Byte(S[0]));
end;
function TStreamHelper.ReadShortString: ShortString;
begin
if not ReadShortString(Result) then
raise EReadError.CreateRes(@SReadError);
end;
{$IFEND}
function TStreamHelper.ReadSmallInt(Endianness: TEndianness;
var Value: SmallInt): Boolean;
begin
Result := TryReadBuffer(Value, 2);
if Result and (Endianness = BigEndian) then
Value := Swap(Value)
end;
function TStreamHelper.ReadSmallInt(Endianness: TEndianness): SmallInt;
begin
if not ReadSmallInt(Endianness, Result) then
raise EReadError.CreateRes(@SReadError);
end;
function TStreamHelper.TryReadBuffer(var Buffer; Count: Integer): Boolean;
var
BytesRead: Integer;
begin
BytesRead := Read(Buffer, Count);
Result := (BytesRead = Count);
if not Result then Seek(-BytesRead, soCurrent);
end;
function TStreamHelper.TryReadHeader(const Header; HeaderSize: Byte;
AlwaysResetPosition: Boolean = False): Boolean;
var
Buffer: array[Byte] of Byte;
BytesRead: Integer;
begin
BytesRead := Read(Buffer, HeaderSize);
Result := (BytesRead = HeaderSize) and CompareMem(@Buffer, @Header, HeaderSize);
if not Result or AlwaysResetPosition then Seek(-BytesRead, soCurrent);
end;
procedure TStreamHelper.WriteByte(Value: Byte);
begin
WriteBuffer(Value, 1);
end;
{$IF Declared(AnsiChar)}
procedure TStreamHelper.WriteByte(Value: AnsiChar);
begin
WriteBuffer(Value, 1);
end;
{$ELSE}
procedure TStreamHelper.WriteByte(Value: Char);
begin
WriteBuffer(Value, 1);
end;
{$IFEND}
procedure TStreamHelper.WriteDouble(Value: Double; Endianness: TEndianness);
begin
if Endianness = BigEndian then
Value := SwapDouble(Value);
WriteBuffer(Value, 8);
end;
procedure TStreamHelper.WriteWideChars(const S: UnicodeString; Endianness: TEndianness);
var
Ch: WideChar;
begin
if Endianness = SmallEndian then
WriteBuffer(Pointer(S)^, Length(S) * 2)
else
for Ch in S do
begin
WriteBuffer(WordRec(Ch).Hi, 1);
WriteBuffer(WordRec(Ch).Lo, 1);
end;
end;
procedure TStreamHelper.WriteWord(Value: Word; Endianness: TEndianness);
begin
if Endianness = SmallEndian then
WriteBuffer(Value, 2)
else
begin
WriteBuffer(WordRec(Value).Hi, 1);
WriteBuffer(WordRec(Value).Lo, 1);
end;
end;
procedure TStreamHelper.WriteLongWord(Value: LongWord; Endianness: TEndianness);
begin
if Endianness = BigEndian then
Value := SwapLongWord(Value);
WriteBuffer(Value, 4);
end;
procedure TStreamHelper.WriteSmallInt(Value: SmallInt; Endianness: TEndianness);
begin
if Endianness = SmallEndian then
WriteBuffer(Value, 2)
else
begin
WriteBuffer(WordRec(Value).Hi, 1);
WriteBuffer(WordRec(Value).Lo, 1);
end;
end;
procedure TStreamHelper.WriteLongInt(Value: LongInt; Endianness: TEndianness);
begin
if Endianness = BigEndian then
Value := SwapLongInt(Value);
WriteBuffer(Value, 4);
end;
{$IF Declared(UTF8String)}
procedure TStreamHelper.WriteUTF8Chars(const S: UTF8String);
begin
WriteBuffer(Pointer(S)^, Length(S));
end;
{$IFEND}
procedure TStreamHelper.WriteUTF8Chars(const S: TBytes);
begin
WriteBuffer(Pointer(S)^, Length(S));
end;
procedure TStreamHelper.WriteUTF8Chars(const S: UnicodeString);
begin
{$IF Declared(UTF8String)}
WriteUTF8Chars(UTF8Encode(S));
{$ELSE}
WriteUTF8Chars(TEncoding.UTF8.GetBytes(S));
{$IFEND}
end;
procedure TStreamHelper.WriteUTF8Chars(const S: UnicodeString;
const Args: array of const);
begin
WriteUTF8Chars(UnicodeFormat(S, Args));
end;
end.