"Fossies" - the Fresh Open Source Software Archive

Member "grzip-0.3.0/main.c" (31 Jan 2007, 20420 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 "main.c" see the Fossies "Dox" file reference documentation.

    1 /*----------------------------------------------*/
    2 /* GRZipII/libGRZip compressor           main.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 <utime.h>
   43 
   44 uint8  GRZipIISign[12]={0x47,0x52,0x5A,0x69,0x70,0x49,0x49,0x0,0x2,0x4,':',')'};
   45 
   46 sint32 BlockSize=5*1024*1024;
   47 sint32 FastBWTMode=1;
   48 sint32 DeltaFilter=0;
   49 sint32 UseCompression=1;
   50 sint32 Verbose=-1;
   51 sint32 UseLZP=1;
   52 sint32 AdaptativeBSize=0;
   53 sint32 ForceOverwrite=0;
   54 sint32 Keep=0;
   55 sint32 AreFiles=0;
   56 sint32 NumFiles=0;
   57 sint32 StdIn=0;
   58 sint32 StdOut=0;
   59 sint32 Mode=GRZ_Compression_BWT+GRZ_Compression_WFC;
   60 sint32 LZPHTS=GRZ_LZP_HTS15;
   61 sint32 LZPMML=GRZ_LZP_MML32;
   62 
   63 char** Files=NULL;
   64 char * FileOut=NULL;
   65 
   66 void ShowVersion(FILE *fd)
   67 {
   68     fprintf(fd, _("This is grzip, yet another BSLDCA compressor. Version 0.3.0, 31-Jan-2007.\n"));
   69     fprintf(fd, _("Copyright (c) 2002-2004 by Grebnov Ilya <Ilya.Grebnov@magicssoft.ru>.\n"));
   70     fprintf(fd, _("Enhanced version by <Jean-Pierre.Demailly@ujf-grenoble.fr>, Jan 2007.\n\n"));
   71 }
   72 
   73 void ShowUsage(FILE *fd)
   74 {
   75   ShowVersion(fd);
   76   fprintf(fd, _("Usage: grzip <Switches> InputFile(s)\n\n"));
   77   fprintf(fd, _("Switches:\n"));
   78   fprintf(fd, _("  -i       All further args are input files\n"));
   79   fprintf(fd, _("  -f       Force overwrite of output file\n"));   
   80   fprintf(fd, _("  -k       Keep input file after operation\n"));   
   81   fprintf(fd, _("  -r       Remove input file after operation\n"));      
   82   fprintf(fd, _("  -c       Use stdout as output\n"));
   83   fprintf(fd, _("  -d       Decompression (compression is default)\n"));
   84   fprintf(fd, _("           equivalent to using 'grunzip' instead of 'grzip' and\n"));
   85   fprintf(fd, _("           using 'grzcat' is equivalent to using 'grunzip -c -q'\n"));
   86   fprintf(fd, _("  -z       Compression (this is the default)\n"));   
   87   fprintf(fd, _("  -o<outf> Name of output file\n"));
   88   fprintf(fd, _("  -b<size> Maximum block size (e.g. -b1m, -b1024k)\n"));
   89   fprintf(fd, _("           (default -b5m or -b5120k, maximum -b8m)\n"));
   90   fprintf(fd, _("  -m<mode> Compression algorithms, default: -m1\n"));
   91   fprintf(fd, _("           -m1 LZP + BWT + WFC + EC\n"));
   92   fprintf(fd, _("           -m2 LZP + BWT + MTF + EC\n"));
   93   fprintf(fd, _("           -m3 LZP + ST4 + WFC + EC\n"));
   94   fprintf(fd, _("           -m4 LZP + ST4 + MTF + EC\n"));
   95   fprintf(fd, _("  -L<size> LZP Hash table size [10-18], 4*2^Size bytes, default: -L15\n"));
   96   fprintf(fd, _("  -M<size> LZP Minimum Matched Len [2,5,8...,47], default: -M32\n"));
   97   fprintf(fd, _("  -S       Use alternative BWT Sorting algorithm\n"));
   98   fprintf(fd, _("           (faster for repetitive blocks)\n"));
   99   fprintf(fd, _("  -D       Enable Delta filter\n"));
  100   fprintf(fd, _("  -a       Enable Adaptative block size reduction\n"));
  101   fprintf(fd, _("  -l       Disable LZP preprocessing\n"));
  102   fprintf(fd, _("  -p       Disable all Preprocessing techniques\n"));
  103   fprintf(fd, _("  -q       Run quietly/silently\n"));
  104   fprintf(fd, _("  -v       Set verbose mode\n"));
  105   fprintf(fd, _("  -V       Show version\n"));
  106   fprintf(fd, _("  -h       Show all Switches\n"));
  107   exit(0);
  108 }
  109 
  110 void MakeStdin()   
  111 {
  112   NumFiles=1;
  113   StdIn=1;
  114   Files=(char **)malloc(sizeof(char *));
  115   Files[0] = "<stdin>";
  116 }
  117 
  118 void Options(int argc,char * argv[])
  119 {
  120 char *s, *ss=NULL;
  121 sint32 i;
  122 
  123 if (!strcmp(argv[0], "grunzip")) UseCompression=0;
  124 if (!strcmp(argv[0], "grzcat"))
  125   {
  126     UseCompression=0;
  127     StdOut=1;
  128     Verbose=0;
  129   }
  130      
  131 for (i=1; i<argc; i++)
  132    {
  133    s = argv[i];
  134    if (*s!='-' || AreFiles)
  135    {
  136      if (StdIn) ShowUsage(stderr);
  137      ++NumFiles;
  138      Files = (char **)realloc(Files, NumFiles*sizeof(char *));
  139      Files[NumFiles-1]=argv[i];
  140      continue;
  141    } 
  142    else
  143    {
  144      ++s;
  145      if (*s!='\0' && index("bimoLM", *s) && s[1]=='\0' && i<argc-1) 
  146        ss=argv[++i];
  147      else
  148        ss=s+1;
  149    }
  150    if (*s=='\0')
  151    {
  152      if (!StdIn)
  153        MakeStdin();
  154      continue;
  155    }  
  156    switch (*s)
  157    {
  158      case 'b':
  159       { 
  160         if (!*ss) ShowUsage(stderr); else s=ss; 
  161         while ((*s>='0')&&(*s<='9')) s++;
  162         if (*s=='m')
  163         {
  164            BlockSize=atoi(ss)*1024*1024;
  165            break;
  166         }
  167         if (*s=='k')
  168         {
  169            BlockSize=atoi(ss)*1024;
  170            break;
  171         }
  172         ShowUsage(stderr);
  173        }
  174      case 'm':
  175       {
  176         if (!*ss) ShowUsage(stderr); else s=ss;
  177         while ((*s>='0')&&(*s<='9')) s++;
  178         s--;
  179         switch (atoi(ss))
  180         {
  181           case 1:{Mode=GRZ_Compression_BWT+GRZ_Compression_WFC;break;}
  182           case 2:{Mode=GRZ_Compression_BWT+GRZ_Compression_MTF;break;}
  183           case 3:{Mode=GRZ_Compression_ST4+GRZ_Compression_WFC;break;}
  184           case 4:{Mode=GRZ_Compression_ST4+GRZ_Compression_MTF;break;}
  185           default:ShowUsage(stderr);
  186         }
  187         break;
  188       }
  189      case 'o':
  190       {
  191         if (!*ss || StdOut) ShowUsage(stderr);
  192         if (FileOut) free(FileOut);
  193         FileOut = strdup(ss);
  194         break;
  195       }
  196      case 'L':
  197       {
  198         if (!*ss) ShowUsage(stderr); else s=ss;
  199         while ((*s>='0')&&(*s<='9')) s++;
  200         s--;
  201         switch (atoi(ss))
  202         {
  203           case 10:{LZPHTS=GRZ_LZP_HTS10;break;}
  204           case 11:{LZPHTS=GRZ_LZP_HTS11;break;}
  205           case 12:{LZPHTS=GRZ_LZP_HTS12;break;}
  206           case 13:{LZPHTS=GRZ_LZP_HTS13;break;}
  207           case 14:{LZPHTS=GRZ_LZP_HTS14;break;}
  208           case 15:{LZPHTS=GRZ_LZP_HTS15;break;}
  209           case 16:{LZPHTS=GRZ_LZP_HTS16;break;}
  210           case 17:{LZPHTS=GRZ_LZP_HTS17;break;}
  211           case 18:{LZPHTS=GRZ_LZP_HTS18;break;}
  212           default:ShowUsage(stderr);
  213         }
  214         break;
  215       }
  216      case 'M':
  217       {
  218         if (!*ss) ShowUsage(stderr); else s=ss;
  219         while ((*s>='0')&&(*s<='9')) s++;
  220         s--;
  221         switch (atoi(ss))
  222         {
  223           case 2 :{LZPMML=GRZ_LZP_MML2;break;}
  224           case 5 :{LZPMML=GRZ_LZP_MML5;break;}
  225           case 8 :{LZPMML=GRZ_LZP_MML8;break;}
  226           case 11:{LZPMML=GRZ_LZP_MML11;break;}
  227           case 14:{LZPMML=GRZ_LZP_MML14;break;}
  228           case 17:{LZPMML=GRZ_LZP_MML17;break;}
  229           case 20:{LZPMML=GRZ_LZP_MML20;break;}
  230           case 23:{LZPMML=GRZ_LZP_MML23;break;}
  231           case 26:{LZPMML=GRZ_LZP_MML26;break;}
  232           case 29:{LZPMML=GRZ_LZP_MML29;break;}
  233           case 32:{LZPMML=GRZ_LZP_MML32;break;}
  234           case 35:{LZPMML=GRZ_LZP_MML35;break;}
  235           case 38:{LZPMML=GRZ_LZP_MML38;break;}
  236           case 41:{LZPMML=GRZ_LZP_MML41;break;}
  237           case 44:{LZPMML=GRZ_LZP_MML44;break;}
  238           case 47:{LZPMML=GRZ_LZP_MML47;break;}
  239           default:ShowUsage(stderr);
  240         }
  241         break;
  242       }
  243      case 'h': {ShowUsage(stdout);break;}
  244      case 'l': {UseLZP=0;break;}
  245      case 'a': {AdaptativeBSize=1;break;}
  246      case 'f': {ForceOverwrite=1;break;}      
  247      case 'i': {if (Files) ShowUsage(stderr);AreFiles=1;break;}
  248      case 'k': {Keep=1;break;}
  249      case 'r': {Keep=0;break;}      
  250      case 'c': {if (FileOut) ShowUsage(stderr);StdOut=1;break;}
  251      case 'd': {UseCompression=0;break;}
  252      case 'z': {UseCompression=1;break;}      
  253      case 'p': {DeltaFilter=0;UseLZP=0;AdaptativeBSize=0;break;}
  254      case 'q': {Verbose=0;break;}            
  255      case 'v': {Verbose=1;break;}
  256      case 'D': {DeltaFilter=1;break;}      
  257      case 'S': {FastBWTMode=0;break;}
  258      case 'V': {ShowVersion(stdout);exit(0);}      
  259      default : {ShowUsage(stderr);}
  260    }
  261  }
  262 }
  263 
  264 void MakeMode(void)
  265 {
  266  if ((BlockSize>8*1024*1024)||(BlockSize<1024)) ShowUsage(stderr);
  267  if (BlockSize>GRZ_MaxBlockSize) BlockSize=GRZ_MaxBlockSize;
  268  if (UseLZP)
  269    Mode=Mode+LZPMML+LZPHTS;
  270  else
  271    Mode+=GRZ_Disable_LZP;
  272  if (FastBWTMode)
  273    Mode+=GRZ_BWTSorting_Fast;
  274  else
  275    Mode+=GRZ_BWTSorting_Strong;
  276  if (DeltaFilter)
  277    Mode+=GRZ_Enable_DeltaFlt;
  278  else
  279    Mode+=GRZ_Disable_DeltaFlt;
  280 }
  281 
  282 void Compression(char * InputFile)
  283 {
  284 FILE * FInput=NULL;
  285 FILE * FOutput=NULL;
  286 char * OutputFile=NULL;
  287 sint32 l= strlen(InputFile);
  288 long FileSize=0;
  289 long ReadSize=0;
  290 struct stat buf;
  291 struct utimbuf timebuf;
  292 
  293   if (l>=4 && !strcmp(InputFile+(l-4),".grz"))
  294   {
  295     fprintf(stderr, _("grzip: input file %s has suffix .grz\n"), InputFile);
  296     fprintf(stderr, _("Will not try to compress it !!\n\n"));
  297     return;
  298   }
  299 
  300   if (StdIn)
  301     FInput = stdin;
  302   else
  303   {
  304     FInput=fopen(InputFile,"rb");
  305     if (FInput==NULL)
  306     {
  307       fprintf(stderr, _("grzip: cannot open input file %s!\n"), InputFile);
  308       return;
  309     }
  310 
  311     if (stat(InputFile, &buf) || !S_ISREG(buf.st_mode))
  312     {
  313       fprintf(stderr, _("grzip: %s is not a regular file. Ignored\n"), 
  314               InputFile);
  315       return;
  316     }
  317   }
  318 
  319   if (StdOut)
  320     FOutput=stdout;
  321   else
  322   {
  323     if (FileOut)
  324       OutputFile=FileOut;
  325     else
  326       OutputFile=malloc(l+5);
  327     if (!OutputFile) 
  328     {
  329       fprintf(stderr, _("grzip: available memory seems to be exhausted !!\n"));
  330       exit(-1);
  331     }
  332     if (!FileOut) 
  333     {
  334       strcpy(OutputFile, InputFile);
  335       strcpy(OutputFile+l, ".grz");
  336     }
  337     if (!ForceOverwrite)
  338     {
  339       if (stat(OutputFile, &buf)==0)
  340       {
  341     fprintf(stderr, _("File %s already exists.\n"), OutputFile);
  342     fprintf(stderr, _("Use -f option to force overwriting\n"));   
  343     return;
  344       }
  345     }
  346     FOutput=fopen(OutputFile,"wb");
  347   } 
  348    
  349   if (FOutput==NULL)
  350   {
  351     fclose(FInput);
  352     fprintf(stderr, _("Cannot create output file %s!\n"), OutputFile);
  353     return;
  354   }  
  355 
  356   if (Verbose)
  357     fprintf(stderr, "grzip: %s --> %s\n", InputFile, 
  358             OutputFile?OutputFile:"<stdout>");
  359 
  360   fwrite(GRZipIISign,1,sizeof(GRZipIISign),FOutput);
  361 
  362   if (!StdIn) 
  363   {
  364     if (fseek(FInput,0,SEEK_END))
  365     {
  366       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  367       fprintf(stderr, _("grzip: IO error on file %s!\n"), InputFile);
  368       return ;
  369     }
  370     FileSize=ftell(FInput);
  371     if (FileSize==-1)
  372     {
  373       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  374       fprintf(stderr, _("grzip: IO error on file %s!\n"), InputFile);
  375       return ;
  376     }
  377   
  378     if (fseek(FInput,0,SEEK_SET))
  379     {
  380       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  381       fprintf(stderr, _("grzip: IO error on file %s!\n"), InputFile);
  382       return ;
  383     }
  384   }
  385 
  386   uint8 * Input=(uint8 *)malloc(BlockSize+1024);
  387   if (Input==NULL)
  388   {
  389     if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  390     fprintf(stderr, _("grzip: not enough memory!\n"));
  391     return ;
  392   }
  393   uint8 * Output=(uint8 *)malloc(BlockSize+1024);
  394   if (Output==NULL)
  395   {
  396     free(Input);
  397     if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  398     fprintf(stderr, _("grzip: not enough memory!\n"));
  399     return ;
  400   }
  401 
  402   clock_t BeginTime = clock();
  403 
  404   /*  while ((feof(FInput)==0)&&(ftell(FInput)!=FileSize)) */
  405   while (!feof(FInput))
  406   {
  407     if (!StdIn)
  408     {
  409       if (Verbose)
  410         fprintf(stderr, "\r");
  411       double Tmp=ftell(FInput);Tmp=(100*Tmp)/FileSize;
  412       if (Verbose) {
  413         fprintf(stderr, _("Compressing %.55s (%02d%%)"), InputFile,(sint32)Tmp);
  414         fflush(stderr);
  415       }
  416     }
  417     sint32 NumRead=fread(Input,1,BlockSize,FInput);
  418     if (AdaptativeBSize)
  419     {
  420        sint32 NewSize=GRZip_GetAdaptativeBlockSize((uint8 *)Input,NumRead);
  421        if (NewSize!=NumRead)
  422        {
  423          long NewPos=ftell(FInput)+(long)(NewSize-NumRead);
  424          fseek(FInput,NewPos,SEEK_SET);
  425          NumRead=NewSize;
  426        }
  427     }
  428     ReadSize += (long) NumRead;
  429 
  430     sint32 Size=GRZip_CompressBlock(Input,NumRead,Output,Mode);
  431     
  432     fwrite(Output,1,Size,FOutput);
  433   }
  434   
  435   fprintf(stderr, "\r                                                                          \r");fflush(stderr);
  436 
  437   double TotTime=((double)(clock()-BeginTime))/CLOCKS_PER_SEC;
  438   if (Verbose)
  439     fprintf(stderr, 
  440        _("Compressed %ld Bytes into %ld Bytes in %.3f seconds.\n"),    
  441        ReadSize, ftell(FOutput),TotTime);
  442 
  443   if (OutputFile && !StdIn && !StdOut)
  444   {    
  445     timebuf.modtime=buf.st_mtime;
  446     timebuf.actime=buf.st_atime;     
  447     utime(OutputFile, &timebuf);
  448   }
  449    
  450   free(Input);free(Output);
  451   if (FInput!=stdin) fclose(FInput);
  452   if (FOutput!=stdout) fclose(FOutput);
  453   if (OutputFile) free(OutputFile);
  454      
  455   if (!StdIn && !StdOut && !Keep)
  456     unlink(InputFile);
  457 }
  458 
  459 void Decompression(char * InputFile)
  460 {
  461 FILE * FInput=NULL;
  462 FILE * FOutput=NULL;
  463 char * OutputFile=NULL;
  464 sint32 l= strlen(InputFile);
  465 long FileSize=0;
  466 long OutSize=0;
  467 long ReadSize=0;
  468 uint8 BlockSign[28];
  469 uint8 SignTest[sizeof(GRZipIISign)];
  470 struct stat buf;
  471 struct utimbuf timebuf;   
  472 
  473   if (StdIn)
  474     FInput = stdin;
  475   else
  476   {
  477     if (l<4 || strcmp(InputFile+(l-4),".grz"))
  478     {
  479       fprintf(stderr, _("grzip: input file %s does not have suffix .grz\n"), InputFile);
  480       fprintf(stderr, _("Will not try to decompress it !!\n"));
  481       return;
  482     }
  483 
  484     FInput=fopen(InputFile,"rb");
  485     if (FInput==NULL)
  486     {
  487       fprintf(stderr, _("grzip: cannot open input file %s!\n"), InputFile);
  488       return;
  489     }
  490 
  491     if (stat(InputFile, &buf) || !S_ISREG(buf.st_mode))
  492     {
  493       fprintf(stderr, _("grzip: %s is not a regular file. Ignored\n"), 
  494               InputFile);
  495       return;
  496     }
  497   }
  498 
  499   if (StdOut)
  500     FOutput=stdout;
  501   else
  502   {
  503     if (FileOut)
  504       OutputFile=FileOut;
  505     else
  506     {
  507       OutputFile = malloc(l+5);
  508       if (!OutputFile) 
  509       {
  510         fprintf(stderr, _("grzip: available memory seems to be exhausted !!\n"));
  511         exit(-1);
  512       }
  513       strcpy(OutputFile, InputFile);
  514       OutputFile[l-4] = '\0';
  515     }
  516     if (!ForceOverwrite)
  517     {
  518        struct stat buf;
  519        if (stat(OutputFile, &buf)==0)
  520        {
  521       fprintf(stderr, _("grzip: file %s already exists.\n"), OutputFile);
  522       fprintf(stderr, _("Use -f option to force overwriting\n"));     
  523       return;
  524        }
  525     }
  526     FOutput=fopen(OutputFile,"wb");
  527   } 
  528    
  529   if (FOutput==NULL)
  530   {
  531     fclose(FInput);
  532     fprintf(stderr, _("grzip: cannot create output file %s!\n"), OutputFile);
  533     return;
  534   }  
  535 
  536   if (Verbose)
  537     fprintf(stderr, _("grzip: %s --> %s\n"), InputFile, 
  538             OutputFile?OutputFile:"<stdout>");
  539   
  540   if (fread(SignTest,1,sizeof(GRZipIISign),FInput)!=sizeof(GRZipIISign))
  541   {
  542     if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  543     fprintf(stderr, _("This is not a grzip archive!\n"));
  544     return;
  545   }
  546 
  547   ReadSize=sizeof(GRZipIISign);
  548 
  549   if (memcmp(SignTest,GRZipIISign,sizeof(GRZipIISign))!=0)
  550   {
  551     if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  552     fprintf(stderr, _("This is not a grzip archive!\n"));
  553     return;
  554   }
  555 
  556   if (!StdIn)
  557   {
  558     if (fseek(FInput,0,SEEK_END))
  559     {
  560       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  561       fprintf(stderr, _("grzip: IO error on file %s!\n"), InputFile);
  562       return ;
  563     }
  564     FileSize=ftell(FInput);
  565     if (FileSize==-1)
  566     {
  567       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  568       fprintf(stderr, _("grzip: IO error on file %s!\n"), InputFile);
  569       return ;
  570     }
  571     if (fseek(FInput,sizeof(GRZipIISign),SEEK_SET))
  572     {
  573       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  574       fprintf(stderr, _("grzip: IO error on file %s!\n"), InputFile);
  575       return ;
  576     }
  577   }
  578 
  579   clock_t BeginTime = clock();
  580 
  581   while (!feof(FInput))
  582   {
  583     if (!StdIn)
  584     {
  585       if (ftell(FInput)==FileSize) break;
  586       if (Verbose)
  587         fprintf(stderr, "\r");
  588       double Tmp=ftell(FInput);Tmp=(100*Tmp)/FileSize;
  589       if (Verbose) {
  590         fprintf(stderr, _("Decompressing %.55s (%02d%%)"), InputFile,(sint32)Tmp);
  591         fflush(stderr);
  592       }
  593     }
  594 
  595     sint32 NumRead=fread(BlockSign,1,28,FInput);
  596     ReadSize += (long) NumRead;
  597 
  598     if (NumRead!=28)
  599     {
  600       if (FOutput!=stdout) fclose(FOutput); 
  601       if (StdIn) break;
  602       fclose(FInput);
  603       fprintf(stderr, _("grzip: unexpected end of file %s!\n"), InputFile);
  604       return ;
  605     }
  606 
  607     if (GRZip_CheckBlockSign(BlockSign,28)!=GRZ_NO_ERROR)
  608     {
  609       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  610       fprintf(stderr, _("grzip: CRC check failed!\n"));      
  611       return ;
  612     }
  613 
  614     uint8 * Input=(uint8 *)malloc(1024+(*(sint32 *)(BlockSign+16)));
  615     if (Input==NULL)
  616     {
  617       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  618       fprintf(stderr, _("grzip: available memory seems to be exhausted !!\n"));
  619       return ;
  620     }
  621 
  622     memcpy(Input,BlockSign,28);
  623 
  624     uint8 * Output=(uint8 *)malloc(1024+(*(sint32 *)(Input)));
  625     if (Output==NULL)
  626     {
  627       free(Input);
  628       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  629       fprintf(stderr, _("grzip: available memory seems to be exhausted !!\n"));
  630       return ;
  631     }    
  632 
  633     NumRead=fread(Input+28,1,*(sint32 *)(Input+16),FInput);
  634     ReadSize += (long) NumRead;
  635 
  636     if (NumRead!=*(sint32 *)(Input+16))
  637     {
  638       free(Input); free(Output);
  639       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  640       fprintf(stderr, _("grzip: unexpected end of file %s!\n"), InputFile);
  641       return ;
  642     }
  643 
  644     sint32 Size=GRZip_DecompressBlock(Input,NumRead+28,Output);
  645     OutSize += (long)Size;
  646 
  647     if (Size==GRZ_NOT_ENOUGH_MEMORY)
  648     {
  649       free(Input); free(Output);
  650       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  651       fprintf(stderr, _("grzip: available memory seems to be exhausted !!\n"));
  652       return ;
  653     }
  654 
  655     if (Size<0)
  656     {
  657       free(Input); free(Output);
  658       if (FOutput!=stdout) fclose(FOutput); fclose(FInput);
  659       fprintf(stderr, _("grzip: CRC check failed!\n"));
  660       return ;
  661     }
  662 
  663     fwrite(Output,1,Size,FOutput);
  664     free(Input); free(Output);
  665 
  666   }
  667   if (Verbose)
  668   {
  669     fprintf(stderr, "\r                                                                          \r");
  670     fflush(stderr);
  671   }
  672 
  673   double TotTime=((double)(clock()-BeginTime))/CLOCKS_PER_SEC;
  674   if (Verbose)
  675     fprintf(stderr, _("Decompressed %ld Bytes into %ld Bytes in %.3f seconds.\n"), ReadSize,OutSize,TotTime);
  676 
  677   if (OutputFile && !StdIn && !StdOut)
  678   {    
  679     timebuf.modtime=buf.st_mtime;
  680     timebuf.actime=buf.st_atime;     
  681     utime(OutputFile, &timebuf);
  682   }   
  683    
  684   if (FInput!=stdin) fclose(FInput);
  685   if (FOutput!=stdout) fclose(FOutput);
  686   if (OutputFile) free(OutputFile);
  687 
  688   if (!StdIn && !StdOut && !Keep)
  689     unlink(InputFile);
  690 }
  691 
  692 int main(int argc,char * argv[])
  693 {
  694   int i;
  695 
  696 #ifdef ENABLE_NLS
  697 #define PACKAGE "grzip"
  698   setlocale (LC_ALL, "");
  699   /* This is not for gettext but all i18n software should have this line. */
  700 
  701   bindtextdomain (PACKAGE, LOCALEDIR);
  702   textdomain(PACKAGE);
  703 #endif
  704 
  705   Options(argc, argv);
  706 
  707   if (!NumFiles) 
  708   {
  709     MakeStdin();
  710     if (!FileOut) StdOut=1;
  711     if (Verbose==-1) Verbose=0;
  712   }
  713 
  714   if (StdOut && NumFiles>1) {
  715     fprintf(stderr, _("grzip: cannot use <stdout> as output with several input files !!\n"));
  716     exit(-1);
  717   }
  718 
  719   MakeMode();
  720 
  721   if (Verbose) ShowVersion(stderr);
  722 
  723   for (i=0; i<NumFiles; i++)
  724   {
  725     if (UseCompression)
  726       Compression(Files[i]);
  727     else
  728       Decompression(Files[i]);
  729   }
  730 
  731   return 0;
  732 }
  733 
  734 /*----------------------------------------------*/
  735 /* End                                   main.c */
  736 /*----------------------------------------------*/