v1_decoder.cpp 4.92 KB
Newer Older
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 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
25 26 27 28 29

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
31 32
#include <stdlib.h>
#include <string.h>
33
#include <limits>
34
#include <limits.h>
35

36
#include "decoder.hpp"
37 38 39 40 41
#include "v1_decoder.hpp"
#include "likely.hpp"
#include "wire.hpp"
#include "err.hpp"

42
zmq::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 48
    errno_assert (rc == 0);

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 53 54
}

zmq::v1_decoder_t::~v1_decoder_t ()
{
55
    const int rc = _in_progress.close ();
56 57 58
    errno_assert (rc == 0);
}

59
int 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 63
    //  Otherwise allocate the buffer for message data and read the
    //  message data into it.
64
    if (*_tmpbuf == UCHAR_MAX)
65
        next_step (_tmpbuf, 8, &v1_decoder_t::eight_byte_size_ready);
66 67
    else {
        //  There has to be at least one byte (the flags) in the message).
68
        if (!*_tmpbuf) {
69 70 71 72
            errno = EPROTO;
            return -1;
        }

73 74
        if (_max_msg_size >= 0
            && static_cast<int64_t> (*_tmpbuf - 1) > _max_msg_size) {
75 76
            errno = EMSGSIZE;
            return -1;
77 78
        }

79
        int rc = _in_progress.close ();
80
        assert (rc == 0);
81
        rc = _in_progress.init_size (*_tmpbuf - 1);
82 83
        if (rc != 0) {
            errno_assert (errno == ENOMEM);
84
            rc = _in_progress.init ();
85
            errno_assert (rc == 0);
86 87
            errno = ENOMEM;
            return -1;
88
        }
89

90
        next_step (_tmpbuf, 1, &v1_decoder_t::flags_ready);
91
    }
92
    return 0;
93 94
}

95
int zmq::v1_decoder_t::eight_byte_size_ready (unsigned char const *)
96
{
97 98
    //  8-byte payload length is read. Allocate the buffer
    //  for message body and read the message data into it.
99
    const uint64_t payload_length = get_uint64 (_tmpbuf);
100

101 102
    //  There has to be at least one byte (the flags) in the message).
    if (payload_length == 0) {
103 104
        errno = EPROTO;
        return -1;
105
    }
106 107

    //  Message size must not exceed the maximum allowed size.
108 109
    if (_max_msg_size >= 0
        && payload_length - 1 > static_cast<uint64_t> (_max_msg_size)) {
110 111
        errno = EMSGSIZE;
        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 118
        errno = EMSGSIZE;
        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 132
        errno = ENOMEM;
        return -1;
133 134
    }

135
    next_step (_tmpbuf, 1, &v1_decoder_t::flags_ready);
136
    return 0;
137 138
}

139
int 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
}

150
int zmq::v1_decoder_t::message_ready (unsigned char const *)
151 152 153
{
    //  Message is completely read. Push it further and start reading
    //  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
}