User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 425,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,653 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 1161 | Replies: 10
Reply
Join Date: May 2008
Posts: 8
Reputation: rayman341 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rayman341 rayman341 is offline Offline
Newbie Poster

Hard C++ to Delphi Conversion

  #1  
May 15th, 2008
I am working on porting C++ code to Delphi and I am only having a problem with the correct conversion of memmove() statements in the C++ code.

If you or any one you know could help me to convert this C++ code to Delphi I would be thankful.

// Move TCP packet body by sizeof(VPNSEC_HEADER) bytes
memmove(((unsigned char*)pTcpHdr) + sizeof (VPNSEC_HEADER), pTcpHdr, PacketBuffer.m_Length - (sizeof(ether_header) + sizeof(DWORD)*pIpHeader->ip_hl));

// Initialize the injected header
pVpnHdr = (PVPNSEC_HEADER)pTcpHdr;
pVpnHdr->m_Length = (unsigned short)(PacketBuffer.m_Length - (sizeof(ether_header) + sizeof(DWORD)*pIpHeader->ip_hl));
memcpy (pVpnHdr->m_Key, EncKey, 8);

// Calculate padding
padding = 8 - (pVpnHdr->m_Length%8);

// Zero initialize padding
memset (((unsigned char*)(pVpnHdr + 1)) + pVpnHdr->m_Length, 0, padding);

Ray
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: Blumenau, Brazil
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: Hard C++ to Delphi Conversion

  #2  
May 15th, 2008
I think that you can use this procedures (bellow) from System unit:
memmove or memcpy: procedure Move(const Source; var Dest; Count: Integer);

memset: procedure FillChar(var X; Count: Integer; value: Byte);

Bye
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)

Brazil - Blumenau
Reply With Quote  
Join Date: May 2008
Posts: 8
Reputation: rayman341 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rayman341 rayman341 is offline Offline
Newbie Poster

Re: Hard C++ to Delphi Conversion

  #3  
May 15th, 2008
Thank you, but how do I handle the offset:

memmove(((unsigned char*)pTcpHdr) + sizeof (VPNSEC_HEADER), pTcpHdr, PacketBuffer.m_Length - (sizeof(ether_header) + sizeof(DWORD)*pIpHeader->ip_hl));

Ray
Reply With Quote  
Join Date: Jun 2006
Location: Blumenau, Brazil
Posts: 71
Reputation: Micheus is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 4
Micheus's Avatar
Micheus Micheus is offline Offline
Junior Poster in Training

Re: Hard C++ to Delphi Conversion

  #4  
May 15th, 2008
I'm not sure about Delphi I, but maybe you can do something like this:
  1. var
  2. PtrSource :^Byte;
  3. begin
  4. PtrSource := pTcpHdr;
  5. Inc(PtrSource, sizeof (VPNSEC_HEADER));
  6. Move(pTcpHdr^, PtrSource^, PacketBuffer.m_Length - (sizeof(ether_header) + sizeof(DWORD)*pIpHeader^.ip_hl))
  7. end;
notice that parameters positions change between the two compilers:
void * memmove ( void * destination, const void * source, size_t num );
procedure Move(const Source; var Dest; Count: Integer);


You must try.
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)

Brazil - Blumenau
Reply With Quote  
Join Date: May 2008
Posts: 8
Reputation: rayman341 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rayman341 rayman341 is offline Offline
Newbie Poster

Re: Hard C++ to Delphi Conversion

  #5  
May 15th, 2008
//
// Custom VPN information header
//
type
TVPNSecHeaderPtr = ^TVPNSecHeader;
TVPNSecHeader = packed record
h_KeyId: array [1..8] of Byte;
h_Length: Word;
h_IpProto: Byte;
end;

//
// IP header
//
type
TIPHeaderPtr = ^TIPHeader;
TIPHeader = packed record
VerLen: Byte;
TOS: Byte;
TotalLen: Word;
Identifer: Word;
FragOffsets: Word;
TTL: Byte;
Protocol: Byte;
CheckSum: Word;
SourceIp: DWORD;
DestIp: DWORD;
Options: DWORD;
end;

//
// TCP header
//
TTCPHeaderPtr = ^TTCPHeader;
TTCPHeader = packed record
SourcePort:Word;
DestPort:Word;
SequenceNumberWord;
AcknowledgementNumberWord;
Offset:Byte; //only left 4 bits. Header length in 32-bit segments
Flags:Byte;
Window:Word;
Checksum:Word; //includes speudo header instead of TCP header.
UrgentPointer:Word;
end;

pIPHeader: TIPHeaderPtr;
pTcpHdr: TTCPHeaderPtr;

Are you sure PtrSource should be a Byte?
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Hard C++ to Delphi Conversion

  #6  
May 15th, 2008
Yes, it should be cast to a BytePtr. If you don't then anything you add to it is multiplied by the size of the object's elements.

  1. type
  2. pPoint = ^tPoint;
  3. tPoint: record x, y: integer end;
  4.  
  5. function get_point( points: pPoint; index: integer ): tPoint;
  6. begin
  7. inc( points, index ); // same as in C (points += index)
  8. result := points^
  9. end;
  10.  
  11. function get_point( points: pPoint; index: integer ): tPoint;
  12. var p: BytePtr;
  13. begin
  14. p := pointer( points );
  15. inc( p, index *sizeof( tPoint ) );
  16. points := pointer( p );
  17. result := points^
  18. end;

The first version works the same as in C and C++. If the size of the pointer's target is known, it is automatically multiplied into the index:
  1. int *a;
  2. a[ 5 ] == (a +5)

If, however, the offset calculation is in bytes you must first cast it to a byte pointer:
  1. int *a;
  2. a[ 5 ] == ((unsigned char*)a +(5 *sizeof( a[ 0 ] )))

Hope this helps.
Reply With Quote  
Join Date: May 2008
Posts: 8
Reputation: rayman341 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rayman341 rayman341 is offline Offline
Newbie Poster

Re: Hard C++ to Delphi Conversion

  #7  
May 16th, 2008
So what is:
memset (((unsigned char*)(pVpnHdr + 1)) + pVpnHdr->m_Length, 0, padding);

converted to Delphi?
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,876
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Hard C++ to Delphi Conversion

  #8  
May 16th, 2008
Messy.
  1. var
  2. bp: PByte;
  3. begin
  4. bp := pVpnHdr;
  5. inc( bp, sizeof( pVpnHdr ) +pVpnHdr^.m_Length );
  6. fillchar( bp^, padding, 0 )
  7. end;

Micheus already gave you the basics of this above.
Last edited by Duoas : May 16th, 2008 at 10:30 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 11:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC