/src/libzmq/src/ws_decoder.cpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #include "precompiled.hpp" |
4 | | #include <stdlib.h> |
5 | | #include <string.h> |
6 | | #include <cmath> |
7 | | |
8 | | #include "ws_protocol.hpp" |
9 | | #include "ws_decoder.hpp" |
10 | | #include "likely.hpp" |
11 | | #include "wire.hpp" |
12 | | #include "err.hpp" |
13 | | |
14 | | zmq::ws_decoder_t::ws_decoder_t (size_t bufsize_, |
15 | | int64_t maxmsgsize_, |
16 | | bool zero_copy_, |
17 | | bool must_mask_) : |
18 | 0 | decoder_base_t<ws_decoder_t, shared_message_memory_allocator> (bufsize_), |
19 | 0 | _msg_flags (0), |
20 | 0 | _zero_copy (zero_copy_), |
21 | 0 | _max_msg_size (maxmsgsize_), |
22 | 0 | _must_mask (must_mask_), |
23 | 0 | _size (0) |
24 | 0 | { |
25 | 0 | memset (_tmpbuf, 0, sizeof (_tmpbuf)); |
26 | 0 | int rc = _in_progress.init (); |
27 | 0 | errno_assert (rc == 0); |
28 | | |
29 | | // At the beginning, read one byte and go to opcode_ready state. |
30 | 0 | next_step (_tmpbuf, 1, &ws_decoder_t::opcode_ready); |
31 | 0 | } |
32 | | |
33 | | zmq::ws_decoder_t::~ws_decoder_t () |
34 | 0 | { |
35 | 0 | const int rc = _in_progress.close (); |
36 | 0 | errno_assert (rc == 0); |
37 | 0 | } |
38 | | |
39 | | int zmq::ws_decoder_t::opcode_ready (unsigned char const *) |
40 | 0 | { |
41 | 0 | const bool final = (_tmpbuf[0] & 0x80) != 0; // final bit |
42 | 0 | if (!final) |
43 | 0 | return -1; // non final messages are not supported |
44 | | |
45 | 0 | _opcode = static_cast<zmq::ws_protocol_t::opcode_t> (_tmpbuf[0] & 0xF); |
46 | |
|
47 | 0 | _msg_flags = 0; |
48 | |
|
49 | 0 | switch (_opcode) { |
50 | 0 | case zmq::ws_protocol_t::opcode_binary: |
51 | 0 | break; |
52 | 0 | case zmq::ws_protocol_t::opcode_close: |
53 | 0 | _msg_flags = msg_t::command | msg_t::close_cmd; |
54 | 0 | break; |
55 | 0 | case zmq::ws_protocol_t::opcode_ping: |
56 | 0 | _msg_flags = msg_t::ping | msg_t::command; |
57 | 0 | break; |
58 | 0 | case zmq::ws_protocol_t::opcode_pong: |
59 | 0 | _msg_flags = msg_t::pong | msg_t::command; |
60 | 0 | break; |
61 | 0 | default: |
62 | 0 | return -1; |
63 | 0 | } |
64 | | |
65 | 0 | next_step (_tmpbuf, 1, &ws_decoder_t::size_first_byte_ready); |
66 | |
|
67 | 0 | return 0; |
68 | 0 | } |
69 | | |
70 | | int zmq::ws_decoder_t::size_first_byte_ready (unsigned char const *read_from_) |
71 | 0 | { |
72 | 0 | const bool is_masked = (_tmpbuf[0] & 0x80) != 0; |
73 | |
|
74 | 0 | if (is_masked != _must_mask) // wrong mask value |
75 | 0 | return -1; |
76 | | |
77 | 0 | _size = static_cast<uint64_t> (_tmpbuf[0] & 0x7F); |
78 | |
|
79 | 0 | if (_size < 126) { |
80 | 0 | if (_must_mask) |
81 | 0 | next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready); |
82 | 0 | else if (_opcode == ws_protocol_t::opcode_binary) { |
83 | 0 | if (_size == 0) |
84 | 0 | return -1; |
85 | 0 | next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready); |
86 | 0 | } else |
87 | 0 | return size_ready (read_from_); |
88 | 0 | } else if (_size == 126) |
89 | 0 | next_step (_tmpbuf, 2, &ws_decoder_t::short_size_ready); |
90 | 0 | else |
91 | 0 | next_step (_tmpbuf, 8, &ws_decoder_t::long_size_ready); |
92 | | |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | | |
97 | | int zmq::ws_decoder_t::short_size_ready (unsigned char const *read_from_) |
98 | 0 | { |
99 | 0 | _size = (_tmpbuf[0] << 8) | _tmpbuf[1]; |
100 | |
|
101 | 0 | if (_must_mask) |
102 | 0 | next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready); |
103 | 0 | else if (_opcode == ws_protocol_t::opcode_binary) { |
104 | 0 | if (_size == 0) |
105 | 0 | return -1; |
106 | 0 | next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready); |
107 | 0 | } else |
108 | 0 | return size_ready (read_from_); |
109 | | |
110 | 0 | return 0; |
111 | 0 | } |
112 | | |
113 | | int zmq::ws_decoder_t::long_size_ready (unsigned char const *read_from_) |
114 | 0 | { |
115 | | // The payload size is encoded as 64-bit unsigned integer. |
116 | | // The most significant byte comes first. |
117 | 0 | _size = get_uint64 (_tmpbuf); |
118 | |
|
119 | 0 | if (_must_mask) |
120 | 0 | next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready); |
121 | 0 | else if (_opcode == ws_protocol_t::opcode_binary) { |
122 | 0 | if (_size == 0) |
123 | 0 | return -1; |
124 | 0 | next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready); |
125 | 0 | } else |
126 | 0 | return size_ready (read_from_); |
127 | | |
128 | 0 | return 0; |
129 | 0 | } |
130 | | |
131 | | int zmq::ws_decoder_t::mask_ready (unsigned char const *read_from_) |
132 | 0 | { |
133 | 0 | memcpy (_mask, _tmpbuf, 4); |
134 | |
|
135 | 0 | if (_opcode == ws_protocol_t::opcode_binary) { |
136 | 0 | if (_size == 0) |
137 | 0 | return -1; |
138 | | |
139 | 0 | next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready); |
140 | 0 | } else |
141 | 0 | return size_ready (read_from_); |
142 | | |
143 | 0 | return 0; |
144 | 0 | } |
145 | | |
146 | | int zmq::ws_decoder_t::flags_ready (unsigned char const *read_from_) |
147 | 0 | { |
148 | 0 | unsigned char flags; |
149 | |
|
150 | 0 | if (_must_mask) |
151 | 0 | flags = _tmpbuf[0] ^ _mask[0]; |
152 | 0 | else |
153 | 0 | flags = _tmpbuf[0]; |
154 | |
|
155 | 0 | if (flags & ws_protocol_t::more_flag) |
156 | 0 | _msg_flags |= msg_t::more; |
157 | 0 | if (flags & ws_protocol_t::command_flag) |
158 | 0 | _msg_flags |= msg_t::command; |
159 | |
|
160 | 0 | _size--; |
161 | |
|
162 | 0 | return size_ready (read_from_); |
163 | 0 | } |
164 | | |
165 | | |
166 | | int zmq::ws_decoder_t::size_ready (unsigned char const *read_pos_) |
167 | 0 | { |
168 | | // Message size must not exceed the maximum allowed size. |
169 | 0 | if (_max_msg_size >= 0) |
170 | 0 | if (unlikely (_size > static_cast<uint64_t> (_max_msg_size))) { |
171 | 0 | errno = EMSGSIZE; |
172 | 0 | return -1; |
173 | 0 | } |
174 | | |
175 | | // Message size must fit into size_t data type. |
176 | 0 | if (unlikely (_size != static_cast<size_t> (_size))) { |
177 | 0 | errno = EMSGSIZE; |
178 | 0 | return -1; |
179 | 0 | } |
180 | | |
181 | 0 | int rc = _in_progress.close (); |
182 | 0 | assert (rc == 0); |
183 | | |
184 | | // the current message can exceed the current buffer. We have to copy the buffer |
185 | | // data into a new message and complete it in the next receive. |
186 | | |
187 | 0 | shared_message_memory_allocator &allocator = get_allocator (); |
188 | 0 | if (unlikely (!_zero_copy || allocator.data () > read_pos_ |
189 | 0 | || static_cast<size_t> (read_pos_ - allocator.data ()) |
190 | 0 | > allocator.size () |
191 | 0 | || _size > static_cast<size_t> ( |
192 | 0 | allocator.data () + allocator.size () - read_pos_))) { |
193 | | // a new message has started, but the size would exceed the pre-allocated arena |
194 | | // (or read_pos_ is in the initial handshake buffer) |
195 | | // this happens every time when a message does not fit completely into the buffer |
196 | 0 | rc = _in_progress.init_size (static_cast<size_t> (_size)); |
197 | 0 | } else { |
198 | | // construct message using n bytes from the buffer as storage |
199 | | // increase buffer ref count |
200 | | // if the message will be a large message, pass a valid refcnt memory location as well |
201 | 0 | rc = _in_progress.init ( |
202 | 0 | const_cast<unsigned char *> (read_pos_), static_cast<size_t> (_size), |
203 | 0 | shared_message_memory_allocator::call_dec_ref, allocator.buffer (), |
204 | 0 | allocator.provide_content ()); |
205 | | |
206 | | // For small messages, data has been copied and refcount does not have to be increased |
207 | 0 | if (_in_progress.is_zcmsg ()) { |
208 | 0 | allocator.advance_content (); |
209 | 0 | allocator.inc_ref (); |
210 | 0 | } |
211 | 0 | } |
212 | |
|
213 | 0 | if (unlikely (rc)) { |
214 | 0 | errno_assert (errno == ENOMEM); |
215 | 0 | rc = _in_progress.init (); |
216 | 0 | errno_assert (rc == 0); |
217 | 0 | errno = ENOMEM; |
218 | 0 | return -1; |
219 | 0 | } |
220 | | |
221 | 0 | _in_progress.set_flags (_msg_flags); |
222 | | // this sets read_pos to |
223 | | // the message data address if the data needs to be copied |
224 | | // for small message / messages exceeding the current buffer |
225 | | // or |
226 | | // to the current start address in the buffer because the message |
227 | | // was constructed to use n bytes from the address passed as argument |
228 | 0 | next_step (_in_progress.data (), _in_progress.size (), |
229 | 0 | &ws_decoder_t::message_ready); |
230 | |
|
231 | 0 | return 0; |
232 | 0 | } |
233 | | |
234 | | int zmq::ws_decoder_t::message_ready (unsigned char const *) |
235 | 0 | { |
236 | 0 | if (_must_mask) { |
237 | 0 | int mask_index = _opcode == ws_protocol_t::opcode_binary ? 1 : 0; |
238 | |
|
239 | 0 | unsigned char *data = |
240 | 0 | static_cast<unsigned char *> (_in_progress.data ()); |
241 | 0 | for (size_t i = 0; i < _size; ++i, mask_index++) |
242 | 0 | data[i] = data[i] ^ _mask[mask_index % 4]; |
243 | 0 | } |
244 | | |
245 | | // Message is completely read. Signal this to the caller |
246 | | // and prepare to decode next message. |
247 | 0 | next_step (_tmpbuf, 1, &ws_decoder_t::opcode_ready); |
248 | 0 | return 1; |
249 | 0 | } |