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 #ifndef __ZMQ_DECODER_HPP_INCLUDED__
31 #define __ZMQ_DECODER_HPP_INCLUDED__
32 
33 #include <algorithm>
34 #include <cstddef>
35 #include <cstring>
36 
37 #include "decoder_allocators.hpp"
38 #include "err.hpp"
39 #include "i_decoder.hpp"
40 #include "stdint.hpp"
41 
42 namespace zmq
43 {
44 //  Helper base class for decoders that know the amount of data to read
45 //  in advance at any moment. Knowing the amount in advance is a property
46 //  of the protocol used. 0MQ framing protocol is based size-prefixed
47 //  paradigm, which qualifies it to be parsed by this class.
48 //  On the other hand, XML-based transports (like XMPP or SOAP) don't allow
49 //  for knowing the size of data to read in advance and should use different
50 //  decoding algorithms.
51 //
52 //  This class implements the state machine that parses the incoming buffer.
53 //  Derived class should implement individual state machine actions.
54 //
55 //  Buffer management is done by an allocator policy.
56 template <typename T, typename A = c_single_allocator>
57 class decoder_base_t : public i_decoder
58 {
59   public:
decoder_base_t(const size_t buf_size_)60     explicit decoder_base_t (const size_t buf_size_) :
61         _next (NULL),
62         _read_pos (NULL),
63         _to_read (0),
64         _allocator (buf_size_)
65     {
66         _buf = _allocator.allocate ();
67     }
68 
~decoder_base_t()69     ~decoder_base_t () ZMQ_OVERRIDE { _allocator.deallocate (); }
70 
71     //  Returns a buffer to be filled with binary data.
get_buffer(unsigned char ** data_,std::size_t * size_)72     void get_buffer (unsigned char **data_, std::size_t *size_) ZMQ_FINAL
73     {
74         _buf = _allocator.allocate ();
75 
76         //  If we are expected to read large message, we'll opt for zero-
77         //  copy, i.e. we'll ask caller to fill the data directly to the
78         //  message. Note that subsequent read(s) are non-blocking, thus
79         //  each single read reads at most SO_RCVBUF bytes at once not
80         //  depending on how large is the chunk returned from here.
81         //  As a consequence, large messages being received won't block
82         //  other engines running in the same I/O thread for excessive
83         //  amounts of time.
84         if (_to_read >= _allocator.size ()) {
85             *data_ = _read_pos;
86             *size_ = _to_read;
87             return;
88         }
89 
90         *data_ = _buf;
91         *size_ = _allocator.size ();
92     }
93 
94     //  Processes the data in the buffer previously allocated using
95     //  get_buffer function. size_ argument specifies number of bytes
96     //  actually filled into the buffer. Function returns 1 when the
97     //  whole message was decoded or 0 when more data is required.
98     //  On error, -1 is returned and errno set accordingly.
99     //  Number of bytes processed is returned in bytes_used_.
decode(const unsigned char * data_,std::size_t size_,std::size_t & bytes_used_)100     int decode (const unsigned char *data_,
101                 std::size_t size_,
102                 std::size_t &bytes_used_) ZMQ_FINAL
103     {
104         bytes_used_ = 0;
105 
106         //  In case of zero-copy simply adjust the pointers, no copying
107         //  is required. Also, run the state machine in case all the data
108         //  were processed.
109         if (data_ == _read_pos) {
110             zmq_assert (size_ <= _to_read);
111             _read_pos += size_;
112             _to_read -= size_;
113             bytes_used_ = size_;
114 
115             while (!_to_read) {
116                 const int rc =
117                   (static_cast<T *> (this)->*_next) (data_ + bytes_used_);
118                 if (rc != 0)
119                     return rc;
120             }
121             return 0;
122         }
123 
124         while (bytes_used_ < size_) {
125             //  Copy the data from buffer to the message.
126             const size_t to_copy = std::min (_to_read, size_ - bytes_used_);
127             // Only copy when destination address is different from the
128             // current address in the buffer.
129             if (_read_pos != data_ + bytes_used_) {
130                 memcpy (_read_pos, data_ + bytes_used_, to_copy);
131             }
132 
133             _read_pos += to_copy;
134             _to_read -= to_copy;
135             bytes_used_ += to_copy;
136             //  Try to get more space in the message to fill in.
137             //  If none is available, return.
138             while (_to_read == 0) {
139                 // pass current address in the buffer
140                 const int rc =
141                   (static_cast<T *> (this)->*_next) (data_ + bytes_used_);
142                 if (rc != 0)
143                     return rc;
144             }
145         }
146 
147         return 0;
148     }
149 
resize_buffer(std::size_t new_size_)150     void resize_buffer (std::size_t new_size_) ZMQ_FINAL
151     {
152         _allocator.resize (new_size_);
153     }
154 
155   protected:
156     //  Prototype of state machine action. Action should return false if
157     //  it is unable to push the data to the system.
158     typedef int (T::*step_t) (unsigned char const *);
159 
160     //  This function should be called from derived class to read data
161     //  from the buffer and schedule next state machine action.
next_step(void * read_pos_,std::size_t to_read_,step_t next_)162     void next_step (void *read_pos_, std::size_t to_read_, step_t next_)
163     {
164         _read_pos = static_cast<unsigned char *> (read_pos_);
165         _to_read = to_read_;
166         _next = next_;
167     }
168 
get_allocator()169     A &get_allocator () { return _allocator; }
170 
171   private:
172     //  Next step. If set to NULL, it means that associated data stream
173     //  is dead. Note that there can be still data in the process in such
174     //  case.
175     step_t _next;
176 
177     //  Where to store the read data.
178     unsigned char *_read_pos;
179 
180     //  How much data to read before taking next step.
181     std::size_t _to_read;
182 
183     //  The duffer for data to decode.
184     A _allocator;
185     unsigned char *_buf;
186 
187     ZMQ_NON_COPYABLE_NOR_MOVABLE (decoder_base_t)
188 };
189 }
190 
191 #endif
192