1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2020 Andy Green <[email protected]>
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 * C++ classes for Secure Streams - atomic heap messages
25 */
26
27 #include <libwebsockets.hxx>
28 #include "private-lib-misc-lwsac.h"
29
30 void
start(bool atomic)31 lssAc::start(bool atomic)
32 {
33 if (atomic && ac->next) {
34 struct lwsac *ac2 = NULL, *i;
35 size_t total = (size_t)lwsac_total_alloc(ac);
36 uint8_t *p = (uint8_t *)lwsac_use(&ac2, total, total);
37
38 /*
39 * He wants a single linear buffer, and we have more than one
40 * piece... let's make a new, single one, copy the fragments
41 * in and replace the fragmented one with the unified copy.
42 */
43
44 i = ac;
45 while (i) {
46 size_t bl = lwsac_get_tail_pos(i) -
47 lwsac_sizeof(i == ac);
48 memcpy(p, (uint8_t *)i + lwsac_sizeof(i == ac), bl);
49 p += bl;
50 }
51
52 lwsac_free(&ac);
53 ac = ac2;
54 }
55
56 iter = ac;
57 }
58
59 int
get(lssbuf_t * lb)60 lssAc::get(lssbuf_t *lb)
61 {
62 if (!ac)
63 return 1;
64
65 lb->buf = (uint8_t *)iter + lwsac_sizeof(iter == ac);
66 lb->len = lwsac_get_tail_pos(iter) - lwsac_sizeof(iter == ac);
67 iter = iter->next;
68
69 return 0;
70 }
71
72 void
append(lssbuf_t * lb)73 lssAc::append(lssbuf_t *lb)
74 {
75 uint8_t *p = (uint8_t *)lwsac_use(&ac, lb->len, lb->len);
76
77 if (!p)
78 throw lssException("oom");
79 memcpy(p, lb->buf, lb->len);
80 }
81