mirror of
https://github.com/hashcat/hashcat.git
synced 2024-12-22 06:38:11 +00:00
- Added Makefile target for native compilation
- Moved rules_optimize tool to hashcat-utils
This commit is contained in:
parent
5fa4d9918b
commit
2be8f5f982
@ -10,10 +10,18 @@ File.: Host
|
||||
Desc.: Implemented a new feature that allows to quit at next restore point update (and disable it)
|
||||
Issue: 10
|
||||
|
||||
Type.: Feature
|
||||
File.: Host
|
||||
Desc.: Added a Makefile target for native compilation
|
||||
|
||||
Type.: Feature
|
||||
File.: Host
|
||||
Desc.: Added option --gpu-platform to select a single OpenCL platform in case multiple OpenCL platforms are present
|
||||
|
||||
Type.: Feature
|
||||
File.: Host
|
||||
Desc.: Moved rules_optimize to hashcat-utils
|
||||
|
||||
Type.: Bug
|
||||
File.: Host
|
||||
Desc.: Fixed a bug in combination of --restore and a user immediately aborting the session after restart
|
||||
|
77
src/Makefile
77
src/Makefile
@ -54,11 +54,16 @@ OCLLIBPATH64 := $(AMDAPPLIBPATH64)
|
||||
|
||||
NVML := $(GDK)/usr/include/nvidia/gdk
|
||||
|
||||
CLCOMPILE_PATH := tools/clcompile
|
||||
RULES_OPTIMIZE_PATH := tools/rules_optimize
|
||||
##
|
||||
## Native compiler paths
|
||||
##
|
||||
|
||||
INSTALL := install
|
||||
CP := cp
|
||||
CC_NATIVE := gcc
|
||||
|
||||
##
|
||||
## Compiler paths
|
||||
## Cross compiler paths
|
||||
##
|
||||
|
||||
CC_LINUX_32 := gcc
|
||||
@ -70,13 +75,6 @@ CC_WIN_64 := x86_64-w64-mingw32-gcc
|
||||
DLL_WIN_32 := i686-w64-mingw32-dlltool
|
||||
DLL_WIN_64 := x86_64-w64-mingw32-dlltool
|
||||
|
||||
CLCOMPILE := $(CLCOMPILE_PATH)/clcompile.bin
|
||||
|
||||
BIN := .
|
||||
|
||||
INSTALL := install
|
||||
CP := cp
|
||||
|
||||
##
|
||||
## Misc stuff
|
||||
##
|
||||
@ -102,38 +100,21 @@ CFLAGS_WIN += -I$(OCL)/include/ -I$(ADL)/include/ -I$(NVAPI)/
|
||||
LFLAGS_LINUX := -lpthread -lOpenCL -ldl
|
||||
LFLAGS_WIN := -lpsapi
|
||||
|
||||
CFLAGS_NATIVE := $(CFLAGS_LINUX)
|
||||
LFLAGS_NATIVE := $(LFLAGS_LINUX)
|
||||
|
||||
##
|
||||
## Targets: Global
|
||||
##
|
||||
|
||||
all: binaries_all
|
||||
native: oclHashcat
|
||||
|
||||
binaries_all: linux32 linux64 win32 win64 rules_optimize
|
||||
binaries: linux32 linux64 win32 win64
|
||||
|
||||
release: binaries
|
||||
|
||||
linux: linux32 linux64 rules_optimize
|
||||
|
||||
windows: win32 win64 rules_optimize
|
||||
|
||||
clean: clean_bin
|
||||
|
||||
clean_all: clean_bin clean_clcompile
|
||||
|
||||
clean_bin:
|
||||
rm -f obj/*.o lib/*.a $(BIN)/*.bin $(BIN)/*.exe $(BIN)/*.app *.restore *.out *.pot *.dictstat *.log
|
||||
rm -rf *Hashcat.induct
|
||||
rm -rf *Hashcat.outfiles
|
||||
$(MAKE) -C $(RULES_OPTIMIZE_PATH) clean
|
||||
|
||||
clean_clcompile:
|
||||
rm -f ${CLCOMPILE_PATH}/clcompile.bin
|
||||
|
||||
${CLCOMPILE}: ${CLCOMPILE_PATH}/clcompile.c
|
||||
$(CC_LINUX_64) $(CFLAGS) -o $@ -I$(CFLAGS_LINUX) $< -L$(OCLLIBPATH64) $(LFLAGS_LINUX)
|
||||
|
||||
rules_optimize:
|
||||
$(MAKE) -C $(RULES_OPTIMIZE_PATH)
|
||||
clean:
|
||||
rm -f obj/*.o lib/*.a ./*.bin ./*.exe ./*.app *.restore *.out *.pot *.dictstat *.log oclHashcat
|
||||
rm -rf *.induct
|
||||
rm -rf *.outfiles
|
||||
|
||||
linux32: oclHashcat32.bin
|
||||
linux64: oclHashcat64.bin
|
||||
@ -143,11 +124,10 @@ win64: oclHashcat64.exe
|
||||
|
||||
##
|
||||
## Targets: Linux install
|
||||
## We need some better native compilation logic here, fixing it to 64 bit for now and AMD kernels
|
||||
## How to make /usr/bin/install doing recursive??
|
||||
##
|
||||
|
||||
install: linux64 amd_all
|
||||
install: native
|
||||
$(INSTALL) -m 755 -d $(DOCUMENT_FOLDER)
|
||||
$(CP) -a docs/* $(DOCUMENT_FOLDER)/
|
||||
$(INSTALL) -m 755 -d $(DOCUMENT_FOLDER)/extra
|
||||
@ -162,7 +142,6 @@ install: linux64 amd_all
|
||||
$(INSTALL) -m 755 -T hashcat.hcstat $(SHARED_FOLDER)/hashcat.hcstat
|
||||
$(INSTALL) -m 755 -s -T oclHashcat64.bin $(INSTALL_FOLDER)/oclHashcat
|
||||
|
||||
|
||||
uninstall:
|
||||
$(RM) -f $(INSTALL_FOLDER)/oclHashcat
|
||||
$(RM) -rf $(SHARED_FOLDER)
|
||||
@ -179,7 +158,17 @@ lib/libOpenCL64.a:
|
||||
${DLL_WIN_64} -A -k -l lib/libOpenCL64.a -d lib/OpenCL64.def
|
||||
|
||||
##
|
||||
## oclHashcat
|
||||
## native compiled oclHashcat
|
||||
##
|
||||
|
||||
obj/%.oclHashcat.NATIVE.o: src/%.c
|
||||
$(CC_NATIVE) $(CFLAGS) $(CFLAGS_NATIVE) -c -o $@ $<
|
||||
|
||||
oclHashcat: src/oclHashcat.c obj/ext_OpenCL.oclHashcat.NATIVE.o obj/ext_nvml.oclHashcat.NATIVE.o obj/ext_ADL.oclHashcat.NATIVE.o obj/shared.oclHashcat.NATIVE.o obj/rp_gpu_on_cpu.oclHashcat.NATIVE.o
|
||||
$(CC_NATIVE) $(CFLAGS) $(CFLAGS_NATIVE) -o $@ $^ $(LFLAGS_NATIVE) -DCOMPTIME=$(NOW) -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\"
|
||||
|
||||
##
|
||||
## cross compiled oclHashcat for release
|
||||
##
|
||||
|
||||
obj/%.oclHashcat.LINUX.32.o: src/%.c
|
||||
@ -195,13 +184,13 @@ obj/%.oclHashcat.WIN.64.o: src/%.c
|
||||
$(CC_WIN_64) $(CFLAGS) $(CFLAGS_WIN) $(CFLAGS_64) -c -o $@ $<
|
||||
|
||||
oclHashcat32.bin: src/oclHashcat.c obj/ext_OpenCL.oclHashcat.LINUX.32.o obj/ext_nvml.oclHashcat.LINUX.32.o obj/ext_ADL.oclHashcat.LINUX.32.o obj/shared.oclHashcat.LINUX.32.o obj/rp_gpu_on_cpu.oclHashcat.LINUX.32.o
|
||||
$(CC_LINUX_32) $(CFLAGS) $(CFLAGS_LINUX) $(CFLAGS_32) -o $@ -DCOMPTIME=$(NOW) $^ -L$(OCLLIBPATH32) $(LFLAGS_LINUX) -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\"
|
||||
$(CC_LINUX_32) $(CFLAGS) $(CFLAGS_LINUX) $(CFLAGS_32) -o $@ $^ -L$(OCLLIBPATH32) $(LFLAGS_LINUX) -DCOMPTIME=$(NOW) -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\"
|
||||
|
||||
oclHashcat64.bin: src/oclHashcat.c obj/ext_OpenCL.oclHashcat.LINUX.64.o obj/ext_nvml.oclHashcat.LINUX.64.o obj/ext_ADL.oclHashcat.LINUX.64.o obj/shared.oclHashcat.LINUX.64.o obj/rp_gpu_on_cpu.oclHashcat.LINUX.64.o
|
||||
$(CC_LINUX_64) $(CFLAGS) $(CFLAGS_LINUX) $(CFLAGS_64) -o $@ -DCOMPTIME=$(NOW) $^ -L$(OCLLIBPATH64) $(LFLAGS_LINUX) -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\"
|
||||
$(CC_LINUX_64) $(CFLAGS) $(CFLAGS_LINUX) $(CFLAGS_64) -o $@ $^ -L$(OCLLIBPATH64) $(LFLAGS_LINUX) -DCOMPTIME=$(NOW) -DINSTALL_FOLDER=\"$(INSTALL_FOLDER)\" -DSHARED_FOLDER=\"$(SHARED_FOLDER)\" -DDOCUMENT_FOLDER=\"$(DOCUMENT_FOLDER)\"
|
||||
|
||||
oclHashcat32.exe: src/oclHashcat.c obj/ext_OpenCL.oclHashcat.WIN.32.o obj/ext_nvapi.oclHashcat.WIN.32.o obj/ext_ADL.oclHashcat.WIN.32.o obj/shared.oclHashcat.WIN.32.o obj/rp_gpu_on_cpu.oclHashcat.WIN.32.o lib/libOpenCL.a /usr/i686-w64-mingw32/lib/CRT_glob.o $(NVAPI)/x86/nvapi.lib
|
||||
$(CC_WIN_32) $(CFLAGS) $(CFLAGS_WIN) $(CFLAGS_32) -o $@ -DCOMPTIME=$(NOW) $^ -L$(OCLLIBPATH32) $(LFLAGS_WIN) -static-libgcc
|
||||
$(CC_WIN_32) $(CFLAGS) $(CFLAGS_WIN) $(CFLAGS_32) -o $@ $^ -L$(OCLLIBPATH32) $(LFLAGS_WIN) -DCOMPTIME=$(NOW) -static-libgcc
|
||||
|
||||
oclHashcat64.exe: src/oclHashcat.c obj/ext_OpenCL.oclHashcat.WIN.64.o obj/ext_nvapi.oclHashcat.WIN.64.o obj/ext_ADL.oclHashcat.WIN.64.o obj/shared.oclHashcat.WIN.64.o obj/rp_gpu_on_cpu.oclHashcat.WIN.64.o lib/libOpenCL64.a /usr/x86_64-w64-mingw32/lib/CRT_glob.o $(NVAPI)/amd64/nvapi64.lib
|
||||
$(CC_WIN_64) $(CFLAGS) $(CFLAGS_WIN) $(CFLAGS_64) -o $@ -DCOMPTIME=$(NOW) $^ -L$(OCLLIBPATH64) $(LFLAGS_WIN) -static-libgcc
|
||||
$(CC_WIN_64) $(CFLAGS) $(CFLAGS_WIN) $(CFLAGS_64) -o $@ $^ -L$(OCLLIBPATH64) $(LFLAGS_WIN) -DCOMPTIME=$(NOW) -static-libgcc
|
||||
|
@ -1,254 +0,0 @@
|
||||
/**
|
||||
* Author......: Jens Steube <jens.steube@gmail.com>
|
||||
* License.....: MIT
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <CL/cl.h>
|
||||
|
||||
#define CL_PLATFORMS_MAX 128
|
||||
#define CL_DEVICES_MAX 128
|
||||
|
||||
//char options[] = "–x spir -spir-std=1.2 -I.";
|
||||
char options[] = "-I. -x clc++ -cl-std=CL1.2";
|
||||
|
||||
static void checkErr (char *func, cl_int err)
|
||||
{
|
||||
if (err != CL_SUCCESS)
|
||||
{
|
||||
fprintf (stderr, "%s(): ", func);
|
||||
|
||||
switch (err)
|
||||
{
|
||||
case CL_BUILD_PROGRAM_FAILURE :
|
||||
fprintf (stderr, "CL_BUILD_PROGRAM_FAILURE");
|
||||
break;
|
||||
|
||||
case CL_COMPILER_NOT_AVAILABLE :
|
||||
fprintf (stderr, "CL_COMPILER_NOT_AVAILABLE");
|
||||
break;
|
||||
|
||||
case CL_DEVICE_NOT_AVAILABLE :
|
||||
fprintf (stderr, "CL_DEVICE_NOT_AVAILABLE");
|
||||
break;
|
||||
|
||||
case CL_DEVICE_NOT_FOUND :
|
||||
fprintf (stderr, "CL_DEVICE_NOT_FOUND");
|
||||
break;
|
||||
|
||||
case CL_INVALID_BINARY :
|
||||
fprintf (stderr, "CL_INVALID_BINARY");
|
||||
break;
|
||||
|
||||
case CL_INVALID_BUILD_OPTIONS :
|
||||
fprintf (stderr, "CL_INVALID_BUILD_OPTIONS");
|
||||
break;
|
||||
|
||||
case CL_INVALID_CONTEXT :
|
||||
fprintf (stderr, "CL_INVALID_CONTEXT");
|
||||
break;
|
||||
|
||||
case CL_INVALID_DEVICE :
|
||||
fprintf (stderr, "CL_INVALID_DEVICE");
|
||||
break;
|
||||
|
||||
case CL_INVALID_DEVICE_TYPE :
|
||||
fprintf (stderr, "CL_INVALID_DEVICE_TYPE");
|
||||
break;
|
||||
|
||||
case CL_INVALID_OPERATION :
|
||||
fprintf (stderr, "CL_INVALID_OPERATION");
|
||||
break;
|
||||
|
||||
case CL_INVALID_PLATFORM :
|
||||
fprintf (stderr, "CL_INVALID_PLATFORM");
|
||||
break;
|
||||
|
||||
case CL_INVALID_PROGRAM :
|
||||
fprintf (stderr, "CL_INVALID_PROGRAM");
|
||||
break;
|
||||
|
||||
case CL_INVALID_VALUE :
|
||||
fprintf (stderr, "CL_INVALID_VALUE");
|
||||
break;
|
||||
|
||||
case CL_OUT_OF_HOST_MEMORY :
|
||||
fprintf (stderr, "CL_OUT_OF_HOST_MEMORY");
|
||||
break;
|
||||
|
||||
default :
|
||||
fprintf (stderr, "Unknown error code: %d", err);
|
||||
break;
|
||||
}
|
||||
|
||||
fprintf (stderr, "\n");
|
||||
|
||||
exit (err);
|
||||
}
|
||||
}
|
||||
|
||||
static char *load_kernel (const char *kernel_file)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
|
||||
if ((fp = fopen (kernel_file, "rb")) != NULL)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (stat (kernel_file, &st) == -1)
|
||||
{
|
||||
fprintf (stderr, "! stat() failed (%d) : %s\n", errno, strerror (errno));
|
||||
}
|
||||
|
||||
char *buf = (char *) malloc (st.st_size + 1);
|
||||
|
||||
memset (buf, 0, st.st_size + 1);
|
||||
|
||||
size_t num_read = fread (buf, sizeof (char), st.st_size, fp);
|
||||
|
||||
if (num_read != (size_t) st.st_size)
|
||||
{
|
||||
fprintf (stderr, "! fread() [%s] failed (%d) : %s", kernel_file, errno, strerror (errno));
|
||||
fclose (fp);
|
||||
exit (-1);
|
||||
}
|
||||
|
||||
fclose (fp);
|
||||
|
||||
return buf;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "! fopen() [%s] failed (%d) : %s", kernel_file, errno, strerror (errno));
|
||||
exit (-1);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int writeProgramBins (char *dst, unsigned char *binary, size_t binary_size)
|
||||
{
|
||||
FILE *fp = fopen (dst, "wb");
|
||||
|
||||
if (!fp)
|
||||
{
|
||||
fprintf(stderr, "! fopen() [%s] failed (%d) : %s\n", dst, errno, strerror (errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite (binary, sizeof (unsigned char), binary_size, fp) != binary_size)
|
||||
{
|
||||
fprintf(stderr, "! fwrite() [%s] failed (%d) : %s\n", dst, errno, strerror (errno));
|
||||
fclose (fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fclose (fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf (stderr, "> Usage: %s ccopts src dst\n", argv[0]);
|
||||
|
||||
return (-1);
|
||||
}
|
||||
|
||||
char *ccopts = argv[1];
|
||||
char *src = argv[2];
|
||||
char *dst = argv[3];
|
||||
|
||||
char *programSrc = load_kernel (src);
|
||||
|
||||
if (programSrc == NULL)
|
||||
{
|
||||
fprintf (stderr, "Unable to open %s. Exiting.\n", src);
|
||||
|
||||
return (-1);
|
||||
}
|
||||
|
||||
cl_device_id devices[1];
|
||||
cl_uint nDevices;
|
||||
|
||||
cl_platform_id platform;
|
||||
cl_uint platforms;
|
||||
|
||||
cl_int err;
|
||||
|
||||
err = clGetPlatformIDs(1, &platform, &platforms);
|
||||
|
||||
checkErr ((char *) "clGetPlatformIDs", err);
|
||||
|
||||
err = clGetDeviceIDs (platform, CL_DEVICE_TYPE_GPU, 1, devices, &nDevices);
|
||||
|
||||
checkErr ((char *) "clGetDeviceIDs", err);
|
||||
|
||||
cl_context context = clCreateContext (NULL, 1, devices, NULL, NULL, &err);
|
||||
|
||||
checkErr ((char *) "clCreateContext", err);
|
||||
|
||||
cl_program program = clCreateProgramWithSource (context, 1, (const char **) &programSrc, NULL, &err);
|
||||
|
||||
checkErr ((char *) "clCreateProgramWithSource", err);
|
||||
|
||||
size_t opt_len = strlen (ccopts) + 1 + strlen (options) + 1;
|
||||
|
||||
char *options2 = (char *) malloc (opt_len + 1);
|
||||
|
||||
memset (options2, 0, opt_len + 1);
|
||||
|
||||
snprintf (options2, opt_len, "%s %s", options, ccopts);
|
||||
|
||||
err = clCompileProgram (program, 1, devices, options2, 0, NULL, NULL, NULL, NULL);
|
||||
|
||||
//checkErr ((char *) "clCompileProgram", err);
|
||||
|
||||
size_t ret_val_size = 0;
|
||||
|
||||
err = clGetProgramBuildInfo (program, devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
|
||||
|
||||
checkErr ((char *) "clGetProgramBuildInfo", err);
|
||||
|
||||
if (ret_val_size > 1)
|
||||
{
|
||||
char *build_log = (char *) malloc (ret_val_size + 1);
|
||||
|
||||
memset (build_log, 0, ret_val_size + 1);
|
||||
|
||||
err = clGetProgramBuildInfo (program, devices[0], CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
|
||||
|
||||
checkErr ((char *) "clGetProgramBuildInfo", err);
|
||||
|
||||
puts (build_log);
|
||||
}
|
||||
|
||||
size_t binary_size;
|
||||
|
||||
err = clGetProgramInfo (program, CL_PROGRAM_BINARY_SIZES, sizeof (size_t), &binary_size, NULL);
|
||||
|
||||
checkErr ((char *) "clGetProgramInfo", err);
|
||||
|
||||
unsigned char *binary = (unsigned char *) malloc (binary_size);
|
||||
|
||||
memset(binary, 0, binary_size);
|
||||
|
||||
err = clGetProgramInfo (program, CL_PROGRAM_BINARIES, sizeof (binary), &binary, NULL);
|
||||
|
||||
checkErr ((char *) "clGetProgramInfo", err);
|
||||
|
||||
err = writeProgramBins (dst, binary, binary_size);
|
||||
|
||||
checkErr ((char *) "writeProgramBins", err);
|
||||
|
||||
return 0;
|
||||
}
|
@ -58,8 +58,6 @@ chmod 700 $OUT/example*.sh
|
||||
chmod 700 $OUT/extra
|
||||
chmod 700 $OUT/extra/tab_completion/*.sh
|
||||
chmod 700 $OUT/extra/tab_completion/install
|
||||
chmod 600 $OUT/extra/rules_optimize/*.exe
|
||||
chmod 700 $OUT/extra/rules_optimize/*.bin
|
||||
chmod 700 $OUT/include
|
||||
chmod 600 $OUT/include/*
|
||||
chmod 700 $OUT/OpenCL
|
||||
|
@ -1,22 +0,0 @@
|
||||
##
|
||||
## Author......: Jens Steube <jens.steube@gmail.com>
|
||||
## License.....: MIT
|
||||
##
|
||||
|
||||
GCC ?= /usr/bin/x86_64-linux-gnu-gcc
|
||||
MINGW ?= i686-w64-mingw32-gcc
|
||||
ROOT := ../..
|
||||
CFLAGS := -O2 -s -std=c99 -pipe -W -Wall -I$(ROOT)/include/ -I../rules_test/
|
||||
SRC := ./rules_optimize.c
|
||||
TARGET := $(ROOT)/extra/rules_optimize/rules_optimize
|
||||
|
||||
all: ${TARGET}.bin ${TARGET}.exe
|
||||
|
||||
${TARGET}.bin: $(SRC)
|
||||
${GCC} ${CFLAGS} ../rules_test/cpu_rules.c $< -o ${TARGET}.bin
|
||||
|
||||
${TARGET}.exe: $(SRC)
|
||||
${MINGW} ${CFLAGS} ../rules_test/cpu_rules.c $< -o ${TARGET}.exe
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)*.bin $(TARGET)*.exe
|
@ -1,255 +0,0 @@
|
||||
/**
|
||||
* Author......: Jens Steube <jens.steube@gmail.com>
|
||||
* License.....: MIT
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MIN_FUNCTIONS 1
|
||||
#define MAX_FUNCTIONS 5
|
||||
|
||||
int max_len = 0;
|
||||
|
||||
#include "cpu_rules.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char rule_buf[BLOCK_SIZE];
|
||||
int rule_len;
|
||||
|
||||
} rule_t;
|
||||
|
||||
static int cmp (const void *p1, const void *p2)
|
||||
{
|
||||
rule_t *r1 = (rule_t *) p1;
|
||||
rule_t *r2 = (rule_t *) p2;
|
||||
|
||||
return r1->rule_len - r2->rule_len;
|
||||
}
|
||||
|
||||
int process_block (int o[BLOCK_SIZE], char *block_ptr[BLOCK_SIZE], int block_cnt, char *word_buf, char final_buf[BLOCK_SIZE], int final_len, char rule_buf[BLOCK_SIZE])
|
||||
{
|
||||
int last_o = o[0];
|
||||
|
||||
for (int i = 1; i < block_cnt; i++)
|
||||
{
|
||||
if (o[i] < last_o) return (0);
|
||||
}
|
||||
|
||||
memset (rule_buf, 0, BLOCK_SIZE);
|
||||
|
||||
strcat (rule_buf, block_ptr[o[0]]);
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 1; i < block_cnt; i++)
|
||||
{
|
||||
strcat (rule_buf, " ");
|
||||
|
||||
strcat (rule_buf, block_ptr[o[i]]);
|
||||
}
|
||||
|
||||
char out_buf[BLOCK_SIZE];
|
||||
|
||||
memset (out_buf, 0, sizeof (out_buf));
|
||||
|
||||
int out_len = apply_rule_cpu (rule_buf, strlen (rule_buf), word_buf, strlen (word_buf), out_buf);
|
||||
|
||||
if (out_len == final_len)
|
||||
{
|
||||
if (memcmp (final_buf, out_buf, out_len) == 0) return (1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int next_permutation (int *o, int *p, int k)
|
||||
{
|
||||
p[k]--;
|
||||
|
||||
int j = k % 2 * p[k];
|
||||
|
||||
int tmp = o[j];
|
||||
|
||||
o[j] = o[k];
|
||||
|
||||
o[k] = tmp;
|
||||
|
||||
for (k = 1; p[k] == 0; k++) p[k] = k;
|
||||
|
||||
return k;
|
||||
}
|
||||
|
||||
int main ()
|
||||
{
|
||||
FILE *fp = stdin;
|
||||
|
||||
char line_buf[BUFSIZ];
|
||||
|
||||
while (!feof (fp))
|
||||
{
|
||||
/*
|
||||
* line
|
||||
*/
|
||||
|
||||
char *line_ptr = fgets (line_buf, BUFSIZ, fp);
|
||||
|
||||
if (line_ptr == NULL) continue;
|
||||
|
||||
int line_len = strlen (line_ptr);
|
||||
|
||||
if (line_len && line_ptr[line_len - 1] == '\n') line_len--;
|
||||
if (line_len && line_ptr[line_len - 1] == '\r') line_len--;
|
||||
|
||||
line_ptr[line_len] = 0;
|
||||
|
||||
/*
|
||||
* split
|
||||
*/
|
||||
|
||||
char *word_buf = line_ptr;
|
||||
|
||||
char *sep = strchr (line_ptr, ':');
|
||||
|
||||
if (sep == NULL) continue;
|
||||
|
||||
*sep = 0;
|
||||
|
||||
int word_len = sep - word_buf;
|
||||
|
||||
if (strstr (word_buf, "$HEX[")) continue; // not yet supported
|
||||
|
||||
char *rule_buf = sep + 1;
|
||||
|
||||
if (strchr (rule_buf, ':')) continue; // another one? ignore line
|
||||
|
||||
/*
|
||||
* final
|
||||
*/
|
||||
|
||||
char final_buf[BLOCK_SIZE];
|
||||
|
||||
memset (final_buf, 0, sizeof (final_buf));
|
||||
|
||||
int final_len = apply_rule_cpu (rule_buf, strlen (rule_buf), word_buf, strlen (word_buf), final_buf);
|
||||
|
||||
if (final_len < 0) continue;
|
||||
|
||||
if ((final_len == word_len) && (memcmp (word_buf, final_buf, final_len)) == 0) continue;
|
||||
|
||||
/*
|
||||
* split into blocks
|
||||
*/
|
||||
|
||||
char *block_ptr[BLOCK_SIZE];
|
||||
int block_cnt = 0;
|
||||
|
||||
char *ptr = rule_buf;
|
||||
|
||||
for (char *next = NULL; (next = strchr (ptr, ' ')) != NULL; ptr = next + 1)
|
||||
{
|
||||
if (next[1] == ' ') next++;
|
||||
|
||||
*next = 0;
|
||||
|
||||
block_ptr[block_cnt] = ptr;
|
||||
|
||||
block_cnt++;
|
||||
}
|
||||
|
||||
block_ptr[block_cnt] = ptr;
|
||||
|
||||
block_cnt++;
|
||||
|
||||
if (block_cnt < MIN_FUNCTIONS) continue; // to many
|
||||
if (block_cnt > MAX_FUNCTIONS) continue; // to many
|
||||
|
||||
/*
|
||||
* permute blocks, this where the real work starts..
|
||||
*/
|
||||
|
||||
int o[BLOCK_SIZE];
|
||||
int p[BLOCK_SIZE];
|
||||
|
||||
for (int i = 0; i < block_cnt + 1; i++)
|
||||
{
|
||||
o[i] = i;
|
||||
p[i] = i;
|
||||
}
|
||||
|
||||
int k = 1;
|
||||
|
||||
rule_t *rules_buf = (rule_t *) calloc (120 * MAX_FUNCTIONS, sizeof (rule_t)); // 5! = 120, so its guaranteed
|
||||
int rules_cnt = 0;
|
||||
|
||||
char rule_out_buf[BLOCK_SIZE];
|
||||
|
||||
for (int i0 = 0, i1 = 1; i0 < block_cnt; i0++, i1++)
|
||||
{
|
||||
if (process_block (o, block_ptr, i1, word_buf, final_buf, final_len, rule_out_buf) == 1)
|
||||
{
|
||||
memcpy (rules_buf[rules_cnt].rule_buf, rule_out_buf, BLOCK_SIZE);
|
||||
|
||||
rules_buf[rules_cnt].rule_len = i1;
|
||||
|
||||
rules_cnt++;
|
||||
}
|
||||
}
|
||||
|
||||
if (block_cnt >= 2)
|
||||
{
|
||||
while ((k = next_permutation (o, p, k)) != block_cnt)
|
||||
{
|
||||
for (int i0 = 0, i1 = 1; i0 < block_cnt; i0++, i1++)
|
||||
{
|
||||
if (process_block (o, block_ptr, i1, word_buf, final_buf, final_len, rule_out_buf) == 1)
|
||||
{
|
||||
memcpy (rules_buf[rules_cnt].rule_buf, rule_out_buf, BLOCK_SIZE);
|
||||
|
||||
rules_buf[rules_cnt].rule_len = i1;
|
||||
|
||||
rules_cnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i0 = 0, i1 = 1; i0 < block_cnt; i0++, i1++)
|
||||
{
|
||||
if (process_block (o, block_ptr, i1, word_buf, final_buf, final_len, rule_out_buf) == 1)
|
||||
{
|
||||
memcpy (rules_buf[rules_cnt].rule_buf, rule_out_buf, BLOCK_SIZE);
|
||||
|
||||
rules_buf[rules_cnt].rule_len = i1;
|
||||
|
||||
rules_cnt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sort and output the ones with the less length
|
||||
*/
|
||||
|
||||
qsort (rules_buf, rules_cnt, sizeof (rule_t), cmp);
|
||||
|
||||
int first_len = rules_buf[0].rule_len;
|
||||
|
||||
for (int i = 0; i < rules_cnt; i++)
|
||||
{
|
||||
rule_t *rule_buf = &rules_buf[i];
|
||||
|
||||
if (rule_buf->rule_len > first_len) break;
|
||||
|
||||
puts (rule_buf->rule_buf);
|
||||
}
|
||||
|
||||
free (rules_buf);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user