forked from basak/ddar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.c
184 lines (157 loc) · 4.81 KB
/
analyze.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
/*
Copyright 2010-2011 True Blue Logic Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <byteswap.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "sqlite3.h"
#include "sha2.h"
#include "scan.h"
struct analyze_ctx {
sqlite3 *db;
sqlite3_stmt *db_insert_stmt;
int64_t offset;
};
static void print_sqlite3_error(sqlite3 *db) {
fputs("sqlite3: ", stderr);
fputs(sqlite3_errmsg(db), stderr);
fputc('\n', stderr);
}
static void store_chunk(struct analyze_ctx *analyze,
const struct scan_chunk_data *chunk_data) {
int i;
sha256_ctx sha256;
unsigned char sha256_digest[32];
char sha256_digest_string[65];
int chunk_size = 0;
sha256_init(&sha256);
for (i=0; i<2; i++) {
chunk_size += chunk_data[i].size;
if (chunk_data[i].buf && chunk_data[i].size)
sha256_update(&sha256, chunk_data[i].buf, chunk_data[i].size);
}
sha256_final(&sha256, sha256_digest);
sprintf(sha256_digest_string, "%016" PRIx64
"%016" PRIx64
"%016" PRIx64
"%016" PRIx64,
bswap_64(*(uint64_t *)&sha256_digest[0]),
bswap_64(*(uint64_t *)&sha256_digest[8]),
bswap_64(*(uint64_t *)&sha256_digest[16]),
bswap_64(*(uint64_t *)&sha256_digest[24]));
if (sqlite3_bind_text(analyze->db_insert_stmt, 1, sha256_digest_string, -1,
SQLITE_STATIC) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
if (sqlite3_bind_int64(analyze->db_insert_stmt, 3, analyze->offset) !=
SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
if (sqlite3_bind_int(analyze->db_insert_stmt, 4, chunk_size) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
if (sqlite3_step(analyze->db_insert_stmt) != SQLITE_DONE) {
print_sqlite3_error(analyze->db);
abort();
}
if (sqlite3_reset(analyze->db_insert_stmt) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
#if 0
printf("%016lx%016lx%016lx%016lx %d\n",
bswap64(*(uint64_t *)&sha256_digest[0]),
bswap64(*(uint64_t *)&sha256_digest[8]),
bswap64(*(uint64_t *)&sha256_digest[16]),
bswap64(*(uint64_t *)&sha256_digest[24]),
chunk_size);
#endif
analyze->offset += chunk_size;
}
struct analyze_ctx *analyze_open(const char *db_filename, const char *tag) {
struct analyze_ctx *analyze;
analyze = malloc(sizeof(struct analyze_ctx));
if (!analyze)
return 0;
analyze->offset = 0;
if (sqlite3_open(db_filename, &analyze->db) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
if (sqlite3_exec(analyze->db, "BEGIN", NULL, NULL, NULL) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
if (sqlite3_prepare(analyze->db, "INSERT INTO chunk (sha256, tag, offset, length) VALUES (?, ?, ?, ?)", -1, &analyze->db_insert_stmt, NULL) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
if (sqlite3_bind_text(analyze->db_insert_stmt, 2, tag, -1,
SQLITE_STATIC) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
return analyze;
}
void analyze_close(struct analyze_ctx *analyze) {
sqlite3_finalize(analyze->db_insert_stmt);
if (sqlite3_exec(analyze->db, "COMMIT", NULL, NULL, NULL) != SQLITE_OK) {
print_sqlite3_error(analyze->db);
abort();
}
}
int main(int argc, char **argv) {
struct scan_ctx *scan;
struct scan_chunk_data chunk_data[2];
struct analyze_ctx *analyze;
int fd, result;
analyze = analyze_open(argv[2], argv[1]);
if (!analyze) {
perror("analyze_init");
return EXIT_FAILURE;
}
scan = scan_init();
if (!scan) {
perror("scan_init");
return EXIT_FAILURE;
}
fd = open(argv[1], O_RDWR);
if (fd < 0) {
perror("open");
return EXIT_FAILURE;
}
scan_set_fd(scan, fd);
scan_set_aio(scan);
if (!scan_begin(scan))
return EXIT_FAILURE;
do {
result = scan_read_chunk(scan, chunk_data);
if (result & SCAN_CHUNK_FOUND)
store_chunk(analyze, chunk_data);
else {
fputs("Scan error\n", stderr);
return EXIT_FAILURE;
}
} while (!(result & SCAN_CHUNK_LAST));
analyze_close(analyze);
scan_free(scan);
return EXIT_SUCCESS;
}
/* vim: set ts=8 sts=4 sw=4 cindent : */