1 /* filter.c --
2
3 This file is part of the lzop file compressor.
4
5 Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
6 All Rights Reserved.
7
8 lzop and the LZO library are free software; you can redistribute them
9 and/or modify them under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzop/
26 */
27
28
29 #include "conf.h"
30
31 #if (ACC_CC_MSC && (_MSC_VER >= 1000))
32 /* avoid '-W4' warnings */
33 # pragma warning(disable: 4244)
34 #endif
35
36
37 /*************************************************************************
38 //
39 **************************************************************************/
40
t_sub1(lzo_bytep p,lzo_uint l)41 void t_sub1(lzo_bytep p, lzo_uint l)
42 {
43 unsigned char b = 0;
44
45 if (l > 0) do {
46 *p -= b;
47 b += *p++;
48 } while (--l > 0);
49 }
50
51
t_add1(lzo_bytep p,lzo_uint l)52 void t_add1(lzo_bytep p, lzo_uint l)
53 {
54 unsigned char b = 0;
55
56 if (l > 0) do {
57 b += *p;
58 *p++ = b;
59 } while (--l > 0);
60 }
61
62
63 /*************************************************************************
64 //
65 **************************************************************************/
66
t_sub(lzo_bytep p,lzo_uint l,int n)67 void t_sub(lzo_bytep p, lzo_uint l, int n)
68 {
69 unsigned char b[16];
70 int i;
71
72 assert(n > 0 && n <= (int)sizeof(b));
73 if (l <= (lzo_uint)n)
74 return;
75
76 n--;
77 i = n; do b[i] = 0; while (--i >= 0);
78
79 i = n;
80 do {
81 *p -= b[i];
82 b[i] += *p++;
83 if (--i < 0)
84 i = n;
85 } while (--l > 0);
86 }
87
88
t_add(lzo_bytep p,lzo_uint l,int n)89 void t_add(lzo_bytep p, lzo_uint l, int n)
90 {
91 unsigned char b[16];
92 int i;
93
94 assert(n > 0 && n <= (int)sizeof(b));
95 if (l <= (lzo_uint)n)
96 return;
97
98 n--;
99 i = n; do b[i] = 0; while (--i >= 0);
100
101 i = n;
102 do {
103 b[i] += *p;
104 *p++ = b[i];
105 if (--i < 0)
106 i = n;
107 } while (--l > 0);
108 }
109
110
111 /*************************************************************************
112 //
113 **************************************************************************/
114
t_mtf(lzo_bytep p,lzo_uint l)115 void t_mtf(lzo_bytep p, lzo_uint l)
116 {
117 unsigned char b[256];
118 unsigned char c;
119 unsigned i;
120
121 if (l <= 1)
122 return;
123
124 i = 256; do { --i; b[i] = (unsigned char) i; } while (i != 0);
125
126 do {
127 c = *p;
128 for (i = 0; c != b[i]; )
129 i++;
130 *p++ = (unsigned char) i;
131 if (i > 0)
132 {
133 do b[i] = b[i-1]; while (--i > 0);
134 b[0] = c;
135 }
136 } while (--l > 0);
137 }
138
139
t_unmtf(lzo_bytep p,lzo_uint l)140 void t_unmtf(lzo_bytep p, lzo_uint l)
141 {
142 unsigned char b[256];
143 unsigned char c;
144 unsigned i;
145
146 if (l <= 1)
147 return;
148
149 i = 256; do { --i; b[i] = (unsigned char) i; } while (i != 0);
150
151 do {
152 i = *p;
153 c = b[i];
154 *p++ = c;
155 if (i > 0)
156 {
157 do b[i] = b[i-1]; while (--i > 0);
158 b[0] = c;
159 }
160 } while (--l > 0);
161 }
162
163
164 /* vim:set ts=4 sw=4 et: */
165