4 * Author: Lasse Collin <lasse.collin@tukaani.org>
6 * This file has been put into the public domain.
7 * You can do whatever you want with this file.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/crc32.h>
17 /* Maximum supported dictionary size */
18 #define DICT_MAX (1 << 20)
20 /* Device name to pass to register_chrdev(). */
21 #define DEVICE_NAME "xz_dec_test"
23 /* Dynamically allocated device major number */
24 static int device_major
;
27 * We reuse the same decoder state, and thus can decode only one
30 static bool device_is_open
;
32 /* XZ decoder state */
33 static struct xz_dec
*state
;
36 * Return value of xz_dec_run(). We need to avoid calling xz_dec_run() after
37 * it has returned XZ_STREAM_END, so we make this static.
39 static enum xz_ret ret
;
42 * Input and output buffers. The input buffer is used as a temporary safe
43 * place for the data coming from the userspace.
45 static uint8_t buffer_in
[1024];
46 static uint8_t buffer_out
[1024];
49 * Structure to pass the input and output buffers to the XZ decoder.
50 * A few of the fields are never modified so we initialize them here.
52 static struct xz_buf buffers
= {
55 .out_size
= sizeof(buffer_out
)
59 * CRC32 of uncompressed data. This is used to give the user a simple way
60 * to check that the decoder produces correct output.
64 static int xz_dec_test_open(struct inode
*i
, struct file
*f
)
69 device_is_open
= true;
79 printk(KERN_INFO DEVICE_NAME
": opened\n");
83 static int xz_dec_test_release(struct inode
*i
, struct file
*f
)
85 device_is_open
= false;
88 printk(KERN_INFO DEVICE_NAME
": input was truncated\n");
90 printk(KERN_INFO DEVICE_NAME
": closed\n");
95 * Decode the data given to us from the userspace. CRC32 of the uncompressed
96 * data is calculated and is printed at the end of successful decoding. The
97 * uncompressed data isn't stored anywhere for further use.
99 * The .xz file must have exactly one Stream and no Stream Padding. The data
100 * after the first Stream is considered to be garbage.
102 static ssize_t
xz_dec_test_write(struct file
*file
, const char __user
*buf
,
103 size_t size
, loff_t
*pos
)
109 printk(KERN_INFO DEVICE_NAME
": %zu bytes of "
110 "garbage at the end of the file\n",
116 printk(KERN_INFO DEVICE_NAME
": decoding %zu bytes of input\n",
120 while ((remaining
> 0 || buffers
.out_pos
== buffers
.out_size
)
122 if (buffers
.in_pos
== buffers
.in_size
) {
124 buffers
.in_size
= min(remaining
, sizeof(buffer_in
));
125 if (copy_from_user(buffer_in
, buf
, buffers
.in_size
))
128 buf
+= buffers
.in_size
;
129 remaining
-= buffers
.in_size
;
133 ret
= xz_dec_run(state
, &buffers
);
134 crc
= crc32(crc
, buffer_out
, buffers
.out_pos
);
139 printk(KERN_INFO DEVICE_NAME
": XZ_OK\n");
143 printk(KERN_INFO DEVICE_NAME
": XZ_STREAM_END, "
144 "CRC32 = 0x%08X\n", ~crc
);
145 return size
- remaining
- (buffers
.in_size
- buffers
.in_pos
);
147 case XZ_MEMLIMIT_ERROR
:
148 printk(KERN_INFO DEVICE_NAME
": XZ_MEMLIMIT_ERROR\n");
151 case XZ_FORMAT_ERROR
:
152 printk(KERN_INFO DEVICE_NAME
": XZ_FORMAT_ERROR\n");
155 case XZ_OPTIONS_ERROR
:
156 printk(KERN_INFO DEVICE_NAME
": XZ_OPTIONS_ERROR\n");
160 printk(KERN_INFO DEVICE_NAME
": XZ_DATA_ERROR\n");
164 printk(KERN_INFO DEVICE_NAME
": XZ_BUF_ERROR\n");
168 printk(KERN_INFO DEVICE_NAME
": Bug detected!\n");
175 /* Allocate the XZ decoder state and register the character device. */
176 static int __init
xz_dec_test_init(void)
178 static const struct file_operations fileops
= {
179 .owner
= THIS_MODULE
,
180 .open
= &xz_dec_test_open
,
181 .release
= &xz_dec_test_release
,
182 .write
= &xz_dec_test_write
185 state
= xz_dec_init(XZ_PREALLOC
, DICT_MAX
);
189 device_major
= register_chrdev(0, DEVICE_NAME
, &fileops
);
190 if (device_major
< 0) {
195 printk(KERN_INFO DEVICE_NAME
": module loaded\n");
196 printk(KERN_INFO DEVICE_NAME
": Create a device node with "
197 "'mknod " DEVICE_NAME
" c %d 0' and write .xz files "
198 "to it.\n", device_major
);
202 static void __exit
xz_dec_test_exit(void)
204 unregister_chrdev(device_major
, DEVICE_NAME
);
206 printk(KERN_INFO DEVICE_NAME
": module unloaded\n");
209 module_init(xz_dec_test_init
);
210 module_exit(xz_dec_test_exit
);
212 MODULE_DESCRIPTION("XZ decompressor tester");
213 MODULE_VERSION("1.0");
214 MODULE_AUTHOR("Lasse Collin <lasse.collin@tukaani.org>");
217 * This code is in the public domain, but in Linux it's simplest to just
218 * say it's GPL and consider the authors as the copyright holders.
220 MODULE_LICENSE("GPL");