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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [jffs2/] [compr_zlib.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
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
 * The original JFFS, from which the design for JFFS2 was derived,
9
 * was designed and implemented by Axis Communications AB.
10
 *
11
 * The contents of this file are subject to the Red Hat eCos Public
12
 * License Version 1.1 (the "Licence"); you may not use this file
13
 * except in compliance with the Licence.  You may obtain a copy of
14
 * the Licence at http://www.redhat.com/
15
 *
16
 * Software distributed under the Licence is distributed on an "AS IS"
17
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18
 * See the Licence for the specific language governing rights and
19
 * limitations under the Licence.
20
 *
21
 * The Original Code is JFFS2 - Journalling Flash File System, version 2
22
 *
23
 * Alternatively, the contents of this file may be used under the
24
 * terms of the GNU General Public License version 2 (the "GPL"), in
25
 * which case the provisions of the GPL are applicable instead of the
26
 * above.  If you wish to allow the use of your version of this file
27
 * only under the terms of the GPL and not to allow others to use your
28
 * version of this file under the RHEPL, indicate your decision by
29
 * deleting the provisions above and replace them with the notice and
30
 * other provisions required by the GPL.  If you do not delete the
31
 * provisions above, a recipient may use your version of this file
32
 * under either the RHEPL or the GPL.
33
 *
34
 * $Id: compr_zlib.c,v 1.1.1.1 2004-04-15 01:11:03 phoenix Exp $
35
 *
36
 */
37
 
38
#ifndef __KERNEL__
39
#error "The userspace support got too messy and was removed. Update your mkfs.jffs2"
40
#endif
41
 
42
#include <linux/config.h>
43
#include <linux/kernel.h>
44
#include <linux/mtd/compatmac.h> /* for min() */
45
#include <linux/slab.h>
46
#include <linux/jffs2.h>
47
#include <linux/zlib.h>
48
#include "nodelist.h"
49
 
50
        /* Plan: call deflate() with avail_in == *sourcelen,
51
                avail_out = *dstlen - 12 and flush == Z_FINISH.
52
                If it doesn't manage to finish, call it again with
53
                avail_in == 0 and avail_out set to the remaining 12
54
                bytes for it to clean up.
55
           Q: Is 12 bytes sufficient?
56
        */
57
#define STREAM_END_SPACE 12
58
 
59
static DECLARE_MUTEX(deflate_sem);
60
static DECLARE_MUTEX(inflate_sem);
61
static void *deflate_workspace;
62
static void *inflate_workspace;
63
 
64
int __init jffs2_zlib_init(void)
65
{
66
        deflate_workspace = vmalloc(zlib_deflate_workspacesize());
67
        if (!deflate_workspace) {
68
                printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
69
                return -ENOMEM;
70
        }
71
        D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
72
        inflate_workspace = vmalloc(zlib_inflate_workspacesize());
73
        if (!inflate_workspace) {
74
                printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
75
                vfree(deflate_workspace);
76
                return -ENOMEM;
77
        }
78
        D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
79
        return 0;
80
}
81
 
82
void jffs2_zlib_exit(void)
83
{
84
        vfree(deflate_workspace);
85
        vfree(inflate_workspace);
86
}
87
 
88
int zlib_compress(unsigned char *data_in, unsigned char *cpage_out,
89
                   __u32 *sourcelen, __u32 *dstlen)
90
{
91
        z_stream strm;
92
        int ret;
93
 
94
        if (*dstlen <= STREAM_END_SPACE)
95
                return -1;
96
 
97
        down(&deflate_sem);
98
        strm.workspace = deflate_workspace;
99
 
100
        if (Z_OK != zlib_deflateInit(&strm, 3)) {
101
                printk(KERN_WARNING "deflateInit failed\n");
102
                up(&deflate_sem);
103
                return -1;
104
        }
105
 
106
        strm.next_in = data_in;
107
        strm.total_in = 0;
108
 
109
        strm.next_out = cpage_out;
110
        strm.total_out = 0;
111
 
112
        while (strm.total_out < *dstlen - STREAM_END_SPACE && strm.total_in < *sourcelen) {
113
                strm.avail_out = *dstlen - (strm.total_out + STREAM_END_SPACE);
114
                strm.avail_in = min((unsigned)(*sourcelen-strm.total_in), strm.avail_out);
115
                D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
116
                          strm.avail_in, strm.avail_out));
117
                ret = zlib_deflate(&strm, Z_PARTIAL_FLUSH);
118
                D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
119
                          strm.avail_in, strm.avail_out, strm.total_in, strm.total_out));
120
                if (ret != Z_OK) {
121
                        D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
122
                        zlib_deflateEnd(&strm);
123
                        up(&deflate_sem);
124
                        return -1;
125
                }
126
        }
127
        strm.avail_out += STREAM_END_SPACE;
128
        strm.avail_in = 0;
129
        ret = zlib_deflate(&strm, Z_FINISH);
130
        zlib_deflateEnd(&strm);
131
        up(&deflate_sem);
132
        if (ret != Z_STREAM_END) {
133
                D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
134
                return -1;
135
        }
136
 
137
        D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n",
138
                  strm.total_in, strm.total_out));
139
 
140
        if (strm.total_out >= strm.total_in)
141
                return -1;
142
 
143
        *dstlen = strm.total_out;
144
        *sourcelen = strm.total_in;
145
        return 0;
146
}
147
 
148
void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
149
                      __u32 srclen, __u32 destlen)
150
{
151
        z_stream strm;
152
        int ret;
153
 
154
        down(&inflate_sem);
155
        strm.workspace = inflate_workspace;
156
 
157
        if (Z_OK != zlib_inflateInit(&strm)) {
158
                printk(KERN_WARNING "inflateInit failed\n");
159
                up(&inflate_sem);
160
                return;
161
        }
162
        strm.next_in = data_in;
163
        strm.avail_in = srclen;
164
        strm.total_in = 0;
165
 
166
        strm.next_out = cpage_out;
167
        strm.avail_out = destlen;
168
        strm.total_out = 0;
169
 
170
        while((ret = zlib_inflate(&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(&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.