| 1 | /* Delta.c -- Delta converter\r |
| 2 | 2021-02-09 : Igor Pavlov : Public domain */\r |
| 3 | \r |
| 4 | #include "Precomp.h"\r |
| 5 | \r |
| 6 | #include "Delta.h"\r |
| 7 | \r |
| 8 | void Delta_Init(Byte *state)\r |
| 9 | {\r |
| 10 | unsigned i;\r |
| 11 | for (i = 0; i < DELTA_STATE_SIZE; i++)\r |
| 12 | state[i] = 0;\r |
| 13 | }\r |
| 14 | \r |
| 15 | \r |
| 16 | void Delta_Encode(Byte *state, unsigned delta, Byte *data, SizeT size)\r |
| 17 | {\r |
| 18 | Byte temp[DELTA_STATE_SIZE];\r |
| 19 | \r |
| 20 | if (size == 0)\r |
| 21 | return;\r |
| 22 | \r |
| 23 | {\r |
| 24 | unsigned i = 0;\r |
| 25 | do\r |
| 26 | temp[i] = state[i];\r |
| 27 | while (++i != delta);\r |
| 28 | }\r |
| 29 | \r |
| 30 | if (size <= delta)\r |
| 31 | {\r |
| 32 | unsigned i = 0, k;\r |
| 33 | do\r |
| 34 | {\r |
| 35 | Byte b = *data;\r |
| 36 | *data++ = (Byte)(b - temp[i]);\r |
| 37 | temp[i] = b;\r |
| 38 | }\r |
| 39 | while (++i != size);\r |
| 40 | \r |
| 41 | k = 0;\r |
| 42 | \r |
| 43 | do\r |
| 44 | {\r |
| 45 | if (i == delta)\r |
| 46 | i = 0;\r |
| 47 | state[k] = temp[i++];\r |
| 48 | }\r |
| 49 | while (++k != delta);\r |
| 50 | \r |
| 51 | return;\r |
| 52 | }\r |
| 53 | \r |
| 54 | {\r |
| 55 | Byte *p = data + size - delta;\r |
| 56 | {\r |
| 57 | unsigned i = 0;\r |
| 58 | do\r |
| 59 | state[i] = *p++;\r |
| 60 | while (++i != delta);\r |
| 61 | }\r |
| 62 | {\r |
| 63 | const Byte *lim = data + delta;\r |
| 64 | ptrdiff_t dif = -(ptrdiff_t)delta;\r |
| 65 | \r |
| 66 | if (((ptrdiff_t)size + dif) & 1)\r |
| 67 | {\r |
| 68 | --p; *p = (Byte)(*p - p[dif]);\r |
| 69 | }\r |
| 70 | \r |
| 71 | while (p != lim)\r |
| 72 | {\r |
| 73 | --p; *p = (Byte)(*p - p[dif]);\r |
| 74 | --p; *p = (Byte)(*p - p[dif]);\r |
| 75 | }\r |
| 76 | \r |
| 77 | dif = -dif;\r |
| 78 | \r |
| 79 | do\r |
| 80 | {\r |
| 81 | --p; *p = (Byte)(*p - temp[--dif]);\r |
| 82 | }\r |
| 83 | while (dif != 0);\r |
| 84 | }\r |
| 85 | }\r |
| 86 | }\r |
| 87 | \r |
| 88 | \r |
| 89 | void Delta_Decode(Byte *state, unsigned delta, Byte *data, SizeT size)\r |
| 90 | {\r |
| 91 | unsigned i;\r |
| 92 | const Byte *lim;\r |
| 93 | \r |
| 94 | if (size == 0)\r |
| 95 | return;\r |
| 96 | \r |
| 97 | i = 0;\r |
| 98 | lim = data + size;\r |
| 99 | \r |
| 100 | if (size <= delta)\r |
| 101 | {\r |
| 102 | do\r |
| 103 | *data = (Byte)(*data + state[i++]);\r |
| 104 | while (++data != lim);\r |
| 105 | \r |
| 106 | for (; delta != i; state++, delta--)\r |
| 107 | *state = state[i];\r |
| 108 | data -= i;\r |
| 109 | }\r |
| 110 | else\r |
| 111 | {\r |
| 112 | /*\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 |
| 117 | \r |
| 118 | if (delta == 1)\r |
| 119 | {\r |
| 120 | I(0)\r |
| 121 | if ((lim - data) & 1) { U(0) }\r |
| 122 | while (data != lim) { U(0) U(0) }\r |
| 123 | data -= 1;\r |
| 124 | }\r |
| 125 | else if (delta == 2)\r |
| 126 | {\r |
| 127 | I(0) I(1)\r |
| 128 | lim -= 1; while (data < lim) { U(0) U(1) }\r |
| 129 | lim += 1; F(0)\r |
| 130 | data -= 2;\r |
| 131 | }\r |
| 132 | else if (delta == 3)\r |
| 133 | {\r |
| 134 | I(0) I(1) I(2)\r |
| 135 | lim -= 2; while (data < lim) { U(0) U(1) U(2) }\r |
| 136 | lim += 2; F(0) F(1)\r |
| 137 | data -= 3;\r |
| 138 | }\r |
| 139 | else if (delta == 4)\r |
| 140 | {\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 |
| 144 | data -= 4;\r |
| 145 | }\r |
| 146 | else\r |
| 147 | */\r |
| 148 | {\r |
| 149 | do\r |
| 150 | {\r |
| 151 | *data = (Byte)(*data + state[i++]);\r |
| 152 | data++;\r |
| 153 | }\r |
| 154 | while (i != delta);\r |
| 155 | \r |
| 156 | {\r |
| 157 | ptrdiff_t dif = -(ptrdiff_t)delta;\r |
| 158 | do\r |
| 159 | *data = (Byte)(*data + data[dif]);\r |
| 160 | while (++data != lim);\r |
| 161 | data += dif;\r |
| 162 | }\r |
| 163 | }\r |
| 164 | }\r |
| 165 | \r |
| 166 | do\r |
| 167 | *state++ = *data;\r |
| 168 | while (++data != lim);\r |
| 169 | }\r |