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