"Fossies" - the Fresh Open Source Software Archive

Member "xdelta3-3.0.11/examples/encode_decode_test.c" (24 Mar 2015, 4275 Bytes) of package /linux/misc/xdelta3-3.0.11.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.

    1 //
    2 // Permission to distribute this example by
    3 // Copyright (C) 2007 Ralf Junker
    4 // Ralf Junker <delphi@yunqa.de>
    5 // http://www.yunqa.de/delphi/
    6 
    7 //---------------------------------------------------------------------------
    8 
    9 #include <stdio.h>
   10 #include <sys/stat.h>
   11 #include "xdelta3.h"
   12 #include "xdelta3.c"
   13 
   14 //---------------------------------------------------------------------------
   15 
   16 int code (
   17   int encode,
   18   FILE*  InFile,
   19   FILE*  SrcFile ,
   20   FILE* OutFile,
   21   int BufSize )
   22 {
   23   int r, ret;
   24   struct stat statbuf;
   25   xd3_stream stream;
   26   xd3_config config;
   27   xd3_source source;
   28   void* Input_Buf;
   29   int Input_Buf_Read;
   30 
   31   if (BufSize < XD3_ALLOCSIZE)
   32     BufSize = XD3_ALLOCSIZE;
   33 
   34   memset (&stream, 0, sizeof (stream));
   35   memset (&source, 0, sizeof (source));
   36 
   37   xd3_init_config(&config, XD3_ADLER32);
   38   config.winsize = BufSize;
   39   xd3_config_stream(&stream, &config);
   40 
   41   if (SrcFile)
   42   {
   43     r = fstat(fileno(SrcFile), &statbuf);
   44     if (r)
   45       return r;
   46 
   47     source.blksize = BufSize;
   48     source.curblk = malloc(source.blksize);
   49 
   50     /* Load 1st block of stream. */
   51     r = fseek(SrcFile, 0, SEEK_SET);
   52     if (r)
   53       return r;
   54     source.onblk = fread((void*)source.curblk, 1, source.blksize, SrcFile);
   55     source.curblkno = 0;
   56     /* Set the stream. */
   57     xd3_set_source(&stream, &source);
   58   }
   59 
   60   Input_Buf = malloc(BufSize);
   61 
   62   fseek(InFile, 0, SEEK_SET);
   63   do
   64   {
   65     Input_Buf_Read = fread(Input_Buf, 1, BufSize, InFile);
   66     if (Input_Buf_Read < BufSize)
   67     {
   68       xd3_set_flags(&stream, XD3_FLUSH | stream.flags);
   69     }
   70     xd3_avail_input(&stream, Input_Buf, Input_Buf_Read);
   71 
   72 process:
   73     if (encode)
   74       ret = xd3_encode_input(&stream);
   75     else
   76       ret = xd3_decode_input(&stream);
   77 
   78     switch (ret)
   79     {
   80     case XD3_INPUT:
   81       {
   82         fprintf (stderr,"XD3_INPUT\n");
   83         continue;
   84       }
   85 
   86     case XD3_OUTPUT:
   87       {
   88         fprintf (stderr,"XD3_OUTPUT\n");
   89         r = fwrite(stream.next_out, 1, stream.avail_out, OutFile);
   90         if (r != (int)stream.avail_out)
   91           return r;
   92     xd3_consume_output(&stream);
   93         goto process;
   94       }
   95 
   96     case XD3_GETSRCBLK:
   97       {
   98         fprintf (stderr,"XD3_GETSRCBLK %qd\n", source.getblkno);
   99         if (SrcFile)
  100         {
  101           r = fseek(SrcFile, source.blksize * source.getblkno, SEEK_SET);
  102           if (r)
  103             return r;
  104           source.onblk = fread((void*)source.curblk, 1,
  105                    source.blksize, SrcFile);
  106           source.curblkno = source.getblkno;
  107         }
  108         goto process;
  109       }
  110 
  111     case XD3_GOTHEADER:
  112       {
  113         fprintf (stderr,"XD3_GOTHEADER\n");
  114         goto process;
  115       }
  116 
  117     case XD3_WINSTART:
  118       {
  119         fprintf (stderr,"XD3_WINSTART\n");
  120         goto process;
  121       }
  122 
  123     case XD3_WINFINISH:
  124       {
  125         fprintf (stderr,"XD3_WINFINISH\n");
  126         goto process;
  127       }
  128 
  129     default:
  130       {
  131         fprintf (stderr,"!!! INVALID %s %d !!!\n",
  132         stream.msg, ret);
  133         return ret;
  134       }
  135 
  136     }
  137 
  138   }
  139   while (Input_Buf_Read == BufSize);
  140 
  141   free(Input_Buf);
  142 
  143   free((void*)source.curblk);
  144   xd3_close_stream(&stream);
  145   xd3_free_stream(&stream);
  146 
  147   return 0;
  148 
  149 };
  150 
  151 
  152 int main(int argc, char* argv[])
  153 {
  154   FILE*  InFile;
  155   FILE*  SrcFile;
  156   FILE* OutFile;
  157   int r;
  158 
  159   if (argc != 3) {
  160     fprintf (stderr, "usage: %s source input\n", argv[0]);
  161     return 1;
  162   }
  163 
  164   char *input = argv[2];
  165   char *source = argv[1];
  166   const char *output = "encoded.testdata";
  167   const char *decoded = "decoded.testdata";
  168 
  169   /* Encode */
  170 
  171   InFile = fopen(input, "rb");
  172   SrcFile = fopen(source, "rb");
  173   OutFile = fopen(output, "wb");
  174 
  175   r = code (1, InFile, SrcFile, OutFile, 0x1000);
  176 
  177   fclose(OutFile);
  178   fclose(SrcFile);
  179   fclose(InFile);
  180 
  181   if (r) {
  182     fprintf (stderr, "Encode error: %d\n", r);
  183     return r;
  184   }
  185 
  186   /* Decode */
  187 
  188   InFile = fopen(output, "rb");
  189   SrcFile = fopen(source, "rb");
  190   OutFile = fopen(decoded, "wb");
  191 
  192   r = code (0, InFile, SrcFile, OutFile, 0x1000);
  193 
  194   fclose(OutFile);
  195   fclose(SrcFile);
  196   fclose(InFile);
  197 
  198   if (r) {
  199     fprintf (stderr, "Decode error: %d\n", r);
  200     return r;
  201   }
  202 
  203   return 0;
  204 }