aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/base64.cpp6
-rw-r--r--src/message.cpp4
-rw-r--r--src/pickle.cpp54
3 files changed, 62 insertions, 2 deletions
diff --git a/src/base64.cpp b/src/base64.cpp
index bf8492e..66f512b 100644
--- a/src/base64.cpp
+++ b/src/base64.cpp
@@ -45,6 +45,12 @@ static const std::uint8_t DECODE_BASE64[128] = {
} // namespace
+std::size_t olm::encode_base64_length(
+ std::size_t input_length
+) {
+ return 4 * ((input_length + 2) / 3) + (input_length + 2) % 3 - 2;
+}
+
std::uint8_t * olm::encode_base64(
std::uint8_t const * input, std::size_t input_length,
std::uint8_t * output
diff --git a/src/message.cpp b/src/message.cpp
index 30abd9c..23ec823 100644
--- a/src/message.cpp
+++ b/src/message.cpp
@@ -136,7 +136,7 @@ static std::uint8_t const * decode(
std::uint8_t const * len_start = pos;
pos = varint_skip(pos, end);
std::size_t len = varint_decode<std::size_t>(len_start, pos);
- if (len > end - pos) return end;
+ if (len + pos > end) return end;
value = pos;
value_length = len;
pos += len;
@@ -157,7 +157,7 @@ static std::uint8_t const * skip_unknown(
std::uint8_t const * len_start = pos;
pos = varint_skip(pos, end);
std::size_t len = varint_decode<std::size_t>(len_start, pos);
- if (len > end - pos) return end;
+ if (len + pos > end) return end;
pos += len;
} else {
return end;
diff --git a/src/pickle.cpp b/src/pickle.cpp
index 25580d2..00f7cd4 100644
--- a/src/pickle.cpp
+++ b/src/pickle.cpp
@@ -14,6 +14,60 @@
*/
#include "olm/pickle.hh"
+std::uint8_t * olm::pickle(
+ std::uint8_t * pos,
+ std::uint32_t value
+) {
+ pos += 4;
+ for (unsigned i = 4; i--;) { *(--pos) = value; value >>= 8; }
+ return pos + 4;
+}
+
+
+std::uint8_t const * olm::unpickle(
+ std::uint8_t const * pos, std::uint8_t const * end,
+ std::uint32_t & value
+) {
+ value = 0;
+ if (end < pos + 4) return end;
+ for (unsigned i = 4; i--;) { value <<= 8; value |= *(pos++); }
+ return pos;
+}
+
+std::uint8_t * olm::pickle(
+ std::uint8_t * pos,
+ bool value
+) {
+ *(pos++) = value ? 1 : 0;
+ return pos;
+}
+
+std::uint8_t const * olm::unpickle(
+ std::uint8_t const * pos, std::uint8_t const * end,
+ bool & value
+) {
+ if (pos == end) return end;
+ value = *(pos++);
+ return pos;
+}
+
+std::uint8_t * olm::pickle_bytes(
+ std::uint8_t * pos,
+ std::uint8_t const * bytes, std::size_t bytes_length
+) {
+ std::memcpy(pos, bytes, bytes_length);
+ return pos + bytes_length;
+}
+
+std::uint8_t const * olm::unpickle_bytes(
+ std::uint8_t const * pos, std::uint8_t const * end,
+ std::uint8_t * bytes, std::size_t bytes_length
+) {
+ if (end < pos + bytes_length) return end;
+ std::memcpy(bytes, pos, bytes_length);
+ return pos + bytes_length;
+}
+
std::size_t olm::pickle_length(
const olm::Curve25519PublicKey & value