Skip to content

Commit e05f8c7

Browse files
committed
use FPC LEtoN/BEtoN intrinsics for wire-to-native reads
Replace the hand-rolled {$IFDEF HASHLIB_LITTLE_ENDIAN} branches in the six private read-side helpers with FPC's dedicated, self-endian-aware RTL intrinsics: LeToNativeUInt16/32/64 -> LEtoN BeToNativeUInt16/32/64 -> BEtoN The intrinsic is used on FPC; Delphi keeps the existing fallback verbatim via {$ELSE}. Generated little-endian code is unchanged (LEtoN/BEtoN are the identity on LE hosts, a bswap on BE) -- this is an idiomatic/clarity change that drops these functions' reliance on the HASHLIB_LITTLE_ENDIAN define and defers the endianness decision to the RTL. All Read* overloads route through these helpers, so the whole read path is covered transitively.
1 parent ba4d0a9 commit e05f8c7

1 file changed

Lines changed: 42 additions & 18 deletions

File tree

HashLib/src/Utils/HlpBinaryPrimitives.pas

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,56 +145,80 @@ class procedure TBinaryPrimitives.CheckBounds(const AData: THashLibByteArray; AO
145145

146146
class function TBinaryPrimitives.LeToNativeUInt16(AValue: UInt16): UInt16;
147147
begin
148-
{$IFDEF HASHLIB_LITTLE_ENDIAN}
149-
Result := AValue;
148+
{$IFDEF FPC}
149+
Result := LEtoN(AValue);
150150
{$ELSE}
151+
{$IFDEF HASHLIB_LITTLE_ENDIAN}
152+
Result := AValue;
153+
{$ELSE}
151154
Result := TBitOperations.ReverseBytesUInt16(AValue);
152-
{$ENDIF}
155+
{$ENDIF HASHLIB_LITTLE_ENDIAN}
156+
{$ENDIF FPC}
153157
end;
154158

155159
class function TBinaryPrimitives.LeToNativeUInt32(AValue: UInt32): UInt32;
156160
begin
157-
{$IFDEF HASHLIB_LITTLE_ENDIAN}
158-
Result := AValue;
161+
{$IFDEF FPC}
162+
Result := LEtoN(AValue);
159163
{$ELSE}
164+
{$IFDEF HASHLIB_LITTLE_ENDIAN}
165+
Result := AValue;
166+
{$ELSE}
160167
Result := TBitOperations.ReverseBytesUInt32(AValue);
161-
{$ENDIF}
168+
{$ENDIF HASHLIB_LITTLE_ENDIAN}
169+
{$ENDIF FPC}
162170
end;
163171

164172
class function TBinaryPrimitives.LeToNativeUInt64(AValue: UInt64): UInt64;
165173
begin
166-
{$IFDEF HASHLIB_LITTLE_ENDIAN}
167-
Result := AValue;
174+
{$IFDEF FPC}
175+
Result := LEtoN(AValue);
168176
{$ELSE}
177+
{$IFDEF HASHLIB_LITTLE_ENDIAN}
178+
Result := AValue;
179+
{$ELSE}
169180
Result := TBitOperations.ReverseBytesUInt64(AValue);
170-
{$ENDIF}
181+
{$ENDIF HASHLIB_LITTLE_ENDIAN}
182+
{$ENDIF FPC}
171183
end;
172184

173185
class function TBinaryPrimitives.BeToNativeUInt16(AValue: UInt16): UInt16;
174186
begin
175-
{$IFDEF HASHLIB_LITTLE_ENDIAN}
176-
Result := TBitOperations.ReverseBytesUInt16(AValue);
187+
{$IFDEF FPC}
188+
Result := BEtoN(AValue);
177189
{$ELSE}
190+
{$IFDEF HASHLIB_LITTLE_ENDIAN}
191+
Result := TBitOperations.ReverseBytesUInt16(AValue);
192+
{$ELSE}
178193
Result := AValue;
179-
{$ENDIF}
194+
{$ENDIF HASHLIB_LITTLE_ENDIAN}
195+
{$ENDIF FPC}
180196
end;
181197

182198
class function TBinaryPrimitives.BeToNativeUInt32(AValue: UInt32): UInt32;
183199
begin
184-
{$IFDEF HASHLIB_LITTLE_ENDIAN}
185-
Result := TBitOperations.ReverseBytesUInt32(AValue);
200+
{$IFDEF FPC}
201+
Result := BEtoN(AValue);
186202
{$ELSE}
203+
{$IFDEF HASHLIB_LITTLE_ENDIAN}
204+
Result := TBitOperations.ReverseBytesUInt32(AValue);
205+
{$ELSE}
187206
Result := AValue;
188-
{$ENDIF}
207+
{$ENDIF HASHLIB_LITTLE_ENDIAN}
208+
{$ENDIF FPC}
189209
end;
190210

191211
class function TBinaryPrimitives.BeToNativeUInt64(AValue: UInt64): UInt64;
192212
begin
193-
{$IFDEF HASHLIB_LITTLE_ENDIAN}
194-
Result := TBitOperations.ReverseBytesUInt64(AValue);
213+
{$IFDEF FPC}
214+
Result := BEtoN(AValue);
195215
{$ELSE}
216+
{$IFDEF HASHLIB_LITTLE_ENDIAN}
217+
Result := TBitOperations.ReverseBytesUInt64(AValue);
218+
{$ELSE}
196219
Result := AValue;
197-
{$ENDIF}
220+
{$ENDIF HASHLIB_LITTLE_ENDIAN}
221+
{$ENDIF FPC}
198222
end;
199223

200224
// ============================================================================

0 commit comments

Comments
 (0)