Path: blob/master/thirdparty/graphite/src/Decompressor.cpp
9902 views
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later1// Copyright 2015, SIL International, All rights reserved.23#include <cassert>45#include "inc/Decompressor.h"6#include "inc/Compression.h"78using namespace lz4;910namespace {1112inline13u32 read_literal(u8 const * &s, u8 const * const e, u32 l) {14if (l == 15 && s != e)15{16u8 b = 0;17do { l += b = *s++; } while(b==0xff && s != e);18}19return l;20}2122bool read_sequence(u8 const * &src, u8 const * const end, u8 const * &literal,23u32 & literal_len, u32 & match_len, u32 & match_dist)24{25u8 const token = *src++;2627literal_len = read_literal(src, end, token >> 4);28literal = src;29src += literal_len;3031// Normal exit for end of stream, wrap arround check and parital match check.32if (src > end - sizeof(u16) || src < literal)33return false;3435match_dist = *src++;36match_dist |= *src++ << 8;37match_len = read_literal(src, end, token & 0xf) + MINMATCH;3839// Malformed stream check.40return src <= end-MINCODA;41}4243}4445int lz4::decompress(void const *in, size_t in_size, void *out, size_t out_size)46{47if (out_size <= in_size || in_size < MINSRCSIZE)48return -1;4950u8 const * src = static_cast<u8 const *>(in),51* literal = 0,52* const src_end = src + in_size;5354u8 * dst = static_cast<u8*>(out),55* const dst_end = dst + out_size;5657// Check the in and out size hasn't wrapped around.58if (src >= src_end || dst >= dst_end)59return -1;6061u32 literal_len = 0,62match_len = 0,63match_dist = 0;6465while (read_sequence(src, src_end, literal, literal_len, match_len,66match_dist))67{68if (literal_len != 0)69{70// Copy in literal. At this point the a minimal literal + minminal71// match plus the coda (1 + 2 + 5) must be 8 bytes or more allowing72// us to remain within the src buffer for an overrun_copy on73// machines upto 64 bits.74if (align(literal_len) > out_size)75return -1;76dst = overrun_copy(dst, literal, literal_len);77out_size -= literal_len;78}7980// Copy, possibly repeating, match from earlier in the81// decoded output.82u8 const * const pcpy = dst - match_dist;83if (pcpy < static_cast<u8*>(out)84|| match_len > unsigned(out_size - LASTLITERALS)85// Wrap around checks:86|| out_size < LASTLITERALS || pcpy >= dst)87return -1;88if (dst > pcpy+sizeof(unsigned long)89&& align(match_len) <= out_size)90dst = overrun_copy(dst, pcpy, match_len);91else92dst = safe_copy(dst, pcpy, match_len);93out_size -= match_len;94}9596if (literal > src_end - literal_len || literal_len > out_size)97return -1;98dst = fast_copy(dst, literal, literal_len);99100return int(dst - (u8*)out);101}102103104