AgentXcpp  Revision:0.2
Internals Documentation
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Pages
util.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2012 Tanjeff-Nicolai Moos <tanjeff@cccmz.de>
3  *
4  * This file is part of the agentXcpp library.
5  *
6  * AgentXcpp is free software: you can redistribute it and/or modify
7  * it under the terms of the AgentXcpp library license, version 1, which
8  * consists of the GNU General Public License and some additional
9  * permissions.
10  *
11  * AgentXcpp is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See the AgentXcpp library license in the LICENSE file of this package
17  * for more details.
18  */
19 
20 #ifndef _HELPER_H_
21 #define _HELPER_H_
22 
23 #include <boost/cstdint.hpp>
24 
25 #include "binary.hpp"
26 
27 using boost::uint16_t;
28 using boost::uint32_t;
29 
30 namespace agentxcpp
31 {
32 
33  inline uint64_t read64(binary::const_iterator& pos, bool big_endian)
34  {
35  uint64_t value;
36  if( big_endian )
37  {
38  value = static_cast<uint64_t>(*pos++) << 56;
39  value |= static_cast<uint64_t>(*pos++) << 48;
40  value |= static_cast<uint64_t>(*pos++) << 40;
41  value |= static_cast<uint64_t>(*pos++) << 32;
42  value |= static_cast<uint64_t>(*pos++) << 24;
43  value |= static_cast<uint64_t>(*pos++) << 16;
44  value |= static_cast<uint64_t>(*pos++) << 8;
45  value |= static_cast<uint64_t>(*pos++) << 0;
46  }
47  else
48  {
49  value = static_cast<uint64_t>(*pos++) << 0;
50  value |= static_cast<uint64_t>(*pos++) << 8;
51  value |= static_cast<uint64_t>(*pos++) << 16;
52  value |= static_cast<uint64_t>(*pos++) << 24;
53  value |= static_cast<uint64_t>(*pos++) << 32;
54  value |= static_cast<uint64_t>(*pos++) << 40;
55  value |= static_cast<uint64_t>(*pos++) << 48;
56  value |= static_cast<uint64_t>(*pos++) << 56;
57  }
58  return value;
59  }
60 
61 
62  /**
63  * \brief Write a 64-bit value into a string
64  *
65  * \param serialized The string to which the value is appended.
66  *
67  * \param value The value which is appended to the string.
68  */
69  inline void write64(binary& serialized, uint64_t value)
70  {
71  // always big endian
72  serialized.push_back(value >> 56 & 0xff);
73  serialized.push_back(value >> 48 & 0xff);
74  serialized.push_back(value >> 40 & 0xff);
75  serialized.push_back(value >> 32 & 0xff);
76  serialized.push_back(value >> 24 & 0xff);
77  serialized.push_back(value >> 16 & 0xff);
78  serialized.push_back(value >> 8 & 0xff);
79  serialized.push_back(value >> 0 & 0xff);
80  }
81 
82 
83  inline uint32_t read32(binary::const_iterator& pos, bool big_endian)
84  {
85  uint32_t value;
86  if( big_endian )
87  {
88  value = *pos++ << 24;
89  value |= *pos++ << 16;
90  value |= *pos++ << 8;
91  value |= *pos++ << 0;
92  }
93  else
94  {
95  value = *pos++ << 0;
96  value |= *pos++ << 8;
97  value |= *pos++ << 16;
98  value |= *pos++ << 24;
99  }
100 
101  return value;
102  }
103 
104  /**
105  * \brief Write a 32-bit value into a string
106  *
107  * \param serialized The string to which the value is appended.
108  *
109  * \param value The value which is appended to the string.
110  */
111  inline void write32(binary& serialized, uint32_t value)
112  {
113  // always big endian
114  serialized.push_back(value >> 24 & 0xff);
115  serialized.push_back(value >> 16 & 0xff);
116  serialized.push_back(value >> 8 & 0xff);
117  serialized.push_back(value >> 0 & 0xff);
118  }
119 
120  inline uint16_t read16(binary::const_iterator& pos, bool big_endian)
121  {
122  uint16_t value = 0;
123  if( big_endian )
124  {
125  value |= *pos++ << 8;
126  value |= *pos++ << 0;
127  }
128  else
129  {
130  value = *pos++ << 0;
131  value |= *pos++ << 8;
132  }
133 
134  return value;
135  }
136 
137 
138  /**
139  * \brief Write a 16-bit value into a string
140  *
141  * \param serialized The string to which the value is appended.
142  *
143  * \param value The value which is appended to the string.
144  */
145  inline void write16(binary& serialized, uint16_t value)
146  {
147  // always big endian
148  serialized.push_back(value >> 8 & 0xff);
149  serialized.push_back(value >> 0 & 0xff);
150  }
151 }
152 
153 #endif