• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (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 #if defined(_DEBUG)
201 void
lws_buflist_describe(struct lws_buflist ** head,void * id,const char * reason)202 lws_buflist_describe(struct lws_buflist **head, void *id, const char *reason)
203 {
204 	struct lws_buflist *old;
205 	int n = 0;
206 
207 	if (*head == NULL)
208 		lwsl_notice("%p: %s: buflist empty\n", id, reason);
209 
210 	while (*head) {
211 		lwsl_notice("%p: %s: %d: %llu / %llu (%llu left)\n", id,
212 			    reason, n,
213 			    (unsigned long long)(*head)->pos,
214 			    (unsigned long long)(*head)->len,
215 			    (unsigned long long)(*head)->len - (*head)->pos);
216 		old = *head;
217 		head = &((*head)->next);
218 		if (*head == old) {
219 			lwsl_err("%s: next points to self\n", __func__);
220 			break;
221 		}
222 		n++;
223 	}
224 }
225 #endif
226