-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdumper.cpp
More file actions
457 lines (429 loc) · 12 KB
/
Copy pathdumper.cpp
File metadata and controls
457 lines (429 loc) · 12 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#define __USE_MINGW_ANSI_STDIO 1
#include <string.h>
#include <stdio.h>
#include <cstdlib>
#include "dumper.h"
#include "format.h"
#include "reader.h"
#include "record.h"
namespace
{
static constexpr char alphabet[] = "0123456789ABCDEF";
}
HexDumper::HexDumper()
{
}
HexDumper::~HexDumper()
{
if (pos != 0)
{
printf("\t\t%s\n", string);
pos = 0;
}
}
void HexDumper::clearString()
{
memset(string + 2, ' ', 16 * 3 - 1);
memset(string + 2 + 16 * 3 + 2, ' ', 16);
pos = 0;
}
void HexDumper::dumpIt(const unsigned char* buffer, size_t length)
{
while (length > 0)
{
string[2 + pos * 3] = alphabet[*buffer >> 4];
string[2 + pos * 3 + 1] = alphabet[*buffer & 0x0F];
string[2 + 16 * 3 + 2 + pos] = *buffer >= 0x20 && *buffer < 0x7f ? *buffer : '.';
--length;
++buffer;
if (++pos == 16)
{
printf("\t\t%s\n", string);
clearString();
}
}
}
void HexDumper::flush()
{
if (pos != 0)
{
printf("\t\t%s\n", string);
clearString();
pos = 0;
}
}
void CompactHexDumper::dumpIt(const unsigned char* buffer, size_t length)
{
char string[1024];
char* p = string;
while (length > 0)
{
*p++ = alphabet[*buffer >> 4];
*p++ = alphabet[*buffer & 0x0F];
if (p >= string + sizeof(string))
{
printf("%.1024s", string);
p = string;
}
++buffer;
--length;
}
if (p > string)
{
printf("%.*s", static_cast<int>(p - string), string);
}
}
#include <map>
#include <unordered_map>
#include <tuple>
#include <vector>
#include "fbinterface.h"
#include "status.h"
namespace
{
typedef std::pair<std::string, std::string> TableKey;
typedef std::vector<Ods::Descriptor13> Format;
typedef std::map<size_t, Format> FormatList;
struct TableHash
{
std::size_t operator()(const TableKey& value) const
{
return std::hash<TableKey::first_type>{}(value.first) + std::hash<TableKey::second_type>{}(value.second);
}
};
std::unordered_map<TableKey, FormatList, TableHash> cache;
void dumpData(const Reader& file, const Format& format, const unsigned char* data, size_t index)
{
static Firebird::IUtil* util = master->getUtilInterface();
if (data[index / 8] & 1 << (index % 8))
{
printf("<NULL>\n");
}
else
{
const Ods::Descriptor13& desc = format[index];
/* This case is impossible because length of format is formed for longest field and is chosen by length
if (desc.dsc_offset + desc.dsc_length > dataLength)
{
fprintf(stderr, "Data for field %zu run out of buffer: %u of %zu\n", i, desc.dsc_offset + desc.dsc_length, dataLength);
return;
}
*/
const unsigned char* v = data + desc.dsc_offset;
switch (desc.dsc_dtype)
{
case dtype_text:
{
if (desc.dsc_sub_type == fb_text_subtype_binary)
{
HexDumper dumper;
dumper.dumpIt(v, desc.dsc_length);
}
else
{
printf("'%.*s'\n", desc.dsc_length, v);
}
break;
}
case dtype_varying:
{
uint16_t len = file.gatherInt16(v);
if (len > desc.dsc_length - offsetof(paramvary, vary_string))
{
fprintf(stderr, "Structure error: actual length of varying string %u is bigger than declared field length %u\n", len, desc.dsc_length - 2);
len = desc.dsc_length - offsetof(paramvary, vary_string);
}
if (desc.dsc_sub_type == fb_text_subtype_binary)
{
HexDumper dumper;
dumper.dumpIt(v + offsetof(paramvary, vary_string), len);
}
else
{
printf("'%.*s'\n", len, v + offsetof(paramvary, vary_string));
}
break;
}
case dtype_short:
{
printf("%s\n", formatDecimal(desc.dsc_scale, file.gatherInt16(v)).c_str());
break;
}
case dtype_long:
{
printf("%s\n", formatDecimal(desc.dsc_scale, file.gatherInt32(v)).c_str());
break;
}
case dtype_sql_date:
{
printf("%s\n", formatISCDate(file.gatherInt32(v)).c_str());
break;
}
case dtype_sql_time:
{
printf("%s\n", formatISCTime(file.gatherInt32(v)).c_str());
break;
}
case dtype_timestamp:
{
printf("%s %s\n", formatISCDate(file.gatherInt32(v)).c_str(), formatISCTime(file.gatherInt32(v + offsetof(ISC_TIMESTAMP, timestamp_time))).c_str());
break;
}
case dtype_blob:
case dtype_array:
{
printf("%x:%x\n", file.gatherInt32(v), file.gatherInt32(v + offsetof(ISC_QUAD, isc_quad_low)));
break;
}
case dtype_int64:
{
printf("%s\n", formatDecimal(desc.dsc_scale, file.gatherInt64(v)).c_str());
break;
}
case dtype_boolean:
{
printf("%s\n", *v ? "TRUE" : "FALSE");
break;
}
case dtype_dec64:
{
// Here I hope that byte layout of DecFloat matches layout of integers of the same size
// Perhaps it is not true.
Status st("Get DecFloat16 interface");
Firebird::IDecFloat16* d = util->getDecFloat16(&st);
char buf[50];
static_assert(sizeof(uint64_t) == sizeof(FB_DEC16));
uint64_t tmp = file.gatherInt64(v);
d->toString(st("DecFloat to string"), reinterpret_cast<const FB_DEC16*>(&tmp), sizeof(buf), buf);
printf("%s\n", buf);
break;
}
case dtype_dec128:
{
Status st("Get DecFloat34 interface");
Firebird::IDecFloat34* d = util->getDecFloat34(&st);
char buf[100];
static_assert(sizeof(FB_I128) == sizeof(FB_DEC34));
FB_I128 tmp;
file.gatherInt128(v, tmp);
d->toString(st("DecFloat to string"), reinterpret_cast<const FB_DEC34*>(&tmp), sizeof(buf), buf);
printf("%s\n", buf);
break;
}
case dtype_int128:
{
Status st("Get Int128 interface");
Firebird::IInt128* d = util->getInt128(&st);
char buf[100];
FB_I128 tmp;
file.gatherInt128(v, tmp);
d->toString(st("Int128 to string"), &tmp, desc.dsc_scale, sizeof(buf), buf);
printf("%s\n", buf);
break;
}
case dtype_sql_time_tz:
{
printf("%s ", formatISCTime(file.gatherInt32(v)).c_str());
short timeZone = file.gatherInt16(v + offsetof(ISC_TIME_TZ, time_zone));
if (timeZone < 0)
{
printf("TZ id %u\n", timeZone);
}
else
{
short offset = timeZone - 1439;
printf("%d:%u\n", offset / 60, std::abs(offset) % 60);
}
break;
}
case dtype_timestamp_tz:
{
printf("%s %s ", formatISCDate(file.gatherInt32(v)).c_str(), formatISCTime(file.gatherInt32(v + offsetof(ISC_TIMESTAMP, timestamp_time))).c_str());
short timeZone = file.gatherInt16(v + offsetof(ISC_TIMESTAMP_TZ, time_zone));
if (timeZone < 0)
{
printf("TZ id %u\n", timeZone);
}
else
{
short offset = timeZone - 1439;
printf("%d:%u\n", offset / 60, std::abs(offset) % 60);
}
break;
}
// Ex-types are not used in storage, at least for now
default:
{
printf("*** unknown type %u ***\n", desc.dsc_dtype);
HexDumper dumper;
dumper.dumpIt(v, desc.dsc_length);
}
}
}
}
}
void dumpData(Attachment& att, const Reader& file, const std::string& schema, const std::string& name, const unsigned char* oldData, size_t oldDataLength, const unsigned char* newData, size_t newDataLength)
{
if (att)
{
TableKey tableKey(schema, name);
auto itr = cache.find(tableKey);
if (itr == cache.end())
{
Status st(nullptr);
if (!att.trans)
{
att.trans.reset(att->startTransaction(st("Start transaction"), 0, nullptr));
}
if (!att.formatQuery)
{
if (schema.empty())
{
att.formatQuery.reset(att->prepare(st("Prepare format query"), att.trans.get(),
0, "select FMT.RDB$DESCRIPTOR from RDB$RELATIONS REL left join RDB$FORMATS FMT on REL.RDB$RELATION_ID=FMT.RDB$RELATION_ID"
" where REL.RDB$RELATION_NAME=?"
" order by FMT.RDB$FORMAT DESC",
SQL_DIALECT_V6, Firebird::IStatement::PREPARE_PREFETCH_NONE));
}
else
{
att.formatQuery.reset(att->prepare(st("Prepare format query"), att.trans.get(),
0, "select FMT.RDB$DESCRIPTOR from RDB$RELATIONS REL left join RDB$FORMATS FMT on REL.RDB$RELATION_ID=FMT.RDB$RELATION_ID"
" where REL.RDB$SCHEMA_NAME = ? AND REL.RDB$RELATION_NAME=?"
" order by FMT.RDB$FORMAT DESC",
SQL_DIALECT_V6, Firebird::IStatement::PREPARE_PREFETCH_NONE));
}
}
DECLARE
{
BLOB(format);
} formatInfo;
std::unique_ptr<Firebird::IResultSet, interface_deleter> cursor;
if (schema.empty())
{
DECLARE
{
VARCHAR_UTF8(name, 63);
} tableName;
tableName.name_null = 0;
tableName.name = name;
cursor.reset(att.formatQuery->openCursor(st("Open format list"), att.trans.get(), tableName.metadata, tableName.data, formatInfo.metadata, 0));
}
else
{
DECLARE
{
VARCHAR_UTF8(schema, 63);
VARCHAR_UTF8(name, 63);
} tableName;
tableName.schema_null = 0;
tableName.schema = schema;
tableName.name_null = 0;
tableName.name = name;
cursor.reset(att.formatQuery->openCursor(st("Open format list"), att.trans.get(), tableName.metadata, tableName.data, formatInfo.metadata, 0));
}
// This should create a new empty entry to prevent following attempts to load the same table
itr = cache.emplace(tableKey, FormatList()).first;
FormatList& list = itr->second;
while (cursor->fetchNext(st("Fetch formats list"), formatInfo.data) == Firebird::IStatus::RESULT_OK)
{
std::unique_ptr<Firebird::IBlob, interface_deleter> blob(att->openBlob(st("Open format BLOB"), att.trans.get(), &formatInfo.format.value, 0, nullptr));
short fieldsNumber;
unsigned r = 0;
if (blob->getSegment(st("Read relation format"), sizeof(fieldsNumber), &fieldsNumber, &r) == Firebird::IStatus::RESULT_NO_DATA || r != sizeof(fieldsNumber))
{
throw static_exception("Record format is badly truncated");
}
std::vector<Ods::Descriptor13> format(fieldsNumber);
unsigned to_read = sizeof(Ods::Descriptor13) * fieldsNumber;
unsigned char* p = reinterpret_cast<unsigned char*>(format.data());
r = 0;
while (to_read > 0 && blob->getSegment(&st, to_read, p, &r) != Firebird::IStatus::RESULT_NO_DATA)
{
to_read -= r;
p += r;
r = 0;
}
// Defaults can be ignored
blob->close(st("Close format BLOB"));
blob.release();
// Recalculate required buffer length and offsets
size_t dataSize = 0;
for (short i = 0; i < fieldsNumber; ++i)
{
const Ods::Descriptor13& desc = format[i];
if (desc.dsc_offset != 0)
{
// This is a real, existing field
size_t newSize = desc.dsc_offset + desc.dsc_length;
if (newSize > dataSize)
dataSize = newSize;
}
}
// If format with this size already exist - don't replace it.
// This matches applier's logic
list.try_emplace(dataSize, std::move(format));
}
}
const Format* oldFormat = nullptr;
size_t oldFormatSize = 0;
const Format* newFormat = nullptr;
size_t newFormatSize = 0;
if (oldData != nullptr && oldDataLength != 0)
{
const auto fmtItr = itr->second.find(oldDataLength);
if (fmtItr == itr->second.end())
{
fprintf(stderr, "No format of length %zu found\n", oldDataLength);
// And fall out to binary dump
}
else
{
oldFormat = &fmtItr->second;
oldFormatSize = oldFormat->size();
}
}
if (newData != nullptr && newDataLength != 0)
{
const auto fmtItr = itr->second.find(newDataLength);
if (fmtItr == itr->second.end())
{
fprintf(stderr, "No format of length %zu found\n", newDataLength);
// And fall out to binary dump
}
else
{
newFormat = &fmtItr->second;
newFormatSize = newFormat->size();
}
}
size_t maxSize = std::max(oldFormatSize, newFormatSize);
for (size_t i = 0; i < maxSize; ++i)
{
if (oldData != nullptr && oldDataLength != 0 && i < oldFormatSize)
{
printf("\t\t%zu< ", i);
dumpData(file, *oldFormat, oldData, i);
}
if (newData != nullptr && newDataLength != 0 && i < newFormatSize)
{
printf("\t\t%zu> ", i);
dumpData(file, *newFormat, newData, i);
}
}
return;
}
CompactHexDumper dmp;
if (oldData != nullptr && oldDataLength != 0)
{
printf("\t\t< ");
dmp.dumpIt(oldData, oldDataLength);
printf("\n");
}
if (newData != nullptr && newDataLength != 0)
{
printf("\t\t> ");
dmp.dumpIt(newData, newDataLength);
printf("\n");
}
}