-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.rl
127 lines (109 loc) · 2.51 KB
/
reader.rl
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
#include "globals.h"
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#
#define MAX_LEN 1024
#define MAX_WORD_SIZE 10240
#
int counter;
char temp[MAX_WORD_SIZE];
char *buff, *buff1;
char filename[256];
struct stat s, s1;
int offset;
int match_len;
int match;
int lhs_context = 25;
int rhs_context = 50;
typedef struct output {
char filename[256];
XXH64_hash_t hash;
int offset;
int length;
} OUTPUT;
/* filename hash offset and length */
int scan(const char *in);
%%{
machine strings;
main := |*
"./"(any - '\n') + '\n' => {
memset(test_data.filename, '\0', 256);
strncpy(test_data.filename, &buff[ts - in], te - ts - 1);
};
xdigit{16} => {
char temp[te - ts + 1];
memset(temp, '\0', te - ts + 1);
strncpy(temp, &buff[ts - in], te - ts);
if (!cmp_Canonical_XXH64(temp, XXH64("derivation", 10, 0))) {
match = 1;
}
};
[ ]digit+[ ]{2} => {
if (match) {
char temp[te - ts + 1];
memset(temp, '\0', te - ts + 1);
strncpy(temp, &buff[(ts + 1) - in], te - ts - 3);
test_data.offset = atoi(temp);
}
};
digit+'\n' => {
if (match) {
char temp[te - ts + 1];
memset(temp, '\0', te - ts + 1);
strncpy(temp, &buff[ts - in], te - ts - 1);
printf("%d", test_data.offset);
print_context(test_data.filename, test_data.offset, 10, lhs_context,
rhs_context);
match = 0;
}
};
any;
*| ;
}%%
%% write data;
int scan(const char *in) {
OUTPUT test_data;
int cs = 0;
int act = 0;
const char *p = in;
const char *pe = in + strlen(in);
const char *ts = NULL, *te = NULL;
const char *eof = pe;
%% write init;
%% write exec;
if (cs == strings_error)
printf("Error near %zd\n", p - in);
else if (ts)
printf("offsets: ts %zd te: %zd pe: %zd\n", ts - in, te - in, pe - in);
return EXIT_SUCCESS;
}
int reader(const char *source) {
strncpy(filename, source, 256);
int fd;
fd = open(source, O_RDONLY);
if (fd < 0)
return EXIT_FAILURE;
fstat(fd, &s);
/* PROT_READ disallows writing to buff: will segv */
buff = mmap(NULL, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (buff != (void *)-1) {
scan((char *)buff);
munmap(buff, s.st_size);
}
close(fd);
/* printf("\n\tcounts:%d\n",counter); */
return 0;
}