[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: 139
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 : ");
fgets(input, 200, stdin);
input[strlen (input) - 1] = '\0';
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 r;
r = fread(buf, size, 1, fr);
printf("%d\n", r);
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)&&(100000000))
rt = 9999999;
else
rt = 99999999;


mpz_root (y, x, rt);
mpz_t root;
mpz_init(root);
mpz_t remain;
mpz_init(remain);

mpz_rootrem (root, remain, y, rt);

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

do
{

	num = num/10;
	++rootnum;

}while(num != 0);

printf("\nEnter compressed filename : ");
fgets(output, 200, stdin);
output[strlen (output) - 1] = '\0';


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;
}

fwrite(buf, mpz_sizeinbase(y, 10), 1, fw);
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'");
if(ferror(fw))
{

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

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 w;
w = fwrite(&rt, rootnum, 1, fw);
printf("%d\n", w);

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;
	
}
fwrite(plus, 4, 1, fw);

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;
	
}
fwrite(remain, mpz_sizeinbase(remain, 10), 1, fw);

fclose(fw);

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

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

Post Reply