forked from alitrack/undark
-
Notifications
You must be signed in to change notification settings - Fork 4
/
undark.c
1461 lines (1151 loc) · 41.5 KB
/
undark.c
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* undark - generic data puller from SQLite DBs.
*
* Rather CPU intensive likely, relies on the correlation
* that the SQLite length of payload should be the same
* as the summation of the payload cell sizes.
*
* Written by Paul L Daniels ([email protected])
*
* BSD Revised licence ( see LICENCE )
*
* Original version released October 6, 2013
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
//#include <winsock2.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include "varint.h"
#define FL __FILE__,__LINE__
#define VERBOSE if (g->verbose)
#define DEBUG if (g->debug)
#define DECODE_MODE_FREESPACE 1
#define DECODE_MODE_NORMAL 0
#define PAYLOAD_SIZE_MINIMUM 10
#define PAYLOAD_CELLS_MAX 1000
#define OVERFLOW_PAGES_MAX 10000
#define PARAM_VERSION "--version"
#define PARAM_HELP "--help"
#define PARAM_FINE_SEARCH "--fine-search"
#define PARAM_FREESPACE_ONLY "--freespace"
#define PARAM_FREESPACE_MINIMUM "--freespace-minimum="
#define PARAM_NO_BLOBS "--no-blobs"
#define PARAM_BLOB_SIZE_LIMIT "--blob-size-limit="
#define PARAM_CELLCOUNT_MIN "--cellcount-min="
#define PARAM_CELLCOUNT_MAX "--cellcount-max="
#define PARAM_ROWSIZE_MIN "--rowsize-min="
#define PARAM_ROWSIZE_MAX "--rowsize-max="
#define PARAM_PAGE_SIZE "--page-size="
#define PARAM_PAGE_START "--page-start=" // add to 0.5
#define PARAM_PAGE_END "--page-end=" // add to 0.5
#define PARAM_REMOVED_ONLY "--removed-only"
struct globals {
uint8_t debug;
uint8_t verbose;
char *input_file; // actual file name
char *db_origin; // the mmap'd file origin
char *db_end; // the computed end of the mmap'd file ( based on file size )
char *db_cfp; // current file position
char *db_cpp; // current page position
char *db_cpp_limit; // end of the current page
size_t db_size;
uint32_t page_size, page_count, page_number;
uint32_t page_start, page_end;
uint32_t freelist_first_page, freelist_page_count;
uint32_t *freelist_pages;
uint32_t freelist_pages_current_index;
int freelist_space_only;
int removed_only;
size_t freespace_minimum;
time_t date_upper, date_lower; // deprecated - now that Undark has become a generic tool
int cc_min, cc_max; // cell count limits
size_t rs_min, rs_max; // row/payload limits
int report_blobs; // do we even handle blob data
size_t blob_size_limit; // at which point do we cut over to dumping to *.blob files?
int blob_count;
int fine_search;
};
struct cell {
int t; // serial
int o; // offset
int s; // size
};
struct sql_payload {
uint64_t prefix_length;
uint64_t length;
uint64_t rowid;
uint64_t header_size;
int cell_count;
int cell_page;
int cell_page_offset;
struct cell cells[PAYLOAD_CELLS_MAX+1];
uint32_t overflow_pages[OVERFLOW_PAGES_MAX+1];
char *mapped_data, *mapped_data_endpoint;
};
struct sqlite_leaf_header {
int page_number;
int page_byte;
uint16_t freeblock_offset;
uint16_t freeblock_size;
uint16_t freeblock_next;
int cellcount;
int cell_offset;
int freebytes;
};
char version[] = "undark version 0.7.1, by Paul L Daniels ( [email protected] )\n";
char help[] = "-i <sqlite DB> [-d] [-v] [-V|--version] [--cellcount-min=<count>] [--cellcount-max=<count>] [--rowsize-min=<bytes>] [--rowsize-max=<bytes>] [--no-blobs] [--blob-size-limit=<bytes>] [--page-size=<bytes>] [--page-start=<number>] [--page-end=<number>] [--freespace] [--freespace-minimum=<bytes>]\n"
"\t-i: input SQLite3 format database\n"
"\t-d: enable debugging output (very large dumps)\n"
"\t-v: enable verbose output\n"
"\t-V|--version: show version of software\n"
"\t-h|--help: show this help\n"
"\t--cellcount-min: define the minimum number of cells a row must have to be extracted\n"
"\t--cellcount-max: define the maximum number of cells a row must have to be extracted\n"
"\t--rowsize-min: define the minimum number of bytes a row must have to be extracted\n"
"\t--rowsize-max: define the maximum number of bytes a row must have to be extracted\n"
"\t--no-blobs: disable the dumping of blob data\n"
"\t--blob-size-limit: all blobs larger than this size are dumped to .blob files\n"
"\t--fine-search: search DB shifting one byte at a time, rather than records\n"
"\t--page-size: hard code the page size for the DB (useful when header is damaged)\n"
"\t--removed-only: Dumps rows that have their key set to -1\n"
//"\t--page-start: starting page to scan in db\n"
//"\t--page-end: ending page to scan in db\n"
"\t--freespace: search for rows in the freespace\n"
//"\t--freespace-minimum: smallest freespace size to search in\n"
;
/*-----------------------------------------------------------------\
Date Code: : 20131023-105927
Function Name : UNDARK_init
Returns Type : int
----Parameter List
1. struct globals *g ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int UNDARK_init( struct globals *g ) {
/**
* Initialise our globals
*/
g->page_size = 0;
g->page_count = 0;
g->page_number = 1;
g->debug = 0;
g->verbose = 0;
g->input_file = NULL;
g->date_lower = 0;
g->date_upper = 0;
g->cc_max = PAYLOAD_CELLS_MAX;
g->cc_min = 2;
g->rs_max = SIZE_MAX;
g->rs_min = 10;
g->blob_count = 0;
g->report_blobs = 1;
g->blob_size_limit = SIZE_MAX; // C99
g->fine_search = 0;
g->freelist_space_only = 0;
g->removed_only = 0;
g->freespace_minimum = SIZE_MAX; // C99
g->page_start = 0;
g->page_end = 0;
g->db_cfp = NULL;
g->db_cpp = NULL;
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20131023-105933
Function Name : UNDARK_parse_parameters
Returns Type : int
----Parameter List
1. int argc,
2. char **argv,
3. struct globals *g ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int UNDARK_parse_parameters( int argc, char **argv, struct globals *g ) {
int param;
/**
* We need at least some additional parameters
*
*/
if (argc < 2) {
fprintf(stderr,"%s", help);
fprintf(stderr,"Sizeof double = %ld, long double = %ld\n", sizeof(double), sizeof(long double));
exit(1);
}
/**
* Decode the input parameters.
* Yes, I know, I should do this using gnu params etc.
*/
for (param = 1; param < argc; param++) {
char *p = argv[param];
if (strcmp(p, "-V") == 0) { fprintf(stdout,"%s", version); exit(0); }
if (strcmp(p, "-h") == 0) { fprintf(stdout,"%s %s", argv[0], help); exit(0); }
if (strcmp(p, "-d") == 0) g->debug = 1;
if (strcmp(p, "-v") == 0) g->verbose = 1;
if (strcmp(p, "-i") == 0) {
param++;
if (param < argc) {
g->input_file = argv[param];
} else {
fprintf(stderr,"Not enough paramters\n");
exit(1);
}
} else if (strncmp(p,"--", 2) == 0) {
DEBUG fprintf(stderr,"Parameter: '%s' %d\n", p, (int)strlen(PARAM_BLOB_SIZE_LIMIT));
// extended parameters
if (strncmp(p,PARAM_VERSION, strlen(PARAM_VERSION))==0) {
fprintf(stderr,"%s", version);
exit(0);
} else if (strncmp(p,PARAM_HELP, strlen(PARAM_HELP))==0) {
fprintf(stderr,"%s %s", argv[0], help);
exit(0);
} else if (strncmp(p,PARAM_NO_BLOBS, strlen(PARAM_NO_BLOBS))==0) {
g->report_blobs = 0;
} else if (strncmp(p,PARAM_BLOB_SIZE_LIMIT, strlen(PARAM_BLOB_SIZE_LIMIT))==0) {
p = p +strlen(PARAM_BLOB_SIZE_LIMIT);
g->blob_size_limit = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_PAGE_START, strlen(PARAM_PAGE_START))==0) {
p = p +strlen(PARAM_PAGE_START);
g->page_start = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_PAGE_END, strlen(PARAM_PAGE_END))==0) {
p = p +strlen(PARAM_PAGE_END);
g->page_end = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_PAGE_SIZE, strlen(PARAM_PAGE_SIZE))==0) {
p = p +strlen(PARAM_PAGE_SIZE);
g->page_size = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_FREESPACE_MINIMUM, strlen(PARAM_FREESPACE_MINIMUM))==0) {
p = p +strlen(PARAM_FREESPACE_MINIMUM);
g->freespace_minimum = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_CELLCOUNT_MIN, strlen(PARAM_CELLCOUNT_MIN))==0) {
p = p +strlen(PARAM_CELLCOUNT_MIN);
g->cc_min = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_CELLCOUNT_MAX, strlen(PARAM_CELLCOUNT_MAX))==0) {
p = p +strlen(PARAM_CELLCOUNT_MAX);
g->cc_max = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_ROWSIZE_MIN, strlen(PARAM_ROWSIZE_MIN))==0) {
p = p +strlen(PARAM_ROWSIZE_MIN);
g->rs_min = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_ROWSIZE_MAX, strlen(PARAM_ROWSIZE_MAX))==0) {
p = p +strlen(PARAM_ROWSIZE_MAX);
g->rs_max = strtol( p, NULL, 10 );
} else if (strncmp(p,PARAM_FINE_SEARCH, strlen(PARAM_FINE_SEARCH))==0) {
g->fine_search = 1;
} else if (strncmp(p,PARAM_FREESPACE_ONLY, strlen(PARAM_FREESPACE_ONLY))==0) {
g->freelist_space_only = 1;
} else if (strncmp(p,PARAM_REMOVED_ONLY, strlen(PARAM_REMOVED_ONLY))==0) {
g->removed_only = 1;
} else {
fprintf(stderr,"Cannot interpret extended parameter: \"%s\"\n",p);
exit(1);
}
}
}
if (g->input_file == NULL) {
fprintf(stderr,"ERROR: Need input file\n");
exit(1);
}
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20131006-121932
Function Name : to_signed_byte
Returns Type : char
----Parameter List
1. unsigned char value,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Converts 2's compliment byte to signed integer
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char to_signed_byte(unsigned char value) {
int signed_value = value;
if (value >> 7) signed_value |= -1 << 7;
return signed_value;
}
int to_signed_int( unsigned int value ) {
int signed_value = value;
if (value >> 15) signed_value |= -1 << 15;
return signed_value;
}
long int to_signed_long( unsigned long int value ) {
long int signed_value = value;
if (value >> 31) signed_value |= -1 << 31;
return signed_value;
}
/*-----------------------------------------------------------------\
Date Code: : 20131002-220244
Function Name : tdump
Returns Type : int
----Parameter List
1. char *p,
2. uint16_t l ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int tdump( char *p, uint16_t l ) {
while (l--) {
if (isprint(*p)) fprintf(stdout,"%c", *p); else fprintf(stdout,".");
p++;
}
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20131006-122020
Function Name : sqltdump
Returns Type : int
----Parameter List
1. char *p,
2. uint16_t l ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Dumps text in a SQL friendly format ( doubling of single quotes )
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int sqltdump( char *p, uint16_t l ) {
fprintf(stdout,"\"");
while (l--) {
if (*p == '\"') fprintf(stdout,"\"");
if (isprint(*p)) fprintf(stdout,"%c", *p); else fprintf(stdout,".");
p++;
}
fprintf(stdout,"\"");
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20131007-004650
Function Name : blob_dump
Returns Type : int
----Parameter List
1. unsigned char *p,
2. uint16_t l ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int blob_dump( unsigned char *p, uint16_t l ) {
fprintf(stdout,"x'");
while (l--) {
fprintf(stdout,"%02X", ( unsigned char)*p);
p++;
}
fprintf(stdout,"'");
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20131002-220250
Function Name : hdump
Returns Type : int
----Parameter List
1. char *p,
2. uint16_t l ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Combo hex + text dump, 16 byte wide rows
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int hdump( unsigned char *p, uint16_t length, char *msg ) {
int oc = 0;
int ll = length;
fprintf(stdout,"%s: Hexdumping %d bytes from %p\n", msg, ll, p);
uint16_t c = 0;
if (p == NULL) {
fprintf(stdout,"ERROR: NULL passed.\n");
// exit(1);
}
while (ll > 0) {
int br;
unsigned char *op;
fprintf(stdout,"%04X [%06d] ",oc, ll);
oc+=16;
br = ll;
op = p;
while (ll--) {
fprintf(stdout,"%02X ", *p);
c++;
p++;
if (c%16 == 0) break;
}
ll = br;
p = op;
c = 0;
fprintf(stdout, " [%06d]", ll );
while (ll--) {
fprintf(stdout,"%c", isprint(*p)?*p:'.');
c++;
p++;
if (c%16 == 0) break;
}
fprintf(stdout," %d\n",ll);
}
fprintf(stdout,"\n");
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20131007-184003
Function Name : blob_dump_to_file
Returns Type : int
----Parameter List
1. struct globals *glob,
2. char *p,
3. size_t l ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int blob_dump_to_file( struct globals *g, char *p, size_t l ) {
int f;
ssize_t written;
char fn[1024];
snprintf(fn, sizeof(fn), "%d.blob", g->blob_count);
DEBUG fprintf(stdout,"%s:%d:DEBUG: Writing %ld bytes to %s\n", FL , l, fn );
f = open(fn, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
if (!f) { fprintf(stderr,"Cannot open %s (%s)\n", fn, strerror(errno)); return 1; }
written = write(f, p, l);
if ( written != l ) {
fprintf(stderr,"Wrote %ld of %ld bytes to %s ( %s )\n", written, l, fn, strerror(errno));
close(f);
return 1;
}
close(f);
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20131003-223556
Function Name : *bstrstr
Returns Type : char
----Parameter List
1. char *needle,
2. char *haystack,
3. char *limit ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Searches for the needle among a haystack possibly containing
\0 delimeted data.
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
char *bstrstr( char *haystack, char *needle, char *limit ) {
char *p;
if ((!needle)||(*needle == '\0')) return NULL;
if (!haystack) return NULL;
if ((limit == NULL)||(limit <=haystack)) return NULL;
p = haystack;
while (p < limit) {
char *tp;
char *tn;
tn = needle;
tp = p;
while ((*tn) && (tp < limit) && (*tp == *tn)) {
tn++;
tp++;
if (*tn == '\0') return p;
}
p++;
}
return NULL;
}
/*-----------------------------------------------------------------\
Date Code: : 20131004-175721
Function Name : decode_row_meta
Returns Type : int
----Parameter List
1. uint8_t *p,
2. struct sql_payload *payload ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
Decodes the payload header data so that we can then later
pull the actual data from the file.
--------------------------------------------------------------------
Changes:
added 'mode', standard, or freespace
\------------------------------------------------------------------*/
int decode_row( struct globals *g, char *p, char *data_endpoint, struct sql_payload *payload, int mode, size_t forced_length ) {
int t = 0, offset;
char *plh_ep; // payload header end point
char *base = p;
DEBUG {
fprintf(stdout,"%s:%d:DEBUG:DECODING ROW-------------------------MODE:%s\n", FL, (mode?"Freespace":"Standard"));
hdump((unsigned char *)p, 16, "Decode_row start data");
}
payload->overflow_pages[0] = 0;
payload->cell_count = 0;
if ( mode == DECODE_MODE_FREESPACE ) {
payload->length = forced_length -4; // and we still have to deduct the payload header size
} else {
varint_decode( &(payload->length), p, &p );
}
if (payload->length > g->db_size) return 0;
if (payload->length < g->rs_min) return 0;
if (payload->length > g->rs_max) return 0;
DEBUG fprintf(stdout,"%s:%d:DEBUG:Payload size: %lu\n", FL, (unsigned long int)payload->length);
if ( mode == DECODE_MODE_FREESPACE ) {
payload->rowid = 1;
} else {
varint_decode( &(payload->rowid), p, &p );
}
if (payload->rowid < 1) return 0;
payload->prefix_length = p -base; // store this so we know how many bytes the length + Row ID took up.
plh_ep = p; // first set up the beginning of the payload header array size.
varint_decode( &(payload->header_size), p, &p );
if (payload->header_size > g->page_size) return 0;
if (mode == DECODE_MODE_FREESPACE) {
payload->length -= payload->header_size;
DEBUG fprintf(stdout,"%s:%d:DEBUG: Looking for %lu bytes of data after the payload header\n", FL , (long unsigned int)payload->length);
fflush(stdout);
}
// If the payload size exceeds the page_size, then we have to do some more checking
// page size: 4096, payload->length: 4121 to 5229 CRASH
if (payload->length > (g->page_size -35)) {
//printf("Payload length: %ld, Page size: %u\n", payload->length, g->page_size);
//printf("this payload > page size\n");
uint32_t tmp, ovp;
int ovpi = 1;
// get the FIRST overflow page
memcpy(&tmp, data_endpoint -4, 4);
ovp = payload->overflow_pages[0] = ntohl(tmp);
// if the page is beyond the file range, then we've just got defective input data
if (ovp > g->page_count) return 0;
DEBUG fprintf(stdout,"%s:%d:DEBUG: First overflow page = %lu\n", FL , (long unsigned int)ovp);
DEBUG hdump((unsigned char *)(data_endpoint -16), 16, "First overflow page start data");
while (ovp > 0) {
void *calculated_address;
calculated_address = g->db_origin +( (ovp -1) *g->page_size);
DEBUG fprintf(stdout,"%s:%d:DEBUG: Calculated address: %p\n", FL, calculated_address);
// test for seeking beyond the db limit
//if ((g->db_origin +((ovp -1) *g->page_size)) > (g->db_end -4)) {
if ( calculated_address > (void *)(g->db_end -4)) { //PLD:20141220-0000
DEBUG fprintf(stdout,"%s:%d:ERROR: Seek beyond end of data looking for overflow page (%p > %p)\n", FL, calculated_address, g->db_end);
break;
}
if ( calculated_address < (void *)(g->db_origin)) { //PLD:20141220-0000
DEBUG fprintf(stdout,"%s:%d:ERROR: Seek before DB starts (%p < %p)\n", FL, calculated_address, g->db_origin);
break;
}
memcpy(&tmp, calculated_address, 4);
ovp = payload->overflow_pages[ovpi] = ntohl(tmp);
DEBUG fprintf(stdout,"%s:%d:DEBUG: overflow page[%d] = %d\n", FL , ovpi, ovp);
DEBUG fflush(stdout);
ovpi++;
if (ovpi > OVERFLOW_PAGES_MAX) {
fprintf(stdout,"ERROR: No more space for overflow pages\n");
fflush(stdout);
payload->overflow_pages[0] = 0;
break;
}
payload->overflow_pages[ovpi] = 0;
}
DEBUG {
fprintf(stdout,"DEBUG: Total of %d overflow pages\n",ovpi);
ovpi = 0;
while (payload->overflow_pages[ovpi]) {
fprintf(stdout,"DEBUG: Overflow %d->%d\n", ovpi, payload->overflow_pages[ovpi]);
ovpi++;
}
}
} // overflow handling
if (payload->header_size > g->page_size) return 0; // sorry, no can do with the way we're playing this decoding game.
if (payload->header_size < 2) return 0; // need at least 2 bytes
plh_ep += payload->header_size; // if we got a sane value, then we can use this for the full decode size ( includes the size of the first varint telling us the size )
DEBUG { fprintf(stdout,"[L:%lld][id:%lld][PLHz:%lld]",(long long int) payload->length, (long long int)payload->rowid, (long long int)payload->header_size); }
t = 0;
offset = 0;
while (1) {
uint64_t s;
int vil;
vil = varint_decode( &s, p, &p );
if (vil > 8) return 0; // no var int should be bigger than 8 bytes.
payload->cells[t].t = s; // set the type
switch (s) {
case 0: s = 0; break;
case 1: s = 1; break;
case 2: s = 2; break;
case 3: s = 3; break;
case 4: s = 4; break;
case 5: s = 6; break;
case 6: case 7: s = 8; break;
case 8: case 9: s = 0; break;
case 10: case 11: DEBUG fprintf(stdout,"%s:%d:DEBUG: celltype 10/11 reserved, aborting row.\n",FL); s = 0; return 0; break;
default:
if ((s >= 12)&&((s&0x01)==0)) { payload->cells[t].t = 12; s = (s-12)/2; }
else if ((s >= 13)&&((s&0x01)==1)) { payload->cells[t].t = 13; s = (s-13)/2; }
break;
}
payload->cells[t].s = s; // set the size/length
payload->cells[t].o = (plh_ep +offset) -base;
offset += payload->cells[t].s;
if (offset > payload->length) return 0;
DEBUG { fprintf(stdout,"[%d:%d:%d-%d(%ld)]", t, payload->cells[t].t, payload->cells[t].s, payload->cells[t].o, plh_ep -p ); }
if (p >= plh_ep) break;
t++;
payload->cell_count++;
if ( t > g->cc_max ) return 0;
} // while decoding the cells
if (p == plh_ep) {
DEBUG {
fprintf(stdout,"DEBUG: Payload head size match. (%ld =? %ld)\n ", p -base,plh_ep -base);
fprintf(stdout,"DEBUG: Data size by cell meta sum = %d\n ", offset );
}
} else {
DEBUG {
fprintf(stdout,"DEBUG: Payload scan end point, and predicted end point didn't match, difference %ld \n", p -plh_ep );
}
}
if ( t < g->cc_min ) {
DEBUG fprintf(stdout,"%s:%d:DEBUG: cell count under the minimum, so aborting\n", FL );
return 0;
}
DEBUG fprintf(stdout,"Offset [%u] + headersize [%lu] = length check [%lu]... \n", offset, (unsigned long int)payload->header_size, (unsigned long int)payload->length);
if (mode == DECODE_MODE_FREESPACE) {
/** there can often be multiple entries within freespace, so we have to be
* a little looser with our acceptance criterion
*/
if (offset <= payload->length) {
DEBUG fprintf(stdout,"%s:%d:DEBUG: FREESPACE SUBMATCH FOUND ( %u of %lu used )\n", FL , offset, (long unsigned int) payload->length);
return (offset +payload->header_size +4);
}
}
if (offset + payload->header_size == payload->length) {
DEBUG fprintf(stdout,"\nMATCH FOUND!\n");
return 1;
}
return 0;
}
/*-----------------------------------------------------------------\
Date Code: : 20150723-210259
Function Name : ntonll
Returns Type : uint64_t
----Parameter List
1. uint64_t value,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
uint64_t swap64(uint64_t x) {
uint8_t i;
uint64_t y ;
uint8_t *px, *py;
px = (uint8_t *)&x;
py = (uint8_t *)&y;
for (i=0; i<8; i++) {
*(py+i) = *(px +(7-i));
}
return y;
}
uint64_t ntohll(uint64_t value) {
uint64_t t;
// hdump( &value, 8, "\nUxx: ");
if (1==ntohl(1)) {
return value;
} else {
t = swap64(value);
// hdump( &t, 8, "\nSWP: ");
return t;
}
}
/*-----------------------------------------------------------------\
Date Code: : 20131008-182215
Function Name : dump_row
Returns Type : int
----Parameter List
1. struct globals *glob,
2. char *p,
3. char *data_endpoint,
4. struct sql_payload *payload,
5. int decode ,
------------------
Exit Codes :
Side Effects :
--------------------------------------------------------------------
Comments:
--------------------------------------------------------------------
Changes:
\------------------------------------------------------------------*/
int dump_row( struct globals *g, char *base, char *data_endpoint, struct sql_payload *payload, int mode ) {
int t = 0;
int ovpi;
void *addr;
DEBUG fprintf(stdout,"\n-DUMPING ROW------------------\n");
DEBUG hdump((unsigned char *)base, 16, "Dump_row starting data");
if ( payload->length > g->db_size ) {
DEBUG fprintf(stdout,"%s:%d:ERROR: Nonsensical payload length of %ld requested, ignoring.\n", FL, (long int)payload->length);
return -1;
}
if (payload->overflow_pages[0] == 0) {
payload->mapped_data = base;
payload->mapped_data_endpoint = data_endpoint;
} else {
int msize = (payload->length)*sizeof(uint8_t);
// count size of overflow pages (if any)
ovpi = 0;
while (payload->overflow_pages[ovpi]) {
addr = g->db_origin +((payload->overflow_pages[ovpi]-1) *g->page_size) +4; //PLD:20141221-2240 segfault fix
if (( addr < (void *)g->db_origin) || ( addr+4 > (void *)g->db_end)) {
DEBUG fprintf(stdout,"%s:%d:dump_row:ERROR: page seek request outside of boundaries of file (%p < %p > %p)\n", FL, g->db_origin, addr, g->db_end);
return -1;
}
msize += g->page_size -4;
ovpi++;
}
printf("plength %ld, total: %d\n", payload->length, msize);
//__asm__("int $3");
payload->mapped_data = malloc(msize);
if ( !payload->mapped_data ) {
fprintf(stderr,"%s:%d:ERROR: Cannot allocate %ld bytes for mapped data\n", FL, (long int)payload->length +100);
return -1;
}
DEBUG fprintf(stdout,"ALLOCATED %d bytes to mapped data\n", (int)(payload->length +100) );
if (!payload->mapped_data){ fprintf(stderr,"ERROR: Cannot allocate %d bytes for payload\n", (int)(payload->length +1)); return 0; }
memset( payload->mapped_data, 'X', payload->length +1 );
// load in the first, default page.
DEBUG fprintf(stdout,"Copying data for initial page\n");
memcpy(payload->mapped_data, base, data_endpoint -base );
payload->mapped_data_endpoint = payload->mapped_data +(data_endpoint -base -4);
// DEBUG hdump( (unsigned char *)payload->mapped_data, payload->mapped_data_endpoint -payload->mapped_data +4 );
// Load in the overflow pages (if any)
ovpi = 0;
while (payload->overflow_pages[ovpi]) {
DEBUG fprintf(stdout,"Copying data from file to memory for page %d to offset [%d]\n", payload->overflow_pages[ovpi], (int)(payload->mapped_data_endpoint -payload->mapped_data));
addr = g->db_origin +((payload->overflow_pages[ovpi]-1) *g->page_size) +4; //PLD:20141221-2240 segfault fix
if (( addr < (void *)g->db_origin) || ( addr+4 > (void *)g->db_end)) {
DEBUG fprintf(stdout,"%s:%d:dump_row:ERROR: page seek request outside of boundaries of file (%p < %p > %p)\n", FL, g->db_origin, addr, g->db_end);
return -1;
}
memcpy(payload->mapped_data_endpoint, addr, g->page_size -4);
payload->mapped_data_endpoint += g->page_size -4;
// DEBUG hdump( (unsigned char *)payload->mapped_data, payload->mapped_data_endpoint -payload->mapped_data );
ovpi++;
}
}
DEBUG hdump((unsigned char *)payload->mapped_data, payload->mapped_data_endpoint -payload->mapped_data, "Payload mapped data" );
if (mode == DECODE_MODE_FREESPACE) {
t = 0;
fprintf(stdout,"-1");
} else t = -1;
while (t <= payload->cell_count) {
DEBUG fprintf(stdout,"%s:%d:DEBUG: Cell[%d], Type:%d, size:%d, offset:%d\n", FL , t, payload->cells[t].t, payload->cells[t].s, payload->cells[t].o);
if (t == -1) fprintf(stdout,"%ld", (long unsigned int) payload->rowid);
if (t>=0) { fprintf(stdout,",");
switch (payload->cells[t].t) {
case 0: fprintf(stdout,"NULL"); break;