Dandaman955

Untitled

Jul 12th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. void DecompressFile(unsigned char *buf, int FileLength, int p, int q, unsigned char *name);
  6.  
  7. // argv[0] - Program name.
  8. // argv[1] - File to be read.
  9. // argv[2] - File to be written.
  10. // argv[3] - Compression argument.
  11. // argv[4] - First value index.
  12. // argv[5] - Second value index.
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.   FILE *fp;        
  17.   int FileLength;
  18.   unsigned char *fbuf;
  19.  
  20.   if(argc<5)  // If there's less than 5 arguments, exit the program.
  21.   {
  22.             fprintf(stderr,"Not enough arguments.");
  23.             exit(1);
  24.   }
  25.   if((fp = fopen(argv[1],"rb"))== NULL)
  26.   {
  27.             fprintf(stderr,"Invalid file.");
  28.             exit(1);    
  29.   }
  30.   fseek(fp, 0, SEEK_END);
  31.   FileLength = ftell(fp);
  32.   rewind(fp);
  33.   if ((fbuf = (unsigned char*) malloc(FileLength)) == NULL)              
  34.   {
  35.         fprintf(stderr, "Not enough memory.");
  36.         exit(1);                                                                                                    
  37.   }  
  38.   fread(fbuf, sizeof(unsigned char), FileLength, fp);
  39.   fclose(fp);
  40.   char *ptr;
  41.   int Ind1 = strtoul(argv[4], &ptr, 16);
  42.   int Ind2 = strtoul(argv[5], &ptr, 16);
  43.  
  44.   if(*argv[3]=='c')
  45.   {
  46.            // CompressFile(fbuf, FileLength);      
  47.   }
  48.   else if(*argv[3]=='d')
  49.   {
  50.             DecompressFile(fbuf, FileLength, Ind1, Ind2, argv[2]);
  51.   }
  52.   else
  53.   {         // Should it come across an unknown character type...
  54.             fprintf(stderr,"Invalid compression character type.");
  55.             exit(1);
  56.   }
  57.   puts("File compressed/decompressed successfully with no errors.");
  58.   free(fbuf);
  59.   return 0;
  60. }
  61.  
  62. void DecompressFile(unsigned char *buf, int FileLength, int p, int q, unsigned char *name){
  63.   int i = 0;       // Buffer increment counter.
  64.   int b;           // Loop decrement counter.
  65.   unsigned char c; // Stores the compressed byte.
  66.   uint32_t d;           // Stores the decompressed byte.
  67.   char *fbuf;      
  68.  
  69.   if ((fbuf = (unsigned char*) malloc(FileLength)) == NULL)              
  70.   {
  71.         fprintf(stderr, "Not enough memory (2).");
  72.         exit(1);              
  73.   }
  74.   FILE *fp;
  75.   fp = fopen(name,"wb");
  76.   while(i<FileLength)
  77.   {
  78.          c = 0;
  79.          d = 0;            
  80.          for(b=7; b>=0; b--)
  81.          {
  82.                c = buf[i];
  83.                if((c >> b) & 1)
  84.                {      
  85.                     d = d|(q << (b * 4));
  86.                }
  87.                else
  88.                {
  89.                     d = d|(p << (b * 4));
  90.                }
  91.          }
  92.          
  93.          fbuf[i] = d;
  94.          
  95.   //       printf("%d",d);
  96.          
  97.          i+=4;
  98.   }
  99.   i = 0;
  100.   while(i!=(FileLength*4))
  101.   {
  102.   //fwrite(fbuf, 4, FileLength/4, fp);
  103.        fprintf(fp,"%c", fbuf[i]);
  104.        i++;
  105.   }
  106.   free(fbuf);
  107.   fclose(fp);  
  108.   return;  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment