Decompressor.cpp (4129B)
1 /* GRAPHITE2 LICENSING 2 3 Copyright 2015, SIL International 4 All rights reserved. 5 6 This library is free software; you can redistribute it and/or modify 7 it under the terms of the GNU Lesser General Public License as published 8 by the Free Software Foundation; either version 2.1 of License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should also have received a copy of the GNU Lesser General Public 17 License along with this library in the file named "LICENSE". 18 If not, write to the Free Software Foundation, 51 Franklin Street, 19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 20 internet at http://www.fsf.org/licenses/lgpl.html. 21 22 Alternatively, the contents of this file may be used under the terms of the 23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public 24 License, as published by the Free Software Foundation, either version 2 25 of the License or (at your option) any later version. 26 */ 27 #include <cassert> 28 29 #include "inc/Decompressor.h" 30 #include "inc/Compression.h" 31 32 using namespace lz4; 33 34 namespace { 35 36 inline 37 u32 read_literal(u8 const * &s, u8 const * const e, u32 l) { 38 if (l == 15 && s != e) 39 { 40 u8 b = 0; 41 do { l += b = *s++; } while(b==0xff && s != e); 42 } 43 return l; 44 } 45 46 bool read_sequence(u8 const * &src, u8 const * const end, u8 const * &literal, 47 u32 & literal_len, u32 & match_len, u32 & match_dist) 48 { 49 u8 const token = *src++; 50 51 literal_len = read_literal(src, end, token >> 4); 52 literal = src; 53 src += literal_len; 54 55 // Normal exit for end of stream, wrap arround check and parital match check. 56 if (src > end - sizeof(u16) || src < literal) 57 return false; 58 59 match_dist = *src++; 60 match_dist |= *src++ << 8; 61 match_len = read_literal(src, end, token & 0xf) + MINMATCH; 62 63 // Malformed stream check. 64 return src <= end-MINCODA; 65 } 66 67 } 68 69 int lz4::decompress(void const *in, size_t in_size, void *out, size_t out_size) 70 { 71 if (out_size <= in_size || in_size < MINSRCSIZE) 72 return -1; 73 74 u8 const * src = static_cast<u8 const *>(in), 75 * literal = 0, 76 * const src_end = src + in_size; 77 78 u8 * dst = static_cast<u8*>(out), 79 * const dst_end = dst + out_size; 80 81 // Check the in and out size hasn't wrapped around. 82 if (src >= src_end || dst >= dst_end) 83 return -1; 84 85 u32 literal_len = 0, 86 match_len = 0, 87 match_dist = 0; 88 89 while (read_sequence(src, src_end, literal, literal_len, match_len, 90 match_dist)) 91 { 92 if (literal_len != 0) 93 { 94 // Copy in literal. At this point the a minimal literal + minminal 95 // match plus the coda (1 + 2 + 5) must be 8 bytes or more allowing 96 // us to remain within the src buffer for an overrun_copy on 97 // machines upto 64 bits. 98 if (align(literal_len) > out_size) 99 return -1; 100 dst = overrun_copy(dst, literal, literal_len); 101 out_size -= literal_len; 102 } 103 104 // Copy, possibly repeating, match from earlier in the 105 // decoded output. 106 u8 const * const pcpy = dst - match_dist; 107 if (pcpy < static_cast<u8*>(out) 108 || match_len > unsigned(out_size - LASTLITERALS) 109 // Wrap around checks: 110 || out_size < LASTLITERALS || pcpy >= dst) 111 return -1; 112 if (dst > pcpy+sizeof(unsigned long) 113 && align(match_len) <= out_size) 114 dst = overrun_copy(dst, pcpy, match_len); 115 else 116 dst = safe_copy(dst, pcpy, match_len); 117 out_size -= match_len; 118 } 119 120 if (literal > src_end - literal_len || literal_len > out_size) 121 return -1; 122 dst = fast_copy(dst, literal, literal_len); 123 124 return int(dst - (u8*)out); 125 }