Add unsigned versions of ntohl and htonl

All systems i know of define ntohl and htonl to use unsigned 32bit
types. To keep backward compatibility for now I've added overloaded
versions so the impact on existing code is a small as possible. But I've
marked the signed versions as deprecated.

The code will now also use SwapEndian which is available as optimized
versions on some platforms.

git-svn-id: trunk@22994 -
This commit is contained in:
masta 2012-11-16 12:37:07 +00:00
parent 02bccda89e
commit e370303fda
2 changed files with 27 additions and 15 deletions

View File

@ -235,29 +235,39 @@ end;
type thostaddr= packed array[1..4] of byte;
function htonl( host : longint):longint; inline;
function htonl( host : longint):longint; inline;overload;deprecated;
begin
{$ifdef FPC_BIG_ENDIAN}
htonl:=host;
{$else}
htonl:=THostAddr(host)[4];
htonl:=htonl or longint( (THostAddr(host)[3]) shl 8);
htonl:=htonl or longint( (THostAddr(host)[2]) shl 16);
htonl:=htonl or longint( (THostAddr(host)[1]) shl 24);
htonl:=SwapEndian(host);
{$endif}
end;
Function NToHl (Net : Longint) : Longint; inline;
function htonl( host : cardinal):cardinal; inline;overload;
begin
{$ifdef FPC_BIG_ENDIAN}
htonl:=host;
{$else}
htonl:=SwapEndian(host);
{$endif}
end;
Function NToHl (Net : longint) : longint; inline;overload;deprecated;
begin
{$ifdef FPC_BIG_ENDIAN}
ntohl:=net;
{$else}
ntohl:=THostAddr(Net)[4];
ntohl:=ntohl or longint( (THostAddr(Net)[3]) shl 8);
ntohl:=ntohl or longint( (THostAddr(Net)[2]) shl 16);
ntohl:=ntohl or longint( (THostAddr(Net)[1]) shl 24);
ntohl:=SwapEndian(net);
{$endif}
end;
Function NToHl (Net : cardinal) : cardinal; inline;overload;
begin
{$ifdef FPC_BIG_ENDIAN}
ntohl:=net;
{$else}
ntohl:=SwapEndian(net);
{$endif}
end;
@ -267,7 +277,7 @@ begin
{$ifdef FPC_BIG_ENDIAN}
htons:=host;
{$else}
htons:=swap(host);
htons:=SwapEndian(host);
{$endif}
end;
@ -277,7 +287,7 @@ begin
{$ifdef FPC_BIG_ENDIAN}
ntohs:=net;
{$else}
ntohs:=swap(net);
ntohs:=SwapEndian(net);
{$endif}
end;

View File

@ -205,8 +205,10 @@ Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):
Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);deprecated;
{ Utility routines}
function htonl( host : longint):longint; inline;
Function NToHl( Net : Longint) : Longint; inline;
function htonl( host : longint):longint; inline; overload;deprecated;
Function NToHl( Net : longint):longint; inline; overload;deprecated;
function htonl( host : cardinal):cardinal; inline; overload;
Function NToHl( Net : cardinal):cardinal; inline; overload;
function htons( host : word):word; inline;
Function NToHs( Net : word):word; inline;