1 /* Delta.c -- Delta converter
\r
2 2021-02-09 : Igor Pavlov : Public domain */
\r
8 void Delta_Init(Byte *state)
\r
11 for (i = 0; i < DELTA_STATE_SIZE; i++)
\r
16 void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size)
\r
18 Byte temp[DELTA_STATE_SIZE];
\r
27 while (++i != delta);
\r
36 *data++ = (Byte)(b - temp[i]);
\r
39 while (++i != size);
\r
47 state[k] = temp[i++];
\r
49 while (++k != delta);
\r
55 Byte *p = data + size - delta;
\r
60 while (++i != delta);
\r
63 const Byte *lim = data + delta;
\r
64 ptrdiff_t dif = -(ptrdiff_t)delta;
\r
66 if (((ptrdiff_t)size + dif) & 1)
\r
68 --p; *p = (Byte)(*p - p[dif]);
\r
73 --p; *p = (Byte)(*p - p[dif]);
\r
74 --p; *p = (Byte)(*p - p[dif]);
\r
81 --p; *p = (Byte)(*p - temp[--dif]);
\r
89 void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size)
\r
103 *data = (Byte)(*data + state[i++]);
\r
104 while (++data != lim);
\r
106 for (; delta != i; state++, delta--)
\r
113 #define B(n) b ## n
\r
114 #define I(n) Byte B(n) = state[n];
\r
115 #define U(n) { B(n) = (Byte)((B(n)) + *data++); data[-1] = (B(n)); }
\r
116 #define F(n) if (data != lim) { U(n) }
\r
121 if ((lim - data) & 1) { U(0) }
\r
122 while (data != lim) { U(0) U(0) }
\r
125 else if (delta == 2)
\r
128 lim -= 1; while (data < lim) { U(0) U(1) }
\r
132 else if (delta == 3)
\r
135 lim -= 2; while (data < lim) { U(0) U(1) U(2) }
\r
136 lim += 2; F(0) F(1)
\r
139 else if (delta == 4)
\r
141 I(0) I(1) I(2) I(3)
\r
142 lim -= 3; while (data < lim) { U(0) U(1) U(2) U(3) }
\r
143 lim += 3; F(0) F(1) F(2)
\r
151 *data = (Byte)(*data + state[i++]);
\r
154 while (i != delta);
\r
157 ptrdiff_t dif = -(ptrdiff_t)delta;
\r
159 *data = (Byte)(*data + data[dif]);
\r
160 while (++data != lim);
\r
168 while (++data != lim);
\r