summaryrefslogtreecommitdiff
path: root/bitmask_android/lzo/src/lzo1b_9x.c
blob: e00186d598d37dd0762bc811a5938cb5301dad1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* lzo1b_9x.c -- implementation of the LZO1B-999 compression algorithm

   This file is part of the LZO real-time data compression library.

   Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
   All Rights Reserved.

   The LZO library is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   The LZO library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the LZO library; see the file COPYING.
   If not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

   Markus F.X.J. Oberhumer
   <markus@oberhumer.com>
   http://www.oberhumer.com/opensource/lzo/
 */


#include "config1b.h"


/***********************************************************************
//
************************************************************************/

#define SWD_N          0xffffL          /* size of ring buffer */
#define SWD_THRESHOLD       2           /* lower limit for match length */
#define SWD_F            2048           /* upper limit for match length */


#define LZO1B 1
#define LZO_COMPRESS_T  lzo1b_999_t
#define lzo_swd_t       lzo1b_999_swd_t
#include "lzo_mchw.ch"



/***********************************************************************
//
************************************************************************/

static lzo_bytep
code_match ( LZO_COMPRESS_T *c, lzo_bytep op, lzo_uint m_len, lzo_uint m_off )
{
    if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET)
    {
        assert(m_len >= M2_MIN_LEN);
        assert(m_off >= M2_MIN_OFFSET);

        m_off -= M2_MIN_OFFSET;
        /* code match len + low offset bits */
        *op++ = LZO_BYTE(((m_len - (M2_MIN_LEN - 2)) << M2O_BITS) |
                          (m_off & M2O_MASK));
        /* code high offset bits */
        *op++ = LZO_BYTE(m_off >> M2O_BITS);
        c->m2_m++;
    }
    else
    {
        assert(m_len >= M3_MIN_LEN);
        assert(m_off <= M3_MAX_OFFSET);

        m_off -= M3_MIN_OFFSET - M3_EOF_OFFSET;
        /* code match len */
        if (m_len <= M3_MAX_LEN)
            *op++ = LZO_BYTE(M3_MARKER | (m_len - (M3_MIN_LEN - 1)));
        else
        {
            assert(m_len >= M4_MIN_LEN);
            /* code M4 match len flag */
            *op++ = M4_MARKER;
            /* code match len */
            m_len -= M4_MIN_LEN - 1;
            while (m_len > 255)
            {
                m_len -= 255;
                *op++ = 0;
            }
            assert(m_len > 0);
            *op++ = LZO_BYTE(m_len);
        }
        /* code low offset bits */
        *op++ = LZO_BYTE(m_off & M3O_MASK);
        /* code high offset bits */
        *op++ = LZO_BYTE(m_off >> M3O_BITS);

        c->r1_m_len = 0;
        c->m3_m++;
    }
    return op;
}


/***********************************************************************
// this is a public function, but there is no prototype in a header file
************************************************************************/

LZO_EXTERN(int)
lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
                                    lzo_bytep out, lzo_uintp out_len,
                                    lzo_voidp wrkmem,
                                    lzo_callback_p cb,
                                    lzo_uint max_chain );

LZO_PUBLIC(int)
lzo1b_999_compress_callback ( const lzo_bytep in , lzo_uint  in_len,
                                    lzo_bytep out, lzo_uintp out_len,
                                    lzo_voidp wrkmem,
                                    lzo_callback_p cb,
                                    lzo_uint max_chain )
{
    lzo_bytep op;
    const lzo_bytep ii;
    lzo_uint lit;
    lzo_uint m_len, m_off;
    LZO_COMPRESS_T cc;
    LZO_COMPRESS_T * const c = &cc;
    lzo_swd_p const swd = (lzo_swd_p) wrkmem;
    int r;

    /* sanity check */
    LZO_COMPILE_TIME_ASSERT(LZO1B_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T)

    c->init = 0;
    c->ip = c->in = in;
    c->in_end = in + in_len;
    c->cb = cb;
    c->r1_r = c->m3_r = c->m2_m = c->m3_m = 0;

    op = out;
    ii = c->ip;             /* point to start of literal run */
    lit = 0;
    c->r1_m_len = 0;

    r = init_match(c,swd,NULL,0,0);
    if (r != 0)
        return r;
    if (max_chain > 0)
        swd->max_chain = max_chain;

    r = find_match(c,swd,0,0);
    if (r != 0)
        return r;
    while (c->look > 0)
    {
        int lazy_match_min_gain = -1;
        lzo_uint ahead = 0;

        m_len = c->m_len;
        m_off = c->m_off;

#if 0
        printf("%5ld: %5d len:%3d off:%5d\n", (c->ip-c->look)-in, c->look,
                m_len, m_off);
#endif

        assert(c->ip - c->look >= in);
        if (lit == 0)
            ii = c->ip - c->look;
        assert(ii + lit == c->ip - c->look);
        assert(swd->b_char == *(c->ip - c->look));

        if ((m_len < M2_MIN_LEN) ||
            (m_len < M3_MIN_LEN && m_off > M2_MAX_OFFSET))
        {
            m_len = 0;
        }
        else
        {
            assert(c->ip - c->look - m_off >= in);
            assert(c->ip - c->look - m_off + m_len < c->ip);
            assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
                              m_len) == 0);

            if (lit > 0)
            {
                /* we have a current literal run: do not try a lazy match,
                   if the literal could be coded into a r1 match */
                if (lit == 1 && c->r1_m_len == M2_MIN_LEN)
                    lazy_match_min_gain = -1;
                else
                    lazy_match_min_gain = 1;

#if (M2_MIN_LEN == 2)
                if (m_len == 2)
                {
                    /* don't code a match of len 2 if we have to
                       code a literal run. Code a literal instead. */
                    m_len = 0;
                }
#endif
#if (M2_MIN_LEN == M3_MIN_LEN)
                if (m_len == M2_MIN_LEN && m_off > M2_MAX_OFFSET)
                {
                    /* don't code a M3 match of len 3 if we have to
                       code a literal run. Code a literal instead. */
                    m_len = 0;
                }
#endif
            }
            else
            {
                /* no current literal run: only try a lazy match,
                   if the literal could be coded into a r1 match */
                if (c->r1_m_len == M2_MIN_LEN)
                    lazy_match_min_gain = 0;
                else
                    lazy_match_min_gain = -1;
            }
        }


        /* try a lazy match */
        if (m_len == 0)
            lazy_match_min_gain = -1;
        if (lazy_match_min_gain >= 0 && c->look > m_len)
        {
            assert(m_len > 0);

            r = find_match(c,swd,1,0);
            assert(r == 0); LZO_UNUSED(r);
            assert(c->look > 0);

            if (m_len <= M2_MAX_LEN && m_off <= M2_MAX_OFFSET &&
                c->m_off > M2_MAX_OFFSET)
                lazy_match_min_gain += 1;

            if (c->m_len >= m_len + lazy_match_min_gain)
            {
                c->lazy++;
#if !defined(NDEBUG)
                m_len = c->m_len;
                m_off = c->m_off;
                assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off,
                                  m_len) == 0);
#endif
                lit++;
                assert(ii + lit == c->ip - c->look);
                continue;
            }
            else
            {
                ahead = 1;
                assert(ii + lit + 1 == c->ip - c->look);
            }
            assert(m_len > 0);
        }
        assert(ii + lit + ahead == c->ip - c->look);


        if (m_len == 0)
        {
            /* a literal */
            lit++;
            r = find_match(c,swd,1,0);
            assert(r == 0); LZO_UNUSED(r);
        }
        else
        {
            /* 1 - store run */
            if (lit > 0)
            {
                /* code current literal run */
                if (lit == 1 && c->r1_m_len == M2_MIN_LEN)
                {
                    /* Code a context sensitive R1 match. */
                    assert((op[-2] >> M2O_BITS) == (M2_MARKER >> M2O_BITS));
                    op[-2] &= M2O_MASK;
                    assert((op[-2] >> M2O_BITS) == 0);
                    /* copy 1 literal */
                    *op++ = *ii++;
                    assert(ii + ahead == c->ip - c->look);
                    c->r1_r++;
                }
                else
                {
                    op = STORE_RUN(op,ii,lit);
                }
                if (lit < R0FAST)
                    c->r1_m_len = m_len;
                else
                    c->r1_m_len = 0;
                lit = 0;
            }
            else
                c->r1_m_len = 0;

            /* 2 - code match */
            op = code_match(c,op,m_len,m_off);
            r = find_match(c,swd,m_len,1+ahead);
            assert(r == 0); LZO_UNUSED(r);
        }

        c->codesize = pd(op, out);
    }


    /* store final run */
    if (lit > 0)
        op = STORE_RUN(op,ii,lit);

#if defined(LZO_EOF_CODE)
    *op++ = M3_MARKER | 1;
    *op++ = 0;
    *op++ = 0;
#endif

    c->codesize = pd(op, out);
    assert(c->textsize == in_len);

    *out_len = pd(op, out);

    if (c->cb && c->cb->nprogress)
        (*c->cb->nprogress)(c->cb, c->textsize, c->codesize, 0);

#if 0
    printf("%ld %ld -> %ld: %ld %ld %ld %ld %ld\n",
        (long) c->textsize, (long)in_len, (long) c->codesize,
        c->r1_r, c->m3_r, c->m2_m, c->m3_m, c->lazy);
#endif
    return LZO_E_OK;
}



/***********************************************************************
//
************************************************************************/

LZO_PUBLIC(int)
lzo1b_999_compress  ( const lzo_bytep in , lzo_uint  in_len,
                            lzo_bytep out, lzo_uintp out_len,
                            lzo_voidp wrkmem )
{
    return lzo1b_999_compress_callback(in,in_len,out,out_len,wrkmem,
                                       (lzo_callback_p) 0, 0);
}


/*
vi:ts=4:et
*/