@@ -23,16 +23,7 @@ interface
2323 HlpArrayUtils;
2424
2525resourcestring
26- SImpossibleRepresentationInt32 =
27- ' Current Data Structure cannot be Represented as an "Int32" Type.' ;
28- SImpossibleRepresentationUInt8 =
29- ' Current Data Structure cannot be Represented as an "UInt8" Type.' ;
30- SImpossibleRepresentationUInt16 =
31- ' Current Data Structure cannot be Represented as an "UInt16" Type.' ;
32- SImpossibleRepresentationUInt32 =
33- ' Current Data Structure cannot be Represented as an "UInt32" Type.' ;
34- SImpossibleRepresentationUInt64 =
35- ' Current Data Structure cannot be Represented as an "UInt64" Type.' ;
26+ SDifferingSizeOfByteArrayAndIntType = ' The size of the byte array (%0:d) and integer type (%1:d) have to match.' ;
3627
3728type
3829 THashResult = class sealed(TInterfacedObject, IHashResult)
@@ -184,51 +175,56 @@ function THashResult.GetHashCode: {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
184175
185176function THashResult.GetInt32 : Int32;
186177begin
187- if ( System.Length(FHash) <> 4 ) then
178+ if System.Length(FHash) <> sizeof(Int32 ) then
188179 begin
189- raise EInvalidOperationHashLibException.CreateRes
190- (@SImpossibleRepresentationInt32);
180+ raise EInvalidOperationHashLibException.CreateResFmt(
181+ @SDifferingSizeOfByteArrayAndIntType,
182+ [System.Length(FHash), sizeof(Int32)]);
191183 end ;
192184 result := Int32((Int32(FHash[0 ]) shl 24 ) or (Int32(FHash[1 ]) shl 16 ) or
193185 (Int32(FHash[2 ]) shl 8 ) or (Int32(FHash[3 ])));
194186end ;
195187
196188function THashResult.GetUInt8 : UInt8;
197189begin
198- if ( System.Length(FHash) <> 1 ) then
190+ if System.Length(FHash) <> sizeof(UInt8 ) then
199191 begin
200- raise EInvalidOperationHashLibException.CreateRes
201- (@SImpossibleRepresentationUInt8);
192+ raise EInvalidOperationHashLibException.CreateResFmt(
193+ @SDifferingSizeOfByteArrayAndIntType,
194+ [System.Length(FHash), sizeof(UInt8)]);
202195 end ;
203196 result := (UInt8(FHash[0 ]));
204197end ;
205198
206199function THashResult.GetUInt16 : UInt16;
207200begin
208- if ( System.Length(FHash) <> 2 ) then
201+ if System.Length(FHash) <> sizeof(UInt16 ) then
209202 begin
210- raise EInvalidOperationHashLibException.CreateRes
211- (@SImpossibleRepresentationUInt16);
203+ raise EInvalidOperationHashLibException.CreateResFmt(
204+ @SDifferingSizeOfByteArrayAndIntType,
205+ [System.Length(FHash), sizeof(UInt16)]);
212206 end ;
213207 result := (UInt16(FHash[0 ]) shl 8 ) or (UInt16(FHash[1 ]));
214208end ;
215209
216210function THashResult.GetUInt32 : UInt32;
217211begin
218- if ( System.Length(FHash) <> 4 ) then
212+ if System.Length(FHash) <> sizeof(UInt32 ) then
219213 begin
220- raise EInvalidOperationHashLibException.CreateRes
221- (@SImpossibleRepresentationUInt32);
214+ raise EInvalidOperationHashLibException.CreateResFmt(
215+ @SDifferingSizeOfByteArrayAndIntType,
216+ [System.Length(FHash), sizeof(UInt32)]);
222217 end ;
223218 result := TConverters.ReadBytesAsUInt32BE(PByte(FHash), 0 );
224219end ;
225220
226221function THashResult.GetUInt64 : UInt64;
227222begin
228- if ( System.Length(FHash) <> 8 ) then
223+ if System.Length(FHash) <> sizeof(UInt64 ) then
229224 begin
230- raise EInvalidOperationHashLibException.CreateRes
231- (@SImpossibleRepresentationUInt64);
225+ raise EInvalidOperationHashLibException.CreateResFmt(
226+ @SDifferingSizeOfByteArrayAndIntType,
227+ [System.Length(FHash), sizeof(UInt64)]);
232228 end ;
233229 result := TConverters.ReadBytesAsUInt64BE(PByte(FHash), 0 );
234230end ;
0 commit comments