842_decompress.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * 842 Software Decompression
  3. *
  4. * Copyright (C) 2015 Dan Streetman, IBM Corp
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See 842.h for details of the 842 compressed format.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #define MODULE_NAME "842_decompress"
  20. #include "842.h"
  21. #include "842_debugfs.h"
  22. /* rolling fifo sizes */
  23. #define I2_FIFO_SIZE (2 * (1 << I2_BITS))
  24. #define I4_FIFO_SIZE (4 * (1 << I4_BITS))
  25. #define I8_FIFO_SIZE (8 * (1 << I8_BITS))
  26. static u8 decomp_ops[OPS_MAX][4] = {
  27. { D8, N0, N0, N0 },
  28. { D4, D2, I2, N0 },
  29. { D4, I2, D2, N0 },
  30. { D4, I2, I2, N0 },
  31. { D4, I4, N0, N0 },
  32. { D2, I2, D4, N0 },
  33. { D2, I2, D2, I2 },
  34. { D2, I2, I2, D2 },
  35. { D2, I2, I2, I2 },
  36. { D2, I2, I4, N0 },
  37. { I2, D2, D4, N0 },
  38. { I2, D4, I2, N0 },
  39. { I2, D2, I2, D2 },
  40. { I2, D2, I2, I2 },
  41. { I2, D2, I4, N0 },
  42. { I2, I2, D4, N0 },
  43. { I2, I2, D2, I2 },
  44. { I2, I2, I2, D2 },
  45. { I2, I2, I2, I2 },
  46. { I2, I2, I4, N0 },
  47. { I4, D4, N0, N0 },
  48. { I4, D2, I2, N0 },
  49. { I4, I2, D2, N0 },
  50. { I4, I2, I2, N0 },
  51. { I4, I4, N0, N0 },
  52. { I8, N0, N0, N0 }
  53. };
  54. struct sw842_param {
  55. u8 *in;
  56. u8 bit;
  57. u64 ilen;
  58. u8 *out;
  59. u8 *ostart;
  60. u64 olen;
  61. };
  62. #define beN_to_cpu(d, s) \
  63. ((s) == 2 ? be16_to_cpu(get_unaligned((__be16 *)d)) : \
  64. (s) == 4 ? be32_to_cpu(get_unaligned((__be32 *)d)) : \
  65. (s) == 8 ? be64_to_cpu(get_unaligned((__be64 *)d)) : \
  66. 0)
  67. static int next_bits(struct sw842_param *p, u64 *d, u8 n);
  68. static int __split_next_bits(struct sw842_param *p, u64 *d, u8 n, u8 s)
  69. {
  70. u64 tmp = 0;
  71. int ret;
  72. if (n <= s) {
  73. pr_debug("split_next_bits invalid n %u s %u\n", n, s);
  74. return -EINVAL;
  75. }
  76. ret = next_bits(p, &tmp, n - s);
  77. if (ret)
  78. return ret;
  79. ret = next_bits(p, d, s);
  80. if (ret)
  81. return ret;
  82. *d |= tmp << s;
  83. return 0;
  84. }
  85. static int next_bits(struct sw842_param *p, u64 *d, u8 n)
  86. {
  87. u8 *in = p->in, b = p->bit, bits = b + n;
  88. if (n > 64) {
  89. pr_debug("next_bits invalid n %u\n", n);
  90. return -EINVAL;
  91. }
  92. /* split this up if reading > 8 bytes, or if we're at the end of
  93. * the input buffer and would read past the end
  94. */
  95. if (bits > 64)
  96. return __split_next_bits(p, d, n, 32);
  97. else if (p->ilen < 8 && bits > 32 && bits <= 56)
  98. return __split_next_bits(p, d, n, 16);
  99. else if (p->ilen < 4 && bits > 16 && bits <= 24)
  100. return __split_next_bits(p, d, n, 8);
  101. if (DIV_ROUND_UP(bits, 8) > p->ilen)
  102. return -EOVERFLOW;
  103. if (bits <= 8)
  104. *d = *in >> (8 - bits);
  105. else if (bits <= 16)
  106. *d = be16_to_cpu(get_unaligned((__be16 *)in)) >> (16 - bits);
  107. else if (bits <= 32)
  108. *d = be32_to_cpu(get_unaligned((__be32 *)in)) >> (32 - bits);
  109. else
  110. *d = be64_to_cpu(get_unaligned((__be64 *)in)) >> (64 - bits);
  111. *d &= GENMASK_ULL(n - 1, 0);
  112. p->bit += n;
  113. if (p->bit > 7) {
  114. p->in += p->bit / 8;
  115. p->ilen -= p->bit / 8;
  116. p->bit %= 8;
  117. }
  118. return 0;
  119. }
  120. static int do_data(struct sw842_param *p, u8 n)
  121. {
  122. u64 v;
  123. int ret;
  124. if (n > p->olen)
  125. return -ENOSPC;
  126. ret = next_bits(p, &v, n * 8);
  127. if (ret)
  128. return ret;
  129. switch (n) {
  130. case 2:
  131. put_unaligned(cpu_to_be16((u16)v), (__be16 *)p->out);
  132. break;
  133. case 4:
  134. put_unaligned(cpu_to_be32((u32)v), (__be32 *)p->out);
  135. break;
  136. case 8:
  137. put_unaligned(cpu_to_be64((u64)v), (__be64 *)p->out);
  138. break;
  139. default:
  140. return -EINVAL;
  141. }
  142. p->out += n;
  143. p->olen -= n;
  144. return 0;
  145. }
  146. static int __do_index(struct sw842_param *p, u8 size, u8 bits, u64 fsize)
  147. {
  148. u64 index, offset, total = round_down(p->out - p->ostart, 8);
  149. int ret;
  150. ret = next_bits(p, &index, bits);
  151. if (ret)
  152. return ret;
  153. offset = index * size;
  154. /* a ring buffer of fsize is used; correct the offset */
  155. if (total > fsize) {
  156. /* this is where the current fifo is */
  157. u64 section = round_down(total, fsize);
  158. /* the current pos in the fifo */
  159. u64 pos = total - section;
  160. /* if the offset is past/at the pos, we need to
  161. * go back to the last fifo section
  162. */
  163. if (offset >= pos)
  164. section -= fsize;
  165. offset += section;
  166. }
  167. if (offset + size > total) {
  168. pr_debug("index%x %lx points past end %lx\n", size,
  169. (unsigned long)offset, (unsigned long)total);
  170. return -EINVAL;
  171. }
  172. if (size != 2 && size != 4 && size != 8)
  173. WARN(1, "__do_index invalid size %x\n", size);
  174. else
  175. pr_debug("index%x to %lx off %lx adjoff %lx tot %lx data %lx\n",
  176. size, (unsigned long)index,
  177. (unsigned long)(index * size), (unsigned long)offset,
  178. (unsigned long)total,
  179. (unsigned long)beN_to_cpu(&p->ostart[offset], size));
  180. memcpy(p->out, &p->ostart[offset], size);
  181. p->out += size;
  182. p->olen -= size;
  183. return 0;
  184. }
  185. static int do_index(struct sw842_param *p, u8 n)
  186. {
  187. switch (n) {
  188. case 2:
  189. return __do_index(p, 2, I2_BITS, I2_FIFO_SIZE);
  190. case 4:
  191. return __do_index(p, 4, I4_BITS, I4_FIFO_SIZE);
  192. case 8:
  193. return __do_index(p, 8, I8_BITS, I8_FIFO_SIZE);
  194. default:
  195. return -EINVAL;
  196. }
  197. }
  198. static int do_op(struct sw842_param *p, u8 o)
  199. {
  200. int i, ret = 0;
  201. if (o >= OPS_MAX)
  202. return -EINVAL;
  203. for (i = 0; i < 4; i++) {
  204. u8 op = decomp_ops[o][i];
  205. pr_debug("op is %x\n", op);
  206. switch (op & OP_ACTION) {
  207. case OP_ACTION_DATA:
  208. ret = do_data(p, op & OP_AMOUNT);
  209. break;
  210. case OP_ACTION_INDEX:
  211. ret = do_index(p, op & OP_AMOUNT);
  212. break;
  213. case OP_ACTION_NOOP:
  214. break;
  215. default:
  216. pr_err("Internal error, invalid op %x\n", op);
  217. return -EINVAL;
  218. }
  219. if (ret)
  220. return ret;
  221. }
  222. if (sw842_template_counts)
  223. atomic_inc(&template_count[o]);
  224. return 0;
  225. }
  226. /**
  227. * sw842_decompress
  228. *
  229. * Decompress the 842-compressed buffer of length @ilen at @in
  230. * to the output buffer @out, using no more than @olen bytes.
  231. *
  232. * The compressed buffer must be only a single 842-compressed buffer,
  233. * with the standard format described in the comments in 842.h
  234. * Processing will stop when the 842 "END" template is detected,
  235. * not the end of the buffer.
  236. *
  237. * Returns: 0 on success, error on failure. The @olen parameter
  238. * will contain the number of output bytes written on success, or
  239. * 0 on error.
  240. */
  241. int sw842_decompress(const u8 *in, unsigned int ilen,
  242. u8 *out, unsigned int *olen)
  243. {
  244. struct sw842_param p;
  245. int ret;
  246. u64 op, rep, tmp, bytes, total;
  247. u64 crc;
  248. p.in = (u8 *)in;
  249. p.bit = 0;
  250. p.ilen = ilen;
  251. p.out = out;
  252. p.ostart = out;
  253. p.olen = *olen;
  254. total = p.olen;
  255. *olen = 0;
  256. do {
  257. ret = next_bits(&p, &op, OP_BITS);
  258. if (ret)
  259. return ret;
  260. pr_debug("template is %lx\n", (unsigned long)op);
  261. switch (op) {
  262. case OP_REPEAT:
  263. ret = next_bits(&p, &rep, REPEAT_BITS);
  264. if (ret)
  265. return ret;
  266. if (p.out == out) /* no previous bytes */
  267. return -EINVAL;
  268. /* copy rep + 1 */
  269. rep++;
  270. if (rep * 8 > p.olen)
  271. return -ENOSPC;
  272. while (rep-- > 0) {
  273. memcpy(p.out, p.out - 8, 8);
  274. p.out += 8;
  275. p.olen -= 8;
  276. }
  277. if (sw842_template_counts)
  278. atomic_inc(&template_repeat_count);
  279. break;
  280. case OP_ZEROS:
  281. if (8 > p.olen)
  282. return -ENOSPC;
  283. memset(p.out, 0, 8);
  284. p.out += 8;
  285. p.olen -= 8;
  286. if (sw842_template_counts)
  287. atomic_inc(&template_zeros_count);
  288. break;
  289. case OP_SHORT_DATA:
  290. ret = next_bits(&p, &bytes, SHORT_DATA_BITS);
  291. if (ret)
  292. return ret;
  293. if (!bytes || bytes > SHORT_DATA_BITS_MAX)
  294. return -EINVAL;
  295. while (bytes-- > 0) {
  296. ret = next_bits(&p, &tmp, 8);
  297. if (ret)
  298. return ret;
  299. *p.out = (u8)tmp;
  300. p.out++;
  301. p.olen--;
  302. }
  303. if (sw842_template_counts)
  304. atomic_inc(&template_short_data_count);
  305. break;
  306. case OP_END:
  307. if (sw842_template_counts)
  308. atomic_inc(&template_end_count);
  309. break;
  310. default: /* use template */
  311. ret = do_op(&p, op);
  312. if (ret)
  313. return ret;
  314. break;
  315. }
  316. } while (op != OP_END);
  317. /*
  318. * crc(0:31) is saved in compressed data starting with the
  319. * next bit after End of stream template.
  320. */
  321. ret = next_bits(&p, &crc, CRC_BITS);
  322. if (ret)
  323. return ret;
  324. /*
  325. * Validate CRC saved in compressed data.
  326. */
  327. if (crc != (u64)crc32_be(0, out, total - p.olen)) {
  328. pr_debug("CRC mismatch for decompression\n");
  329. return -EINVAL;
  330. }
  331. if (unlikely((total - p.olen) > UINT_MAX))
  332. return -ENOSPC;
  333. *olen = total - p.olen;
  334. return 0;
  335. }
  336. EXPORT_SYMBOL_GPL(sw842_decompress);
  337. static int __init sw842_init(void)
  338. {
  339. if (sw842_template_counts)
  340. sw842_debugfs_create();
  341. return 0;
  342. }
  343. module_init(sw842_init);
  344. static void __exit sw842_exit(void)
  345. {
  346. if (sw842_template_counts)
  347. sw842_debugfs_remove();
  348. }
  349. module_exit(sw842_exit);
  350. MODULE_LICENSE("GPL");
  351. MODULE_DESCRIPTION("Software 842 Decompressor");
  352. MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");