xref: /aosp_15_r20/external/openthread/third_party/tcplp/lib/lbuf.c (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2018, Sam Kumar <[email protected]>
3*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2018, University of California, Berkeley
4*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
5*cfb92d14SAndroid Build Coastguard Worker  *
6*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
7*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
8*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must retain the above copyright
9*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
10*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
11*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
12*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
13*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
14*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
15*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
16*cfb92d14SAndroid Build Coastguard Worker  *
17*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
28*cfb92d14SAndroid Build Coastguard Worker  */
29*cfb92d14SAndroid Build Coastguard Worker 
30*cfb92d14SAndroid Build Coastguard Worker /* LINKED BUFFER */
31*cfb92d14SAndroid Build Coastguard Worker 
32*cfb92d14SAndroid Build Coastguard Worker #include "lbuf.h"
33*cfb92d14SAndroid Build Coastguard Worker #include <string.h>
34*cfb92d14SAndroid Build Coastguard Worker #include <openthread/tcp.h>
35*cfb92d14SAndroid Build Coastguard Worker 
lbuf_init(struct lbufhead * buffer)36*cfb92d14SAndroid Build Coastguard Worker void lbuf_init(struct lbufhead* buffer) {
37*cfb92d14SAndroid Build Coastguard Worker     memset(buffer, 0x00, sizeof(struct lbufhead));
38*cfb92d14SAndroid Build Coastguard Worker }
39*cfb92d14SAndroid Build Coastguard Worker 
lbuf_append(struct lbufhead * buffer,otLinkedBuffer * newentry)40*cfb92d14SAndroid Build Coastguard Worker void lbuf_append(struct lbufhead* buffer, otLinkedBuffer* newentry) {
41*cfb92d14SAndroid Build Coastguard Worker     otLinkedBuffer* tail = buffer->tail;
42*cfb92d14SAndroid Build Coastguard Worker     if (tail == NULL) {
43*cfb92d14SAndroid Build Coastguard Worker         buffer->head = newentry;
44*cfb92d14SAndroid Build Coastguard Worker         buffer->tail = newentry;
45*cfb92d14SAndroid Build Coastguard Worker         buffer->offset = 0;
46*cfb92d14SAndroid Build Coastguard Worker         buffer->length = newentry->mLength;
47*cfb92d14SAndroid Build Coastguard Worker         newentry->mNext = NULL;
48*cfb92d14SAndroid Build Coastguard Worker     } else {
49*cfb92d14SAndroid Build Coastguard Worker         tail->mNext = newentry;
50*cfb92d14SAndroid Build Coastguard Worker         buffer->tail = newentry;
51*cfb92d14SAndroid Build Coastguard Worker         buffer->length += newentry->mLength;
52*cfb92d14SAndroid Build Coastguard Worker         newentry->mNext = NULL;
53*cfb92d14SAndroid Build Coastguard Worker     }
54*cfb92d14SAndroid Build Coastguard Worker }
55*cfb92d14SAndroid Build Coastguard Worker 
lbuf_extend(struct lbufhead * buffer,size_t numbytes)56*cfb92d14SAndroid Build Coastguard Worker void lbuf_extend(struct lbufhead* buffer, size_t numbytes) {
57*cfb92d14SAndroid Build Coastguard Worker     buffer->tail->mLength += numbytes;
58*cfb92d14SAndroid Build Coastguard Worker     buffer->length += numbytes;
59*cfb92d14SAndroid Build Coastguard Worker }
60*cfb92d14SAndroid Build Coastguard Worker 
lbuf_pop(struct lbufhead * buffer,size_t numbytes,uint32_t * ntraversed)61*cfb92d14SAndroid Build Coastguard Worker size_t lbuf_pop(struct lbufhead* buffer, size_t numbytes, uint32_t* ntraversed) {
62*cfb92d14SAndroid Build Coastguard Worker     otLinkedBuffer* curr = buffer->head;
63*cfb92d14SAndroid Build Coastguard Worker     size_t bytesleft = numbytes;
64*cfb92d14SAndroid Build Coastguard Worker     size_t curroffset = buffer->offset;
65*cfb92d14SAndroid Build Coastguard Worker     if (curr == NULL) {
66*cfb92d14SAndroid Build Coastguard Worker         return 0;
67*cfb92d14SAndroid Build Coastguard Worker     }
68*cfb92d14SAndroid Build Coastguard Worker     while (bytesleft >= curr->mLength - curroffset) {
69*cfb92d14SAndroid Build Coastguard Worker         ++*ntraversed;
70*cfb92d14SAndroid Build Coastguard Worker         bytesleft -= (curr->mLength - curroffset);
71*cfb92d14SAndroid Build Coastguard Worker         buffer->length -= (curr->mLength - curroffset);
72*cfb92d14SAndroid Build Coastguard Worker         if (buffer->tail == curr) {
73*cfb92d14SAndroid Build Coastguard Worker             buffer->head = NULL;
74*cfb92d14SAndroid Build Coastguard Worker             buffer->tail = NULL;
75*cfb92d14SAndroid Build Coastguard Worker             buffer->offset = 0;
76*cfb92d14SAndroid Build Coastguard Worker             return numbytes - bytesleft;
77*cfb92d14SAndroid Build Coastguard Worker         }
78*cfb92d14SAndroid Build Coastguard Worker         curr = curr->mNext;
79*cfb92d14SAndroid Build Coastguard Worker         curroffset = 0;
80*cfb92d14SAndroid Build Coastguard Worker     }
81*cfb92d14SAndroid Build Coastguard Worker     /* Handle the last entry. */
82*cfb92d14SAndroid Build Coastguard Worker     buffer->head = curr;
83*cfb92d14SAndroid Build Coastguard Worker     buffer->offset = curroffset + bytesleft;
84*cfb92d14SAndroid Build Coastguard Worker     buffer->length -= bytesleft;
85*cfb92d14SAndroid Build Coastguard Worker     return numbytes;
86*cfb92d14SAndroid Build Coastguard Worker }
87*cfb92d14SAndroid Build Coastguard Worker 
lbuf_getrange(struct lbufhead * buffer,size_t offset,size_t numbytes,otLinkedBuffer ** first,size_t * firstoffset,otLinkedBuffer ** last,size_t * lastextra)88*cfb92d14SAndroid Build Coastguard Worker int lbuf_getrange(struct lbufhead* buffer, size_t offset, size_t numbytes,
89*cfb92d14SAndroid Build Coastguard Worker                   otLinkedBuffer** first, size_t* firstoffset,
90*cfb92d14SAndroid Build Coastguard Worker                   otLinkedBuffer** last, size_t* lastextra) {
91*cfb92d14SAndroid Build Coastguard Worker     otLinkedBuffer* curr = buffer->head;
92*cfb92d14SAndroid Build Coastguard Worker     size_t offsetleft = offset + buffer->offset;
93*cfb92d14SAndroid Build Coastguard Worker     size_t bytesleft = numbytes;
94*cfb92d14SAndroid Build Coastguard Worker     if (buffer->length < offset + numbytes) {
95*cfb92d14SAndroid Build Coastguard Worker         return 1; // out of range
96*cfb92d14SAndroid Build Coastguard Worker     }
97*cfb92d14SAndroid Build Coastguard Worker     while (offsetleft > 0 && offsetleft >= curr->mLength) {
98*cfb92d14SAndroid Build Coastguard Worker         offsetleft -= curr->mLength;
99*cfb92d14SAndroid Build Coastguard Worker         curr = curr->mNext;
100*cfb92d14SAndroid Build Coastguard Worker     }
101*cfb92d14SAndroid Build Coastguard Worker     *first = curr;
102*cfb92d14SAndroid Build Coastguard Worker     *firstoffset = offsetleft;
103*cfb92d14SAndroid Build Coastguard Worker     bytesleft += offsetleft;
104*cfb92d14SAndroid Build Coastguard Worker     while (bytesleft > 0 && bytesleft > curr->mLength) {
105*cfb92d14SAndroid Build Coastguard Worker         bytesleft -= curr->mLength;
106*cfb92d14SAndroid Build Coastguard Worker         curr = curr->mNext;
107*cfb92d14SAndroid Build Coastguard Worker     }
108*cfb92d14SAndroid Build Coastguard Worker     *last = curr;
109*cfb92d14SAndroid Build Coastguard Worker     *lastextra = curr->mLength - bytesleft;
110*cfb92d14SAndroid Build Coastguard Worker     return 0;
111*cfb92d14SAndroid Build Coastguard Worker }
112*cfb92d14SAndroid Build Coastguard Worker 
lbuf_used_space(const struct lbufhead * buffer)113*cfb92d14SAndroid Build Coastguard Worker size_t lbuf_used_space(const struct lbufhead* buffer) {
114*cfb92d14SAndroid Build Coastguard Worker     return buffer->length;
115*cfb92d14SAndroid Build Coastguard Worker }
116