"Fossies" - the Fresh Open Source Software Archive

Member "grzip-0.3.0/GRZipII.c" (4 Jan 2007, 13833 Bytes) of package /linux/privat/old/grzip-0.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "GRZipII.c" see the Fossies "Dox" file reference documentation.

    1 /*-------------------------------------------------*/
    2 /* GRZipII/libGRZip compressor           GRZipII.c */
    3 /* GRZipII file to file compressor                 */
    4 /*-------------------------------------------------*/
    5 
    6 /*--
    7   This file is a part of GRZipII and/or libGRZip, a program
    8   and library for lossless, block-sorting data compression.
    9 
   10   Copyright (C) 2002-2004 Grebnov Ilya. All rights reserved.
   11 
   12   This library is free software; you can redistribute it and/or
   13   modify it under the terms of the GNU Lesser General Public
   14   License as published by the Free Software Foundation; either
   15   version 2.1 of the License, or (at your option) any later version.
   16 
   17   This library is distributed in the hope that it will be useful,
   18   but WITHOUT ANY WARRANTY; without even the implied warranty of
   19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   20   Lesser General Public License for more details.
   21 
   22   Grebnov Ilya, Ivanovo, Russian Federation.
   23   Ilya.Grebnov@magicssoft.ru, http://magicssoft.ru/
   24 
   25   This program is based on (at least) the work of:
   26   Juergen Abel, Jon L. Bentley, Edgar Binder,
   27   Charles Bloom, Mike Burrows, Andrey Cadach,
   28   Damien Debin, Sebastian Deorowicz, Peter Fenwick,
   29   George Plechanov, Michael Schindler, Robert Sedgewick,
   30   Julian Seward, David Wheeler, Vadim Yoockin.
   31 
   32   Normal compression mode:
   33     Compression     memory use : [9-11]*BlockLen + 1Mb
   34     Decompression   memory use : 7*BlockLen      + 1Mb
   35   Fast compression mode:
   36     Compression     memory use : 7*BlockLen      + 1Mb
   37     Decompression   memory use : 7.125*BlockLen  + 1Mb
   38 
   39   For more information on these sources, see the manual.
   40 --*/
   41 
   42 #include <time.h>
   43 #include <stdio.h>
   44 #include <stdlib.h>
   45 #include <string.h>
   46 
   47 #include "libgrzip.c"
   48 
   49 uint8  GRZipIISign[12]={0x47,0x52,0x5A,0x69,0x70,0x49,0x49,0x0,0x2,0x4,':',')'};
   50 
   51 sint32 BlockSize=5*1024*1024;
   52 sint32 FastBWTMode=1;
   53 sint32 DeltaFilter=0;
   54 sint32 UseLZP=1;
   55 sint32 AdaptativeBSize=0;
   56 sint32 Mode=GRZ_Compression_BWT+GRZ_Compression_WFC;
   57 sint32 LZPHTS=GRZ_LZP_HTS15;
   58 sint32 LZPMML=GRZ_LZP_MML32;
   59 
   60 void ShowAllSwitches(void)
   61 {
   62   fprintf(stdout,"Usage:GRZipII <e|d> InputFile OutputFile <Switches>\n\n");
   63   fprintf(stdout,"<Switches>\n");
   64   fprintf(stdout,"  -b<size> Maximum block size(e.g. -b1m, -b1024k)\n");
   65   fprintf(stdout,"           (default -b5m or -b5120k, maximum -b8m)\n");
   66   fprintf(stdout,"  -m<mode> Compression algorithms, default: -m1\n");
   67   fprintf(stdout,"           -m1 LZP + BWT + WFC + EC\n");
   68   fprintf(stdout,"           -m2 LZP + BWT + MTF + EC\n");
   69   fprintf(stdout,"           -m3 LZP + ST4 + WFC + EC\n");
   70   fprintf(stdout,"           -m4 LZP + ST4 + MTF + EC\n");
   71   fprintf(stdout,"  -L<size> LZP Hash table size[10-18], 4*2^Size bytes, default: -L15\n");
   72   fprintf(stdout,"  -M<size> LZP Minimum Matched Len [2,5,8...,47], default: -M32\n");
   73   fprintf(stdout,"  -S       Use alternative BWT Sorting algorithm\n");
   74   fprintf(stdout,"           (faster for repetitive blocks)\n");
   75   fprintf(stdout,"  -a       Enable Adaptative block size reduction\n");
   76   fprintf(stdout,"  -l       Disable LZP preprocessing\n");
   77   fprintf(stdout,"  -d       Enable Delta filter\n");
   78   fprintf(stdout,"  -p       Disable all Preprocessing techniques\n");
   79   fprintf(stdout,"  -h       Show all Switches\n");
   80   exit(0);
   81 }
   82 
   83 void ShowUsage(void)
   84 {
   85   fprintf(stdout,"Usage:GRZipII <e|d> InputFile OutputFile <Switches>\n\n");
   86   fprintf(stdout,"<Switches>\n");
   87   fprintf(stdout,"  -b<size> Maximum block size(e.g. -b1m, -b1024k)\n");
   88   fprintf(stdout,"           (default -b5m or -b5120k, maximum -b8m)\n");
   89   fprintf(stdout,"  -m<mode> Compression algorithms, default: -m1\n");
   90   fprintf(stdout,"           -m1 LZP + BWT + WFC + EC\n");
   91   fprintf(stdout,"           -m2 LZP + BWT + MTF + EC\n");
   92   fprintf(stdout,"           -m3 LZP + ST4 + WFC + EC\n");
   93   fprintf(stdout,"           -m4 LZP + ST4 + MTF + EC\n");
   94   fprintf(stdout,"  -a       Enable Adaptative block size reduction\n");
   95   fprintf(stdout,"  -l       Disable LZP preprocessing\n");
   96   fprintf(stdout,"  -p       Disable all Preprocessing techniques\n");
   97   fprintf(stdout,"  -h       Show all Switches\n");
   98   exit(0);
   99 }
  100 
  101 void Options(char * s)
  102 {
  103  while (*(++s)!='\0')
  104    switch (*s)
  105    {
  106      case 'b':
  107       {
  108         char *ss=++s;
  109         while ((*s>='0')&&(*s<='9')) s++;
  110         if (*s=='m')
  111         {
  112            BlockSize=atoi(ss)*1024*1024;
  113            break;
  114         }
  115         if (*s=='k')
  116         {
  117            BlockSize=atoi(ss)*1024;
  118            break;
  119         }
  120         ShowUsage();
  121        }
  122      case 'm':
  123       {
  124         char *ss=++s;
  125         while ((*s>='0')&&(*s<='9')) s++;
  126         s--;
  127         switch (atoi(ss))
  128         {
  129           case 1:{Mode=GRZ_Compression_BWT+GRZ_Compression_WFC;break;}
  130           case 2:{Mode=GRZ_Compression_BWT+GRZ_Compression_MTF;break;}
  131           case 3:{Mode=GRZ_Compression_ST4+GRZ_Compression_WFC;break;}
  132           case 4:{Mode=GRZ_Compression_ST4+GRZ_Compression_MTF;break;}
  133           default:ShowUsage();
  134         }
  135         break;
  136       }
  137      case 'L':
  138       {
  139         char *ss=++s;
  140         while ((*s>='0')&&(*s<='9')) s++;
  141         s--;
  142         switch (atoi(ss))
  143         {
  144           case 10:{LZPHTS=GRZ_LZP_HTS10;break;}
  145           case 11:{LZPHTS=GRZ_LZP_HTS11;break;}
  146           case 12:{LZPHTS=GRZ_LZP_HTS12;break;}
  147           case 13:{LZPHTS=GRZ_LZP_HTS13;break;}
  148           case 14:{LZPHTS=GRZ_LZP_HTS14;break;}
  149           case 15:{LZPHTS=GRZ_LZP_HTS15;break;}
  150           case 16:{LZPHTS=GRZ_LZP_HTS16;break;}
  151           case 17:{LZPHTS=GRZ_LZP_HTS17;break;}
  152           case 18:{LZPHTS=GRZ_LZP_HTS18;break;}
  153           default:ShowUsage();
  154         }
  155         break;
  156       }
  157      case 'M':
  158       {
  159         char *ss=++s;
  160         while ((*s>='0')&&(*s<='9')) s++;
  161         s--;
  162         switch (atoi(ss))
  163         {
  164           case 2 :{LZPMML=GRZ_LZP_MML2;break;}
  165           case 5 :{LZPMML=GRZ_LZP_MML5;break;}
  166           case 8 :{LZPMML=GRZ_LZP_MML8;break;}
  167           case 11:{LZPMML=GRZ_LZP_MML11;break;}
  168           case 14:{LZPMML=GRZ_LZP_MML14;break;}
  169           case 17:{LZPMML=GRZ_LZP_MML17;break;}
  170           case 20:{LZPMML=GRZ_LZP_MML20;break;}
  171           case 23:{LZPMML=GRZ_LZP_MML23;break;}
  172           case 26:{LZPMML=GRZ_LZP_MML26;break;}
  173           case 29:{LZPMML=GRZ_LZP_MML29;break;}
  174           case 32:{LZPMML=GRZ_LZP_MML32;break;}
  175           case 35:{LZPMML=GRZ_LZP_MML35;break;}
  176           case 38:{LZPMML=GRZ_LZP_MML38;break;}
  177           case 41:{LZPMML=GRZ_LZP_MML41;break;}
  178           case 44:{LZPMML=GRZ_LZP_MML44;break;}
  179           case 47:{LZPMML=GRZ_LZP_MML47;break;}
  180           default:ShowUsage();
  181         }
  182         break;
  183       }
  184      case 'h': {ShowAllSwitches();break;}
  185      case 'S': {FastBWTMode=0;break;}
  186      case 'l': {UseLZP=0;break;}
  187      case 'a': {AdaptativeBSize=1;break;}
  188      case 'd': {DeltaFilter=1;break;}
  189      case 'p': {DeltaFilter=0;UseLZP=0;AdaptativeBSize=0;break;}
  190      default : ShowUsage();
  191    }
  192 }
  193 
  194 void MakeMode(void)
  195 {
  196  if ((BlockSize>8*1024*1024)||(BlockSize<1024)) ShowUsage();
  197  if (BlockSize>GRZ_MaxBlockSize) BlockSize=GRZ_MaxBlockSize;
  198  if (UseLZP)
  199    Mode=Mode+LZPMML+LZPHTS;
  200  else
  201    Mode+=GRZ_Disable_LZP;
  202  if (FastBWTMode)
  203    Mode+=GRZ_BWTSorting_Fast;
  204  else
  205    Mode+=GRZ_BWTSorting_Strong;
  206  if (DeltaFilter)
  207    Mode+=GRZ_Enable_DeltaFlt;
  208  else
  209    Mode+=GRZ_Disable_DeltaFlt;
  210 }
  211 
  212 sint32 CLine(sint32 argc,char ** argv)
  213 {
  214   sint32 Result=0,i;
  215   if (argc==1) ShowUsage();
  216   if (*argv[1]=='-') {Options(argv[1]);ShowUsage();};
  217   if (argc<4) ShowUsage();
  218   if (strlen(argv[1])!=1) ShowUsage();
  219   switch (*argv[1])
  220   {
  221     case 'e': {Result=1;break; }
  222     case 'd': {Result=2;break; }
  223     default : ShowUsage();
  224   }
  225   for (i=4;i<argc;i++)
  226     if (*argv[i]=='-')
  227       Options(argv[i]);
  228     else
  229       ShowUsage();
  230   MakeMode();
  231   return (Result);
  232 }
  233 
  234 void Compression(char ** argv)
  235 {
  236   FILE * FInput=fopen(argv[2],"rb");
  237   if (FInput==NULL)
  238   {
  239     fprintf(stderr,"Can't open input file %s!\n",argv[2]);
  240     return ;
  241   }
  242   FILE * FOutput=fopen(argv[3],"wb");
  243   if (FOutput==NULL)
  244   {
  245     fclose(FInput);
  246     fprintf(stderr,"Can't create output file %s!\n",argv[3]);
  247     return ;
  248   }
  249   fwrite(GRZipIISign,1,sizeof(GRZipIISign),FOutput);
  250 
  251   if (fseek(FInput,0,SEEK_END))
  252   {
  253     fclose(FOutput); fclose(FInput);
  254     fprintf(stderr,"IO error on file %s!\n",argv[2]);
  255     return ;
  256   }
  257   long RealSize=ftell(FInput);
  258   if (RealSize==-1)
  259   {
  260     fclose(FOutput); fclose(FInput);
  261     fprintf(stderr,"IO error on file %s!\n",argv[2]);
  262     return ;
  263   }
  264 
  265   if (fseek(FInput,0,SEEK_SET))
  266   {
  267     fclose(FOutput); fclose(FInput);
  268     fprintf(stderr,"IO error on file %s!\n",argv[2]);
  269     return ;
  270   }
  271 
  272   uint8 * Input=(uint8 *)malloc(BlockSize+1024);
  273   if (Input==NULL)
  274   {
  275     fclose(FOutput); fclose(FInput);
  276     fprintf(stderr,"Not enough memory!\n");
  277     return ;
  278   }
  279   uint8 * Output=(uint8 *)malloc(BlockSize+1024);
  280   if (Output==NULL)
  281   {
  282     free(Input); fclose(FOutput); fclose(FInput);
  283     fprintf(stderr,"Not enough memory!\n");
  284     return ;
  285   }
  286 
  287   clock_t BeginTime = clock();
  288 
  289   while ((feof(FInput)==0)&&(ftell(FInput)!=RealSize))
  290   {
  291     fprintf(stdout,"\r");
  292     double Tmp=ftell(FInput);Tmp=(100*Tmp)/RealSize;
  293     fprintf(stdout,"Compressing %.55s(%02d%%)",argv[2],(sint32)Tmp);fflush(stdout);
  294 
  295     sint32 NumRead=fread(Input,1,BlockSize,FInput);
  296 
  297     if (AdaptativeBSize)
  298     {
  299        sint32 NewSize=GRZip_GetAdaptativeBlockSize(Input,NumRead);
  300        if (NewSize!=NumRead)
  301        {
  302          sint32 NewPos=ftell(FInput)+NewSize-NumRead;
  303          fseek(FInput,NewPos,SEEK_SET);
  304          NumRead=NewSize;
  305        }
  306     }
  307 
  308     sint32 Size=GRZip_CompressBlock(Input,NumRead,Output,Mode);
  309 
  310     fwrite(Output,1,Size,FOutput);
  311   }
  312 
  313   fprintf(stdout,"\r                                                                          \r");fflush(stdout);
  314 
  315   double TotTime=((double)(clock()-BeginTime))/CLOCKS_PER_SEC;
  316   fprintf(stdout,"Compressed %ld into %ld in %.3f seconds.\n",RealSize,ftell(FOutput),TotTime);
  317 
  318   free(Input);free(Output);
  319   fclose(FOutput);fclose(FInput);
  320 }
  321 
  322 void Decompression(char ** argv)
  323 {
  324   uint8 BlockSign[28];
  325 
  326   FILE * FInput=fopen(argv[2],"rb");
  327   if (FInput==NULL)
  328   {
  329     fprintf(stderr,"Can't open input file %s!\n",argv[2]);
  330     return ;
  331   }
  332   FILE * FOutput=fopen(argv[3],"wb");
  333   if (FOutput==NULL)
  334   {
  335     fclose(FInput);
  336     fprintf(stderr,"Can't create output file %s!\n",argv[3]);
  337     return ;
  338   }
  339   uint8 SignTest[sizeof(GRZipIISign)];
  340 
  341   if (fread(SignTest,1,sizeof(GRZipIISign),FInput)!=sizeof(GRZipIISign))
  342   {
  343     fclose(FInput);fclose(FOutput);
  344     fprintf(stderr,"This's not GRZip archive!\n");
  345     return;
  346   }
  347 
  348   if (memcmp(SignTest,GRZipIISign,sizeof(GRZipIISign))!=0)
  349   {
  350     fclose(FInput);fclose(FOutput);
  351     fprintf(stderr,"This's not GRZip archive!\n");
  352     return;
  353   }
  354 
  355   if (fseek(FInput,0,SEEK_END))
  356   {
  357     fclose(FOutput); fclose(FInput);
  358     fprintf(stderr,"IO error on file %s!\n",argv[2]);
  359     return ;
  360   }
  361   long FileSize=ftell(FInput);
  362   if (FileSize==-1)
  363   {
  364     fclose(FOutput); fclose(FInput);
  365     fprintf(stderr,"IO error on file %s!\n",argv[2]);
  366     return ;
  367   }
  368   if (fseek(FInput,sizeof(GRZipIISign),SEEK_SET))
  369   {
  370     fclose(FOutput); fclose(FInput);
  371     fprintf(stderr,"IO error on file %s!\n",argv[2]);
  372     return ;
  373   }
  374 
  375   clock_t BeginTime = clock();
  376 
  377   while ((feof(FInput)==0)&&(ftell(FInput)!=FileSize))
  378   {
  379     fprintf(stdout,"\r");
  380     double Tmp=ftell(FInput);Tmp=(100*Tmp)/FileSize;
  381     fprintf(stdout,"Decompressing %.55s(%02d%%)",argv[2],(sint32)Tmp);fflush(stdout);
  382 
  383     sint32 NumRead=fread(BlockSign,1,28,FInput);
  384     if (NumRead!=28)
  385     {
  386       fclose(FOutput); fclose(FInput);
  387       fprintf(stderr,"Unexpected end of file %s!\n",argv[2]);
  388       return ;
  389     }
  390 
  391     if (GRZip_CheckBlockSign(BlockSign,28)!=GRZ_NO_ERROR)
  392     {
  393       fclose(FOutput); fclose(FInput);
  394       fprintf(stderr,"CRC check failed!\n");
  395       return ;
  396     }
  397 
  398     uint8 * Input=(uint8 *)malloc(1024+(*(sint32 *)(BlockSign+16)));
  399     if (Input==NULL)
  400     {
  401       fclose(FOutput); fclose(FInput);
  402       fprintf(stderr,"Not enough memory!\n");
  403       return ;
  404     }
  405 
  406     memcpy(Input,BlockSign,28);
  407 
  408     uint8 * Output=(uint8 *)malloc(1024+(*(sint32 *)(Input)));
  409     if (Output==NULL)
  410     {
  411       free(Input);fclose(FOutput); fclose(FInput);
  412       fprintf(stderr,"Not enough memory!\n");
  413       return ;
  414     }
  415 
  416     NumRead=fread(Input+28,1,*(sint32 *)(Input+16),FInput);
  417     if (NumRead!=*(sint32 *)(Input+16))
  418     {
  419       free(Input); free(Output); fclose(FOutput); fclose(FInput);
  420       fprintf(stderr,"Unexpected end of file %s!\n",argv[2]);
  421       return ;
  422     }
  423 
  424     sint32 Size=GRZip_DecompressBlock(Input,NumRead+28,Output);
  425 
  426     if (Size==GRZ_NOT_ENOUGH_MEMORY)
  427     {
  428       free(Input); free(Output); fclose(FOutput); fclose(FInput);
  429       fprintf(stderr,"Not enough memory!\n");
  430       return ;
  431     }
  432 
  433     if (Size<0)
  434     {
  435       free(Input); free(Output); fclose(FOutput); fclose(FInput);
  436       fprintf(stderr,"CRC check failed!\n");
  437       return ;
  438     }
  439 
  440     fwrite(Output,1,Size,FOutput);
  441 
  442     free(Input); free(Output);
  443 
  444   }
  445 
  446   fprintf(stdout,"\r                                                                          \r");fflush(stdout);
  447 
  448   double TotTime=((double)(clock()-BeginTime))/CLOCKS_PER_SEC;
  449   fprintf(stdout,"Decompressed %ld into %ld in %.3f seconds.\n",FileSize,ftell(FOutput),TotTime);
  450 
  451   fclose(FOutput);fclose(FInput);
  452 }
  453 
  454 int main(int argc,char * argv[])
  455 {
  456   fprintf(stderr,"This is GRZipII, yet another BSLDCA compressor. Version 0.2.4, 12-Feb-2004.\n");
  457   fprintf(stderr,"CopyRight (c) 2002-2004 by Grebnov Ilya <Ilya.Grebnov@magicssoft.ru>.\n\n");
  458   switch (CLine(argc, argv))
  459   {
  460     case 1: {Compression(argv);break;}
  461     case 2: {Decompression(argv);break;}
  462   }
  463   return 0;
  464 }
  465 
  466 /*-------------------------------------------------*/
  467 /* End                                   GRZipII.c */
  468 /*-------------------------------------------------*/