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