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
#include "Bytes.h"
8
#include "ParamNumber.h"
9
#include "ValueEncoder.h"
10

11
namespace TW::Ethereum::ABI {
12

13 1
void ParamByteArray::encodeBytes(const Data& bytes, Data& data) {
14 1
    ValueEncoder::encodeUInt256(uint256_t(bytes.size()), data);
15

16 1
    const auto count = bytes.size();
17 1
    const auto padding = ValueEncoder::padNeeded32(count);
18 1
    data.insert(data.end(), bytes.begin(), bytes.begin() + count);
19 1
    append(data, Data(padding));
20
}
21

22 1
bool ParamByteArray::decodeBytes(const Data& encoded, Data& decoded, size_t& offset_inout) {
23 1
    size_t origOffset = offset_inout;
24
    // read len
25 1
    uint256_t len256;
26 1
    if (!ABI::decode(encoded, len256, offset_inout)) {
27 0
        return false;
28
    }
29
    // check if length is in the size_t range
30 1
    size_t len = static_cast<size_t>(len256);
31 1
    if (len256 != uint256_t(len)) {
32 1
        return false;
33
    }
34
    // check if there is enough data
35 1
    if (encoded.size() < offset_inout + len) {
36 1
        return false;
37
    }
38
    // read data
39 1
    decoded = Data(encoded.begin() + offset_inout, encoded.begin() + offset_inout + len);
40 1
    offset_inout += len;
41
    // padding
42 1
    offset_inout = origOffset + ValueEncoder::paddedTo32(offset_inout - origOffset);
43 1
    return true;
44
}
45

46 1
void ParamByteArrayFix::encode(Data& data) const {
47 1
    const auto count = _bytes.size();
48 1
    const auto padding = ValueEncoder::padNeeded32(count);
49 1
    data.insert(data.end(), _bytes.begin(), _bytes.begin() + count);
50 1
    append(data, Data(padding));
51
}
52

53 1
bool ParamByteArrayFix::decodeBytesFix(const Data& encoded, size_t n, Data& decoded,
54
                                       size_t& offset_inout) {
55 1
    size_t origOffset = offset_inout;
56 1
    if (encoded.size() < offset_inout + n) {
57
        // not enough data
58 0
        return false;
59
    }
60 1
    if (decoded.size() < n) {
61 1
        append(decoded, Data(n - decoded.size()));
62
    }
63 1
    std::copy(encoded.begin() + offset_inout, encoded.begin() + (offset_inout + n),
64 1
              decoded.begin());
65 1
    offset_inout += n;
66
    // padding
67 1
    offset_inout = origOffset + ValueEncoder::paddedTo32(offset_inout - origOffset);
68 1
    return true;
69
}
70

71 1
void ParamString::encodeString(const std::string& decoded, Data& data) {
72 1
    auto bytes = Data(decoded.begin(), decoded.end());
73 1
    ParamByteArray::encodeBytes(bytes, data);
74
}
75

76 1
bool ParamString::decodeString(const Data& encoded, std::string& decoded, size_t& offset_inout) {
77 1
    Data decodedData;
78 1
    if (!ParamByteArray::decodeBytes(encoded, decodedData, offset_inout)) {
79 0
        return false;
80
    }
81 1
    decoded = std::string(decodedData.begin(), decodedData.end());
82 1
    return true;
83
}
84

85
} // namespace TW::Ethereum::ABI

Read our documentation on viewing source code .

Loading