ws_decoder.cpp
Go to the documentation of this file.
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  decoder_base_t<ws_decoder_t, shared_message_memory_allocator> (bufsize_),
19  _msg_flags (0),
20  _zero_copy (zero_copy_),
21  _max_msg_size (maxmsgsize_),
22  _must_mask (must_mask_),
23  _size (0)
24 {
25  memset (_tmpbuf, 0, sizeof (_tmpbuf));
26  int rc = _in_progress.init ();
27  errno_assert (rc == 0);
28 
29  // At the beginning, read one byte and go to opcode_ready state.
30  next_step (_tmpbuf, 1, &ws_decoder_t::opcode_ready);
31 }
32 
33 zmq::ws_decoder_t::~ws_decoder_t ()
34 {
35  const int rc = _in_progress.close ();
36  errno_assert (rc == 0);
37 }
38 
39 int zmq::ws_decoder_t::opcode_ready (unsigned char const *)
40 {
41  const bool final = (_tmpbuf[0] & 0x80) != 0; // final bit
42  if (!final)
43  return -1; // non final messages are not supported
44 
45  _opcode = static_cast<zmq::ws_protocol_t::opcode_t> (_tmpbuf[0] & 0xF);
46 
47  _msg_flags = 0;
48 
49  switch (_opcode) {
51  break;
53  _msg_flags = msg_t::command | msg_t::close_cmd;
54  break;
56  _msg_flags = msg_t::ping | msg_t::command;
57  break;
59  _msg_flags = msg_t::pong | msg_t::command;
60  break;
61  default:
62  return -1;
63  }
64 
65  next_step (_tmpbuf, 1, &ws_decoder_t::size_first_byte_ready);
66 
67  return 0;
68 }
69 
70 int zmq::ws_decoder_t::size_first_byte_ready (unsigned char const *read_from_)
71 {
72  const bool is_masked = (_tmpbuf[0] & 0x80) != 0;
73 
74  if (is_masked != _must_mask) // wrong mask value
75  return -1;
76 
77  _size = static_cast<uint64_t> (_tmpbuf[0] & 0x7F);
78 
79  if (_size < 126) {
80  if (_must_mask)
81  next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready);
82  else if (_opcode == ws_protocol_t::opcode_binary) {
83  if (_size == 0)
84  return -1;
85  next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
86  } else
87  return size_ready (read_from_);
88  } else if (_size == 126)
89  next_step (_tmpbuf, 2, &ws_decoder_t::short_size_ready);
90  else
91  next_step (_tmpbuf, 8, &ws_decoder_t::long_size_ready);
92 
93  return 0;
94 }
95 
96 
97 int zmq::ws_decoder_t::short_size_ready (unsigned char const *read_from_)
98 {
99  _size = (_tmpbuf[0] << 8) | _tmpbuf[1];
100 
101  if (_must_mask)
102  next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready);
103  else if (_opcode == ws_protocol_t::opcode_binary) {
104  if (_size == 0)
105  return -1;
106  next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
107  } else
108  return size_ready (read_from_);
109 
110  return 0;
111 }
112 
113 int zmq::ws_decoder_t::long_size_ready (unsigned char const *read_from_)
114 {
115  // The payload size is encoded as 64-bit unsigned integer.
116  // The most significant byte comes first.
117  _size = get_uint64 (_tmpbuf);
118 
119  if (_must_mask)
120  next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready);
121  else if (_opcode == ws_protocol_t::opcode_binary) {
122  if (_size == 0)
123  return -1;
124  next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
125  } else
126  return size_ready (read_from_);
127 
128  return 0;
129 }
130 
131 int zmq::ws_decoder_t::mask_ready (unsigned char const *read_from_)
132 {
133  memcpy (_mask, _tmpbuf, 4);
134 
135  if (_opcode == ws_protocol_t::opcode_binary) {
136  if (_size == 0)
137  return -1;
138 
139  next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
140  } else
141  return size_ready (read_from_);
142 
143  return 0;
144 }
145 
146 int zmq::ws_decoder_t::flags_ready (unsigned char const *read_from_)
147 {
148  unsigned char flags;
149 
150  if (_must_mask)
151  flags = _tmpbuf[0] ^ _mask[0];
152  else
153  flags = _tmpbuf[0];
154 
156  _msg_flags |= msg_t::more;
158  _msg_flags |= msg_t::command;
159 
160  _size--;
161 
162  return size_ready (read_from_);
163 }
164 
165 
166 int zmq::ws_decoder_t::size_ready (unsigned char const *read_pos_)
167 {
168  // Message size must not exceed the maximum allowed size.
169  if (_max_msg_size >= 0)
170  if (unlikely (_size > static_cast<uint64_t> (_max_msg_size))) {
171  errno = EMSGSIZE;
172  return -1;
173  }
174 
175  // Message size must fit into size_t data type.
176  if (unlikely (_size != static_cast<size_t> (_size))) {
177  errno = EMSGSIZE;
178  return -1;
179  }
180 
181  int rc = _in_progress.close ();
182  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  shared_message_memory_allocator &allocator = get_allocator ();
188  if (unlikely (!_zero_copy || allocator.data () > read_pos_
189  || static_cast<size_t> (read_pos_ - allocator.data ())
190  > allocator.size ()
191  || _size > static_cast<size_t> (
192  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  rc = _in_progress.init_size (static_cast<size_t> (_size));
197  } 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  rc = _in_progress.init (
202  const_cast<unsigned char *> (read_pos_), static_cast<size_t> (_size),
204  allocator.provide_content ());
205 
206  // For small messages, data has been copied and refcount does not have to be increased
207  if (_in_progress.is_zcmsg ()) {
208  allocator.advance_content ();
209  allocator.inc_ref ();
210  }
211  }
212 
213  if (unlikely (rc)) {
214  errno_assert (errno == ENOMEM);
215  rc = _in_progress.init ();
216  errno_assert (rc == 0);
217  errno = ENOMEM;
218  return -1;
219  }
220 
221  _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  next_step (_in_progress.data (), _in_progress.size (),
229  &ws_decoder_t::message_ready);
230 
231  return 0;
232 }
233 
234 int zmq::ws_decoder_t::message_ready (unsigned char const *)
235 {
236  if (_must_mask) {
237  int mask_index = _opcode == ws_protocol_t::opcode_binary ? 1 : 0;
238 
239  unsigned char *data =
240  static_cast<unsigned char *> (_in_progress.data ());
241  for (size_t i = 0; i < _size; ++i, mask_index++)
242  data[i] = data[i] ^ _mask[mask_index % 4];
243  }
244 
245  // Message is completely read. Signal this to the caller
246  // and prepare to decode next message.
247  next_step (_tmpbuf, 1, &ws_decoder_t::opcode_ready);
248  return 1;
249 }
zmq::msg_t::command
@ command
Definition: msg.hpp:56
zmq::shared_message_memory_allocator::call_dec_ref
static void call_dec_ref(void *, void *hint_)
Definition: decoder_allocators.cpp:103
precompiled.hpp
errno
int errno
zmq::ws_protocol_t::opcode_close
@ opcode_close
Definition: ws_protocol.hpp:18
flags
GLbitfield flags
Definition: glcorearb.h:3585
zmq::ZMQ_FINAL::_tmpbuf
unsigned char _tmpbuf[8]
Definition: v1_decoder.hpp:26
wire.hpp
errno_assert
#define errno_assert(x)
Definition: err.hpp:113
zmq::ZMQ_FINAL::_in_progress
msg_t _in_progress
Definition: raw_decoder.hpp:32
ws_decoder.hpp
zmq::msg_t::init
int init()
Definition: msg.cpp:50
i
int i
Definition: gmock-matchers_test.cc:764
EMSGSIZE
#define EMSGSIZE
Definition: zmq.h:131
zmq::msg_t::more
@ more
Definition: msg.hpp:55
zmq::ws_protocol_t::opcode_binary
@ opcode_binary
Definition: ws_protocol.hpp:17
zmq::ws_protocol_t::opcode_pong
@ opcode_pong
Definition: ws_protocol.hpp:20
zmq::ws_protocol_t::more_flag
@ more_flag
Definition: ws_protocol.hpp:25
zmq::ws_protocol_t::opcode_t
opcode_t
Definition: ws_protocol.hpp:13
zmq::ws_protocol_t::opcode_ping
@ opcode_ping
Definition: ws_protocol.hpp:19
zmq::get_uint64
uint64_t get_uint64(const unsigned char *buffer_)
Definition: wire.hpp:63
err.hpp
likely.hpp
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
zmq::msg_t::pong
@ pong
Definition: msg.hpp:60
zmq::msg_t::ping
@ ping
Definition: msg.hpp:59
ws_protocol.hpp
unlikely
#define unlikely(x)
Definition: likely.hpp:11
zmq::ws_protocol_t::command_flag
@ command_flag
Definition: ws_protocol.hpp:26
zmq::msg_t::close_cmd
@ close_cmd
Definition: msg.hpp:63


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:02