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

Dani AI

Generated

A few focused notes and a compact Delphi example to fill the gap between the C code and the replies from , and @Micheus.

Key points first: keep the original pTcpHdr pointer unchanged (use a separate byte pointer for the destination), compute the IP-header length from the IPv4 Ver/IHL byte (low 4 bits), and compute padding so it yields 0..7 (avoid writing eight bytes when already aligned). Delphi’s Move is the natural memmove replacement and the FillChar family can zero the padding, but watch buffer bounds and endianness for any header fields you write.

Example (adapt to your actual type/constant names):

const
  ETH_HDR_LEN = 14;  // typical Ethernet II header size

var
  ihlWords, ipBytes, payloadLen, pad: Integer;
  dest: PByte;
begin
  ihlWords := pIPHeader^.VerLen and $0F;         // IHL = low 4 bits (32-bit words)
  ipBytes := ihlWords * 4;
  payloadLen := PacketBuffer.m_Length - (ETH_HDR_LEN + ipBytes);

  dest := PByte(PAnsiChar(pTcpHdr) + SizeOf(TVPNSecHeader));
  Move(pTcpHdr^, dest^, payloadLen);             // memmove semantics

  pad := (8 - (payloadLen mod 8)) mod 8;          // yields 0..7
  if pad <> 0 then
    FillChar(dest[payloadLen], pad, 0);

  pVpnHdr := TVPNSecHeaderPtr(pTcpHdr);          // header lives at start of packet
  pVpnHdr^.h_Length := Word(payloadLen);         // convert to network byte order if needed
end;

Troubleshooting tips: verify payloadLen is non-negative and within the buffer; replace ETH_HDR_LEN if your capture uses VLANs or different link-layer framing; if the header length field is transmitted on the wire, perform host-to-network byte order conversion before sending. This covers the missing pieces that caused confusion earlier and ties together the byte-pointer advice from and the Move/FillChar idea from .

Recommended Answers

All 10 Replies

Member Avatar for Member #82008

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

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

Member Avatar for Member #82008

I'm not sure about Delphi I, but maybe you can do something like this:

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;

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.

//
// 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:DWord;
AcknowledgementNumber:DWord;
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.

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:

int *a;
a[ 5 ] == (a +5)

If, however, the offset calculation is in bytes you must first cast it to a byte pointer:

int *a;
a[ 5 ] == ((unsigned char*)a +(5 *sizeof( a[ 0 ] )))

Hope this helps.

So what is:
memset (((unsigned char*)(pVpnHdr + 1)) + pVpnHdr->m_Length, 0, padding);

converted to Delphi?

Messy.

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.

So after the fillchar() do I ref pTcpHdr or bp.

if I understand this correctly pTcpHdr is still used since bp only help change the contants of pTcpHdr.

Ray

Member Avatar for Member #82008

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.

Thank you by this support Duoas. I forgot to explain this.

So after the fillchar() do I ref pTcpHdr or bp.

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.

You must use bp to move across memory allocated to pTcpHdr.
If you change pTcpHdr value, you are losting the start of memory allocated to it and lost its reference for future access.

Bye

I think he wants to know how to access the struct/record elements.

As Micheus explained, both reference the same memory. The only difference is how they treat that memory.

bp thinks the memory is just a list of bytes.

pTcpHdr thinks the memory is a record containing information about a Tcp Packet (and it would be right). I was careful in the example not to change the pTcpHdr variable, so you can still access the packet header by dereferencing it: pTcpHdr^.Options := 42; Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.