blob: e4189e51dd11fa4d2024f8259342dde95e8a6daf [file] [log] [blame]
wdenkdd875c72004-01-03 21:24:46 +00001/*
2 * uncompress.c
3 *
4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 2000-2002 Transmeta Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License (Version 2) as
9 * published by the Free Software Foundation.
10 *
11 * cramfs interfaces to the uncompression library. There's really just
12 * three entrypoints:
13 *
14 * - cramfs_uncompress_init() - called to initialize the thing.
15 * - cramfs_uncompress_exit() - tell me when you're done
16 * - cramfs_uncompress_block() - uncompress a block.
17 *
18 * NOTE NOTE NOTE! The uncompression is entirely single-threaded. We
19 * only have one stream, and we'll initialize it only once even if it
20 * then is used by multiple filesystems.
21 */
22
23#include <common.h>
24#include <malloc.h>
25#include <watchdog.h>
26#include <zlib.h>
27
Jon Loeligerdd60d122007-07-09 17:56:50 -050028#if defined(CONFIG_CMD_JFFS2)
wdenkdd875c72004-01-03 21:24:46 +000029
30static z_stream stream;
31
Marian Balakowicz321359f2008-01-08 18:11:43 +010032void *zalloc(void *, unsigned, unsigned);
33void zfree(void *, void *, unsigned);
wdenkdd875c72004-01-03 21:24:46 +000034
35/* Returns length of decompressed data. */
36int cramfs_uncompress_block (void *dst, void *src, int srclen)
37{
38 int err;
39
40 inflateReset (&stream);
41
42 stream.next_in = src;
43 stream.avail_in = srclen;
44
45 stream.next_out = dst;
46 stream.avail_out = 4096 * 2;
47
48 err = inflate (&stream, Z_FINISH);
49
50 if (err != Z_STREAM_END)
51 goto err;
52 return stream.total_out;
53
54 err:
55 /*printf ("Error %d while decompressing!\n", err); */
56 /*printf ("%p(%d)->%p\n", src, srclen, dst); */
57 return -1;
58}
59
60int cramfs_uncompress_init (void)
61{
62 int err;
63
64 stream.zalloc = zalloc;
65 stream.zfree = zfree;
66 stream.next_in = 0;
67 stream.avail_in = 0;
68
69#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
70 stream.outcb = (cb_func) WATCHDOG_RESET;
71#else
72 stream.outcb = Z_NULL;
73#endif /* CONFIG_HW_WATCHDOG */
74
75 err = inflateInit (&stream);
76 if (err != Z_OK) {
77 printf ("Error: inflateInit2() returned %d\n", err);
78 return -1;
79 }
80
81 return 0;
82}
83
84int cramfs_uncompress_exit (void)
85{
86 inflateEnd (&stream);
87 return 0;
88}
89
90#endif /* CFG_FS_CRAMFS */