1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include <cstddef>
10
#include <cstdint>
11
#include <vector>
12

13
namespace TW::Ripple {
14

15
enum class FieldType;
16

17
/// Encodes a field type.
18 1
inline void encodeType(FieldType type, int key, std::vector<uint8_t>& data) {
19 1
    const auto typeValue = static_cast<int>(type);
20 1
    if (key <= 0xf) {
21 1
        data.push_back(static_cast<uint8_t>((typeValue << 4) | key));
22 1
    } else {
23 0
        data.push_back(static_cast<uint8_t>(typeValue << 4));
24 0
        data.push_back(static_cast<uint8_t>(key));
25
    }
26
}
27

28
/// Encodes a variable length.
29 1
inline void encodeVariableLength(size_t length, std::vector<uint8_t>& data) {
30 1
    if (length <= 192) {
31 1
        data.push_back(static_cast<unsigned char>(length));
32 1
    } else if (length <= 12480) {
33 0
        length -= 193;
34 0
        data.push_back(static_cast<unsigned char>(length >> 8));
35 0
        data.push_back(static_cast<unsigned char>(length & 0xff));
36 0
    } else if (length <= 918744) {
37 0
        length -= 12481;
38 0
        data.push_back(static_cast<unsigned char>(length >> 16));
39 0
        data.push_back(static_cast<unsigned char>((length >> 8) & 0xff));
40 0
        data.push_back(static_cast<unsigned char>(length & 0xff));
41
    }
42
}
43

44
/// Encodes a variable length bytes.
45 1
inline void encodeBytes(std::vector<uint8_t> bytes, std::vector<uint8_t>& data) {
46 1
    encodeVariableLength(bytes.size(), data);
47 1
    data.insert(data.end(), bytes.begin(), bytes.end());
48
}
49

50
} // namespace TW::Ripple

Read our documentation on viewing source code .

Loading