Skip to content

Commit 76fa43f

Browse files
committed
use FPC LEtoN/BEtoN intrinsics for wire-to-native reads
Replace the hand-rolled {$IFDEF CRYPTOLIB_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 CRYPTOLIB_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 056e5da commit 76fa43f

1 file changed

Lines changed: 42 additions & 18 deletions

File tree

CryptoLib/src/Misc/ClpBinaryPrimitives.pas

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

162162
class function TBinaryPrimitives.LeToNativeUInt16(AValue: UInt16): UInt16;
163163
begin
164-
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
165-
Result := AValue;
164+
{$IFDEF FPC}
165+
Result := LEtoN(AValue);
166166
{$ELSE}
167+
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
168+
Result := AValue;
169+
{$ELSE}
167170
Result := TBitOperations.ReverseBytesUInt16(AValue);
168-
{$ENDIF}
171+
{$ENDIF CRYPTOLIB_LITTLE_ENDIAN}
172+
{$ENDIF FPC}
169173
end;
170174

171175
class function TBinaryPrimitives.LeToNativeUInt32(AValue: UInt32): UInt32;
172176
begin
173-
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
174-
Result := AValue;
177+
{$IFDEF FPC}
178+
Result := LEtoN(AValue);
175179
{$ELSE}
180+
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
181+
Result := AValue;
182+
{$ELSE}
176183
Result := TBitOperations.ReverseBytesUInt32(AValue);
177-
{$ENDIF}
184+
{$ENDIF CRYPTOLIB_LITTLE_ENDIAN}
185+
{$ENDIF FPC}
178186
end;
179187

180188
class function TBinaryPrimitives.LeToNativeUInt64(AValue: UInt64): UInt64;
181189
begin
182-
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
183-
Result := AValue;
190+
{$IFDEF FPC}
191+
Result := LEtoN(AValue);
184192
{$ELSE}
193+
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
194+
Result := AValue;
195+
{$ELSE}
185196
Result := TBitOperations.ReverseBytesUInt64(AValue);
186-
{$ENDIF}
197+
{$ENDIF CRYPTOLIB_LITTLE_ENDIAN}
198+
{$ENDIF FPC}
187199
end;
188200

189201
class function TBinaryPrimitives.BeToNativeUInt16(AValue: UInt16): UInt16;
190202
begin
191-
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
192-
Result := TBitOperations.ReverseBytesUInt16(AValue);
203+
{$IFDEF FPC}
204+
Result := BEtoN(AValue);
193205
{$ELSE}
206+
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
207+
Result := TBitOperations.ReverseBytesUInt16(AValue);
208+
{$ELSE}
194209
Result := AValue;
195-
{$ENDIF}
210+
{$ENDIF CRYPTOLIB_LITTLE_ENDIAN}
211+
{$ENDIF FPC}
196212
end;
197213

198214
class function TBinaryPrimitives.BeToNativeUInt32(AValue: UInt32): UInt32;
199215
begin
200-
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
201-
Result := TBitOperations.ReverseBytesUInt32(AValue);
216+
{$IFDEF FPC}
217+
Result := BEtoN(AValue);
202218
{$ELSE}
219+
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
220+
Result := TBitOperations.ReverseBytesUInt32(AValue);
221+
{$ELSE}
203222
Result := AValue;
204-
{$ENDIF}
223+
{$ENDIF CRYPTOLIB_LITTLE_ENDIAN}
224+
{$ENDIF FPC}
205225
end;
206226

207227
class function TBinaryPrimitives.BeToNativeUInt64(AValue: UInt64): UInt64;
208228
begin
209-
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
210-
Result := TBitOperations.ReverseBytesUInt64(AValue);
229+
{$IFDEF FPC}
230+
Result := BEtoN(AValue);
211231
{$ELSE}
232+
{$IFDEF CRYPTOLIB_LITTLE_ENDIAN}
233+
Result := TBitOperations.ReverseBytesUInt64(AValue);
234+
{$ELSE}
212235
Result := AValue;
213-
{$ENDIF}
236+
{$ENDIF CRYPTOLIB_LITTLE_ENDIAN}
237+
{$ENDIF FPC}
214238
end;
215239

216240
// ============================================================================

0 commit comments

Comments
 (0)