OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [fs/] [jffs2/] [v2_0/] [src/] [compr_zlib.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright (C) 2001, 2002 Red Hat, Inc.
5
 *
6
 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7
 *
8
 * For licensing information, see the file 'LICENCE' in this directory.
9
 *
10
 * $Id: compr_zlib.c,v 1.1.1.1 2004-02-14 13:29:19 phoenix Exp $
11
 *
12
 */
13
 
14
#if !defined(__KERNEL__) && !defined(__ECOS)
15
#error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
16
#endif
17
 
18
#include <linux/config.h>
19
#include <linux/kernel.h>
20
#include <linux/mtd/compatmac.h> /* for min() */
21
#include <linux/slab.h>
22
#include <linux/zlib.h>
23
#include <linux/zutil.h>
24
#include <asm/semaphore.h>
25
#include "nodelist.h"
26
 
27
        /* Plan: call deflate() with avail_in == *sourcelen,
28
                avail_out = *dstlen - 12 and flush == Z_FINISH.
29
                If it doesn't manage to finish, call it again with
30
                avail_in == 0 and avail_out set to the remaining 12
31
                bytes for it to clean up.
32
           Q: Is 12 bytes sufficient?
33
        */
34
#define STREAM_END_SPACE 12
35
 
36
static DECLARE_MUTEX(deflate_sem);
37
static DECLARE_MUTEX(inflate_sem);
38
static z_stream inf_strm, def_strm;
39
 
40
#ifdef __KERNEL__ /* Linux-only */
41
int __init jffs2_zlib_init(void)
42
{
43
        def_strm.workspace = vmalloc(zlib_deflate_workspacesize());
44
        if (!def_strm.workspace) {
45
                printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
46
                return -ENOMEM;
47
        }
48
        D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
49
        inf_strm.workspace = vmalloc(zlib_inflate_workspacesize());
50
        if (!inf_strm.workspace) {
51
                printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
52
                vfree(def_strm.workspace);
53
                return -ENOMEM;
54
        }
55
        D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
56
        return 0;
57
}
58
 
59
void jffs2_zlib_exit(void)
60
{
61
        vfree(def_strm.workspace);
62
        vfree(inf_strm.workspace);
63
}
64
#endif /* __KERNEL__ */
65
 
66
int jffs2_zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
67
                   uint32_t *sourcelen, uint32_t *dstlen)
68
{
69
        int ret;
70
 
71
        if (*dstlen <= STREAM_END_SPACE)
72
                return -1;
73
 
74
        down(&deflate_sem);
75
 
76
        if (Z_OK != zlib_deflateInit(&def_strm, 3)) {
77
                printk(KERN_WARNING "deflateInit failed\n");
78
                up(&deflate_sem);
79
                return -1;
80
        }
81
 
82
        def_strm.next_in = data_in;
83
        def_strm.total_in = 0;
84
 
85
        def_strm.next_out = cpage_out;
86
        def_strm.total_out = 0;
87
 
88
        while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
89
                def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
90
                def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
91
                D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
92
                          def_strm.avail_in, def_strm.avail_out));
93
                ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
94
                D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
95
                          def_strm.avail_in, def_strm.avail_out, def_strm.total_in, def_strm.total_out));
96
                if (ret != Z_OK) {
97
                        D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
98
                        zlib_deflateEnd(&def_strm);
99
                        up(&deflate_sem);
100
                        return -1;
101
                }
102
        }
103
        def_strm.avail_out += STREAM_END_SPACE;
104
        def_strm.avail_in = 0;
105
        ret = zlib_deflate(&def_strm, Z_FINISH);
106
        zlib_deflateEnd(&def_strm);
107
 
108
        if (ret != Z_STREAM_END) {
109
                D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
110
                ret = -1;
111
                goto out;
112
        }
113
 
114
        if (def_strm.total_out >= def_strm.total_in) {
115
                D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld; failing\n",
116
                          def_strm.total_in, def_strm.total_out));
117
                ret = -1;
118
                goto out;
119
        }
120
 
121
        D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
122
                  def_strm.total_in, def_strm.total_out));
123
 
124
        *dstlen = def_strm.total_out;
125
        *sourcelen = def_strm.total_in;
126
        ret = 0;
127
 out:
128
        up(&deflate_sem);
129
        return ret;
130
}
131
 
132
void jffs2_zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
133
                      uint32_t srclen, uint32_t destlen)
134
{
135
        int ret;
136
        int wbits = MAX_WBITS;
137
 
138
        down(&inflate_sem);
139
 
140
        inf_strm.next_in = data_in;
141
        inf_strm.avail_in = srclen;
142
        inf_strm.total_in = 0;
143
 
144
        inf_strm.next_out = cpage_out;
145
        inf_strm.avail_out = destlen;
146
        inf_strm.total_out = 0;
147
 
148
        /* If it's deflate, and it's got no preset dictionary, then
149
           we can tell zlib to skip the adler32 check. */
150
        if (srclen > 2 && !(data_in[1] & PRESET_DICT) &&
151
            ((data_in[0] & 0x0f) == Z_DEFLATED) &&
152
            !(((data_in[0]<<8) + data_in[1]) % 31)) {
153
 
154
                D2(printk(KERN_DEBUG "inflate skipping adler32\n"));
155
                wbits = -((data_in[0] >> 4) + 8);
156
                inf_strm.next_in += 2;
157
                inf_strm.avail_in -= 2;
158
        } else {
159
                /* Let this remain D1 for now -- it should never happen */
160
                D1(printk(KERN_DEBUG "inflate not skipping adler32\n"));
161
        }
162
 
163
 
164
        if (Z_OK != zlib_inflateInit2(&inf_strm, wbits)) {
165
                printk(KERN_WARNING "inflateInit failed\n");
166
                up(&inflate_sem);
167
                return;
168
        }
169
 
170
        while((ret = zlib_inflate(&inf_strm, Z_FINISH)) == Z_OK)
171
                ;
172
        if (ret != Z_STREAM_END) {
173
                printk(KERN_NOTICE "inflate returned %d\n", ret);
174
        }
175
        zlib_inflateEnd(&inf_strm);
176
        up(&inflate_sem);
177
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.