-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmake_chunks.c
76 lines (65 loc) · 1.82 KB
/
make_chunks.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
#include <errno.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include "sha.h"
#include "chunk.h"
static int get_numchunks(char *);
int main(int argc, char *argv[]) {
// expect one argument atleast.
if( argc < 2 ) {
fprintf(stderr,"usage: %s <input-file>", argv[0]);
exit(-1);
}
char *input_file = argv[1];
FILE *fp = fopen(input_file, "rb");
int numchunks = get_numchunks(input_file), i = 0;
// check for sanity.
if( numchunks < 0 || fp == NULL) {
fprintf(stderr, "Can't stat the file %s: %s\n", input_file, strerror(errno));
exit(-1);
}
// create the array.
uint8_t **hashes = (uint8_t **) malloc(numchunks * sizeof(uint8_t *));
if( hashes == NULL ) {
fprintf(stderr, "Out of memory!!!");
exit(-1);
}
// allocate the individual elements.
for(i=0;i<numchunks;i++) {
hashes[i] = malloc((SHA1_HASH_SIZE)*sizeof(uint8_t));
if( hashes[i] == NULL ) {
fprintf(stderr, "Out of memory!!!");
exit(-1);
}
}
int chunks = make_chunks(fp, hashes);
char ascii[SHA1_HASH_SIZE*2+1]; // the ascii string.
for(i=0;i<chunks;i++) {
hex2ascii(hashes[i], SHA1_HASH_SIZE, ascii);
printf("%d %s\n",i,ascii);
free(hashes[i]);
}
// free the memory.
free(hashes);
// close file.
fclose(fp);
// return success.
return 0;
}
int get_numchunks(char *filename) {
struct stat file;
// the stat returned the file.
if( !stat(filename,&file)) {
// return the number of chunks for the file.
double length = (double) file.st_size;
return ceil(length / BT_CHUNK_SIZE);
} else {
// return error.
return -1;
}
}