[C][GMP] What are these trash data written on files ?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
Hadi_Lovelorn
Posts: 140
Joined: 2022-05-02 23:12
Been thanked: 1 time

[C][GMP] What are these trash data written on files ?

#1 Post by Hadi_Lovelorn »

Hi There

I'm still working on my project , I added the remain of rooted files for extreme compression to be lossless ..... But files have weird data when are written .......

The Code :

Code: Select all


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

int main(void)
{

	FILE * fr, * fw;
	char input[201];
	char output[201];
	unsigned long int size = 0;
	unsigned long int rt;
	unsigned char ch[5] = {112,111,119,101,114};
	unsigned char plus[4]={112,108,117,115};


	printf("Enter filename : ");
	char * r1;
	r1 = fgets(input, 200, stdin);
	input[strcspn(input, "\r\n")] = 0;
	printf("%s\n", r1);
	fr = fopen(input, "rb");

if (fr == NULL)
{
    fprintf(stderr, "Error during opening of file %s\n", input);
    perror("");
    return 1;
}

	fseek(fr, 0L, SEEK_END);
	size = ftell(fr);
	fseek(fr, 0L, SEEK_SET);

if (size == -1)
{

	fprintf(stderr, "Size doesn't match\n'");
	perror("");
	return 1;

}

if ( size > SIZE_MAX)
{

	fprintf(stderr, "The size of file is greater than acceptable\n");
	perror("");
	return 1;

}

	unsigned char * buf = NULL;
	buf = malloc((size_t)size);
if(buf == NULL)
{

	fprintf(stderr, "Buffer failure\n");
	perror("");
	return 1;

}

	int r2;
	r2 = fread(buf, size, 1, fr);
	printf("%d\n", r2);
if (ferror(fr))
{
	fprintf(stderr, "Error during reading from file %s\n", input);
	perror("");
	fclose(fr);
	return 1;
}
	fclose(fr);
	fr = NULL;

	mpz_t x;
	mpz_init(x);
	mpz_t y;
	mpz_init(y);

	mpz_import(x, size, 1, (size_t)1u, 0, 0, buf);

if (size<=15)
	rt = 1;
else if((size>15)&&(size<=100))
	rt = 9;
else if((size>100)&&(size<=1000))
	rt = 99;
else if((size>1000)&&(size<=10000))
	rt = 999;
else if((size>10000)&&(size<=100000))
	rt = 9999;
else if((size>100000)&&(size<=1000000))
	rt = 99999;
else if((size>1000000)&&(size<=10000000))
	rt = 999999;
else if((size>10000000)&&(size<=100000000))
	rt = 9999999;
else
	rt = 99999999;


	mpz_root (y, x, rt);
	mpz_t a;
	mpz_init(a);
	mpz_t b;
	mpz_init(b);

	mpz_rootrem (a, b, y, rt);

	int rootnum = 0;
	unsigned long int num = rt;

do
{

	num = num/10;
	++rootnum;

}while(num != 0);

	printf("\nEnter compressed filename : ");
	char * r3;
	r3 = fgets(output, 200, stdin);
	output[strcspn(output, "\r\n")] = 0;
	printf("%s\n", r3);


	memset (buf, 0u, size);

	mpz_export(buf, NULL, 1, (size_t)1u, 0, 0, y);

	fw = fopen(output, "wb");
if (fw == NULL) {
	fprintf(stderr, "Error during opening of file %s\n", output);
	perror("");
	return 1;
}

	int w;
	w = fwrite(buf, mpz_sizeinbase(y, 10), 1, fw);
	printf("%d\n", w);

if (ferror(fw)) {
	fprintf(stderr, "Error during writing to file %s\n", output);
	perror("");
	fclose(fw);
	return 1;
}

	fclose(fw);

	fw = fopen(output, "ab");
if(fw == NULL)
	{
	printf("File can't be written\n'");
	return 1;
	}
if(ferror(fw))
{

	fprintf(stderr, "Error during writing to file %s\n", output);
	perror("");
	fclose(fw);
	return 1;
	
}
	int w2;
	w2 = fwrite(ch, 5, 1, fw);
	printf("%d\n", w2);

	fclose(fw);

	fw = fopen(output, "ab");
if(fw == NULL)
	{
	printf("File can't be written\n'");
	return 1;
	}
if(ferror(fw))
{

	fprintf(stderr, "Error during writing to file %s\n", output);
	perror("");
	fclose(fw);
	return 1;
	
}
	int w3;
	w3 = fwrite(&rt, rootnum, 1, fw);
	printf("%d\n", w3);

	fclose(fw);

	fw = fopen(output, "ab");

if(fw == NULL)
	{
	printf("File can't be written\n'");
	return 1;
	}
if(ferror(fw))
{

	fprintf(stderr, "Error during writing to file %s\n", output);
	perror("");
	fclose(fw);
	return 1;
	
}
	int w4;
	w4 = fwrite(plus, 4, 1, fw);
	printf("%d\n", w4);

	fclose(fw);

	fw = fopen(output, "ab");
if(fw == NULL)
	{
	printf("File can't be written\n'");
	}
if(ferror(fw))
{

	fprintf(stderr, "Error during writing to file %s\n", output);
	perror("");
	fclose(fw);
	return 1;
	
}

	int w5;
	w5 = fwrite(b, mpz_sizeinbase(b, 10), 1, fw);
	printf("%d\n", w5);

	fclose(fw);

	free(buf);
	mpz_clear(x);
	mpz_clear(y);
	mpz_clear(a);
	mpz_clear(b);

return 0;

}
This is a file compressed from Alizee J'en Ai Marre music video on YouTube :

Screenshot

This is another file compressed from Mike Oldfield Secrets and Far Above The Clouds from YouTube :

Screenshot

This is worse than the 2 former files , It has two 0E and filename and pluspower in it ! :

Screenshot

These are not acceptable and reasonable ........ Is there any GMP pro to say why these trash data are written on files ?

Thanks
Last edited by Hadi_Lovelorn on 2024-12-26 02:56, edited 1 time in total.

User avatar
ruwolf
Posts: 907
Joined: 2008-02-18 05:04
Location: Banovce nad Bebravou
Has thanked: 89 times
Been thanked: 67 times

Re: [C][GMP] What are these trash data written on files ?

#2 Post by ruwolf »

What is your intended goal?
Do you want to compress data using GMP?
Which kind of data?
Losslessly or with inaccuracies?
Do you know binary arithmetic?
Maybe some users are much more knowledgeable about these topics and will be able to tell you if your goal is mathematically possible or impossible before you attempt it.
And maybe someone has not only tried it, but also done it, and it's even available in Debian.

User avatar
Hadi_Lovelorn
Posts: 140
Joined: 2022-05-02 23:12
Been thanked: 1 time

Re: [C][GMP] What are these trash data written on files ?

#3 Post by Hadi_Lovelorn »

ruwolf

I updated my program but still writes trash data

Yes , The program reads files as data-number and gets the appropriate root ( even huge ones ) and writes it with the exponent and remain in the compressed file ... It's lossless , Yes , I'm good in math ( but my brain works slowly because of heavy psycho-atric medicines )

It's possible , Just needs enough RAM for whole large files but we can split files to 256 MB files and then after decompression merge them together ....

It's my own idead since 2008 .... I don't think it's available in Debian

Thanks for Your attention

Post Reply