1/* Copyright 2015 OpenMarket Ltd
2 *
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
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include "olm/pickle.hh"
16#include "olm/pickle.h"
17
18std::uint8_t * olm::pickle(
19 std::uint8_t * pos,
20 std::uint32_t value
21) {
22 pos += 4;
23 for (unsigned i = 4; i--;) { *(--pos) = value; value >>= 8; }
24 return pos + 4;
25}
26
27
28std::uint8_t const * olm::unpickle(
29 std::uint8_t const * pos, std::uint8_t const * end,
30 std::uint32_t & value
31) {
32 value = 0;
33 if (!pos || end < pos + 4) return nullptr;
34 for (unsigned i = 4; i--;) { value <<= 8; value |= *(pos++); }
35 return pos;
36}
37
38std::uint8_t * olm::pickle(
39 std::uint8_t * pos,
40 std::uint8_t value
41) {
42 *(pos++) = value;
43 return pos;
44}
45
46std::uint8_t const * olm::unpickle(
47 std::uint8_t const * pos, std::uint8_t const * end,
48 std::uint8_t & value
49) {
50 if (!pos || pos == end) return nullptr;
51 value = *(pos++);
52 return pos;
53}
54
55std::uint8_t * olm::pickle(
56 std::uint8_t * pos,
57 bool value
58) {
59 *(pos++) = value ? 1 : 0;
60 return pos;
61}
62
63std::uint8_t const * olm::unpickle(
64 std::uint8_t const * pos, std::uint8_t const * end,
65 bool & value
66) {
67 if (!pos || pos == end) return nullptr;
68 value = *(pos++);
69 return pos;
70}
71
72std::uint8_t * olm::pickle_bytes(
73 std::uint8_t * pos,
74 std::uint8_t const * bytes, std::size_t bytes_length
75) {
76 std::memcpy(dest: pos, src: bytes, n: bytes_length);
77 return pos + bytes_length;
78}
79
80std::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
83) {
84 if (!pos || end < pos + bytes_length) return nullptr;
85 std::memcpy(dest: bytes, src: pos, n: bytes_length);
86 return pos + bytes_length;
87}
88
89
90std::size_t olm::pickle_length(
91 const _olm_curve25519_public_key & value
92) {
93 return sizeof(value.public_key);
94}
95
96
97std::uint8_t * olm::pickle(
98 std::uint8_t * pos,
99 const _olm_curve25519_public_key & value
100) {
101 pos = olm::pickle_bytes(
102 pos, bytes: value.public_key, bytes_length: sizeof(value.public_key)
103 );
104 return pos;
105}
106
107
108std::uint8_t const * olm::unpickle(
109 std::uint8_t const * pos, std::uint8_t const * end,
110 _olm_curve25519_public_key & value
111) {
112 return olm::unpickle_bytes(
113 pos, end, bytes: value.public_key, bytes_length: sizeof(value.public_key)
114 );
115}
116
117
118std::size_t olm::pickle_length(
119 const _olm_curve25519_key_pair & value
120) {
121 return sizeof(value.public_key.public_key)
122 + sizeof(value.private_key.private_key);
123}
124
125
126std::uint8_t * olm::pickle(
127 std::uint8_t * pos,
128 const _olm_curve25519_key_pair & value
129) {
130 pos = olm::pickle_bytes(
131 pos, bytes: value.public_key.public_key,
132 bytes_length: sizeof(value.public_key.public_key)
133 );
134 pos = olm::pickle_bytes(
135 pos, bytes: value.private_key.private_key,
136 bytes_length: sizeof(value.private_key.private_key)
137 );
138 return pos;
139}
140
141
142std::uint8_t const * olm::unpickle(
143 std::uint8_t const * pos, std::uint8_t const * end,
144 _olm_curve25519_key_pair & value
145) {
146 pos = olm::unpickle_bytes(
147 pos, end, bytes: value.public_key.public_key,
148 bytes_length: sizeof(value.public_key.public_key)
149 );
150 if (!pos) return nullptr;
151
152 pos = olm::unpickle_bytes(
153 pos, end, bytes: value.private_key.private_key,
154 bytes_length: sizeof(value.private_key.private_key)
155 );
156 if (!pos) return nullptr;
157
158 return pos;
159}
160
161////// pickle.h implementations
162
163std::size_t _olm_pickle_ed25519_public_key_length(
164 const _olm_ed25519_public_key * value
165) {
166 return sizeof(value->public_key);
167}
168
169
170std::uint8_t * _olm_pickle_ed25519_public_key(
171 std::uint8_t * pos,
172 const _olm_ed25519_public_key *value
173) {
174 return olm::pickle_bytes(
175 pos, bytes: value->public_key, bytes_length: sizeof(value->public_key)
176 );
177}
178
179
180std::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
183) {
184 return olm::unpickle_bytes(
185 pos, end, bytes: value->public_key, bytes_length: sizeof(value->public_key)
186 );
187}
188
189
190std::size_t _olm_pickle_ed25519_key_pair_length(
191 const _olm_ed25519_key_pair *value
192) {
193 return sizeof(value->public_key.public_key)
194 + sizeof(value->private_key.private_key);
195}
196
197
198std::uint8_t * _olm_pickle_ed25519_key_pair(
199 std::uint8_t * pos,
200 const _olm_ed25519_key_pair *value
201) {
202 pos = olm::pickle_bytes(
203 pos, bytes: value->public_key.public_key,
204 bytes_length: sizeof(value->public_key.public_key)
205 );
206 pos = olm::pickle_bytes(
207 pos, bytes: value->private_key.private_key,
208 bytes_length: sizeof(value->private_key.private_key)
209 );
210 return pos;
211}
212
213
214std::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
217) {
218 pos = olm::unpickle_bytes(
219 pos, end, bytes: value->public_key.public_key,
220 bytes_length: sizeof(value->public_key.public_key)
221 );
222 if (!pos) return nullptr;
223
224 pos = olm::unpickle_bytes(
225 pos, end, bytes: value->private_key.private_key,
226 bytes_length: sizeof(value->private_key.private_key)
227 );
228 if (!pos) return nullptr;
229
230 return pos;
231}
232
233uint8_t * _olm_pickle_uint32(uint8_t * pos, uint32_t value) {
234 return olm::pickle(pos, value);
235}
236
237uint8_t const * _olm_unpickle_uint32(
238 uint8_t const * pos, uint8_t const * end,
239 uint32_t *value
240) {
241 return olm::unpickle(pos, end, value&: *value);
242}
243
244uint8_t * _olm_pickle_uint8(uint8_t * pos, uint8_t value) {
245 return olm::pickle(pos, value);
246}
247
248uint8_t const * _olm_unpickle_uint8(
249 uint8_t const * pos, uint8_t const * end,
250 uint8_t *value
251) {
252 return olm::unpickle(pos, end, value&: *value);
253}
254
255uint8_t * _olm_pickle_bool(uint8_t * pos, int value) {
256 return olm::pickle(pos, value: (bool)value);
257}
258
259uint8_t const * _olm_unpickle_bool(
260 uint8_t const * pos, uint8_t const * end,
261 int *value
262) {
263 return olm::unpickle(pos, end, value&: *reinterpret_cast<bool *>(value));
264}
265
266uint8_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);
269}
270
271uint8_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);
274}
275