source: rtems/cpukit/libfs/src/jffs2/src/compr.c @ 3ef7e744

5
Last change on this file since 3ef7e744 was 3c96bee, checked in by Sebastian Huber <sebastian.huber@…>, on 09/12/13 at 13:32:07

JFFS2: Add RTEMS support

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright © 2001-2007 Red Hat, Inc.
5 * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6 * Copyright © 2004 Ferenc Havasi <havasi@inf.u-szeged.hu>,
7 *                  University of Szeged, Hungary
8 * Copyright © 2013 embedded brains GmbH <rtems@embedded-brains.de>
9 *
10 * Created by Arjan van de Ven <arjan@infradead.org>
11 *
12 * For licensing information, see the file 'LICENCE' in this directory.
13 *
14 */
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18#include "compr.h"
19
20/* jffs2_compress:
21 * @data_in: Pointer to uncompressed data
22 * @cpage_out: Pointer to returned pointer to buffer for compressed data
23 * @datalen: On entry, holds the amount of data available for compression.
24 *      On exit, expected to hold the amount of data actually compressed.
25 * @cdatalen: On entry, holds the amount of space available for compressed
26 *      data. On exit, expected to hold the actual size of the compressed
27 *      data.
28 *
29 * Returns: Lower byte to be stored with data indicating compression type used.
30 * Zero is used to show that the data could not be compressed - the
31 * compressed version was actually larger than the original.
32 * Upper byte will be used later. (soon)
33 *
34 * If the cdata buffer isn't large enough to hold all the uncompressed data,
35 * jffs2_compress should compress as much as will fit, and should set
36 * *datalen accordingly to show the amount of data which were compressed.
37 */
38uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
39                        unsigned char *data_in, unsigned char **cpage_out,
40                        uint32_t *datalen, uint32_t *cdatalen)
41{
42        struct super_block *sb = OFNI_BS_2SFFJ(c);
43        rtems_jffs2_compressor_control *cc = sb->s_compressor_control;
44        int ret;
45
46        if (cc != NULL) {
47                *cpage_out = &cc->buffer[0];
48                ret = (*cc->compress)(cc, data_in, *cpage_out, datalen, cdatalen);
49        } else {
50                ret = JFFS2_COMPR_NONE;
51        }
52
53        if (ret == JFFS2_COMPR_NONE) {
54                *cpage_out = data_in;
55                *datalen = *cdatalen;
56        }
57        return ret;
58}
59
60int jffs2_decompress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
61                     uint16_t comprtype, unsigned char *cdata_in,
62                     unsigned char *data_out, uint32_t cdatalen, uint32_t datalen)
63{
64        struct super_block *sb = OFNI_BS_2SFFJ(c);
65        rtems_jffs2_compressor_control *cc = sb->s_compressor_control;
66
67        /* Older code had a bug where it would write non-zero 'usercompr'
68           fields. Deal with it. */
69        if ((comprtype & 0xff) <= JFFS2_COMPR_ZLIB)
70                comprtype &= 0xff;
71
72        switch (comprtype & 0xff) {
73        case JFFS2_COMPR_NONE:
74                /* This should be special-cased elsewhere, but we might as well deal with it */
75                memcpy(data_out, cdata_in, datalen);
76                break;
77        case JFFS2_COMPR_ZERO:
78                memset(data_out, 0, datalen);
79                break;
80        default:
81                if (cc != NULL) {
82                        return (*cc->decompress)(cc, comprtype, cdata_in, data_out, cdatalen, datalen);
83                } else {
84                        return -EIO;
85                }
86        }
87        return 0;
88}
Note: See TracBrowser for help on using the repository browser.