Skip to content

Commit

Permalink
Merge pull request #14 from Zachareee/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Zachareee authored Aug 7, 2023
2 parents 8d1b14a + b1d8cd2 commit ddb7783
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 84 deletions.
40 changes: 38 additions & 2 deletions .github/workflows/make.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
linux-build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

Expand All @@ -29,7 +29,7 @@ jobs:
id: version
with:
scheme: semver
increment: patch
increment: minor

- name: Build
run: echo "char *version = \"${{ steps.version.outputs.version }}\";" > headers/version.h && make && ls
Expand All @@ -38,6 +38,42 @@ jobs:
uses: ncipollo/release-action@v1
with:
artifacts: "SHA1check"
replacesArtifacts: false
allowUpdates: true
generateReleaseNotes: true
name: "Release v${{ steps.version.outputs.version }}"
tag: "v${{ steps.version.outputs.version }}"

windows-build:
# The type of runner that the job will run on
runs-on: windows-2022

defaults:
run:
shell: bash

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout@v3

- name: Increment version
uses: reecetech/[email protected]
id: version
with:
scheme: semver
increment: minor

- name: Build
run: echo "char *version = \"${{ steps.version.outputs.version }}\";" > headers/version.h && make && ls

- name: Create Release
uses: ncipollo/release-action@v1
with:
artifacts: "SHA1check.exe"
replacesArtifacts: false
allowUpdates: true
generateReleaseNotes: true
name: "Release v${{ steps.version.outputs.version }}"
tag: "v${{ steps.version.outputs.version }}"
15 changes: 13 additions & 2 deletions args.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include "paths.h"
#include "version.h"

#ifdef _WIN32
#define win 1
#else
#define win 0
#endif

char *help_string =
"usage: %s [-h] [-s SOURCE] [-d DESTINATION] path\n\
\n\
Expand Down Expand Up @@ -100,8 +106,13 @@ void parse_args(int argc, char **argv, char *src,
else directory = argv[optind];
}

// realpath(directory, dir); // uncomment for testing
strcpy(dir, directory); // uncomment for production
// for windows, removes " at the end of the path and changes all backslashes
if (win) {
int len = strlen(directory);
if (directory[len - 1] == '\"') directory[len-1] = 0;
}

strcpy(dir, directory);

if (source[0] == ':') strcpy(src, source + 1);
else {
Expand Down
9 changes: 9 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@echo off
for %%i in (*.c) do (
echo Compiling %%i
gcc -c -I headers %%i -o %%~ni.o
echo.
)
gcc *.o -o SHA1check.exe
del *.o
pause
89 changes: 59 additions & 30 deletions compare.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -8,27 +7,66 @@
#include "files.h"
#include "hashing.h"
#include "paths.h"
#define LINELEN 128

regex_t reghex1;
regex_t reghex2;

// init regex sequences
void regex_init() {
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);

int is_hex(char c) {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f');
}

// homemade regex
int matcher(char *str, int ver, int *range) {
int len = strlen(str);
if (ver == 1) {
for (int i = 0; i < len; i++) {
int j = i;
for (; j < len; j++) {
if (!is_hex(str[j])) break;
}

if (j - i == 40) {
if (range) {
range[0] = i;
range[1] = j;
}
return 0;
}
}
} else {
for (int i = 0; i < len; i++) {
int j = 0;
for (; i + j < len && j < 10; j++) {
if (!is_hex(str[i + j])) break;
}

if (j != 10) continue;

for (; i + j < len && j < 13; j++) {
if (str[i + j] != '*') break;
}

if (j != 13) continue;

for (; i + j < len && j < 23; j++) {
if (!is_hex(str[i + j])) break;
}

if (j != 23) continue;
if (range) {
range[0] = i;
range[1] = i + 23;
}

return 0;
}
}
return 1;
}

// 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;
if (!matcher(line, 1, NULL)) return 1;
if (!matcher(line, 2, NULL)) return 2;
return 0;
}

Expand All @@ -51,26 +89,23 @@ void obf_hash(char *line, char *result) {
// 0 if hashsum equal and positive if strcmp fails
int compare(char *dir, char *line, int ver) {
int limit;
regex_t *reg;
regmatch_t pmatch[1];

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

int range[2] = {0};
// find hexstring and copy to hex, return -1 if no matches
int match = regexec(reg, line, 1, pmatch, 0);
int match = matcher(line, ver, range);
if (match) return -1;

char hex[41] = {0};
strncpy(hex, line + pmatch[0].rm_so, limit);
char hex[limit + 1];
strncpy(hex, line + range[0], limit);

// trim line to find file
trim(line, pmatch[0].rm_so);
trim(line, range[0]);

// copy to path and concat
char path[PATH_MAX];
Expand All @@ -91,7 +126,7 @@ int compare(char *dir, char *line, int ver) {
result = (long) strcmp(hash_value, hex);
} else {
long size;
sscanf(line + pmatch[0].rm_eo, "%ld", &size);
sscanf(line + range[1], "%ld", &size);

char temp[24] = {0};
obf_hash(hash_value, temp);
Expand All @@ -105,9 +140,3 @@ int compare(char *dir, char *line, int ver) {
free(hash_value);
return !!result;
}

// frees regex allocs
void free_regex() {
regfree(&reghex1);
regfree(&reghex2);
}
41 changes: 33 additions & 8 deletions getdent.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <dirent.h>
#include <fcntl.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined __unix__
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>

#ifdef __unix__
#define BUF_SIZE 1024
#define define_func void getdent(char *name) { \
int fd, nread; \
Expand Down Expand Up @@ -55,11 +55,36 @@ error:
exit(-4); \
}

#endif
#elif defined _WIN32
#include <Windows.h>

#ifdef __APPLE__
#define define_func void getdent(const char *folder) { \
char path[MAX_PATH]; \
sprintf(path, "%s/*", folder); \
WIN32_FIND_DATA fd; \
HANDLE handle = FindFirstFile(path, &fd); \
if(handle == INVALID_HANDLE_VALUE) return; \
do { \
if(!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..")) continue; \
sprintf(path, "%s/%s", folder, fd.cFileName); \
\
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) getdent(path); \
else { \
if (!strcmp(path, src) || !strcmp(path, dst)) continue; \
if (path_exists(pass, get_relative_path(dir, path)) \
|| path_exists(fail, get_relative_path(dir, path))) \
continue; \
\
add_path_to_dir(get_relative_path(dir, path), extras); \
(*count)++; \
} \
} while(FindNextFile(handle, &fd)); \
FindClose(handle); \
}

#else
#include <ftw.h>

#define getdent looper
#define define_func int loop_files(const char *path, const struct stat *sb, \
int type, struct FTW* buf) { \
if (type != FTW_F) return 0; \
Expand All @@ -72,7 +97,7 @@ error:
return 0; \
} \
\
void looper(char *name) { \
void getdent(char *name) { \
nftw(name, &loop_files, 1, FTW_PHYS); \
}

Expand Down
Loading

0 comments on commit ddb7783

Please sign in to comment.