2015-06-22 18:50:56 +02:00
|
|
|
/* Copyright 2015 OpenMarket Ltd
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-06-27 01:15:23 +02:00
|
|
|
#ifndef OLM_PICKLE_HH_
|
|
|
|
#define OLM_PICKLE_HH_
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
#include "olm/list.hh"
|
2016-09-02 16:35:04 +02:00
|
|
|
#include "olm/crypto.h"
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
namespace olm {
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
inline std::size_t pickle_length(
|
2015-06-12 17:15:37 +02:00
|
|
|
const std::uint32_t & value
|
|
|
|
) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
std::uint8_t * pickle(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t * pos,
|
|
|
|
std::uint32_t value
|
2016-04-26 19:10:13 +02:00
|
|
|
);
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
std::uint8_t const * unpickle(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
|
|
|
std::uint32_t & value
|
2016-04-26 19:10:13 +02:00
|
|
|
);
|
|
|
|
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
inline std::size_t pickle_length(
|
2015-06-12 17:15:37 +02:00
|
|
|
const bool & value
|
|
|
|
) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
std::uint8_t * pickle(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t * pos,
|
|
|
|
bool value
|
2016-04-26 19:10:13 +02:00
|
|
|
);
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
std::uint8_t const * unpickle(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
|
|
|
bool & value
|
2016-04-26 19:10:13 +02:00
|
|
|
);
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
template<typename T, std::size_t max_size>
|
|
|
|
std::size_t pickle_length(
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::List<T, max_size> const & list
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
|
|
|
std::size_t length = pickle_length(std::uint32_t(list.size()));
|
|
|
|
for (auto const & value : list) {
|
|
|
|
length += pickle_length(value);
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, std::size_t max_size>
|
|
|
|
std::uint8_t * pickle(
|
|
|
|
std::uint8_t * pos,
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::List<T, max_size> const & list
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
|
|
|
pos = pickle(pos, std::uint32_t(list.size()));
|
|
|
|
for (auto const & value : list) {
|
|
|
|
pos = pickle(pos, value);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, std::size_t max_size>
|
|
|
|
std::uint8_t const * unpickle(
|
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
2015-06-27 01:15:23 +02:00
|
|
|
olm::List<T, max_size> & list
|
2015-06-12 17:15:37 +02:00
|
|
|
) {
|
|
|
|
std::uint32_t size;
|
|
|
|
pos = unpickle(pos, end, size);
|
2015-08-11 14:14:06 +02:00
|
|
|
while (size-- && pos != end) {
|
2015-06-12 17:15:37 +02:00
|
|
|
T * value = list.insert(list.end());
|
|
|
|
pos = unpickle(pos, end, *value);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
std::uint8_t * pickle_bytes(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t * pos,
|
|
|
|
std::uint8_t const * bytes, std::size_t bytes_length
|
2016-04-26 19:10:13 +02:00
|
|
|
);
|
2015-06-12 17:15:37 +02:00
|
|
|
|
2016-04-26 19:10:13 +02:00
|
|
|
std::uint8_t const * unpickle_bytes(
|
2015-06-12 17:15:37 +02:00
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
|
|
|
std::uint8_t * bytes, std::size_t bytes_length
|
2016-04-26 19:10:13 +02:00
|
|
|
);
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
std::size_t pickle_length(
|
2016-09-02 16:13:24 +02:00
|
|
|
const _olm_curve25519_public_key & value
|
2015-06-12 17:15:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
std::uint8_t * pickle(
|
|
|
|
std::uint8_t * pos,
|
2016-09-02 16:13:24 +02:00
|
|
|
const _olm_curve25519_public_key & value
|
2015-06-12 17:15:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
std::uint8_t const * unpickle(
|
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_public_key & value
|
2015-06-12 17:15:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t pickle_length(
|
2016-09-02 16:13:24 +02:00
|
|
|
const _olm_curve25519_key_pair & value
|
2015-06-12 17:15:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
std::uint8_t * pickle(
|
|
|
|
std::uint8_t * pos,
|
2016-09-02 16:13:24 +02:00
|
|
|
const _olm_curve25519_key_pair & value
|
2015-06-12 17:15:37 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
std::uint8_t const * unpickle(
|
|
|
|
std::uint8_t const * pos, std::uint8_t const * end,
|
2016-09-02 16:13:24 +02:00
|
|
|
_olm_curve25519_key_pair & value
|
2015-06-12 17:15:37 +02:00
|
|
|
);
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
} // namespace olm
|
2015-06-12 17:15:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-27 01:15:23 +02:00
|
|
|
#endif /* OLM_PICKLE_HH */
|