Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Copyright (c) 2013, 2014
  3 * Phillip Lougher <phillip@squashfs.org.uk>
  4 *
  5 * This work is licensed under the terms of the GNU GPL, version 2. See
  6 * the COPYING file in the top-level directory.
  7 */
  8
  9#include <linux/buffer_head.h>
 10#include <linux/mutex.h>
 11#include <linux/slab.h>
 12#include <linux/vmalloc.h>
 13#include <linux/lz4.h>
 14
 15#include "squashfs_fs.h"
 16#include "squashfs_fs_sb.h"
 17#include "squashfs.h"
 18#include "decompressor.h"
 19#include "page_actor.h"
 20
 21#define LZ4_LEGACY	1
 22
 23struct lz4_comp_opts {
 24	__le32 version;
 25	__le32 flags;
 26};
 27
 28struct squashfs_lz4 {
 29	void *input;
 30	void *output;
 31};
 32
 33
 34static void *lz4_comp_opts(struct squashfs_sb_info *msblk,
 35	void *buff, int len)
 36{
 37	struct lz4_comp_opts *comp_opts = buff;
 38
 39	/* LZ4 compressed filesystems always have compression options */
 40	if (comp_opts == NULL || len < sizeof(*comp_opts))
 41		return ERR_PTR(-EIO);
 42
 43	if (le32_to_cpu(comp_opts->version) != LZ4_LEGACY) {
 44		/* LZ4 format currently used by the kernel is the 'legacy'
 45		 * format */
 46		ERROR("Unknown LZ4 version\n");
 47		return ERR_PTR(-EINVAL);
 48	}
 49
 50	return NULL;
 51}
 52
 53
 54static void *lz4_init(struct squashfs_sb_info *msblk, void *buff)
 55{
 56	int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
 57	struct squashfs_lz4 *stream;
 58
 59	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
 60	if (stream == NULL)
 61		goto failed;
 62	stream->input = vmalloc(block_size);
 63	if (stream->input == NULL)
 64		goto failed2;
 65	stream->output = vmalloc(block_size);
 66	if (stream->output == NULL)
 67		goto failed3;
 68
 69	return stream;
 70
 71failed3:
 72	vfree(stream->input);
 73failed2:
 74	kfree(stream);
 75failed:
 76	ERROR("Failed to initialise LZ4 decompressor\n");
 77	return ERR_PTR(-ENOMEM);
 78}
 79
 80
 81static void lz4_free(void *strm)
 82{
 83	struct squashfs_lz4 *stream = strm;
 84
 85	if (stream) {
 86		vfree(stream->input);
 87		vfree(stream->output);
 88	}
 89	kfree(stream);
 90}
 91
 92
 93static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,
 94	struct buffer_head **bh, int b, int offset, int length,
 95	struct squashfs_page_actor *output)
 96{
 
 
 97	struct squashfs_lz4 *stream = strm;
 98	void *buff = stream->input, *data;
 99	int avail, i, bytes = length, res;
100	size_t dest_len = output->length;
101
102	for (i = 0; i < b; i++) {
103		avail = min(bytes, msblk->devblksize - offset);
104		memcpy(buff, bh[i]->b_data + offset, avail);
 
 
105		buff += avail;
106		bytes -= avail;
107		offset = 0;
108		put_bh(bh[i]);
109	}
110
111	res = lz4_decompress_unknownoutputsize(stream->input, length,
112					stream->output, &dest_len);
113	if (res)
 
114		return -EIO;
115
116	bytes = dest_len;
117	data = squashfs_first_page(output);
118	buff = stream->output;
119	while (data) {
120		if (bytes <= PAGE_SIZE) {
121			memcpy(data, buff, bytes);
122			break;
123		}
124		memcpy(data, buff, PAGE_SIZE);
125		buff += PAGE_SIZE;
126		bytes -= PAGE_SIZE;
127		data = squashfs_next_page(output);
128	}
129	squashfs_finish_page(output);
130
131	return dest_len;
132}
133
134const struct squashfs_decompressor squashfs_lz4_comp_ops = {
135	.init = lz4_init,
136	.comp_opts = lz4_comp_opts,
137	.free = lz4_free,
138	.decompress = lz4_uncompress,
139	.id = LZ4_COMPRESSION,
140	.name = "lz4",
141	.supported = 1
142};
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (c) 2013, 2014
  4 * Phillip Lougher <phillip@squashfs.org.uk>
 
 
 
  5 */
  6
  7#include <linux/bio.h>
  8#include <linux/mutex.h>
  9#include <linux/slab.h>
 10#include <linux/vmalloc.h>
 11#include <linux/lz4.h>
 12
 13#include "squashfs_fs.h"
 14#include "squashfs_fs_sb.h"
 15#include "squashfs.h"
 16#include "decompressor.h"
 17#include "page_actor.h"
 18
 19#define LZ4_LEGACY	1
 20
 21struct lz4_comp_opts {
 22	__le32 version;
 23	__le32 flags;
 24};
 25
 26struct squashfs_lz4 {
 27	void *input;
 28	void *output;
 29};
 30
 31
 32static void *lz4_comp_opts(struct squashfs_sb_info *msblk,
 33	void *buff, int len)
 34{
 35	struct lz4_comp_opts *comp_opts = buff;
 36
 37	/* LZ4 compressed filesystems always have compression options */
 38	if (comp_opts == NULL || len < sizeof(*comp_opts))
 39		return ERR_PTR(-EIO);
 40
 41	if (le32_to_cpu(comp_opts->version) != LZ4_LEGACY) {
 42		/* LZ4 format currently used by the kernel is the 'legacy'
 43		 * format */
 44		ERROR("Unknown LZ4 version\n");
 45		return ERR_PTR(-EINVAL);
 46	}
 47
 48	return NULL;
 49}
 50
 51
 52static void *lz4_init(struct squashfs_sb_info *msblk, void *buff)
 53{
 54	int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
 55	struct squashfs_lz4 *stream;
 56
 57	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
 58	if (stream == NULL)
 59		goto failed;
 60	stream->input = vmalloc(block_size);
 61	if (stream->input == NULL)
 62		goto failed2;
 63	stream->output = vmalloc(block_size);
 64	if (stream->output == NULL)
 65		goto failed3;
 66
 67	return stream;
 68
 69failed3:
 70	vfree(stream->input);
 71failed2:
 72	kfree(stream);
 73failed:
 74	ERROR("Failed to initialise LZ4 decompressor\n");
 75	return ERR_PTR(-ENOMEM);
 76}
 77
 78
 79static void lz4_free(void *strm)
 80{
 81	struct squashfs_lz4 *stream = strm;
 82
 83	if (stream) {
 84		vfree(stream->input);
 85		vfree(stream->output);
 86	}
 87	kfree(stream);
 88}
 89
 90
 91static int lz4_uncompress(struct squashfs_sb_info *msblk, void *strm,
 92	struct bio *bio, int offset, int length,
 93	struct squashfs_page_actor *output)
 94{
 95	struct bvec_iter_all iter_all = {};
 96	struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
 97	struct squashfs_lz4 *stream = strm;
 98	void *buff = stream->input, *data;
 99	int bytes = length, res;
 
100
101	while (bio_next_segment(bio, &iter_all)) {
102		int avail = min(bytes, ((int)bvec->bv_len) - offset);
103
104		data = page_address(bvec->bv_page) + bvec->bv_offset;
105		memcpy(buff, data + offset, avail);
106		buff += avail;
107		bytes -= avail;
108		offset = 0;
 
109	}
110
111	res = LZ4_decompress_safe(stream->input, stream->output,
112		length, output->length);
113
114	if (res < 0)
115		return -EIO;
116
117	bytes = res;
118	data = squashfs_first_page(output);
119	buff = stream->output;
120	while (data) {
121		if (bytes <= PAGE_SIZE) {
122			memcpy(data, buff, bytes);
123			break;
124		}
125		memcpy(data, buff, PAGE_SIZE);
126		buff += PAGE_SIZE;
127		bytes -= PAGE_SIZE;
128		data = squashfs_next_page(output);
129	}
130	squashfs_finish_page(output);
131
132	return res;
133}
134
135const struct squashfs_decompressor squashfs_lz4_comp_ops = {
136	.init = lz4_init,
137	.comp_opts = lz4_comp_opts,
138	.free = lz4_free,
139	.decompress = lz4_uncompress,
140	.id = LZ4_COMPRESSION,
141	.name = "lz4",
142	.supported = 1
143};