Browse Source

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 -
masta 12 years ago
parent
commit
e370303fda
2 changed files with 26 additions and 14 deletions
  1. 22 12
      rtl/inc/sockets.inc
  2. 4 2
      rtl/inc/socketsh.inc

+ 22 - 12
rtl/inc/sockets.inc

@@ -235,29 +235,39 @@ end;
 
 
 type thostaddr= packed array[1..4] of byte;
 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:=SwapEndian(host);
+{$endif}
+end;
 
 
+function htonl( host : cardinal):cardinal; inline;overload;
 begin
 begin
 {$ifdef FPC_BIG_ENDIAN}
 {$ifdef FPC_BIG_ENDIAN}
   htonl:=host;
   htonl:=host;
 {$else}
 {$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}
 {$endif}
 end;
 end;
 
 
-Function NToHl (Net : Longint) : Longint; inline;
+Function NToHl (Net : longint) : longint; inline;overload;deprecated;
+begin
+{$ifdef FPC_BIG_ENDIAN}
+  ntohl:=net;
+{$else}
+  ntohl:=SwapEndian(net);
+{$endif}
+end;
 
 
+Function NToHl (Net : cardinal) : cardinal; inline;overload;
 begin
 begin
 {$ifdef FPC_BIG_ENDIAN}
 {$ifdef FPC_BIG_ENDIAN}
   ntohl:=net;
   ntohl:=net;
 {$else}
 {$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}
 {$endif}
 end;
 end;
 
 
@@ -267,7 +277,7 @@ begin
 {$ifdef FPC_BIG_ENDIAN}
 {$ifdef FPC_BIG_ENDIAN}
   htons:=host;
   htons:=host;
 {$else}
 {$else}
-  htons:=swap(host);
+  htons:=SwapEndian(host);
 {$endif}
 {$endif}
 end;
 end;
 
 
@@ -277,7 +287,7 @@ begin
 {$ifdef FPC_BIG_ENDIAN}
 {$ifdef FPC_BIG_ENDIAN}
   ntohs:=net;
   ntohs:=net;
 {$else}
 {$else}
-  ntohs:=swap(net);
+  ntohs:=SwapEndian(net);
 {$endif}
 {$endif}
 end;
 end;
 
 

+ 4 - 2
rtl/inc/socketsh.inc

@@ -205,8 +205,10 @@ Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):
 Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);deprecated;
 Procedure Sock2File(Sock:Longint;Var SockIn,SockOut:File);deprecated;
 
 
 { Utility routines}
 { 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 htons( host : word):word; inline;
 Function NToHs( Net : word):word; inline;
 Function NToHs( Net : word):word; inline;