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