1 // SPDX-License-Identifier: 0BSD
6 * Author: Lasse Collin <lasse.collin@tukaani.org>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/crc32.h>
16 /* Maximum supported dictionary size */
17 #define DICT_MAX (1 << 20)
19 /* Device name to pass to register_chrdev(). */
20 #define DEVICE_NAME "xz_dec_test"
22 /* Dynamically allocated device major number */
23 static int device_major
;
26 * We reuse the same decoder state, and thus can decode only one
29 static bool device_is_open
;
31 /* XZ decoder state */
32 static struct xz_dec
*state
;
35 * Return value of xz_dec_run(). We need to avoid calling xz_dec_run() after
36 * it has returned XZ_STREAM_END, so we make this static.
38 static enum xz_ret ret
;
41 * Input and output buffers. The input buffer is used as a temporary safe
42 * place for the data coming from the userspace.
44 static uint8_t buffer_in
[1024];
45 static uint8_t buffer_out
[1024];
48 * Structure to pass the input and output buffers to the XZ decoder.
49 * A few of the fields are never modified so we initialize them here.
51 static struct xz_buf buffers
= {
54 .out_size
= sizeof(buffer_out
)
58 * CRC32 of uncompressed data. This is used to give the user a simple way
59 * to check that the decoder produces correct output.
63 static int xz_dec_test_open(struct inode
*i
, struct file
*f
)
68 device_is_open
= true;
78 printk(KERN_INFO DEVICE_NAME
": opened\n");
82 static int xz_dec_test_release(struct inode
*i
, struct file
*f
)
84 device_is_open
= false;
87 printk(KERN_INFO DEVICE_NAME
": input was truncated\n");
89 printk(KERN_INFO DEVICE_NAME
": closed\n");
94 * Decode the data given to us from the userspace. CRC32 of the uncompressed
95 * data is calculated and is printed at the end of successful decoding. The
96 * uncompressed data isn't stored anywhere for further use.
98 * The .xz file must have exactly one Stream and no Stream Padding. The data
99 * after the first Stream is considered to be garbage.
101 static ssize_t
xz_dec_test_write(struct file
*file
, const char __user
*buf
,
102 size_t size
, loff_t
*pos
)
108 printk(KERN_INFO DEVICE_NAME
": %zu bytes of "
109 "garbage at the end of the file\n",
115 printk(KERN_INFO DEVICE_NAME
": decoding %zu bytes of input\n",
119 while ((remaining
> 0 || buffers
.out_pos
== buffers
.out_size
)
121 if (buffers
.in_pos
== buffers
.in_size
) {
123 buffers
.in_size
= min(remaining
, sizeof(buffer_in
));
124 if (copy_from_user(buffer_in
, buf
, buffers
.in_size
))
127 buf
+= buffers
.in_size
;
128 remaining
-= buffers
.in_size
;
132 ret
= xz_dec_run(state
, &buffers
);
133 crc
= crc32(crc
, buffer_out
, buffers
.out_pos
);
138 printk(KERN_INFO DEVICE_NAME
": XZ_OK\n");
142 printk(KERN_INFO DEVICE_NAME
": XZ_STREAM_END, "
143 "CRC32 = 0x%08X\n", ~crc
);
144 return size
- remaining
- (buffers
.in_size
- buffers
.in_pos
);
146 case XZ_MEMLIMIT_ERROR
:
147 printk(KERN_INFO DEVICE_NAME
": XZ_MEMLIMIT_ERROR\n");
150 case XZ_FORMAT_ERROR
:
151 printk(KERN_INFO DEVICE_NAME
": XZ_FORMAT_ERROR\n");
154 case XZ_OPTIONS_ERROR
:
155 printk(KERN_INFO DEVICE_NAME
": XZ_OPTIONS_ERROR\n");
159 printk(KERN_INFO DEVICE_NAME
": XZ_DATA_ERROR\n");
163 printk(KERN_INFO DEVICE_NAME
": XZ_BUF_ERROR\n");
167 printk(KERN_INFO DEVICE_NAME
": Bug detected!\n");
174 /* Allocate the XZ decoder state and register the character device. */
175 static int __init
xz_dec_test_init(void)
177 static const struct file_operations fileops
= {
178 .owner
= THIS_MODULE
,
179 .open
= &xz_dec_test_open
,
180 .release
= &xz_dec_test_release
,
181 .write
= &xz_dec_test_write
184 state
= xz_dec_init(XZ_PREALLOC
, DICT_MAX
);
188 device_major
= register_chrdev(0, DEVICE_NAME
, &fileops
);
189 if (device_major
< 0) {
194 printk(KERN_INFO DEVICE_NAME
": module loaded\n");
195 printk(KERN_INFO DEVICE_NAME
": Create a device node with "
196 "'mknod " DEVICE_NAME
" c %d 0' and write .xz files "
197 "to it.\n", device_major
);
201 static void __exit
xz_dec_test_exit(void)
203 unregister_chrdev(device_major
, DEVICE_NAME
);
205 printk(KERN_INFO DEVICE_NAME
": module unloaded\n");
208 module_init(xz_dec_test_init
);
209 module_exit(xz_dec_test_exit
);
211 MODULE_DESCRIPTION("XZ decompressor tester");
212 MODULE_VERSION("1.0");
213 MODULE_AUTHOR("Lasse Collin <lasse.collin@tukaani.org>");
214 MODULE_LICENSE("Dual BSD/GPL");