home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lzop-1.00.tar.gz / lzop-1.00.tar / lzop-1.00 / src / compress.c < prev    next >
C/C++ Source or Header  |  1998-04-15  |  7KB  |  341 lines

  1. /* compress.c --
  2.  
  3.    This file is part of the lzop file compressor.
  4.  
  5.    Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
  6.    Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
  7.    Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
  8.  
  9.    lzop and the LZO library are free software; you can redistribute them
  10.    and/or modify them under the terms of the GNU General Public License as
  11.    published by the Free Software Foundation; either version 2 of
  12.    the License, or (at your option) any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; see the file COPYING.
  21.    If not, write to the Free Software Foundation, Inc.,
  22.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  
  24.    Markus F.X.J. Oberhumer
  25.    markus.oberhumer@jk.uni-linz.ac.at
  26.  */
  27.  
  28.  
  29. #include "lzop.h"
  30.  
  31.  
  32. /*************************************************************************
  33. //
  34. **************************************************************************/
  35.  
  36. int x_set_method(int m, int l)
  37. {
  38.     int r;
  39.  
  40. #if defined(WITH_LZO)
  41.     r = lzo_set_method(m,l);
  42.     if (r >= 0)
  43.         return r;
  44. #endif
  45. #if defined(WITH_ZLIB)
  46.     r = zlib_set_method(m,l);
  47.     if (r >= 0)
  48.         return r;
  49. #endif
  50.  
  51.     UNUSED(r);
  52.     return -1;
  53. }
  54.  
  55.  
  56. int x_get_method(header_t *h)
  57. {
  58.     int r;
  59.  
  60. #if defined(WITH_LZO)
  61.     r = lzo_get_method(h);
  62.     if (r >= 0)
  63.         return r;
  64. #endif
  65. #if defined(WITH_ZLIB)
  66.     r = zlib_get_method(h);
  67.     if (r >= 0)
  68.         return 0;
  69. #endif
  70.  
  71.     UNUSED(r);
  72.     return 14;
  73. }
  74.  
  75.  
  76. /*************************************************************************
  77. // memory setup
  78. **************************************************************************/
  79.  
  80. lzo_bool x_enter(const header_t *h)
  81. {
  82.     if (h == NULL)
  83.     {
  84.         lzo_bool ok = 1;
  85. #if defined(WITH_LZO)
  86.         ok &= lzo_enter(h);
  87. #endif
  88. #if defined(WITH_ZLIB)
  89.         ok &= zlib_enter(h);
  90. #endif
  91.         return ok;
  92.     }
  93.  
  94.     switch (h->method)
  95.     {
  96. #if defined(WITH_LZO)
  97.     case M_LZO1X_1:
  98.     case M_LZO1X_1_15:
  99.     case M_LZO1X_999:
  100.         return lzo_enter(h);
  101. #endif
  102. #if defined(WITH_ZLIB)
  103.     case M_ZLIB:
  104.         return zlib_enter(h);
  105. #endif
  106.     }
  107.     return 0;
  108. }
  109.  
  110.  
  111. void x_leave(const header_t *h)
  112. {
  113.     if (h == NULL)
  114.     {
  115. #if defined(WITH_ZLIB)
  116.         zlib_leave(h);
  117. #endif
  118. #if defined(WITH_LZO)
  119.         lzo_leave(h);
  120. #endif
  121.         return;
  122.     }
  123.  
  124.     switch (h->method)
  125.     {
  126. #if defined(WITH_LZO)
  127.     case M_LZO1X_1:
  128.     case M_LZO1X_1_15:
  129.     case M_LZO1X_999:
  130.         lzo_leave(h);
  131.         break;
  132. #endif
  133. #if defined(WITH_ZLIB)
  134.     case M_ZLIB:
  135.         zlib_leave(h);
  136.         break;
  137. #endif
  138.     }
  139. }
  140.  
  141.  
  142. /*************************************************************************
  143. // compress a file
  144. **************************************************************************/
  145.  
  146. lzo_bool x_compress(file_t *fip, file_t *fop, header_t *h)
  147. {
  148.     lzo_bool ok = 0;
  149.  
  150.     init_compress_header(h,fip,fop);
  151.     switch (h->method)
  152.     {
  153. #if defined(WITH_LZO)
  154.     case M_LZO1X_1:
  155.     case M_LZO1X_1_15:
  156.     case M_LZO1X_999:
  157.         lzo_init_compress_header(h);
  158.         break;
  159. #endif
  160. #if defined(WITH_ZLIB)
  161.     case M_ZLIB:
  162.         zlib_init_compress_header(h);
  163.         break;
  164. #endif
  165.     }
  166.  
  167.     if (!x_enter(h))
  168.         e_memory();
  169.  
  170.     if (opt_verbose > 1)
  171.     {
  172.         if (opt_unlink)
  173.             fprintf(con_term,"replacing %s with %s", fip->name, fop->name);
  174.         else
  175.             fprintf(con_term,"compressing %s into %s", fip->name, fop->name);
  176.         fflush(con_term);
  177.         set_err_nl(1);
  178.     }
  179.  
  180.     write_header(fop,h);
  181.  
  182.     fip->bytes_processed = fop->bytes_processed = 0;
  183.  
  184.     switch (h->method)
  185.     {
  186. #if defined(WITH_LZO)
  187.     case M_LZO1X_1:
  188.     case M_LZO1X_1_15:
  189.     case M_LZO1X_999:
  190.         ok = lzo_compress(fip,fop,h);
  191.         break;
  192. #endif
  193. #if defined(WITH_ZLIB)
  194.     case M_ZLIB:
  195.         ok = zlib_compress(fip,fop,h);
  196.         break;
  197. #endif
  198.     default:
  199.         fatal(fip,"Internal error");
  200.         ok = 0;
  201.         break;
  202.     }
  203.  
  204.     if (opt_cmd == CMD_COMPRESS && opt_verbose > 1)
  205.     {
  206.         fprintf(con_term, ok ? "\n" : " FAILED\n");
  207.         fflush(con_term);
  208.     }
  209.     set_err_nl(0);
  210.  
  211.     x_leave(h);
  212.     return ok;
  213. }
  214.  
  215.  
  216. /*************************************************************************
  217. // decompress a file
  218. **************************************************************************/
  219.  
  220. lzo_bool x_decompress(file_t *fip, file_t *fop,
  221.                       const header_t *h, lzo_bool skip)
  222. {
  223.     lzo_bool ok = 0;
  224.  
  225.     if (!x_enter(h))
  226.         e_memory();
  227.  
  228.     if (skip)
  229.     {
  230.         assert(opt_cmd != CMD_TEST);
  231.         assert(fop->fd < 0);
  232.         if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 0)
  233.             fprintf(con_term,"skipping %s [%s]", fip->name, fop->name);
  234.         fflush(con_term);
  235.         set_err_nl(1);
  236.     }
  237.     else if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 1)
  238.     {
  239.         if (opt_unlink)
  240.             fprintf(con_term,"restoring %s into %s", fip->name, fop->name);
  241.         else
  242.             fprintf(con_term,"decompressing %s into %s", fip->name, fop->name);
  243.         fflush(con_term);
  244.         set_err_nl(1);
  245.     }
  246.     else if (opt_cmd == CMD_TEST && opt_verbose > 0)
  247.     {
  248.         /* note: gzip is quiet by default when testing, lzop is not */
  249.         if (opt_verbose > 2)
  250.             fprintf(con_term,"testing %s [%s]", fip->name, fop->name);
  251.         else
  252.             fprintf(con_term,"testing %s", fip->name);
  253.         fflush(con_term);
  254.         set_err_nl(1);
  255.     }
  256.  
  257.     fip->bytes_processed = fop->bytes_processed = 0;
  258.  
  259.     switch (h->method)
  260.     {
  261. #if defined(WITH_LZO)
  262.     case M_LZO1X_1:
  263.     case M_LZO1X_1_15:
  264.     case M_LZO1X_999:
  265.         ok = lzo_decompress(fip,fop,h,skip);
  266.         break;
  267. #endif
  268. #if defined(WITH_ZLIB)
  269.     case M_ZLIB:
  270.         ok = zlib_decompress(fip,fop,h,skip);
  271.         break;
  272. #endif
  273.     default:
  274.         fatal(fip,"Internal error");
  275.         ok = 0;
  276.         break;
  277.     }
  278.  
  279.     if (skip && opt_cmd == CMD_DECOMPRESS && opt_verbose > 0)
  280.     {
  281.         fprintf(con_term, ok ? "\n" : " FAILED\n");
  282.         fflush(con_term);
  283.     }
  284.     else if (opt_cmd == CMD_DECOMPRESS && opt_verbose > 1)
  285.     {
  286.         fprintf(con_term, ok ? "\n" : " FAILED\n");
  287.         fflush(con_term);
  288.     }
  289.     else if (opt_cmd == CMD_TEST && opt_verbose > 0)
  290.     {
  291.         fprintf(con_term, ok ? " OK\n" : " FAILED\n");
  292.         fflush(con_term);
  293.     }
  294.     set_err_nl(0);
  295.  
  296.     if (ok && opt_cmd == CMD_TEST)
  297.         do_test(h,fop->bytes_processed,fip->bytes_processed);
  298.     if (ok && opt_cmd == CMD_LIST)
  299.         do_list(h,fop->bytes_processed,fip->bytes_processed);
  300.     if (ok && opt_cmd == CMD_LS)
  301.         do_ls(h,fop->bytes_processed,fip->bytes_processed);
  302.     if (ok && opt_cmd == CMD_INFO)
  303.         do_info(h,fop->bytes_processed,fip->bytes_processed);
  304.  
  305.     x_leave(h);
  306.     return ok;
  307. }
  308.  
  309.  
  310. /*************************************************************************
  311. //
  312. **************************************************************************/
  313.  
  314. void x_filter(lzo_byte *p, lzo_uint l, const header_t *h)
  315. {
  316.     unsigned f = (unsigned) h->filter;
  317.     lzo_bool c = (opt_cmd == CMD_COMPRESS);
  318.  
  319.     if (f == 0 || l <= 0)
  320.         return;
  321.     if (f == 1)
  322.     {
  323.         if (c) t_sub1(p,l); else t_add1(p,l);
  324.     }
  325.     else if (f <= 16)
  326.     {
  327.         if (c) t_sub(p,l,f); else t_add(p,l,f);
  328.     }
  329.     else
  330.     {
  331.         fatal(NULL,"Invalid filter");
  332.     }
  333. }
  334.  
  335.  
  336.  
  337. /*
  338. vi:ts=4
  339. */
  340.  
  341.