xref: /OK3568_Linux_fs/kernel/lib/xz/xz_dec_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * XZ decoder tester
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Author: Lasse Collin <lasse.collin@tukaani.org>
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This file has been put into the public domain.
7*4882a593Smuzhiyun  * You can do whatever you want with this file.
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/fs.h>
13*4882a593Smuzhiyun #include <linux/uaccess.h>
14*4882a593Smuzhiyun #include <linux/crc32.h>
15*4882a593Smuzhiyun #include <linux/xz.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* Maximum supported dictionary size */
18*4882a593Smuzhiyun #define DICT_MAX (1 << 20)
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /* Device name to pass to register_chrdev(). */
21*4882a593Smuzhiyun #define DEVICE_NAME "xz_dec_test"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* Dynamically allocated device major number */
24*4882a593Smuzhiyun static int device_major;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * We reuse the same decoder state, and thus can decode only one
28*4882a593Smuzhiyun  * file at a time.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun static bool device_is_open;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* XZ decoder state */
33*4882a593Smuzhiyun static struct xz_dec *state;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * Return value of xz_dec_run(). We need to avoid calling xz_dec_run() after
37*4882a593Smuzhiyun  * it has returned XZ_STREAM_END, so we make this static.
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun static enum xz_ret ret;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun  * Input and output buffers. The input buffer is used as a temporary safe
43*4882a593Smuzhiyun  * place for the data coming from the userspace.
44*4882a593Smuzhiyun  */
45*4882a593Smuzhiyun static uint8_t buffer_in[1024];
46*4882a593Smuzhiyun static uint8_t buffer_out[1024];
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun  * Structure to pass the input and output buffers to the XZ decoder.
50*4882a593Smuzhiyun  * A few of the fields are never modified so we initialize them here.
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun static struct xz_buf buffers = {
53*4882a593Smuzhiyun 	.in = buffer_in,
54*4882a593Smuzhiyun 	.out = buffer_out,
55*4882a593Smuzhiyun 	.out_size = sizeof(buffer_out)
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun  * CRC32 of uncompressed data. This is used to give the user a simple way
60*4882a593Smuzhiyun  * to check that the decoder produces correct output.
61*4882a593Smuzhiyun  */
62*4882a593Smuzhiyun static uint32_t crc;
63*4882a593Smuzhiyun 
xz_dec_test_open(struct inode * i,struct file * f)64*4882a593Smuzhiyun static int xz_dec_test_open(struct inode *i, struct file *f)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	if (device_is_open)
67*4882a593Smuzhiyun 		return -EBUSY;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	device_is_open = true;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	xz_dec_reset(state);
72*4882a593Smuzhiyun 	ret = XZ_OK;
73*4882a593Smuzhiyun 	crc = 0xFFFFFFFF;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	buffers.in_pos = 0;
76*4882a593Smuzhiyun 	buffers.in_size = 0;
77*4882a593Smuzhiyun 	buffers.out_pos = 0;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	printk(KERN_INFO DEVICE_NAME ": opened\n");
80*4882a593Smuzhiyun 	return 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
xz_dec_test_release(struct inode * i,struct file * f)83*4882a593Smuzhiyun static int xz_dec_test_release(struct inode *i, struct file *f)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	device_is_open = false;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (ret == XZ_OK)
88*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": input was truncated\n");
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	printk(KERN_INFO DEVICE_NAME ": closed\n");
91*4882a593Smuzhiyun 	return 0;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun  * Decode the data given to us from the userspace. CRC32 of the uncompressed
96*4882a593Smuzhiyun  * data is calculated and is printed at the end of successful decoding. The
97*4882a593Smuzhiyun  * uncompressed data isn't stored anywhere for further use.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * The .xz file must have exactly one Stream and no Stream Padding. The data
100*4882a593Smuzhiyun  * after the first Stream is considered to be garbage.
101*4882a593Smuzhiyun  */
xz_dec_test_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)102*4882a593Smuzhiyun static ssize_t xz_dec_test_write(struct file *file, const char __user *buf,
103*4882a593Smuzhiyun 				 size_t size, loff_t *pos)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	size_t remaining;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (ret != XZ_OK) {
108*4882a593Smuzhiyun 		if (size > 0)
109*4882a593Smuzhiyun 			printk(KERN_INFO DEVICE_NAME ": %zu bytes of "
110*4882a593Smuzhiyun 					"garbage at the end of the file\n",
111*4882a593Smuzhiyun 					size);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 		return -ENOSPC;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	printk(KERN_INFO DEVICE_NAME ": decoding %zu bytes of input\n",
117*4882a593Smuzhiyun 			size);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	remaining = size;
120*4882a593Smuzhiyun 	while ((remaining > 0 || buffers.out_pos == buffers.out_size)
121*4882a593Smuzhiyun 			&& ret == XZ_OK) {
122*4882a593Smuzhiyun 		if (buffers.in_pos == buffers.in_size) {
123*4882a593Smuzhiyun 			buffers.in_pos = 0;
124*4882a593Smuzhiyun 			buffers.in_size = min(remaining, sizeof(buffer_in));
125*4882a593Smuzhiyun 			if (copy_from_user(buffer_in, buf, buffers.in_size))
126*4882a593Smuzhiyun 				return -EFAULT;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 			buf += buffers.in_size;
129*4882a593Smuzhiyun 			remaining -= buffers.in_size;
130*4882a593Smuzhiyun 		}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		buffers.out_pos = 0;
133*4882a593Smuzhiyun 		ret = xz_dec_run(state, &buffers);
134*4882a593Smuzhiyun 		crc = crc32(crc, buffer_out, buffers.out_pos);
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	switch (ret) {
138*4882a593Smuzhiyun 	case XZ_OK:
139*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": XZ_OK\n");
140*4882a593Smuzhiyun 		return size;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	case XZ_STREAM_END:
143*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": XZ_STREAM_END, "
144*4882a593Smuzhiyun 				"CRC32 = 0x%08X\n", ~crc);
145*4882a593Smuzhiyun 		return size - remaining - (buffers.in_size - buffers.in_pos);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	case XZ_MEMLIMIT_ERROR:
148*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": XZ_MEMLIMIT_ERROR\n");
149*4882a593Smuzhiyun 		break;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	case XZ_FORMAT_ERROR:
152*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": XZ_FORMAT_ERROR\n");
153*4882a593Smuzhiyun 		break;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	case XZ_OPTIONS_ERROR:
156*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": XZ_OPTIONS_ERROR\n");
157*4882a593Smuzhiyun 		break;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	case XZ_DATA_ERROR:
160*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": XZ_DATA_ERROR\n");
161*4882a593Smuzhiyun 		break;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	case XZ_BUF_ERROR:
164*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": XZ_BUF_ERROR\n");
165*4882a593Smuzhiyun 		break;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	default:
168*4882a593Smuzhiyun 		printk(KERN_INFO DEVICE_NAME ": Bug detected!\n");
169*4882a593Smuzhiyun 		break;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return -EIO;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /* Allocate the XZ decoder state and register the character device. */
xz_dec_test_init(void)176*4882a593Smuzhiyun static int __init xz_dec_test_init(void)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	static const struct file_operations fileops = {
179*4882a593Smuzhiyun 		.owner = THIS_MODULE,
180*4882a593Smuzhiyun 		.open = &xz_dec_test_open,
181*4882a593Smuzhiyun 		.release = &xz_dec_test_release,
182*4882a593Smuzhiyun 		.write = &xz_dec_test_write
183*4882a593Smuzhiyun 	};
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	state = xz_dec_init(XZ_PREALLOC, DICT_MAX);
186*4882a593Smuzhiyun 	if (state == NULL)
187*4882a593Smuzhiyun 		return -ENOMEM;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	device_major = register_chrdev(0, DEVICE_NAME, &fileops);
190*4882a593Smuzhiyun 	if (device_major < 0) {
191*4882a593Smuzhiyun 		xz_dec_end(state);
192*4882a593Smuzhiyun 		return device_major;
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	printk(KERN_INFO DEVICE_NAME ": module loaded\n");
196*4882a593Smuzhiyun 	printk(KERN_INFO DEVICE_NAME ": Create a device node with "
197*4882a593Smuzhiyun 			"'mknod " DEVICE_NAME " c %d 0' and write .xz files "
198*4882a593Smuzhiyun 			"to it.\n", device_major);
199*4882a593Smuzhiyun 	return 0;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
xz_dec_test_exit(void)202*4882a593Smuzhiyun static void __exit xz_dec_test_exit(void)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	unregister_chrdev(device_major, DEVICE_NAME);
205*4882a593Smuzhiyun 	xz_dec_end(state);
206*4882a593Smuzhiyun 	printk(KERN_INFO DEVICE_NAME ": module unloaded\n");
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun module_init(xz_dec_test_init);
210*4882a593Smuzhiyun module_exit(xz_dec_test_exit);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun MODULE_DESCRIPTION("XZ decompressor tester");
213*4882a593Smuzhiyun MODULE_VERSION("1.0");
214*4882a593Smuzhiyun MODULE_AUTHOR("Lasse Collin <lasse.collin@tukaani.org>");
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  * This code is in the public domain, but in Linux it's simplest to just
218*4882a593Smuzhiyun  * say it's GPL and consider the authors as the copyright holders.
219*4882a593Smuzhiyun  */
220*4882a593Smuzhiyun MODULE_LICENSE("GPL");
221