ledger-core
BytesWriter.h
1 /*
2  *
3  * BytesWriter
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 22/09/2016.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 
32 #pragma once
33 
34 #include <cstdio>
35 #include <vector>
36 #include "../utils/endian.h"
37 #include "../math/BigInt.h"
38 
39 namespace ledger {
40  namespace core {
44  class BytesWriter {
45 
46  public:
47  BytesWriter(size_t size);
48  BytesWriter() {};
49 
55  inline BytesWriter& writeByte(uint8_t byte) {
56  _bytes.push_back(byte);
57  return *this;
58  }
59 
65  template<typename T> BytesWriter& writeBeValue(const T value) {
66  auto ptr = reinterpret_cast<const uint8_t *>(&value);
68  auto i = sizeof(value);
69  do {
70  i -= 1;
71  writeByte(ptr[i]);
72  } while (i > 0);
73  } else {
74  for (auto i = 0; i < sizeof(value); i++) {
75  writeByte(ptr[i]);
76  }
77  }
78  return *this;
79  }
80 
86  template<typename T> BytesWriter& writeLeValue(const T value) {
87  auto ptr = reinterpret_cast<const uint8_t *>(&value);
89  auto i = sizeof(value);
90  do {
91  i -= 1;
92  writeByte(ptr[i]);
93  } while (i > 0);
94  } else {
95  for (auto i = 0; i < sizeof(value); i++) {
96  writeByte(ptr[i]);
97  }
98  }
99  return *this;
100  }
101 
107  BytesWriter& writeByteArray(const std::vector<uint8_t>& data);
108 
114  BytesWriter &writeLeByteArray(const std::vector<uint8_t> &data);
115 
121  BytesWriter& writeBeBigInt(const BigInt& i);
127  BytesWriter& writeLeBigInt(const BigInt& i);
133  BytesWriter& writeString(const std::string& str);
139  BytesWriter& writeVarInt(uint64_t i);
145  BytesWriter& writeVarString(const std::string& str);
146 
151  std::vector<uint8_t> toByteArray() const;
152 
153  private:
154  std::vector<uint8_t> _bytes;
155  };
156  }
157 }
bool isSystemBigEndian()
Definition: endian.cpp:45
std::vector< uint8_t > toByteArray() const
Definition: BytesWriter.cpp:42
BytesWriter & writeVarString(const std::string &str)
Definition: BytesWriter.cpp:90
BytesWriter & writeLeBigInt(const BigInt &i)
Definition: BytesWriter.cpp:65
BytesWriter & writeLeValue(const T value)
Definition: BytesWriter.h:86
BytesWriter & writeVarInt(uint64_t i)
Definition: BytesWriter.cpp:78
BytesWriter & writeBeValue(const T value)
Definition: BytesWriter.h:65
Definition: BytesWriter.h:44
BytesWriter & writeString(const std::string &str)
Definition: BytesWriter.cpp:71
BytesWriter & writeByteArray(const std::vector< uint8_t > &data)
Definition: BytesWriter.cpp:46
BytesWriter & writeBeBigInt(const BigInt &i)
Definition: BytesWriter.cpp:60
BytesWriter & writeLeByteArray(const std::vector< uint8_t > &data)
Definition: BytesWriter.cpp:53
Definition: Account.cpp:8
BytesWriter & writeByte(uint8_t byte)
Definition: BytesWriter.h:55
Definition: BigInt.h:56