75dfc376a6597e22eac86b40505377bc10f4aec8
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.30 / 052-pcomp_lzma_support.patch
1 --- /dev/null
2 +++ b/crypto/unlzma.c
3 @@ -0,0 +1,723 @@
4 +/*
5 + * LZMA uncompresion module for pcomp
6 + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
7 + *
8 + * Based on:
9 + * Initial Linux kernel adaptation
10 + * Copyright (C) 2006 Alain < alain@knaff.lu >
11 + *
12 + * Based on small lzma deflate implementation/Small range coder
13 + * implementation for lzma.
14 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
15 + *
16 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
17 + * Copyright (C) 1999-2005 Igor Pavlov
18 + *
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.
22 + *
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.
27 + */
28 +
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>
37 +
38 +#include <crypto/internal/compress.h>
39 +#include "unlzma.h"
40 +
41 +static int instance = 0;
42 +
43 +struct unlzma_buffer {
44 + struct unlzma_buffer *last;
45 + int offset;
46 + int size;
47 + u8 *ptr;
48 +};
49 +
50 +struct unlzma_ctx {
51 + struct task_struct *thread;
52 + wait_queue_head_t next_req;
53 + struct mutex mutex;
54 + bool active;
55 + bool cancel;
56 +
57 + const u8 *next_in;
58 + int avail_in;
59 +
60 + u8 *next_out;
61 + int avail_out;
62 +
63 + /* reader state */
64 + u32 code;
65 + u32 range;
66 + u32 bound;
67 +
68 + /* writer state */
69 + u8 previous_byte;
70 + ssize_t pos;
71 + struct unlzma_buffer *head;
72 + int buf_full;
73 +
74 + /* cstate */
75 + int state;
76 + u32 rep0, rep1, rep2, rep3;
77 +
78 + u32 dict_size;
79 +
80 + void *workspace;
81 + int workspace_size;
82 +};
83 +
84 +static inline bool
85 +unlzma_should_stop(struct unlzma_ctx *ctx)
86 +{
87 + return unlikely(kthread_should_stop() || ctx->cancel);
88 +}
89 +
90 +static void
91 +get_buffer(struct unlzma_ctx *ctx)
92 +{
93 + struct unlzma_buffer *bh;
94 +
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;
100 + ctx->head = bh;
101 + ctx->buf_full = 0;
102 +}
103 +
104 +static void
105 +unlzma_request_buffer(struct unlzma_ctx *ctx, int *avail)
106 +{
107 + do {
108 + mutex_unlock(&ctx->mutex);
109 + if (wait_event_interruptible(ctx->next_req,
110 + unlzma_should_stop(ctx) || (*avail > 0)))
111 + schedule();
112 + mutex_lock(&ctx->mutex);
113 + } while (*avail <= 0 && !unlzma_should_stop(ctx));
114 +
115 + if (!unlzma_should_stop(ctx) && ctx->buf_full)
116 + get_buffer(ctx);
117 +}
118 +
119 +static u8
120 +rc_read(struct unlzma_ctx *ctx)
121 +{
122 + if (unlikely(ctx->avail_in <= 0))
123 + unlzma_request_buffer(ctx, &ctx->avail_in);
124 +
125 + if (unlzma_should_stop(ctx))
126 + return 0;
127 +
128 + ctx->avail_in--;
129 + return *(ctx->next_in++);
130 +}
131 +
132 +
133 +static inline void
134 +rc_get_code(struct unlzma_ctx *ctx)
135 +{
136 + ctx->code = (ctx->code << 8) | rc_read(ctx);
137 +}
138 +
139 +static void
140 +rc_normalize(struct unlzma_ctx *ctx)
141 +{
142 + if (ctx->range < (1 << RC_TOP_BITS)) {
143 + ctx->range <<= 8;
144 + rc_get_code(ctx);
145 + }
146 +}
147 +
148 +static int
149 +rc_is_bit_0(struct unlzma_ctx *ctx, u16 *p)
150 +{
151 + rc_normalize(ctx);
152 + ctx->bound = *p * (ctx->range >> RC_MODEL_TOTAL_BITS);
153 + return ctx->code < ctx->bound;
154 +}
155 +
156 +static void
157 +rc_update_bit_0(struct unlzma_ctx *ctx, u16 *p)
158 +{
159 + ctx->range = ctx->bound;
160 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
161 +}
162 +
163 +static void
164 +rc_update_bit_1(struct unlzma_ctx *ctx, u16 *p)
165 +{
166 + ctx->range -= ctx->bound;
167 + ctx->code -= ctx->bound;
168 + *p -= *p >> RC_MOVE_BITS;
169 +}
170 +
171 +static bool
172 +rc_get_bit(struct unlzma_ctx *ctx, u16 *p, int *symbol)
173 +{
174 + if (rc_is_bit_0(ctx, p)) {
175 + rc_update_bit_0(ctx, p);
176 + *symbol *= 2;
177 + return 0;
178 + } else {
179 + rc_update_bit_1(ctx, p);
180 + *symbol = *symbol * 2 + 1;
181 + return 1;
182 + }
183 +}
184 +
185 +static int
186 +rc_direct_bit(struct unlzma_ctx *ctx)
187 +{
188 + rc_normalize(ctx);
189 + ctx->range >>= 1;
190 + if (ctx->code >= ctx->range) {
191 + ctx->code -= ctx->range;
192 + return 1;
193 + }
194 + return 0;
195 +}
196 +
197 +static void
198 +rc_bit_tree_decode(struct unlzma_ctx *ctx, u16 *p, int num_levels, int *symbol)
199 +{
200 + int i = num_levels;
201 +
202 + *symbol = 1;
203 + while (i--)
204 + rc_get_bit(ctx, p + *symbol, symbol);
205 + *symbol -= 1 << num_levels;
206 +}
207 +
208 +static u8
209 +peek_old_byte(struct unlzma_ctx *ctx, u32 offs)
210 +{
211 + struct unlzma_buffer *bh = ctx->head;
212 + u32 pos;
213 +
214 + pos = ctx->pos - offs;
215 + if (pos >= ctx->dict_size) {
216 + pos = (~pos % ctx->dict_size);
217 + }
218 +
219 + while (bh->offset > pos) {
220 + bh = bh->last;
221 + BUG_ON(!bh);
222 + }
223 +
224 + pos -= bh->offset;
225 + BUG_ON(pos >= bh->size);
226 +
227 + return bh->ptr[pos];
228 +}
229 +
230 +static void
231 +write_byte(struct unlzma_ctx *ctx, u8 byte)
232 +{
233 + if (unlikely(ctx->avail_out <= 0)) {
234 + unlzma_request_buffer(ctx, &ctx->avail_out);
235 + }
236 +
237 + if (!ctx->avail_out)
238 + return;
239 +
240 + ctx->previous_byte = byte;
241 + *(ctx->next_out++) = byte;
242 + ctx->avail_out--;
243 + if (ctx->avail_out == 0)
244 + ctx->buf_full = 1;
245 + ctx->pos++;
246 +}
247 +
248 +
249 +static inline void
250 +copy_byte(struct unlzma_ctx *ctx, u32 offs)
251 +{
252 + write_byte(ctx, peek_old_byte(ctx, offs));
253 +}
254 +
255 +static void
256 +copy_bytes(struct unlzma_ctx *ctx, u32 rep0, int len)
257 +{
258 + do {
259 + copy_byte(ctx, rep0);
260 + len--;
261 + if (unlzma_should_stop(ctx))
262 + break;
263 + } while (len != 0);
264 +}
265 +
266 +static void
267 +process_bit0(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob,
268 + int lc, u32 literal_pos_mask)
269 +{
270 + int mi = 1;
271 + rc_update_bit_0(ctx, prob);
272 + prob = (p + LZMA_LITERAL +
273 + (LZMA_LIT_SIZE
274 + * (((ctx->pos & literal_pos_mask) << lc)
275 + + (ctx->previous_byte >> (8 - lc))))
276 + );
277 +
278 + if (ctx->state >= LZMA_NUM_LIT_STATES) {
279 + int match_byte = peek_old_byte(ctx, ctx->rep0);
280 + do {
281 + u16 bit;
282 + u16 *prob_lit;
283 +
284 + match_byte <<= 1;
285 + bit = match_byte & 0x100;
286 + prob_lit = prob + 0x100 + bit + mi;
287 + if (rc_get_bit(ctx, prob_lit, &mi) != !!bit)
288 + break;
289 + } while (mi < 0x100);
290 + }
291 + while (mi < 0x100) {
292 + u16 *prob_lit = prob + mi;
293 + rc_get_bit(ctx, prob_lit, &mi);
294 + }
295 + write_byte(ctx, mi);
296 + if (ctx->state < 4)
297 + ctx->state = 0;
298 + else if (ctx->state < 10)
299 + ctx->state -= 3;
300 + else
301 + ctx->state -= 6;
302 +}
303 +
304 +static void
305 +process_bit1(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob)
306 +{
307 + int offset;
308 + u16 *prob_len;
309 + int num_bits;
310 + int len;
311 +
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;
321 + } else {
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
327 + + (ctx->state <<
328 + LZMA_NUM_POS_BITS_MAX) +
329 + pos_state);
330 + if (rc_is_bit_0(ctx, prob)) {
331 + rc_update_bit_0(ctx, prob);
332 +
333 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ?
334 + 9 : 11;
335 + copy_byte(ctx, ctx->rep0);
336 + return;
337 + } else {
338 + rc_update_bit_1(ctx, prob);
339 + }
340 + } else {
341 + u32 distance;
342 +
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;
348 + } else {
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;
354 + } else {
355 + rc_update_bit_1(ctx, prob);
356 + distance = ctx->rep3;
357 + ctx->rep3 = ctx->rep2;
358 + }
359 + ctx->rep2 = ctx->rep1;
360 + }
361 + ctx->rep1 = ctx->rep0;
362 + ctx->rep0 = distance;
363 + }
364 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ? 8 : 11;
365 + prob = p + LZMA_REP_LEN_CODER;
366 + }
367 +
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
372 + + (pos_state <<
373 + LZMA_LEN_NUM_LOW_BITS));
374 + offset = 0;
375 + num_bits = LZMA_LEN_NUM_LOW_BITS;
376 + } else {
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
382 + + (pos_state <<
383 + LZMA_LEN_NUM_MID_BITS));
384 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
385 + num_bits = LZMA_LEN_NUM_MID_BITS;
386 + } else {
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;
392 + }
393 + }
394 +
395 + rc_bit_tree_decode(ctx, prob_len, num_bits, &len);
396 + len += offset;
397 +
398 + if (ctx->state < 4) {
399 + int pos_slot;
400 +
401 + ctx->state += LZMA_NUM_LIT_STATES;
402 + prob =
403 + p + LZMA_POS_SLOT +
404 + ((len <
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,
410 + &pos_slot);
411 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
412 + int i, mi;
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;
419 + } else {
420 + num_bits -= LZMA_NUM_ALIGN_BITS;
421 + while (num_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;
427 + }
428 + i = 1;
429 + mi = 1;
430 + while (num_bits--) {
431 + if (rc_get_bit(ctx, prob + mi, &mi))
432 + ctx->rep0 |= i;
433 + i <<= 1;
434 + }
435 + } else
436 + ctx->rep0 = pos_slot;
437 + if (++(ctx->rep0) == 0)
438 + return;
439 + }
440 +
441 + len += LZMA_MATCH_MIN_LEN;
442 +
443 + copy_bytes(ctx, ctx->rep0, len);
444 +}
445 +
446 +
447 +static int
448 +do_unlzma(struct unlzma_ctx *ctx)
449 +{
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;
454 + int lc, pb, lp;
455 + int num_probs;
456 + int i, mi;
457 + u16 *p;
458 +
459 + for (i = 0; i < sizeof(struct lzma_header); i++) {
460 + hdr_buf[i] = rc_read(ctx);
461 + }
462 +
463 + ctx->pos = 0;
464 + get_buffer(ctx);
465 + ctx->active = true;
466 + ctx->state = 0;
467 + ctx->rep0 = ctx->rep1 = ctx->rep2 = ctx->rep3 = 1;
468 +
469 + ctx->previous_byte = 0;
470 + ctx->code = 0;
471 + ctx->range = 0xFFFFFFFF;
472 +
473 + ctx->dict_size = le32_to_cpu(header->dict_size);
474 +
475 + if (header->pos >= (9 * 5 * 5))
476 + return -1;
477 +
478 + mi = 0;
479 + lc = header->pos;
480 + while (lc >= 9) {
481 + mi++;
482 + lc -= 9;
483 + }
484 + pb = 0;
485 + lp = mi;
486 + while (lp >= 5) {
487 + pb++;
488 + lp -= 5;
489 + }
490 + pos_state_mask = (1 << pb) - 1;
491 + literal_pos_mask = (1 << lp) - 1;
492 +
493 + if (ctx->dict_size == 0)
494 + ctx->dict_size = 1;
495 +
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);
502 + }
503 + p = (u16 *) ctx->workspace;
504 + if (!p)
505 + return -1;
506 +
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;
510 +
511 + for (i = 0; i < 5; i++)
512 + rc_get_code(ctx);
513 +
514 + while (1) {
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);
521 + else {
522 + process_bit1(ctx, p, pos_state, prob);
523 + if (ctx->rep0 == 0)
524 + break;
525 + }
526 + if (unlzma_should_stop(ctx))
527 + break;
528 + }
529 + if (likely(!unlzma_should_stop(ctx)))
530 + rc_normalize(ctx);
531 +
532 + return ctx->pos;
533 +}
534 +
535 +
536 +static void
537 +unlzma_reset_buf(struct unlzma_ctx *ctx)
538 +{
539 + ctx->avail_in = 0;
540 + ctx->next_in = NULL;
541 + ctx->avail_out = 0;
542 + ctx->next_out = NULL;
543 +}
544 +
545 +static int
546 +unlzma_thread(void *data)
547 +{
548 + struct unlzma_ctx *ctx = data;
549 +
550 + mutex_lock(&ctx->mutex);
551 + do {
552 + if (do_unlzma(ctx) < 0)
553 + ctx->pos = 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;
560 + kfree(bh);
561 + }
562 + } while (!kthread_should_stop());
563 + mutex_unlock(&ctx->mutex);
564 + return 0;
565 +}
566 +
567 +
568 +static int
569 +unlzma_init(struct crypto_tfm *tfm)
570 +{
571 + return 0;
572 +}
573 +
574 +static void
575 +unlzma_cancel(struct unlzma_ctx *ctx)
576 +{
577 + unlzma_reset_buf(ctx);
578 +
579 + if (!ctx->active)
580 + return;
581 +
582 + ctx->cancel = true;
583 + do {
584 + mutex_unlock(&ctx->mutex);
585 + wake_up(&ctx->next_req);
586 + schedule();
587 + mutex_lock(&ctx->mutex);
588 + } while (ctx->cancel);
589 +}
590 +
591 +
592 +static void
593 +unlzma_exit(struct crypto_tfm *tfm)
594 +{
595 + struct unlzma_ctx *ctx = crypto_tfm_ctx(tfm);
596 +
597 + if (ctx->thread) {
598 + unlzma_cancel(ctx);
599 + kthread_stop(ctx->thread);
600 + ctx->thread = NULL;
601 + }
602 +}
603 +
604 +static int
605 +unlzma_decompress_setup(struct crypto_pcomp *tfm, void *p, unsigned int len)
606 +{
607 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
608 + int ret = 0;
609 +
610 + if (ctx->thread)
611 + return 0;
612 +
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;
619 + }
620 +
621 + return ret;
622 +}
623 +
624 +static int
625 +unlzma_decompress_init(struct crypto_pcomp *tfm)
626 +{
627 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
628 +
629 + ctx->pos = 0;
630 + return 0;
631 +}
632 +
633 +static void
634 +unlzma_wait_complete(struct unlzma_ctx *ctx, bool finish)
635 +{
636 + do {
637 + mutex_unlock(&ctx->mutex);
638 + wake_up(&ctx->next_req);
639 + schedule();
640 + mutex_lock(&ctx->mutex);
641 + } while (ctx->active && (ctx->avail_in > 0) && (ctx->avail_out > 0));
642 +}
643 +
644 +static int
645 +unlzma_decompress_update(struct crypto_pcomp *tfm, struct comp_request *req)
646 +{
647 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
648 + size_t pos = 0;
649 +
650 + mutex_lock(&ctx->mutex);
651 + if (!ctx->active && !req->avail_in)
652 + goto out;
653 +
654 + pos = ctx->pos;
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;
659 +
660 + unlzma_wait_complete(ctx, false);
661 +
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;
666 + ctx->next_in = 0;
667 + ctx->avail_in = 0;
668 + pos = ctx->pos - pos;
669 +
670 +out:
671 + mutex_unlock(&ctx->mutex);
672 + return pos;
673 +}
674 +
675 +static int
676 +unlzma_decompress_final(struct crypto_pcomp *tfm, struct comp_request *req)
677 +{
678 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
679 + int ret = 0;
680 +
681 + /* cancel pending operation */
682 + mutex_lock(&ctx->mutex);
683 + if (ctx->active) {
684 + // ret = -EINVAL;
685 + unlzma_cancel(ctx);
686 + }
687 + ctx->pos = 0;
688 + mutex_unlock(&ctx->mutex);
689 + return ret;
690 +}
691 +
692 +
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,
698 +
699 + .base = {
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,
706 + }
707 +};
708 +
709 +static int __init
710 +unlzma_mod_init(void)
711 +{
712 + return crypto_register_pcomp(&unlzma_alg);
713 +}
714 +
715 +static void __exit
716 +unlzma_mod_exit(void)
717 +{
718 + crypto_unregister_pcomp(&unlzma_alg);
719 +}
720 +
721 +module_init(unlzma_mod_init);
722 +module_exit(unlzma_mod_exit);
723 +
724 +MODULE_LICENSE("GPL");
725 +MODULE_DESCRIPTION("LZMA Decompression Algorithm");
726 +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
727 --- a/crypto/Kconfig
728 +++ b/crypto/Kconfig
729 @@ -758,6 +758,12 @@ config CRYPTO_ZLIB
730 help
731 This is the zlib algorithm.
732
733 +config CRYPTO_UNLZMA
734 + tristate "LZMA decompression"
735 + select CRYPTO_PCOMP
736 + help
737 + This is the lzma decompression module.
738 +
739 config CRYPTO_LZO
740 tristate "LZO compression algorithm"
741 select CRYPTO_ALGAPI
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
752 --- /dev/null
753 +++ b/crypto/unlzma.h
754 @@ -0,0 +1,80 @@
755 +/* LZMA uncompresion module for pcomp
756 + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
757 + *
758 + * Based on:
759 + * Initial Linux kernel adaptation
760 + * Copyright (C) 2006 Alain < alain@knaff.lu >
761 + *
762 + * Based on small lzma deflate implementation/Small range coder
763 + * implementation for lzma.
764 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
765 + *
766 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
767 + * Copyright (C) 1999-2005 Igor Pavlov
768 + *
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.
772 + */
773 +#ifndef __UNLZMA_H
774 +#define __UNLZMA_H
775 +
776 +struct lzma_header {
777 + __u8 pos;
778 + __le32 dict_size;
779 +} __attribute__ ((packed)) ;
780 +
781 +
782 +#define RC_TOP_BITS 24
783 +#define RC_MOVE_BITS 5
784 +#define RC_MODEL_TOTAL_BITS 11
785 +
786 +#define LZMA_BASE_SIZE 1846
787 +#define LZMA_LIT_SIZE 768
788 +
789 +#define LZMA_NUM_POS_BITS_MAX 4
790 +
791 +#define LZMA_LEN_NUM_LOW_BITS 3
792 +#define LZMA_LEN_NUM_MID_BITS 3
793 +#define LZMA_LEN_NUM_HIGH_BITS 8
794 +
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))
803 +
804 +#define LZMA_NUM_STATES 12
805 +#define LZMA_NUM_LIT_STATES 7
806 +
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))
810 +
811 +#define LZMA_NUM_POS_SLOT_BITS 6
812 +#define LZMA_NUM_LEN_TO_POS_STATES 4
813 +
814 +#define LZMA_NUM_ALIGN_BITS 4
815 +
816 +#define LZMA_MATCH_MIN_LEN 2
817 +
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)
833 +
834 +#endif