"Fossies" - the Fresh Open Source Software Archive

Member "xdelta3-3.1.0/xdelta3-lzma.h" (4 Jan 2016, 4602 Bytes) of package /linux/misc/xdelta3-3.1.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 "xdelta3-lzma.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.0.11_vs_3.1.0.

    1 /* xdelta 3 - delta compression tools and library
    2  * Copyright (C) 2012, 2013, 2014, 2015.  Joshua P. MacDonald
    3  *
    4  *  This program is free software; you can redistribute it and/or modify
    5  *  it under the terms of the GNU General Public License as published by
    6  *  the Free Software Foundation; either version 2 of the License, or
    7  *  (at your option) any later version.
    8  *
    9  *  This program is distributed in the hope that it will be useful,
   10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  *  GNU General Public License for more details.
   13  *
   14  *  You should have received a copy of the GNU General Public License
   15  *  along with this program; if not, write to the Free Software
   16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   17  */
   18 
   19 /* Note: The use of the _easy_ decoder means we're not calling the
   20  * xd3_stream malloc hooks.  TODO(jmacd) Fix if anyone cares. */
   21 
   22 #ifndef _XDELTA3_LZMA_H_
   23 #define _XDELTA3_LZMA_H_
   24 
   25 #include <lzma.h>
   26 
   27 typedef struct _xd3_lzma_stream xd3_lzma_stream;
   28 
   29 struct _xd3_lzma_stream {
   30   lzma_stream lzma;
   31   lzma_options_lzma options;
   32   lzma_filter filters[2];
   33 };
   34 
   35 static xd3_sec_stream* 
   36 xd3_lzma_alloc (xd3_stream *stream)
   37 {
   38   return (xd3_sec_stream*) xd3_alloc (stream, sizeof (xd3_lzma_stream), 1);
   39 }
   40 
   41 static void
   42 xd3_lzma_destroy (xd3_stream *stream, xd3_sec_stream *sec_stream)
   43 {
   44   xd3_lzma_stream *ls = (xd3_lzma_stream*) sec_stream;
   45   lzma_end (&ls->lzma);
   46   xd3_free (stream, ls);
   47 }
   48 
   49 static int
   50 xd3_lzma_init (xd3_stream *stream, xd3_lzma_stream *sec, int is_encode)
   51 {
   52   int ret;
   53 
   54   memset (&sec->lzma, 0, sizeof(sec->lzma));
   55 
   56   if (is_encode)
   57     {
   58       uint32_t preset = 
   59     (stream->flags & XD3_COMPLEVEL_MASK) >> XD3_COMPLEVEL_SHIFT;
   60 
   61       if (lzma_lzma_preset(&sec->options, preset)) 
   62     {
   63       stream->msg = "invalid lzma preset";
   64       return XD3_INVALID;
   65     }
   66 
   67       sec->filters[0].id = LZMA_FILTER_LZMA2;
   68       sec->filters[0].options = &sec->options;
   69       sec->filters[1].id = LZMA_VLI_UNKNOWN;
   70 
   71       ret = lzma_stream_encoder (&sec->lzma, &sec->filters[0], LZMA_CHECK_NONE);
   72     }
   73   else 
   74     {
   75       ret = lzma_stream_decoder (&sec->lzma, UINT64_MAX, LZMA_TELL_NO_CHECK);
   76     }
   77   
   78   if (ret != LZMA_OK)
   79     {
   80       stream->msg = "lzma stream init failed";
   81       return XD3_INTERNAL;
   82     }
   83 
   84   return 0;
   85 }
   86 
   87 static int xd3_decode_lzma (xd3_stream *stream, xd3_lzma_stream *sec,
   88              const uint8_t **input_pos,
   89              const uint8_t  *const input_end,
   90              uint8_t       **output_pos,
   91              const uint8_t  *const output_end)
   92 {
   93   uint8_t *output = *output_pos;
   94   const uint8_t *input = *input_pos;
   95   size_t avail_in = input_end - input;
   96   size_t avail_out = output_end - output;
   97 
   98   sec->lzma.avail_in = avail_in;
   99   sec->lzma.next_in = input;
  100   sec->lzma.avail_out = avail_out;
  101   sec->lzma.next_out = output;
  102   
  103   while (1) 
  104     {
  105       int lret = lzma_code (&sec->lzma, LZMA_RUN);
  106 
  107       switch (lret)
  108     {
  109     case LZMA_NO_CHECK: 
  110     case LZMA_OK:
  111       if (sec->lzma.avail_out == 0) 
  112         {
  113           (*output_pos) = sec->lzma.next_out;
  114           (*input_pos) = sec->lzma.next_in;
  115           return 0;
  116         }
  117       break;
  118 
  119     default:
  120       stream->msg = "lzma decoding error";
  121       return XD3_INTERNAL;
  122     }
  123     }
  124 }
  125 
  126 #if XD3_ENCODER
  127 
  128 static int xd3_encode_lzma (xd3_stream *stream, 
  129              xd3_lzma_stream *sec, 
  130              xd3_output   *input,
  131              xd3_output   *output,
  132              xd3_sec_cfg  *cfg)
  133 
  134 {
  135   lzma_action action = LZMA_RUN;
  136 
  137   cfg->inefficient = 1;  /* Can't skip windows */
  138   sec->lzma.next_in = NULL;
  139   sec->lzma.avail_in = 0;
  140   sec->lzma.next_out = (output->base + output->next);
  141   sec->lzma.avail_out = (output->avail - output->next);
  142 
  143   while (1)
  144     {
  145       int lret;
  146       size_t nwrite;
  147       if (sec->lzma.avail_in == 0 && input != NULL)
  148     {
  149       sec->lzma.avail_in = input->next;
  150       sec->lzma.next_in = input->base;
  151       
  152       if ((input = input->next_page) == NULL)
  153         {
  154           action = LZMA_SYNC_FLUSH;
  155         }
  156     }
  157 
  158       lret = lzma_code (&sec->lzma, action);
  159 
  160       nwrite = (output->avail - output->next) - sec->lzma.avail_out;
  161 
  162       if (nwrite != 0) 
  163     {
  164       output->next += nwrite;
  165 
  166       if (output->next == output->avail)
  167         {
  168           if ((output = xd3_alloc_output (stream, output)) == NULL)
  169         {
  170           return ENOMEM;
  171         }
  172           
  173           sec->lzma.next_out = output->base;
  174           sec->lzma.avail_out = output->avail;
  175         }
  176     }
  177 
  178       switch (lret)
  179     {
  180     case LZMA_OK:
  181       break;
  182 
  183     case LZMA_STREAM_END:
  184       return 0;
  185 
  186     default:
  187       stream->msg = "lzma encoding error";
  188       return XD3_INTERNAL;
  189     }
  190     }
  191 
  192   return 0;
  193 }
  194 
  195 #endif /* XD3_ENCODER */
  196 
  197 #endif /* _XDELTA3_LZMA_H_ */