68.1.1
by root
added: |
1 |
#include <stdio.h> |
2 |
#include <string.h> |
|
3 |
#include <errno.h> |
|
4 |
#include <stdlib.h> |
|
5 |
#include <stdint.h> |
|
68.1.2
by root
removed redundant code |
6 |
#include "cbuffer.h" |
68.1.3
by root
added usage(), additional sanity checks. |
7 |
#include <inttypes.h> |
68.1.1
by root
added: |
8 |
|
9 |
int debug = 0; |
|
10 |
||
68.1.3
by root
added usage(), additional sanity checks. |
11 |
void usage(char* p){ |
12 |
fprintf(stderr, "Usage: \n\ |
|
13 |
%s /path/to/ibdata db/table\n", p); |
|
14 |
}
|
|
15 |
||
68.1.1
by root
added: |
16 |
int main(int argc, char** argv) |
17 |
{
|
|
18 |
byte* pattern; |
|
19 |
unsigned long int pattern_size; |
|
20 |
unsigned long int cb_buffer_size = 10*1024*1024; |
|
21 |
unsigned long int i = 0, j, pos; |
|
22 |
||
23 |
uint64_t table_id = 0; |
|
24 |
||
68.1.3
by root
added usage(), additional sanity checks. |
25 |
if(argc != 3){ |
26 |
usage(argv[0]); |
|
27 |
exit(EXIT_FAILURE); |
|
28 |
}
|
|
68.1.1
by root
added: |
29 |
FILE *f = fopen(argv[1], "r"); |
30 |
if(f == NULL){ |
|
31 |
fprintf(stderr, "Can't open file '%s'\n", argv[1]); |
|
32 |
perror("fopen()"); |
|
68.1.3
by root
added usage(), additional sanity checks. |
33 |
usage(argv[0]); |
68.1.1
by root
added: |
34 |
exit(EXIT_FAILURE); |
35 |
}
|
|
36 |
if(NULL == (pattern = malloc(strlen(argv[2]) + 2))){ |
|
68.1.3
by root
added usage(), additional sanity checks. |
37 |
fprintf(stderr, "Can't allocate %zu bytes for the pattern", strlen(argv[2])); |
68.1.1
by root
added: |
38 |
perror("malloc()"); |
39 |
exit(EXIT_FAILURE); |
|
40 |
}
|
|
41 |
memcpy(pattern, argv[2], strlen(argv[2])); |
|
42 |
pattern[strlen(argv[2])] = 0x03; |
|
43 |
pattern[strlen(argv[2]) + 1] = 0x08; |
|
44 |
pattern_size = strlen(argv[2]) + 2; |
|
45 |
if(debug){ |
|
46 |
fprintf(stderr, "pattern size: %lu\n", pattern_size); |
|
47 |
fprintf(stderr, "pattern\nascii: "); |
|
48 |
for(i = 0; i < pattern_size; i++){ |
|
49 |
fprintf(stderr, "%c", pattern[i]); |
|
50 |
}
|
|
51 |
fprintf(stderr, "\n"); |
|
52 |
fprintf(stderr, "hex: "); |
|
53 |
for(i = 0; i < pattern_size; i++){ |
|
54 |
fprintf(stderr, "%02X ", pattern[i]); |
|
55 |
}
|
|
56 |
fprintf(stderr, "\n"); |
|
57 |
}
|
|
58 |
||
59 |
CircularBuffer cb; |
|
60 |
ElemType elem; |
|
61 |
elem.value = 0; |
|
62 |
cbInit(&cb, cb_buffer_size); |
|
63 |
||
64 |
int pattern_found = 0; |
|
65 |
while(!feof(f)){ |
|
66 |
pos = cbGetPos(&cb); |
|
67 |
pattern_found = 1; |
|
68 |
for(j = 0; j < pattern_size; j++){ |
|
69 |
if(cbIsEmpty(&cb)){ |
|
70 |
cbFill(&cb, f); |
|
71 |
}
|
|
72 |
if(!cbIsEmpty(&cb)){ |
|
73 |
cbRead(&cb, &elem); |
|
74 |
if(pattern[j] != elem.value){ |
|
75 |
pattern_found = 0; |
|
76 |
}
|
|
77 |
}
|
|
78 |
}
|
|
79 |
if(pattern_found){ |
|
80 |
if(debug) fprintf(stderr, "Pattern found\n"); |
|
81 |
// pattern matches if we reached here
|
|
82 |
// read next 8 bytes and print
|
|
83 |
for(j = 0; j < 8; j++){ |
|
84 |
if(cbIsEmpty(&cb)){ |
|
85 |
cbFill(&cb, f); |
|
86 |
}
|
|
87 |
if(!cbIsEmpty(&cb)){ |
|
88 |
cbRead(&cb, &elem); |
|
89 |
table_id = table_id | (elem.value << i); |
|
90 |
}
|
|
91 |
}
|
|
68.1.3
by root
added usage(), additional sanity checks. |
92 |
printf("%s %" PRIu64 "\n", argv[2], table_id ); |
68.1.1
by root
added: |
93 |
}
|
94 |
else{ |
|
95 |
if(debug) fprintf(stderr, "Pattern not found\n"); |
|
96 |
cbSetPos(&cb, pos+1); |
|
97 |
}
|
|
98 |
}
|
|
99 |
return EXIT_SUCCESS; |
|
100 |
}
|