-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
ClumpletReader.cpp
608 lines (548 loc) · 13.9 KB
/
ClumpletReader.cpp
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* PROGRAM: Client/Server Common Code
* MODULE: ClumpletReader.cpp
* DESCRIPTION: Secure handling of clumplet buffers
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Nickolay Samofatov
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*/
#include "firebird.h"
#include "../common/classes/ClumpletReader.h"
#include "fb_exception.h"
#include "../jrd/ibase.h"
#ifdef DEBUG_CLUMPLETS
#include "../jrd/gds_proto.h"
#include <ctype.h>
namespace Firebird {
class ClumpletDump : public ClumpletReader
{
public:
ClumpletDump(Kind k, const UCHAR* buffer, size_t buffLen)
: ClumpletReader(k, buffer, buffLen) { }
static string hexString(const UCHAR* b, size_t len)
{
string t1, t2;
for (; len > 0; --len, ++b) {
if (isprint(*b))
t2 += *b;
else {
t1.printf("<%02x>", *b);
t2 += t1;
}
}
return t2;
}
protected:
virtual void usage_mistake(const char* what) const
{
fatal_exception::raiseFmt(
"Internal error when using clumplet API: %s", what);
}
virtual void invalid_structure(const char* what) const
{
fatal_exception::raiseFmt(
"Invalid clumplet buffer structure: %s", what);
}
};
void ClumpletReader::dump() const
{
static int dmp = 0;
gds__log("*** DUMP ***");
if (dmp) {
// Avoid infinite recursion during dump
gds__log("recursion");
return;
}
dmp++;
try {
ClumpletDump d(kind, getBuffer(), getBufferLength());
int t = (kind == SpbStart || kind == UnTagged || kind == WideUnTagged) ? -1 : d.getBufferTag();
gds__log("Tag=%d Offset=%d Length=%d Eof=%d\n", t, getCurOffset(), getBufferLength(), isEof());
for (d.rewind(); !(d.isEof()); d.moveNext())
{
gds__log("Clump %d at offset %d: %s", d.getClumpTag(), d.getCurOffset(),
ClumpletDump::hexString(d.getBytes(), d.getClumpLength()).c_str());
}
}
catch (const fatal_exception& x) {
gds__log("Fatal exception during clumplet dump: %s", x.what());
size_t l = getBufferLength() - getCurOffset();
const UCHAR *p = getBuffer() + getCurOffset();
gds__log("Plain dump starting with offset %d: %s", getCurOffset(),
ClumpletDump::hexString(p, l).c_str());
}
dmp--;
}
}
#endif //DEBUG_CLUMPLETS
namespace Firebird {
ClumpletReader::ClumpletReader(Kind k, const UCHAR* buffer, size_t buffLen) :
kind(k), static_buffer(buffer), static_buffer_end(buffer + buffLen)
{
rewind(); // this will set cur_offset and spbState
}
ClumpletReader::ClumpletReader(MemoryPool& pool, Kind k, const UCHAR* buffer, size_t buffLen) :
AutoStorage(pool), kind(k), static_buffer(buffer), static_buffer_end(buffer + buffLen)
{
rewind(); // this will set cur_offset and spbState
}
void ClumpletReader::usage_mistake(const char* what) const
{
#ifdef DEBUG_CLUMPLETS
dump();
#endif
fatal_exception::raiseFmt(
"Internal error when using clumplet API: %s", what);
}
void ClumpletReader::invalid_structure(const char* what) const
{
#ifdef DEBUG_CLUMPLETS
dump();
#endif
fatal_exception::raiseFmt(
"Invalid clumplet buffer structure: %s", what);
}
UCHAR ClumpletReader::getBufferTag() const
{
const UCHAR* const buffer_end = getBufferEnd();
const UCHAR* buffer_start = getBuffer();
switch (kind)
{
case Tpb:
case Tagged:
case WideTagged:
if (buffer_end - buffer_start == 0)
{
invalid_structure("empty buffer");
return 0;
}
return buffer_start[0];
case SpbStart:
case UnTagged:
case WideUnTagged:
case SpbItems:
usage_mistake("buffer is not tagged");
return 0;
case SpbAttach:
if (buffer_end - buffer_start == 0)
{
invalid_structure("empty buffer");
return 0;
}
switch (buffer_start[0])
{
case isc_spb_version1:
// This is old SPB format, it's almost like DPB -
// buffer's tag is the first byte.
return buffer_start[0];
case isc_spb_version:
// Buffer's tag is the second byte
if (buffer_end - buffer_start == 1)
{
invalid_structure("buffer too short (1 byte)");
return 0;
}
return buffer_start[1];
default:
invalid_structure("spb in service attach should begin with isc_spb_version1 or isc_spb_version");
return 0;
}
default:
fb_assert(false);
return 0;
}
}
ClumpletReader::ClumpletType ClumpletReader::getClumpletType(UCHAR tag) const
{
switch (kind)
{
case Tagged:
case UnTagged:
case SpbAttach:
return TraditionalDpb;
case WideTagged:
case WideUnTagged:
return Wide;
case Tpb:
switch (tag)
{
case isc_tpb_lock_write:
case isc_tpb_lock_read:
return TraditionalDpb;
}
return SingleTpb;
case SpbItems:
return SingleTpb;
case SpbStart:
switch (spbState) {
case 0:
return SingleTpb;
case isc_action_svc_backup:
case isc_action_svc_restore:
switch (tag)
{
case isc_spb_bkp_file:
case isc_spb_dbname:
return StringSpb;
case isc_spb_bkp_factor:
case isc_spb_bkp_length:
case isc_spb_res_length:
case isc_spb_res_buffers:
case isc_spb_res_page_size:
case isc_spb_options:
return IntSpb;
case isc_spb_verbose:
return SingleTpb;
case isc_spb_res_access_mode:
return ByteSpb;
}
invalid_structure("unknown parameter for backup/restore");
break;
case isc_action_svc_repair:
switch (tag)
{
case isc_spb_dbname:
return StringSpb;
case isc_spb_options:
case isc_spb_rpr_commit_trans:
case isc_spb_rpr_rollback_trans:
case isc_spb_rpr_recover_two_phase:
return IntSpb;
}
invalid_structure("unknown parameter for repair");
break;
case isc_action_svc_add_user:
case isc_action_svc_delete_user:
case isc_action_svc_modify_user:
case isc_action_svc_display_user:
switch (tag)
{
case isc_spb_dbname:
case isc_spb_sql_role_name:
case isc_spb_sec_username:
case isc_spb_sec_password:
case isc_spb_sec_groupname:
case isc_spb_sec_firstname:
case isc_spb_sec_middlename:
case isc_spb_sec_lastname:
return StringSpb;
case isc_spb_sec_userid:
case isc_spb_sec_groupid:
return IntSpb;
}
invalid_structure("unknown parameter for security database operation");
break;
case isc_action_svc_properties:
switch (tag)
{
case isc_spb_dbname:
return StringSpb;
case isc_spb_prp_page_buffers:
case isc_spb_prp_sweep_interval:
case isc_spb_prp_shutdown_db:
case isc_spb_prp_deny_new_attachments:
case isc_spb_prp_deny_new_transactions:
case isc_spb_prp_set_sql_dialect:
case isc_spb_options:
case isc_spb_prp_force_shutdown:
case isc_spb_prp_attachments_shutdown:
case isc_spb_prp_transactions_shutdown:
return IntSpb;
case isc_spb_prp_reserve_space:
case isc_spb_prp_write_mode:
case isc_spb_prp_access_mode:
case isc_spb_prp_shutdown_mode:
case isc_spb_prp_online_mode:
return ByteSpb;
}
invalid_structure("unknown parameter for setting database properties");
break;
// case isc_action_svc_add_license:
// case isc_action_svc_remove_license:
case isc_action_svc_db_stats:
switch (tag)
{
case isc_spb_dbname:
case isc_spb_command_line:
return StringSpb;
case isc_spb_options:
return IntSpb;
}
invalid_structure("unknown parameter for getting statistics");
break;
case isc_action_svc_get_ib_log:
invalid_structure("unknown parameter for getting log");
break;
}
invalid_structure("wrong spb state");
break;
}
invalid_structure("unknown reason");
return SingleTpb;
}
void ClumpletReader::adjustSpbState()
{
switch (kind)
{
case SpbStart:
if (spbState == 0) { // Just started with service start block
spbState = getClumpTag();
}
break;
default:
break;
}
}
size_t ClumpletReader::getClumpletSize(bool wTag, bool wLength, bool wData) const
{
const UCHAR* clumplet = getBuffer() + cur_offset;
const UCHAR* const buffer_end = getBufferEnd();
// Check for EOF
if (clumplet >= buffer_end) {
usage_mistake("read past EOF");
return 0;
}
size_t rc = wTag ? 1 : 0;
size_t lengthSize = 0;
size_t dataSize = 0;
switch (getClumpletType(clumplet[0]))
{
// This form allows clumplets of virtually any size
case Wide:
// Check did we receive length component for clumplet
if (buffer_end - clumplet < 5) {
invalid_structure("buffer end before end of clumplet - no length component");
return rc;
}
lengthSize = 4;
dataSize = clumplet[4];
dataSize <<= 8;
dataSize += clumplet[3];
dataSize <<= 8;
dataSize += clumplet[2];
dataSize <<= 8;
dataSize += clumplet[1];
break;
// This is the most widely used form
case TraditionalDpb:
// Check did we receive length component for clumplet
if (buffer_end - clumplet < 2) {
invalid_structure("buffer end before end of clumplet - no length component");
return rc;
}
lengthSize = 1;
dataSize = clumplet[1];
break;
// Almost all TPB parameters are single bytes
case SingleTpb:
break;
// Used in SPB for long strings
case StringSpb:
// Check did we receive length component for clumplet
if (buffer_end - clumplet < 3) {
invalid_structure("buffer end before end of clumplet - no length component");
return rc;
}
lengthSize = 2;
dataSize = clumplet[2];
dataSize <<= 8;
dataSize += clumplet[1];
break;
// Used in SPB for 4-byte integers
case IntSpb:
dataSize = 4;
break;
// Used in SPB for single byte
case ByteSpb:
dataSize = 1;
break;
}
const size_t total = 1 + lengthSize + dataSize;
if (clumplet + total > buffer_end) {
invalid_structure("buffer end before end of clumplet - clumplet too long");
size_t delta = total - (buffer_end - clumplet);
if (delta > dataSize)
dataSize = 0;
else
dataSize -= delta;
}
if (wLength) {
rc += lengthSize;
}
if (wData) {
rc += dataSize;
}
return rc;
}
void ClumpletReader::moveNext()
{
if (isEof())
return; // no need to raise useless exceptions
size_t cs = getClumpletSize(true, true, true);
adjustSpbState();
cur_offset += cs;
}
void ClumpletReader::rewind()
{
if (! getBuffer()) {
cur_offset = 0;
spbState = 0;
return;
}
if (kind == UnTagged || kind == WideUnTagged || kind == SpbStart || kind == SpbItems)
cur_offset = 0;
else if (kind == SpbAttach && getBufferLength() > 0
&& getBuffer()[0] != isc_spb_version1)
cur_offset = 2;
else
cur_offset = 1;
spbState = 0;
}
bool ClumpletReader::find(UCHAR tag)
{
const size_t co = getCurOffset();
for (rewind(); !isEof(); moveNext())
{
if (tag == getClumpTag())
{
return true;
}
}
setCurOffset(co);
return false;
}
// Methods which work with currently selected clumplet
UCHAR ClumpletReader::getClumpTag() const
{
const UCHAR* clumplet = getBuffer() + cur_offset;
const UCHAR* const buffer_end = getBufferEnd();
// Check for EOF
if (clumplet >= buffer_end) {
usage_mistake("read past EOF");
return 0;
}
return clumplet[0];
}
size_t ClumpletReader::getClumpLength() const
{
return getClumpletSize(false, false, true);
}
const UCHAR* ClumpletReader::getBytes() const
{
return getBuffer() + cur_offset + getClumpletSize(true, true, false);
}
SINT64 ClumpletReader::fromVaxInteger(const UCHAR* ptr, size_t length)
{
// This code is taken from gds__vax_integer
SINT64 value = 0;
int shift = 0;
while (length > 0) {
--length;
value += ((SLONG) *ptr++) << shift;
shift += 8;
}
return value;
}
SLONG ClumpletReader::getInt() const
{
const size_t length = getClumpLength();
if (length > 4) {
invalid_structure("length of integer exceeds 4 bytes");
return 0;
}
return fromVaxInteger(getBytes(), length);
}
double ClumpletReader::getDouble() const
{
if (getClumpLength() != sizeof(double)) {
invalid_structure("length of double must be equal 8 bytes");
return 0;
}
// based on XDR code
union {
double temp_double;
SLONG temp_long[2];
} temp;
fb_assert(sizeof(double) == sizeof(temp));
const UCHAR* ptr = getBytes();
temp.temp_long[FB_LONG_DOUBLE_FIRST] = fromVaxInteger(ptr, sizeof(SLONG));
temp.temp_long[FB_LONG_DOUBLE_SECOND] = fromVaxInteger(ptr + sizeof(SLONG), sizeof(SLONG));
return temp.temp_double;
}
ISC_TIMESTAMP ClumpletReader::getTimeStamp() const
{
ISC_TIMESTAMP value;
if (getClumpLength() != sizeof(ISC_TIMESTAMP)) {
invalid_structure("length of ISC_TIMESTAMP must be equal 8 bytes");
value.timestamp_date = 0;
value.timestamp_time = 0;
return value;
}
const UCHAR* ptr = getBytes();
value.timestamp_date = fromVaxInteger(ptr, sizeof(SLONG));
value.timestamp_time = fromVaxInteger(ptr + sizeof(SLONG), sizeof(SLONG));
return value;
}
SINT64 ClumpletReader::getBigInt() const
{
const size_t length = getClumpLength();
if (length > 8) {
invalid_structure("length of BigInt exceeds 8 bytes");
return 0;
}
return fromVaxInteger(getBytes(), length);
}
string& ClumpletReader::getString(string& str) const
{
const UCHAR* ptr = getBytes();
const size_t length = getClumpLength();
str.assign(reinterpret_cast<const char*>(ptr), length);
str.recalculate_length();
if (str.length() + 1 < length)
{
invalid_structure("string length doesn't match with clumplet");
}
return str;
}
PathName& ClumpletReader::getPath(PathName& str) const
{
const UCHAR* ptr = getBytes();
const size_t length = getClumpLength();
str.assign(reinterpret_cast<const char*>(ptr), length);
str.recalculate_length();
if (str.length() + 1 < length)
{
invalid_structure("path length doesn't match with clumplet");
}
return str;
}
bool ClumpletReader::getBoolean() const
{
const UCHAR* ptr = getBytes();
const size_t length = getClumpLength();
if (length > 1) {
invalid_structure("length of boolean exceeds 1 byte");
return false;
}
return length && ptr[0];
}
} // namespace