git-svn-id: https://192.168.0.254/svn/Componentes.Terceros.RemObjects@68 b6239004-a887-0f4b-9937-50029ccdca16
205 lines
5.4 KiB
ObjectPascal
205 lines
5.4 KiB
ObjectPascal
unit uROEndianness;
|
|
|
|
{----------------------------------------------------------------------------}
|
|
{ RemObjects SDK Library - Core Library }
|
|
{ }
|
|
{ compiler: Delphi 5 and up, Kylix 2 and up }
|
|
{ platform: Win32, Linux }
|
|
{ }
|
|
{ (c)opyright RemObjects Software. all rights reserved. }
|
|
{ }
|
|
{ Using this code requires a valid license of the RemObjects SDK }
|
|
{ which can be obtained at http://www.remobjects.com. }
|
|
{----------------------------------------------------------------------------}
|
|
|
|
{$I RemObjects.inc}
|
|
|
|
interface
|
|
|
|
{$IFDEF DELPHI6}
|
|
type
|
|
UInt64 = Int64;
|
|
{$ENDIF DELPHI6}
|
|
|
|
// Output is always little endian (Intel's)
|
|
function EndianSafeWriteInteger(aIn: integer): integer;
|
|
function EndianSafeWriteInt16(aIn: Word): Word;
|
|
function EndianSafeWriteInt32(aIn: Cardinal): Cardinal;
|
|
function EndianSafeWriteInt64(aIn: UInt64): UInt64;
|
|
function EndianSafeWriteDouble(aIn: double): double;
|
|
function EndianSafeWriteSingle(aIn: single): single;
|
|
function EndianSafeWriteCurrency(aIn: Currency): Currency;
|
|
|
|
// Input is always little endian (Intel's)
|
|
function EndianSafeReadInteger(aIn: integer): integer;
|
|
function EndianSafeReadInt16(aIn: Word): Word;
|
|
function EndianSafeReadInt32(aIn: Cardinal): Cardinal;
|
|
function EndianSafeReadInt64(aIn: UInt64): UInt64;
|
|
function EndianSafeReadDouble(aIn: double): double;
|
|
function EndianSafeReadSingle(aIn: single): single;
|
|
function EndianSafeReadCurrency(aIn: Currency): Currency;
|
|
|
|
function ReverseByteOrder(aData: pointer; aSize: integer): pointer; overload;
|
|
procedure ReverseByteOrder(aDataIn, aDataOut: pointer; aSize: integer); overload;
|
|
|
|
implementation
|
|
|
|
type
|
|
TByteArray = array of byte;
|
|
PByteArray = ^TByteArray;
|
|
|
|
function EndianSafeWriteInteger(aIn: integer): integer;
|
|
{$IFDEF ENDIAN_BIG}
|
|
var
|
|
tmp: pointer;
|
|
{$ENDIF}
|
|
begin
|
|
{$IFDEF ENDIAN_BIG}
|
|
tmp := ReverseByteOrder(@aIn, SizeOf(integer));
|
|
Result := PInteger(tmp)^;
|
|
FreeMem(tmp, SizeOf(integer));
|
|
{$ELSE}
|
|
Result := aIn;
|
|
{$ENDIF}
|
|
end;
|
|
|
|
function EndianSafeWriteInt16(aIn: Word): Word;
|
|
begin
|
|
{$IFDEF ENDIAN_BIG}
|
|
Result := ((aIn shr 8) and $00ff) + ((aIn shl 8) and $ff00);
|
|
{$ELSE}
|
|
Result := aIn;
|
|
{$ENDIF}
|
|
end;
|
|
|
|
function EndianSafeWriteInt32(aIn: Cardinal): Cardinal;
|
|
begin
|
|
{$IFDEF ENDIAN_BIG}
|
|
Result := ((aIn and $00ff) shl 24) + ((aIn and $ff00) shl 8) +
|
|
((aIn and $ff0000) shr 8) + ((aIn shr 24) and $ff);
|
|
{$ELSE}
|
|
Result := aIn;
|
|
{$ENDIF}
|
|
end;
|
|
|
|
function EndianSafeWriteInt64(aIn: UInt64): UInt64;
|
|
{$IFDEF ENDIAN_BIG}
|
|
var
|
|
arrIn: array[0..7] of byte absolute aIn;
|
|
arrRes: array[0..7] of byte absolute Result;
|
|
i: byte;
|
|
{$ENDIF}
|
|
begin
|
|
{$IFDEF ENDIAN_BIG}
|
|
Result := 0;
|
|
for i := 0 to 7 do arrRes[i] := arrIn[7 - i];
|
|
{$ELSE}
|
|
Result := aIn;
|
|
{$ENDIF}
|
|
end;
|
|
|
|
function EndianSafeWriteDouble(aIn: double): double;
|
|
{$IFDEF ENDIAN_BIG}
|
|
var
|
|
arrIn: array[0..7] of byte absolute aIn;
|
|
arrRes: array[0..7] of byte absolute Result;
|
|
i: byte;
|
|
{$ENDIF}
|
|
begin
|
|
{$IFDEF ENDIAN_BIG}
|
|
Result := 0;
|
|
for i := 0 to 7 do arrRes[i] := arrIn[7 - i];
|
|
{$ELSE}
|
|
Result := aIn;
|
|
{$ENDIF}
|
|
end;
|
|
|
|
function EndianSafeWriteSingle(aIn: single): single;
|
|
{$IFDEF ENDIAN_BIG}
|
|
var
|
|
arrIn: array[0..3] of byte absolute aIn;
|
|
arrRes: array[0..3] of byte absolute Result;
|
|
i: byte;
|
|
{$ENDIF}
|
|
begin
|
|
{$IFDEF ENDIAN_BIG}
|
|
Result := 0;
|
|
for i := 0 to 3 do arrRes[i] := arrIn[3 - i];
|
|
{$ELSE}
|
|
Result := aIn;
|
|
{$ENDIF}
|
|
end;
|
|
|
|
function EndianSafeWriteCurrency(aIn: Currency): Currency;
|
|
{$IFDEF ENDIAN_BIG}
|
|
var
|
|
arrIn: array[0..7] of byte absolute aIn;
|
|
arrRes: array[0..7] of byte absolute Result;
|
|
i: byte;
|
|
{$ENDIF}
|
|
begin
|
|
{$IFDEF ENDIAN_BIG}
|
|
Result := 0;
|
|
for i := 0 to 7 do arrRes[i] := arrIn[7 - i];
|
|
{$ELSE}
|
|
Result := aIn;
|
|
{$ENDIF}
|
|
end;
|
|
|
|
function EndianSafeReadInteger(aIn: integer): integer;
|
|
begin
|
|
Result := EndianSafeWriteInteger(aIn);
|
|
end;
|
|
|
|
function EndianSafeReadInt16(aIn: Word): Word;
|
|
begin
|
|
Result := EndianSafeWriteInt16(aIn);
|
|
end;
|
|
|
|
function EndianSafeReadInt32(aIn: Cardinal): Cardinal;
|
|
begin
|
|
Result := EndianSafeWriteInt32(aIn);
|
|
end;
|
|
|
|
function EndianSafeReadInt64(aIn: UInt64): UInt64;
|
|
begin
|
|
Result := EndianSafeWriteInt64(aIn);
|
|
end;
|
|
|
|
function EndianSafeReadDouble(aIn: double): double;
|
|
begin
|
|
Result := EndianSafeWriteDouble(aIn);
|
|
end;
|
|
|
|
function EndianSafeReadSingle(aIn: single): single;
|
|
begin
|
|
Result := EndianSafeWriteSingle(aIn);
|
|
end;
|
|
|
|
function EndianSafeReadCurrency(aIn: Currency): Currency;
|
|
begin
|
|
Result := EndianSafeWriteCurrency(aIn);
|
|
end;
|
|
|
|
function ReverseByteOrder(aData: pointer; aSize: integer): pointer;
|
|
var
|
|
arrIn: TByteArray absolute aData;
|
|
arrOut: PByteArray;
|
|
i: integer;
|
|
begin
|
|
GetMem(Result, aSize);
|
|
arrOut := Result;
|
|
for i := 0 to aSize - 1 do arrOut^[i] := arrIn[aSize - 1 - i];
|
|
end;
|
|
|
|
procedure ReverseByteOrder(aDataIn, aDataOut: pointer; aSize: integer);
|
|
var
|
|
arrIn: TByteArray absolute aDataIn;
|
|
arrOut: TByteArray absolute aDataOut;
|
|
i: integer;
|
|
begin
|
|
for i := 0 to aSize - 1 do arrOut[i] := arrIn[aSize - 1 - i];
|
|
end;
|
|
|
|
end.
|