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;
76 + u32 rep0, rep1, rep2, rep3;
85 +unlzma_should_stop(struct unlzma_ctx *ctx)
87 + return unlikely(kthread_should_stop() || ctx->cancel);
91 +get_buffer(struct unlzma_ctx *ctx)
93 + struct unlzma_buffer *bh;
95 + bh = kzalloc(sizeof(struct unlzma_buffer), GFP_KERNEL);
96 + bh->ptr = ctx->next_out;
97 + bh->offset = ctx->pos;
98 + bh->last = ctx->head;
99 + bh->size = ctx->avail_out;
105 +unlzma_request_buffer(struct unlzma_ctx *ctx, int *avail)
108 + mutex_unlock(&ctx->mutex);
109 + if (wait_event_interruptible(ctx->next_req,
110 + unlzma_should_stop(ctx) || (*avail > 0)))
112 + mutex_lock(&ctx->mutex);
113 + } while (*avail <= 0 && !unlzma_should_stop(ctx));
115 + if (!unlzma_should_stop(ctx) && ctx->buf_full)
120 +rc_read(struct unlzma_ctx *ctx)
122 + if (unlikely(ctx->avail_in <= 0))
123 + unlzma_request_buffer(ctx, &ctx->avail_in);
125 + if (unlzma_should_stop(ctx))
129 + return *(ctx->next_in++);
134 +rc_get_code(struct unlzma_ctx *ctx)
136 + ctx->code = (ctx->code << 8) | rc_read(ctx);
140 +rc_normalize(struct unlzma_ctx *ctx)
142 + if (ctx->range < (1 << RC_TOP_BITS)) {
149 +rc_is_bit_0(struct unlzma_ctx *ctx, u16 *p)
152 + ctx->bound = *p * (ctx->range >> RC_MODEL_TOTAL_BITS);
153 + return ctx->code < ctx->bound;
157 +rc_update_bit_0(struct unlzma_ctx *ctx, u16 *p)
159 + ctx->range = ctx->bound;
160 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
164 +rc_update_bit_1(struct unlzma_ctx *ctx, u16 *p)
166 + ctx->range -= ctx->bound;
167 + ctx->code -= ctx->bound;
168 + *p -= *p >> RC_MOVE_BITS;
172 +rc_get_bit(struct unlzma_ctx *ctx, u16 *p, int *symbol)
174 + if (rc_is_bit_0(ctx, p)) {
175 + rc_update_bit_0(ctx, p);
179 + rc_update_bit_1(ctx, p);
180 + *symbol = *symbol * 2 + 1;
186 +rc_direct_bit(struct unlzma_ctx *ctx)
190 + if (ctx->code >= ctx->range) {
191 + ctx->code -= ctx->range;
198 +rc_bit_tree_decode(struct unlzma_ctx *ctx, u16 *p, int num_levels, int *symbol)
200 + int i = num_levels;
204 + rc_get_bit(ctx, p + *symbol, symbol);
205 + *symbol -= 1 << num_levels;
209 +peek_old_byte(struct unlzma_ctx *ctx, u32 offs)
211 + struct unlzma_buffer *bh = ctx->head;
214 + pos = ctx->pos - offs;
215 + if (pos >= ctx->dict_size) {
216 + pos = (~pos % ctx->dict_size);
219 + while (bh->offset > pos) {
225 + BUG_ON(pos >= bh->size);
227 + return bh->ptr[pos];
231 +write_byte(struct unlzma_ctx *ctx, u8 byte)
233 + if (unlikely(ctx->avail_out <= 0)) {
234 + unlzma_request_buffer(ctx, &ctx->avail_out);
237 + if (!ctx->avail_out)
240 + ctx->previous_byte = byte;
241 + *(ctx->next_out++) = byte;
243 + if (ctx->avail_out == 0)
250 +copy_byte(struct unlzma_ctx *ctx, u32 offs)
252 + write_byte(ctx, peek_old_byte(ctx, offs));
256 +copy_bytes(struct unlzma_ctx *ctx, u32 rep0, int len)
259 + copy_byte(ctx, rep0);
261 + if (unlzma_should_stop(ctx))
263 + } while (len != 0);
267 +process_bit0(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob,
268 + int lc, u32 literal_pos_mask)
271 + rc_update_bit_0(ctx, prob);
272 + prob = (p + LZMA_LITERAL +
274 + * (((ctx->pos & literal_pos_mask) << lc)
275 + + (ctx->previous_byte >> (8 - lc))))
278 + if (ctx->state >= LZMA_NUM_LIT_STATES) {
279 + int match_byte = peek_old_byte(ctx, ctx->rep0);
285 + bit = match_byte & 0x100;
286 + prob_lit = prob + 0x100 + bit + mi;
287 + if (rc_get_bit(ctx, prob_lit, &mi) != !!bit)
289 + } while (mi < 0x100);
291 + while (mi < 0x100) {
292 + u16 *prob_lit = prob + mi;
293 + rc_get_bit(ctx, prob_lit, &mi);
295 + write_byte(ctx, mi);
296 + if (ctx->state < 4)
298 + else if (ctx->state < 10)
305 +process_bit1(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob)
312 + rc_update_bit_1(ctx, prob);
313 + prob = p + LZMA_IS_REP + ctx->state;
314 + if (rc_is_bit_0(ctx, prob)) {
315 + rc_update_bit_0(ctx, prob);
316 + ctx->rep3 = ctx->rep2;
317 + ctx->rep2 = ctx->rep1;
318 + ctx->rep1 = ctx->rep0;
319 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ? 0 : 3;
320 + prob = p + LZMA_LEN_CODER;
322 + rc_update_bit_1(ctx, prob);
323 + prob = p + LZMA_IS_REP_G0 + ctx->state;
324 + if (rc_is_bit_0(ctx, prob)) {
325 + rc_update_bit_0(ctx, prob);
326 + prob = (p + LZMA_IS_REP_0_LONG
328 + LZMA_NUM_POS_BITS_MAX) +
330 + if (rc_is_bit_0(ctx, prob)) {
331 + rc_update_bit_0(ctx, prob);
333 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ?
335 + copy_byte(ctx, ctx->rep0);
338 + rc_update_bit_1(ctx, prob);
343 + rc_update_bit_1(ctx, prob);
344 + prob = p + LZMA_IS_REP_G1 + ctx->state;
345 + if (rc_is_bit_0(ctx, prob)) {
346 + rc_update_bit_0(ctx, prob);
347 + distance = ctx->rep1;
349 + rc_update_bit_1(ctx, prob);
350 + prob = p + LZMA_IS_REP_G2 + ctx->state;
351 + if (rc_is_bit_0(ctx, prob)) {
352 + rc_update_bit_0(ctx, prob);
353 + distance = ctx->rep2;
355 + rc_update_bit_1(ctx, prob);
356 + distance = ctx->rep3;
357 + ctx->rep3 = ctx->rep2;
359 + ctx->rep2 = ctx->rep1;
361 + ctx->rep1 = ctx->rep0;
362 + ctx->rep0 = distance;
364 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ? 8 : 11;
365 + prob = p + LZMA_REP_LEN_CODER;
368 + prob_len = prob + LZMA_LEN_CHOICE;
369 + if (rc_is_bit_0(ctx, prob_len)) {
370 + rc_update_bit_0(ctx, prob_len);
371 + prob_len = (prob + LZMA_LEN_LOW
373 + LZMA_LEN_NUM_LOW_BITS));
375 + num_bits = LZMA_LEN_NUM_LOW_BITS;
377 + rc_update_bit_1(ctx, prob_len);
378 + prob_len = prob + LZMA_LEN_CHOICE_2;
379 + if (rc_is_bit_0(ctx, prob_len)) {
380 + rc_update_bit_0(ctx, prob_len);
381 + prob_len = (prob + LZMA_LEN_MID
383 + LZMA_LEN_NUM_MID_BITS));
384 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
385 + num_bits = LZMA_LEN_NUM_MID_BITS;
387 + rc_update_bit_1(ctx, prob_len);
388 + prob_len = prob + LZMA_LEN_HIGH;
389 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
390 + + (1 << LZMA_LEN_NUM_MID_BITS));
391 + num_bits = LZMA_LEN_NUM_HIGH_BITS;
395 + rc_bit_tree_decode(ctx, prob_len, num_bits, &len);
398 + if (ctx->state < 4) {
401 + ctx->state += LZMA_NUM_LIT_STATES;
403 + p + LZMA_POS_SLOT +
405 + LZMA_NUM_LEN_TO_POS_STATES ? len :
406 + LZMA_NUM_LEN_TO_POS_STATES - 1)
407 + << LZMA_NUM_POS_SLOT_BITS);
408 + rc_bit_tree_decode(ctx, prob,
409 + LZMA_NUM_POS_SLOT_BITS,
411 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
413 + num_bits = (pos_slot >> 1) - 1;
414 + ctx->rep0 = 2 | (pos_slot & 1);
415 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
416 + ctx->rep0 <<= num_bits;
417 + prob = p + LZMA_SPEC_POS +
418 + ctx->rep0 - pos_slot - 1;
420 + num_bits -= LZMA_NUM_ALIGN_BITS;
422 + ctx->rep0 = (ctx->rep0 << 1) |
423 + rc_direct_bit(ctx);
424 + prob = p + LZMA_ALIGN;
425 + ctx->rep0 <<= LZMA_NUM_ALIGN_BITS;
426 + num_bits = LZMA_NUM_ALIGN_BITS;
430 + while (num_bits--) {
431 + if (rc_get_bit(ctx, prob + mi, &mi))
436 + ctx->rep0 = pos_slot;
437 + if (++(ctx->rep0) == 0)
441 + len += LZMA_MATCH_MIN_LEN;
443 + copy_bytes(ctx, ctx->rep0, len);
448 +do_unlzma(struct unlzma_ctx *ctx)
450 + u8 hdr_buf[sizeof(struct lzma_header)];
451 + struct lzma_header *header = (struct lzma_header *)hdr_buf;
452 + u32 pos_state_mask;
453 + u32 literal_pos_mask;
459 + for (i = 0; i < sizeof(struct lzma_header); i++) {
460 + hdr_buf[i] = rc_read(ctx);
465 + ctx->active = true;
467 + ctx->rep0 = ctx->rep1 = ctx->rep2 = ctx->rep3 = 1;
469 + ctx->previous_byte = 0;
471 + ctx->range = 0xFFFFFFFF;
473 + ctx->dict_size = le32_to_cpu(header->dict_size);
475 + if (header->pos >= (9 * 5 * 5))
490 + pos_state_mask = (1 << pb) - 1;
491 + literal_pos_mask = (1 << lp) - 1;
493 + if (ctx->dict_size == 0)
494 + ctx->dict_size = 1;
496 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
497 + if (ctx->workspace_size < num_probs * sizeof(*p)) {
498 + if (ctx->workspace)
499 + vfree(ctx->workspace);
500 + ctx->workspace_size = num_probs * sizeof(*p);
501 + ctx->workspace = vmalloc(ctx->workspace_size);
503 + p = (u16 *) ctx->workspace;
507 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
508 + for (i = 0; i < num_probs; i++)
509 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
511 + for (i = 0; i < 5; i++)
515 + int pos_state = ctx->pos & pos_state_mask;
516 + u16 *prob = p + LZMA_IS_MATCH +
517 + (ctx->state << LZMA_NUM_POS_BITS_MAX) + pos_state;
518 + if (rc_is_bit_0(ctx, prob))
519 + process_bit0(ctx, p, pos_state, prob,
520 + lc, literal_pos_mask);
522 + process_bit1(ctx, p, pos_state, prob);
523 + if (ctx->rep0 == 0)
526 + if (unlzma_should_stop(ctx))
529 + if (likely(!unlzma_should_stop(ctx)))
537 +unlzma_reset_buf(struct unlzma_ctx *ctx)
540 + ctx->next_in = NULL;
541 + ctx->avail_out = 0;
542 + ctx->next_out = NULL;
546 +unlzma_thread(void *data)
548 + struct unlzma_ctx *ctx = data;
550 + mutex_lock(&ctx->mutex);
552 + if (do_unlzma(ctx) < 0)
554 + unlzma_reset_buf(ctx);
555 + ctx->cancel = false;
556 + ctx->active = false;
557 + while (ctx->head) {
558 + struct unlzma_buffer *bh = ctx->head;
559 + ctx->head = bh->last;
562 + } while (!kthread_should_stop());
563 + mutex_unlock(&ctx->mutex);
569 +unlzma_init(struct crypto_tfm *tfm)
575 +unlzma_cancel(struct unlzma_ctx *ctx)
577 + unlzma_reset_buf(ctx);
582 + ctx->cancel = true;
584 + mutex_unlock(&ctx->mutex);
585 + wake_up(&ctx->next_req);
587 + mutex_lock(&ctx->mutex);
588 + } while (ctx->cancel);
593 +unlzma_exit(struct crypto_tfm *tfm)
595 + struct unlzma_ctx *ctx = crypto_tfm_ctx(tfm);
598 + unlzma_cancel(ctx);
599 + kthread_stop(ctx->thread);
600 + ctx->thread = NULL;
605 +unlzma_decompress_setup(struct crypto_pcomp *tfm, void *p, unsigned int len)
607 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
613 + mutex_init(&ctx->mutex);
614 + init_waitqueue_head(&ctx->next_req);
615 + ctx->thread = kthread_run(unlzma_thread, ctx, "unlzma/%d", instance++);
616 + if (IS_ERR(ctx->thread)) {
617 + ret = PTR_ERR(ctx->thread);
618 + ctx->thread = NULL;
625 +unlzma_decompress_init(struct crypto_pcomp *tfm)
627 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
634 +unlzma_wait_complete(struct unlzma_ctx *ctx, bool finish)
637 + mutex_unlock(&ctx->mutex);
638 + wake_up(&ctx->next_req);
640 + mutex_lock(&ctx->mutex);
641 + } while (ctx->active && (ctx->avail_in > 0) && (ctx->avail_out > 0));
645 +unlzma_decompress_update(struct crypto_pcomp *tfm, struct comp_request *req)
647 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
650 + mutex_lock(&ctx->mutex);
651 + if (!ctx->active && !req->avail_in)
655 + ctx->next_in = req->next_in;
656 + ctx->avail_in = req->avail_in;
657 + ctx->next_out = req->next_out;
658 + ctx->avail_out = req->avail_out;
660 + unlzma_wait_complete(ctx, false);
662 + req->next_in = ctx->next_in;
663 + req->avail_in = ctx->avail_in;
664 + req->next_out = ctx->next_out;
665 + req->avail_out = ctx->avail_out;
668 + pos = ctx->pos - pos;
671 + mutex_unlock(&ctx->mutex);
676 +unlzma_decompress_final(struct crypto_pcomp *tfm, struct comp_request *req)
678 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
681 + /* cancel pending operation */
682 + mutex_lock(&ctx->mutex);
685 + unlzma_cancel(ctx);
688 + mutex_unlock(&ctx->mutex);
693 +static struct pcomp_alg unlzma_alg = {
694 + .decompress_setup = unlzma_decompress_setup,
695 + .decompress_init = unlzma_decompress_init,
696 + .decompress_update = unlzma_decompress_update,
697 + .decompress_final = unlzma_decompress_final,
700 + .cra_name = "lzma",
701 + .cra_flags = CRYPTO_ALG_TYPE_PCOMPRESS,
702 + .cra_ctxsize = sizeof(struct unlzma_ctx),
703 + .cra_module = THIS_MODULE,
704 + .cra_init = unlzma_init,
705 + .cra_exit = unlzma_exit,
710 +unlzma_mod_init(void)
712 + return crypto_register_pcomp(&unlzma_alg);
716 +unlzma_mod_exit(void)
718 + crypto_unregister_pcomp(&unlzma_alg);
721 +module_init(unlzma_mod_init);
722 +module_exit(unlzma_mod_exit);
724 +MODULE_LICENSE("GPL");
725 +MODULE_DESCRIPTION("LZMA Decompression Algorithm");
726 +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
729 @@ -768,6 +768,12 @@ config CRYPTO_ZLIB
731 This is the zlib algorithm.
733 +config CRYPTO_UNLZMA
734 + tristate "LZMA decompression"
735 + select CRYPTO_PCOMP
737 + This is the lzma decompression module.
740 tristate "LZO compression algorithm"
742 --- a/crypto/Makefile
743 +++ b/crypto/Makefile
744 @@ -75,6 +75,7 @@ obj-$(CONFIG_CRYPTO_SEED) += seed.o
745 obj-$(CONFIG_CRYPTO_SALSA20) += salsa20_generic.o
746 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
747 obj-$(CONFIG_CRYPTO_ZLIB) += zlib.o
748 +obj-$(CONFIG_CRYPTO_UNLZMA) += unlzma.o
749 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
750 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
751 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o
753 +++ b/crypto/unlzma.h
755 +/* LZMA uncompresion module for pcomp
756 + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
759 + * Initial Linux kernel adaptation
760 + * Copyright (C) 2006 Alain < alain@knaff.lu >
762 + * Based on small lzma deflate implementation/Small range coder
763 + * implementation for lzma.
764 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
766 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
767 + * Copyright (C) 1999-2005 Igor Pavlov
769 + * This program is free software; you can redistribute it and/or modify it
770 + * under the terms of the GNU General Public License version 2 as published
771 + * by the Free Software Foundation.
776 +struct lzma_header {
779 +} __attribute__ ((packed)) ;
782 +#define RC_TOP_BITS 24
783 +#define RC_MOVE_BITS 5
784 +#define RC_MODEL_TOTAL_BITS 11
786 +#define LZMA_BASE_SIZE 1846
787 +#define LZMA_LIT_SIZE 768
789 +#define LZMA_NUM_POS_BITS_MAX 4
791 +#define LZMA_LEN_NUM_LOW_BITS 3
792 +#define LZMA_LEN_NUM_MID_BITS 3
793 +#define LZMA_LEN_NUM_HIGH_BITS 8
795 +#define LZMA_LEN_CHOICE 0
796 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
797 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
798 +#define LZMA_LEN_MID (LZMA_LEN_LOW \
799 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
800 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \
801 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
802 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
804 +#define LZMA_NUM_STATES 12
805 +#define LZMA_NUM_LIT_STATES 7
807 +#define LZMA_START_POS_MODEL_INDEX 4
808 +#define LZMA_END_POS_MODEL_INDEX 14
809 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
811 +#define LZMA_NUM_POS_SLOT_BITS 6
812 +#define LZMA_NUM_LEN_TO_POS_STATES 4
814 +#define LZMA_NUM_ALIGN_BITS 4
816 +#define LZMA_MATCH_MIN_LEN 2
818 +#define LZMA_IS_MATCH 0
819 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
820 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
821 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
822 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
823 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
824 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
825 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
826 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \
827 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
828 +#define LZMA_ALIGN (LZMA_SPEC_POS \
829 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
830 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
831 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
832 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)