1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * 842 Software Decompression
5 * Copyright (C) 2015 Dan Streetman, IBM Corp
7 * See 842.h for details of the 842 compressed format.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define MODULE_NAME "842_decompress"
14 #include "842_debugfs.h"
16 /* rolling fifo sizes */
17 #define I2_FIFO_SIZE (2 * (1 << I2_BITS))
18 #define I4_FIFO_SIZE (4 * (1 << I4_BITS))
19 #define I8_FIFO_SIZE (8 * (1 << I8_BITS))
21 static u8 decomp_ops[OPS_MAX][4] = {
59 #define beN_to_cpu(d, s) \
60 ((s) == 2 ? be16_to_cpu(get_unaligned((__be16 *)d)) : \
61 (s) == 4 ? be32_to_cpu(get_unaligned((__be32 *)d)) : \
62 (s) == 8 ? be64_to_cpu(get_unaligned((__be64 *)d)) : \
65 static int next_bits(struct sw842_param *p, u64 *d, u8 n);
67 static int __split_next_bits(struct sw842_param *p, u64 *d, u8 n, u8 s)
73 pr_debug("split_next_bits invalid n %u s %u\n", n, s);
77 ret = next_bits(p, &tmp, n - s);
80 ret = next_bits(p, d, s);
87 static int next_bits(struct sw842_param *p, u64 *d, u8 n)
89 u8 *in = p->in, b = p->bit, bits = b + n;
92 pr_debug("next_bits invalid n %u\n", n);
96 /* split this up if reading > 8 bytes, or if we're at the end of
97 * the input buffer and would read past the end
100 return __split_next_bits(p, d, n, 32);
101 else if (p->ilen < 8 && bits > 32 && bits <= 56)
102 return __split_next_bits(p, d, n, 16);
103 else if (p->ilen < 4 && bits > 16 && bits <= 24)
104 return __split_next_bits(p, d, n, 8);
106 if (DIV_ROUND_UP(bits, 8) > p->ilen)
110 *d = *in >> (8 - bits);
112 *d = be16_to_cpu(get_unaligned((__be16 *)in)) >> (16 - bits);
114 *d = be32_to_cpu(get_unaligned((__be32 *)in)) >> (32 - bits);
116 *d = be64_to_cpu(get_unaligned((__be64 *)in)) >> (64 - bits);
118 *d &= GENMASK_ULL(n - 1, 0);
124 p->ilen -= p->bit / 8;
131 static int do_data(struct sw842_param *p, u8 n)
139 ret = next_bits(p, &v, n * 8);
145 put_unaligned(cpu_to_be16((u16)v), (__be16 *)p->out);
148 put_unaligned(cpu_to_be32((u32)v), (__be32 *)p->out);
151 put_unaligned(cpu_to_be64((u64)v), (__be64 *)p->out);
163 static int __do_index(struct sw842_param *p, u8 size, u8 bits, u64 fsize)
165 u64 index, offset, total = round_down(p->out - p->ostart, 8);
168 ret = next_bits(p, &index, bits);
172 offset = index * size;
174 /* a ring buffer of fsize is used; correct the offset */
176 /* this is where the current fifo is */
177 u64 section = round_down(total, fsize);
178 /* the current pos in the fifo */
179 u64 pos = total - section;
181 /* if the offset is past/at the pos, we need to
182 * go back to the last fifo section
190 if (offset + size > total) {
191 pr_debug("index%x %lx points past end %lx\n", size,
192 (unsigned long)offset, (unsigned long)total);
196 if (size != 2 && size != 4 && size != 8)
197 WARN(1, "__do_index invalid size %x\n", size);
199 pr_debug("index%x to %lx off %lx adjoff %lx tot %lx data %lx\n",
200 size, (unsigned long)index,
201 (unsigned long)(index * size), (unsigned long)offset,
202 (unsigned long)total,
203 (unsigned long)beN_to_cpu(&p->ostart[offset], size));
205 memcpy(p->out, &p->ostart[offset], size);
212 static int do_index(struct sw842_param *p, u8 n)
216 return __do_index(p, 2, I2_BITS, I2_FIFO_SIZE);
218 return __do_index(p, 4, I4_BITS, I4_FIFO_SIZE);
220 return __do_index(p, 8, I8_BITS, I8_FIFO_SIZE);
226 static int do_op(struct sw842_param *p, u8 o)
233 for (i = 0; i < 4; i++) {
234 u8 op = decomp_ops[o][i];
236 pr_debug("op is %x\n", op);
238 switch (op & OP_ACTION) {
240 ret = do_data(p, op & OP_AMOUNT);
242 case OP_ACTION_INDEX:
243 ret = do_index(p, op & OP_AMOUNT);
248 pr_err("Internal error, invalid op %x\n", op);
256 if (sw842_template_counts)
257 atomic_inc(&template_count[o]);
265 * Decompress the 842-compressed buffer of length @ilen at @in
266 * to the output buffer @out, using no more than @olen bytes.
268 * The compressed buffer must be only a single 842-compressed buffer,
269 * with the standard format described in the comments in 842.h
270 * Processing will stop when the 842 "END" template is detected,
271 * not the end of the buffer.
273 * Returns: 0 on success, error on failure. The @olen parameter
274 * will contain the number of output bytes written on success, or
277 int sw842_decompress(const u8 *in, unsigned int ilen,
278 u8 *out, unsigned int *olen)
280 struct sw842_param p;
282 u64 op, rep, tmp, bytes, total;
297 ret = next_bits(&p, &op, OP_BITS);
301 pr_debug("template is %lx\n", (unsigned long)op);
305 ret = next_bits(&p, &rep, REPEAT_BITS);
309 if (p.out == out) /* no previous bytes */
315 if (rep * 8 > p.olen)
319 memcpy(p.out, p.out - 8, 8);
324 if (sw842_template_counts)
325 atomic_inc(&template_repeat_count);
336 if (sw842_template_counts)
337 atomic_inc(&template_zeros_count);
341 ret = next_bits(&p, &bytes, SHORT_DATA_BITS);
345 if (!bytes || bytes > SHORT_DATA_BITS_MAX)
348 while (bytes-- > 0) {
349 ret = next_bits(&p, &tmp, 8);
357 if (sw842_template_counts)
358 atomic_inc(&template_short_data_count);
362 if (sw842_template_counts)
363 atomic_inc(&template_end_count);
366 default: /* use template */
372 } while (op != OP_END);
375 * crc(0:31) is saved in compressed data starting with the
376 * next bit after End of stream template.
378 ret = next_bits(&p, &crc, CRC_BITS);
383 * Validate CRC saved in compressed data.
385 if (crc != (u64)crc32_be(0, out, total - p.olen)) {
386 pr_debug("CRC mismatch for decompression\n");
390 if (unlikely((total - p.olen) > UINT_MAX))
393 *olen = total - p.olen;
397 EXPORT_SYMBOL_GPL(sw842_decompress);
399 static int __init sw842_init(void)
401 if (sw842_template_counts)
402 sw842_debugfs_create();
406 module_init(sw842_init);
408 static void __exit sw842_exit(void)
410 if (sw842_template_counts)
411 sw842_debugfs_remove();
413 module_exit(sw842_exit);
415 MODULE_LICENSE("GPL");
416 MODULE_DESCRIPTION("Software 842 Decompressor");
417 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");