-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths_indexes.c
More file actions
103 lines (96 loc) · 2.39 KB
/
s_indexes.c
File metadata and controls
103 lines (96 loc) · 2.39 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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include "cbuffer.h"
#include <inttypes.h>
int debug = 0;
void usage(char* p){
fprintf(stderr, "Usage: \n\
%s /path/to/ibdata table_id\n", p);
}
int main(int argc, char** argv)
{
unsigned long int pattern_size = 12;
byte pattern[pattern_size];
unsigned long int cb_buffer_size = 10*1024*1024;
unsigned long int i = 0, j, pos;
uint64_t table_id = 0;
uint64_t index_id = 0;
if(argc != 3){
usage(argv[0]);
exit(EXIT_FAILURE);
}
FILE *f = fopen(argv[1], "r");
if(f == NULL){
fprintf(stderr, "Can't open file '%s'\n", argv[1]);
perror("fopen()");
usage(argv[0]);
exit(EXIT_FAILURE);
}
pattern[0] = 0x00;
pattern[1] = 0x08;
table_id = (uint64_t)strtoull(argv[2], NULL, 10);
for(i = 0; i < 8; i++){
byte r = (table_id >> 8*(7 - i) );
pattern[2 + i] = r;
if(debug) fprintf(stderr, "pattern[%lu] = %02X\n", 2 + i, pattern[2 + i]);
}
pattern[10] = 0x01;
pattern[11] = 0x08;
if(debug){
fprintf(stderr, "pattern size: %lu\n", pattern_size);
fprintf(stderr, "pattern\nascii: ");
for(i = 0; i < pattern_size; i++){
fprintf(stderr, "%c", pattern[i]);
}
fprintf(stderr, "\n");
fprintf(stderr, "hex: ");
for(i = 0; i < pattern_size; i++){
fprintf(stderr, "%02X ", pattern[i]);
}
fprintf(stderr, "\n");
}
CircularBuffer cb;
ElemType elem;
elem.value = 0;
cbInit(&cb, cb_buffer_size);
int pattern_found = 0;
while(!feof(f)){
pos = cbGetPos(&cb);
pattern_found = 1;
for(j = 0; j < pattern_size; j++){
if(cbIsEmpty(&cb)){
cbFill(&cb, f);
}
if(!cbIsEmpty(&cb)){
cbRead(&cb, &elem);
if(pattern[j] != elem.value){
pattern_found = 0;
}
}
}
if(pattern_found){
if(debug) fprintf(stderr, "Pattern found\n");
// pattern matches if we reached here
// read next 8 bytes and print
index_id = 0;
for(j = 0; j < 8; j++){
if(cbIsEmpty(&cb)){
cbFill(&cb, f);
}
if(!cbIsEmpty(&cb)){
cbRead(&cb, &elem);
index_id = index_id | (elem.value << (7-j));
}
}
printf("%" PRIu32 "-%" PRIu32 "\n", (uint32_t)(index_id >> 32), (uint32_t)(index_id & 0x00000000FFFFFFFF));
}
else{
if(debug) fprintf(stderr, "Pattern not found\n");
cbSetPos(&cb, pos+1);
}
}
return EXIT_SUCCESS;
}