1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "private-lib-core.h"
26 
27 #ifdef LWS_HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30 
31 /* lws_buflist */
32 
33 int
lws_buflist_append_segment(struct lws_buflist ** head,const uint8_t * buf,size_t len)34 lws_buflist_append_segment(struct lws_buflist **head, const uint8_t *buf,
35 			   size_t len)
36 {
37 	struct lws_buflist *nbuf;
38 	int first = !*head;
39 	void *p = *head;
40 	int sanity = 1024;
41 
42 	assert(buf);
43 	assert(len);
44 
45 	/* append at the tail */
46 	while (*head) {
47 		if (!--sanity) {
48 			lwsl_err("%s: buflist reached sanity limit\n", __func__);
49 			return -1;
50 		}
51 		if (*head == (*head)->next) {
52 			lwsl_err("%s: corrupt list points to self\n", __func__);
53 			return -1;
54 		}
55 		head = &((*head)->next);
56 	}
57 
58 	(void)p;
59 	lwsl_info("%s: len %u first %d %p\n", __func__, (unsigned int)len,
60 					      first, p);
61 
62 	nbuf = (struct lws_buflist *)lws_malloc(sizeof(struct lws_buflist) +
63 						len + LWS_PRE + 1, __func__);
64 	if (!nbuf) {
65 		lwsl_err("%s: OOM\n", __func__);
66 		return -1;
67 	}
68 
69 	nbuf->len = len;
70 	nbuf->pos = 0;
71 	nbuf->next = NULL;
72 
73 	/* whoever consumes this might need LWS_PRE from the start... */
74 	p = (uint8_t *)nbuf + sizeof(*nbuf) + LWS_PRE;
75 	memcpy(p, buf, len);
76 
77 	*head = nbuf;
78 
79 	return first; /* returns 1 if first segment just created */
80 }
81 
82 static int
lws_buflist_destroy_segment(struct lws_buflist ** head)83 lws_buflist_destroy_segment(struct lws_buflist **head)
84 {
85 	struct lws_buflist *old = *head;
86 
87 	assert(*head);
88 	*head = old->next;
89 	old->next = NULL;
90 	old->pos = old->len = 0;
91 	lws_free(old);
92 
93 	return !*head; /* returns 1 if last segment just destroyed */
94 }
95 
96 void
lws_buflist_destroy_all_segments(struct lws_buflist ** head)97 lws_buflist_destroy_all_segments(struct lws_buflist **head)
98 {
99 	struct lws_buflist *p = *head, *p1;
100 
101 	while (p) {
102 		p1 = p->next;
103 		p->next = NULL;
104 		lws_free(p);
105 		p = p1;
106 	}
107 
108 	*head = NULL;
109 }
110 
111 size_t
lws_buflist_next_segment_len(struct lws_buflist ** head,uint8_t ** buf)112 lws_buflist_next_segment_len(struct lws_buflist **head, uint8_t **buf)
113 {
114 	struct lws_buflist *b = (*head);
115 
116 	if (buf)
117 		*buf = NULL;
118 
119 	if (!b)
120 		return 0;	/* there is no next segment len */
121 
122 	if (!b->len && b->next)
123 		if (lws_buflist_destroy_segment(head))
124 			return 0;
125 
126 	b = (*head);
127 	if (!b)
128 		return 0;	/* there is no next segment len */
129 
130 	assert(b->pos < b->len);
131 
132 	if (buf)
133 		*buf = ((uint8_t *)b) + sizeof(*b) + b->pos + LWS_PRE;
134 
135 	return b->len - b->pos;
136 }
137 
138 size_t
lws_buflist_use_segment(struct lws_buflist ** head,size_t len)139 lws_buflist_use_segment(struct lws_buflist **head, size_t len)
140 {
141 	struct lws_buflist *b = (*head);
142 
143 	assert(b);
144 	assert(len);
145 	assert(b->pos + len <= b->len);
146 
147 	b->pos = b->pos + (size_t)len;
148 
149 	assert(b->pos <= b->len);
150 
151 	if (b->pos < b->len)
152 		return (unsigned int)(b->len - b->pos);
153 
154 	if (lws_buflist_destroy_segment(head))
155 		/* last segment was just destroyed */
156 		return 0;
157 
158 	return lws_buflist_next_segment_len(head, NULL);
159 }
160 
161 size_t
lws_buflist_total_len(struct lws_buflist ** head)162 lws_buflist_total_len(struct lws_buflist **head)
163 {
164 	struct lws_buflist *p = *head;
165 	size_t size = 0;
166 
167 	while (p) {
168 		size += p->len;
169 		p = p->next;
170 	}
171 
172 	return size;
173 }
174 
175 int
lws_buflist_linear_copy(struct lws_buflist ** head,size_t ofs,uint8_t * buf,size_t len)176 lws_buflist_linear_copy(struct lws_buflist **head, size_t ofs, uint8_t *buf,
177 			size_t len)
178 {
179 	struct lws_buflist *p = *head;
180 	uint8_t *obuf = buf;
181 	size_t s;
182 
183 	while (p && len) {
184 		if (ofs < p->len) {
185 			s = p->len - ofs;
186 			if (s > len)
187 				s = len;
188 			memcpy(buf, ((uint8_t *)&p[1]) + LWS_PRE + ofs, s);
189 			len -= s;
190 			buf += s;
191 			ofs = 0;
192 		} else
193 			ofs -= p->len;
194 		p = p->next;
195 	}
196 
197 	return lws_ptr_diff(buf, obuf);
198 }
199 
200 int
lws_buflist_linear_use(struct lws_buflist ** head,uint8_t * buf,size_t len)201 lws_buflist_linear_use(struct lws_buflist **head, uint8_t *buf, size_t len)
202 {
203 	uint8_t *obuf = buf;
204 	size_t s;
205 
206 	while (*head && len) {
207 		s = (*head)->len - (*head)->pos;
208 		if (s > len)
209 			s = len;
210 		memcpy(buf, ((uint8_t *)((*head) + 1)) +
211 			    LWS_PRE + (*head)->pos, s);
212 		len -= s;
213 		buf += s;
214 		lws_buflist_use_segment(head, s);
215 	}
216 
217 	return lws_ptr_diff(buf, obuf);
218 }
219 
220 int
lws_buflist_fragment_use(struct lws_buflist ** head,uint8_t * buf,size_t len,char * frag_first,char * frag_fin)221 lws_buflist_fragment_use(struct lws_buflist **head, uint8_t *buf,
222 			 size_t len, char *frag_first, char *frag_fin)
223 {
224 	uint8_t *obuf = buf;
225 	size_t s;
226 
227 	if (!*head)
228 		return 0;
229 
230 	s = (*head)->len - (*head)->pos;
231 	if (s > len)
232 		s = len;
233 
234 	if (frag_first)
235 		*frag_first = !(*head)->pos;
236 
237 	if (frag_fin)
238 		*frag_fin = (*head)->pos + s == (*head)->len;
239 
240 	memcpy(buf, ((uint8_t *)((*head) + 1)) + LWS_PRE + (*head)->pos, s);
241 	len -= s;
242 	buf += s;
243 	lws_buflist_use_segment(head, s);
244 
245 	return lws_ptr_diff(buf, obuf);
246 }
247 
248 #if defined(_DEBUG)
249 void
lws_buflist_describe(struct lws_buflist ** head,void * id,const char * reason)250 lws_buflist_describe(struct lws_buflist **head, void *id, const char *reason)
251 {
252 	struct lws_buflist *old;
253 	int n = 0;
254 
255 	if (*head == NULL)
256 		lwsl_notice("%p: %s: buflist empty\n", id, reason);
257 
258 	while (*head) {
259 		lwsl_notice("%p: %s: %d: %llu / %llu (%llu left)\n", id,
260 			    reason, n,
261 			    (unsigned long long)(*head)->pos,
262 			    (unsigned long long)(*head)->len,
263 			    (unsigned long long)(*head)->len - (*head)->pos);
264 		old = *head;
265 		head = &((*head)->next);
266 		if (*head == old) {
267 			lwsl_err("%s: next points to self\n", __func__);
268 			break;
269 		}
270 		n++;
271 	}
272 }
273 #endif
274