| | |
Hard C++ to Delphi Conversion
![]() |
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
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
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
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
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
Brazil - Blumenau
I'm not sure about Delphi I, but maybe you can do something like this:
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.
delphi Syntax (Toggle Plain Text)
var PtrSource :^Byte; begin PtrSource := pTcpHdr; Inc(PtrSource, sizeof (VPNSEC_HEADER)); Move(pTcpHdr^, PtrSource^, PacketBuffer.m_Length - (sizeof(ether_header) + sizeof(DWORD)*pIpHeader^.ip_hl)) end;
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
Brazil - Blumenau
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
//
// 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;
SequenceNumber
Word;
AcknowledgementNumber
Word;
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?
// 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;
SequenceNumber
Word;AcknowledgementNumber
Word;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?
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.
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:
If, however, the offset calculation is in bytes you must first cast it to a byte pointer:
Hope this helps.
Delphi Syntax (Toggle Plain Text)
type pPoint = ^tPoint; tPoint: record x, y: integer end; function get_point( points: pPoint; index: integer ): tPoint; begin inc( points, index ); // same as in C (points += index) result := points^ end; function get_point( points: pPoint; index: integer ): tPoint; var p: BytePtr; begin p := pointer( points ); inc( p, index *sizeof( tPoint ) ); points := pointer( p ); result := points^ 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:
C++ Syntax (Toggle Plain Text)
int *a; a[ 5 ] == (a +5)
If, however, the offset calculation is in bytes you must first cast it to a byte pointer:
C++ Syntax (Toggle Plain Text)
int *a; a[ 5 ] == ((unsigned char*)a +(5 *sizeof( a[ 0 ] )))
Hope this helps.
Messy.
Micheus already gave you the basics of this above.
Delphi Syntax (Toggle Plain Text)
var bp: PByte; begin bp := pVpnHdr; inc( bp, sizeof( pVpnHdr ) +pVpnHdr^.m_Length ); fillchar( bp^, padding, 0 ) end;
Micheus already gave you the basics of this above.
Last edited by Duoas; May 16th, 2008 at 10:30 am.
•
•
•
•
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.
both is the same thing - pointing to same memory address.
•
•
•
•
if I understand this correctly pTcpHdr is still used since bp only help change the contants of pTcpHdr.
If you change pTcpHdr value, you are losting the start of memory allocated to it and lost its reference for future access.
Bye
"It always has, at least, two ways to make one same thing. Exactly that they are certain and wrong"(Micheus)
Brazil - Blumenau
Brazil - Blumenau
![]() |
Other Threads in the Pascal and Delphi Forum
- Previous Thread: more C++ to Delphi
- Next Thread: System.DateTime to TDateTime
Views: 2925 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for Pascal and Delphi
animation api app array button compile console data database dbisam delete delphi delphihelpimageforloop documentcomplete2 edit environment error errors events file form function gdi gis lasrautoinc media navigatecomplete2 network object open opengl pascal passing path player procedure search set sql table twebbrowser username variable win7 windows






