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_WIRE_HPP_INCLUDED__
31#define __ZMQ_WIRE_HPP_INCLUDED__
32
33#include "stdint.hpp"
34
35namespace zmq
36{
37// Helper functions to convert different integer types to/from network
38// byte order.
39
40inline void put_uint8 (unsigned char *buffer_, uint8_t value_)
41{
42 *buffer_ = value_;
43}
44
45inline uint8_t get_uint8 (const unsigned char *buffer_)
46{
47 return *buffer_;
48}
49
50inline void put_uint16 (unsigned char *buffer_, uint16_t value_)
51{
52 buffer_[0] = static_cast<unsigned char> (((value_) >> 8) & 0xff);
53 buffer_[1] = static_cast<unsigned char> (value_ & 0xff);
54}
55
56inline uint16_t get_uint16 (const unsigned char *buffer_)
57{
58 return ((static_cast<uint16_t> (buffer_[0])) << 8)
59 | (static_cast<uint16_t> (buffer_[1]));
60}
61
62inline void put_uint32 (unsigned char *buffer_, uint32_t value_)
63{
64 buffer_[0] = static_cast<unsigned char> (((value_) >> 24) & 0xff);
65 buffer_[1] = static_cast<unsigned char> (((value_) >> 16) & 0xff);
66 buffer_[2] = static_cast<unsigned char> (((value_) >> 8) & 0xff);
67 buffer_[3] = static_cast<unsigned char> (value_ & 0xff);
68}
69
70inline uint32_t get_uint32 (const unsigned char *buffer_)
71{
72 return ((static_cast<uint32_t> (buffer_[0])) << 24)
73 | ((static_cast<uint32_t> (buffer_[1])) << 16)
74 | ((static_cast<uint32_t> (buffer_[2])) << 8)
75 | (static_cast<uint32_t> (buffer_[3]));
76}
77
78inline void put_uint64 (unsigned char *buffer_, uint64_t value_)
79{
80 buffer_[0] = static_cast<unsigned char> (((value_) >> 56) & 0xff);
81 buffer_[1] = static_cast<unsigned char> (((value_) >> 48) & 0xff);
82 buffer_[2] = static_cast<unsigned char> (((value_) >> 40) & 0xff);
83 buffer_[3] = static_cast<unsigned char> (((value_) >> 32) & 0xff);
84 buffer_[4] = static_cast<unsigned char> (((value_) >> 24) & 0xff);
85 buffer_[5] = static_cast<unsigned char> (((value_) >> 16) & 0xff);
86 buffer_[6] = static_cast<unsigned char> (((value_) >> 8) & 0xff);
87 buffer_[7] = static_cast<unsigned char> (value_ & 0xff);
88}
89
90inline uint64_t get_uint64 (const unsigned char *buffer_)
91{
92 return ((static_cast<uint64_t> (buffer_[0])) << 56)
93 | ((static_cast<uint64_t> (buffer_[1])) << 48)
94 | ((static_cast<uint64_t> (buffer_[2])) << 40)
95 | ((static_cast<uint64_t> (buffer_[3])) << 32)
96 | ((static_cast<uint64_t> (buffer_[4])) << 24)
97 | ((static_cast<uint64_t> (buffer_[5])) << 16)
98 | ((static_cast<uint64_t> (buffer_[6])) << 8)
99 | (static_cast<uint64_t> (buffer_[7]));
100}
101}
102
103#endif
104