1/* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */ 2/* 3 * Squashfs - a compressed read only filesystem for Linux 4 * 5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 6 * Phillip Lougher <phillip@lougher.demon.co.uk> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 2, 11 * or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21 * 22 * zlib_wrapper.c 23 */ 24 25 26#include <linux/mutex.h> 27#include <linux/buffer_head.h> 28#include <linux/slab.h> 29#include <linux/zlib.h> 30#include <linux/vmalloc.h> 31 32#include "squashfs_fs.h" 33#include "squashfs_fs_sb.h" 34#include "squashfs.h" 35#include "decompressor.h" 36 37static void *zlib_init(struct squashfs_sb_info *dummy, void *buff, int len) 38{ 39 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL); 40 if (stream == NULL) 41 goto failed; 42 stream->workspace = vmalloc(zlib_inflate_workspacesize()); 43 if (stream->workspace == NULL) 44 goto failed; 45 46 return stream; 47 48failed: 49 ERROR("Failed to allocate zlib workspace\n"); 50 kfree(stream); 51 return ERR_PTR(-ENOMEM); 52} 53 54 55static void zlib_free(void *strm) 56{ 57 z_stream *stream = strm; 58 59 if (stream) 60 vfree(stream->workspace); 61 kfree(stream); 62} 63 64 65static int zlib_uncompress(struct squashfs_sb_info *msblk, void **buffer, 66 struct buffer_head **bh, int b, int offset, int length, int srclength, 67 int pages) 68{ 69 int zlib_err, zlib_init = 0; 70 int k = 0, page = 0; 71 z_stream *stream = msblk->stream; 72 73 mutex_lock(&msblk->read_data_mutex); 74 75 stream->avail_out = 0; 76 stream->avail_in = 0; 77 78 do { 79 if (stream->avail_in == 0 && k < b) { 80 int avail = min(length, msblk->devblksize - offset); 81 length -= avail; 82 wait_on_buffer(bh[k]); 83 if (!buffer_uptodate(bh[k])) 84 goto release_mutex; 85 86 stream->next_in = bh[k]->b_data + offset; 87 stream->avail_in = avail; 88 offset = 0; 89 } 90 91 if (stream->avail_out == 0 && page < pages) { 92 stream->next_out = buffer[page++]; 93 stream->avail_out = PAGE_CACHE_SIZE; 94 } 95 96 if (!zlib_init) { 97 zlib_err = zlib_inflateInit(stream); 98 if (zlib_err != Z_OK) { 99 ERROR("zlib_inflateInit returned unexpected " 100 "result 0x%x, srclength %d\n", 101 zlib_err, srclength); 102 goto release_mutex; 103 } 104 zlib_init = 1; 105 } 106 107 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH); 108 109 if (stream->avail_in == 0 && k < b) 110 put_bh(bh[k++]); 111 } while (zlib_err == Z_OK); 112 113 if (zlib_err != Z_STREAM_END) { 114 ERROR("zlib_inflate error, data probably corrupt\n"); 115 goto release_mutex; 116 } 117 118 zlib_err = zlib_inflateEnd(stream); 119 if (zlib_err != Z_OK) { 120 ERROR("zlib_inflate error, data probably corrupt\n"); 121 goto release_mutex; 122 } 123 124 if (k < b) { 125 ERROR("zlib_uncompress error, data remaining\n"); 126 goto release_mutex; 127 } 128 129 length = stream->total_out; 130 mutex_unlock(&msblk->read_data_mutex); 131 return length; 132 133release_mutex: 134 mutex_unlock(&msblk->read_data_mutex); 135 136 for (; k < b; k++) 137 put_bh(bh[k]); 138 139 return -EIO; 140} 141 142const struct squashfs_decompressor squashfs_zlib_comp_ops = { 143 .init = zlib_init, 144 .free = zlib_free, 145 .decompress = zlib_uncompress, 146 .id = ZLIB_COMPRESSION, 147 .name = "zlib", 148 .supported = 1 149}; 150