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
Micheus
Junior Poster in Training
72 posts since Jun 2006
Reputation Points: 10
Solved Threads: 4
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.
Micheus
Junior Poster in Training
72 posts since Jun 2006
Reputation Points: 10
Solved Threads: 4
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 inbytes you must first cast it to a byte pointer:
int *a;
a[ 5 ] == ((unsigned char*)a +(5 *sizeof( a[ 0 ] )))
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
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 supportDuoas. 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
Micheus
Junior Poster in Training
72 posts since Jun 2006
Reputation Points: 10
Solved Threads: 4
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.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229