1/*
2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#include "precompiled.hpp"
31#include <stdlib.h>
32#include <string.h>
33#include <limits>
34#include <limits.h>
35
36#include "decoder.hpp"
37#include "v1_decoder.hpp"
38#include "likely.hpp"
39#include "wire.hpp"
40#include "err.hpp"
41
42zmq::v1_decoder_t::v1_decoder_t (size_t bufsize_, int64_t maxmsgsize_) :
43 decoder_base_t<v1_decoder_t> (bufsize_),
44 _max_msg_size (maxmsgsize_)
45{
46 int rc = _in_progress.init ();
47 errno_assert (rc == 0);
48
49 // At the beginning, read one byte and go to one_byte_size_ready state.
50 next_step (_tmpbuf, 1, &v1_decoder_t::one_byte_size_ready);
51}
52
53zmq::v1_decoder_t::~v1_decoder_t ()
54{
55 int rc = _in_progress.close ();
56 errno_assert (rc == 0);
57}
58
59int zmq::v1_decoder_t::one_byte_size_ready (unsigned char const *)
60{
61 // First byte of size is read. If it is UCHAR_MAX (0xff) read 8-byte size.
62 // Otherwise allocate the buffer for message data and read the
63 // message data into it.
64 if (*_tmpbuf == UCHAR_MAX)
65 next_step (_tmpbuf, 8, &v1_decoder_t::eight_byte_size_ready);
66 else {
67 // There has to be at least one byte (the flags) in the message).
68 if (!*_tmpbuf) {
69 errno = EPROTO;
70 return -1;
71 }
72
73 if (_max_msg_size >= 0
74 && static_cast<int64_t> (*_tmpbuf - 1) > _max_msg_size) {
75 errno = EMSGSIZE;
76 return -1;
77 }
78
79 int rc = _in_progress.close ();
80 assert (rc == 0);
81 rc = _in_progress.init_size (*_tmpbuf - 1);
82 if (rc != 0) {
83 errno_assert (errno == ENOMEM);
84 rc = _in_progress.init ();
85 errno_assert (rc == 0);
86 errno = ENOMEM;
87 return -1;
88 }
89
90 next_step (_tmpbuf, 1, &v1_decoder_t::flags_ready);
91 }
92 return 0;
93}
94
95int zmq::v1_decoder_t::eight_byte_size_ready (unsigned char const *)
96{
97 // 8-byte payload length is read. Allocate the buffer
98 // for message body and read the message data into it.
99 const uint64_t payload_length = get_uint64 (_tmpbuf);
100
101 // There has to be at least one byte (the flags) in the message).
102 if (payload_length == 0) {
103 errno = EPROTO;
104 return -1;
105 }
106
107 // Message size must not exceed the maximum allowed size.
108 if (_max_msg_size >= 0
109 && payload_length - 1 > static_cast<uint64_t> (_max_msg_size)) {
110 errno = EMSGSIZE;
111 return -1;
112 }
113
114#ifndef __aarch64__
115 // Message size must fit within range of size_t data type.
116 if (payload_length - 1 > std::numeric_limits<size_t>::max ()) {
117 errno = EMSGSIZE;
118 return -1;
119 }
120#endif
121
122 const size_t msg_size = static_cast<size_t> (payload_length - 1);
123
124 int rc = _in_progress.close ();
125 assert (rc == 0);
126 rc = _in_progress.init_size (msg_size);
127 if (rc != 0) {
128 errno_assert (errno == ENOMEM);
129 rc = _in_progress.init ();
130 errno_assert (rc == 0);
131 errno = ENOMEM;
132 return -1;
133 }
134
135 next_step (_tmpbuf, 1, &v1_decoder_t::flags_ready);
136 return 0;
137}
138
139int zmq::v1_decoder_t::flags_ready (unsigned char const *)
140{
141 // Store the flags from the wire into the message structure.
142 _in_progress.set_flags (_tmpbuf[0] & msg_t::more);
143
144 next_step (_in_progress.data (), _in_progress.size (),
145 &v1_decoder_t::message_ready);
146
147 return 0;
148}
149
150int zmq::v1_decoder_t::message_ready (unsigned char const *)
151{
152 // Message is completely read. Push it further and start reading
153 // new message. (in_progress is a 0-byte message after this point.)
154 next_step (_tmpbuf, 1, &v1_decoder_t::one_byte_size_ready);
155 return 1;
156}
157