C++ to Delphi CheckSum code

Thread Solved

Join Date: May 2008
Posts: 8
Reputation: rayman341 is an unknown quantity at this point 
Solved Threads: 0
rayman341 rayman341 is offline Offline
Newbie Poster

C++ to Delphi CheckSum code

 
0
  #1
May 16th, 2008
typedef struct iphdr
{
u_char ip_hl:4, /* header length */
ip_v:4; /* version */
u_char ip_tos; /* type of service */
short ip_len; /* total length */
u_short ip_id; /* identification */
short ip_off; /* fragment offset field */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
u_char ip_ttl; /* time to live */
u_char ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src,ip_dst; /* source and dest address */
} iphdr, *iphdr_ptr;


VOID RecalculateIPChecksum (
iphdr_ptr pIpHeader
)
{
unsigned short word16;
unsigned int sum = 0;
unsigned int i = 0;
PUCHAR buff;

// Initialize checksum to zero
pIpHeader->ip_sum = 0;
buff = (PUCHAR)pIpHeader;

// Calculate IP header checksum
for (i = 0; i < pIpHeader->ip_hl*sizeof(DWORD); i=i+2)
{
word16 = ((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
sum = sum+word16;
}

// keep only the last 16 bits of the 32 bit calculated sum and add the carries
while (sum>>16)
sum = (sum & 0xFFFF)+(sum >> 16);

// Take the one's complement of sum
sum = ~sum;

pIpHeader->ip_sum = htons((unsigned short) sum);
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: C++ to Delphi CheckSum code

 
0
  #2
May 16th, 2008
That C code is a mess. And it is dangerous because it uses bitfields (which C++ does not guarantee to be packed in any specific order -- so results vary by compiler!).

Alas, why don't programmers stp usng stpd shrt nonsns nms.

Anyway:
  1. uses
  2. SysUtils, // for PByteArray
  3. Winsock; // for htons()
  4.  
  5. const
  6. IP_DF = $4000; // don't fragment flag
  7. IP_MF = $2000; // more fragments flag
  8.  
  9. type
  10. in_addr = record ... end; // you must fill this in
  11.  
  12. iphdr_ptr = ^iphdr;
  13. iphdr = packed record
  14. ip_hl__ip_v: byte; // header length, version: four bits each
  15. ip_tos: byte; // type of service
  16. ip_len: shortint; // total length
  17. ip_id: word; // identification
  18. ip_off: shortint; // fragment offset field
  19. ip_ttl: byte; // time to live
  20. ip_p: byte; // protocol
  21. ip_sum: word; // checksum
  22. ip_src: in_addr; // source address
  23. ip_dst: in_addr // destination address
  24. end;
  25.  
  26. procedure RecalculateIPChecksum( pIpHeader: iphdr_ptr );
  27. var
  28. sum: longword;
  29. i: longword;
  30. buff: PByteArray; // see note 1
  31. begin
  32. sum := 0;
  33. i := 0;
  34.  
  35. // Initialize checksum to zero
  36. pIpHeader^.ip_sum := 0;
  37. buff := pIpHeader;
  38.  
  39. // Calculate IP header checksum
  40. while i < (pIpHeader^.ip_hl__ip_v and $F) *sizeof( longword ) do // see note 2
  41. begin
  42. inc( sum, (buff[ i ] shl 8) +buff[ i +1 ] );
  43. inc( i, 2 )
  44. end;
  45.  
  46. // Keep only the last 16 bits of the 32 bit calculated sum and add the carries
  47. while (sum shr 16) <> 0 do
  48. sum := (sum and $FFFF) +(sum shr 16);
  49.  
  50. // Take the one's complement of sum
  51. sum := sum xor $FFFFFFFF;
  52.  
  53. // ...and store it in network order
  54. pIpHeader^.ip_sum := htons( sum )
  55. end;

The above code presumes the following:
  1. typedef unsigned char* PUCHAR;
  2. bitfields are packed in MS VC++ order [1], meaning that ip_hl is in the lower four bits and ip_v is in the upper four bits. If this is not the case you will need to change that and $F to shr 4.

Enjoy!
Last edited by Duoas; May 16th, 2008 at 10:19 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 8
Reputation: rayman341 is an unknown quantity at this point 
Solved Threads: 0
rayman341 rayman341 is offline Offline
Newbie Poster

Re: C++ to Delphi CheckSum code

 
0
  #3
May 16th, 2008
Thank you so much. This gives me a lot of help with other peaces of code.

Ray
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Pascal and Delphi Forum


Views: 2564 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC