summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2021-11-30 23:48:52 -0500
committerMalfurious <m@lfurio.us>2021-11-30 23:48:52 -0500
commit977317cb3ecf79695f3750f5d88df8ed223b4b22 (patch)
tree36c53e128ed1a1bbb37826a64697cb2bad11c8ee
parenta4312cb44fcf66cfd98e29d87afb5cdce028bb32 (diff)
downloadSorensenCompression-977317cb3ecf79695f3750f5d88df8ed223b4b22.tar.gz
SorensenCompression-977317cb3ecf79695f3750f5d88df8ed223b4b22.zip
Commit main C file
The current 'best' parameters by my testing are SHA1 for the hash and bithack01 as the iterator. Expect more options to arrive over time... The string to compress/decompress is taken as the first (and only) command-line argument. Signed-off-by: Malfurious <m@lfurio.us>
-rw-r--r--main.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..3b6ec7a
--- /dev/null
+++ b/main.c
@@ -0,0 +1,64 @@
+/* TODO: build system
+ * gcc -std=gnu99 -W -Wall -Werror -O3 main.c mpz.c -o pszip -lgmp -lcrypto */
+
+#include <openssl/sha.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mpz.h"
+
+/* Hash is currently downgraded to SHA1 for now, as it is faster and there is
+ * not yet a reason for anything stronger. */
+#define HASH_LENGTH SHA_DIGEST_LENGTH
+static void hash_mpz(void *output, const mpz_t n, size_t size)
+{
+ SHA1((void *)mpz_limbs_read(n), size, output);
+}
+
+int main(int argc, char **argv)
+{
+ unsigned char original_hash[HASH_LENGTH];
+ unsigned char iteration_hash[HASH_LENGTH];
+ struct pszip_mpz_state mpzs;
+ mpz_t original;
+
+ if (argc < 2)
+ {
+ fprintf(stderr, "Usage: %s <string>\n", argv[0]);
+ return 1;
+ }
+
+ /* Process original data */
+ size_t size = strlen(argv[1]);
+ pszip_mpz_init_data(original, argv[1], size);
+ mp_bitcnt_t popcnt = mpz_popcount(original);
+ hash_mpz(original_hash, original, size);
+ mpz_clear(original);
+
+ /* Display metadata */
+ printf("%s\n", argv[1]);
+ for (int i = 0; i < HASH_LENGTH; i++)
+ printf("%02x", original_hash[i]);
+ printf("\nsize = %zu\n", size);
+ printf("popcnt = %llu\n", (long long unsigned)popcnt);
+
+ /* Search for solution (1 thread) */
+ pszip_mpz_state_init(&mpzs, popcnt, size);
+
+ while (1)
+ {
+ hash_mpz(iteration_hash, mpzs.current, size);
+ if (memcmp(iteration_hash, original_hash, HASH_LENGTH) == 0)
+ break;
+ pszip_mpz_iterate_bithack01(&mpzs);
+ }
+
+ /* Results */
+ printf("\nFound!\n");
+ fwrite(mpz_limbs_read(mpzs.current), 1, size, stdout);
+ printf("\n");
+
+ pszip_mpz_state_clear(&mpzs);
+ return 0;
+}