Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- void DecompressFile(unsigned char *buf, int FileLength, int p, int q, unsigned char *name);
- // argv[0] - Program name.
- // argv[1] - File to be read.
- // argv[2] - File to be written.
- // argv[3] - Compression argument.
- // argv[4] - First value index.
- // argv[5] - Second value index.
- int main(int argc, char *argv[])
- {
- FILE *fp;
- int FileLength;
- unsigned char *fbuf;
- if(argc<5) // If there's less than 5 arguments, exit the program.
- {
- fprintf(stderr,"Not enough arguments.");
- exit(1);
- }
- if((fp = fopen(argv[1],"rb"))== NULL)
- {
- fprintf(stderr,"Invalid file.");
- exit(1);
- }
- fseek(fp, 0, SEEK_END);
- FileLength = ftell(fp);
- rewind(fp);
- if ((fbuf = (unsigned char*) malloc(FileLength)) == NULL)
- {
- fprintf(stderr, "Not enough memory.");
- exit(1);
- }
- fread(fbuf, sizeof(unsigned char), FileLength, fp);
- fclose(fp);
- char *ptr;
- int Ind1 = strtoul(argv[4], &ptr, 16);
- int Ind2 = strtoul(argv[5], &ptr, 16);
- if(*argv[3]=='c')
- {
- // CompressFile(fbuf, FileLength);
- }
- else if(*argv[3]=='d')
- {
- DecompressFile(fbuf, FileLength, Ind1, Ind2, argv[2]);
- }
- else
- { // Should it come across an unknown character type...
- fprintf(stderr,"Invalid compression character type.");
- exit(1);
- }
- puts("File compressed/decompressed successfully with no errors.");
- free(fbuf);
- return 0;
- }
- void DecompressFile(unsigned char *buf, int FileLength, int p, int q, unsigned char *name){
- int i = 0; // Buffer increment counter.
- int b; // Loop decrement counter.
- unsigned char c; // Stores the compressed byte.
- uint32_t d; // Stores the decompressed byte.
- char *fbuf;
- if ((fbuf = (unsigned char*) malloc(FileLength)) == NULL)
- {
- fprintf(stderr, "Not enough memory (2).");
- exit(1);
- }
- FILE *fp;
- fp = fopen(name,"wb");
- while(i<FileLength)
- {
- c = 0;
- d = 0;
- for(b=7; b>=0; b--)
- {
- c = buf[i];
- if((c >> b) & 1)
- {
- d = d|(q << (b * 4));
- }
- else
- {
- d = d|(p << (b * 4));
- }
- }
- fbuf[i] = d;
- // printf("%d",d);
- i+=4;
- }
- i = 0;
- while(i!=(FileLength*4))
- {
- //fwrite(fbuf, 4, FileLength/4, fp);
- fprintf(fp,"%c", fbuf[i]);
- i++;
- }
- free(fbuf);
- fclose(fp);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment