remove an empty patch file (thx, xiangfu)
[openwrt.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,748 @@
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 <net/netlink.h>
40 +#include "unlzma.h"
41 +
42 +static int instance = 0;
43 +
44 +struct unlzma_buffer {
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 + int buf_full;
72 + int n_buffers;
73 + int buffers_max;
74 + struct unlzma_buffer *buffers;
75 +
76 + /* cstate */
77 + int state;
78 + u32 rep0, rep1, rep2, rep3;
79 +
80 + u32 dict_size;
81 +
82 + void *workspace;
83 + int workspace_size;
84 +};
85 +
86 +static inline bool
87 +unlzma_should_stop(struct unlzma_ctx *ctx)
88 +{
89 + return unlikely(kthread_should_stop() || ctx->cancel);
90 +}
91 +
92 +static void
93 +get_buffer(struct unlzma_ctx *ctx)
94 +{
95 + struct unlzma_buffer *bh;
96 +
97 + BUG_ON(ctx->n_buffers >= ctx->buffers_max);
98 + bh = &ctx->buffers[ctx->n_buffers++];
99 + bh->ptr = ctx->next_out;
100 + bh->offset = ctx->pos;
101 + bh->size = ctx->avail_out;
102 + ctx->buf_full = 0;
103 +}
104 +
105 +static void
106 +unlzma_request_buffer(struct unlzma_ctx *ctx, int *avail)
107 +{
108 + do {
109 + mutex_unlock(&ctx->mutex);
110 + if (wait_event_interruptible(ctx->next_req,
111 + unlzma_should_stop(ctx) || (*avail > 0)))
112 + schedule();
113 + mutex_lock(&ctx->mutex);
114 + } while (*avail <= 0 && !unlzma_should_stop(ctx));
115 +
116 + if (!unlzma_should_stop(ctx) && ctx->buf_full)
117 + get_buffer(ctx);
118 +}
119 +
120 +static u8
121 +rc_read(struct unlzma_ctx *ctx)
122 +{
123 + if (unlikely(ctx->avail_in <= 0))
124 + unlzma_request_buffer(ctx, &ctx->avail_in);
125 +
126 + if (unlzma_should_stop(ctx))
127 + return 0;
128 +
129 + ctx->avail_in--;
130 + return *(ctx->next_in++);
131 +}
132 +
133 +
134 +static inline void
135 +rc_get_code(struct unlzma_ctx *ctx)
136 +{
137 + ctx->code = (ctx->code << 8) | rc_read(ctx);
138 +}
139 +
140 +static void
141 +rc_normalize(struct unlzma_ctx *ctx)
142 +{
143 + if (ctx->range < (1 << RC_TOP_BITS)) {
144 + ctx->range <<= 8;
145 + rc_get_code(ctx);
146 + }
147 +}
148 +
149 +static int
150 +rc_is_bit_0(struct unlzma_ctx *ctx, u16 *p)
151 +{
152 + rc_normalize(ctx);
153 + ctx->bound = *p * (ctx->range >> RC_MODEL_TOTAL_BITS);
154 + return ctx->code < ctx->bound;
155 +}
156 +
157 +static void
158 +rc_update_bit_0(struct unlzma_ctx *ctx, u16 *p)
159 +{
160 + ctx->range = ctx->bound;
161 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
162 +}
163 +
164 +static void
165 +rc_update_bit_1(struct unlzma_ctx *ctx, u16 *p)
166 +{
167 + ctx->range -= ctx->bound;
168 + ctx->code -= ctx->bound;
169 + *p -= *p >> RC_MOVE_BITS;
170 +}
171 +
172 +static bool
173 +rc_get_bit(struct unlzma_ctx *ctx, u16 *p, int *symbol)
174 +{
175 + if (rc_is_bit_0(ctx, p)) {
176 + rc_update_bit_0(ctx, p);
177 + *symbol *= 2;
178 + return 0;
179 + } else {
180 + rc_update_bit_1(ctx, p);
181 + *symbol = *symbol * 2 + 1;
182 + return 1;
183 + }
184 +}
185 +
186 +static int
187 +rc_direct_bit(struct unlzma_ctx *ctx)
188 +{
189 + rc_normalize(ctx);
190 + ctx->range >>= 1;
191 + if (ctx->code >= ctx->range) {
192 + ctx->code -= ctx->range;
193 + return 1;
194 + }
195 + return 0;
196 +}
197 +
198 +static void
199 +rc_bit_tree_decode(struct unlzma_ctx *ctx, u16 *p, int num_levels, int *symbol)
200 +{
201 + int i = num_levels;
202 +
203 + *symbol = 1;
204 + while (i--)
205 + rc_get_bit(ctx, p + *symbol, symbol);
206 + *symbol -= 1 << num_levels;
207 +}
208 +
209 +static u8
210 +peek_old_byte(struct unlzma_ctx *ctx, u32 offs)
211 +{
212 + struct unlzma_buffer *bh = &ctx->buffers[ctx->n_buffers - 1];
213 + int i = ctx->n_buffers;
214 + u32 pos;
215 +
216 + BUG_ON(!ctx->n_buffers);
217 + pos = ctx->pos - offs;
218 + if (pos >= ctx->dict_size) {
219 + pos = (~pos % ctx->dict_size);
220 + }
221 +
222 + while (bh->offset > pos) {
223 + bh--;
224 + i--;
225 + BUG_ON(!i);
226 + }
227 +
228 + pos -= bh->offset;
229 + BUG_ON(pos >= bh->size);
230 +
231 + return bh->ptr[pos];
232 +}
233 +
234 +static void
235 +write_byte(struct unlzma_ctx *ctx, u8 byte)
236 +{
237 + if (unlikely(ctx->avail_out <= 0)) {
238 + unlzma_request_buffer(ctx, &ctx->avail_out);
239 + }
240 +
241 + if (!ctx->avail_out)
242 + return;
243 +
244 + ctx->previous_byte = byte;
245 + *(ctx->next_out++) = byte;
246 + ctx->avail_out--;
247 + if (ctx->avail_out == 0)
248 + ctx->buf_full = 1;
249 + ctx->pos++;
250 +}
251 +
252 +
253 +static inline void
254 +copy_byte(struct unlzma_ctx *ctx, u32 offs)
255 +{
256 + write_byte(ctx, peek_old_byte(ctx, offs));
257 +}
258 +
259 +static void
260 +copy_bytes(struct unlzma_ctx *ctx, u32 rep0, int len)
261 +{
262 + do {
263 + copy_byte(ctx, rep0);
264 + len--;
265 + if (unlzma_should_stop(ctx))
266 + break;
267 + } while (len != 0);
268 +}
269 +
270 +static void
271 +process_bit0(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob,
272 + int lc, u32 literal_pos_mask)
273 +{
274 + int mi = 1;
275 + rc_update_bit_0(ctx, prob);
276 + prob = (p + LZMA_LITERAL +
277 + (LZMA_LIT_SIZE
278 + * (((ctx->pos & literal_pos_mask) << lc)
279 + + (ctx->previous_byte >> (8 - lc))))
280 + );
281 +
282 + if (ctx->state >= LZMA_NUM_LIT_STATES) {
283 + int match_byte = peek_old_byte(ctx, ctx->rep0);
284 + do {
285 + u16 bit;
286 + u16 *prob_lit;
287 +
288 + match_byte <<= 1;
289 + bit = match_byte & 0x100;
290 + prob_lit = prob + 0x100 + bit + mi;
291 + if (rc_get_bit(ctx, prob_lit, &mi) != !!bit)
292 + break;
293 + } while (mi < 0x100);
294 + }
295 + while (mi < 0x100) {
296 + u16 *prob_lit = prob + mi;
297 + rc_get_bit(ctx, prob_lit, &mi);
298 + }
299 + write_byte(ctx, mi);
300 + if (ctx->state < 4)
301 + ctx->state = 0;
302 + else if (ctx->state < 10)
303 + ctx->state -= 3;
304 + else
305 + ctx->state -= 6;
306 +}
307 +
308 +static void
309 +process_bit1(struct unlzma_ctx *ctx, u16 *p, int pos_state, u16 *prob)
310 +{
311 + int offset;
312 + u16 *prob_len;
313 + int num_bits;
314 + int len;
315 +
316 + rc_update_bit_1(ctx, prob);
317 + prob = p + LZMA_IS_REP + ctx->state;
318 + if (rc_is_bit_0(ctx, prob)) {
319 + rc_update_bit_0(ctx, prob);
320 + ctx->rep3 = ctx->rep2;
321 + ctx->rep2 = ctx->rep1;
322 + ctx->rep1 = ctx->rep0;
323 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ? 0 : 3;
324 + prob = p + LZMA_LEN_CODER;
325 + } else {
326 + rc_update_bit_1(ctx, prob);
327 + prob = p + LZMA_IS_REP_G0 + ctx->state;
328 + if (rc_is_bit_0(ctx, prob)) {
329 + rc_update_bit_0(ctx, prob);
330 + prob = (p + LZMA_IS_REP_0_LONG
331 + + (ctx->state <<
332 + LZMA_NUM_POS_BITS_MAX) +
333 + pos_state);
334 + if (rc_is_bit_0(ctx, prob)) {
335 + rc_update_bit_0(ctx, prob);
336 +
337 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ?
338 + 9 : 11;
339 + copy_byte(ctx, ctx->rep0);
340 + return;
341 + } else {
342 + rc_update_bit_1(ctx, prob);
343 + }
344 + } else {
345 + u32 distance;
346 +
347 + rc_update_bit_1(ctx, prob);
348 + prob = p + LZMA_IS_REP_G1 + ctx->state;
349 + if (rc_is_bit_0(ctx, prob)) {
350 + rc_update_bit_0(ctx, prob);
351 + distance = ctx->rep1;
352 + } else {
353 + rc_update_bit_1(ctx, prob);
354 + prob = p + LZMA_IS_REP_G2 + ctx->state;
355 + if (rc_is_bit_0(ctx, prob)) {
356 + rc_update_bit_0(ctx, prob);
357 + distance = ctx->rep2;
358 + } else {
359 + rc_update_bit_1(ctx, prob);
360 + distance = ctx->rep3;
361 + ctx->rep3 = ctx->rep2;
362 + }
363 + ctx->rep2 = ctx->rep1;
364 + }
365 + ctx->rep1 = ctx->rep0;
366 + ctx->rep0 = distance;
367 + }
368 + ctx->state = ctx->state < LZMA_NUM_LIT_STATES ? 8 : 11;
369 + prob = p + LZMA_REP_LEN_CODER;
370 + }
371 +
372 + prob_len = prob + LZMA_LEN_CHOICE;
373 + if (rc_is_bit_0(ctx, prob_len)) {
374 + rc_update_bit_0(ctx, prob_len);
375 + prob_len = (prob + LZMA_LEN_LOW
376 + + (pos_state <<
377 + LZMA_LEN_NUM_LOW_BITS));
378 + offset = 0;
379 + num_bits = LZMA_LEN_NUM_LOW_BITS;
380 + } else {
381 + rc_update_bit_1(ctx, prob_len);
382 + prob_len = prob + LZMA_LEN_CHOICE_2;
383 + if (rc_is_bit_0(ctx, prob_len)) {
384 + rc_update_bit_0(ctx, prob_len);
385 + prob_len = (prob + LZMA_LEN_MID
386 + + (pos_state <<
387 + LZMA_LEN_NUM_MID_BITS));
388 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
389 + num_bits = LZMA_LEN_NUM_MID_BITS;
390 + } else {
391 + rc_update_bit_1(ctx, prob_len);
392 + prob_len = prob + LZMA_LEN_HIGH;
393 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
394 + + (1 << LZMA_LEN_NUM_MID_BITS));
395 + num_bits = LZMA_LEN_NUM_HIGH_BITS;
396 + }
397 + }
398 +
399 + rc_bit_tree_decode(ctx, prob_len, num_bits, &len);
400 + len += offset;
401 +
402 + if (ctx->state < 4) {
403 + int pos_slot;
404 +
405 + ctx->state += LZMA_NUM_LIT_STATES;
406 + prob =
407 + p + LZMA_POS_SLOT +
408 + ((len <
409 + LZMA_NUM_LEN_TO_POS_STATES ? len :
410 + LZMA_NUM_LEN_TO_POS_STATES - 1)
411 + << LZMA_NUM_POS_SLOT_BITS);
412 + rc_bit_tree_decode(ctx, prob,
413 + LZMA_NUM_POS_SLOT_BITS,
414 + &pos_slot);
415 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
416 + int i, mi;
417 + num_bits = (pos_slot >> 1) - 1;
418 + ctx->rep0 = 2 | (pos_slot & 1);
419 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
420 + ctx->rep0 <<= num_bits;
421 + prob = p + LZMA_SPEC_POS +
422 + ctx->rep0 - pos_slot - 1;
423 + } else {
424 + num_bits -= LZMA_NUM_ALIGN_BITS;
425 + while (num_bits--)
426 + ctx->rep0 = (ctx->rep0 << 1) |
427 + rc_direct_bit(ctx);
428 + prob = p + LZMA_ALIGN;
429 + ctx->rep0 <<= LZMA_NUM_ALIGN_BITS;
430 + num_bits = LZMA_NUM_ALIGN_BITS;
431 + }
432 + i = 1;
433 + mi = 1;
434 + while (num_bits--) {
435 + if (rc_get_bit(ctx, prob + mi, &mi))
436 + ctx->rep0 |= i;
437 + i <<= 1;
438 + }
439 + } else
440 + ctx->rep0 = pos_slot;
441 + if (++(ctx->rep0) == 0)
442 + return;
443 + }
444 +
445 + len += LZMA_MATCH_MIN_LEN;
446 +
447 + copy_bytes(ctx, ctx->rep0, len);
448 +}
449 +
450 +
451 +static int
452 +do_unlzma(struct unlzma_ctx *ctx)
453 +{
454 + u8 hdr_buf[sizeof(struct lzma_header)];
455 + struct lzma_header *header = (struct lzma_header *)hdr_buf;
456 + u32 pos_state_mask;
457 + u32 literal_pos_mask;
458 + int lc, pb, lp;
459 + int num_probs;
460 + int i, mi;
461 + u16 *p;
462 +
463 + for (i = 0; i < sizeof(struct lzma_header); i++) {
464 + hdr_buf[i] = rc_read(ctx);
465 + }
466 +
467 + ctx->n_buffers = 0;
468 + ctx->pos = 0;
469 + get_buffer(ctx);
470 + ctx->active = true;
471 + ctx->state = 0;
472 + ctx->rep0 = ctx->rep1 = ctx->rep2 = ctx->rep3 = 1;
473 +
474 + ctx->previous_byte = 0;
475 + ctx->code = 0;
476 + ctx->range = 0xFFFFFFFF;
477 +
478 + ctx->dict_size = le32_to_cpu(header->dict_size);
479 +
480 + if (header->pos >= (9 * 5 * 5))
481 + return -1;
482 +
483 + mi = 0;
484 + lc = header->pos;
485 + while (lc >= 9) {
486 + mi++;
487 + lc -= 9;
488 + }
489 + pb = 0;
490 + lp = mi;
491 + while (lp >= 5) {
492 + pb++;
493 + lp -= 5;
494 + }
495 + pos_state_mask = (1 << pb) - 1;
496 + literal_pos_mask = (1 << lp) - 1;
497 +
498 + if (ctx->dict_size == 0)
499 + ctx->dict_size = 1;
500 +
501 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
502 + if (ctx->workspace_size < num_probs * sizeof(*p)) {
503 + if (ctx->workspace)
504 + vfree(ctx->workspace);
505 + ctx->workspace_size = num_probs * sizeof(*p);
506 + ctx->workspace = vmalloc(ctx->workspace_size);
507 + }
508 + p = (u16 *) ctx->workspace;
509 + if (!p)
510 + return -1;
511 +
512 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
513 + for (i = 0; i < num_probs; i++)
514 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
515 +
516 + for (i = 0; i < 5; i++)
517 + rc_get_code(ctx);
518 +
519 + while (1) {
520 + int pos_state = ctx->pos & pos_state_mask;
521 + u16 *prob = p + LZMA_IS_MATCH +
522 + (ctx->state << LZMA_NUM_POS_BITS_MAX) + pos_state;
523 + if (rc_is_bit_0(ctx, prob))
524 + process_bit0(ctx, p, pos_state, prob,
525 + lc, literal_pos_mask);
526 + else {
527 + process_bit1(ctx, p, pos_state, prob);
528 + if (ctx->rep0 == 0)
529 + break;
530 + }
531 + if (unlzma_should_stop(ctx))
532 + break;
533 + }
534 + if (likely(!unlzma_should_stop(ctx)))
535 + rc_normalize(ctx);
536 +
537 + return ctx->pos;
538 +}
539 +
540 +
541 +static void
542 +unlzma_reset_buf(struct unlzma_ctx *ctx)
543 +{
544 + ctx->avail_in = 0;
545 + ctx->next_in = NULL;
546 + ctx->avail_out = 0;
547 + ctx->next_out = NULL;
548 +}
549 +
550 +static int
551 +unlzma_thread(void *data)
552 +{
553 + struct unlzma_ctx *ctx = data;
554 +
555 + mutex_lock(&ctx->mutex);
556 + do {
557 + if (do_unlzma(ctx) < 0)
558 + ctx->pos = 0;
559 + unlzma_reset_buf(ctx);
560 + ctx->cancel = false;
561 + ctx->active = false;
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 + if (ctx->buffers)
602 + kfree(ctx->buffers);
603 + ctx->buffers_max = 0;
604 + ctx->buffers = NULL;
605 + }
606 +}
607 +
608 +static int
609 +unlzma_decompress_setup(struct crypto_pcomp *tfm, void *p, unsigned int len)
610 +{
611 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
612 + struct nlattr *tb[UNLZMA_DECOMP_MAX + 1];
613 + int ret = 0;
614 +
615 + if (ctx->thread)
616 + return -EINVAL;
617 +
618 + if (!p)
619 + return -EINVAL;
620 +
621 + ret = nla_parse(tb, UNLZMA_DECOMP_MAX, p, len, NULL);
622 + if (!tb[UNLZMA_DECOMP_OUT_BUFFERS])
623 + return -EINVAL;
624 +
625 + if (ctx->buffers_max && (ctx->buffers_max <
626 + nla_get_u32(tb[UNLZMA_DECOMP_OUT_BUFFERS]))) {
627 + kfree(ctx->buffers);
628 + ctx->buffers_max = 0;
629 + ctx->buffers = NULL;
630 + }
631 + if (!ctx->buffers) {
632 + ctx->buffers_max = nla_get_u32(tb[UNLZMA_DECOMP_OUT_BUFFERS]);
633 + ctx->buffers = kzalloc(sizeof(struct unlzma_buffer) * ctx->buffers_max, GFP_KERNEL);
634 + }
635 + if (!ctx->buffers)
636 + return -ENOMEM;
637 +
638 + mutex_init(&ctx->mutex);
639 + init_waitqueue_head(&ctx->next_req);
640 + ctx->thread = kthread_run(unlzma_thread, ctx, "unlzma/%d", instance++);
641 + if (IS_ERR(ctx->thread)) {
642 + ret = PTR_ERR(ctx->thread);
643 + ctx->thread = NULL;
644 + }
645 +
646 + return ret;
647 +}
648 +
649 +static int
650 +unlzma_decompress_init(struct crypto_pcomp *tfm)
651 +{
652 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
653 +
654 + ctx->pos = 0;
655 + return 0;
656 +}
657 +
658 +static void
659 +unlzma_wait_complete(struct unlzma_ctx *ctx, bool finish)
660 +{
661 + do {
662 + mutex_unlock(&ctx->mutex);
663 + wake_up(&ctx->next_req);
664 + schedule();
665 + mutex_lock(&ctx->mutex);
666 + } while (ctx->active && (ctx->avail_in > 0) && (ctx->avail_out > 0));
667 +}
668 +
669 +static int
670 +unlzma_decompress_update(struct crypto_pcomp *tfm, struct comp_request *req)
671 +{
672 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
673 + size_t pos = 0;
674 +
675 + mutex_lock(&ctx->mutex);
676 + if (!ctx->active && !req->avail_in)
677 + goto out;
678 +
679 + pos = ctx->pos;
680 + ctx->next_in = req->next_in;
681 + ctx->avail_in = req->avail_in;
682 + ctx->next_out = req->next_out;
683 + ctx->avail_out = req->avail_out;
684 +
685 + unlzma_wait_complete(ctx, false);
686 +
687 + req->next_in = ctx->next_in;
688 + req->avail_in = ctx->avail_in;
689 + req->next_out = ctx->next_out;
690 + req->avail_out = ctx->avail_out;
691 + ctx->next_in = 0;
692 + ctx->avail_in = 0;
693 + pos = ctx->pos - pos;
694 +
695 +out:
696 + mutex_unlock(&ctx->mutex);
697 + return pos;
698 +}
699 +
700 +static int
701 +unlzma_decompress_final(struct crypto_pcomp *tfm, struct comp_request *req)
702 +{
703 + struct unlzma_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
704 + int ret = 0;
705 +
706 + /* cancel pending operation */
707 + mutex_lock(&ctx->mutex);
708 + if (ctx->active) {
709 + // ret = -EINVAL;
710 + unlzma_cancel(ctx);
711 + }
712 + ctx->pos = 0;
713 + mutex_unlock(&ctx->mutex);
714 + return ret;
715 +}
716 +
717 +
718 +static struct pcomp_alg unlzma_alg = {
719 + .decompress_setup = unlzma_decompress_setup,
720 + .decompress_init = unlzma_decompress_init,
721 + .decompress_update = unlzma_decompress_update,
722 + .decompress_final = unlzma_decompress_final,
723 +
724 + .base = {
725 + .cra_name = "lzma",
726 + .cra_flags = CRYPTO_ALG_TYPE_PCOMPRESS,
727 + .cra_ctxsize = sizeof(struct unlzma_ctx),
728 + .cra_module = THIS_MODULE,
729 + .cra_init = unlzma_init,
730 + .cra_exit = unlzma_exit,
731 + }
732 +};
733 +
734 +static int __init
735 +unlzma_mod_init(void)
736 +{
737 + return crypto_register_pcomp(&unlzma_alg);
738 +}
739 +
740 +static void __exit
741 +unlzma_mod_exit(void)
742 +{
743 + crypto_unregister_pcomp(&unlzma_alg);
744 +}
745 +
746 +module_init(unlzma_mod_init);
747 +module_exit(unlzma_mod_exit);
748 +
749 +MODULE_LICENSE("GPL");
750 +MODULE_DESCRIPTION("LZMA Decompression Algorithm");
751 +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
752 --- a/crypto/Kconfig
753 +++ b/crypto/Kconfig
754 @@ -758,6 +758,12 @@ config CRYPTO_ZLIB
755 help
756 This is the zlib algorithm.
757
758 +config CRYPTO_UNLZMA
759 + tristate "LZMA decompression"
760 + select CRYPTO_PCOMP
761 + help
762 + This is the lzma decompression module.
763 +
764 config CRYPTO_LZO
765 tristate "LZO compression algorithm"
766 select CRYPTO_ALGAPI
767 --- a/crypto/Makefile
768 +++ b/crypto/Makefile
769 @@ -75,6 +75,7 @@ obj-$(CONFIG_CRYPTO_SEED) += seed.o
770 obj-$(CONFIG_CRYPTO_SALSA20) += salsa20_generic.o
771 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
772 obj-$(CONFIG_CRYPTO_ZLIB) += zlib.o
773 +obj-$(CONFIG_CRYPTO_UNLZMA) += unlzma.o
774 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
775 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
776 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o
777 --- /dev/null
778 +++ b/crypto/unlzma.h
779 @@ -0,0 +1,80 @@
780 +/* LZMA uncompresion module for pcomp
781 + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
782 + *
783 + * Based on:
784 + * Initial Linux kernel adaptation
785 + * Copyright (C) 2006 Alain < alain@knaff.lu >
786 + *
787 + * Based on small lzma deflate implementation/Small range coder
788 + * implementation for lzma.
789 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
790 + *
791 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
792 + * Copyright (C) 1999-2005 Igor Pavlov
793 + *
794 + * This program is free software; you can redistribute it and/or modify it
795 + * under the terms of the GNU General Public License version 2 as published
796 + * by the Free Software Foundation.
797 + */
798 +#ifndef __UNLZMA_H
799 +#define __UNLZMA_H
800 +
801 +struct lzma_header {
802 + __u8 pos;
803 + __le32 dict_size;
804 +} __attribute__ ((packed)) ;
805 +
806 +
807 +#define RC_TOP_BITS 24
808 +#define RC_MOVE_BITS 5
809 +#define RC_MODEL_TOTAL_BITS 11
810 +
811 +#define LZMA_BASE_SIZE 1846
812 +#define LZMA_LIT_SIZE 768
813 +
814 +#define LZMA_NUM_POS_BITS_MAX 4
815 +
816 +#define LZMA_LEN_NUM_LOW_BITS 3
817 +#define LZMA_LEN_NUM_MID_BITS 3
818 +#define LZMA_LEN_NUM_HIGH_BITS 8
819 +
820 +#define LZMA_LEN_CHOICE 0
821 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
822 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
823 +#define LZMA_LEN_MID (LZMA_LEN_LOW \
824 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
825 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \
826 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
827 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
828 +
829 +#define LZMA_NUM_STATES 12
830 +#define LZMA_NUM_LIT_STATES 7
831 +
832 +#define LZMA_START_POS_MODEL_INDEX 4
833 +#define LZMA_END_POS_MODEL_INDEX 14
834 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
835 +
836 +#define LZMA_NUM_POS_SLOT_BITS 6
837 +#define LZMA_NUM_LEN_TO_POS_STATES 4
838 +
839 +#define LZMA_NUM_ALIGN_BITS 4
840 +
841 +#define LZMA_MATCH_MIN_LEN 2
842 +
843 +#define LZMA_IS_MATCH 0
844 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
845 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
846 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
847 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
848 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
849 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
850 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
851 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \
852 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
853 +#define LZMA_ALIGN (LZMA_SPEC_POS \
854 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
855 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
856 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
857 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
858 +
859 +#endif
860 --- a/include/crypto/compress.h
861 +++ b/include/crypto/compress.h
862 @@ -49,6 +49,12 @@ enum zlib_decomp_params {
863
864 #define ZLIB_DECOMP_MAX (__ZLIB_DECOMP_MAX - 1)
865
866 +enum unlzma_decomp_params {
867 + UNLZMA_DECOMP_OUT_BUFFERS = 1, /* naximum number of output buffers */
868 + __UNLZMA_DECOMP_MAX,
869 +};
870 +#define UNLZMA_DECOMP_MAX (__UNLZMA_DECOMP_MAX - 1)
871 +
872
873 struct crypto_pcomp {
874 struct crypto_tfm base;
This page took 0.105267 seconds and 5 git commands to generate.