Skip to content

Commit

Permalink
Merge pull request #12 from Zachareee/dev
Browse files Browse the repository at this point in the history
Updated for v2 hashes
  • Loading branch information
Zachareee authored Jul 31, 2023
2 parents b493e3a + 1233aeb commit 1151ccf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ hashes.sha1
*.swp
checksum.txt
*.o
SHA1check*
66 changes: 55 additions & 11 deletions compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,67 @@
#include "paths.h"
#define LINELEN 128

regex_t reghex;
regex_t reghex1;
regex_t reghex2;

// init regex sequences
void regex_init() {
int regh = regcomp(&reghex, "[0-9a-fA-F]{40}", REG_EXTENDED);
if (regh) {
int regh1 = regcomp(&reghex1, "[0-9a-fA-F]{40}", REG_EXTENDED);
int regh2 = regcomp(&reghex2, "[0-9a-fA-F]{10}\\*\\*\\*[0-9a-fA-F]{10}", REG_EXTENDED);
if (regh1 | regh2) {
printf("Something went wrong while initialising pattern matcher\n");
exit(-2);
}
}

// if full hash given, return 1
// if partial hash given in the form of reghex2, returns 2
// if unknown, return 0
int get_hash_ver(char *line) {
if (!regexec(&reghex1, line, 0, NULL, 0)) return 1;
if (!regexec(&reghex2, line, 0, NULL, 0)) return 2;
return 0;
}

// trim trailing whitespaces in the line
void trim(char *line, int offset) {
for (int i = offset - 1; line[i] == ' ' || line[i] == '\t'; i--) {
line[i] = 0;
}
}

// obfuscates hash
void obf_hash(char *line, char *result) {
strncpy(result, line, 10);
strncpy(result + 10, "***", 3);
strncpy(result + 13, line + 30, 10);
}

// compare hash of file against recorded hash
// returns -1 if no hash found, -2 if file not found
// 0 if hashsum equal and positive if strcmp fails
int compare(char *dir, char *line) {
regmatch_t pmatch[1];
int compare(char *dir, char *line, int ver) {
long result;
char *hash_value;
char hex[41];
hex[40] = 0;
regmatch_t pmatch[1];
int limit;
regex_t *reg;

if (ver == 1) {
limit = 40;
reg = &reghex1;
} else {
limit = 23;
reg = &reghex2;
}

hex[limit] = 0;

// find hexstring and copy to hex, return -1 if no matches
int match = regexec(&reghex, line, 1, pmatch, 0);
int match = regexec(reg, line, 1, pmatch, 0);
if (match) return -1;
strncpy(hex, line + pmatch[0].rm_so, 40);
strncpy(hex, line + pmatch[0].rm_so, limit);

// trim line to find file
trim(line, pmatch[0].rm_so);
Expand All @@ -56,8 +87,20 @@ int compare(char *dir, char *line) {
struct stat s;
stat(path, &s);

char *hash_value = hash(path, s.st_size);
int result = strcmp(hash_value, hex);
hash_value = hash(path, s.st_size);

if (ver == 1) {
result = (long) strcmp(hash_value, hex);
} else {
long size;
sscanf(line + pmatch[0].rm_eo, "%ld", &size);

char temp[24];
obf_hash(hash_value, temp);
result = (long) strcmp(temp, hex);
result |= s.st_size - size;
}

fprintf(stderr, "OK\n");

// free malloc-ed variables
Expand All @@ -67,5 +110,6 @@ int compare(char *dir, char *line) {

// frees regex allocs
void free_regex() {
regfree(&reghex);
regfree(&reghex1);
regfree(&reghex2);
}
3 changes: 2 additions & 1 deletion headers/compare.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
int compare(char *dir, char *line);
int compare(char *dir, char *line, int version);
void regex_init();
void free_regex();
int get_hash_ver(char *line);
23 changes: 18 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,24 @@ int main(int argc, char **argv) {
regex_init();

FILE *hashfile = fopen(src, "r");

int version = 0;
char *line;
while ((line = get_line(hashfile)) && !version) {
version = get_hash_ver(line);
free(line);
if (version) goto ver;
}

printf("Could not find hashsum version, exiting...\n");
return -5;

ver:

fprintf(stderr, "Version %d hashfile detected\n", version);
fseek(hashfile, 0, SEEK_SET);
FILE *checkfile = fopen(dst, "w");

char *line = get_line(hashfile);
char ptr[PATH_MAX];
char *state;
// 0: OK, 1: MISSING, 2: FAILED, 3: EXTRA
Expand All @@ -58,12 +73,10 @@ int main(int argc, char **argv) {
failed = calloc(1, sizeof(dir_t));
extra = calloc(1, sizeof(dir_t));

while (line) {
while ((line = get_line(hashfile))) {
strcpy(ptr, line);
free(line);
int c = compare(dir, ptr);

line = get_line(hashfile);
int c = compare(dir, ptr, version);

if (c == -1) continue;

Expand Down

0 comments on commit 1151ccf

Please sign in to comment.