5 + * LZMA uncompresion module for pcomp
6 + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
9 + * Initial Linux kernel adaptation
10 + * Copyright (C) 2006 Alain < alain@knaff.lu >
12 + * Based on small lzma deflate implementation/Small range coder
13 + * implementation for lzma.
14 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
16 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
17 + * Copyright (C) 1999-2005 Igor Pavlov
19 + * This program is free software; you can redistribute it and/or modify it
20 + * under the terms of the GNU General Public License version 2 as published
21 + * by the Free Software Foundation.
23 + * FIXME: the current implementation assumes that the caller will
24 + * not free any output buffers until the whole decompression has been
25 + * completed. This is necessary, because LZMA looks back at old output
26 + * instead of doing a separate dictionary allocation, which saves RAM.
29 +#include <linux/init.h>
30 +#include <linux/module.h>
31 +#include <linux/vmalloc.h>
32 +#include <linux/interrupt.h>
33 +#include <linux/mm.h>
34 +#include <linux/net.h>
35 +#include <linux/slab.h>
36 +#include <linux/kthread.h>
38 +#include <crypto/internal/compress.h>
41 +static int instance = 0;
43 +struct unlzma_buffer {
44 + struct unlzma_buffer *last;
51 + struct task_struct *thread;
52 + wait_queue_head_t next_req;
71 + struct unlzma_buffer *head;
75 + u32 rep0, rep1, rep2, rep3;
84 +unlzma_should_stop(struct unlzma_ctx *ctx)
86 + return unlikely(kthread_should_stop() || ctx->cancel);
90 +unlzma_request_buffer(struct unlzma_ctx *ctx, int *avail)
92 + mutex_unlock(&ctx->mutex);
93 + wait_event(ctx->next_req, unlzma_should_stop(ctx) || (*avail > 0));
94 + mutex_lock(&ctx->mutex);
98 +rc_read(struct unlzma_ctx *ctx)
100 + if (unlikely(ctx->avail_in <= 0))
101 + unlzma_request_buffer(ctx, &ctx->avail_in);
103 + if (unlzma_should_stop(ctx))
107 + return *(ctx->next_in++);
112 +rc_get_code(struct unlzma_ctx *ctx)
114 + ctx->code = (ctx->code << 8) | rc_read(ctx);
118 +rc_normalize(struct unlzma_ctx *ctx)
120 + if (ctx->range < (1 << RC_TOP_BITS)) {
127 +rc_is_bit_0(struct unlzma_ctx *ctx, u16 *p)
130 + ctx->bound = *p * (ctx->range >> RC_MODEL_TOTAL_BITS);
131 + return ctx->code < ctx->bound;
135 +rc_update_bit_0(struct unlzma_ctx *ctx, u16 *p)
137 + ctx->range = ctx->bound;
138 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
142 +rc_update_bit_1(struct unlzma_ctx *ctx, u16 *p)
144 + ctx->range -= ctx->bound;
145 + ctx->code -= ctx->bound;
146 + *p -= *p >> RC_MOVE_BITS;
150 +rc_get_bit(struct unlzma_ctx *ctx, u16 *p, int *symbol)
152 + if (rc_is_bit_0(ctx, p)) {
153 + rc_update_bit_0(ctx, p);
157 + rc_update_bit_1(ctx, p);
158 + *symbol = *symbol * 2 + 1;
164 +rc_direct_bit(struct unlzma_ctx *ctx)
168 + if (ctx->code >= ctx->range) {
169 + ctx->code -= ctx->range;
176 +rc_bit_tree_decode(struct unlzma_ctx *ctx, u16 *p, int num_levels, int *symbol)
178 + int i = num_levels;
182 + rc_get_bit(ctx, p + *symbol, symbol);
183 + *symbol -= 1 << num_levels;
187 +peek_old_byte(struct unlzma_ctx *ctx, u32 offs)
189 + struct unlzma_buffer *bh = ctx->head;
192 + pos = ctx->pos - offs;
193 + if (pos >= ctx->dict_size) {
194 + pos = (~pos % ctx->dict_size);
197 + while (bh->offset > pos) {
204 + if (pos > bh->size)
207 + return bh->ptr[pos];
211 +get_buffer(struct unlzma_ctx *ctx)
213 + struct unlzma_buffer *bh;
215 + bh = kzalloc(sizeof(struct unlzma_buffer), GFP_KERNEL);
216 + bh->ptr = ctx->next_out;
217 + bh->offset = ctx->pos;
218 + bh->last = ctx->head;
219 + bh->size = ctx->avail_out;
224 +write_byte(struct unlzma_ctx *ctx, u8 byte)
226 + if (unlikely(ctx->avail_out <= 0)) {
227 + unlzma_request_buffer(ctx, &ctx->avail_out);
231 + if (!ctx->avail_out)
234 + ctx->previous_byte = byte;
235 + *(ctx->next_out++) = byte;
242 +copy_byte(struct unlzma_ctx *ctx, u32 offs)
244 + write_byte(ctx, peek_old_byte(ctx, offs));
248 +copy_bytes(struct unlzma_ctx *ctx, u32 rep0, int len)
251 + copy_byte(ctx, rep0);
253 + if (unlzma_should_stop(ctx))
255 + } while (len != 0);
259 +process_bit0(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob,
260 + int lc, u32 literal_pos_mask)
263 + rc_update_bit_0(ctx, prob);
264 + prob = (p + LZMA_LITERAL +
266 + * (((ctx->pos & literal_pos_mask) << lc)
267 + + (ctx->previous_byte >> (8 - lc))))
270 + if (ctx->state >= LZMA_NUM_LIT_STATES) {
271 + int match_byte = peek_old_byte(ctx, ctx->rep0);
277 + bit = match_byte & 0x100;
278 + prob_lit = prob + 0x100 + bit + mi;
279 + if (rc_get_bit(ctx, prob_lit, &mi) != !!bit)
281 + } while (mi < 0x100);
283 + while (mi < 0x100) {
284 + u16 *prob_lit = prob + mi;
285 + rc_get_bit(ctx, prob_lit, &mi);
287 + write_byte(ctx, mi);
288 + if (ctx->state < 4)
290 + else if (ctx->state < 10)
297 +process_bit1(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob)
304 + rc_update_bit_1(ctx, prob);
305 + prob = p + LZMA_IS_REP + ctx->state;
306 + if (rc_is_bit_0(ctx, prob)) {
307 + rc_update_bit_0(ctx, prob);
308 + ctx->rep3 = ctx->rep2;
309 + ctx->rep2 = ctx->rep1;
310 + ctx->rep1 = ctx->rep0;
311 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ? 0 : 3;
312 + prob = p + LZMA_LEN_CODER;
314 + rc_update_bit_1(ctx, prob);
315 + prob = p + LZMA_IS_REP_G0 + ctx->state;
316 + if (rc_is_bit_0(ctx, prob)) {
317 + rc_update_bit_0(ctx, prob);
318 + prob = (p + LZMA_IS_REP_0_LONG
320 + LZMA_NUM_POS_BITS_MAX) +
322 + if (rc_is_bit_0(ctx, prob)) {
323 + rc_update_bit_0(ctx, prob);
325 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ?
327 + copy_byte(ctx, ctx->rep0);
330 + rc_update_bit_1(ctx, prob);
335 + rc_update_bit_1(ctx, prob);
336 + prob = p + LZMA_IS_REP_G1 + ctx->state;
337 + if (rc_is_bit_0(ctx, prob)) {
338 + rc_update_bit_0(ctx, prob);
339 + distance = ctx->rep1;
341 + rc_update_bit_1(ctx, prob);
342 + prob = p + LZMA_IS_REP_G2 + ctx->state;
343 + if (rc_is_bit_0(ctx, prob)) {
344 + rc_update_bit_0(ctx, prob);
345 + distance = ctx->rep2;
347 + rc_update_bit_1(ctx, prob);
348 + distance = ctx->rep3;
349 + ctx->rep3 = ctx->rep2;
351 + ctx->rep2 = ctx->rep1;
353 + ctx->rep1 = ctx->rep0;
354 + ctx->rep0 = distance;
356 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ? 8 : 11;
357 + prob = p + LZMA_REP_LEN_CODER;
360 + prob_len = prob + LZMA_LEN_CHOICE;
361 + if (rc_is_bit_0(ctx, prob_len)) {
362 + rc_update_bit_0(ctx, prob_len);
363 + prob_len = (prob + LZMA_LEN_LOW
365 + LZMA_LEN_NUM_LOW_BITS));
367 + num_bits = LZMA_LEN_NUM_LOW_BITS;
369 + rc_update_bit_1(ctx, prob_len);
370 + prob_len = prob + LZMA_LEN_CHOICE_2;
371 + if (rc_is_bit_0(ctx, prob_len)) {
372 + rc_update_bit_0(ctx, prob_len);
373 + prob_len = (prob + LZMA_LEN_MID
375 + LZMA_LEN_NUM_MID_BITS));
376 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
377 + num_bits = LZMA_LEN_NUM_MID_BITS;
379 + rc_update_bit_1(ctx, prob_len);
380 + prob_len = prob + LZMA_LEN_HIGH;
381 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
382 + + (1 << LZMA_LEN_NUM_MID_BITS));
383 + num_bits = LZMA_LEN_NUM_HIGH_BITS;
387 + rc_bit_tree_decode(ctx, prob_len, num_bits, &len);
390 + if (ctx->state < 4) {
393 + ctx->state += LZMA_NUM_LIT_STATES;
395 + p + LZMA_POS_SLOT +
397 + LZMA_NUM_LEN_TO_POS_STATES ? len :
398 + LZMA_NUM_LEN_TO_POS_STATES - 1)
399 + << LZMA_NUM_POS_SLOT_BITS);
400 + rc_bit_tree_decode(ctx, prob,
401 + LZMA_NUM_POS_SLOT_BITS,
403 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
405 + num_bits = (pos_slot >> 1) - 1;
406 + ctx->rep0 = 2 | (pos_slot & 1);
407 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
408 + ctx->rep0 <<= num_bits;
409 + prob = p + LZMA_SPEC_POS +
410 + ctx->rep0 - pos_slot - 1;
412 + num_bits -= LZMA_NUM_ALIGN_BITS;
414 + ctx->rep0 = (ctx->rep0 << 1) |
415 + rc_direct_bit(ctx);
416 + prob = p + LZMA_ALIGN;
417 + ctx->rep0 <<= LZMA_NUM_ALIGN_BITS;
418 + num_bits = LZMA_NUM_ALIGN_BITS;
422 + while (num_bits--) {
423 + if (rc_get_bit(ctx, prob + mi, &mi))
428 + ctx->rep0 = pos_slot;
429 + if (++(ctx->rep0) == 0)
433 + len += LZMA_MATCH_MIN_LEN;
435 + copy_bytes(ctx, ctx->rep0, len);
440 +do_unlzma(struct unlzma_ctx *ctx)
442 + u8 hdr_buf[sizeof(struct lzma_header)];
443 + struct lzma_header *header = (struct lzma_header *)hdr_buf;
444 + u32 pos_state_mask;
445 + u32 literal_pos_mask;
451 + for (i = 0; i < sizeof(struct lzma_header); i++) {
452 + hdr_buf[i] = rc_read(ctx);
457 + ctx->active = true;
459 + ctx->rep0 = ctx->rep1 = ctx->rep2 = ctx->rep3 = 1;
461 + ctx->previous_byte = 0;
463 + ctx->range = 0xFFFFFFFF;
465 + ctx->dict_size = le32_to_cpu(header->dict_size);
467 + if (header->pos >= (9 * 5 * 5))
482 + pos_state_mask = (1 << pb) - 1;
483 + literal_pos_mask = (1 << lp) - 1;
485 + if (ctx->dict_size == 0)
486 + ctx->dict_size = 1;
488 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
489 + if (ctx->workspace_size < num_probs * sizeof(*p)) {
490 + if (ctx->workspace)
491 + vfree(ctx->workspace);
492 + ctx->workspace = vmalloc(num_probs * sizeof(*p));
494 + p = (u16 *) ctx->workspace;
498 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
499 + for (i = 0; i < num_probs; i++)
500 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
502 + for (i = 0; i < 5; i++)
506 + int pos_state = ctx->pos & pos_state_mask;
507 + u16 *prob = p + LZMA_IS_MATCH +
508 + (ctx->state << LZMA_NUM_POS_BITS_MAX) + pos_state;
509 + if (rc_is_bit_0(ctx, prob))
510 + process_bit0(ctx, p, pos_state, prob,
511 + lc, literal_pos_mask);
513 + process_bit1(ctx, p, pos_state, prob);
514 + if (ctx->rep0 == 0)
517 + if (unlzma_should_stop(ctx))
526 +unlzma_reset_buf(struct unlzma_ctx *ctx)
529 + ctx->next_in = NULL;
530 + ctx->avail_out = 0;
531 + ctx->next_out = NULL;
535 +unlzma_thread(void *data)
537 + struct unlzma_ctx *ctx = data;
539 + mutex_lock(&ctx->mutex);
541 + if (do_unlzma(ctx) < 0)
543 + unlzma_reset_buf(ctx);
544 + ctx->cancel = false;
545 + ctx->active = false;
546 + while (ctx->head) {
547 + struct unlzma_buffer *bh = ctx->head;
548 + ctx->head = bh->last;
551 + } while (!kthread_should_stop());
552 + mutex_unlock(&ctx->mutex);
558 +unlzma_init(struct crypto_tfm *tfm)
564 +unlzma_cancel(struct unlzma_ctx *ctx)
566 + unlzma_reset_buf(ctx);
571 + ctx->cancel = true;
573 + mutex_unlock(&ctx->mutex);
574 + wake_up(&ctx->next_req);
576 + mutex_lock(&ctx->mutex);
577 + } while (ctx->cancel);
582 +unlzma_exit(struct crypto_tfm *tfm)
584 + struct unlzma_ctx *ctx = crypto_tfm_ctx(tfm);
587 + unlzma_cancel(ctx);
588 + kthread_stop(ctx->thread);
589 + ctx->thread = NULL;
594 +unlzma_decompress_setup(struct crypto_pcomp *tfm, void *p, unsigned int len)
596 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
602 + mutex_init(&ctx->mutex);
603 + init_waitqueue_head(&ctx->next_req);
604 + ctx->thread = kthread_run(unlzma_thread, ctx, "unlzma/%d", instance++);
605 + if (IS_ERR(ctx->thread)) {
606 + ret = PTR_ERR(ctx->thread);
607 + ctx->thread = NULL;
614 +unlzma_decompress_init(struct crypto_pcomp *tfm)
616 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
623 +unlzma_wait_complete(struct unlzma_ctx *ctx, bool finish)
626 + mutex_unlock(&ctx->mutex);
627 + wake_up(&ctx->next_req);
629 + mutex_lock(&ctx->mutex);
630 + } while (ctx->active && (ctx->avail_in > 0) && (ctx->avail_out > 0));
634 +unlzma_decompress_update(struct crypto_pcomp *tfm, struct comp_request *req)
636 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
639 + mutex_lock(&ctx->mutex);
640 + if (!ctx->active && !req->avail_in)
644 + ctx->next_in = req->next_in;
645 + ctx->avail_in = req->avail_in;
646 + ctx->next_out = req->next_out;
647 + ctx->avail_out = req->avail_out;
649 + unlzma_wait_complete(ctx, false);
651 + req->next_in = ctx->next_in;
652 + req->avail_in = ctx->avail_in;
653 + req->next_out = ctx->next_out;
654 + req->avail_out = ctx->avail_out;
655 + pos = ctx->pos - pos;
658 + mutex_unlock(&ctx->mutex);
663 +unlzma_decompress_final(struct crypto_pcomp *tfm, struct comp_request *req)
665 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
668 + /* cancel pending operation */
669 + mutex_lock(&ctx->mutex);
672 + unlzma_cancel(ctx);
675 + mutex_unlock(&ctx->mutex);
680 +static struct pcomp_alg unlzma_alg = {
681 + .decompress_setup = unlzma_decompress_setup,
682 + .decompress_init = unlzma_decompress_init,
683 + .decompress_update = unlzma_decompress_update,
684 + .decompress_final = unlzma_decompress_final,
687 + .cra_name = "lzma",
688 + .cra_flags = CRYPTO_ALG_TYPE_PCOMPRESS,
689 + .cra_ctxsize = sizeof(struct unlzma_ctx),
690 + .cra_module = THIS_MODULE,
691 + .cra_init = unlzma_init,
692 + .cra_exit = unlzma_exit,
697 +unlzma_mod_init(void)
699 + return crypto_register_pcomp(&unlzma_alg);
703 +unlzma_mod_exit(void)
705 + crypto_unregister_pcomp(&unlzma_alg);
708 +module_init(unlzma_mod_init);
709 +module_exit(unlzma_mod_exit);
711 +MODULE_LICENSE("GPL");
712 +MODULE_DESCRIPTION("LZMA Decompression Algorithm");
713 +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
716 @@ -758,6 +758,12 @@ config CRYPTO_ZLIB
718 This is the zlib algorithm.
720 +config CRYPTO_UNLZMA
721 + tristate "LZMA decompression"
722 + select CRYPTO_PCOMP
724 + This is the lzma decompression module.
727 tristate "LZO compression algorithm"
729 --- a/crypto/Makefile
730 +++ b/crypto/Makefile
731 @@ -75,6 +75,7 @@ obj-$(CONFIG_CRYPTO_SEED) += seed.o
732 obj-$(CONFIG_CRYPTO_SALSA20) += salsa20_generic.o
733 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
734 obj-$(CONFIG_CRYPTO_ZLIB) += zlib.o
735 +obj-$(CONFIG_CRYPTO_UNLZMA) += unlzma.o
736 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
737 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
738 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o
740 +++ b/crypto/unlzma.h
742 +/* LZMA uncompresion module for pcomp
743 + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
746 + * Initial Linux kernel adaptation
747 + * Copyright (C) 2006 Alain < alain@knaff.lu >
749 + * Based on small lzma deflate implementation/Small range coder
750 + * implementation for lzma.
751 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
753 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
754 + * Copyright (C) 1999-2005 Igor Pavlov
756 + * This program is free software; you can redistribute it and/or modify it
757 + * under the terms of the GNU General Public License version 2 as published
758 + * by the Free Software Foundation.
763 +struct lzma_header {
766 +} __attribute__ ((packed)) ;
769 +#define RC_TOP_BITS 24
770 +#define RC_MOVE_BITS 5
771 +#define RC_MODEL_TOTAL_BITS 11
773 +#define LZMA_BASE_SIZE 1846
774 +#define LZMA_LIT_SIZE 768
776 +#define LZMA_NUM_POS_BITS_MAX 4
778 +#define LZMA_LEN_NUM_LOW_BITS 3
779 +#define LZMA_LEN_NUM_MID_BITS 3
780 +#define LZMA_LEN_NUM_HIGH_BITS 8
782 +#define LZMA_LEN_CHOICE 0
783 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
784 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
785 +#define LZMA_LEN_MID (LZMA_LEN_LOW \
786 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
787 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \
788 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
789 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
791 +#define LZMA_NUM_STATES 12
792 +#define LZMA_NUM_LIT_STATES 7
794 +#define LZMA_START_POS_MODEL_INDEX 4
795 +#define LZMA_END_POS_MODEL_INDEX 14
796 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
798 +#define LZMA_NUM_POS_SLOT_BITS 6
799 +#define LZMA_NUM_LEN_TO_POS_STATES 4
801 +#define LZMA_NUM_ALIGN_BITS 4
803 +#define LZMA_MATCH_MIN_LEN 2
805 +#define LZMA_IS_MATCH 0
806 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
807 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
808 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
809 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
810 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
811 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
812 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
813 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \
814 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
815 +#define LZMA_ALIGN (LZMA_SPEC_POS \
816 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
817 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
818 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
819 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)