xz_dec_test.c 5.2 KB

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