1 /* Copyright 2015 OpenMarket Ltd
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include "olm/pickle.hh"
16 #include "olm/pickle.h"
18 std::uint8_t * olm::pickle(
23 for (unsigned i = 4; i--;) { *(--pos) = value; value >>= 8; }
28 std::uint8_t const * olm::unpickle(
29 std::uint8_t const * pos, std::uint8_t const * end,
33 if (!pos || end < pos + 4) return nullptr;
34 for (unsigned i = 4; i--;) { value <<= 8; value |= *(pos++); }
38 std::uint8_t * olm::pickle(
46 std::uint8_t const * olm::unpickle(
47 std::uint8_t const * pos, std::uint8_t const * end,
50 if (!pos || pos == end) return nullptr;
55 std::uint8_t * olm::pickle(
59 *(pos++) = value ? 1 : 0;
63 std::uint8_t const * olm::unpickle(
64 std::uint8_t const * pos, std::uint8_t const * end,
67 if (!pos || pos == end) return nullptr;
72 std::uint8_t * olm::pickle_bytes(
74 std::uint8_t const * bytes, std::size_t bytes_length
76 std::memcpy(pos, bytes, bytes_length);
77 return pos + bytes_length;
80 std::uint8_t const * olm::unpickle_bytes(
81 std::uint8_t const * pos, std::uint8_t const * end,
82 std::uint8_t * bytes, std::size_t bytes_length
84 if (!pos || end < pos + bytes_length) return nullptr;
85 std::memcpy(bytes, pos, bytes_length);
86 return pos + bytes_length;
90 std::size_t olm::pickle_length(
91 const _olm_curve25519_public_key & value
93 return sizeof(value.public_key);
97 std::uint8_t * olm::pickle(
99 const _olm_curve25519_public_key & value
101 pos = olm::pickle_bytes(
102 pos, value.public_key, sizeof(value.public_key)
108 std::uint8_t const * olm::unpickle(
109 std::uint8_t const * pos, std::uint8_t const * end,
110 _olm_curve25519_public_key & value
112 return olm::unpickle_bytes(
113 pos, end, value.public_key, sizeof(value.public_key)
118 std::size_t olm::pickle_length(
119 const _olm_curve25519_key_pair & value
121 return sizeof(value.public_key.public_key)
122 + sizeof(value.private_key.private_key);
126 std::uint8_t * olm::pickle(
128 const _olm_curve25519_key_pair & value
130 pos = olm::pickle_bytes(
131 pos, value.public_key.public_key,
132 sizeof(value.public_key.public_key)
134 pos = olm::pickle_bytes(
135 pos, value.private_key.private_key,
136 sizeof(value.private_key.private_key)
142 std::uint8_t const * olm::unpickle(
143 std::uint8_t const * pos, std::uint8_t const * end,
144 _olm_curve25519_key_pair & value
146 pos = olm::unpickle_bytes(
147 pos, end, value.public_key.public_key,
148 sizeof(value.public_key.public_key)
150 if (!pos) return nullptr;
152 pos = olm::unpickle_bytes(
153 pos, end, value.private_key.private_key,
154 sizeof(value.private_key.private_key)
156 if (!pos) return nullptr;
161 ////// pickle.h implementations
163 std::size_t _olm_pickle_ed25519_public_key_length(
164 const _olm_ed25519_public_key * value
166 return sizeof(value->public_key);
170 std::uint8_t * _olm_pickle_ed25519_public_key(
172 const _olm_ed25519_public_key *value
174 return olm::pickle_bytes(
175 pos, value->public_key, sizeof(value->public_key)
180 std::uint8_t const * _olm_unpickle_ed25519_public_key(
181 std::uint8_t const * pos, std::uint8_t const * end,
182 _olm_ed25519_public_key * value
184 return olm::unpickle_bytes(
185 pos, end, value->public_key, sizeof(value->public_key)
190 std::size_t _olm_pickle_ed25519_key_pair_length(
191 const _olm_ed25519_key_pair *value
193 return sizeof(value->public_key.public_key)
194 + sizeof(value->private_key.private_key);
198 std::uint8_t * _olm_pickle_ed25519_key_pair(
200 const _olm_ed25519_key_pair *value
202 pos = olm::pickle_bytes(
203 pos, value->public_key.public_key,
204 sizeof(value->public_key.public_key)
206 pos = olm::pickle_bytes(
207 pos, value->private_key.private_key,
208 sizeof(value->private_key.private_key)
214 std::uint8_t const * _olm_unpickle_ed25519_key_pair(
215 std::uint8_t const * pos, std::uint8_t const * end,
216 _olm_ed25519_key_pair *value
218 pos = olm::unpickle_bytes(
219 pos, end, value->public_key.public_key,
220 sizeof(value->public_key.public_key)
222 if (!pos) return nullptr;
224 pos = olm::unpickle_bytes(
225 pos, end, value->private_key.private_key,
226 sizeof(value->private_key.private_key)
228 if (!pos) return nullptr;
233 uint8_t * _olm_pickle_uint32(uint8_t * pos, uint32_t value) {
234 return olm::pickle(pos, value);
237 uint8_t const * _olm_unpickle_uint32(
238 uint8_t const * pos, uint8_t const * end,
241 return olm::unpickle(pos, end, *value);
244 uint8_t * _olm_pickle_uint8(uint8_t * pos, uint8_t value) {
245 return olm::pickle(pos, value);
248 uint8_t const * _olm_unpickle_uint8(
249 uint8_t const * pos, uint8_t const * end,
252 return olm::unpickle(pos, end, *value);
255 uint8_t * _olm_pickle_bool(uint8_t * pos, int value) {
256 return olm::pickle(pos, (bool)value);
259 uint8_t const * _olm_unpickle_bool(
260 uint8_t const * pos, uint8_t const * end,
263 return olm::unpickle(pos, end, *reinterpret_cast<bool *>(value));
266 uint8_t * _olm_pickle_bytes(uint8_t * pos, uint8_t const * bytes,
267 size_t bytes_length) {
268 return olm::pickle_bytes(pos, bytes, bytes_length);
271 uint8_t const * _olm_unpickle_bytes(uint8_t const * pos, uint8_t const * end,
272 uint8_t * bytes, size_t bytes_length) {
273 return olm::unpickle_bytes(pos, end, bytes, bytes_length);