ReactOS 0.4.15-dev-8145-ga541a46
mockbuffer.c File Reference
#include "precomp.h"
Include dependency graph for mockbuffer.c:

Go to the source code of this file.

Functions

VOID XNdisGetFirstBufferFromPacket (PNDIS_PACKET Packet, PNDIS_BUFFER *FirstBuffer, PVOID *FirstBufferVA, PUINT FirstBufferLength, PUINT TotalBufferLength)
 
VOID XNdisQueryBuffer (PNDIS_BUFFER Buffer, PVOID *VirtualAddress, PUINT Length)
 
__inline INT SkipToOffset (PNDIS_BUFFER Buffer, UINT Offset, PCHAR *Data, PUINT Size)
 
UINT CopyBufferToBufferChain (PNDIS_BUFFER DstBuffer, UINT DstOffset, PCHAR SrcData, UINT Length)
 
UINT CopyBufferChainToBuffer (PCHAR DstData, PNDIS_BUFFER SrcBuffer, UINT SrcOffset, UINT Length)
 
UINT CopyPacketToBuffer (PCHAR DstData, PNDIS_PACKET SrcPacket, UINT SrcOffset, UINT Length)
 
UINT CopyPacketToBufferChain (PNDIS_BUFFER DstBuffer, UINT DstOffset, PNDIS_PACKET SrcPacket, UINT SrcOffset, UINT Length)
 
PVOID AdjustPacket (PNDIS_PACKET Packet, UINT Available, UINT Needed)
 
UINT ResizePacket (PNDIS_PACKET Packet, UINT Size)
 
NDIS_STATUS PrependPacket (PNDIS_PACKET Packet, PCHAR Data, UINT Length, BOOLEAN Copy)
 
void GetDataPtr (PNDIS_PACKET Packet, UINT Offset, PCHAR *DataOut, PUINT Size)
 
NDIS_STATUS AllocatePacketWithBufferX (PNDIS_PACKET *NdisPacket, PCHAR Data, UINT Len, PCHAR File, UINT Line)
 
VOID FreeNdisPacketX (PNDIS_PACKET Packet, PCHAR File, UINT Line)
 

Function Documentation

◆ AdjustPacket()

PVOID AdjustPacket ( PNDIS_PACKET  Packet,
UINT  Available,
UINT  Needed 
)

Definition at line 334 of file mockbuffer.c.

347{
348 PNDIS_BUFFER NdisBuffer;
349 INT Adjust;
350
351 TI_DbgPrint(DEBUG_PBUFFER, ("Available = %d, Needed = %d.\n", Available, Needed));
352
353 Adjust = Available - Needed;
354
355 NdisQueryPacket(Packet, NULL, NULL, &NdisBuffer, NULL);
356
357 /* If Adjust is zero there is no need to adjust this packet as
358 there is no additional space at start the of first buffer */
359 if (Adjust != 0) {
360 NdisBuffer->MappedSystemVa = (PVOID) ((ULONG_PTR)(NdisBuffer->MappedSystemVa) + Adjust);
361 NdisBuffer->ByteOffset += Adjust;
362 NdisBuffer->ByteCount -= Adjust;
363 }
364
365 return NdisBuffer->MappedSystemVa;
366}
#define NULL
Definition: types.h:112
#define DEBUG_PBUFFER
Definition: debug.h:20
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1549
static __inline VOID NdisQueryPacket(IN PNDIS_PACKET Packet, OUT PUINT PhysicalBufferCount OPTIONAL, OUT PUINT BufferCount OPTIONAL, OUT PNDIS_BUFFER *FirstBuffer OPTIONAL, OUT PUINT TotalPacketLength OPTIONAL)
Definition: ndis.h:3593
MDL * PNDIS_BUFFER
Definition: ndis.h:343
static LONGLONG Adjust(LONGLONG value, const LONGLONG *pModifier, DWORD dwFlags)
Definition: seeking.c:201
void * PVOID
Definition: typedefs.h:50
int32_t INT
Definition: typedefs.h:58
uint32_t ULONG_PTR
Definition: typedefs.h:65

◆ AllocatePacketWithBufferX()

NDIS_STATUS AllocatePacketWithBufferX ( PNDIS_PACKET NdisPacket,
PCHAR  Data,
UINT  Len,
PCHAR  File,
UINT  Line 
)

Definition at line 425 of file mockbuffer.c.

427 {
431 PCHAR NewData;
432
433 NewData = exAllocatePool( NonPagedPool, Len );
434 if( !NewData ) return NDIS_STATUS_NOT_ACCEPTED; // XXX
435
436 if( Data )
437 RtlCopyMemory(NewData, Data, Len);
438
440 if( Status != NDIS_STATUS_SUCCESS ) {
441 exFreePool( NewData );
442 return Status;
443 }
445
447 if( Status != NDIS_STATUS_SUCCESS ) {
448 exFreePool( NewData );
450 }
452
454 *NdisPacket = Packet;
455
456 return NDIS_STATUS_SUCCESS;
457}
Definition: bufpool.h:45
Definition: File.h:16
#define Len
Definition: deflate.h:82
VOID EXPORT NdisAllocateBuffer(OUT PNDIS_STATUS Status, OUT PNDIS_BUFFER *Buffer, IN NDIS_HANDLE PoolHandle, IN PVOID VirtualAddress, IN UINT Length)
Definition: buffer.c:336
VOID EXPORT NdisAllocatePacket(OUT PNDIS_STATUS Status, OUT PNDIS_PACKET *Packet, IN NDIS_HANDLE PoolHandle)
Definition: buffer.c:394
#define NonPagedPool
Definition: env_spec_w32.h:307
Status
Definition: gdiplustypes.h:25
#define NDIS_BUFFER_TAG
Definition: lan.h:22
#define NDIS_PACKET_TAG
Definition: lan.h:23
NDIS_HANDLE GlobalBufferPool
Definition: routines.c:4
NDIS_HANDLE GlobalPacketPool
Definition: routines.c:3
#define exAllocatePool(x, y)
Definition: memtrack.h:17
#define exFreePool(x)
Definition: memtrack.h:18
#define TrackWithTag(w, x, y, z)
Definition: memtrack.h:19
#define FreeNdisPacket(x)
Definition: memtrack.h:8
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
#define NDIS_STATUS_NOT_ACCEPTED
Definition: ndis.h:350
#define NdisChainBufferAtFront(Packet, Buffer)
Definition: ndis.h:3106
int NDIS_STATUS
Definition: ntddndis.h:475
Definition: ncftp.h:79
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
char * PCHAR
Definition: typedefs.h:51

◆ CopyBufferChainToBuffer()

UINT CopyBufferChainToBuffer ( PCHAR  DstData,
PNDIS_BUFFER  SrcBuffer,
UINT  SrcOffset,
UINT  Length 
)

Definition at line 157 of file mockbuffer.c.

175{
176 UINT BytesCopied, BytesToCopy, SrcSize;
177 PCHAR SrcData;
178
179 TI_DbgPrint(DEBUG_PBUFFER, ("DstData 0x%X SrcBuffer 0x%X SrcOffset 0x%X Length %d\n",DstData,SrcBuffer, SrcOffset, Length));
180
181 /* Skip SrcOffset bytes in the source buffer chain */
182 if (SkipToOffset(SrcBuffer, SrcOffset, &SrcData, &SrcSize) == -1)
183 return 0;
184
185 /* Start copying the data */
186 BytesCopied = 0;
187 for (;;) {
188 BytesToCopy = MIN(SrcSize, Length);
189
190 TI_DbgPrint(DEBUG_PBUFFER, ("Copying (%d) bytes from 0x%X to 0x%X\n", BytesToCopy, SrcData, DstData));
191
192 RtlCopyMemory((PVOID)DstData, (PVOID)SrcData, BytesToCopy);
194 DstData = (PCHAR)((ULONG_PTR)DstData + BytesToCopy);
195
197 if (Length == 0)
198 break;
199
200 SrcSize -= BytesToCopy;
201 if (SrcSize == 0) {
202 /* No more bytes in source buffer. Proceed to
203 the next buffer in the source buffer chain */
204 NdisGetNextBuffer(SrcBuffer, &SrcBuffer);
205 if (!SrcBuffer)
206 break;
207
208 XNdisQueryBuffer(SrcBuffer, (PVOID)&SrcData, &SrcSize);
209 }
210 }
211
212 return BytesCopied;
213}
#define MIN(x, y)
Definition: rdesktop.h:171
#define PCHAR
Definition: match.c:90
VOID XNdisQueryBuffer(PNDIS_BUFFER Buffer, PVOID *VirtualAddress, PUINT Length)
Definition: mockbuffer.c:40
__inline INT SkipToOffset(PNDIS_BUFFER Buffer, UINT Offset, PCHAR *Data, PUINT Size)
Definition: mockbuffer.c:58
#define NdisGetNextBuffer(CurrentBuffer, NextBuffer)
Definition: ndis.h:3386
unsigned int UINT
Definition: ndis.h:50
_In_ UINT _In_ UINT _In_ PNDIS_PACKET _In_ UINT _Out_ PUINT BytesCopied
Definition: ndis.h:3171
_In_ UINT _In_ UINT BytesToCopy
Definition: ndis.h:3168
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102

Referenced by CopyPacketToBuffer().

◆ CopyBufferToBufferChain()

UINT CopyBufferToBufferChain ( PNDIS_BUFFER  DstBuffer,
UINT  DstOffset,
PCHAR  SrcData,
UINT  Length 
)

Definition at line 100 of file mockbuffer.c.

118{
119 UINT BytesCopied, BytesToCopy, DstSize;
120 PCHAR DstData;
121
122 TI_DbgPrint(DEBUG_PBUFFER, ("DstBuffer (0x%X) DstOffset (0x%X) SrcData (0x%X) Length (%d)\n", DstBuffer, DstOffset, SrcData, Length));
123
124 /* Skip DstOffset bytes in the destination buffer chain */
125 if (SkipToOffset(DstBuffer, DstOffset, &DstData, &DstSize) == -1)
126 return 0;
127
128 /* Start copying the data */
129 BytesCopied = 0;
130 for (;;) {
131 BytesToCopy = MIN(DstSize, Length);
132
133 RtlCopyMemory((PVOID)DstData, (PVOID)SrcData, BytesToCopy);
135 SrcData = (PCHAR)((ULONG_PTR)SrcData + BytesToCopy);
136
138 if (Length == 0)
139 break;
140
141 DstSize -= BytesToCopy;
142 if (DstSize == 0) {
143 /* No more bytes in destination buffer. Proceed to
144 the next buffer in the destination buffer chain */
145 NdisGetNextBuffer(DstBuffer, &DstBuffer);
146 if (!DstBuffer)
147 break;
148
149 XNdisQueryBuffer(DstBuffer, (PVOID)&DstData, &DstSize);
150 }
151 }
152
153 return BytesCopied;
154}

◆ CopyPacketToBuffer()

UINT CopyPacketToBuffer ( PCHAR  DstData,
PNDIS_PACKET  SrcPacket,
UINT  SrcOffset,
UINT  Length 
)

Definition at line 216 of file mockbuffer.c.

234{
235 PNDIS_BUFFER FirstBuffer;
237 UINT FirstLength;
239
240 TI_DbgPrint(DEBUG_PBUFFER, ("DstData (0x%X) SrcPacket (0x%X) SrcOffset (0x%X) Length (%d)\n", DstData, SrcPacket, SrcOffset, Length));
241
243 &FirstBuffer,
244 &Address,
245 &FirstLength,
246 &TotalLength);
247
248 return CopyBufferChainToBuffer(DstData, FirstBuffer, SrcOffset, Length);
249}
UINT CopyBufferChainToBuffer(PCHAR DstData, PNDIS_BUFFER SrcBuffer, UINT SrcOffset, UINT Length)
Definition: mockbuffer.c:157
VOID XNdisGetFirstBufferFromPacket(PNDIS_PACKET Packet, PNDIS_BUFFER *FirstBuffer, PVOID *FirstBufferVA, PUINT FirstBufferLength, PUINT TotalBufferLength)
Definition: mockbuffer.c:13
static WCHAR Address[46]
Definition: ping.c:68
_In_ ULONG TotalLength
Definition: usbdlib.h:158

◆ CopyPacketToBufferChain()

UINT CopyPacketToBufferChain ( PNDIS_BUFFER  DstBuffer,
UINT  DstOffset,
PNDIS_PACKET  SrcPacket,
UINT  SrcOffset,
UINT  Length 
)

Definition at line 252 of file mockbuffer.c.

272{
273 PNDIS_BUFFER SrcBuffer;
274 PCHAR DstData, SrcData;
275 UINT DstSize, SrcSize;
276 UINT Count, Total;
277
278 TI_DbgPrint(DEBUG_PBUFFER, ("DstBuffer (0x%X) DstOffset (0x%X) SrcPacket (0x%X) SrcOffset (0x%X) Length (%d)\n", DstBuffer, DstOffset, SrcPacket, SrcOffset, Length));
279
280 /* Skip DstOffset bytes in the destination buffer chain */
281 XNdisQueryBuffer(DstBuffer, (PVOID)&DstData, &DstSize);
282 if (SkipToOffset(DstBuffer, DstOffset, &DstData, &DstSize) == -1)
283 return 0;
284
285 /* Skip SrcOffset bytes in the source packet */
286 XNdisGetFirstBufferFromPacket(SrcPacket, &SrcBuffer, (PVOID *)&SrcData, &SrcSize, &Total);
287 if (SkipToOffset(SrcBuffer, SrcOffset, &SrcData, &SrcSize) == -1)
288 return 0;
289
290 /* Copy the data */
291 for (Total = 0;;) {
292 /* Find out how many bytes we can copy at one time */
293 if (Length < SrcSize)
294 Count = Length;
295 else
296 Count = SrcSize;
297 if (DstSize < Count)
298 Count = DstSize;
299
300 RtlCopyMemory((PVOID)DstData, (PVOID)SrcData, Count);
301
302 Total += Count;
303 Length -= Count;
304 if (Length == 0)
305 break;
306
307 DstSize -= Count;
308 if (DstSize == 0) {
309 /* No more bytes in destination buffer. Proceed to
310 the next buffer in the destination buffer chain */
311 NdisGetNextBuffer(DstBuffer, &DstBuffer);
312 if (!DstBuffer)
313 break;
314
315 XNdisQueryBuffer(DstBuffer, (PVOID)&DstData, &DstSize);
316 }
317
318 SrcSize -= Count;
319 if (SrcSize == 0) {
320 /* No more bytes in source buffer. Proceed to
321 the next buffer in the source buffer chain */
322 NdisGetNextBuffer(SrcBuffer, &SrcBuffer);
323 if (!SrcBuffer)
324 break;
325
326 XNdisQueryBuffer(SrcBuffer, (PVOID)&SrcData, &SrcSize);
327 }
328 }
329
330 return Total;
331}
int Count
Definition: noreturn.cpp:7

◆ FreeNdisPacketX()

VOID FreeNdisPacketX ( PNDIS_PACKET  Packet,
PCHAR  File,
UINT  Line 
)

Definition at line 460 of file mockbuffer.c.

469{
470 PNDIS_BUFFER Buffer, NextBuffer;
471
472 TI_DbgPrint(DEBUG_PBUFFER, ("Packet (0x%X)\n", Packet));
473
474 /* Free all the buffers in the packet first */
476 for (; Buffer != NULL; Buffer = NextBuffer) {
477 PVOID Data;
478 UINT Length;
479
480 NdisGetNextBuffer(Buffer, &NextBuffer);
485 }
486
487 /* Finally free the NDIS packet descriptor */
490}
VOID EXPORT NdisFreePacket(IN PNDIS_PACKET Packet)
Definition: buffer.c:828
#define UntrackFL(x, y, z)
Definition: memtrack.h:20
#define NdisFreeBuffer
Definition: ndis.h:2895

◆ GetDataPtr()

void GetDataPtr ( PNDIS_PACKET  Packet,
UINT  Offset,
PCHAR DataOut,
PUINT  Size 
)

Definition at line 414 of file mockbuffer.c.

417 {
419
421 if( !Buffer ) return;
422 SkipToOffset( Buffer, Offset, DataOut, Size );
423}
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533

Referenced by AddGenericHeaderIPv4(), LANTransmit(), LANTransmitInternal(), LoopTransmit(), PrepareARPPacket(), ProtocolReceive(), SendFragments(), SendPacket(), and TCPSendDataCallback().

◆ PrependPacket()

NDIS_STATUS PrependPacket ( PNDIS_PACKET  Packet,
PCHAR  Data,
UINT  Length,
BOOLEAN  Copy 
)

Definition at line 394 of file mockbuffer.c.

395 {
398 PCHAR NewBuf;
399
400 if( Copy ) {
401 NewBuf = PoolAllocateBuffer( Length );
402 if( !NewBuf ) return STATUS_NO_MEMORY;
403 RtlCopyMemory( NewBuf, Data, Length );
404 } else NewBuf = Data;
405
407 if( Status != NDIS_STATUS_SUCCESS ) return Status;
408
410
411 return STATUS_SUCCESS;
412}
VOID Copy(PVOID Src, PVOID Dst, ULONG NumBytes)
Definition: mmixer.c:126
PVOID PoolAllocateBuffer(ULONG Size)
Definition: mockpool.c:13
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_SUCCESS
Definition: shellext.h:65

◆ ResizePacket()

UINT ResizePacket ( PNDIS_PACKET  Packet,
UINT  Size 
)

Definition at line 369 of file mockbuffer.c.

380{
381 PNDIS_BUFFER NdisBuffer;
382 UINT OldSize;
383
384 NdisQueryPacket(Packet, NULL, NULL, &NdisBuffer, NULL);
385
386 OldSize = NdisBuffer->ByteCount;
387
388 if (Size != OldSize)
389 NdisBuffer->ByteCount = Size;
390
391 return OldSize;
392}

◆ SkipToOffset()

__inline INT SkipToOffset ( PNDIS_BUFFER  Buffer,
UINT  Offset,
PCHAR Data,
PUINT  Size 
)

Definition at line 58 of file mockbuffer.c.

77{
78 for (;;) {
79
80 if (!Buffer)
81 return -1;
82
84
85 if (Offset < *Size) {
86 *Data = (PCHAR)((ULONG_PTR) *Data + Offset);
87 *Size -= Offset;
88 break;
89 }
90
91 Offset -= *Size;
92
94 }
95
96 return Offset;
97}

Referenced by CopyBufferChainToBuffer(), CopyBufferToBufferChain(), CopyPacketToBufferChain(), and GetDataPtr().

◆ XNdisGetFirstBufferFromPacket()

VOID XNdisGetFirstBufferFromPacket ( PNDIS_PACKET  Packet,
PNDIS_BUFFER FirstBuffer,
PVOID FirstBufferVA,
PUINT  FirstBufferLength,
PUINT  TotalBufferLength 
)

Definition at line 13 of file mockbuffer.c.

18{
19 PNDIS_BUFFER _Buffer;
20
21 _Buffer = (Packet)->Private.Head;
22 *(FirstBuffer) = _Buffer;
23 *(FirstBufferVA) = MmGetMdlVirtualAddress(_Buffer);
24 if (_Buffer != NULL) {
25 *(FirstBufferLength) = MmGetMdlByteCount(_Buffer);
26 _Buffer = _Buffer->Next;
27 } else
28 *(FirstBufferLength) = 0;
29 *(TotalBufferLength) = *(FirstBufferLength);
30 while (_Buffer != NULL) {
31 *(TotalBufferLength) += MmGetMdlByteCount(_Buffer);
32 _Buffer = _Buffer->Next;
33 }
34}
#define MmGetMdlByteCount(_Mdl)
#define MmGetMdlVirtualAddress(_Mdl)

Referenced by CopyPacketToBuffer(), and CopyPacketToBufferChain().

◆ XNdisQueryBuffer()

VOID XNdisQueryBuffer ( PNDIS_BUFFER  Buffer,
PVOID VirtualAddress,
PUINT  Length 
)

Definition at line 39 of file mockbuffer.c.

51{
52 if (VirtualAddress != NULL)
53 *(PVOID*)VirtualAddress = Buffer->MappedSystemVa;
54
56}
_Must_inspect_result_ _In_ WDFDMATRANSACTION _In_ PFN_WDF_PROGRAM_DMA _In_ WDF_DMA_DIRECTION _In_ PMDL _In_ PVOID VirtualAddress
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS _In_ LARGE_INTEGER ByteCount
Definition: iotypes.h:1099

Referenced by CopyBufferChainToBuffer(), CopyBufferToBufferChain(), CopyPacketToBufferChain(), FreeNdisPacketX(), and SkipToOffset().