Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Genius compression algorithm! Fast compression! 50% compression ratio!

Name: Anonymous 2019-07-17 21:22

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sha256.c"

void compress(FILE *in, FILE *out)
{
fseek(in, 0, SEEK_END);
long inlen = ftell(in);
rewind(in);
BYTE parity = inlen % 2;
fwrite(&parity, sizeof(BYTE), 1, out);
BYTE hash[SHA256_BLOCK_SIZE];
SHA256_CTX ctx;
sha256_init(&ctx);
BYTE b;
long outlen = inlen / 2;
int i;
for(i = 0; i < outlen; i++) {
fread(&b, sizeof(BYTE), 1, in);
fwrite(&b, sizeof(BYTE), 1, out);
sha256_update(&ctx, &b, 1);
}
for(; i < inlen; i++) {
fread(&b, sizeof(BYTE), 1, in);
sha256_update(&ctx, &b, 1);
}
sha256_final(&ctx, hash);
fwrite(&hash, sizeof(BYTE), SHA256_BLOCK_SIZE, out);
}

void do_search(SHA256_CTX *ctx, BYTE *buf, BYTE *thash, int len);

void decompress(FILE *in, FILE *out)
{
fseek(in, 0, SEEK_END);
long inlen = ftell(in) - 1 - SHA256_BLOCK_SIZE;
rewind(in);
BYTE parity;
fread(&parity, sizeof(BYTE), 1, in);
long outlen = inlen + parity;
BYTE *buf = calloc(outlen, sizeof(BYTE));
SHA256_CTX ctx;
sha256_init(&ctx);
while(inlen --> 0) {
BYTE b;
fread(&b, sizeof(BYTE), 1, in);
fwrite(&b, sizeof(BYTE), 1, out);
sha256_update(&ctx, &b, 1);
}
BYTE hash[SHA256_BLOCK_SIZE];
fread(&hash, sizeof(BYTE), SHA256_BLOCK_SIZE, in);
do_search(&ctx, buf, hash, outlen);
fwrite(&buf, sizeof(BYTE), outlen, out);
}

void do_search(SHA256_CTX *ctx, BYTE *buf, BYTE *thash, int len)
{
SHA256_CTX *ctxs = malloc(sizeof(SHA256_CTX) * (len + 1));
ctxs[0] = *ctx;
int i = 0;
while(1) {
if(i == len) {
BYTE hash[SHA256_BLOCK_SIZE];
sha256_final(&ctxs[i], hash);
if(!memcmp(&hash, thash, len)) {
return;
}
while(!++buf[--i]);
}
else {
ctxs[i] = ctxs[i];
sha256_update(&ctxs[i], &buf[i], 1);
i++;
}
}
}

int main(int argv, char **argc)
{
if(argv != 4) {
puts("Wrong number of arguments.");
return EXIT_FAILURE;
}
FILE *in = fopen(argc[2], "rb"), *out = fopen(argc[3], "wb");
if(argc[1][0] == 'd') {
decompress(in, out);
}
else {
compress(in, out);
}
fclose(in);
fclose(out);
return EXIT_SUCCESS;
}

Name: Anonymous 2019-07-17 21:24

Now with proper formatting!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sha256.c"

void compress(FILE *in, FILE *out)
{
fseek(in, 0, SEEK_END);
long inlen = ftell(in);
rewind(in);
BYTE parity = inlen % 2;
fwrite(&parity, sizeof(BYTE), 1, out);
BYTE hash[SHA256_BLOCK_SIZE];
SHA256_CTX ctx;
sha256_init(&ctx);
BYTE b;
long outlen = inlen / 2;
int i;
for(i = 0; i < outlen; i++) {
fread(&b, sizeof(BYTE), 1, in);
fwrite(&b, sizeof(BYTE), 1, out);
sha256_update(&ctx, &b, 1);
}
for(; i < inlen; i++) {
fread(&b, sizeof(BYTE), 1, in);
sha256_update(&ctx, &b, 1);
}
sha256_final(&ctx, hash);
fwrite(&hash, sizeof(BYTE), SHA256_BLOCK_SIZE, out);
}

void do_search(SHA256_CTX *ctx, BYTE *buf, BYTE *thash, int len);

void decompress(FILE *in, FILE *out)
{
fseek(in, 0, SEEK_END);
long inlen = ftell(in) - 1 - SHA256_BLOCK_SIZE;
rewind(in);
BYTE parity;
fread(&parity, sizeof(BYTE), 1, in);
long outlen = inlen + parity;
BYTE *buf = calloc(outlen, sizeof(BYTE));
SHA256_CTX ctx;
sha256_init(&ctx);
while(inlen --> 0) {
BYTE b;
fread(&b, sizeof(BYTE), 1, in);
fwrite(&b, sizeof(BYTE), 1, out);
sha256_update(&ctx, &b, 1);
}
BYTE hash[SHA256_BLOCK_SIZE];
fread(&hash, sizeof(BYTE), SHA256_BLOCK_SIZE, in);
do_search(&ctx, buf, hash, outlen);
fwrite(&buf, sizeof(BYTE), outlen, out);
}

void do_search(SHA256_CTX *ctx, BYTE *buf, BYTE *thash, int len)
{
SHA256_CTX *ctxs = malloc(sizeof(SHA256_CTX) * (len + 1));
ctxs[0] = *ctx;
int i = 0;
while(1) {
if(i == len) {
BYTE hash[SHA256_BLOCK_SIZE];
sha256_final(&ctxs[i], hash);
if(!memcmp(&hash, thash, len)) {
return;
}
while(!++buf[--i]);
}
else {
ctxs[i + 1] = ctxs[i];
sha256_update(&ctxs[i + 1], &buf[i], 1);
i++;
}
}
}

int main(int argv, char **argc)
{
if(argv != 4) {
puts("Wrong number of arguments.");
return EXIT_FAILURE;
}
FILE *in = fopen(argc[2], "rb"), *out = fopen(argc[3], "wb");
if(argc[1][0] == 'd') {
decompress(in, out);
}
else {
compress(in, out);
}
fclose(in);
fclose(out);
return EXIT_SUCCESS;
}

Don't change these.
Name: Email:
Entire Thread Thread List