Back to home page

Redis cross reference

 
 

    


0001 /*
0002  * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>
0003  * 
0004  * Redistribution and use in source and binary forms, with or without modifica-
0005  * tion, are permitted provided that the following conditions are met:
0006  * 
0007  *   1.  Redistributions of source code must retain the above copyright notice,
0008  *       this list of conditions and the following disclaimer.
0009  * 
0010  *   2.  Redistributions in binary form must reproduce the above copyright
0011  *       notice, this list of conditions and the following disclaimer in the
0012  *       documentation and/or other materials provided with the distribution.
0013  * 
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0015  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
0016  * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
0017  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
0018  * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0019  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
0020  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
0021  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
0022  * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
0023  * OF THE POSSIBILITY OF SUCH DAMAGE.
0024  *
0025  * Alternatively, the contents of this file may be used under the terms of
0026  * the GNU General Public License ("GPL") version 2 or any later version,
0027  * in which case the provisions of the GPL are applicable instead of
0028  * the above. If you wish to allow the use of your version of this file
0029  * only under the terms of the GPL and not to allow others to use your
0030  * version of this file under the BSD license, indicate your decision
0031  * by deleting the provisions above and replace them with the notice
0032  * and other provisions required by the GPL. If you do not delete the
0033  * provisions above, a recipient may use your version of this file under
0034  * either the BSD or the GPL.
0035  */
0036 
0037 #include "lzfP.h"
0038 
0039 #if AVOID_ERRNO
0040 # define SET_ERRNO(n)
0041 #else
0042 # include <errno.h>
0043 # define SET_ERRNO(n) errno = (n)
0044 #endif
0045 
0046 /*
0047 #if (__i386 || __amd64) && __GNUC__ >= 3
0048 # define lzf_movsb(dst, src, len)                \
0049    asm ("rep movsb"                              \
0050         : "=D" (dst), "=S" (src), "=c" (len)     \
0051         :  "0" (dst),  "1" (src),  "2" (len));
0052 #endif
0053 */
0054 
0055 unsigned int 
0056 lzf_decompress (const void *const in_data,  unsigned int in_len,
0057                 void             *out_data, unsigned int out_len)
0058 {
0059   u8 const *ip = (const u8 *)in_data;
0060   u8       *op = (u8 *)out_data;
0061   u8 const *const in_end  = ip + in_len;
0062   u8       *const out_end = op + out_len;
0063 
0064   do
0065     {
0066       unsigned int ctrl = *ip++;
0067 
0068       if (ctrl < (1 << 5)) /* literal run */
0069         {
0070           ctrl++;
0071 
0072           if (op + ctrl > out_end)
0073             {
0074               SET_ERRNO (E2BIG);
0075               return 0;
0076             }
0077 
0078 #if CHECK_INPUT
0079           if (ip + ctrl > in_end)
0080             {
0081               SET_ERRNO (EINVAL);
0082               return 0;
0083             }
0084 #endif
0085 
0086 #ifdef lzf_movsb
0087           lzf_movsb (op, ip, ctrl);
0088 #else
0089           do
0090             *op++ = *ip++;
0091           while (--ctrl);
0092 #endif
0093         }
0094       else /* back reference */
0095         {
0096           unsigned int len = ctrl >> 5;
0097 
0098           u8 *ref = op - ((ctrl & 0x1f) << 8) - 1;
0099 
0100 #if CHECK_INPUT
0101           if (ip >= in_end)
0102             {
0103               SET_ERRNO (EINVAL);
0104               return 0;
0105             }
0106 #endif
0107           if (len == 7)
0108             {
0109               len += *ip++;
0110 #if CHECK_INPUT
0111               if (ip >= in_end)
0112                 {
0113                   SET_ERRNO (EINVAL);
0114                   return 0;
0115                 }
0116 #endif
0117             }
0118 
0119           ref -= *ip++;
0120 
0121           if (op + len + 2 > out_end)
0122             {
0123               SET_ERRNO (E2BIG);
0124               return 0;
0125             }
0126 
0127           if (ref < (u8 *)out_data)
0128             {
0129               SET_ERRNO (EINVAL);
0130               return 0;
0131             }
0132 
0133 #ifdef lzf_movsb
0134           len += 2;
0135           lzf_movsb (op, ref, len);
0136 #else
0137           *op++ = *ref++;
0138           *op++ = *ref++;
0139 
0140           do
0141             *op++ = *ref++;
0142           while (--len);
0143 #endif
0144         }
0145     }
0146   while (ip < in_end);
0147 
0148   return op - (u8 *)out_data;
0149 }
0150