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 buffer->length += numbytes;
59 }
60
lbuf_pop(struct lbufhead * buffer,size_t numbytes,uint32_t * ntraversed)61 size_t lbuf_pop(struct lbufhead* buffer, size_t numbytes, uint32_t* ntraversed) {
62 otLinkedBuffer* curr = buffer->head;
63 size_t bytesleft = numbytes;
64 size_t curroffset = buffer->offset;
65 if (curr == NULL) {
66 return 0;
67 }
68 while (bytesleft >= curr->mLength - curroffset) {
69 ++*ntraversed;
70 bytesleft -= (curr->mLength - curroffset);
71 buffer->length -= (curr->mLength - curroffset);
72 if (buffer->tail == curr) {
73 buffer->head = NULL;
74 buffer->tail = NULL;
75 buffer->offset = 0;
76 return numbytes - bytesleft;
77 }
78 curr = curr->mNext;
79 curroffset = 0;
80 }
81 /* Handle the last entry. */
82 buffer->head = curr;
83 buffer->offset = curroffset + bytesleft;
84 buffer->length -= bytesleft;
85 return numbytes;
86 }
87
lbuf_getrange(struct lbufhead * buffer,size_t offset,size_t numbytes,otLinkedBuffer ** first,size_t * firstoffset,otLinkedBuffer ** last,size_t * lastextra)88 int lbuf_getrange(struct lbufhead* buffer, size_t offset, size_t numbytes,
89 otLinkedBuffer** first, size_t* firstoffset,
90 otLinkedBuffer** last, size_t* lastextra) {
91 otLinkedBuffer* curr = buffer->head;
92 size_t offsetleft = offset + buffer->offset;
93 size_t bytesleft = numbytes;
94 if (buffer->length < offset + numbytes) {
95 return 1; // out of range
96 }
97 while (offsetleft > 0 && offsetleft >= curr->mLength) {
98 offsetleft -= curr->mLength;
99 curr = curr->mNext;
100 }
101 *first = curr;
102 *firstoffset = offsetleft;
103 bytesleft += offsetleft;
104 while (bytesleft > 0 && bytesleft > curr->mLength) {
105 bytesleft -= curr->mLength;
106 curr = curr->mNext;
107 }
108 *last = curr;
109 *lastextra = curr->mLength - bytesleft;
110 return 0;
111 }
112
lbuf_used_space(const struct lbufhead * buffer)113 size_t lbuf_used_space(const struct lbufhead* buffer) {
114 return buffer->length;
115 }
116