1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Packet Pool Management (Packet)                                     */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_packet.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_packet_data_retrieve                            PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function copies data from a NetX packet (or packet chain) into */
45 /*    the supplied user buffer.                                           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    packet_ptr                            Pointer to the source packet  */
50 /*    buffer_start                          Pointer to destination area   */
51 /*    bytes_copied                          Number of bytes copied        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
70 /*  09-30-2020     Yuxin Zhou               Modified comment(s), and      */
71 /*                                            verified memcpy use cases,  */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_packet_data_retrieve(NX_PACKET * packet_ptr,VOID * buffer_start,ULONG * bytes_copied)75 UINT  _nx_packet_data_retrieve(NX_PACKET *packet_ptr, VOID *buffer_start, ULONG *bytes_copied)
76 {
77 
78 ULONG  remaining_bytes;
79 UCHAR *destination_ptr;
80 ULONG  bytes_to_copy;
81 
82     /* If trace is enabled, insert this event into the trace buffer.  */
83     NX_TRACE_IN_LINE_INSERT(NX_TRACE_PACKET_DATA_RETRIEVE, packet_ptr, buffer_start, bytes_copied, 0, NX_TRACE_PACKET_EVENTS, 0, 0);
84 
85     /* Setup the destination pointer.  */
86     destination_ptr =  buffer_start;
87 
88     /* Pickup the amount of bytes to copy.  */
89     *bytes_copied =  packet_ptr -> nx_packet_length;
90 
91     /* Setup the remaining bytes.  */
92     remaining_bytes =  packet_ptr -> nx_packet_length;
93 
94 #ifndef NX_DISABLE_PACKET_CHAIN
95     /* Loop to copy bytes from packet(s).  */
96     while (packet_ptr)
97     {
98 #endif /* NX_DISABLE_PACKET_CHAIN */
99 
100         /* Calculate the bytes to copy in this packet. */
101         /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
102         bytes_to_copy = (ULONG)(packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_prepend_ptr);
103 
104         /* Copy data to destination. */
105         /* Note: The buffer size must be not less than packet_ptr -> nx_packet_length.  */
106         memcpy(destination_ptr, packet_ptr -> nx_packet_prepend_ptr, bytes_to_copy); /* Use case of memcpy is verified. The buffer is provided by user.  lgtm[cpp/banned-api-usage-required-any] */
107 
108         remaining_bytes -= bytes_to_copy;
109         destination_ptr += bytes_to_copy;
110 
111 #ifndef NX_DISABLE_PACKET_CHAIN
112         /* Move to next packet.  */
113         packet_ptr =  packet_ptr -> nx_packet_next;
114     }
115 #endif /* NX_DISABLE_PACKET_CHAIN */
116 
117     /* Determine if the packet chain was valid.  */
118     if (remaining_bytes)
119     {
120 
121         /* Invalid packet chain.  Calculate the actual number of bytes
122            copied.  */
123         *bytes_copied =  *bytes_copied - remaining_bytes;
124 
125         /* Return an error.  */
126         return(NX_INVALID_PACKET);
127     }
128 
129     /* Return successful completion.  */
130     return(NX_SUCCESS);
131 }
132 
133