1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// slang-hex-dump-util.cpp
#include "slang-hex-dump-util.h"
#include "slang-common.h"
#include "slang-string-util.h"
#include "slang-writer.h"
#include "../../slang-com-helper.h"
#include "slang-hash.h"
namespace Slang
{
static const UnownedStringSlice s_start = UnownedStringSlice::fromLiteral("--START--");
static const UnownedStringSlice s_end = UnownedStringSlice::fromLiteral("--END--");
static const char s_hex[] = "0123456789abcdef";
/* static */SlangResult HexDumpUtil::dumpWithMarkers(const List<uint8_t>& data, int maxBytesPerLine, ISlangWriter* writer)
{
return dumpWithMarkers(data.getBuffer(), data.getCount(), maxBytesPerLine, writer);
}
/* static */SlangResult HexDumpUtil::dumpWithMarkers(const uint8_t* data, size_t dataCount, int maxBytesPerLine, ISlangWriter* writer)
{
WriterHelper helper(writer);
SLANG_RETURN_ON_FAIL(helper.write(s_start.begin(), s_start.getLength()));
SLANG_RETURN_ON_FAIL(helper.print(" %zu", dataCount));
const HashCode32 hash = getStableHashCode32((const char*)data, dataCount);
SLANG_RETURN_ON_FAIL(helper.print(" %d\n", int(hash) ));
SLANG_RETURN_ON_FAIL(dump(data, dataCount, maxBytesPerLine, writer));
SLANG_RETURN_ON_FAIL(helper.write(s_end.begin(), s_end.getLength()));
SLANG_RETURN_ON_FAIL(helper.put("\n"));
return SLANG_OK;
}
/* static */void HexDumpUtil::dump(uint32_t value, ISlangWriter* writer)
{
char c[9];
for (int i = 0; i < 8; ++i)
{
c[i] = s_hex[value >> 28];
value <<= 4;
}
writer->write(c, 8);
}
/* static */SlangResult HexDumpUtil::dump(const List<uint8_t>& data, int maxBytesPerLine, ISlangWriter* writer)
{
return dump(data.getBuffer(), data.getCount(), maxBytesPerLine, writer);
}
/* static */SlangResult HexDumpUtil::dump(const uint8_t* data, size_t dataCount, int maxBytesPerLine, ISlangWriter* writer)
{
int maxCharsPerLine = 2 * maxBytesPerLine + 1 + maxBytesPerLine + 1;
const uint8_t* cur = data;
const uint8_t* end = data + dataCount;
while (cur < end)
{
size_t count = size_t(end - cur);
count = (count > size_t(maxBytesPerLine)) ? size_t(maxBytesPerLine) : count;
char* startDst = writer->beginAppendBuffer(maxCharsPerLine);
char* dst = startDst;
for (size_t i = 0; i < count; ++i)
{
uint8_t byte = cur[i];
*dst++ = s_hex[byte >> 4];
*dst++ = s_hex[byte & 0xf];
}
// If not a complete line write spaces
for (size_t i = count; i < size_t(maxBytesPerLine); ++i)
{
*dst++ = ' ';
*dst++ = ' ';
}
*dst++ = ' ';
for (size_t i = 0; i < count; ++i)
{
char c = char(cur[i]);
if ((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= 32 && (c & 0x80) == 0))
{
}
else
{
c = '.';
}
*dst++ = c;
}
*dst++ = '\n';
SLANG_ASSERT(dst <= startDst + maxCharsPerLine);
SLANG_RETURN_ON_FAIL(writer->endAppendBuffer(startDst, size_t(dst - startDst)));
cur += count;
}
return SLANG_OK;
}
static int _parseHexDigit(char c)
{
if (c >= '0' && c <= '9')
{
return c -'0';
}
else if (c >= 'a' && c <= 'f')
{
return c - 'a' + 10;
}
else if (c >= 'A' && c <= 'F')
{
return c - 'A' + 10;
}
return -1;
}
/* static */SlangResult HexDumpUtil::parse(const UnownedStringSlice& lines, List<uint8_t>& outBytes)
{
outBytes.clear();
LineParser lineParser(lines);
for (const auto& line : lineParser)
{
const char* cur = line.begin();
const char* end = line.end();
while(cur + 2 <= end)
{
const char c = cur[0];
if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
{
// Skip to next line
break;
}
const int hi = _parseHexDigit(c);
const int lo = _parseHexDigit(cur[1]);
cur += 2;
if (hi < 0 || lo < 0)
{
return SLANG_FAIL;
}
outBytes.add(uint8_t((hi << 4) | lo));
}
}
return SLANG_OK;
}
static SlangResult _findLine(const UnownedStringSlice& find, UnownedStringSlice& ioRemaining, UnownedStringSlice& outLine)
{
// Find the start line
UnownedStringSlice line;
while (StringUtil::extractLine(ioRemaining, line))
{
if (line.startsWith(find))
{
outLine = line;
return SLANG_OK;
}
}
return SLANG_FAIL;
}
/* static */SlangResult HexDumpUtil::findStartAndEndLines(const UnownedStringSlice& lines, UnownedStringSlice& outStart, UnownedStringSlice& outEnd)
{
UnownedStringSlice remaining(lines);
SLANG_RETURN_ON_FAIL(_findLine(s_start, remaining, outStart));
SLANG_RETURN_ON_FAIL(_findLine(s_end, remaining, outEnd));
return SLANG_OK;
}
/* static */SlangResult HexDumpUtil::parseWithMarkers(const UnownedStringSlice& lines, List<uint8_t>& outBytes)
{
UnownedStringSlice startLine, endLine;
SLANG_RETURN_ON_FAIL(findStartAndEndLines(lines, startLine, endLine));
HashCode32 hash;
size_t size;
{
// Get the size and the hash
List<UnownedStringSlice> slices;
StringUtil::split(startLine, ' ', slices);
if (slices.getCount() != 3)
{
return SLANG_FAIL;
}
// Extract the size
size = StringToInt(String(slices[1]));
hash = HashCode32(StringToInt(String(slices[2])));
}
SLANG_RETURN_ON_FAIL(parse(UnownedStringSlice(startLine.end(), endLine.begin()), outBytes));
// Calc the hash
const HashCode32 readHash = getStableHashCode32((const char*)outBytes.begin(), outBytes.getCount());
if (readHash != hash || size_t(outBytes.getCount()) != size)
{
return SLANG_FAIL;
}
return SLANG_OK;
}
}
|