linux/lib/lzo/lzo1x_decompress.c
<<
>>
Prefs
   1/*
   2 *  LZO1X Decompressor from MiniLZO
   3 *
   4 *  Copyright (C) 1996-2005 Markus F.X.J. Oberhumer <markus@oberhumer.com>
   5 *
   6 *  The full LZO package can be found at:
   7 *  http://www.oberhumer.com/opensource/lzo/
   8 *
   9 *  Changed for kernel use by:
  10 *  Nitin Gupta <nitingupta910@gmail.com>
  11 *  Richard Purdie <rpurdie@openedhand.com>
  12 */
  13
  14#ifndef STATIC
  15#include <linux/module.h>
  16#include <linux/kernel.h>
  17#endif
  18
  19#include <asm/unaligned.h>
  20#include <linux/lzo.h>
  21#include "lzodefs.h"
  22
  23#define HAVE_IP(x, ip_end, ip) ((size_t)(ip_end - ip) < (x))
  24#define HAVE_OP(x, op_end, op) ((size_t)(op_end - op) < (x))
  25#define HAVE_LB(m_pos, out, op) (m_pos < out || m_pos >= op)
  26
  27#define COPY4(dst, src) \
  28                put_unaligned(get_unaligned((const u32 *)(src)), (u32 *)(dst))
  29
  30int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
  31                        unsigned char *out, size_t *out_len)
  32{
  33        const unsigned char * const ip_end = in + in_len;
  34        unsigned char * const op_end = out + *out_len;
  35        const unsigned char *ip = in, *m_pos;
  36        unsigned char *op = out;
  37        size_t t;
  38
  39        *out_len = 0;
  40
  41        if (*ip > 17) {
  42                t = *ip++ - 17;
  43                if (t < 4)
  44                        goto match_next;
  45                if (HAVE_OP(t, op_end, op))
  46                        goto output_overrun;
  47                if (HAVE_IP(t + 1, ip_end, ip))
  48                        goto input_overrun;
  49                do {
  50                        *op++ = *ip++;
  51                } while (--t > 0);
  52                goto first_literal_run;
  53        }
  54
  55        while ((ip < ip_end)) {
  56                t = *ip++;
  57                if (t >= 16)
  58                        goto match;
  59                if (t == 0) {
  60                        if (HAVE_IP(1, ip_end, ip))
  61                                goto input_overrun;
  62                        while (*ip == 0) {
  63                                t += 255;
  64                                ip++;
  65                                if (HAVE_IP(1, ip_end, ip))
  66                                        goto input_overrun;
  67                        }
  68                        t += 15 + *ip++;
  69                }
  70                if (HAVE_OP(t + 3, op_end, op))
  71                        goto output_overrun;
  72                if (HAVE_IP(t + 4, ip_end, ip))
  73                        goto input_overrun;
  74
  75                COPY4(op, ip);
  76                op += 4;
  77                ip += 4;
  78                if (--t > 0) {
  79                        if (t >= 4) {
  80                                do {
  81                                        COPY4(op, ip);
  82                                        op += 4;
  83                                        ip += 4;
  84                                        t -= 4;
  85                                } while (t >= 4);
  86                                if (t > 0) {
  87                                        do {
  88                                                *op++ = *ip++;
  89                                        } while (--t > 0);
  90                                }
  91                        } else {
  92                                do {
  93                                        *op++ = *ip++;
  94                                } while (--t > 0);
  95                        }
  96                }
  97
  98first_literal_run:
  99                t = *ip++;
 100                if (t >= 16)
 101                        goto match;
 102                m_pos = op - (1 + M2_MAX_OFFSET);
 103                m_pos -= t >> 2;
 104                m_pos -= *ip++ << 2;
 105
 106                if (HAVE_LB(m_pos, out, op))
 107                        goto lookbehind_overrun;
 108
 109                if (HAVE_OP(3, op_end, op))
 110                        goto output_overrun;
 111                *op++ = *m_pos++;
 112                *op++ = *m_pos++;
 113                *op++ = *m_pos;
 114
 115                goto match_done;
 116
 117                do {
 118match:
 119                        if (t >= 64) {
 120                                m_pos = op - 1;
 121                                m_pos -= (t >> 2) & 7;
 122                                m_pos -= *ip++ << 3;
 123                                t = (t >> 5) - 1;
 124                                if (HAVE_LB(m_pos, out, op))
 125                                        goto lookbehind_overrun;
 126                                if (HAVE_OP(t + 3 - 1, op_end, op))
 127                                        goto output_overrun;
 128                                goto copy_match;
 129                        } else if (t >= 32) {
 130                                t &= 31;
 131                                if (t == 0) {
 132                                        if (HAVE_IP(1, ip_end, ip))
 133                                                goto input_overrun;
 134                                        while (*ip == 0) {
 135                                                t += 255;
 136                                                ip++;
 137                                                if (HAVE_IP(1, ip_end, ip))
 138                                                        goto input_overrun;
 139                                        }
 140                                        t += 31 + *ip++;
 141                                }
 142                                m_pos = op - 1;
 143                                m_pos -= get_unaligned_le16(ip) >> 2;
 144                                ip += 2;
 145                        } else if (t >= 16) {
 146                                m_pos = op;
 147                                m_pos -= (t & 8) << 11;
 148
 149                                t &= 7;
 150                                if (t == 0) {
 151                                        if (HAVE_IP(1, ip_end, ip))
 152                                                goto input_overrun;
 153                                        while (*ip == 0) {
 154                                                t += 255;
 155                                                ip++;
 156                                                if (HAVE_IP(1, ip_end, ip))
 157                                                        goto input_overrun;
 158                                        }
 159                                        t += 7 + *ip++;
 160                                }
 161                                m_pos -= get_unaligned_le16(ip) >> 2;
 162                                ip += 2;
 163                                if (m_pos == op)
 164                                        goto eof_found;
 165                                m_pos -= 0x4000;
 166                        } else {
 167                                m_pos = op - 1;
 168                                m_pos -= t >> 2;
 169                                m_pos -= *ip++ << 2;
 170
 171                                if (HAVE_LB(m_pos, out, op))
                if ((((((((((((((((((((((((( lookbehind_overrun;
1  73<1a>                        goto    if (HAVE_OP(op_end, op))
1  74<17>                                        goto output_overrun;
1  75<17>
1  76<1a>                op++ = *m_pos++;
  167                op++ = *m_pos++;
 href="lib/lzo/lzo1x_decompress.c#L113" 7d="L168" class="line" name="L78">1  78<1a>                if (            goto match_done;
1  79<1a>                        if ( href="lib/lzo/lzo1x_decompress.c#L161" id="L80" 1class="line" name="L80">1  80<18>
1  81<1a>                            HAVE_LB(m_pos, out, op))
1  82<1a>                                     lookbehind_overrun;
1  83<1a>                            HAVE_OP(t + 3 - 1, op_end, op))
1  84<1a>                                     output_overrun;
1  85<18>
1  86<1a>                            t >= 16) 2 * 41 + 1, op, m_pos, 1  87<1a>                                    ref="+code=op" 4" class="sref">COPY4(op, m_pos,    78<1a>                                    ref="+code=op" class="sref">op, 1  89<1a>                                    ref="+code=m_pos" class="sref">m_pos -= *;
1  90<1a>                                }
t >;
1  91<1a>                        } elllllllll
1  92<1a>                                do {    }
COPY4(op, m_pos, 1  93<1a>                                        *op, 1  94<1a>                                } wh    *m_pos -= *;
1  95<1a>                        }
t -= 4;
1  96<1a>                }
t >= 4);
1  97<19>                                    t > 0) {
1  98<19>                                        }
1  99<1a>                op++ = *m_pos++;
                if (((((((((((((((((((((((((ile (--t > 0);
                        gotose {
    ref="+code=t" c_match" class="sref">copy_match;
                op++ = *m_pos++;
2 104<2a>                op++ = *m_pos++;
2 105<20>                        }
                if (    op++ = *m_pos++;
2 107<2a>                        gotoooooooooile (--t > 0);
2 108<20>                             href="lib/lzo/lzo1x_decompress.c#L161"2id="L109"2class="line" name="L109"2 109<2a>    ref="+code=eof_h_done" class="sref">match_done;
                        gotoref="+code=t" class="sref">t > href="+code=in" class="sref">ip++ &[-2]p; 8) &a href="lib/lzo/lzo1x_decompress.c#L123"2id="L111"2class="line" name="L111"2 111<2a>                *t == 0) {
                *    ref="+code=eof_h_done" class="sref">match_next;
                HAVE_OP(t, op_end, op))
                gotooooooooooooooooo output_overrun;
                            HAVE_IP(t + 1, ip_end, ip))
                do {oooooooooooooooo input_overrun;

                        if (href="+code=op" class="sref">op++ = *ip++;
                            t > 0) {1                                op++ = *ip++;
                                t > 0) {2 href="lib/lzo/lzo1x_decompress.c#L48" 2id="L123"2class="line" name="L123"2 123<2a>                                op++ = *ip++;
                             href="lib/lzo/lzo1x_decompress.c#L161"2id="L125"2class="line" name="L125"2 125<22>
                            ref="+code=t" class="sref">t > hhref="+code=m_poclass="sref">ip++;
                    ile (ip < ip_end)) {
 href="lib/lzo/lzo1x_decompress.c#L160"2id="L118"2class="line" name="L128"2 128<2a>             href="lib/lzo/lzo1x_decompress.c#L161"2id="L119"2class="line" name="L129"2 129<2a>    href="lib/lzo/lzo1x_decompress.c#L161"2id="L130"2class="line" name="L130"2 130<2a>            href="+code=op" cverlelass="sref">output_rlel > href="+code=in" class="sref">op, out,             returnhref="+code=op" LZO_E_EOF_NOT_FOUNDass="sref">out    href="lib/lzo/lzo1x_decompress.c#L160"2id="L123"2class="line" name="L133"2 133<23>    ref="+code=eof_found" class="sref">eof_found;
            href="+code=op" cverlelass="sref">output_rlel > href="+code=in" class="sref">op, out,             returnhhref="+code=t" cclass="sref">ip <a href="+code=op" nd" class="sref">ip_end)) { ?href="+code=op" LZO_E_OKass="sref">out                    href="+code=t" cclass="sref">ip < ip_end)) { ?href="+code=op" LZO_E_INPUT_NOT_CONSUMEDass="sref">outout    ref="+code=eof_t_overrun" class="sref">input_overrun;
            href="+code=op" cverlelass="sref">output_rlel > href="+code=in" class="sref">op, out,             returnhref="+code=op" LZO_E_INPUT_OVERRUNass="sref">out
    ref="+code=eof_ut_overrun" class="sref">output_overrun;
            href="+code=op" cverlelass="sref">output_rlel > href="+code=in" class="sref">op, out,             returnhref="+code=op" LZO_E_OUTPUT_OVERRUNass="sref">out
    ref="+code=op" behind_overrun" class="sref">lookbehind_overrun;
            href="+code=op" cverlelass="sref">output_rlel > href="+code=in" class="sref">op, out,             returnhref="+code=op" LZO_E_LOOKBEHIND_OVERRUNass="sref">out
    #ifndefhref="+code=op" STATICass="sref">out    ref="+code=op" EXPORT_SYMBOL_GPLass="sref">outmatcx_decompress.c#L_safl)) {
 href="lib/lzo/lzo1x_decompress.c#L160"2id="L151"2class="line" name="L151"2 151<2a>    href="lib/lzo/lzo1x_decompress.c#L160"2id="L142"2class="line" name="L152"2 152<25>    ref="+code=t" cMODULE_LICENSEass="sref">matcMODULE_LICENSE("GPL"    ref="+code=eof_MODULE_DESCRIPTIONass="sref">out"LZO1X Dpress.c#Lor"
    #/a>ifhref="lib/lzo/lzo1x_decompress.c#L115"2id="L126"2class="line" name="L156"2 156<2a>    


The original LXR software by the ref="lib/http://sourceforge.net/s.ojects/lxr">LXR resmunity, xxr." nux.no kovely hosted by ref="lib/http://www.redpill-" ns.o.notcRedpill L ns.o AS, 5.