127b27ec6Sopenharmony_ci/* 227b27ec6Sopenharmony_ci * LZ4 auto-framing library 327b27ec6Sopenharmony_ci * Copyright (C) 2011-2016, Yann Collet. 427b27ec6Sopenharmony_ci * 527b27ec6Sopenharmony_ci * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) 627b27ec6Sopenharmony_ci * 727b27ec6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 827b27ec6Sopenharmony_ci * modification, are permitted provided that the following conditions are 927b27ec6Sopenharmony_ci * met: 1027b27ec6Sopenharmony_ci * 1127b27ec6Sopenharmony_ci * - Redistributions of source code must retain the above copyright 1227b27ec6Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 1327b27ec6Sopenharmony_ci * - Redistributions in binary form must reproduce the above 1427b27ec6Sopenharmony_ci * copyright notice, this list of conditions and the following disclaimer 1527b27ec6Sopenharmony_ci * in the documentation and/or other materials provided with the 1627b27ec6Sopenharmony_ci * distribution. 1727b27ec6Sopenharmony_ci * 1827b27ec6Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1927b27ec6Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2027b27ec6Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2127b27ec6Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2227b27ec6Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2327b27ec6Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2427b27ec6Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2527b27ec6Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2627b27ec6Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2727b27ec6Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2827b27ec6Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2927b27ec6Sopenharmony_ci * 3027b27ec6Sopenharmony_ci * You can contact the author at : 3127b27ec6Sopenharmony_ci * - LZ4 homepage : http://www.lz4.org 3227b27ec6Sopenharmony_ci * - LZ4 source repository : https://github.com/lz4/lz4 3327b27ec6Sopenharmony_ci */ 3427b27ec6Sopenharmony_ci 3527b27ec6Sopenharmony_ci/* LZ4F is a stand-alone API to create LZ4-compressed Frames 3627b27ec6Sopenharmony_ci * in full conformance with specification v1.6.1 . 3727b27ec6Sopenharmony_ci * This library rely upon memory management capabilities (malloc, free) 3827b27ec6Sopenharmony_ci * provided either by <stdlib.h>, 3927b27ec6Sopenharmony_ci * or redirected towards another library of user's choice 4027b27ec6Sopenharmony_ci * (see Memory Routines below). 4127b27ec6Sopenharmony_ci */ 4227b27ec6Sopenharmony_ci 4327b27ec6Sopenharmony_ci 4427b27ec6Sopenharmony_ci/*-************************************ 4527b27ec6Sopenharmony_ci* Compiler Options 4627b27ec6Sopenharmony_ci**************************************/ 4727b27ec6Sopenharmony_ci#ifdef _MSC_VER /* Visual Studio */ 4827b27ec6Sopenharmony_ci# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ 4927b27ec6Sopenharmony_ci#endif 5027b27ec6Sopenharmony_ci 5127b27ec6Sopenharmony_ci 5227b27ec6Sopenharmony_ci/*-************************************ 5327b27ec6Sopenharmony_ci* Tuning parameters 5427b27ec6Sopenharmony_ci**************************************/ 5527b27ec6Sopenharmony_ci/* 5627b27ec6Sopenharmony_ci * LZ4F_HEAPMODE : 5727b27ec6Sopenharmony_ci * Select how default compression functions will allocate memory for their hash table, 5827b27ec6Sopenharmony_ci * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()). 5927b27ec6Sopenharmony_ci */ 6027b27ec6Sopenharmony_ci#ifndef LZ4F_HEAPMODE 6127b27ec6Sopenharmony_ci# define LZ4F_HEAPMODE 0 6227b27ec6Sopenharmony_ci#endif 6327b27ec6Sopenharmony_ci 6427b27ec6Sopenharmony_ci 6527b27ec6Sopenharmony_ci/*-************************************ 6627b27ec6Sopenharmony_ci* Library declarations 6727b27ec6Sopenharmony_ci**************************************/ 6827b27ec6Sopenharmony_ci#define LZ4F_STATIC_LINKING_ONLY 6927b27ec6Sopenharmony_ci#include "lz4frame.h" 7027b27ec6Sopenharmony_ci#define LZ4_STATIC_LINKING_ONLY 7127b27ec6Sopenharmony_ci#include "lz4.h" 7227b27ec6Sopenharmony_ci#define LZ4_HC_STATIC_LINKING_ONLY 7327b27ec6Sopenharmony_ci#include "lz4hc.h" 7427b27ec6Sopenharmony_ci#define XXH_STATIC_LINKING_ONLY 7527b27ec6Sopenharmony_ci#include "xxhash.h" 7627b27ec6Sopenharmony_ci 7727b27ec6Sopenharmony_ci 7827b27ec6Sopenharmony_ci/*-************************************ 7927b27ec6Sopenharmony_ci* Memory routines 8027b27ec6Sopenharmony_ci**************************************/ 8127b27ec6Sopenharmony_ci/* 8227b27ec6Sopenharmony_ci * User may redirect invocations of 8327b27ec6Sopenharmony_ci * malloc(), calloc() and free() 8427b27ec6Sopenharmony_ci * towards another library or solution of their choice 8527b27ec6Sopenharmony_ci * by modifying below section. 8627b27ec6Sopenharmony_ci**/ 8727b27ec6Sopenharmony_ci 8827b27ec6Sopenharmony_ci#include <string.h> /* memset, memcpy, memmove */ 8927b27ec6Sopenharmony_ci#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ 9027b27ec6Sopenharmony_ci# define MEM_INIT(p,v,s) memset((p),(v),(s)) 9127b27ec6Sopenharmony_ci#endif 9227b27ec6Sopenharmony_ci 9327b27ec6Sopenharmony_ci#ifndef LZ4_SRC_INCLUDED /* avoid redefinition when sources are coalesced */ 9427b27ec6Sopenharmony_ci# include <stdlib.h> /* malloc, calloc, free */ 9527b27ec6Sopenharmony_ci# define ALLOC(s) malloc(s) 9627b27ec6Sopenharmony_ci# define ALLOC_AND_ZERO(s) calloc(1,(s)) 9727b27ec6Sopenharmony_ci# define FREEMEM(p) free(p) 9827b27ec6Sopenharmony_ci#endif 9927b27ec6Sopenharmony_ci 10027b27ec6Sopenharmony_cistatic void* LZ4F_calloc(size_t s, LZ4F_CustomMem cmem) 10127b27ec6Sopenharmony_ci{ 10227b27ec6Sopenharmony_ci /* custom calloc defined : use it */ 10327b27ec6Sopenharmony_ci if (cmem.customCalloc != NULL) { 10427b27ec6Sopenharmony_ci return cmem.customCalloc(cmem.opaqueState, s); 10527b27ec6Sopenharmony_ci } 10627b27ec6Sopenharmony_ci /* nothing defined : use default <stdlib.h>'s calloc() */ 10727b27ec6Sopenharmony_ci if (cmem.customAlloc == NULL) { 10827b27ec6Sopenharmony_ci return ALLOC_AND_ZERO(s); 10927b27ec6Sopenharmony_ci } 11027b27ec6Sopenharmony_ci /* only custom alloc defined : use it, and combine it with memset() */ 11127b27ec6Sopenharmony_ci { void* const p = cmem.customAlloc(cmem.opaqueState, s); 11227b27ec6Sopenharmony_ci if (p != NULL) MEM_INIT(p, 0, s); 11327b27ec6Sopenharmony_ci return p; 11427b27ec6Sopenharmony_ci} } 11527b27ec6Sopenharmony_ci 11627b27ec6Sopenharmony_cistatic void* LZ4F_malloc(size_t s, LZ4F_CustomMem cmem) 11727b27ec6Sopenharmony_ci{ 11827b27ec6Sopenharmony_ci /* custom malloc defined : use it */ 11927b27ec6Sopenharmony_ci if (cmem.customAlloc != NULL) { 12027b27ec6Sopenharmony_ci return cmem.customAlloc(cmem.opaqueState, s); 12127b27ec6Sopenharmony_ci } 12227b27ec6Sopenharmony_ci /* nothing defined : use default <stdlib.h>'s malloc() */ 12327b27ec6Sopenharmony_ci return ALLOC(s); 12427b27ec6Sopenharmony_ci} 12527b27ec6Sopenharmony_ci 12627b27ec6Sopenharmony_cistatic void LZ4F_free(void* p, LZ4F_CustomMem cmem) 12727b27ec6Sopenharmony_ci{ 12827b27ec6Sopenharmony_ci /* custom malloc defined : use it */ 12927b27ec6Sopenharmony_ci if (cmem.customFree != NULL) { 13027b27ec6Sopenharmony_ci cmem.customFree(cmem.opaqueState, p); 13127b27ec6Sopenharmony_ci return; 13227b27ec6Sopenharmony_ci } 13327b27ec6Sopenharmony_ci /* nothing defined : use default <stdlib.h>'s free() */ 13427b27ec6Sopenharmony_ci FREEMEM(p); 13527b27ec6Sopenharmony_ci} 13627b27ec6Sopenharmony_ci 13727b27ec6Sopenharmony_ci 13827b27ec6Sopenharmony_ci/*-************************************ 13927b27ec6Sopenharmony_ci* Debug 14027b27ec6Sopenharmony_ci**************************************/ 14127b27ec6Sopenharmony_ci#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=1) 14227b27ec6Sopenharmony_ci# include <assert.h> 14327b27ec6Sopenharmony_ci#else 14427b27ec6Sopenharmony_ci# ifndef assert 14527b27ec6Sopenharmony_ci# define assert(condition) ((void)0) 14627b27ec6Sopenharmony_ci# endif 14727b27ec6Sopenharmony_ci#endif 14827b27ec6Sopenharmony_ci 14927b27ec6Sopenharmony_ci#define LZ4F_STATIC_ASSERT(c) { enum { LZ4F_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */ 15027b27ec6Sopenharmony_ci 15127b27ec6Sopenharmony_ci#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) && !defined(DEBUGLOG) 15227b27ec6Sopenharmony_ci# include <stdio.h> 15327b27ec6Sopenharmony_cistatic int g_debuglog_enable = 1; 15427b27ec6Sopenharmony_ci# define DEBUGLOG(l, ...) { \ 15527b27ec6Sopenharmony_ci if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ 15627b27ec6Sopenharmony_ci fprintf(stderr, __FILE__ ": "); \ 15727b27ec6Sopenharmony_ci fprintf(stderr, __VA_ARGS__); \ 15827b27ec6Sopenharmony_ci fprintf(stderr, " \n"); \ 15927b27ec6Sopenharmony_ci } } 16027b27ec6Sopenharmony_ci#else 16127b27ec6Sopenharmony_ci# define DEBUGLOG(l, ...) {} /* disabled */ 16227b27ec6Sopenharmony_ci#endif 16327b27ec6Sopenharmony_ci 16427b27ec6Sopenharmony_ci 16527b27ec6Sopenharmony_ci/*-************************************ 16627b27ec6Sopenharmony_ci* Basic Types 16727b27ec6Sopenharmony_ci**************************************/ 16827b27ec6Sopenharmony_ci#if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) ) 16927b27ec6Sopenharmony_ci# include <stdint.h> 17027b27ec6Sopenharmony_ci typedef uint8_t BYTE; 17127b27ec6Sopenharmony_ci typedef uint16_t U16; 17227b27ec6Sopenharmony_ci typedef uint32_t U32; 17327b27ec6Sopenharmony_ci typedef int32_t S32; 17427b27ec6Sopenharmony_ci typedef uint64_t U64; 17527b27ec6Sopenharmony_ci#else 17627b27ec6Sopenharmony_ci typedef unsigned char BYTE; 17727b27ec6Sopenharmony_ci typedef unsigned short U16; 17827b27ec6Sopenharmony_ci typedef unsigned int U32; 17927b27ec6Sopenharmony_ci typedef signed int S32; 18027b27ec6Sopenharmony_ci typedef unsigned long long U64; 18127b27ec6Sopenharmony_ci#endif 18227b27ec6Sopenharmony_ci 18327b27ec6Sopenharmony_ci 18427b27ec6Sopenharmony_ci/* unoptimized version; solves endianness & alignment issues */ 18527b27ec6Sopenharmony_cistatic U32 LZ4F_readLE32 (const void* src) 18627b27ec6Sopenharmony_ci{ 18727b27ec6Sopenharmony_ci const BYTE* const srcPtr = (const BYTE*)src; 18827b27ec6Sopenharmony_ci U32 value32 = srcPtr[0]; 18927b27ec6Sopenharmony_ci value32 += ((U32)srcPtr[1])<< 8; 19027b27ec6Sopenharmony_ci value32 += ((U32)srcPtr[2])<<16; 19127b27ec6Sopenharmony_ci value32 += ((U32)srcPtr[3])<<24; 19227b27ec6Sopenharmony_ci return value32; 19327b27ec6Sopenharmony_ci} 19427b27ec6Sopenharmony_ci 19527b27ec6Sopenharmony_cistatic void LZ4F_writeLE32 (void* dst, U32 value32) 19627b27ec6Sopenharmony_ci{ 19727b27ec6Sopenharmony_ci BYTE* const dstPtr = (BYTE*)dst; 19827b27ec6Sopenharmony_ci dstPtr[0] = (BYTE)value32; 19927b27ec6Sopenharmony_ci dstPtr[1] = (BYTE)(value32 >> 8); 20027b27ec6Sopenharmony_ci dstPtr[2] = (BYTE)(value32 >> 16); 20127b27ec6Sopenharmony_ci dstPtr[3] = (BYTE)(value32 >> 24); 20227b27ec6Sopenharmony_ci} 20327b27ec6Sopenharmony_ci 20427b27ec6Sopenharmony_cistatic U64 LZ4F_readLE64 (const void* src) 20527b27ec6Sopenharmony_ci{ 20627b27ec6Sopenharmony_ci const BYTE* const srcPtr = (const BYTE*)src; 20727b27ec6Sopenharmony_ci U64 value64 = srcPtr[0]; 20827b27ec6Sopenharmony_ci value64 += ((U64)srcPtr[1]<<8); 20927b27ec6Sopenharmony_ci value64 += ((U64)srcPtr[2]<<16); 21027b27ec6Sopenharmony_ci value64 += ((U64)srcPtr[3]<<24); 21127b27ec6Sopenharmony_ci value64 += ((U64)srcPtr[4]<<32); 21227b27ec6Sopenharmony_ci value64 += ((U64)srcPtr[5]<<40); 21327b27ec6Sopenharmony_ci value64 += ((U64)srcPtr[6]<<48); 21427b27ec6Sopenharmony_ci value64 += ((U64)srcPtr[7]<<56); 21527b27ec6Sopenharmony_ci return value64; 21627b27ec6Sopenharmony_ci} 21727b27ec6Sopenharmony_ci 21827b27ec6Sopenharmony_cistatic void LZ4F_writeLE64 (void* dst, U64 value64) 21927b27ec6Sopenharmony_ci{ 22027b27ec6Sopenharmony_ci BYTE* const dstPtr = (BYTE*)dst; 22127b27ec6Sopenharmony_ci dstPtr[0] = (BYTE)value64; 22227b27ec6Sopenharmony_ci dstPtr[1] = (BYTE)(value64 >> 8); 22327b27ec6Sopenharmony_ci dstPtr[2] = (BYTE)(value64 >> 16); 22427b27ec6Sopenharmony_ci dstPtr[3] = (BYTE)(value64 >> 24); 22527b27ec6Sopenharmony_ci dstPtr[4] = (BYTE)(value64 >> 32); 22627b27ec6Sopenharmony_ci dstPtr[5] = (BYTE)(value64 >> 40); 22727b27ec6Sopenharmony_ci dstPtr[6] = (BYTE)(value64 >> 48); 22827b27ec6Sopenharmony_ci dstPtr[7] = (BYTE)(value64 >> 56); 22927b27ec6Sopenharmony_ci} 23027b27ec6Sopenharmony_ci 23127b27ec6Sopenharmony_ci 23227b27ec6Sopenharmony_ci/*-************************************ 23327b27ec6Sopenharmony_ci* Constants 23427b27ec6Sopenharmony_ci**************************************/ 23527b27ec6Sopenharmony_ci#ifndef LZ4_SRC_INCLUDED /* avoid double definition */ 23627b27ec6Sopenharmony_ci# define KB *(1<<10) 23727b27ec6Sopenharmony_ci# define MB *(1<<20) 23827b27ec6Sopenharmony_ci# define GB *(1<<30) 23927b27ec6Sopenharmony_ci#endif 24027b27ec6Sopenharmony_ci 24127b27ec6Sopenharmony_ci#define _1BIT 0x01 24227b27ec6Sopenharmony_ci#define _2BITS 0x03 24327b27ec6Sopenharmony_ci#define _3BITS 0x07 24427b27ec6Sopenharmony_ci#define _4BITS 0x0F 24527b27ec6Sopenharmony_ci#define _8BITS 0xFF 24627b27ec6Sopenharmony_ci 24727b27ec6Sopenharmony_ci#define LZ4F_BLOCKUNCOMPRESSED_FLAG 0x80000000U 24827b27ec6Sopenharmony_ci#define LZ4F_BLOCKSIZEID_DEFAULT LZ4F_max64KB 24927b27ec6Sopenharmony_ci 25027b27ec6Sopenharmony_cistatic const size_t minFHSize = LZ4F_HEADER_SIZE_MIN; /* 7 */ 25127b27ec6Sopenharmony_cistatic const size_t maxFHSize = LZ4F_HEADER_SIZE_MAX; /* 19 */ 25227b27ec6Sopenharmony_cistatic const size_t BHSize = LZ4F_BLOCK_HEADER_SIZE; /* block header : size, and compress flag */ 25327b27ec6Sopenharmony_cistatic const size_t BFSize = LZ4F_BLOCK_CHECKSUM_SIZE; /* block footer : checksum (optional) */ 25427b27ec6Sopenharmony_ci 25527b27ec6Sopenharmony_ci 25627b27ec6Sopenharmony_ci/*-************************************ 25727b27ec6Sopenharmony_ci* Structures and local types 25827b27ec6Sopenharmony_ci**************************************/ 25927b27ec6Sopenharmony_ci 26027b27ec6Sopenharmony_citypedef enum { LZ4B_COMPRESSED, LZ4B_UNCOMPRESSED} LZ4F_blockCompression_t; 26127b27ec6Sopenharmony_ci 26227b27ec6Sopenharmony_citypedef struct LZ4F_cctx_s 26327b27ec6Sopenharmony_ci{ 26427b27ec6Sopenharmony_ci LZ4F_CustomMem cmem; 26527b27ec6Sopenharmony_ci LZ4F_preferences_t prefs; 26627b27ec6Sopenharmony_ci U32 version; 26727b27ec6Sopenharmony_ci U32 cStage; 26827b27ec6Sopenharmony_ci const LZ4F_CDict* cdict; 26927b27ec6Sopenharmony_ci size_t maxBlockSize; 27027b27ec6Sopenharmony_ci size_t maxBufferSize; 27127b27ec6Sopenharmony_ci BYTE* tmpBuff; /* internal buffer, for streaming */ 27227b27ec6Sopenharmony_ci BYTE* tmpIn; /* starting position of data compress within internal buffer (>= tmpBuff) */ 27327b27ec6Sopenharmony_ci size_t tmpInSize; /* amount of data to compress after tmpIn */ 27427b27ec6Sopenharmony_ci U64 totalInSize; 27527b27ec6Sopenharmony_ci XXH32_state_t xxh; 27627b27ec6Sopenharmony_ci void* lz4CtxPtr; 27727b27ec6Sopenharmony_ci U16 lz4CtxAlloc; /* sized for: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */ 27827b27ec6Sopenharmony_ci U16 lz4CtxState; /* in use as: 0 = none, 1 = lz4 ctx, 2 = lz4hc ctx */ 27927b27ec6Sopenharmony_ci LZ4F_blockCompression_t blockCompression; 28027b27ec6Sopenharmony_ci} LZ4F_cctx_t; 28127b27ec6Sopenharmony_ci 28227b27ec6Sopenharmony_ci 28327b27ec6Sopenharmony_ci/*-************************************ 28427b27ec6Sopenharmony_ci* Error management 28527b27ec6Sopenharmony_ci**************************************/ 28627b27ec6Sopenharmony_ci#define LZ4F_GENERATE_STRING(STRING) #STRING, 28727b27ec6Sopenharmony_cistatic const char* LZ4F_errorStrings[] = { LZ4F_LIST_ERRORS(LZ4F_GENERATE_STRING) }; 28827b27ec6Sopenharmony_ci 28927b27ec6Sopenharmony_ci 29027b27ec6Sopenharmony_ciunsigned LZ4F_isError(LZ4F_errorCode_t code) 29127b27ec6Sopenharmony_ci{ 29227b27ec6Sopenharmony_ci return (code > (LZ4F_errorCode_t)(-LZ4F_ERROR_maxCode)); 29327b27ec6Sopenharmony_ci} 29427b27ec6Sopenharmony_ci 29527b27ec6Sopenharmony_ciconst char* LZ4F_getErrorName(LZ4F_errorCode_t code) 29627b27ec6Sopenharmony_ci{ 29727b27ec6Sopenharmony_ci static const char* codeError = "Unspecified error code"; 29827b27ec6Sopenharmony_ci if (LZ4F_isError(code)) return LZ4F_errorStrings[-(int)(code)]; 29927b27ec6Sopenharmony_ci return codeError; 30027b27ec6Sopenharmony_ci} 30127b27ec6Sopenharmony_ci 30227b27ec6Sopenharmony_ciLZ4F_errorCodes LZ4F_getErrorCode(size_t functionResult) 30327b27ec6Sopenharmony_ci{ 30427b27ec6Sopenharmony_ci if (!LZ4F_isError(functionResult)) return LZ4F_OK_NoError; 30527b27ec6Sopenharmony_ci return (LZ4F_errorCodes)(-(ptrdiff_t)functionResult); 30627b27ec6Sopenharmony_ci} 30727b27ec6Sopenharmony_ci 30827b27ec6Sopenharmony_cistatic LZ4F_errorCode_t LZ4F_returnErrorCode(LZ4F_errorCodes code) 30927b27ec6Sopenharmony_ci{ 31027b27ec6Sopenharmony_ci /* A compilation error here means sizeof(ptrdiff_t) is not large enough */ 31127b27ec6Sopenharmony_ci LZ4F_STATIC_ASSERT(sizeof(ptrdiff_t) >= sizeof(size_t)); 31227b27ec6Sopenharmony_ci return (LZ4F_errorCode_t)-(ptrdiff_t)code; 31327b27ec6Sopenharmony_ci} 31427b27ec6Sopenharmony_ci 31527b27ec6Sopenharmony_ci#define RETURN_ERROR(e) return LZ4F_returnErrorCode(LZ4F_ERROR_ ## e) 31627b27ec6Sopenharmony_ci 31727b27ec6Sopenharmony_ci#define RETURN_ERROR_IF(c,e) if (c) RETURN_ERROR(e) 31827b27ec6Sopenharmony_ci 31927b27ec6Sopenharmony_ci#define FORWARD_IF_ERROR(r) if (LZ4F_isError(r)) return (r) 32027b27ec6Sopenharmony_ci 32127b27ec6Sopenharmony_ciunsigned LZ4F_getVersion(void) { return LZ4F_VERSION; } 32227b27ec6Sopenharmony_ci 32327b27ec6Sopenharmony_ciint LZ4F_compressionLevel_max(void) { return LZ4HC_CLEVEL_MAX; } 32427b27ec6Sopenharmony_ci 32527b27ec6Sopenharmony_cisize_t LZ4F_getBlockSize(LZ4F_blockSizeID_t blockSizeID) 32627b27ec6Sopenharmony_ci{ 32727b27ec6Sopenharmony_ci static const size_t blockSizes[4] = { 64 KB, 256 KB, 1 MB, 4 MB }; 32827b27ec6Sopenharmony_ci 32927b27ec6Sopenharmony_ci if (blockSizeID == 0) blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; 33027b27ec6Sopenharmony_ci if (blockSizeID < LZ4F_max64KB || blockSizeID > LZ4F_max4MB) 33127b27ec6Sopenharmony_ci RETURN_ERROR(maxBlockSize_invalid); 33227b27ec6Sopenharmony_ci { int const blockSizeIdx = (int)blockSizeID - (int)LZ4F_max64KB; 33327b27ec6Sopenharmony_ci return blockSizes[blockSizeIdx]; 33427b27ec6Sopenharmony_ci} } 33527b27ec6Sopenharmony_ci 33627b27ec6Sopenharmony_ci/*-************************************ 33727b27ec6Sopenharmony_ci* Private functions 33827b27ec6Sopenharmony_ci**************************************/ 33927b27ec6Sopenharmony_ci#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) 34027b27ec6Sopenharmony_ci 34127b27ec6Sopenharmony_cistatic BYTE LZ4F_headerChecksum (const void* header, size_t length) 34227b27ec6Sopenharmony_ci{ 34327b27ec6Sopenharmony_ci U32 const xxh = XXH32(header, length, 0); 34427b27ec6Sopenharmony_ci return (BYTE)(xxh >> 8); 34527b27ec6Sopenharmony_ci} 34627b27ec6Sopenharmony_ci 34727b27ec6Sopenharmony_ci 34827b27ec6Sopenharmony_ci/*-************************************ 34927b27ec6Sopenharmony_ci* Simple-pass compression functions 35027b27ec6Sopenharmony_ci**************************************/ 35127b27ec6Sopenharmony_cistatic LZ4F_blockSizeID_t LZ4F_optimalBSID(const LZ4F_blockSizeID_t requestedBSID, 35227b27ec6Sopenharmony_ci const size_t srcSize) 35327b27ec6Sopenharmony_ci{ 35427b27ec6Sopenharmony_ci LZ4F_blockSizeID_t proposedBSID = LZ4F_max64KB; 35527b27ec6Sopenharmony_ci size_t maxBlockSize = 64 KB; 35627b27ec6Sopenharmony_ci while (requestedBSID > proposedBSID) { 35727b27ec6Sopenharmony_ci if (srcSize <= maxBlockSize) 35827b27ec6Sopenharmony_ci return proposedBSID; 35927b27ec6Sopenharmony_ci proposedBSID = (LZ4F_blockSizeID_t)((int)proposedBSID + 1); 36027b27ec6Sopenharmony_ci maxBlockSize <<= 2; 36127b27ec6Sopenharmony_ci } 36227b27ec6Sopenharmony_ci return requestedBSID; 36327b27ec6Sopenharmony_ci} 36427b27ec6Sopenharmony_ci 36527b27ec6Sopenharmony_ci/*! LZ4F_compressBound_internal() : 36627b27ec6Sopenharmony_ci * Provides dstCapacity given a srcSize to guarantee operation success in worst case situations. 36727b27ec6Sopenharmony_ci * prefsPtr is optional : if NULL is provided, preferences will be set to cover worst case scenario. 36827b27ec6Sopenharmony_ci * @return is always the same for a srcSize and prefsPtr, so it can be relied upon to size reusable buffers. 36927b27ec6Sopenharmony_ci * When srcSize==0, LZ4F_compressBound() provides an upper bound for LZ4F_flush() and LZ4F_compressEnd() operations. 37027b27ec6Sopenharmony_ci */ 37127b27ec6Sopenharmony_cistatic size_t LZ4F_compressBound_internal(size_t srcSize, 37227b27ec6Sopenharmony_ci const LZ4F_preferences_t* preferencesPtr, 37327b27ec6Sopenharmony_ci size_t alreadyBuffered) 37427b27ec6Sopenharmony_ci{ 37527b27ec6Sopenharmony_ci LZ4F_preferences_t prefsNull = LZ4F_INIT_PREFERENCES; 37627b27ec6Sopenharmony_ci prefsNull.frameInfo.contentChecksumFlag = LZ4F_contentChecksumEnabled; /* worst case */ 37727b27ec6Sopenharmony_ci prefsNull.frameInfo.blockChecksumFlag = LZ4F_blockChecksumEnabled; /* worst case */ 37827b27ec6Sopenharmony_ci { const LZ4F_preferences_t* const prefsPtr = (preferencesPtr==NULL) ? &prefsNull : preferencesPtr; 37927b27ec6Sopenharmony_ci U32 const flush = prefsPtr->autoFlush | (srcSize==0); 38027b27ec6Sopenharmony_ci LZ4F_blockSizeID_t const blockID = prefsPtr->frameInfo.blockSizeID; 38127b27ec6Sopenharmony_ci size_t const blockSize = LZ4F_getBlockSize(blockID); 38227b27ec6Sopenharmony_ci size_t const maxBuffered = blockSize - 1; 38327b27ec6Sopenharmony_ci size_t const bufferedSize = MIN(alreadyBuffered, maxBuffered); 38427b27ec6Sopenharmony_ci size_t const maxSrcSize = srcSize + bufferedSize; 38527b27ec6Sopenharmony_ci unsigned const nbFullBlocks = (unsigned)(maxSrcSize / blockSize); 38627b27ec6Sopenharmony_ci size_t const partialBlockSize = maxSrcSize & (blockSize-1); 38727b27ec6Sopenharmony_ci size_t const lastBlockSize = flush ? partialBlockSize : 0; 38827b27ec6Sopenharmony_ci unsigned const nbBlocks = nbFullBlocks + (lastBlockSize>0); 38927b27ec6Sopenharmony_ci 39027b27ec6Sopenharmony_ci size_t const blockCRCSize = BFSize * prefsPtr->frameInfo.blockChecksumFlag; 39127b27ec6Sopenharmony_ci size_t const frameEnd = BHSize + (prefsPtr->frameInfo.contentChecksumFlag*BFSize); 39227b27ec6Sopenharmony_ci 39327b27ec6Sopenharmony_ci return ((BHSize + blockCRCSize) * nbBlocks) + 39427b27ec6Sopenharmony_ci (blockSize * nbFullBlocks) + lastBlockSize + frameEnd; 39527b27ec6Sopenharmony_ci } 39627b27ec6Sopenharmony_ci} 39727b27ec6Sopenharmony_ci 39827b27ec6Sopenharmony_cisize_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) 39927b27ec6Sopenharmony_ci{ 40027b27ec6Sopenharmony_ci LZ4F_preferences_t prefs; 40127b27ec6Sopenharmony_ci size_t const headerSize = maxFHSize; /* max header size, including optional fields */ 40227b27ec6Sopenharmony_ci 40327b27ec6Sopenharmony_ci if (preferencesPtr!=NULL) prefs = *preferencesPtr; 40427b27ec6Sopenharmony_ci else MEM_INIT(&prefs, 0, sizeof(prefs)); 40527b27ec6Sopenharmony_ci prefs.autoFlush = 1; 40627b27ec6Sopenharmony_ci 40727b27ec6Sopenharmony_ci return headerSize + LZ4F_compressBound_internal(srcSize, &prefs, 0);; 40827b27ec6Sopenharmony_ci} 40927b27ec6Sopenharmony_ci 41027b27ec6Sopenharmony_ci 41127b27ec6Sopenharmony_ci/*! LZ4F_compressFrame_usingCDict() : 41227b27ec6Sopenharmony_ci * Compress srcBuffer using a dictionary, in a single step. 41327b27ec6Sopenharmony_ci * cdict can be NULL, in which case, no dictionary is used. 41427b27ec6Sopenharmony_ci * dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr). 41527b27ec6Sopenharmony_ci * The LZ4F_preferences_t structure is optional : you may provide NULL as argument, 41627b27ec6Sopenharmony_ci * however, it's the only way to provide a dictID, so it's not recommended. 41727b27ec6Sopenharmony_ci * @return : number of bytes written into dstBuffer, 41827b27ec6Sopenharmony_ci * or an error code if it fails (can be tested using LZ4F_isError()) 41927b27ec6Sopenharmony_ci */ 42027b27ec6Sopenharmony_cisize_t LZ4F_compressFrame_usingCDict(LZ4F_cctx* cctx, 42127b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 42227b27ec6Sopenharmony_ci const void* srcBuffer, size_t srcSize, 42327b27ec6Sopenharmony_ci const LZ4F_CDict* cdict, 42427b27ec6Sopenharmony_ci const LZ4F_preferences_t* preferencesPtr) 42527b27ec6Sopenharmony_ci{ 42627b27ec6Sopenharmony_ci LZ4F_preferences_t prefs; 42727b27ec6Sopenharmony_ci LZ4F_compressOptions_t options; 42827b27ec6Sopenharmony_ci BYTE* const dstStart = (BYTE*) dstBuffer; 42927b27ec6Sopenharmony_ci BYTE* dstPtr = dstStart; 43027b27ec6Sopenharmony_ci BYTE* const dstEnd = dstStart + dstCapacity; 43127b27ec6Sopenharmony_ci 43227b27ec6Sopenharmony_ci if (preferencesPtr!=NULL) 43327b27ec6Sopenharmony_ci prefs = *preferencesPtr; 43427b27ec6Sopenharmony_ci else 43527b27ec6Sopenharmony_ci MEM_INIT(&prefs, 0, sizeof(prefs)); 43627b27ec6Sopenharmony_ci if (prefs.frameInfo.contentSize != 0) 43727b27ec6Sopenharmony_ci prefs.frameInfo.contentSize = (U64)srcSize; /* auto-correct content size if selected (!=0) */ 43827b27ec6Sopenharmony_ci 43927b27ec6Sopenharmony_ci prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize); 44027b27ec6Sopenharmony_ci prefs.autoFlush = 1; 44127b27ec6Sopenharmony_ci if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID)) 44227b27ec6Sopenharmony_ci prefs.frameInfo.blockMode = LZ4F_blockIndependent; /* only one block => no need for inter-block link */ 44327b27ec6Sopenharmony_ci 44427b27ec6Sopenharmony_ci MEM_INIT(&options, 0, sizeof(options)); 44527b27ec6Sopenharmony_ci options.stableSrc = 1; 44627b27ec6Sopenharmony_ci 44727b27ec6Sopenharmony_ci RETURN_ERROR_IF(dstCapacity < LZ4F_compressFrameBound(srcSize, &prefs), dstMaxSize_tooSmall); 44827b27ec6Sopenharmony_ci 44927b27ec6Sopenharmony_ci { size_t const headerSize = LZ4F_compressBegin_usingCDict(cctx, dstBuffer, dstCapacity, cdict, &prefs); /* write header */ 45027b27ec6Sopenharmony_ci FORWARD_IF_ERROR(headerSize); 45127b27ec6Sopenharmony_ci dstPtr += headerSize; /* header size */ } 45227b27ec6Sopenharmony_ci 45327b27ec6Sopenharmony_ci assert(dstEnd >= dstPtr); 45427b27ec6Sopenharmony_ci { size_t const cSize = LZ4F_compressUpdate(cctx, dstPtr, (size_t)(dstEnd-dstPtr), srcBuffer, srcSize, &options); 45527b27ec6Sopenharmony_ci FORWARD_IF_ERROR(cSize); 45627b27ec6Sopenharmony_ci dstPtr += cSize; } 45727b27ec6Sopenharmony_ci 45827b27ec6Sopenharmony_ci assert(dstEnd >= dstPtr); 45927b27ec6Sopenharmony_ci { size_t const tailSize = LZ4F_compressEnd(cctx, dstPtr, (size_t)(dstEnd-dstPtr), &options); /* flush last block, and generate suffix */ 46027b27ec6Sopenharmony_ci FORWARD_IF_ERROR(tailSize); 46127b27ec6Sopenharmony_ci dstPtr += tailSize; } 46227b27ec6Sopenharmony_ci 46327b27ec6Sopenharmony_ci assert(dstEnd >= dstStart); 46427b27ec6Sopenharmony_ci return (size_t)(dstPtr - dstStart); 46527b27ec6Sopenharmony_ci} 46627b27ec6Sopenharmony_ci 46727b27ec6Sopenharmony_ci 46827b27ec6Sopenharmony_ci/*! LZ4F_compressFrame() : 46927b27ec6Sopenharmony_ci * Compress an entire srcBuffer into a valid LZ4 frame, in a single step. 47027b27ec6Sopenharmony_ci * dstBuffer MUST be >= LZ4F_compressFrameBound(srcSize, preferencesPtr). 47127b27ec6Sopenharmony_ci * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default. 47227b27ec6Sopenharmony_ci * @return : number of bytes written into dstBuffer. 47327b27ec6Sopenharmony_ci * or an error code if it fails (can be tested using LZ4F_isError()) 47427b27ec6Sopenharmony_ci */ 47527b27ec6Sopenharmony_cisize_t LZ4F_compressFrame(void* dstBuffer, size_t dstCapacity, 47627b27ec6Sopenharmony_ci const void* srcBuffer, size_t srcSize, 47727b27ec6Sopenharmony_ci const LZ4F_preferences_t* preferencesPtr) 47827b27ec6Sopenharmony_ci{ 47927b27ec6Sopenharmony_ci size_t result; 48027b27ec6Sopenharmony_ci#if (LZ4F_HEAPMODE) 48127b27ec6Sopenharmony_ci LZ4F_cctx_t* cctxPtr; 48227b27ec6Sopenharmony_ci result = LZ4F_createCompressionContext(&cctxPtr, LZ4F_VERSION); 48327b27ec6Sopenharmony_ci FORWARD_IF_ERROR(result); 48427b27ec6Sopenharmony_ci#else 48527b27ec6Sopenharmony_ci LZ4F_cctx_t cctx; 48627b27ec6Sopenharmony_ci LZ4_stream_t lz4ctx; 48727b27ec6Sopenharmony_ci LZ4F_cctx_t* const cctxPtr = &cctx; 48827b27ec6Sopenharmony_ci 48927b27ec6Sopenharmony_ci MEM_INIT(&cctx, 0, sizeof(cctx)); 49027b27ec6Sopenharmony_ci cctx.version = LZ4F_VERSION; 49127b27ec6Sopenharmony_ci cctx.maxBufferSize = 5 MB; /* mess with real buffer size to prevent dynamic allocation; works only because autoflush==1 & stableSrc==1 */ 49227b27ec6Sopenharmony_ci if ( preferencesPtr == NULL 49327b27ec6Sopenharmony_ci || preferencesPtr->compressionLevel < LZ4HC_CLEVEL_MIN ) { 49427b27ec6Sopenharmony_ci LZ4_initStream(&lz4ctx, sizeof(lz4ctx)); 49527b27ec6Sopenharmony_ci cctxPtr->lz4CtxPtr = &lz4ctx; 49627b27ec6Sopenharmony_ci cctxPtr->lz4CtxAlloc = 1; 49727b27ec6Sopenharmony_ci cctxPtr->lz4CtxState = 1; 49827b27ec6Sopenharmony_ci } 49927b27ec6Sopenharmony_ci#endif 50027b27ec6Sopenharmony_ci DEBUGLOG(4, "LZ4F_compressFrame"); 50127b27ec6Sopenharmony_ci 50227b27ec6Sopenharmony_ci result = LZ4F_compressFrame_usingCDict(cctxPtr, dstBuffer, dstCapacity, 50327b27ec6Sopenharmony_ci srcBuffer, srcSize, 50427b27ec6Sopenharmony_ci NULL, preferencesPtr); 50527b27ec6Sopenharmony_ci 50627b27ec6Sopenharmony_ci#if (LZ4F_HEAPMODE) 50727b27ec6Sopenharmony_ci LZ4F_freeCompressionContext(cctxPtr); 50827b27ec6Sopenharmony_ci#else 50927b27ec6Sopenharmony_ci if ( preferencesPtr != NULL 51027b27ec6Sopenharmony_ci && preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN ) { 51127b27ec6Sopenharmony_ci LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem); 51227b27ec6Sopenharmony_ci } 51327b27ec6Sopenharmony_ci#endif 51427b27ec6Sopenharmony_ci return result; 51527b27ec6Sopenharmony_ci} 51627b27ec6Sopenharmony_ci 51727b27ec6Sopenharmony_ci 51827b27ec6Sopenharmony_ci/*-*************************************************** 51927b27ec6Sopenharmony_ci* Dictionary compression 52027b27ec6Sopenharmony_ci*****************************************************/ 52127b27ec6Sopenharmony_ci 52227b27ec6Sopenharmony_cistruct LZ4F_CDict_s { 52327b27ec6Sopenharmony_ci LZ4F_CustomMem cmem; 52427b27ec6Sopenharmony_ci void* dictContent; 52527b27ec6Sopenharmony_ci LZ4_stream_t* fastCtx; 52627b27ec6Sopenharmony_ci LZ4_streamHC_t* HCCtx; 52727b27ec6Sopenharmony_ci}; /* typedef'd to LZ4F_CDict within lz4frame_static.h */ 52827b27ec6Sopenharmony_ci 52927b27ec6Sopenharmony_ciLZ4F_CDict* 53027b27ec6Sopenharmony_ciLZ4F_createCDict_advanced(LZ4F_CustomMem cmem, const void* dictBuffer, size_t dictSize) 53127b27ec6Sopenharmony_ci{ 53227b27ec6Sopenharmony_ci const char* dictStart = (const char*)dictBuffer; 53327b27ec6Sopenharmony_ci LZ4F_CDict* const cdict = (LZ4F_CDict*)LZ4F_malloc(sizeof(*cdict), cmem); 53427b27ec6Sopenharmony_ci DEBUGLOG(4, "LZ4F_createCDict_advanced"); 53527b27ec6Sopenharmony_ci if (!cdict) return NULL; 53627b27ec6Sopenharmony_ci cdict->cmem = cmem; 53727b27ec6Sopenharmony_ci if (dictSize > 64 KB) { 53827b27ec6Sopenharmony_ci dictStart += dictSize - 64 KB; 53927b27ec6Sopenharmony_ci dictSize = 64 KB; 54027b27ec6Sopenharmony_ci } 54127b27ec6Sopenharmony_ci cdict->dictContent = LZ4F_malloc(dictSize, cmem); 54227b27ec6Sopenharmony_ci cdict->fastCtx = (LZ4_stream_t*)LZ4F_malloc(sizeof(LZ4_stream_t), cmem); 54327b27ec6Sopenharmony_ci if (cdict->fastCtx) 54427b27ec6Sopenharmony_ci LZ4_initStream(cdict->fastCtx, sizeof(LZ4_stream_t)); 54527b27ec6Sopenharmony_ci cdict->HCCtx = (LZ4_streamHC_t*)LZ4F_malloc(sizeof(LZ4_streamHC_t), cmem); 54627b27ec6Sopenharmony_ci if (cdict->HCCtx) 54727b27ec6Sopenharmony_ci LZ4_initStream(cdict->HCCtx, sizeof(LZ4_streamHC_t)); 54827b27ec6Sopenharmony_ci if (!cdict->dictContent || !cdict->fastCtx || !cdict->HCCtx) { 54927b27ec6Sopenharmony_ci LZ4F_freeCDict(cdict); 55027b27ec6Sopenharmony_ci return NULL; 55127b27ec6Sopenharmony_ci } 55227b27ec6Sopenharmony_ci memcpy(cdict->dictContent, dictStart, dictSize); 55327b27ec6Sopenharmony_ci LZ4_loadDict (cdict->fastCtx, (const char*)cdict->dictContent, (int)dictSize); 55427b27ec6Sopenharmony_ci LZ4_setCompressionLevel(cdict->HCCtx, LZ4HC_CLEVEL_DEFAULT); 55527b27ec6Sopenharmony_ci LZ4_loadDictHC(cdict->HCCtx, (const char*)cdict->dictContent, (int)dictSize); 55627b27ec6Sopenharmony_ci return cdict; 55727b27ec6Sopenharmony_ci} 55827b27ec6Sopenharmony_ci 55927b27ec6Sopenharmony_ci/*! LZ4F_createCDict() : 56027b27ec6Sopenharmony_ci * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. 56127b27ec6Sopenharmony_ci * LZ4F_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. 56227b27ec6Sopenharmony_ci * LZ4F_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. 56327b27ec6Sopenharmony_ci * @dictBuffer can be released after LZ4F_CDict creation, since its content is copied within CDict 56427b27ec6Sopenharmony_ci * @return : digested dictionary for compression, or NULL if failed */ 56527b27ec6Sopenharmony_ciLZ4F_CDict* LZ4F_createCDict(const void* dictBuffer, size_t dictSize) 56627b27ec6Sopenharmony_ci{ 56727b27ec6Sopenharmony_ci DEBUGLOG(4, "LZ4F_createCDict"); 56827b27ec6Sopenharmony_ci return LZ4F_createCDict_advanced(LZ4F_defaultCMem, dictBuffer, dictSize); 56927b27ec6Sopenharmony_ci} 57027b27ec6Sopenharmony_ci 57127b27ec6Sopenharmony_civoid LZ4F_freeCDict(LZ4F_CDict* cdict) 57227b27ec6Sopenharmony_ci{ 57327b27ec6Sopenharmony_ci if (cdict==NULL) return; /* support free on NULL */ 57427b27ec6Sopenharmony_ci LZ4F_free(cdict->dictContent, cdict->cmem); 57527b27ec6Sopenharmony_ci LZ4F_free(cdict->fastCtx, cdict->cmem); 57627b27ec6Sopenharmony_ci LZ4F_free(cdict->HCCtx, cdict->cmem); 57727b27ec6Sopenharmony_ci LZ4F_free(cdict, cdict->cmem); 57827b27ec6Sopenharmony_ci} 57927b27ec6Sopenharmony_ci 58027b27ec6Sopenharmony_ci 58127b27ec6Sopenharmony_ci/*-********************************* 58227b27ec6Sopenharmony_ci* Advanced compression functions 58327b27ec6Sopenharmony_ci***********************************/ 58427b27ec6Sopenharmony_ci 58527b27ec6Sopenharmony_ciLZ4F_cctx* 58627b27ec6Sopenharmony_ciLZ4F_createCompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version) 58727b27ec6Sopenharmony_ci{ 58827b27ec6Sopenharmony_ci LZ4F_cctx* const cctxPtr = 58927b27ec6Sopenharmony_ci (LZ4F_cctx*)LZ4F_calloc(sizeof(LZ4F_cctx), customMem); 59027b27ec6Sopenharmony_ci if (cctxPtr==NULL) return NULL; 59127b27ec6Sopenharmony_ci 59227b27ec6Sopenharmony_ci cctxPtr->cmem = customMem; 59327b27ec6Sopenharmony_ci cctxPtr->version = version; 59427b27ec6Sopenharmony_ci cctxPtr->cStage = 0; /* Uninitialized. Next stage : init cctx */ 59527b27ec6Sopenharmony_ci 59627b27ec6Sopenharmony_ci return cctxPtr; 59727b27ec6Sopenharmony_ci} 59827b27ec6Sopenharmony_ci 59927b27ec6Sopenharmony_ci/*! LZ4F_createCompressionContext() : 60027b27ec6Sopenharmony_ci * The first thing to do is to create a compressionContext object, which will be used in all compression operations. 60127b27ec6Sopenharmony_ci * This is achieved using LZ4F_createCompressionContext(), which takes as argument a version and an LZ4F_preferences_t structure. 60227b27ec6Sopenharmony_ci * The version provided MUST be LZ4F_VERSION. It is intended to track potential incompatible differences between different binaries. 60327b27ec6Sopenharmony_ci * The function will provide a pointer to an allocated LZ4F_compressionContext_t object. 60427b27ec6Sopenharmony_ci * If the result LZ4F_errorCode_t is not OK_NoError, there was an error during context creation. 60527b27ec6Sopenharmony_ci * Object can release its memory using LZ4F_freeCompressionContext(); 60627b27ec6Sopenharmony_ci**/ 60727b27ec6Sopenharmony_ciLZ4F_errorCode_t 60827b27ec6Sopenharmony_ciLZ4F_createCompressionContext(LZ4F_cctx** LZ4F_compressionContextPtr, unsigned version) 60927b27ec6Sopenharmony_ci{ 61027b27ec6Sopenharmony_ci assert(LZ4F_compressionContextPtr != NULL); /* considered a violation of narrow contract */ 61127b27ec6Sopenharmony_ci /* in case it nonetheless happen in production */ 61227b27ec6Sopenharmony_ci RETURN_ERROR_IF(LZ4F_compressionContextPtr == NULL, parameter_null); 61327b27ec6Sopenharmony_ci 61427b27ec6Sopenharmony_ci *LZ4F_compressionContextPtr = LZ4F_createCompressionContext_advanced(LZ4F_defaultCMem, version); 61527b27ec6Sopenharmony_ci RETURN_ERROR_IF(*LZ4F_compressionContextPtr==NULL, allocation_failed); 61627b27ec6Sopenharmony_ci return LZ4F_OK_NoError; 61727b27ec6Sopenharmony_ci} 61827b27ec6Sopenharmony_ci 61927b27ec6Sopenharmony_ci 62027b27ec6Sopenharmony_ciLZ4F_errorCode_t LZ4F_freeCompressionContext(LZ4F_cctx* cctxPtr) 62127b27ec6Sopenharmony_ci{ 62227b27ec6Sopenharmony_ci if (cctxPtr != NULL) { /* support free on NULL */ 62327b27ec6Sopenharmony_ci LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem); /* note: LZ4_streamHC_t and LZ4_stream_t are simple POD types */ 62427b27ec6Sopenharmony_ci LZ4F_free(cctxPtr->tmpBuff, cctxPtr->cmem); 62527b27ec6Sopenharmony_ci LZ4F_free(cctxPtr, cctxPtr->cmem); 62627b27ec6Sopenharmony_ci } 62727b27ec6Sopenharmony_ci return LZ4F_OK_NoError; 62827b27ec6Sopenharmony_ci} 62927b27ec6Sopenharmony_ci 63027b27ec6Sopenharmony_ci 63127b27ec6Sopenharmony_ci/** 63227b27ec6Sopenharmony_ci * This function prepares the internal LZ4(HC) stream for a new compression, 63327b27ec6Sopenharmony_ci * resetting the context and attaching the dictionary, if there is one. 63427b27ec6Sopenharmony_ci * 63527b27ec6Sopenharmony_ci * It needs to be called at the beginning of each independent compression 63627b27ec6Sopenharmony_ci * stream (i.e., at the beginning of a frame in blockLinked mode, or at the 63727b27ec6Sopenharmony_ci * beginning of each block in blockIndependent mode). 63827b27ec6Sopenharmony_ci */ 63927b27ec6Sopenharmony_cistatic void LZ4F_initStream(void* ctx, 64027b27ec6Sopenharmony_ci const LZ4F_CDict* cdict, 64127b27ec6Sopenharmony_ci int level, 64227b27ec6Sopenharmony_ci LZ4F_blockMode_t blockMode) { 64327b27ec6Sopenharmony_ci if (level < LZ4HC_CLEVEL_MIN) { 64427b27ec6Sopenharmony_ci if (cdict != NULL || blockMode == LZ4F_blockLinked) { 64527b27ec6Sopenharmony_ci /* In these cases, we will call LZ4_compress_fast_continue(), 64627b27ec6Sopenharmony_ci * which needs an already reset context. Otherwise, we'll call a 64727b27ec6Sopenharmony_ci * one-shot API. The non-continued APIs internally perform their own 64827b27ec6Sopenharmony_ci * resets at the beginning of their calls, where they know what 64927b27ec6Sopenharmony_ci * tableType they need the context to be in. So in that case this 65027b27ec6Sopenharmony_ci * would be misguided / wasted work. */ 65127b27ec6Sopenharmony_ci LZ4_resetStream_fast((LZ4_stream_t*)ctx); 65227b27ec6Sopenharmony_ci } 65327b27ec6Sopenharmony_ci LZ4_attach_dictionary((LZ4_stream_t *)ctx, cdict ? cdict->fastCtx : NULL); 65427b27ec6Sopenharmony_ci } else { 65527b27ec6Sopenharmony_ci LZ4_resetStreamHC_fast((LZ4_streamHC_t*)ctx, level); 65627b27ec6Sopenharmony_ci LZ4_attach_HC_dictionary((LZ4_streamHC_t *)ctx, cdict ? cdict->HCCtx : NULL); 65727b27ec6Sopenharmony_ci } 65827b27ec6Sopenharmony_ci} 65927b27ec6Sopenharmony_ci 66027b27ec6Sopenharmony_cistatic int ctxTypeID_to_size(int ctxTypeID) { 66127b27ec6Sopenharmony_ci switch(ctxTypeID) { 66227b27ec6Sopenharmony_ci case 1: 66327b27ec6Sopenharmony_ci return LZ4_sizeofState(); 66427b27ec6Sopenharmony_ci case 2: 66527b27ec6Sopenharmony_ci return LZ4_sizeofStateHC(); 66627b27ec6Sopenharmony_ci default: 66727b27ec6Sopenharmony_ci return 0; 66827b27ec6Sopenharmony_ci } 66927b27ec6Sopenharmony_ci} 67027b27ec6Sopenharmony_ci 67127b27ec6Sopenharmony_ci/*! LZ4F_compressBegin_usingCDict() : 67227b27ec6Sopenharmony_ci * init streaming compression AND writes frame header into @dstBuffer. 67327b27ec6Sopenharmony_ci * @dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes. 67427b27ec6Sopenharmony_ci * @return : number of bytes written into @dstBuffer for the header 67527b27ec6Sopenharmony_ci * or an error code (can be tested using LZ4F_isError()) 67627b27ec6Sopenharmony_ci */ 67727b27ec6Sopenharmony_cisize_t LZ4F_compressBegin_usingCDict(LZ4F_cctx* cctxPtr, 67827b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 67927b27ec6Sopenharmony_ci const LZ4F_CDict* cdict, 68027b27ec6Sopenharmony_ci const LZ4F_preferences_t* preferencesPtr) 68127b27ec6Sopenharmony_ci{ 68227b27ec6Sopenharmony_ci LZ4F_preferences_t const prefNull = LZ4F_INIT_PREFERENCES; 68327b27ec6Sopenharmony_ci BYTE* const dstStart = (BYTE*)dstBuffer; 68427b27ec6Sopenharmony_ci BYTE* dstPtr = dstStart; 68527b27ec6Sopenharmony_ci 68627b27ec6Sopenharmony_ci RETURN_ERROR_IF(dstCapacity < maxFHSize, dstMaxSize_tooSmall); 68727b27ec6Sopenharmony_ci if (preferencesPtr == NULL) preferencesPtr = &prefNull; 68827b27ec6Sopenharmony_ci cctxPtr->prefs = *preferencesPtr; 68927b27ec6Sopenharmony_ci 69027b27ec6Sopenharmony_ci /* cctx Management */ 69127b27ec6Sopenharmony_ci { U16 const ctxTypeID = (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) ? 1 : 2; 69227b27ec6Sopenharmony_ci int requiredSize = ctxTypeID_to_size(ctxTypeID); 69327b27ec6Sopenharmony_ci int allocatedSize = ctxTypeID_to_size(cctxPtr->lz4CtxAlloc); 69427b27ec6Sopenharmony_ci if (allocatedSize < requiredSize) { 69527b27ec6Sopenharmony_ci /* not enough space allocated */ 69627b27ec6Sopenharmony_ci LZ4F_free(cctxPtr->lz4CtxPtr, cctxPtr->cmem); 69727b27ec6Sopenharmony_ci if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { 69827b27ec6Sopenharmony_ci /* must take ownership of memory allocation, 69927b27ec6Sopenharmony_ci * in order to respect custom allocator contract */ 70027b27ec6Sopenharmony_ci cctxPtr->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_stream_t), cctxPtr->cmem); 70127b27ec6Sopenharmony_ci if (cctxPtr->lz4CtxPtr) 70227b27ec6Sopenharmony_ci LZ4_initStream(cctxPtr->lz4CtxPtr, sizeof(LZ4_stream_t)); 70327b27ec6Sopenharmony_ci } else { 70427b27ec6Sopenharmony_ci cctxPtr->lz4CtxPtr = LZ4F_malloc(sizeof(LZ4_streamHC_t), cctxPtr->cmem); 70527b27ec6Sopenharmony_ci if (cctxPtr->lz4CtxPtr) 70627b27ec6Sopenharmony_ci LZ4_initStreamHC(cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t)); 70727b27ec6Sopenharmony_ci } 70827b27ec6Sopenharmony_ci RETURN_ERROR_IF(cctxPtr->lz4CtxPtr == NULL, allocation_failed); 70927b27ec6Sopenharmony_ci cctxPtr->lz4CtxAlloc = ctxTypeID; 71027b27ec6Sopenharmony_ci cctxPtr->lz4CtxState = ctxTypeID; 71127b27ec6Sopenharmony_ci } else if (cctxPtr->lz4CtxState != ctxTypeID) { 71227b27ec6Sopenharmony_ci /* otherwise, a sufficient buffer is already allocated, 71327b27ec6Sopenharmony_ci * but we need to reset it to the correct context type */ 71427b27ec6Sopenharmony_ci if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) { 71527b27ec6Sopenharmony_ci LZ4_initStream((LZ4_stream_t*)cctxPtr->lz4CtxPtr, sizeof(LZ4_stream_t)); 71627b27ec6Sopenharmony_ci } else { 71727b27ec6Sopenharmony_ci LZ4_initStreamHC((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, sizeof(LZ4_streamHC_t)); 71827b27ec6Sopenharmony_ci LZ4_setCompressionLevel((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel); 71927b27ec6Sopenharmony_ci } 72027b27ec6Sopenharmony_ci cctxPtr->lz4CtxState = ctxTypeID; 72127b27ec6Sopenharmony_ci } } 72227b27ec6Sopenharmony_ci 72327b27ec6Sopenharmony_ci /* Buffer Management */ 72427b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.blockSizeID == 0) 72527b27ec6Sopenharmony_ci cctxPtr->prefs.frameInfo.blockSizeID = LZ4F_BLOCKSIZEID_DEFAULT; 72627b27ec6Sopenharmony_ci cctxPtr->maxBlockSize = LZ4F_getBlockSize(cctxPtr->prefs.frameInfo.blockSizeID); 72727b27ec6Sopenharmony_ci 72827b27ec6Sopenharmony_ci { size_t const requiredBuffSize = preferencesPtr->autoFlush ? 72927b27ec6Sopenharmony_ci ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 64 KB : 0) : /* only needs past data up to window size */ 73027b27ec6Sopenharmony_ci cctxPtr->maxBlockSize + ((cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) ? 128 KB : 0); 73127b27ec6Sopenharmony_ci 73227b27ec6Sopenharmony_ci if (cctxPtr->maxBufferSize < requiredBuffSize) { 73327b27ec6Sopenharmony_ci cctxPtr->maxBufferSize = 0; 73427b27ec6Sopenharmony_ci LZ4F_free(cctxPtr->tmpBuff, cctxPtr->cmem); 73527b27ec6Sopenharmony_ci cctxPtr->tmpBuff = (BYTE*)LZ4F_calloc(requiredBuffSize, cctxPtr->cmem); 73627b27ec6Sopenharmony_ci RETURN_ERROR_IF(cctxPtr->tmpBuff == NULL, allocation_failed); 73727b27ec6Sopenharmony_ci cctxPtr->maxBufferSize = requiredBuffSize; 73827b27ec6Sopenharmony_ci } } 73927b27ec6Sopenharmony_ci cctxPtr->tmpIn = cctxPtr->tmpBuff; 74027b27ec6Sopenharmony_ci cctxPtr->tmpInSize = 0; 74127b27ec6Sopenharmony_ci (void)XXH32_reset(&(cctxPtr->xxh), 0); 74227b27ec6Sopenharmony_ci 74327b27ec6Sopenharmony_ci /* context init */ 74427b27ec6Sopenharmony_ci cctxPtr->cdict = cdict; 74527b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) { 74627b27ec6Sopenharmony_ci /* frame init only for blockLinked : blockIndependent will be init at each block */ 74727b27ec6Sopenharmony_ci LZ4F_initStream(cctxPtr->lz4CtxPtr, cdict, cctxPtr->prefs.compressionLevel, LZ4F_blockLinked); 74827b27ec6Sopenharmony_ci } 74927b27ec6Sopenharmony_ci if (preferencesPtr->compressionLevel >= LZ4HC_CLEVEL_MIN) { 75027b27ec6Sopenharmony_ci LZ4_favorDecompressionSpeed((LZ4_streamHC_t*)cctxPtr->lz4CtxPtr, (int)preferencesPtr->favorDecSpeed); 75127b27ec6Sopenharmony_ci } 75227b27ec6Sopenharmony_ci 75327b27ec6Sopenharmony_ci /* Magic Number */ 75427b27ec6Sopenharmony_ci LZ4F_writeLE32(dstPtr, LZ4F_MAGICNUMBER); 75527b27ec6Sopenharmony_ci dstPtr += 4; 75627b27ec6Sopenharmony_ci { BYTE* const headerStart = dstPtr; 75727b27ec6Sopenharmony_ci 75827b27ec6Sopenharmony_ci /* FLG Byte */ 75927b27ec6Sopenharmony_ci *dstPtr++ = (BYTE)(((1 & _2BITS) << 6) /* Version('01') */ 76027b27ec6Sopenharmony_ci + ((cctxPtr->prefs.frameInfo.blockMode & _1BIT ) << 5) 76127b27ec6Sopenharmony_ci + ((cctxPtr->prefs.frameInfo.blockChecksumFlag & _1BIT ) << 4) 76227b27ec6Sopenharmony_ci + ((unsigned)(cctxPtr->prefs.frameInfo.contentSize > 0) << 3) 76327b27ec6Sopenharmony_ci + ((cctxPtr->prefs.frameInfo.contentChecksumFlag & _1BIT ) << 2) 76427b27ec6Sopenharmony_ci + (cctxPtr->prefs.frameInfo.dictID > 0) ); 76527b27ec6Sopenharmony_ci /* BD Byte */ 76627b27ec6Sopenharmony_ci *dstPtr++ = (BYTE)((cctxPtr->prefs.frameInfo.blockSizeID & _3BITS) << 4); 76727b27ec6Sopenharmony_ci /* Optional Frame content size field */ 76827b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.contentSize) { 76927b27ec6Sopenharmony_ci LZ4F_writeLE64(dstPtr, cctxPtr->prefs.frameInfo.contentSize); 77027b27ec6Sopenharmony_ci dstPtr += 8; 77127b27ec6Sopenharmony_ci cctxPtr->totalInSize = 0; 77227b27ec6Sopenharmony_ci } 77327b27ec6Sopenharmony_ci /* Optional dictionary ID field */ 77427b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.dictID) { 77527b27ec6Sopenharmony_ci LZ4F_writeLE32(dstPtr, cctxPtr->prefs.frameInfo.dictID); 77627b27ec6Sopenharmony_ci dstPtr += 4; 77727b27ec6Sopenharmony_ci } 77827b27ec6Sopenharmony_ci /* Header CRC Byte */ 77927b27ec6Sopenharmony_ci *dstPtr = LZ4F_headerChecksum(headerStart, (size_t)(dstPtr - headerStart)); 78027b27ec6Sopenharmony_ci dstPtr++; 78127b27ec6Sopenharmony_ci } 78227b27ec6Sopenharmony_ci 78327b27ec6Sopenharmony_ci cctxPtr->cStage = 1; /* header written, now request input data block */ 78427b27ec6Sopenharmony_ci return (size_t)(dstPtr - dstStart); 78527b27ec6Sopenharmony_ci} 78627b27ec6Sopenharmony_ci 78727b27ec6Sopenharmony_ci 78827b27ec6Sopenharmony_ci/*! LZ4F_compressBegin() : 78927b27ec6Sopenharmony_ci * init streaming compression AND writes frame header into @dstBuffer. 79027b27ec6Sopenharmony_ci * @dstCapacity must be >= LZ4F_HEADER_SIZE_MAX bytes. 79127b27ec6Sopenharmony_ci * @preferencesPtr can be NULL, in which case default parameters are selected. 79227b27ec6Sopenharmony_ci * @return : number of bytes written into dstBuffer for the header 79327b27ec6Sopenharmony_ci * or an error code (can be tested using LZ4F_isError()) 79427b27ec6Sopenharmony_ci */ 79527b27ec6Sopenharmony_cisize_t LZ4F_compressBegin(LZ4F_cctx* cctxPtr, 79627b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 79727b27ec6Sopenharmony_ci const LZ4F_preferences_t* preferencesPtr) 79827b27ec6Sopenharmony_ci{ 79927b27ec6Sopenharmony_ci return LZ4F_compressBegin_usingCDict(cctxPtr, dstBuffer, dstCapacity, 80027b27ec6Sopenharmony_ci NULL, preferencesPtr); 80127b27ec6Sopenharmony_ci} 80227b27ec6Sopenharmony_ci 80327b27ec6Sopenharmony_ci 80427b27ec6Sopenharmony_ci/* LZ4F_compressBound() : 80527b27ec6Sopenharmony_ci * @return minimum capacity of dstBuffer for a given srcSize to handle worst case scenario. 80627b27ec6Sopenharmony_ci * LZ4F_preferences_t structure is optional : if NULL, preferences will be set to cover worst case scenario. 80727b27ec6Sopenharmony_ci * This function cannot fail. 80827b27ec6Sopenharmony_ci */ 80927b27ec6Sopenharmony_cisize_t LZ4F_compressBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) 81027b27ec6Sopenharmony_ci{ 81127b27ec6Sopenharmony_ci if (preferencesPtr && preferencesPtr->autoFlush) { 81227b27ec6Sopenharmony_ci return LZ4F_compressBound_internal(srcSize, preferencesPtr, 0); 81327b27ec6Sopenharmony_ci } 81427b27ec6Sopenharmony_ci return LZ4F_compressBound_internal(srcSize, preferencesPtr, (size_t)-1); 81527b27ec6Sopenharmony_ci} 81627b27ec6Sopenharmony_ci 81727b27ec6Sopenharmony_ci 81827b27ec6Sopenharmony_citypedef int (*compressFunc_t)(void* ctx, const char* src, char* dst, int srcSize, int dstSize, int level, const LZ4F_CDict* cdict); 81927b27ec6Sopenharmony_ci 82027b27ec6Sopenharmony_ci 82127b27ec6Sopenharmony_ci/*! LZ4F_makeBlock(): 82227b27ec6Sopenharmony_ci * compress a single block, add header and optional checksum. 82327b27ec6Sopenharmony_ci * assumption : dst buffer capacity is >= BHSize + srcSize + crcSize 82427b27ec6Sopenharmony_ci */ 82527b27ec6Sopenharmony_cistatic size_t LZ4F_makeBlock(void* dst, 82627b27ec6Sopenharmony_ci const void* src, size_t srcSize, 82727b27ec6Sopenharmony_ci compressFunc_t compress, void* lz4ctx, int level, 82827b27ec6Sopenharmony_ci const LZ4F_CDict* cdict, 82927b27ec6Sopenharmony_ci LZ4F_blockChecksum_t crcFlag) 83027b27ec6Sopenharmony_ci{ 83127b27ec6Sopenharmony_ci BYTE* const cSizePtr = (BYTE*)dst; 83227b27ec6Sopenharmony_ci U32 cSize; 83327b27ec6Sopenharmony_ci assert(compress != NULL); 83427b27ec6Sopenharmony_ci cSize = (U32)compress(lz4ctx, (const char*)src, (char*)(cSizePtr+BHSize), 83527b27ec6Sopenharmony_ci (int)(srcSize), (int)(srcSize-1), 83627b27ec6Sopenharmony_ci level, cdict); 83727b27ec6Sopenharmony_ci 83827b27ec6Sopenharmony_ci if (cSize == 0 || cSize >= srcSize) { 83927b27ec6Sopenharmony_ci cSize = (U32)srcSize; 84027b27ec6Sopenharmony_ci LZ4F_writeLE32(cSizePtr, cSize | LZ4F_BLOCKUNCOMPRESSED_FLAG); 84127b27ec6Sopenharmony_ci memcpy(cSizePtr+BHSize, src, srcSize); 84227b27ec6Sopenharmony_ci } else { 84327b27ec6Sopenharmony_ci LZ4F_writeLE32(cSizePtr, cSize); 84427b27ec6Sopenharmony_ci } 84527b27ec6Sopenharmony_ci if (crcFlag) { 84627b27ec6Sopenharmony_ci U32 const crc32 = XXH32(cSizePtr+BHSize, cSize, 0); /* checksum of compressed data */ 84727b27ec6Sopenharmony_ci LZ4F_writeLE32(cSizePtr+BHSize+cSize, crc32); 84827b27ec6Sopenharmony_ci } 84927b27ec6Sopenharmony_ci return BHSize + cSize + ((U32)crcFlag)*BFSize; 85027b27ec6Sopenharmony_ci} 85127b27ec6Sopenharmony_ci 85227b27ec6Sopenharmony_ci 85327b27ec6Sopenharmony_cistatic int LZ4F_compressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict) 85427b27ec6Sopenharmony_ci{ 85527b27ec6Sopenharmony_ci int const acceleration = (level < 0) ? -level + 1 : 1; 85627b27ec6Sopenharmony_ci DEBUGLOG(5, "LZ4F_compressBlock (srcSize=%i)", srcSize); 85727b27ec6Sopenharmony_ci LZ4F_initStream(ctx, cdict, level, LZ4F_blockIndependent); 85827b27ec6Sopenharmony_ci if (cdict) { 85927b27ec6Sopenharmony_ci return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration); 86027b27ec6Sopenharmony_ci } else { 86127b27ec6Sopenharmony_ci return LZ4_compress_fast_extState_fastReset(ctx, src, dst, srcSize, dstCapacity, acceleration); 86227b27ec6Sopenharmony_ci } 86327b27ec6Sopenharmony_ci} 86427b27ec6Sopenharmony_ci 86527b27ec6Sopenharmony_cistatic int LZ4F_compressBlock_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict) 86627b27ec6Sopenharmony_ci{ 86727b27ec6Sopenharmony_ci int const acceleration = (level < 0) ? -level + 1 : 1; 86827b27ec6Sopenharmony_ci (void)cdict; /* init once at beginning of frame */ 86927b27ec6Sopenharmony_ci DEBUGLOG(5, "LZ4F_compressBlock_continue (srcSize=%i)", srcSize); 87027b27ec6Sopenharmony_ci return LZ4_compress_fast_continue((LZ4_stream_t*)ctx, src, dst, srcSize, dstCapacity, acceleration); 87127b27ec6Sopenharmony_ci} 87227b27ec6Sopenharmony_ci 87327b27ec6Sopenharmony_cistatic int LZ4F_compressBlockHC(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict) 87427b27ec6Sopenharmony_ci{ 87527b27ec6Sopenharmony_ci LZ4F_initStream(ctx, cdict, level, LZ4F_blockIndependent); 87627b27ec6Sopenharmony_ci if (cdict) { 87727b27ec6Sopenharmony_ci return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity); 87827b27ec6Sopenharmony_ci } 87927b27ec6Sopenharmony_ci return LZ4_compress_HC_extStateHC_fastReset(ctx, src, dst, srcSize, dstCapacity, level); 88027b27ec6Sopenharmony_ci} 88127b27ec6Sopenharmony_ci 88227b27ec6Sopenharmony_cistatic int LZ4F_compressBlockHC_continue(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict) 88327b27ec6Sopenharmony_ci{ 88427b27ec6Sopenharmony_ci (void)level; (void)cdict; /* init once at beginning of frame */ 88527b27ec6Sopenharmony_ci return LZ4_compress_HC_continue((LZ4_streamHC_t*)ctx, src, dst, srcSize, dstCapacity); 88627b27ec6Sopenharmony_ci} 88727b27ec6Sopenharmony_ci 88827b27ec6Sopenharmony_cistatic int LZ4F_doNotCompressBlock(void* ctx, const char* src, char* dst, int srcSize, int dstCapacity, int level, const LZ4F_CDict* cdict) 88927b27ec6Sopenharmony_ci{ 89027b27ec6Sopenharmony_ci (void)ctx; (void)src; (void)dst; (void)srcSize; (void)dstCapacity; (void)level; (void)cdict; 89127b27ec6Sopenharmony_ci return 0; 89227b27ec6Sopenharmony_ci} 89327b27ec6Sopenharmony_ci 89427b27ec6Sopenharmony_cistatic compressFunc_t LZ4F_selectCompression(LZ4F_blockMode_t blockMode, int level, LZ4F_blockCompression_t compressMode) 89527b27ec6Sopenharmony_ci{ 89627b27ec6Sopenharmony_ci if (compressMode == LZ4B_UNCOMPRESSED) return LZ4F_doNotCompressBlock; 89727b27ec6Sopenharmony_ci if (level < LZ4HC_CLEVEL_MIN) { 89827b27ec6Sopenharmony_ci if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlock; 89927b27ec6Sopenharmony_ci return LZ4F_compressBlock_continue; 90027b27ec6Sopenharmony_ci } 90127b27ec6Sopenharmony_ci if (blockMode == LZ4F_blockIndependent) return LZ4F_compressBlockHC; 90227b27ec6Sopenharmony_ci return LZ4F_compressBlockHC_continue; 90327b27ec6Sopenharmony_ci} 90427b27ec6Sopenharmony_ci 90527b27ec6Sopenharmony_ci/* Save history (up to 64KB) into @tmpBuff */ 90627b27ec6Sopenharmony_cistatic int LZ4F_localSaveDict(LZ4F_cctx_t* cctxPtr) 90727b27ec6Sopenharmony_ci{ 90827b27ec6Sopenharmony_ci if (cctxPtr->prefs.compressionLevel < LZ4HC_CLEVEL_MIN) 90927b27ec6Sopenharmony_ci return LZ4_saveDict ((LZ4_stream_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); 91027b27ec6Sopenharmony_ci return LZ4_saveDictHC ((LZ4_streamHC_t*)(cctxPtr->lz4CtxPtr), (char*)(cctxPtr->tmpBuff), 64 KB); 91127b27ec6Sopenharmony_ci} 91227b27ec6Sopenharmony_ci 91327b27ec6Sopenharmony_citypedef enum { notDone, fromTmpBuffer, fromSrcBuffer } LZ4F_lastBlockStatus; 91427b27ec6Sopenharmony_ci 91527b27ec6Sopenharmony_cistatic const LZ4F_compressOptions_t k_cOptionsNull = { 0, { 0, 0, 0 } }; 91627b27ec6Sopenharmony_ci 91727b27ec6Sopenharmony_ci 91827b27ec6Sopenharmony_ci /*! LZ4F_compressUpdateImpl() : 91927b27ec6Sopenharmony_ci * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. 92027b27ec6Sopenharmony_ci * When successful, the function always entirely consumes @srcBuffer. 92127b27ec6Sopenharmony_ci * src data is either buffered or compressed into @dstBuffer. 92227b27ec6Sopenharmony_ci * If the block compression does not match the compression of the previous block, the old data is flushed 92327b27ec6Sopenharmony_ci * and operations continue with the new compression mode. 92427b27ec6Sopenharmony_ci * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr) when block compression is turned on. 92527b27ec6Sopenharmony_ci * @compressOptionsPtr is optional : provide NULL to mean "default". 92627b27ec6Sopenharmony_ci * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered. 92727b27ec6Sopenharmony_ci * or an error code if it fails (which can be tested using LZ4F_isError()) 92827b27ec6Sopenharmony_ci * After an error, the state is left in a UB state, and must be re-initialized. 92927b27ec6Sopenharmony_ci */ 93027b27ec6Sopenharmony_cistatic size_t LZ4F_compressUpdateImpl(LZ4F_cctx* cctxPtr, 93127b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 93227b27ec6Sopenharmony_ci const void* srcBuffer, size_t srcSize, 93327b27ec6Sopenharmony_ci const LZ4F_compressOptions_t* compressOptionsPtr, 93427b27ec6Sopenharmony_ci LZ4F_blockCompression_t blockCompression) 93527b27ec6Sopenharmony_ci { 93627b27ec6Sopenharmony_ci size_t const blockSize = cctxPtr->maxBlockSize; 93727b27ec6Sopenharmony_ci const BYTE* srcPtr = (const BYTE*)srcBuffer; 93827b27ec6Sopenharmony_ci const BYTE* const srcEnd = srcPtr + srcSize; 93927b27ec6Sopenharmony_ci BYTE* const dstStart = (BYTE*)dstBuffer; 94027b27ec6Sopenharmony_ci BYTE* dstPtr = dstStart; 94127b27ec6Sopenharmony_ci LZ4F_lastBlockStatus lastBlockCompressed = notDone; 94227b27ec6Sopenharmony_ci compressFunc_t const compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, blockCompression); 94327b27ec6Sopenharmony_ci size_t bytesWritten; 94427b27ec6Sopenharmony_ci DEBUGLOG(4, "LZ4F_compressUpdate (srcSize=%zu)", srcSize); 94527b27ec6Sopenharmony_ci 94627b27ec6Sopenharmony_ci RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized); /* state must be initialized and waiting for next block */ 94727b27ec6Sopenharmony_ci if (dstCapacity < LZ4F_compressBound_internal(srcSize, &(cctxPtr->prefs), cctxPtr->tmpInSize)) 94827b27ec6Sopenharmony_ci RETURN_ERROR(dstMaxSize_tooSmall); 94927b27ec6Sopenharmony_ci 95027b27ec6Sopenharmony_ci if (blockCompression == LZ4B_UNCOMPRESSED && dstCapacity < srcSize) 95127b27ec6Sopenharmony_ci RETURN_ERROR(dstMaxSize_tooSmall); 95227b27ec6Sopenharmony_ci 95327b27ec6Sopenharmony_ci /* flush currently written block, to continue with new block compression */ 95427b27ec6Sopenharmony_ci if (cctxPtr->blockCompression != blockCompression) { 95527b27ec6Sopenharmony_ci bytesWritten = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); 95627b27ec6Sopenharmony_ci dstPtr += bytesWritten; 95727b27ec6Sopenharmony_ci cctxPtr->blockCompression = blockCompression; 95827b27ec6Sopenharmony_ci } 95927b27ec6Sopenharmony_ci 96027b27ec6Sopenharmony_ci if (compressOptionsPtr == NULL) compressOptionsPtr = &k_cOptionsNull; 96127b27ec6Sopenharmony_ci 96227b27ec6Sopenharmony_ci /* complete tmp buffer */ 96327b27ec6Sopenharmony_ci if (cctxPtr->tmpInSize > 0) { /* some data already within tmp buffer */ 96427b27ec6Sopenharmony_ci size_t const sizeToCopy = blockSize - cctxPtr->tmpInSize; 96527b27ec6Sopenharmony_ci assert(blockSize > cctxPtr->tmpInSize); 96627b27ec6Sopenharmony_ci if (sizeToCopy > srcSize) { 96727b27ec6Sopenharmony_ci /* add src to tmpIn buffer */ 96827b27ec6Sopenharmony_ci memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, srcSize); 96927b27ec6Sopenharmony_ci srcPtr = srcEnd; 97027b27ec6Sopenharmony_ci cctxPtr->tmpInSize += srcSize; 97127b27ec6Sopenharmony_ci /* still needs some CRC */ 97227b27ec6Sopenharmony_ci } else { 97327b27ec6Sopenharmony_ci /* complete tmpIn block and then compress it */ 97427b27ec6Sopenharmony_ci lastBlockCompressed = fromTmpBuffer; 97527b27ec6Sopenharmony_ci memcpy(cctxPtr->tmpIn + cctxPtr->tmpInSize, srcBuffer, sizeToCopy); 97627b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 97727b27ec6Sopenharmony_ci 97827b27ec6Sopenharmony_ci dstPtr += LZ4F_makeBlock(dstPtr, 97927b27ec6Sopenharmony_ci cctxPtr->tmpIn, blockSize, 98027b27ec6Sopenharmony_ci compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, 98127b27ec6Sopenharmony_ci cctxPtr->cdict, 98227b27ec6Sopenharmony_ci cctxPtr->prefs.frameInfo.blockChecksumFlag); 98327b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) cctxPtr->tmpIn += blockSize; 98427b27ec6Sopenharmony_ci cctxPtr->tmpInSize = 0; 98527b27ec6Sopenharmony_ci } } 98627b27ec6Sopenharmony_ci 98727b27ec6Sopenharmony_ci while ((size_t)(srcEnd - srcPtr) >= blockSize) { 98827b27ec6Sopenharmony_ci /* compress full blocks */ 98927b27ec6Sopenharmony_ci lastBlockCompressed = fromSrcBuffer; 99027b27ec6Sopenharmony_ci dstPtr += LZ4F_makeBlock(dstPtr, 99127b27ec6Sopenharmony_ci srcPtr, blockSize, 99227b27ec6Sopenharmony_ci compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, 99327b27ec6Sopenharmony_ci cctxPtr->cdict, 99427b27ec6Sopenharmony_ci cctxPtr->prefs.frameInfo.blockChecksumFlag); 99527b27ec6Sopenharmony_ci srcPtr += blockSize; 99627b27ec6Sopenharmony_ci } 99727b27ec6Sopenharmony_ci 99827b27ec6Sopenharmony_ci if ((cctxPtr->prefs.autoFlush) && (srcPtr < srcEnd)) { 99927b27ec6Sopenharmony_ci /* autoFlush : remaining input (< blockSize) is compressed */ 100027b27ec6Sopenharmony_ci lastBlockCompressed = fromSrcBuffer; 100127b27ec6Sopenharmony_ci dstPtr += LZ4F_makeBlock(dstPtr, 100227b27ec6Sopenharmony_ci srcPtr, (size_t)(srcEnd - srcPtr), 100327b27ec6Sopenharmony_ci compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, 100427b27ec6Sopenharmony_ci cctxPtr->cdict, 100527b27ec6Sopenharmony_ci cctxPtr->prefs.frameInfo.blockChecksumFlag); 100627b27ec6Sopenharmony_ci srcPtr = srcEnd; 100727b27ec6Sopenharmony_ci } 100827b27ec6Sopenharmony_ci 100927b27ec6Sopenharmony_ci /* preserve dictionary within @tmpBuff whenever necessary */ 101027b27ec6Sopenharmony_ci if ((cctxPtr->prefs.frameInfo.blockMode==LZ4F_blockLinked) && (lastBlockCompressed==fromSrcBuffer)) { 101127b27ec6Sopenharmony_ci /* linked blocks are only supported in compressed mode, see LZ4F_uncompressedUpdate */ 101227b27ec6Sopenharmony_ci assert(blockCompression == LZ4B_COMPRESSED); 101327b27ec6Sopenharmony_ci if (compressOptionsPtr->stableSrc) { 101427b27ec6Sopenharmony_ci cctxPtr->tmpIn = cctxPtr->tmpBuff; /* src is stable : dictionary remains in src across invocations */ 101527b27ec6Sopenharmony_ci } else { 101627b27ec6Sopenharmony_ci int const realDictSize = LZ4F_localSaveDict(cctxPtr); 101727b27ec6Sopenharmony_ci assert(0 <= realDictSize && realDictSize <= 64 KB); 101827b27ec6Sopenharmony_ci cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; 101927b27ec6Sopenharmony_ci } 102027b27ec6Sopenharmony_ci } 102127b27ec6Sopenharmony_ci 102227b27ec6Sopenharmony_ci /* keep tmpIn within limits */ 102327b27ec6Sopenharmony_ci if (!(cctxPtr->prefs.autoFlush) /* no autoflush : there may be some data left within internal buffer */ 102427b27ec6Sopenharmony_ci && (cctxPtr->tmpIn + blockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize) ) /* not enough room to store next block */ 102527b27ec6Sopenharmony_ci { 102627b27ec6Sopenharmony_ci /* only preserve 64KB within internal buffer. Ensures there is enough room for next block. 102727b27ec6Sopenharmony_ci * note: this situation necessarily implies lastBlockCompressed==fromTmpBuffer */ 102827b27ec6Sopenharmony_ci int const realDictSize = LZ4F_localSaveDict(cctxPtr); 102927b27ec6Sopenharmony_ci cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; 103027b27ec6Sopenharmony_ci assert((cctxPtr->tmpIn + blockSize) <= (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)); 103127b27ec6Sopenharmony_ci } 103227b27ec6Sopenharmony_ci 103327b27ec6Sopenharmony_ci /* some input data left, necessarily < blockSize */ 103427b27ec6Sopenharmony_ci if (srcPtr < srcEnd) { 103527b27ec6Sopenharmony_ci /* fill tmp buffer */ 103627b27ec6Sopenharmony_ci size_t const sizeToCopy = (size_t)(srcEnd - srcPtr); 103727b27ec6Sopenharmony_ci memcpy(cctxPtr->tmpIn, srcPtr, sizeToCopy); 103827b27ec6Sopenharmony_ci cctxPtr->tmpInSize = sizeToCopy; 103927b27ec6Sopenharmony_ci } 104027b27ec6Sopenharmony_ci 104127b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) 104227b27ec6Sopenharmony_ci (void)XXH32_update(&(cctxPtr->xxh), srcBuffer, srcSize); 104327b27ec6Sopenharmony_ci 104427b27ec6Sopenharmony_ci cctxPtr->totalInSize += srcSize; 104527b27ec6Sopenharmony_ci return (size_t)(dstPtr - dstStart); 104627b27ec6Sopenharmony_ci} 104727b27ec6Sopenharmony_ci 104827b27ec6Sopenharmony_ci/*! LZ4F_compressUpdate() : 104927b27ec6Sopenharmony_ci * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. 105027b27ec6Sopenharmony_ci * When successful, the function always entirely consumes @srcBuffer. 105127b27ec6Sopenharmony_ci * src data is either buffered or compressed into @dstBuffer. 105227b27ec6Sopenharmony_ci * If previously an uncompressed block was written, buffered data is flushed 105327b27ec6Sopenharmony_ci * before appending compressed data is continued. 105427b27ec6Sopenharmony_ci * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr). 105527b27ec6Sopenharmony_ci * @compressOptionsPtr is optional : provide NULL to mean "default". 105627b27ec6Sopenharmony_ci * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered. 105727b27ec6Sopenharmony_ci * or an error code if it fails (which can be tested using LZ4F_isError()) 105827b27ec6Sopenharmony_ci * After an error, the state is left in a UB state, and must be re-initialized. 105927b27ec6Sopenharmony_ci */ 106027b27ec6Sopenharmony_cisize_t LZ4F_compressUpdate(LZ4F_cctx* cctxPtr, 106127b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 106227b27ec6Sopenharmony_ci const void* srcBuffer, size_t srcSize, 106327b27ec6Sopenharmony_ci const LZ4F_compressOptions_t* compressOptionsPtr) 106427b27ec6Sopenharmony_ci{ 106527b27ec6Sopenharmony_ci return LZ4F_compressUpdateImpl(cctxPtr, 106627b27ec6Sopenharmony_ci dstBuffer, dstCapacity, 106727b27ec6Sopenharmony_ci srcBuffer, srcSize, 106827b27ec6Sopenharmony_ci compressOptionsPtr, LZ4B_COMPRESSED); 106927b27ec6Sopenharmony_ci} 107027b27ec6Sopenharmony_ci 107127b27ec6Sopenharmony_ci/*! LZ4F_compressUpdate() : 107227b27ec6Sopenharmony_ci * LZ4F_compressUpdate() can be called repetitively to compress as much data as necessary. 107327b27ec6Sopenharmony_ci * When successful, the function always entirely consumes @srcBuffer. 107427b27ec6Sopenharmony_ci * src data is either buffered or compressed into @dstBuffer. 107527b27ec6Sopenharmony_ci * If previously an uncompressed block was written, buffered data is flushed 107627b27ec6Sopenharmony_ci * before appending compressed data is continued. 107727b27ec6Sopenharmony_ci * This is only supported when LZ4F_blockIndependent is used 107827b27ec6Sopenharmony_ci * @dstCapacity MUST be >= LZ4F_compressBound(srcSize, preferencesPtr). 107927b27ec6Sopenharmony_ci * @compressOptionsPtr is optional : provide NULL to mean "default". 108027b27ec6Sopenharmony_ci * @return : the number of bytes written into dstBuffer. It can be zero, meaning input data was just buffered. 108127b27ec6Sopenharmony_ci * or an error code if it fails (which can be tested using LZ4F_isError()) 108227b27ec6Sopenharmony_ci * After an error, the state is left in a UB state, and must be re-initialized. 108327b27ec6Sopenharmony_ci */ 108427b27ec6Sopenharmony_cisize_t LZ4F_uncompressedUpdate(LZ4F_cctx* cctxPtr, 108527b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 108627b27ec6Sopenharmony_ci const void* srcBuffer, size_t srcSize, 108727b27ec6Sopenharmony_ci const LZ4F_compressOptions_t* compressOptionsPtr) { 108827b27ec6Sopenharmony_ci RETURN_ERROR_IF(cctxPtr->prefs.frameInfo.blockMode != LZ4F_blockIndependent, blockMode_invalid); 108927b27ec6Sopenharmony_ci return LZ4F_compressUpdateImpl(cctxPtr, 109027b27ec6Sopenharmony_ci dstBuffer, dstCapacity, 109127b27ec6Sopenharmony_ci srcBuffer, srcSize, 109227b27ec6Sopenharmony_ci compressOptionsPtr, LZ4B_UNCOMPRESSED); 109327b27ec6Sopenharmony_ci} 109427b27ec6Sopenharmony_ci 109527b27ec6Sopenharmony_ci 109627b27ec6Sopenharmony_ci/*! LZ4F_flush() : 109727b27ec6Sopenharmony_ci * When compressed data must be sent immediately, without waiting for a block to be filled, 109827b27ec6Sopenharmony_ci * invoke LZ4_flush(), which will immediately compress any remaining data stored within LZ4F_cctx. 109927b27ec6Sopenharmony_ci * The result of the function is the number of bytes written into dstBuffer. 110027b27ec6Sopenharmony_ci * It can be zero, this means there was no data left within LZ4F_cctx. 110127b27ec6Sopenharmony_ci * The function outputs an error code if it fails (can be tested using LZ4F_isError()) 110227b27ec6Sopenharmony_ci * LZ4F_compressOptions_t* is optional. NULL is a valid argument. 110327b27ec6Sopenharmony_ci */ 110427b27ec6Sopenharmony_cisize_t LZ4F_flush(LZ4F_cctx* cctxPtr, 110527b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 110627b27ec6Sopenharmony_ci const LZ4F_compressOptions_t* compressOptionsPtr) 110727b27ec6Sopenharmony_ci{ 110827b27ec6Sopenharmony_ci BYTE* const dstStart = (BYTE*)dstBuffer; 110927b27ec6Sopenharmony_ci BYTE* dstPtr = dstStart; 111027b27ec6Sopenharmony_ci compressFunc_t compress; 111127b27ec6Sopenharmony_ci 111227b27ec6Sopenharmony_ci if (cctxPtr->tmpInSize == 0) return 0; /* nothing to flush */ 111327b27ec6Sopenharmony_ci RETURN_ERROR_IF(cctxPtr->cStage != 1, compressionState_uninitialized); 111427b27ec6Sopenharmony_ci RETURN_ERROR_IF(dstCapacity < (cctxPtr->tmpInSize + BHSize + BFSize), dstMaxSize_tooSmall); 111527b27ec6Sopenharmony_ci (void)compressOptionsPtr; /* not useful (yet) */ 111627b27ec6Sopenharmony_ci 111727b27ec6Sopenharmony_ci /* select compression function */ 111827b27ec6Sopenharmony_ci compress = LZ4F_selectCompression(cctxPtr->prefs.frameInfo.blockMode, cctxPtr->prefs.compressionLevel, cctxPtr->blockCompression); 111927b27ec6Sopenharmony_ci 112027b27ec6Sopenharmony_ci /* compress tmp buffer */ 112127b27ec6Sopenharmony_ci dstPtr += LZ4F_makeBlock(dstPtr, 112227b27ec6Sopenharmony_ci cctxPtr->tmpIn, cctxPtr->tmpInSize, 112327b27ec6Sopenharmony_ci compress, cctxPtr->lz4CtxPtr, cctxPtr->prefs.compressionLevel, 112427b27ec6Sopenharmony_ci cctxPtr->cdict, 112527b27ec6Sopenharmony_ci cctxPtr->prefs.frameInfo.blockChecksumFlag); 112627b27ec6Sopenharmony_ci assert(((void)"flush overflows dstBuffer!", (size_t)(dstPtr - dstStart) <= dstCapacity)); 112727b27ec6Sopenharmony_ci 112827b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.blockMode == LZ4F_blockLinked) 112927b27ec6Sopenharmony_ci cctxPtr->tmpIn += cctxPtr->tmpInSize; 113027b27ec6Sopenharmony_ci cctxPtr->tmpInSize = 0; 113127b27ec6Sopenharmony_ci 113227b27ec6Sopenharmony_ci /* keep tmpIn within limits */ 113327b27ec6Sopenharmony_ci if ((cctxPtr->tmpIn + cctxPtr->maxBlockSize) > (cctxPtr->tmpBuff + cctxPtr->maxBufferSize)) { /* necessarily LZ4F_blockLinked */ 113427b27ec6Sopenharmony_ci int const realDictSize = LZ4F_localSaveDict(cctxPtr); 113527b27ec6Sopenharmony_ci cctxPtr->tmpIn = cctxPtr->tmpBuff + realDictSize; 113627b27ec6Sopenharmony_ci } 113727b27ec6Sopenharmony_ci 113827b27ec6Sopenharmony_ci return (size_t)(dstPtr - dstStart); 113927b27ec6Sopenharmony_ci} 114027b27ec6Sopenharmony_ci 114127b27ec6Sopenharmony_ci 114227b27ec6Sopenharmony_ci/*! LZ4F_compressEnd() : 114327b27ec6Sopenharmony_ci * When you want to properly finish the compressed frame, just call LZ4F_compressEnd(). 114427b27ec6Sopenharmony_ci * It will flush whatever data remained within compressionContext (like LZ4_flush()) 114527b27ec6Sopenharmony_ci * but also properly finalize the frame, with an endMark and an (optional) checksum. 114627b27ec6Sopenharmony_ci * LZ4F_compressOptions_t structure is optional : you can provide NULL as argument. 114727b27ec6Sopenharmony_ci * @return: the number of bytes written into dstBuffer (necessarily >= 4 (endMark size)) 114827b27ec6Sopenharmony_ci * or an error code if it fails (can be tested using LZ4F_isError()) 114927b27ec6Sopenharmony_ci * The context can then be used again to compress a new frame, starting with LZ4F_compressBegin(). 115027b27ec6Sopenharmony_ci */ 115127b27ec6Sopenharmony_cisize_t LZ4F_compressEnd(LZ4F_cctx* cctxPtr, 115227b27ec6Sopenharmony_ci void* dstBuffer, size_t dstCapacity, 115327b27ec6Sopenharmony_ci const LZ4F_compressOptions_t* compressOptionsPtr) 115427b27ec6Sopenharmony_ci{ 115527b27ec6Sopenharmony_ci BYTE* const dstStart = (BYTE*)dstBuffer; 115627b27ec6Sopenharmony_ci BYTE* dstPtr = dstStart; 115727b27ec6Sopenharmony_ci 115827b27ec6Sopenharmony_ci size_t const flushSize = LZ4F_flush(cctxPtr, dstBuffer, dstCapacity, compressOptionsPtr); 115927b27ec6Sopenharmony_ci DEBUGLOG(5,"LZ4F_compressEnd: dstCapacity=%u", (unsigned)dstCapacity); 116027b27ec6Sopenharmony_ci FORWARD_IF_ERROR(flushSize); 116127b27ec6Sopenharmony_ci dstPtr += flushSize; 116227b27ec6Sopenharmony_ci 116327b27ec6Sopenharmony_ci assert(flushSize <= dstCapacity); 116427b27ec6Sopenharmony_ci dstCapacity -= flushSize; 116527b27ec6Sopenharmony_ci 116627b27ec6Sopenharmony_ci RETURN_ERROR_IF(dstCapacity < 4, dstMaxSize_tooSmall); 116727b27ec6Sopenharmony_ci LZ4F_writeLE32(dstPtr, 0); 116827b27ec6Sopenharmony_ci dstPtr += 4; /* endMark */ 116927b27ec6Sopenharmony_ci 117027b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.contentChecksumFlag == LZ4F_contentChecksumEnabled) { 117127b27ec6Sopenharmony_ci U32 const xxh = XXH32_digest(&(cctxPtr->xxh)); 117227b27ec6Sopenharmony_ci RETURN_ERROR_IF(dstCapacity < 8, dstMaxSize_tooSmall); 117327b27ec6Sopenharmony_ci DEBUGLOG(5,"Writing 32-bit content checksum"); 117427b27ec6Sopenharmony_ci LZ4F_writeLE32(dstPtr, xxh); 117527b27ec6Sopenharmony_ci dstPtr+=4; /* content Checksum */ 117627b27ec6Sopenharmony_ci } 117727b27ec6Sopenharmony_ci 117827b27ec6Sopenharmony_ci cctxPtr->cStage = 0; /* state is now re-usable (with identical preferences) */ 117927b27ec6Sopenharmony_ci cctxPtr->maxBufferSize = 0; /* reuse HC context */ 118027b27ec6Sopenharmony_ci 118127b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.contentSize) { 118227b27ec6Sopenharmony_ci if (cctxPtr->prefs.frameInfo.contentSize != cctxPtr->totalInSize) 118327b27ec6Sopenharmony_ci RETURN_ERROR(frameSize_wrong); 118427b27ec6Sopenharmony_ci } 118527b27ec6Sopenharmony_ci 118627b27ec6Sopenharmony_ci return (size_t)(dstPtr - dstStart); 118727b27ec6Sopenharmony_ci} 118827b27ec6Sopenharmony_ci 118927b27ec6Sopenharmony_ci 119027b27ec6Sopenharmony_ci/*-*************************************************** 119127b27ec6Sopenharmony_ci* Frame Decompression 119227b27ec6Sopenharmony_ci*****************************************************/ 119327b27ec6Sopenharmony_ci 119427b27ec6Sopenharmony_citypedef enum { 119527b27ec6Sopenharmony_ci dstage_getFrameHeader=0, dstage_storeFrameHeader, 119627b27ec6Sopenharmony_ci dstage_init, 119727b27ec6Sopenharmony_ci dstage_getBlockHeader, dstage_storeBlockHeader, 119827b27ec6Sopenharmony_ci dstage_copyDirect, dstage_getBlockChecksum, 119927b27ec6Sopenharmony_ci dstage_getCBlock, dstage_storeCBlock, 120027b27ec6Sopenharmony_ci dstage_flushOut, 120127b27ec6Sopenharmony_ci dstage_getSuffix, dstage_storeSuffix, 120227b27ec6Sopenharmony_ci dstage_getSFrameSize, dstage_storeSFrameSize, 120327b27ec6Sopenharmony_ci dstage_skipSkippable 120427b27ec6Sopenharmony_ci} dStage_t; 120527b27ec6Sopenharmony_ci 120627b27ec6Sopenharmony_cistruct LZ4F_dctx_s { 120727b27ec6Sopenharmony_ci LZ4F_CustomMem cmem; 120827b27ec6Sopenharmony_ci LZ4F_frameInfo_t frameInfo; 120927b27ec6Sopenharmony_ci U32 version; 121027b27ec6Sopenharmony_ci dStage_t dStage; 121127b27ec6Sopenharmony_ci U64 frameRemainingSize; 121227b27ec6Sopenharmony_ci size_t maxBlockSize; 121327b27ec6Sopenharmony_ci size_t maxBufferSize; 121427b27ec6Sopenharmony_ci BYTE* tmpIn; 121527b27ec6Sopenharmony_ci size_t tmpInSize; 121627b27ec6Sopenharmony_ci size_t tmpInTarget; 121727b27ec6Sopenharmony_ci BYTE* tmpOutBuffer; 121827b27ec6Sopenharmony_ci const BYTE* dict; 121927b27ec6Sopenharmony_ci size_t dictSize; 122027b27ec6Sopenharmony_ci BYTE* tmpOut; 122127b27ec6Sopenharmony_ci size_t tmpOutSize; 122227b27ec6Sopenharmony_ci size_t tmpOutStart; 122327b27ec6Sopenharmony_ci XXH32_state_t xxh; 122427b27ec6Sopenharmony_ci XXH32_state_t blockChecksum; 122527b27ec6Sopenharmony_ci int skipChecksum; 122627b27ec6Sopenharmony_ci BYTE header[LZ4F_HEADER_SIZE_MAX]; 122727b27ec6Sopenharmony_ci}; /* typedef'd to LZ4F_dctx in lz4frame.h */ 122827b27ec6Sopenharmony_ci 122927b27ec6Sopenharmony_ci 123027b27ec6Sopenharmony_ciLZ4F_dctx* LZ4F_createDecompressionContext_advanced(LZ4F_CustomMem customMem, unsigned version) 123127b27ec6Sopenharmony_ci{ 123227b27ec6Sopenharmony_ci LZ4F_dctx* const dctx = (LZ4F_dctx*)LZ4F_calloc(sizeof(LZ4F_dctx), customMem); 123327b27ec6Sopenharmony_ci if (dctx == NULL) return NULL; 123427b27ec6Sopenharmony_ci 123527b27ec6Sopenharmony_ci dctx->cmem = customMem; 123627b27ec6Sopenharmony_ci dctx->version = version; 123727b27ec6Sopenharmony_ci return dctx; 123827b27ec6Sopenharmony_ci} 123927b27ec6Sopenharmony_ci 124027b27ec6Sopenharmony_ci/*! LZ4F_createDecompressionContext() : 124127b27ec6Sopenharmony_ci * Create a decompressionContext object, which will track all decompression operations. 124227b27ec6Sopenharmony_ci * Provides a pointer to a fully allocated and initialized LZ4F_decompressionContext object. 124327b27ec6Sopenharmony_ci * Object can later be released using LZ4F_freeDecompressionContext(). 124427b27ec6Sopenharmony_ci * @return : if != 0, there was an error during context creation. 124527b27ec6Sopenharmony_ci */ 124627b27ec6Sopenharmony_ciLZ4F_errorCode_t 124727b27ec6Sopenharmony_ciLZ4F_createDecompressionContext(LZ4F_dctx** LZ4F_decompressionContextPtr, unsigned versionNumber) 124827b27ec6Sopenharmony_ci{ 124927b27ec6Sopenharmony_ci assert(LZ4F_decompressionContextPtr != NULL); /* violation of narrow contract */ 125027b27ec6Sopenharmony_ci RETURN_ERROR_IF(LZ4F_decompressionContextPtr == NULL, parameter_null); /* in case it nonetheless happen in production */ 125127b27ec6Sopenharmony_ci 125227b27ec6Sopenharmony_ci *LZ4F_decompressionContextPtr = LZ4F_createDecompressionContext_advanced(LZ4F_defaultCMem, versionNumber); 125327b27ec6Sopenharmony_ci if (*LZ4F_decompressionContextPtr == NULL) { /* failed allocation */ 125427b27ec6Sopenharmony_ci RETURN_ERROR(allocation_failed); 125527b27ec6Sopenharmony_ci } 125627b27ec6Sopenharmony_ci return LZ4F_OK_NoError; 125727b27ec6Sopenharmony_ci} 125827b27ec6Sopenharmony_ci 125927b27ec6Sopenharmony_ciLZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx) 126027b27ec6Sopenharmony_ci{ 126127b27ec6Sopenharmony_ci LZ4F_errorCode_t result = LZ4F_OK_NoError; 126227b27ec6Sopenharmony_ci if (dctx != NULL) { /* can accept NULL input, like free() */ 126327b27ec6Sopenharmony_ci result = (LZ4F_errorCode_t)dctx->dStage; 126427b27ec6Sopenharmony_ci LZ4F_free(dctx->tmpIn, dctx->cmem); 126527b27ec6Sopenharmony_ci LZ4F_free(dctx->tmpOutBuffer, dctx->cmem); 126627b27ec6Sopenharmony_ci LZ4F_free(dctx, dctx->cmem); 126727b27ec6Sopenharmony_ci } 126827b27ec6Sopenharmony_ci return result; 126927b27ec6Sopenharmony_ci} 127027b27ec6Sopenharmony_ci 127127b27ec6Sopenharmony_ci 127227b27ec6Sopenharmony_ci/*==--- Streaming Decompression operations ---==*/ 127327b27ec6Sopenharmony_ci 127427b27ec6Sopenharmony_civoid LZ4F_resetDecompressionContext(LZ4F_dctx* dctx) 127527b27ec6Sopenharmony_ci{ 127627b27ec6Sopenharmony_ci dctx->dStage = dstage_getFrameHeader; 127727b27ec6Sopenharmony_ci dctx->dict = NULL; 127827b27ec6Sopenharmony_ci dctx->dictSize = 0; 127927b27ec6Sopenharmony_ci dctx->skipChecksum = 0; 128027b27ec6Sopenharmony_ci} 128127b27ec6Sopenharmony_ci 128227b27ec6Sopenharmony_ci 128327b27ec6Sopenharmony_ci/*! LZ4F_decodeHeader() : 128427b27ec6Sopenharmony_ci * input : `src` points at the **beginning of the frame** 128527b27ec6Sopenharmony_ci * output : set internal values of dctx, such as 128627b27ec6Sopenharmony_ci * dctx->frameInfo and dctx->dStage. 128727b27ec6Sopenharmony_ci * Also allocates internal buffers. 128827b27ec6Sopenharmony_ci * @return : nb Bytes read from src (necessarily <= srcSize) 128927b27ec6Sopenharmony_ci * or an error code (testable with LZ4F_isError()) 129027b27ec6Sopenharmony_ci */ 129127b27ec6Sopenharmony_cistatic size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize) 129227b27ec6Sopenharmony_ci{ 129327b27ec6Sopenharmony_ci unsigned blockMode, blockChecksumFlag, contentSizeFlag, contentChecksumFlag, dictIDFlag, blockSizeID; 129427b27ec6Sopenharmony_ci size_t frameHeaderSize; 129527b27ec6Sopenharmony_ci const BYTE* srcPtr = (const BYTE*)src; 129627b27ec6Sopenharmony_ci 129727b27ec6Sopenharmony_ci DEBUGLOG(5, "LZ4F_decodeHeader"); 129827b27ec6Sopenharmony_ci /* need to decode header to get frameInfo */ 129927b27ec6Sopenharmony_ci RETURN_ERROR_IF(srcSize < minFHSize, frameHeader_incomplete); /* minimal frame header size */ 130027b27ec6Sopenharmony_ci MEM_INIT(&(dctx->frameInfo), 0, sizeof(dctx->frameInfo)); 130127b27ec6Sopenharmony_ci 130227b27ec6Sopenharmony_ci /* special case : skippable frames */ 130327b27ec6Sopenharmony_ci if ((LZ4F_readLE32(srcPtr) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) { 130427b27ec6Sopenharmony_ci dctx->frameInfo.frameType = LZ4F_skippableFrame; 130527b27ec6Sopenharmony_ci if (src == (void*)(dctx->header)) { 130627b27ec6Sopenharmony_ci dctx->tmpInSize = srcSize; 130727b27ec6Sopenharmony_ci dctx->tmpInTarget = 8; 130827b27ec6Sopenharmony_ci dctx->dStage = dstage_storeSFrameSize; 130927b27ec6Sopenharmony_ci return srcSize; 131027b27ec6Sopenharmony_ci } else { 131127b27ec6Sopenharmony_ci dctx->dStage = dstage_getSFrameSize; 131227b27ec6Sopenharmony_ci return 4; 131327b27ec6Sopenharmony_ci } } 131427b27ec6Sopenharmony_ci 131527b27ec6Sopenharmony_ci /* control magic number */ 131627b27ec6Sopenharmony_ci#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 131727b27ec6Sopenharmony_ci if (LZ4F_readLE32(srcPtr) != LZ4F_MAGICNUMBER) { 131827b27ec6Sopenharmony_ci DEBUGLOG(4, "frame header error : unknown magic number"); 131927b27ec6Sopenharmony_ci RETURN_ERROR(frameType_unknown); 132027b27ec6Sopenharmony_ci } 132127b27ec6Sopenharmony_ci#endif 132227b27ec6Sopenharmony_ci dctx->frameInfo.frameType = LZ4F_frame; 132327b27ec6Sopenharmony_ci 132427b27ec6Sopenharmony_ci /* Flags */ 132527b27ec6Sopenharmony_ci { U32 const FLG = srcPtr[4]; 132627b27ec6Sopenharmony_ci U32 const version = (FLG>>6) & _2BITS; 132727b27ec6Sopenharmony_ci blockChecksumFlag = (FLG>>4) & _1BIT; 132827b27ec6Sopenharmony_ci blockMode = (FLG>>5) & _1BIT; 132927b27ec6Sopenharmony_ci contentSizeFlag = (FLG>>3) & _1BIT; 133027b27ec6Sopenharmony_ci contentChecksumFlag = (FLG>>2) & _1BIT; 133127b27ec6Sopenharmony_ci dictIDFlag = FLG & _1BIT; 133227b27ec6Sopenharmony_ci /* validate */ 133327b27ec6Sopenharmony_ci if (((FLG>>1)&_1BIT) != 0) RETURN_ERROR(reservedFlag_set); /* Reserved bit */ 133427b27ec6Sopenharmony_ci if (version != 1) RETURN_ERROR(headerVersion_wrong); /* Version Number, only supported value */ 133527b27ec6Sopenharmony_ci } 133627b27ec6Sopenharmony_ci 133727b27ec6Sopenharmony_ci /* Frame Header Size */ 133827b27ec6Sopenharmony_ci frameHeaderSize = minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0); 133927b27ec6Sopenharmony_ci 134027b27ec6Sopenharmony_ci if (srcSize < frameHeaderSize) { 134127b27ec6Sopenharmony_ci /* not enough input to fully decode frame header */ 134227b27ec6Sopenharmony_ci if (srcPtr != dctx->header) 134327b27ec6Sopenharmony_ci memcpy(dctx->header, srcPtr, srcSize); 134427b27ec6Sopenharmony_ci dctx->tmpInSize = srcSize; 134527b27ec6Sopenharmony_ci dctx->tmpInTarget = frameHeaderSize; 134627b27ec6Sopenharmony_ci dctx->dStage = dstage_storeFrameHeader; 134727b27ec6Sopenharmony_ci return srcSize; 134827b27ec6Sopenharmony_ci } 134927b27ec6Sopenharmony_ci 135027b27ec6Sopenharmony_ci { U32 const BD = srcPtr[5]; 135127b27ec6Sopenharmony_ci blockSizeID = (BD>>4) & _3BITS; 135227b27ec6Sopenharmony_ci /* validate */ 135327b27ec6Sopenharmony_ci if (((BD>>7)&_1BIT) != 0) RETURN_ERROR(reservedFlag_set); /* Reserved bit */ 135427b27ec6Sopenharmony_ci if (blockSizeID < 4) RETURN_ERROR(maxBlockSize_invalid); /* 4-7 only supported values for the time being */ 135527b27ec6Sopenharmony_ci if (((BD>>0)&_4BITS) != 0) RETURN_ERROR(reservedFlag_set); /* Reserved bits */ 135627b27ec6Sopenharmony_ci } 135727b27ec6Sopenharmony_ci 135827b27ec6Sopenharmony_ci /* check header */ 135927b27ec6Sopenharmony_ci assert(frameHeaderSize > 5); 136027b27ec6Sopenharmony_ci#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 136127b27ec6Sopenharmony_ci { BYTE const HC = LZ4F_headerChecksum(srcPtr+4, frameHeaderSize-5); 136227b27ec6Sopenharmony_ci RETURN_ERROR_IF(HC != srcPtr[frameHeaderSize-1], headerChecksum_invalid); 136327b27ec6Sopenharmony_ci } 136427b27ec6Sopenharmony_ci#endif 136527b27ec6Sopenharmony_ci 136627b27ec6Sopenharmony_ci /* save */ 136727b27ec6Sopenharmony_ci dctx->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode; 136827b27ec6Sopenharmony_ci dctx->frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)blockChecksumFlag; 136927b27ec6Sopenharmony_ci dctx->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag; 137027b27ec6Sopenharmony_ci dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID; 137127b27ec6Sopenharmony_ci dctx->maxBlockSize = LZ4F_getBlockSize((LZ4F_blockSizeID_t)blockSizeID); 137227b27ec6Sopenharmony_ci if (contentSizeFlag) 137327b27ec6Sopenharmony_ci dctx->frameRemainingSize = dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6); 137427b27ec6Sopenharmony_ci if (dictIDFlag) 137527b27ec6Sopenharmony_ci dctx->frameInfo.dictID = LZ4F_readLE32(srcPtr + frameHeaderSize - 5); 137627b27ec6Sopenharmony_ci 137727b27ec6Sopenharmony_ci dctx->dStage = dstage_init; 137827b27ec6Sopenharmony_ci 137927b27ec6Sopenharmony_ci return frameHeaderSize; 138027b27ec6Sopenharmony_ci} 138127b27ec6Sopenharmony_ci 138227b27ec6Sopenharmony_ci 138327b27ec6Sopenharmony_ci/*! LZ4F_headerSize() : 138427b27ec6Sopenharmony_ci * @return : size of frame header 138527b27ec6Sopenharmony_ci * or an error code, which can be tested using LZ4F_isError() 138627b27ec6Sopenharmony_ci */ 138727b27ec6Sopenharmony_cisize_t LZ4F_headerSize(const void* src, size_t srcSize) 138827b27ec6Sopenharmony_ci{ 138927b27ec6Sopenharmony_ci RETURN_ERROR_IF(src == NULL, srcPtr_wrong); 139027b27ec6Sopenharmony_ci 139127b27ec6Sopenharmony_ci /* minimal srcSize to determine header size */ 139227b27ec6Sopenharmony_ci if (srcSize < LZ4F_MIN_SIZE_TO_KNOW_HEADER_LENGTH) 139327b27ec6Sopenharmony_ci RETURN_ERROR(frameHeader_incomplete); 139427b27ec6Sopenharmony_ci 139527b27ec6Sopenharmony_ci /* special case : skippable frames */ 139627b27ec6Sopenharmony_ci if ((LZ4F_readLE32(src) & 0xFFFFFFF0U) == LZ4F_MAGIC_SKIPPABLE_START) 139727b27ec6Sopenharmony_ci return 8; 139827b27ec6Sopenharmony_ci 139927b27ec6Sopenharmony_ci /* control magic number */ 140027b27ec6Sopenharmony_ci#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 140127b27ec6Sopenharmony_ci if (LZ4F_readLE32(src) != LZ4F_MAGICNUMBER) 140227b27ec6Sopenharmony_ci RETURN_ERROR(frameType_unknown); 140327b27ec6Sopenharmony_ci#endif 140427b27ec6Sopenharmony_ci 140527b27ec6Sopenharmony_ci /* Frame Header Size */ 140627b27ec6Sopenharmony_ci { BYTE const FLG = ((const BYTE*)src)[4]; 140727b27ec6Sopenharmony_ci U32 const contentSizeFlag = (FLG>>3) & _1BIT; 140827b27ec6Sopenharmony_ci U32 const dictIDFlag = FLG & _1BIT; 140927b27ec6Sopenharmony_ci return minFHSize + (contentSizeFlag?8:0) + (dictIDFlag?4:0); 141027b27ec6Sopenharmony_ci } 141127b27ec6Sopenharmony_ci} 141227b27ec6Sopenharmony_ci 141327b27ec6Sopenharmony_ci/*! LZ4F_getFrameInfo() : 141427b27ec6Sopenharmony_ci * This function extracts frame parameters (max blockSize, frame checksum, etc.). 141527b27ec6Sopenharmony_ci * Usage is optional. Objective is to provide relevant information for allocation purposes. 141627b27ec6Sopenharmony_ci * This function works in 2 situations : 141727b27ec6Sopenharmony_ci * - At the beginning of a new frame, in which case it will decode this information from `srcBuffer`, and start the decoding process. 141827b27ec6Sopenharmony_ci * Amount of input data provided must be large enough to successfully decode the frame header. 141927b27ec6Sopenharmony_ci * A header size is variable, but is guaranteed to be <= LZ4F_HEADER_SIZE_MAX bytes. It's possible to provide more input data than this minimum. 142027b27ec6Sopenharmony_ci * - After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx. 142127b27ec6Sopenharmony_ci * The number of bytes consumed from srcBuffer will be updated within *srcSizePtr (necessarily <= original value). 142227b27ec6Sopenharmony_ci * Decompression must resume from (srcBuffer + *srcSizePtr). 142327b27ec6Sopenharmony_ci * @return : an hint about how many srcSize bytes LZ4F_decompress() expects for next call, 142427b27ec6Sopenharmony_ci * or an error code which can be tested using LZ4F_isError() 142527b27ec6Sopenharmony_ci * note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped. 142627b27ec6Sopenharmony_ci * note 2 : frame parameters are *copied into* an already allocated LZ4F_frameInfo_t structure. 142727b27ec6Sopenharmony_ci */ 142827b27ec6Sopenharmony_ciLZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, 142927b27ec6Sopenharmony_ci LZ4F_frameInfo_t* frameInfoPtr, 143027b27ec6Sopenharmony_ci const void* srcBuffer, size_t* srcSizePtr) 143127b27ec6Sopenharmony_ci{ 143227b27ec6Sopenharmony_ci LZ4F_STATIC_ASSERT(dstage_getFrameHeader < dstage_storeFrameHeader); 143327b27ec6Sopenharmony_ci if (dctx->dStage > dstage_storeFrameHeader) { 143427b27ec6Sopenharmony_ci /* frameInfo already decoded */ 143527b27ec6Sopenharmony_ci size_t o=0, i=0; 143627b27ec6Sopenharmony_ci *srcSizePtr = 0; 143727b27ec6Sopenharmony_ci *frameInfoPtr = dctx->frameInfo; 143827b27ec6Sopenharmony_ci /* returns : recommended nb of bytes for LZ4F_decompress() */ 143927b27ec6Sopenharmony_ci return LZ4F_decompress(dctx, NULL, &o, NULL, &i, NULL); 144027b27ec6Sopenharmony_ci } else { 144127b27ec6Sopenharmony_ci if (dctx->dStage == dstage_storeFrameHeader) { 144227b27ec6Sopenharmony_ci /* frame decoding already started, in the middle of header => automatic fail */ 144327b27ec6Sopenharmony_ci *srcSizePtr = 0; 144427b27ec6Sopenharmony_ci RETURN_ERROR(frameDecoding_alreadyStarted); 144527b27ec6Sopenharmony_ci } else { 144627b27ec6Sopenharmony_ci size_t const hSize = LZ4F_headerSize(srcBuffer, *srcSizePtr); 144727b27ec6Sopenharmony_ci if (LZ4F_isError(hSize)) { *srcSizePtr=0; return hSize; } 144827b27ec6Sopenharmony_ci if (*srcSizePtr < hSize) { 144927b27ec6Sopenharmony_ci *srcSizePtr=0; 145027b27ec6Sopenharmony_ci RETURN_ERROR(frameHeader_incomplete); 145127b27ec6Sopenharmony_ci } 145227b27ec6Sopenharmony_ci 145327b27ec6Sopenharmony_ci { size_t decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize); 145427b27ec6Sopenharmony_ci if (LZ4F_isError(decodeResult)) { 145527b27ec6Sopenharmony_ci *srcSizePtr = 0; 145627b27ec6Sopenharmony_ci } else { 145727b27ec6Sopenharmony_ci *srcSizePtr = decodeResult; 145827b27ec6Sopenharmony_ci decodeResult = BHSize; /* block header size */ 145927b27ec6Sopenharmony_ci } 146027b27ec6Sopenharmony_ci *frameInfoPtr = dctx->frameInfo; 146127b27ec6Sopenharmony_ci return decodeResult; 146227b27ec6Sopenharmony_ci } } } 146327b27ec6Sopenharmony_ci} 146427b27ec6Sopenharmony_ci 146527b27ec6Sopenharmony_ci 146627b27ec6Sopenharmony_ci/* LZ4F_updateDict() : 146727b27ec6Sopenharmony_ci * only used for LZ4F_blockLinked mode 146827b27ec6Sopenharmony_ci * Condition : @dstPtr != NULL 146927b27ec6Sopenharmony_ci */ 147027b27ec6Sopenharmony_cistatic void LZ4F_updateDict(LZ4F_dctx* dctx, 147127b27ec6Sopenharmony_ci const BYTE* dstPtr, size_t dstSize, const BYTE* dstBufferStart, 147227b27ec6Sopenharmony_ci unsigned withinTmp) 147327b27ec6Sopenharmony_ci{ 147427b27ec6Sopenharmony_ci assert(dstPtr != NULL); 147527b27ec6Sopenharmony_ci if (dctx->dictSize==0) dctx->dict = (const BYTE*)dstPtr; /* will lead to prefix mode */ 147627b27ec6Sopenharmony_ci assert(dctx->dict != NULL); 147727b27ec6Sopenharmony_ci 147827b27ec6Sopenharmony_ci if (dctx->dict + dctx->dictSize == dstPtr) { /* prefix mode, everything within dstBuffer */ 147927b27ec6Sopenharmony_ci dctx->dictSize += dstSize; 148027b27ec6Sopenharmony_ci return; 148127b27ec6Sopenharmony_ci } 148227b27ec6Sopenharmony_ci 148327b27ec6Sopenharmony_ci assert(dstPtr >= dstBufferStart); 148427b27ec6Sopenharmony_ci if ((size_t)(dstPtr - dstBufferStart) + dstSize >= 64 KB) { /* history in dstBuffer becomes large enough to become dictionary */ 148527b27ec6Sopenharmony_ci dctx->dict = (const BYTE*)dstBufferStart; 148627b27ec6Sopenharmony_ci dctx->dictSize = (size_t)(dstPtr - dstBufferStart) + dstSize; 148727b27ec6Sopenharmony_ci return; 148827b27ec6Sopenharmony_ci } 148927b27ec6Sopenharmony_ci 149027b27ec6Sopenharmony_ci assert(dstSize < 64 KB); /* if dstSize >= 64 KB, dictionary would be set into dstBuffer directly */ 149127b27ec6Sopenharmony_ci 149227b27ec6Sopenharmony_ci /* dstBuffer does not contain whole useful history (64 KB), so it must be saved within tmpOutBuffer */ 149327b27ec6Sopenharmony_ci assert(dctx->tmpOutBuffer != NULL); 149427b27ec6Sopenharmony_ci 149527b27ec6Sopenharmony_ci if (withinTmp && (dctx->dict == dctx->tmpOutBuffer)) { /* continue history within tmpOutBuffer */ 149627b27ec6Sopenharmony_ci /* withinTmp expectation : content of [dstPtr,dstSize] is same as [dict+dictSize,dstSize], so we just extend it */ 149727b27ec6Sopenharmony_ci assert(dctx->dict + dctx->dictSize == dctx->tmpOut + dctx->tmpOutStart); 149827b27ec6Sopenharmony_ci dctx->dictSize += dstSize; 149927b27ec6Sopenharmony_ci return; 150027b27ec6Sopenharmony_ci } 150127b27ec6Sopenharmony_ci 150227b27ec6Sopenharmony_ci if (withinTmp) { /* copy relevant dict portion in front of tmpOut within tmpOutBuffer */ 150327b27ec6Sopenharmony_ci size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer); 150427b27ec6Sopenharmony_ci size_t copySize = 64 KB - dctx->tmpOutSize; 150527b27ec6Sopenharmony_ci const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart; 150627b27ec6Sopenharmony_ci if (dctx->tmpOutSize > 64 KB) copySize = 0; 150727b27ec6Sopenharmony_ci if (copySize > preserveSize) copySize = preserveSize; 150827b27ec6Sopenharmony_ci 150927b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); 151027b27ec6Sopenharmony_ci 151127b27ec6Sopenharmony_ci dctx->dict = dctx->tmpOutBuffer; 151227b27ec6Sopenharmony_ci dctx->dictSize = preserveSize + dctx->tmpOutStart + dstSize; 151327b27ec6Sopenharmony_ci return; 151427b27ec6Sopenharmony_ci } 151527b27ec6Sopenharmony_ci 151627b27ec6Sopenharmony_ci if (dctx->dict == dctx->tmpOutBuffer) { /* copy dst into tmp to complete dict */ 151727b27ec6Sopenharmony_ci if (dctx->dictSize + dstSize > dctx->maxBufferSize) { /* tmp buffer not large enough */ 151827b27ec6Sopenharmony_ci size_t const preserveSize = 64 KB - dstSize; 151927b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize); 152027b27ec6Sopenharmony_ci dctx->dictSize = preserveSize; 152127b27ec6Sopenharmony_ci } 152227b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer + dctx->dictSize, dstPtr, dstSize); 152327b27ec6Sopenharmony_ci dctx->dictSize += dstSize; 152427b27ec6Sopenharmony_ci return; 152527b27ec6Sopenharmony_ci } 152627b27ec6Sopenharmony_ci 152727b27ec6Sopenharmony_ci /* join dict & dest into tmp */ 152827b27ec6Sopenharmony_ci { size_t preserveSize = 64 KB - dstSize; 152927b27ec6Sopenharmony_ci if (preserveSize > dctx->dictSize) preserveSize = dctx->dictSize; 153027b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize); 153127b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer + preserveSize, dstPtr, dstSize); 153227b27ec6Sopenharmony_ci dctx->dict = dctx->tmpOutBuffer; 153327b27ec6Sopenharmony_ci dctx->dictSize = preserveSize + dstSize; 153427b27ec6Sopenharmony_ci } 153527b27ec6Sopenharmony_ci} 153627b27ec6Sopenharmony_ci 153727b27ec6Sopenharmony_ci 153827b27ec6Sopenharmony_ci/*! LZ4F_decompress() : 153927b27ec6Sopenharmony_ci * Call this function repetitively to regenerate compressed data in srcBuffer. 154027b27ec6Sopenharmony_ci * The function will attempt to decode up to *srcSizePtr bytes from srcBuffer 154127b27ec6Sopenharmony_ci * into dstBuffer of capacity *dstSizePtr. 154227b27ec6Sopenharmony_ci * 154327b27ec6Sopenharmony_ci * The number of bytes regenerated into dstBuffer will be provided within *dstSizePtr (necessarily <= original value). 154427b27ec6Sopenharmony_ci * 154527b27ec6Sopenharmony_ci * The number of bytes effectively read from srcBuffer will be provided within *srcSizePtr (necessarily <= original value). 154627b27ec6Sopenharmony_ci * If number of bytes read is < number of bytes provided, then decompression operation is not complete. 154727b27ec6Sopenharmony_ci * Remaining data will have to be presented again in a subsequent invocation. 154827b27ec6Sopenharmony_ci * 154927b27ec6Sopenharmony_ci * The function result is an hint of the better srcSize to use for next call to LZ4F_decompress. 155027b27ec6Sopenharmony_ci * Schematically, it's the size of the current (or remaining) compressed block + header of next block. 155127b27ec6Sopenharmony_ci * Respecting the hint provides a small boost to performance, since it allows less buffer shuffling. 155227b27ec6Sopenharmony_ci * Note that this is just a hint, and it's always possible to any srcSize value. 155327b27ec6Sopenharmony_ci * When a frame is fully decoded, @return will be 0. 155427b27ec6Sopenharmony_ci * If decompression failed, @return is an error code which can be tested using LZ4F_isError(). 155527b27ec6Sopenharmony_ci */ 155627b27ec6Sopenharmony_cisize_t LZ4F_decompress(LZ4F_dctx* dctx, 155727b27ec6Sopenharmony_ci void* dstBuffer, size_t* dstSizePtr, 155827b27ec6Sopenharmony_ci const void* srcBuffer, size_t* srcSizePtr, 155927b27ec6Sopenharmony_ci const LZ4F_decompressOptions_t* decompressOptionsPtr) 156027b27ec6Sopenharmony_ci{ 156127b27ec6Sopenharmony_ci LZ4F_decompressOptions_t optionsNull; 156227b27ec6Sopenharmony_ci const BYTE* const srcStart = (const BYTE*)srcBuffer; 156327b27ec6Sopenharmony_ci const BYTE* const srcEnd = srcStart + *srcSizePtr; 156427b27ec6Sopenharmony_ci const BYTE* srcPtr = srcStart; 156527b27ec6Sopenharmony_ci BYTE* const dstStart = (BYTE*)dstBuffer; 156627b27ec6Sopenharmony_ci BYTE* const dstEnd = dstStart ? dstStart + *dstSizePtr : NULL; 156727b27ec6Sopenharmony_ci BYTE* dstPtr = dstStart; 156827b27ec6Sopenharmony_ci const BYTE* selectedIn = NULL; 156927b27ec6Sopenharmony_ci unsigned doAnotherStage = 1; 157027b27ec6Sopenharmony_ci size_t nextSrcSizeHint = 1; 157127b27ec6Sopenharmony_ci 157227b27ec6Sopenharmony_ci 157327b27ec6Sopenharmony_ci DEBUGLOG(5, "LZ4F_decompress : %p,%u => %p,%u", 157427b27ec6Sopenharmony_ci srcBuffer, (unsigned)*srcSizePtr, dstBuffer, (unsigned)*dstSizePtr); 157527b27ec6Sopenharmony_ci if (dstBuffer == NULL) assert(*dstSizePtr == 0); 157627b27ec6Sopenharmony_ci MEM_INIT(&optionsNull, 0, sizeof(optionsNull)); 157727b27ec6Sopenharmony_ci if (decompressOptionsPtr==NULL) decompressOptionsPtr = &optionsNull; 157827b27ec6Sopenharmony_ci *srcSizePtr = 0; 157927b27ec6Sopenharmony_ci *dstSizePtr = 0; 158027b27ec6Sopenharmony_ci assert(dctx != NULL); 158127b27ec6Sopenharmony_ci dctx->skipChecksum |= (decompressOptionsPtr->skipChecksums != 0); /* once set, disable for the remainder of the frame */ 158227b27ec6Sopenharmony_ci 158327b27ec6Sopenharmony_ci /* behaves as a state machine */ 158427b27ec6Sopenharmony_ci 158527b27ec6Sopenharmony_ci while (doAnotherStage) { 158627b27ec6Sopenharmony_ci 158727b27ec6Sopenharmony_ci switch(dctx->dStage) 158827b27ec6Sopenharmony_ci { 158927b27ec6Sopenharmony_ci 159027b27ec6Sopenharmony_ci case dstage_getFrameHeader: 159127b27ec6Sopenharmony_ci DEBUGLOG(6, "dstage_getFrameHeader"); 159227b27ec6Sopenharmony_ci if ((size_t)(srcEnd-srcPtr) >= maxFHSize) { /* enough to decode - shortcut */ 159327b27ec6Sopenharmony_ci size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, (size_t)(srcEnd-srcPtr)); /* will update dStage appropriately */ 159427b27ec6Sopenharmony_ci FORWARD_IF_ERROR(hSize); 159527b27ec6Sopenharmony_ci srcPtr += hSize; 159627b27ec6Sopenharmony_ci break; 159727b27ec6Sopenharmony_ci } 159827b27ec6Sopenharmony_ci dctx->tmpInSize = 0; 159927b27ec6Sopenharmony_ci if (srcEnd-srcPtr == 0) return minFHSize; /* 0-size input */ 160027b27ec6Sopenharmony_ci dctx->tmpInTarget = minFHSize; /* minimum size to decode header */ 160127b27ec6Sopenharmony_ci dctx->dStage = dstage_storeFrameHeader; 160227b27ec6Sopenharmony_ci /* fall-through */ 160327b27ec6Sopenharmony_ci 160427b27ec6Sopenharmony_ci case dstage_storeFrameHeader: 160527b27ec6Sopenharmony_ci DEBUGLOG(6, "dstage_storeFrameHeader"); 160627b27ec6Sopenharmony_ci { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, (size_t)(srcEnd - srcPtr)); 160727b27ec6Sopenharmony_ci memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy); 160827b27ec6Sopenharmony_ci dctx->tmpInSize += sizeToCopy; 160927b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 161027b27ec6Sopenharmony_ci } 161127b27ec6Sopenharmony_ci if (dctx->tmpInSize < dctx->tmpInTarget) { 161227b27ec6Sopenharmony_ci nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize; /* rest of header + nextBlockHeader */ 161327b27ec6Sopenharmony_ci doAnotherStage = 0; /* not enough src data, ask for some more */ 161427b27ec6Sopenharmony_ci break; 161527b27ec6Sopenharmony_ci } 161627b27ec6Sopenharmony_ci FORWARD_IF_ERROR( LZ4F_decodeHeader(dctx, dctx->header, dctx->tmpInTarget) ); /* will update dStage appropriately */ 161727b27ec6Sopenharmony_ci break; 161827b27ec6Sopenharmony_ci 161927b27ec6Sopenharmony_ci case dstage_init: 162027b27ec6Sopenharmony_ci DEBUGLOG(6, "dstage_init"); 162127b27ec6Sopenharmony_ci if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0); 162227b27ec6Sopenharmony_ci /* internal buffers allocation */ 162327b27ec6Sopenharmony_ci { size_t const bufferNeeded = dctx->maxBlockSize 162427b27ec6Sopenharmony_ci + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) ? 128 KB : 0); 162527b27ec6Sopenharmony_ci if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */ 162627b27ec6Sopenharmony_ci dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/ 162727b27ec6Sopenharmony_ci LZ4F_free(dctx->tmpIn, dctx->cmem); 162827b27ec6Sopenharmony_ci dctx->tmpIn = (BYTE*)LZ4F_malloc(dctx->maxBlockSize + BFSize /* block checksum */, dctx->cmem); 162927b27ec6Sopenharmony_ci RETURN_ERROR_IF(dctx->tmpIn == NULL, allocation_failed); 163027b27ec6Sopenharmony_ci LZ4F_free(dctx->tmpOutBuffer, dctx->cmem); 163127b27ec6Sopenharmony_ci dctx->tmpOutBuffer= (BYTE*)LZ4F_malloc(bufferNeeded, dctx->cmem); 163227b27ec6Sopenharmony_ci RETURN_ERROR_IF(dctx->tmpOutBuffer== NULL, allocation_failed); 163327b27ec6Sopenharmony_ci dctx->maxBufferSize = bufferNeeded; 163427b27ec6Sopenharmony_ci } } 163527b27ec6Sopenharmony_ci dctx->tmpInSize = 0; 163627b27ec6Sopenharmony_ci dctx->tmpInTarget = 0; 163727b27ec6Sopenharmony_ci dctx->tmpOut = dctx->tmpOutBuffer; 163827b27ec6Sopenharmony_ci dctx->tmpOutStart = 0; 163927b27ec6Sopenharmony_ci dctx->tmpOutSize = 0; 164027b27ec6Sopenharmony_ci 164127b27ec6Sopenharmony_ci dctx->dStage = dstage_getBlockHeader; 164227b27ec6Sopenharmony_ci /* fall-through */ 164327b27ec6Sopenharmony_ci 164427b27ec6Sopenharmony_ci case dstage_getBlockHeader: 164527b27ec6Sopenharmony_ci if ((size_t)(srcEnd - srcPtr) >= BHSize) { 164627b27ec6Sopenharmony_ci selectedIn = srcPtr; 164727b27ec6Sopenharmony_ci srcPtr += BHSize; 164827b27ec6Sopenharmony_ci } else { 164927b27ec6Sopenharmony_ci /* not enough input to read cBlockSize field */ 165027b27ec6Sopenharmony_ci dctx->tmpInSize = 0; 165127b27ec6Sopenharmony_ci dctx->dStage = dstage_storeBlockHeader; 165227b27ec6Sopenharmony_ci } 165327b27ec6Sopenharmony_ci 165427b27ec6Sopenharmony_ci if (dctx->dStage == dstage_storeBlockHeader) /* can be skipped */ 165527b27ec6Sopenharmony_ci case dstage_storeBlockHeader: 165627b27ec6Sopenharmony_ci { size_t const remainingInput = (size_t)(srcEnd - srcPtr); 165727b27ec6Sopenharmony_ci size_t const wantedData = BHSize - dctx->tmpInSize; 165827b27ec6Sopenharmony_ci size_t const sizeToCopy = MIN(wantedData, remainingInput); 165927b27ec6Sopenharmony_ci memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy); 166027b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 166127b27ec6Sopenharmony_ci dctx->tmpInSize += sizeToCopy; 166227b27ec6Sopenharmony_ci 166327b27ec6Sopenharmony_ci if (dctx->tmpInSize < BHSize) { /* not enough input for cBlockSize */ 166427b27ec6Sopenharmony_ci nextSrcSizeHint = BHSize - dctx->tmpInSize; 166527b27ec6Sopenharmony_ci doAnotherStage = 0; 166627b27ec6Sopenharmony_ci break; 166727b27ec6Sopenharmony_ci } 166827b27ec6Sopenharmony_ci selectedIn = dctx->tmpIn; 166927b27ec6Sopenharmony_ci } /* if (dctx->dStage == dstage_storeBlockHeader) */ 167027b27ec6Sopenharmony_ci 167127b27ec6Sopenharmony_ci /* decode block header */ 167227b27ec6Sopenharmony_ci { U32 const blockHeader = LZ4F_readLE32(selectedIn); 167327b27ec6Sopenharmony_ci size_t const nextCBlockSize = blockHeader & 0x7FFFFFFFU; 167427b27ec6Sopenharmony_ci size_t const crcSize = dctx->frameInfo.blockChecksumFlag * BFSize; 167527b27ec6Sopenharmony_ci if (blockHeader==0) { /* frameEnd signal, no more block */ 167627b27ec6Sopenharmony_ci DEBUGLOG(5, "end of frame"); 167727b27ec6Sopenharmony_ci dctx->dStage = dstage_getSuffix; 167827b27ec6Sopenharmony_ci break; 167927b27ec6Sopenharmony_ci } 168027b27ec6Sopenharmony_ci if (nextCBlockSize > dctx->maxBlockSize) { 168127b27ec6Sopenharmony_ci RETURN_ERROR(maxBlockSize_invalid); 168227b27ec6Sopenharmony_ci } 168327b27ec6Sopenharmony_ci if (blockHeader & LZ4F_BLOCKUNCOMPRESSED_FLAG) { 168427b27ec6Sopenharmony_ci /* next block is uncompressed */ 168527b27ec6Sopenharmony_ci dctx->tmpInTarget = nextCBlockSize; 168627b27ec6Sopenharmony_ci DEBUGLOG(5, "next block is uncompressed (size %u)", (U32)nextCBlockSize); 168727b27ec6Sopenharmony_ci if (dctx->frameInfo.blockChecksumFlag) { 168827b27ec6Sopenharmony_ci (void)XXH32_reset(&dctx->blockChecksum, 0); 168927b27ec6Sopenharmony_ci } 169027b27ec6Sopenharmony_ci dctx->dStage = dstage_copyDirect; 169127b27ec6Sopenharmony_ci break; 169227b27ec6Sopenharmony_ci } 169327b27ec6Sopenharmony_ci /* next block is a compressed block */ 169427b27ec6Sopenharmony_ci dctx->tmpInTarget = nextCBlockSize + crcSize; 169527b27ec6Sopenharmony_ci dctx->dStage = dstage_getCBlock; 169627b27ec6Sopenharmony_ci if (dstPtr==dstEnd || srcPtr==srcEnd) { 169727b27ec6Sopenharmony_ci nextSrcSizeHint = BHSize + nextCBlockSize + crcSize; 169827b27ec6Sopenharmony_ci doAnotherStage = 0; 169927b27ec6Sopenharmony_ci } 170027b27ec6Sopenharmony_ci break; 170127b27ec6Sopenharmony_ci } 170227b27ec6Sopenharmony_ci 170327b27ec6Sopenharmony_ci case dstage_copyDirect: /* uncompressed block */ 170427b27ec6Sopenharmony_ci DEBUGLOG(6, "dstage_copyDirect"); 170527b27ec6Sopenharmony_ci { size_t sizeToCopy; 170627b27ec6Sopenharmony_ci if (dstPtr == NULL) { 170727b27ec6Sopenharmony_ci sizeToCopy = 0; 170827b27ec6Sopenharmony_ci } else { 170927b27ec6Sopenharmony_ci size_t const minBuffSize = MIN((size_t)(srcEnd-srcPtr), (size_t)(dstEnd-dstPtr)); 171027b27ec6Sopenharmony_ci sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize); 171127b27ec6Sopenharmony_ci memcpy(dstPtr, srcPtr, sizeToCopy); 171227b27ec6Sopenharmony_ci if (!dctx->skipChecksum) { 171327b27ec6Sopenharmony_ci if (dctx->frameInfo.blockChecksumFlag) { 171427b27ec6Sopenharmony_ci (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy); 171527b27ec6Sopenharmony_ci } 171627b27ec6Sopenharmony_ci if (dctx->frameInfo.contentChecksumFlag) 171727b27ec6Sopenharmony_ci (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy); 171827b27ec6Sopenharmony_ci } 171927b27ec6Sopenharmony_ci if (dctx->frameInfo.contentSize) 172027b27ec6Sopenharmony_ci dctx->frameRemainingSize -= sizeToCopy; 172127b27ec6Sopenharmony_ci 172227b27ec6Sopenharmony_ci /* history management (linked blocks only)*/ 172327b27ec6Sopenharmony_ci if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { 172427b27ec6Sopenharmony_ci LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0); 172527b27ec6Sopenharmony_ci } } 172627b27ec6Sopenharmony_ci 172727b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 172827b27ec6Sopenharmony_ci dstPtr += sizeToCopy; 172927b27ec6Sopenharmony_ci if (sizeToCopy == dctx->tmpInTarget) { /* all done */ 173027b27ec6Sopenharmony_ci if (dctx->frameInfo.blockChecksumFlag) { 173127b27ec6Sopenharmony_ci dctx->tmpInSize = 0; 173227b27ec6Sopenharmony_ci dctx->dStage = dstage_getBlockChecksum; 173327b27ec6Sopenharmony_ci } else 173427b27ec6Sopenharmony_ci dctx->dStage = dstage_getBlockHeader; /* new block */ 173527b27ec6Sopenharmony_ci break; 173627b27ec6Sopenharmony_ci } 173727b27ec6Sopenharmony_ci dctx->tmpInTarget -= sizeToCopy; /* need to copy more */ 173827b27ec6Sopenharmony_ci } 173927b27ec6Sopenharmony_ci nextSrcSizeHint = dctx->tmpInTarget + 174027b27ec6Sopenharmony_ci +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0) 174127b27ec6Sopenharmony_ci + BHSize /* next header size */; 174227b27ec6Sopenharmony_ci doAnotherStage = 0; 174327b27ec6Sopenharmony_ci break; 174427b27ec6Sopenharmony_ci 174527b27ec6Sopenharmony_ci /* check block checksum for recently transferred uncompressed block */ 174627b27ec6Sopenharmony_ci case dstage_getBlockChecksum: 174727b27ec6Sopenharmony_ci DEBUGLOG(6, "dstage_getBlockChecksum"); 174827b27ec6Sopenharmony_ci { const void* crcSrc; 174927b27ec6Sopenharmony_ci if ((srcEnd-srcPtr >= 4) && (dctx->tmpInSize==0)) { 175027b27ec6Sopenharmony_ci crcSrc = srcPtr; 175127b27ec6Sopenharmony_ci srcPtr += 4; 175227b27ec6Sopenharmony_ci } else { 175327b27ec6Sopenharmony_ci size_t const stillToCopy = 4 - dctx->tmpInSize; 175427b27ec6Sopenharmony_ci size_t const sizeToCopy = MIN(stillToCopy, (size_t)(srcEnd-srcPtr)); 175527b27ec6Sopenharmony_ci memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy); 175627b27ec6Sopenharmony_ci dctx->tmpInSize += sizeToCopy; 175727b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 175827b27ec6Sopenharmony_ci if (dctx->tmpInSize < 4) { /* all input consumed */ 175927b27ec6Sopenharmony_ci doAnotherStage = 0; 176027b27ec6Sopenharmony_ci break; 176127b27ec6Sopenharmony_ci } 176227b27ec6Sopenharmony_ci crcSrc = dctx->header; 176327b27ec6Sopenharmony_ci } 176427b27ec6Sopenharmony_ci if (!dctx->skipChecksum) { 176527b27ec6Sopenharmony_ci U32 const readCRC = LZ4F_readLE32(crcSrc); 176627b27ec6Sopenharmony_ci U32 const calcCRC = XXH32_digest(&dctx->blockChecksum); 176727b27ec6Sopenharmony_ci#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 176827b27ec6Sopenharmony_ci DEBUGLOG(6, "compare block checksum"); 176927b27ec6Sopenharmony_ci if (readCRC != calcCRC) { 177027b27ec6Sopenharmony_ci DEBUGLOG(4, "incorrect block checksum: %08X != %08X", 177127b27ec6Sopenharmony_ci readCRC, calcCRC); 177227b27ec6Sopenharmony_ci RETURN_ERROR(blockChecksum_invalid); 177327b27ec6Sopenharmony_ci } 177427b27ec6Sopenharmony_ci#else 177527b27ec6Sopenharmony_ci (void)readCRC; 177627b27ec6Sopenharmony_ci (void)calcCRC; 177727b27ec6Sopenharmony_ci#endif 177827b27ec6Sopenharmony_ci } } 177927b27ec6Sopenharmony_ci dctx->dStage = dstage_getBlockHeader; /* new block */ 178027b27ec6Sopenharmony_ci break; 178127b27ec6Sopenharmony_ci 178227b27ec6Sopenharmony_ci case dstage_getCBlock: 178327b27ec6Sopenharmony_ci DEBUGLOG(6, "dstage_getCBlock"); 178427b27ec6Sopenharmony_ci if ((size_t)(srcEnd-srcPtr) < dctx->tmpInTarget) { 178527b27ec6Sopenharmony_ci dctx->tmpInSize = 0; 178627b27ec6Sopenharmony_ci dctx->dStage = dstage_storeCBlock; 178727b27ec6Sopenharmony_ci break; 178827b27ec6Sopenharmony_ci } 178927b27ec6Sopenharmony_ci /* input large enough to read full block directly */ 179027b27ec6Sopenharmony_ci selectedIn = srcPtr; 179127b27ec6Sopenharmony_ci srcPtr += dctx->tmpInTarget; 179227b27ec6Sopenharmony_ci 179327b27ec6Sopenharmony_ci if (0) /* always jump over next block */ 179427b27ec6Sopenharmony_ci case dstage_storeCBlock: 179527b27ec6Sopenharmony_ci { size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize; 179627b27ec6Sopenharmony_ci size_t const inputLeft = (size_t)(srcEnd-srcPtr); 179727b27ec6Sopenharmony_ci size_t const sizeToCopy = MIN(wantedData, inputLeft); 179827b27ec6Sopenharmony_ci memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy); 179927b27ec6Sopenharmony_ci dctx->tmpInSize += sizeToCopy; 180027b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 180127b27ec6Sopenharmony_ci if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */ 180227b27ec6Sopenharmony_ci nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) 180327b27ec6Sopenharmony_ci + (dctx->frameInfo.blockChecksumFlag ? BFSize : 0) 180427b27ec6Sopenharmony_ci + BHSize /* next header size */; 180527b27ec6Sopenharmony_ci doAnotherStage = 0; 180627b27ec6Sopenharmony_ci break; 180727b27ec6Sopenharmony_ci } 180827b27ec6Sopenharmony_ci selectedIn = dctx->tmpIn; 180927b27ec6Sopenharmony_ci } 181027b27ec6Sopenharmony_ci 181127b27ec6Sopenharmony_ci /* At this stage, input is large enough to decode a block */ 181227b27ec6Sopenharmony_ci 181327b27ec6Sopenharmony_ci /* First, decode and control block checksum if it exists */ 181427b27ec6Sopenharmony_ci if (dctx->frameInfo.blockChecksumFlag) { 181527b27ec6Sopenharmony_ci assert(dctx->tmpInTarget >= 4); 181627b27ec6Sopenharmony_ci dctx->tmpInTarget -= 4; 181727b27ec6Sopenharmony_ci assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */ 181827b27ec6Sopenharmony_ci { U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget); 181927b27ec6Sopenharmony_ci U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0); 182027b27ec6Sopenharmony_ci#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 182127b27ec6Sopenharmony_ci RETURN_ERROR_IF(readBlockCrc != calcBlockCrc, blockChecksum_invalid); 182227b27ec6Sopenharmony_ci#else 182327b27ec6Sopenharmony_ci (void)readBlockCrc; 182427b27ec6Sopenharmony_ci (void)calcBlockCrc; 182527b27ec6Sopenharmony_ci#endif 182627b27ec6Sopenharmony_ci } } 182727b27ec6Sopenharmony_ci 182827b27ec6Sopenharmony_ci /* decode directly into destination buffer if there is enough room */ 182927b27ec6Sopenharmony_ci if ( ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) 183027b27ec6Sopenharmony_ci /* unless the dictionary is stored in tmpOut: 183127b27ec6Sopenharmony_ci * in which case it's faster to decode within tmpOut 183227b27ec6Sopenharmony_ci * to benefit from prefix speedup */ 183327b27ec6Sopenharmony_ci && !(dctx->dict!= NULL && (const BYTE*)dctx->dict + dctx->dictSize == dctx->tmpOut) ) 183427b27ec6Sopenharmony_ci { 183527b27ec6Sopenharmony_ci const char* dict = (const char*)dctx->dict; 183627b27ec6Sopenharmony_ci size_t dictSize = dctx->dictSize; 183727b27ec6Sopenharmony_ci int decodedSize; 183827b27ec6Sopenharmony_ci assert(dstPtr != NULL); 183927b27ec6Sopenharmony_ci if (dict && dictSize > 1 GB) { 184027b27ec6Sopenharmony_ci /* overflow control : dctx->dictSize is an int, avoid truncation / sign issues */ 184127b27ec6Sopenharmony_ci dict += dictSize - 64 KB; 184227b27ec6Sopenharmony_ci dictSize = 64 KB; 184327b27ec6Sopenharmony_ci } 184427b27ec6Sopenharmony_ci decodedSize = LZ4_decompress_safe_usingDict( 184527b27ec6Sopenharmony_ci (const char*)selectedIn, (char*)dstPtr, 184627b27ec6Sopenharmony_ci (int)dctx->tmpInTarget, (int)dctx->maxBlockSize, 184727b27ec6Sopenharmony_ci dict, (int)dictSize); 184827b27ec6Sopenharmony_ci RETURN_ERROR_IF(decodedSize < 0, decompressionFailed); 184927b27ec6Sopenharmony_ci if ((dctx->frameInfo.contentChecksumFlag) && (!dctx->skipChecksum)) 185027b27ec6Sopenharmony_ci XXH32_update(&(dctx->xxh), dstPtr, (size_t)decodedSize); 185127b27ec6Sopenharmony_ci if (dctx->frameInfo.contentSize) 185227b27ec6Sopenharmony_ci dctx->frameRemainingSize -= (size_t)decodedSize; 185327b27ec6Sopenharmony_ci 185427b27ec6Sopenharmony_ci /* dictionary management */ 185527b27ec6Sopenharmony_ci if (dctx->frameInfo.blockMode==LZ4F_blockLinked) { 185627b27ec6Sopenharmony_ci LZ4F_updateDict(dctx, dstPtr, (size_t)decodedSize, dstStart, 0); 185727b27ec6Sopenharmony_ci } 185827b27ec6Sopenharmony_ci 185927b27ec6Sopenharmony_ci dstPtr += decodedSize; 186027b27ec6Sopenharmony_ci dctx->dStage = dstage_getBlockHeader; /* end of block, let's get another one */ 186127b27ec6Sopenharmony_ci break; 186227b27ec6Sopenharmony_ci } 186327b27ec6Sopenharmony_ci 186427b27ec6Sopenharmony_ci /* not enough place into dst : decode into tmpOut */ 186527b27ec6Sopenharmony_ci 186627b27ec6Sopenharmony_ci /* manage dictionary */ 186727b27ec6Sopenharmony_ci if (dctx->frameInfo.blockMode == LZ4F_blockLinked) { 186827b27ec6Sopenharmony_ci if (dctx->dict == dctx->tmpOutBuffer) { 186927b27ec6Sopenharmony_ci /* truncate dictionary to 64 KB if too big */ 187027b27ec6Sopenharmony_ci if (dctx->dictSize > 128 KB) { 187127b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB); 187227b27ec6Sopenharmony_ci dctx->dictSize = 64 KB; 187327b27ec6Sopenharmony_ci } 187427b27ec6Sopenharmony_ci dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize; 187527b27ec6Sopenharmony_ci } else { /* dict not within tmpOut */ 187627b27ec6Sopenharmony_ci size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB); 187727b27ec6Sopenharmony_ci dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace; 187827b27ec6Sopenharmony_ci } } 187927b27ec6Sopenharmony_ci 188027b27ec6Sopenharmony_ci /* Decode block into tmpOut */ 188127b27ec6Sopenharmony_ci { const char* dict = (const char*)dctx->dict; 188227b27ec6Sopenharmony_ci size_t dictSize = dctx->dictSize; 188327b27ec6Sopenharmony_ci int decodedSize; 188427b27ec6Sopenharmony_ci if (dict && dictSize > 1 GB) { 188527b27ec6Sopenharmony_ci /* the dictSize param is an int, avoid truncation / sign issues */ 188627b27ec6Sopenharmony_ci dict += dictSize - 64 KB; 188727b27ec6Sopenharmony_ci dictSize = 64 KB; 188827b27ec6Sopenharmony_ci } 188927b27ec6Sopenharmony_ci decodedSize = LZ4_decompress_safe_usingDict( 189027b27ec6Sopenharmony_ci (const char*)selectedIn, (char*)dctx->tmpOut, 189127b27ec6Sopenharmony_ci (int)dctx->tmpInTarget, (int)dctx->maxBlockSize, 189227b27ec6Sopenharmony_ci dict, (int)dictSize); 189327b27ec6Sopenharmony_ci RETURN_ERROR_IF(decodedSize < 0, decompressionFailed); 189427b27ec6Sopenharmony_ci if (dctx->frameInfo.contentChecksumFlag && !dctx->skipChecksum) 189527b27ec6Sopenharmony_ci XXH32_update(&(dctx->xxh), dctx->tmpOut, (size_t)decodedSize); 189627b27ec6Sopenharmony_ci if (dctx->frameInfo.contentSize) 189727b27ec6Sopenharmony_ci dctx->frameRemainingSize -= (size_t)decodedSize; 189827b27ec6Sopenharmony_ci dctx->tmpOutSize = (size_t)decodedSize; 189927b27ec6Sopenharmony_ci dctx->tmpOutStart = 0; 190027b27ec6Sopenharmony_ci dctx->dStage = dstage_flushOut; 190127b27ec6Sopenharmony_ci } 190227b27ec6Sopenharmony_ci /* fall-through */ 190327b27ec6Sopenharmony_ci 190427b27ec6Sopenharmony_ci case dstage_flushOut: /* flush decoded data from tmpOut to dstBuffer */ 190527b27ec6Sopenharmony_ci DEBUGLOG(6, "dstage_flushOut"); 190627b27ec6Sopenharmony_ci if (dstPtr != NULL) { 190727b27ec6Sopenharmony_ci size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr)); 190827b27ec6Sopenharmony_ci memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy); 190927b27ec6Sopenharmony_ci 191027b27ec6Sopenharmony_ci /* dictionary management */ 191127b27ec6Sopenharmony_ci if (dctx->frameInfo.blockMode == LZ4F_blockLinked) 191227b27ec6Sopenharmony_ci LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 1 /*withinTmp*/); 191327b27ec6Sopenharmony_ci 191427b27ec6Sopenharmony_ci dctx->tmpOutStart += sizeToCopy; 191527b27ec6Sopenharmony_ci dstPtr += sizeToCopy; 191627b27ec6Sopenharmony_ci } 191727b27ec6Sopenharmony_ci if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */ 191827b27ec6Sopenharmony_ci dctx->dStage = dstage_getBlockHeader; /* get next block */ 191927b27ec6Sopenharmony_ci break; 192027b27ec6Sopenharmony_ci } 192127b27ec6Sopenharmony_ci /* could not flush everything : stop there, just request a block header */ 192227b27ec6Sopenharmony_ci doAnotherStage = 0; 192327b27ec6Sopenharmony_ci nextSrcSizeHint = BHSize; 192427b27ec6Sopenharmony_ci break; 192527b27ec6Sopenharmony_ci 192627b27ec6Sopenharmony_ci case dstage_getSuffix: 192727b27ec6Sopenharmony_ci RETURN_ERROR_IF(dctx->frameRemainingSize, frameSize_wrong); /* incorrect frame size decoded */ 192827b27ec6Sopenharmony_ci if (!dctx->frameInfo.contentChecksumFlag) { /* no checksum, frame is completed */ 192927b27ec6Sopenharmony_ci nextSrcSizeHint = 0; 193027b27ec6Sopenharmony_ci LZ4F_resetDecompressionContext(dctx); 193127b27ec6Sopenharmony_ci doAnotherStage = 0; 193227b27ec6Sopenharmony_ci break; 193327b27ec6Sopenharmony_ci } 193427b27ec6Sopenharmony_ci if ((srcEnd - srcPtr) < 4) { /* not enough size for entire CRC */ 193527b27ec6Sopenharmony_ci dctx->tmpInSize = 0; 193627b27ec6Sopenharmony_ci dctx->dStage = dstage_storeSuffix; 193727b27ec6Sopenharmony_ci } else { 193827b27ec6Sopenharmony_ci selectedIn = srcPtr; 193927b27ec6Sopenharmony_ci srcPtr += 4; 194027b27ec6Sopenharmony_ci } 194127b27ec6Sopenharmony_ci 194227b27ec6Sopenharmony_ci if (dctx->dStage == dstage_storeSuffix) /* can be skipped */ 194327b27ec6Sopenharmony_ci case dstage_storeSuffix: 194427b27ec6Sopenharmony_ci { size_t const remainingInput = (size_t)(srcEnd - srcPtr); 194527b27ec6Sopenharmony_ci size_t const wantedData = 4 - dctx->tmpInSize; 194627b27ec6Sopenharmony_ci size_t const sizeToCopy = MIN(wantedData, remainingInput); 194727b27ec6Sopenharmony_ci memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy); 194827b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 194927b27ec6Sopenharmony_ci dctx->tmpInSize += sizeToCopy; 195027b27ec6Sopenharmony_ci if (dctx->tmpInSize < 4) { /* not enough input to read complete suffix */ 195127b27ec6Sopenharmony_ci nextSrcSizeHint = 4 - dctx->tmpInSize; 195227b27ec6Sopenharmony_ci doAnotherStage=0; 195327b27ec6Sopenharmony_ci break; 195427b27ec6Sopenharmony_ci } 195527b27ec6Sopenharmony_ci selectedIn = dctx->tmpIn; 195627b27ec6Sopenharmony_ci } /* if (dctx->dStage == dstage_storeSuffix) */ 195727b27ec6Sopenharmony_ci 195827b27ec6Sopenharmony_ci /* case dstage_checkSuffix: */ /* no direct entry, avoid initialization risks */ 195927b27ec6Sopenharmony_ci if (!dctx->skipChecksum) { 196027b27ec6Sopenharmony_ci U32 const readCRC = LZ4F_readLE32(selectedIn); 196127b27ec6Sopenharmony_ci U32 const resultCRC = XXH32_digest(&(dctx->xxh)); 196227b27ec6Sopenharmony_ci#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 196327b27ec6Sopenharmony_ci RETURN_ERROR_IF(readCRC != resultCRC, contentChecksum_invalid); 196427b27ec6Sopenharmony_ci#else 196527b27ec6Sopenharmony_ci (void)readCRC; 196627b27ec6Sopenharmony_ci (void)resultCRC; 196727b27ec6Sopenharmony_ci#endif 196827b27ec6Sopenharmony_ci } 196927b27ec6Sopenharmony_ci nextSrcSizeHint = 0; 197027b27ec6Sopenharmony_ci LZ4F_resetDecompressionContext(dctx); 197127b27ec6Sopenharmony_ci doAnotherStage = 0; 197227b27ec6Sopenharmony_ci break; 197327b27ec6Sopenharmony_ci 197427b27ec6Sopenharmony_ci case dstage_getSFrameSize: 197527b27ec6Sopenharmony_ci if ((srcEnd - srcPtr) >= 4) { 197627b27ec6Sopenharmony_ci selectedIn = srcPtr; 197727b27ec6Sopenharmony_ci srcPtr += 4; 197827b27ec6Sopenharmony_ci } else { 197927b27ec6Sopenharmony_ci /* not enough input to read cBlockSize field */ 198027b27ec6Sopenharmony_ci dctx->tmpInSize = 4; 198127b27ec6Sopenharmony_ci dctx->tmpInTarget = 8; 198227b27ec6Sopenharmony_ci dctx->dStage = dstage_storeSFrameSize; 198327b27ec6Sopenharmony_ci } 198427b27ec6Sopenharmony_ci 198527b27ec6Sopenharmony_ci if (dctx->dStage == dstage_storeSFrameSize) 198627b27ec6Sopenharmony_ci case dstage_storeSFrameSize: 198727b27ec6Sopenharmony_ci { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, 198827b27ec6Sopenharmony_ci (size_t)(srcEnd - srcPtr) ); 198927b27ec6Sopenharmony_ci memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy); 199027b27ec6Sopenharmony_ci srcPtr += sizeToCopy; 199127b27ec6Sopenharmony_ci dctx->tmpInSize += sizeToCopy; 199227b27ec6Sopenharmony_ci if (dctx->tmpInSize < dctx->tmpInTarget) { 199327b27ec6Sopenharmony_ci /* not enough input to get full sBlockSize; wait for more */ 199427b27ec6Sopenharmony_ci nextSrcSizeHint = dctx->tmpInTarget - dctx->tmpInSize; 199527b27ec6Sopenharmony_ci doAnotherStage = 0; 199627b27ec6Sopenharmony_ci break; 199727b27ec6Sopenharmony_ci } 199827b27ec6Sopenharmony_ci selectedIn = dctx->header + 4; 199927b27ec6Sopenharmony_ci } /* if (dctx->dStage == dstage_storeSFrameSize) */ 200027b27ec6Sopenharmony_ci 200127b27ec6Sopenharmony_ci /* case dstage_decodeSFrameSize: */ /* no direct entry */ 200227b27ec6Sopenharmony_ci { size_t const SFrameSize = LZ4F_readLE32(selectedIn); 200327b27ec6Sopenharmony_ci dctx->frameInfo.contentSize = SFrameSize; 200427b27ec6Sopenharmony_ci dctx->tmpInTarget = SFrameSize; 200527b27ec6Sopenharmony_ci dctx->dStage = dstage_skipSkippable; 200627b27ec6Sopenharmony_ci break; 200727b27ec6Sopenharmony_ci } 200827b27ec6Sopenharmony_ci 200927b27ec6Sopenharmony_ci case dstage_skipSkippable: 201027b27ec6Sopenharmony_ci { size_t const skipSize = MIN(dctx->tmpInTarget, (size_t)(srcEnd-srcPtr)); 201127b27ec6Sopenharmony_ci srcPtr += skipSize; 201227b27ec6Sopenharmony_ci dctx->tmpInTarget -= skipSize; 201327b27ec6Sopenharmony_ci doAnotherStage = 0; 201427b27ec6Sopenharmony_ci nextSrcSizeHint = dctx->tmpInTarget; 201527b27ec6Sopenharmony_ci if (nextSrcSizeHint) break; /* still more to skip */ 201627b27ec6Sopenharmony_ci /* frame fully skipped : prepare context for a new frame */ 201727b27ec6Sopenharmony_ci LZ4F_resetDecompressionContext(dctx); 201827b27ec6Sopenharmony_ci break; 201927b27ec6Sopenharmony_ci } 202027b27ec6Sopenharmony_ci } /* switch (dctx->dStage) */ 202127b27ec6Sopenharmony_ci } /* while (doAnotherStage) */ 202227b27ec6Sopenharmony_ci 202327b27ec6Sopenharmony_ci /* preserve history within tmpOut whenever necessary */ 202427b27ec6Sopenharmony_ci LZ4F_STATIC_ASSERT((unsigned)dstage_init == 2); 202527b27ec6Sopenharmony_ci if ( (dctx->frameInfo.blockMode==LZ4F_blockLinked) /* next block will use up to 64KB from previous ones */ 202627b27ec6Sopenharmony_ci && (dctx->dict != dctx->tmpOutBuffer) /* dictionary is not already within tmp */ 202727b27ec6Sopenharmony_ci && (dctx->dict != NULL) /* dictionary exists */ 202827b27ec6Sopenharmony_ci && (!decompressOptionsPtr->stableDst) /* cannot rely on dst data to remain there for next call */ 202927b27ec6Sopenharmony_ci && ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) ) /* valid stages : [init ... getSuffix[ */ 203027b27ec6Sopenharmony_ci { 203127b27ec6Sopenharmony_ci if (dctx->dStage == dstage_flushOut) { 203227b27ec6Sopenharmony_ci size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer); 203327b27ec6Sopenharmony_ci size_t copySize = 64 KB - dctx->tmpOutSize; 203427b27ec6Sopenharmony_ci const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart; 203527b27ec6Sopenharmony_ci if (dctx->tmpOutSize > 64 KB) copySize = 0; 203627b27ec6Sopenharmony_ci if (copySize > preserveSize) copySize = preserveSize; 203727b27ec6Sopenharmony_ci assert(dctx->tmpOutBuffer != NULL); 203827b27ec6Sopenharmony_ci 203927b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize); 204027b27ec6Sopenharmony_ci 204127b27ec6Sopenharmony_ci dctx->dict = dctx->tmpOutBuffer; 204227b27ec6Sopenharmony_ci dctx->dictSize = preserveSize + dctx->tmpOutStart; 204327b27ec6Sopenharmony_ci } else { 204427b27ec6Sopenharmony_ci const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize; 204527b27ec6Sopenharmony_ci size_t const newDictSize = MIN(dctx->dictSize, 64 KB); 204627b27ec6Sopenharmony_ci 204727b27ec6Sopenharmony_ci memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize); 204827b27ec6Sopenharmony_ci 204927b27ec6Sopenharmony_ci dctx->dict = dctx->tmpOutBuffer; 205027b27ec6Sopenharmony_ci dctx->dictSize = newDictSize; 205127b27ec6Sopenharmony_ci dctx->tmpOut = dctx->tmpOutBuffer + newDictSize; 205227b27ec6Sopenharmony_ci } 205327b27ec6Sopenharmony_ci } 205427b27ec6Sopenharmony_ci 205527b27ec6Sopenharmony_ci *srcSizePtr = (size_t)(srcPtr - srcStart); 205627b27ec6Sopenharmony_ci *dstSizePtr = (size_t)(dstPtr - dstStart); 205727b27ec6Sopenharmony_ci return nextSrcSizeHint; 205827b27ec6Sopenharmony_ci} 205927b27ec6Sopenharmony_ci 206027b27ec6Sopenharmony_ci/*! LZ4F_decompress_usingDict() : 206127b27ec6Sopenharmony_ci * Same as LZ4F_decompress(), using a predefined dictionary. 206227b27ec6Sopenharmony_ci * Dictionary is used "in place", without any preprocessing. 206327b27ec6Sopenharmony_ci * It must remain accessible throughout the entire frame decoding. 206427b27ec6Sopenharmony_ci */ 206527b27ec6Sopenharmony_cisize_t LZ4F_decompress_usingDict(LZ4F_dctx* dctx, 206627b27ec6Sopenharmony_ci void* dstBuffer, size_t* dstSizePtr, 206727b27ec6Sopenharmony_ci const void* srcBuffer, size_t* srcSizePtr, 206827b27ec6Sopenharmony_ci const void* dict, size_t dictSize, 206927b27ec6Sopenharmony_ci const LZ4F_decompressOptions_t* decompressOptionsPtr) 207027b27ec6Sopenharmony_ci{ 207127b27ec6Sopenharmony_ci if (dctx->dStage <= dstage_init) { 207227b27ec6Sopenharmony_ci dctx->dict = (const BYTE*)dict; 207327b27ec6Sopenharmony_ci dctx->dictSize = dictSize; 207427b27ec6Sopenharmony_ci } 207527b27ec6Sopenharmony_ci return LZ4F_decompress(dctx, dstBuffer, dstSizePtr, 207627b27ec6Sopenharmony_ci srcBuffer, srcSizePtr, 207727b27ec6Sopenharmony_ci decompressOptionsPtr); 207827b27ec6Sopenharmony_ci} 2079