xz_dec_test.c 5.0 KB

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