1 //===-- DataEncoder.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/DataEncoder.h"
10 
11 #include "lldb/Utility/DataBuffer.h"
12 #include "lldb/Utility/Endian.h"
13 
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/ErrorHandling.h"
16 
17 #include <cstddef>
18 
19 #include <cstring>
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 using namespace llvm::support::endian;
24 
25 // Default constructor.
26 DataEncoder::DataEncoder()
27     : m_byte_order(endian::InlHostByteOrder()), m_addr_size(sizeof(void *)),
28       m_data_sp() {}
29 
30 // This constructor allows us to use data that is owned by someone else. The
31 // data must stay around as long as this object is valid.
32 DataEncoder::DataEncoder(void *data, uint32_t length, ByteOrder endian,
33                          uint8_t addr_size)
34     : m_start(static_cast<uint8_t *>(data)),
35       m_end(static_cast<uint8_t *>(data) + length), m_byte_order(endian),
36       m_addr_size(addr_size), m_data_sp() {}
37 
38 // Make a shared pointer reference to the shared data in "data_sp" and set the
39 // endian swapping setting to "swap", and the address size to "addr_size". The
40 // shared data reference will ensure the data lives as long as any DataEncoder
41 // objects exist that have a reference to this data.
42 DataEncoder::DataEncoder(const DataBufferSP &data_sp, ByteOrder endian,
43                          uint8_t addr_size)
44     : m_start(nullptr), m_end(nullptr), m_byte_order(endian),
45       m_addr_size(addr_size), m_data_sp() {
46   SetData(data_sp);
47 }
48 
49 DataEncoder::~DataEncoder() = default;
50 
51 // Clears the object contents back to a default invalid state, and release any
52 // references to shared data that this object may contain.
53 void DataEncoder::Clear() {
54   m_start = nullptr;
55   m_end = nullptr;
56   m_byte_order = endian::InlHostByteOrder();
57   m_addr_size = sizeof(void *);
58   m_data_sp.reset();
59 }
60 
61 // Assign the data for this object to be a subrange of the shared data in
62 // "data_sp" starting "data_offset" bytes into "data_sp" and ending
63 // "data_length" bytes later. If "data_offset" is not a valid offset into
64 // "data_sp", then this object will contain no bytes. If "data_offset" is
65 // within "data_sp" yet "data_length" is too large, the length will be capped
66 // at the number of bytes remaining in "data_sp". A ref counted pointer to the
67 // data in "data_sp" will be made in this object IF the number of bytes this
68 // object refers to in greater than zero (if at least one byte was available
69 // starting at "data_offset") to ensure the data stays around as long as it is
70 // needed. The address size and endian swap settings will remain unchanged from
71 // their current settings.
72 uint32_t DataEncoder::SetData(const DataBufferSP &data_sp, uint32_t data_offset,
73                               uint32_t data_length) {
74   m_start = m_end = nullptr;
75 
76   if (data_length > 0) {
77     m_data_sp = data_sp;
78     if (data_sp) {
79       const size_t data_size = data_sp->GetByteSize();
80       if (data_offset < data_size) {
81         m_start = data_sp->GetBytes() + data_offset;
82         const size_t bytes_left = data_size - data_offset;
83         // Cap the length of we asked for too many
84         if (data_length <= bytes_left)
85           m_end = m_start + data_length; // We got all the bytes we wanted
86         else
87           m_end = m_start + bytes_left; // Not all the bytes requested were
88                                         // available in the shared data
89       }
90     }
91   }
92 
93   uint32_t new_size = GetByteSize();
94 
95   // Don't hold a shared pointer to the data buffer if we don't share any valid
96   // bytes in the shared buffer.
97   if (new_size == 0)
98     m_data_sp.reset();
99 
100   return new_size;
101 }
102 
103 // Extract a single unsigned char from the binary data and update the offset
104 // pointed to by "offset_ptr".
105 //
106 // RETURNS the byte that was extracted, or zero on failure.
107 uint32_t DataEncoder::PutU8(uint32_t offset, uint8_t value) {
108   if (ValidOffset(offset)) {
109     m_start[offset] = value;
110     return offset + 1;
111   }
112   return UINT32_MAX;
113 }
114 
115 uint32_t DataEncoder::PutU16(uint32_t offset, uint16_t value) {
116   if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
117     if (m_byte_order != endian::InlHostByteOrder())
118       write16be(m_start + offset, value);
119     else
120       write16le(m_start + offset, value);
121 
122     return offset + sizeof(value);
123   }
124   return UINT32_MAX;
125 }
126 
127 uint32_t DataEncoder::PutU32(uint32_t offset, uint32_t value) {
128   if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
129     if (m_byte_order != endian::InlHostByteOrder())
130       write32be(m_start + offset, value);
131     else
132       write32le(m_start + offset, value);
133 
134     return offset + sizeof(value);
135   }
136   return UINT32_MAX;
137 }
138 
139 uint32_t DataEncoder::PutU64(uint32_t offset, uint64_t value) {
140   if (ValidOffsetForDataOfSize(offset, sizeof(value))) {
141     if (m_byte_order != endian::InlHostByteOrder())
142       write64be(m_start + offset, value);
143     else
144       write64le(m_start + offset, value);
145 
146     return offset + sizeof(value);
147   }
148   return UINT32_MAX;
149 }
150 
151 uint32_t DataEncoder::PutUnsigned(uint32_t offset, uint32_t byte_size,
152                                   uint64_t value) {
153   switch (byte_size) {
154   case 1:
155     return PutU8(offset, value);
156   case 2:
157     return PutU16(offset, value);
158   case 4:
159     return PutU32(offset, value);
160   case 8:
161     return PutU64(offset, value);
162   default:
163     llvm_unreachable("GetMax64 unhandled case!");
164   }
165   return UINT32_MAX;
166 }
167 
168 uint32_t DataEncoder::PutData(uint32_t offset, const void *src,
169                               uint32_t src_len) {
170   if (src == nullptr || src_len == 0)
171     return offset;
172 
173   if (ValidOffsetForDataOfSize(offset, src_len)) {
174     memcpy(m_start + offset, src, src_len);
175     return offset + src_len;
176   }
177   return UINT32_MAX;
178 }
179 
180 uint32_t DataEncoder::PutAddress(uint32_t offset, lldb::addr_t addr) {
181   return PutUnsigned(offset, m_addr_size, addr);
182 }
183 
184 uint32_t DataEncoder::PutCString(uint32_t offset, const char *cstr) {
185   if (cstr != nullptr)
186     return PutData(offset, cstr, strlen(cstr) + 1);
187   return UINT32_MAX;
188 }
189