Home | History | Annotate | Download | only in C
      1 /* Delta.c -- Delta converter
      2 2009-05-26 : Igor Pavlov : Public domain */
      3 
      4 #include "Delta.h"
      5 
      6 void Delta_Init(Byte *state)
      7 {
      8   unsigned i;
      9   for (i = 0; i < DELTA_STATE_SIZE; i++)
     10     state[i] = 0;
     11 }
     12 
     13 static void MyMemCpy(Byte *dest, const Byte *src, unsigned size)
     14 {
     15   unsigned i;
     16   for (i = 0; i < size; i++)
     17     dest[i] = src[i];
     18 }
     19 
     20 void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size)
     21 {
     22   Byte buf[DELTA_STATE_SIZE];
     23   unsigned j = 0;
     24   MyMemCpy(buf, state, delta);
     25   {
     26     SizeT i;
     27     for (i = 0; i < size;)
     28     {
     29       for (j = 0; j < delta && i < size; i++, j++)
     30       {
     31         Byte b = data[i];
     32         data[i] = (Byte)(b - buf[j]);
     33         buf[j] = b;
     34       }
     35     }
     36   }
     37   if (j == delta)
     38     j = 0;
     39   MyMemCpy(state, buf + j, delta - j);
     40   MyMemCpy(state + delta - j, buf, j);
     41 }
     42 
     43 void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size)
     44 {
     45   Byte buf[DELTA_STATE_SIZE];
     46   unsigned j = 0;
     47   MyMemCpy(buf, state, delta);
     48   {
     49     SizeT i;
     50     for (i = 0; i < size;)
     51     {
     52       for (j = 0; j < delta && i < size; i++, j++)
     53       {
     54         buf[j] = data[i] = (Byte)(buf[j] + data[i]);
     55       }
     56     }
     57   }
     58   if (j == delta)
     59     j = 0;
     60   MyMemCpy(state, buf + j, delta - j);
     61   MyMemCpy(state + delta - j, buf, j);
     62 }
     63