source: rtems/cpukit/libfs/src/jffs2/src/compr_rubin.c @ 0ec9bbc

5
Last change on this file since 0ec9bbc was 8bcd5a22, checked in by Rickard Strandqvist <rickard_strandqvist@…>, on 01/11/15 at 16:36:48

jffs2: compr_rubin: Remove unused function

Remove the function pulledbits() that is not used anywhere.

This was partially found by using a static code analysis program called cppcheck.

Signed-off-by: Rickard Strandqvist <rickard_strandqvist@…>
Reviewed-by: Richard Weinberger <richard@…>
Signed-off-by: Brian Norris <computersforpeace@…>

  • Property mode set to 100644
File size: 8.7 KB
Line 
1#include "rtems-jffs2-config.h"
2
3/*
4 * JFFS2 -- Journalling Flash File System, Version 2.
5 *
6 * Copyright © 2001-2007 Red Hat, Inc.
7 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
8 *
9 * Created by Arjan van de Ven <arjanv@redhat.com>
10 *
11 * For licensing information, see the file 'LICENCE' in this directory.
12 *
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/string.h>
18#include <linux/types.h>
19#include <linux/jffs2.h>
20#include <linux/errno.h>
21#include "compr.h"
22
23
24#define RUBIN_REG_SIZE   16
25#define UPPER_BIT_RUBIN    (((long) 1)<<(RUBIN_REG_SIZE-1))
26#define LOWER_BITS_RUBIN   ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
27
28
29#define BIT_DIVIDER_MIPS 1043
30static int bits_mips[8] = { 277, 249, 290, 267, 229, 341, 212, 241};
31
32struct pushpull {
33        unsigned char *buf;
34        unsigned int buflen;
35        unsigned int ofs;
36        unsigned int reserve;
37};
38
39struct rubin_state {
40        unsigned long p;
41        unsigned long q;
42        unsigned long rec_q;
43        long bit_number;
44        struct pushpull pp;
45        int bit_divider;
46        int bits[8];
47};
48
49static inline void init_pushpull(struct pushpull *pp, char *buf,
50                                 unsigned buflen, unsigned ofs,
51                                 unsigned reserve)
52{
53        pp->buf = buf;
54        pp->buflen = buflen;
55        pp->ofs = ofs;
56        pp->reserve = reserve;
57}
58
59static inline int pushbit(struct pushpull *pp, int bit, int use_reserved)
60{
61        if (pp->ofs >= pp->buflen - (use_reserved?0:pp->reserve))
62                return -ENOSPC;
63
64        if (bit)
65                pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs & 7)));
66        else
67                pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs & 7)));
68
69        pp->ofs++;
70
71        return 0;
72}
73
74static inline int pushedbits(struct pushpull *pp)
75{
76        return pp->ofs;
77}
78
79static inline int pullbit(struct pushpull *pp)
80{
81        int bit;
82
83        bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1;
84
85        pp->ofs++;
86        return bit;
87}
88
89
90static void init_rubin(struct rubin_state *rs, int div, int *bits)
91{
92        int c;
93
94        rs->q = 0;
95        rs->p = (long) (2 * UPPER_BIT_RUBIN);
96        rs->bit_number = (long) 0;
97        rs->bit_divider = div;
98
99        for (c=0; c<8; c++)
100                rs->bits[c] = bits[c];
101}
102
103
104static int encode(struct rubin_state *rs, long A, long B, int symbol)
105{
106
107        long i0, i1;
108        int ret;
109
110        while ((rs->q >= UPPER_BIT_RUBIN) ||
111               ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
112                rs->bit_number++;
113
114                ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
115                if (ret)
116                        return ret;
117                rs->q &= LOWER_BITS_RUBIN;
118                rs->q <<= 1;
119                rs->p <<= 1;
120        }
121        i0 = A * rs->p / (A + B);
122        if (i0 <= 0)
123                i0 = 1;
124
125        if (i0 >= rs->p)
126                i0 = rs->p - 1;
127
128        i1 = rs->p - i0;
129
130        if (symbol == 0)
131                rs->p = i0;
132        else {
133                rs->p = i1;
134                rs->q += i0;
135        }
136        return 0;
137}
138
139
140static void end_rubin(struct rubin_state *rs)
141{
142
143        int i;
144
145        for (i = 0; i < RUBIN_REG_SIZE; i++) {
146                pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
147                rs->q &= LOWER_BITS_RUBIN;
148                rs->q <<= 1;
149        }
150}
151
152
153static void init_decode(struct rubin_state *rs, int div, int *bits)
154{
155        init_rubin(rs, div, bits);
156
157        /* behalve lower */
158        rs->rec_q = 0;
159
160        for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE;
161             rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp)))
162                ;
163}
164
165static void __do_decode(struct rubin_state *rs, unsigned long p,
166                        unsigned long q)
167{
168        register unsigned long lower_bits_rubin = LOWER_BITS_RUBIN;
169        unsigned long rec_q;
170        int c, bits = 0;
171
172        /*
173         * First, work out how many bits we need from the input stream.
174         * Note that we have already done the initial check on this
175         * loop prior to calling this function.
176         */
177        do {
178                bits++;
179                q &= lower_bits_rubin;
180                q <<= 1;
181                p <<= 1;
182        } while ((q >= UPPER_BIT_RUBIN) || ((p + q) <= UPPER_BIT_RUBIN));
183
184        rs->p = p;
185        rs->q = q;
186
187        rs->bit_number += bits;
188
189        /*
190         * Now get the bits.  We really want this to be "get n bits".
191         */
192        rec_q = rs->rec_q;
193        do {
194                c = pullbit(&rs->pp);
195                rec_q &= lower_bits_rubin;
196                rec_q <<= 1;
197                rec_q += c;
198        } while (--bits);
199        rs->rec_q = rec_q;
200}
201
202static int decode(struct rubin_state *rs, long A, long B)
203{
204        unsigned long p = rs->p, q = rs->q;
205        long i0, threshold;
206        int symbol;
207
208        if (q >= UPPER_BIT_RUBIN || ((p + q) <= UPPER_BIT_RUBIN))
209                __do_decode(rs, p, q);
210
211        i0 = A * rs->p / (A + B);
212        if (i0 <= 0)
213                i0 = 1;
214
215        if (i0 >= rs->p)
216                i0 = rs->p - 1;
217
218        threshold = rs->q + i0;
219        symbol = rs->rec_q >= threshold;
220        if (rs->rec_q >= threshold) {
221                rs->q += i0;
222                i0 = rs->p - i0;
223        }
224
225        rs->p = i0;
226
227        return symbol;
228}
229
230
231
232static int out_byte(struct rubin_state *rs, unsigned char byte)
233{
234        int i, ret;
235        struct rubin_state rs_copy;
236        rs_copy = *rs;
237
238        for (i=0; i<8; i++) {
239                ret = encode(rs, rs->bit_divider-rs->bits[i],
240                             rs->bits[i], byte & 1);
241                if (ret) {
242                        /* Failed. Restore old state */
243                        *rs = rs_copy;
244                        return ret;
245                }
246                byte >>= 1 ;
247        }
248        return 0;
249}
250
251static int in_byte(struct rubin_state *rs)
252{
253        int i, result = 0, bit_divider = rs->bit_divider;
254
255        for (i = 0; i < 8; i++)
256                result |= decode(rs, bit_divider - rs->bits[i],
257                                 rs->bits[i]) << i;
258
259        return result;
260}
261
262
263
264static int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in,
265                             unsigned char *cpage_out, uint32_t *sourcelen,
266                             uint32_t *dstlen)
267        {
268        int outpos = 0;
269        int pos=0;
270        struct rubin_state rs;
271
272        init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32);
273
274        init_rubin(&rs, bit_divider, bits);
275
276        while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
277                pos++;
278
279        end_rubin(&rs);
280
281        if (outpos > pos) {
282                /* We failed */
283                return -1;
284        }
285
286        /* Tell the caller how much we managed to compress,
287         * and how much space it took */
288
289        outpos = (pushedbits(&rs.pp)+7)/8;
290
291        if (outpos >= pos)
292                return -1; /* We didn't actually compress */
293        *sourcelen = pos;
294        *dstlen = outpos;
295        return 0;
296}
297#if 0
298/* _compress returns the compressed size, -1 if bigger */
299int jffs2_rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out,
300                   uint32_t *sourcelen, uint32_t *dstlen)
301{
302        return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in,
303                                 cpage_out, sourcelen, dstlen);
304}
305#endif
306static int jffs2_dynrubin_compress(unsigned char *data_in,
307                                   unsigned char *cpage_out,
308                                   uint32_t *sourcelen, uint32_t *dstlen)
309{
310        int bits[8];
311        unsigned char histo[256];
312        int i;
313        int ret;
314        uint32_t mysrclen, mydstlen;
315
316        mysrclen = *sourcelen;
317        mydstlen = *dstlen - 8;
318
319        if (*dstlen <= 12)
320                return -1;
321
322        memset(histo, 0, 256);
323        for (i=0; i<mysrclen; i++)
324                histo[data_in[i]]++;
325        memset(bits, 0, sizeof(int)*8);
326        for (i=0; i<256; i++) {
327                if (i&128)
328                        bits[7] += histo[i];
329                if (i&64)
330                        bits[6] += histo[i];
331                if (i&32)
332                        bits[5] += histo[i];
333                if (i&16)
334                        bits[4] += histo[i];
335                if (i&8)
336                        bits[3] += histo[i];
337                if (i&4)
338                        bits[2] += histo[i];
339                if (i&2)
340                        bits[1] += histo[i];
341                if (i&1)
342                        bits[0] += histo[i];
343        }
344
345        for (i=0; i<8; i++) {
346                bits[i] = (bits[i] * 256) / mysrclen;
347                if (!bits[i]) bits[i] = 1;
348                if (bits[i] > 255) bits[i] = 255;
349                cpage_out[i] = bits[i];
350        }
351
352        ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen,
353                                &mydstlen);
354        if (ret)
355                return ret;
356
357        /* Add back the 8 bytes we took for the probabilities */
358        mydstlen += 8;
359
360        if (mysrclen <= mydstlen) {
361                /* We compressed */
362                return -1;
363        }
364
365        *sourcelen = mysrclen;
366        *dstlen = mydstlen;
367        return 0;
368}
369
370static void rubin_do_decompress(int bit_divider, int *bits,
371                                unsigned char *cdata_in,
372                                unsigned char *page_out, uint32_t srclen,
373                                uint32_t destlen)
374{
375        int outpos = 0;
376        struct rubin_state rs;
377
378        init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
379        init_decode(&rs, bit_divider, bits);
380
381        while (outpos < destlen)
382                page_out[outpos++] = in_byte(&rs);
383}
384
385
386static int jffs2_rubinmips_decompress(unsigned char *data_in,
387                                      unsigned char *cpage_out,
388                                      uint32_t sourcelen, uint32_t dstlen)
389{
390        rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in,
391                            cpage_out, sourcelen, dstlen);
392        return 0;
393}
394
395static int jffs2_dynrubin_decompress(unsigned char *data_in,
396                                     unsigned char *cpage_out,
397                                     uint32_t sourcelen, uint32_t dstlen)
398{
399        int bits[8];
400        int c;
401
402        for (c=0; c<8; c++)
403                bits[c] = data_in[c];
404
405        rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8,
406                            dstlen);
407        return 0;
408}
409
410static struct jffs2_compressor jffs2_rubinmips_comp = {
411        .priority = JFFS2_RUBINMIPS_PRIORITY,
412        .name = "rubinmips",
413        .compr = JFFS2_COMPR_DYNRUBIN,
414        .compress = NULL, /*&jffs2_rubinmips_compress,*/
415        .decompress = &jffs2_rubinmips_decompress,
416#ifdef JFFS2_RUBINMIPS_DISABLED
417        .disabled = 1,
418#else
419        .disabled = 0,
420#endif
421};
422
423int jffs2_rubinmips_init(void)
424{
425        return jffs2_register_compressor(&jffs2_rubinmips_comp);
426}
427
428void jffs2_rubinmips_exit(void)
429{
430        jffs2_unregister_compressor(&jffs2_rubinmips_comp);
431}
432
433static struct jffs2_compressor jffs2_dynrubin_comp = {
434        .priority = JFFS2_DYNRUBIN_PRIORITY,
435        .name = "dynrubin",
436        .compr = JFFS2_COMPR_RUBINMIPS,
437        .compress = jffs2_dynrubin_compress,
438        .decompress = &jffs2_dynrubin_decompress,
439#ifdef JFFS2_DYNRUBIN_DISABLED
440        .disabled = 1,
441#else
442        .disabled = 0,
443#endif
444};
445
446int jffs2_dynrubin_init(void)
447{
448        return jffs2_register_compressor(&jffs2_dynrubin_comp);
449}
450
451void jffs2_dynrubin_exit(void)
452{
453        jffs2_unregister_compressor(&jffs2_dynrubin_comp);
454}
Note: See TracBrowser for help on using the repository browser.