xref: /llvm-project/llvm/lib/Support/DataExtractor.cpp (revision ba1c91564b69ce6d5cee6524fe6d46f280102ff4)
1 //===-- DataExtractor.cpp -------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Support/DataExtractor.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/Host.h"
13 #include "llvm/Support/SwapByteOrder.h"
14 using namespace llvm;
15 
16 template <typename T>
17 static T getU(uint32_t *offset_ptr, const DataExtractor *de,
18               bool isLittleEndian, const char *Data) {
19   T val = 0;
20   uint32_t offset = *offset_ptr;
21   if (de->isValidOffsetForDataOfSize(offset, sizeof(val))) {
22     std::memcpy(&val, &Data[offset], sizeof(val));
23     if (sys::IsLittleEndianHost != isLittleEndian)
24       sys::swapByteOrder(val);
25 
26     // Advance the offset
27     *offset_ptr += sizeof(val);
28   }
29   return val;
30 }
31 
32 template <typename T>
33 static T *getUs(uint32_t *offset_ptr, T *dst, uint32_t count,
34                 const DataExtractor *de, bool isLittleEndian, const char *Data){
35   uint32_t offset = *offset_ptr;
36 
37   if (count > 0 && de->isValidOffsetForDataOfSize(offset, sizeof(*dst)*count)) {
38     for (T *value_ptr = dst, *end = dst + count; value_ptr != end;
39         ++value_ptr, offset += sizeof(*dst))
40       *value_ptr = getU<T>(offset_ptr, de, isLittleEndian, Data);
41     // Advance the offset
42     *offset_ptr = offset;
43     // Return a non-NULL pointer to the converted data as an indicator of
44     // success
45     return dst;
46   }
47   return nullptr;
48 }
49 
50 uint8_t DataExtractor::getU8(uint32_t *offset_ptr) const {
51   return getU<uint8_t>(offset_ptr, this, IsLittleEndian, Data.data());
52 }
53 
54 uint8_t *
55 DataExtractor::getU8(uint32_t *offset_ptr, uint8_t *dst, uint32_t count) const {
56   return getUs<uint8_t>(offset_ptr, dst, count, this, IsLittleEndian,
57                        Data.data());
58 }
59 
60 
61 uint16_t DataExtractor::getU16(uint32_t *offset_ptr) const {
62   return getU<uint16_t>(offset_ptr, this, IsLittleEndian, Data.data());
63 }
64 
65 uint16_t *DataExtractor::getU16(uint32_t *offset_ptr, uint16_t *dst,
66                                 uint32_t count) const {
67   return getUs<uint16_t>(offset_ptr, dst, count, this, IsLittleEndian,
68                         Data.data());
69 }
70 
71 uint32_t DataExtractor::getU32(uint32_t *offset_ptr) const {
72   return getU<uint32_t>(offset_ptr, this, IsLittleEndian, Data.data());
73 }
74 
75 uint32_t *DataExtractor::getU32(uint32_t *offset_ptr, uint32_t *dst,
76                                 uint32_t count) const {
77   return getUs<uint32_t>(offset_ptr, dst, count, this, IsLittleEndian,
78                         Data.data());
79 }
80 
81 uint64_t DataExtractor::getU64(uint32_t *offset_ptr) const {
82   return getU<uint64_t>(offset_ptr, this, IsLittleEndian, Data.data());
83 }
84 
85 uint64_t *DataExtractor::getU64(uint32_t *offset_ptr, uint64_t *dst,
86                                 uint32_t count) const {
87   return getUs<uint64_t>(offset_ptr, dst, count, this, IsLittleEndian,
88                         Data.data());
89 }
90 
91 uint64_t
92 DataExtractor::getUnsigned(uint32_t *offset_ptr, uint32_t byte_size) const {
93   switch (byte_size) {
94   case 1:
95     return getU8(offset_ptr);
96   case 2:
97     return getU16(offset_ptr);
98   case 4:
99     return getU32(offset_ptr);
100   case 8:
101     return getU64(offset_ptr);
102   }
103   llvm_unreachable("getUnsigned unhandled case!");
104 }
105 
106 int64_t
107 DataExtractor::getSigned(uint32_t *offset_ptr, uint32_t byte_size) const {
108   switch (byte_size) {
109   case 1:
110     return (int8_t)getU8(offset_ptr);
111   case 2:
112     return (int16_t)getU16(offset_ptr);
113   case 4:
114     return (int32_t)getU32(offset_ptr);
115   case 8:
116     return (int64_t)getU64(offset_ptr);
117   }
118   llvm_unreachable("getSigned unhandled case!");
119 }
120 
121 const char *DataExtractor::getCStr(uint32_t *offset_ptr) const {
122   uint32_t offset = *offset_ptr;
123   StringRef::size_type pos = Data.find('\0', offset);
124   if (pos != StringRef::npos) {
125     *offset_ptr = pos + 1;
126     return Data.data() + offset;
127   }
128   return nullptr;
129 }
130 
131 StringRef DataExtractor::getCStrRef(uint32_t *OffsetPtr) const {
132   uint32_t Start = *OffsetPtr;
133   StringRef::size_type Pos = Data.find('\0', Start);
134   if (Pos != StringRef::npos) {
135     *OffsetPtr = Pos + 1;
136     return StringRef(Data.data() + Start, Pos - Start);
137   }
138   return StringRef();
139 }
140 
141 uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
142   uint64_t result = 0;
143   if (Data.empty())
144     return 0;
145 
146   unsigned shift = 0;
147   uint32_t offset = *offset_ptr;
148   uint8_t byte = 0;
149 
150   while (isValidOffset(offset)) {
151     byte = Data[offset++];
152     result |= uint64_t(byte & 0x7f) << shift;
153     shift += 7;
154     if ((byte & 0x80) == 0)
155       break;
156   }
157 
158   *offset_ptr = offset;
159   return result;
160 }
161 
162 int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
163   int64_t result = 0;
164   if (Data.empty())
165     return 0;
166 
167   unsigned shift = 0;
168   uint32_t offset = *offset_ptr;
169   uint8_t byte = 0;
170 
171   while (isValidOffset(offset)) {
172     byte = Data[offset++];
173     result |= uint64_t(byte & 0x7f) << shift;
174     shift += 7;
175     if ((byte & 0x80) == 0)
176       break;
177   }
178 
179   // Sign bit of byte is 2nd high order bit (0x40)
180   if (shift < 64 && (byte & 0x40))
181     result |= -(1ULL << shift);
182 
183   *offset_ptr = offset;
184   return result;
185 }
186