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