| 1 | /* Lzma86Enc.c -- LZMA + x86 (BCJ) Filter Encoder |
| 2 | 2008-08-05 |
| 3 | Igor Pavlov |
| 4 | Public domain */ |
| 5 | |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include "Lzma86Enc.h"cxx . . $ `<file>/compression/lzma/Lzma86Enc.h`/ |
| 9 | |
| 10 | #include "Alloc.h"cxx . . $ `<file>/compression/lzma/Alloc.h`/ |
| 11 | #include "Bra.h"cxx . . $ `<file>/compression/lzma/Bra.h`/ |
| 12 | #include "LzmaEnc.h"cxx . . $ `<file>/compression/lzma/LzmaEnc.h`/ |
| 13 | |
| 14 | #define SZE_OUT_OVERFLOWcxx . . $ `compression/lzma/Lzma86Enc.c:14:9`! SZE_DATA_ERROR |
| 15 | |
| 16 | static void *SzAlloccxx . . $ SzAlloc(417ab160f6baadc6).(void *p, size_t size) { p = p; return MyAlloccxx . . $ MyAlloc(bfcfa4d6b7f7ef64).(size); } |
| 17 | static void SzFreecxx . . $ SzFree(6c58c5e2437d25a2).(void *p, void *address) { p = p; MyFreecxx . . $ MyFree(5d22bdacc48458e8).(address); } |
| 18 | static ISzAlloccxx . . $ ISzAlloc# g_Alloccxx . . $ g_Alloc. = { SzAlloccxx . . $ SzAlloc(417ab160f6baadc6)., SzFreecxx . . $ SzFree(6c58c5e2437d25a2). }; |
| 19 | |
| 20 | #define LZMA86_SIZE_OFFSETcxx . . $ `compression/lzma/Lzma86Enc.c:20:9`! (1 + LZMA_PROPS_SIZEcxx . . $ `compression/lzma/LzmaEnc.h:9:9`!) |
| 21 | #define LZMA86_HEADER_SIZEcxx . . $ `compression/lzma/Lzma86Enc.c:21:9`! (LZMA86_SIZE_OFFSETcxx . . $ `compression/lzma/Lzma86Enc.c:20:9`! + 8) |
| 22 | |
| 23 | int Lzma86_Encodecxx . . $ Lzma86_Encode(7e89eb050c55b2cc).(Bytecxx . . $ Byte# *dest, size_t *destLen, const Bytecxx . . $ Byte# *src, size_t srcLen, |
| 24 | int level, UInt32cxx . . $ UInt32# dictSize, int filterMode) |
| 25 | { |
| 26 | size_t outSize2 = *destLen; |
| 27 | Bytecxx . . $ Byte# *filteredStream; |
| 28 | Boolcxx . . $ Bool# useFilter; |
| 29 | int mainResult = SZ_ERROR_OUTPUT_EOFcxx . . $ `compression/lzma/Types.h:21:9`!; |
| 30 | CLzmaEncPropscxx . . $ CLzmaEncProps# props; |
| 31 | LzmaEncProps_Initcxx . . $ LzmaEncProps_Init(8b45acebcbbd4466).(&props); |
| 32 | props.levelcxx . . $ _CLzmaEncProps#level. = level; |
| 33 | props.dictSizecxx . . $ _CLzmaEncProps#dictSize. = dictSize; |
| 34 | |
| 35 | *destLen = 0; |
| 36 | if (outSize2 < LZMA86_HEADER_SIZEcxx . . $ `compression/lzma/Lzma86Enc.c:21:9`!) |
| 37 | return SZ_ERROR_OUTPUT_EOFcxx . . $ `compression/lzma/Types.h:21:9`!; |
| 38 | |
| 39 | { |
| 40 | int i; |
| 41 | UInt64cxx . . $ UInt64# t = srcLen; |
| 42 | for (i = 0; i < 8; i++, t >>= 8) |
| 43 | dest[LZMA86_SIZE_OFFSETcxx . . $ `compression/lzma/Lzma86Enc.c:20:9`! + i] = (Bytecxx . . $ Byte#)t; |
| 44 | } |
| 45 | |
| 46 | filteredStream = 0; |
| 47 | useFilter = (filterMode != SZ_FILTER_NOcxx . . $ SZ_FILTER_NO.); |
| 48 | if (useFilter) |
| 49 | { |
| 50 | if (srcLen != 0) |
| 51 | { |
| 52 | filteredStream = (Bytecxx . . $ Byte# *)MyAlloccxx . . $ MyAlloc(bfcfa4d6b7f7ef64).(srcLen); |
| 53 | if (filteredStream == 0) |
| 54 | return SZ_ERROR_MEMcxx . . $ `compression/lzma/Types.h:16:9`!; |
| 55 | memcpycxx . . $ memcpy(cc817cb175f282d).(filteredStream, src, srcLen); |
| 56 | } |
| 57 | { |
| 58 | UInt32cxx . . $ UInt32# x86State; |
| 59 | x86_Convert_Initcxx . . $ `compression/lzma/Bra.h:52:9`!(x86State); |
| 60 | x86_Convertcxx . . $ x86_Convert(5125666cce7acd55).(filteredStream, srcLen, 0, &x86State, 1); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | { |
| 65 | size_t minSize = 0; |
| 66 | Boolcxx . . $ Bool# bestIsFiltered = Falsecxx . . $ `compression/lzma/Types.h:83:9`!; |
| 67 | |
| 68 | /* passes for SZ_FILTER_AUTO: |
| 69 | 0 - BCJ + LZMA |
| 70 | 1 - LZMA |
| 71 | 2 - BCJ + LZMA agaian, if pass 0 (BCJ + LZMA) is better. |
| 72 | */ |
| 73 | int numPasses = (filterMode == SZ_FILTER_AUTOcxx . . $ SZ_FILTER_AUTO.) ? 3 : 1; |
| 74 | |
| 75 | int i; |
| 76 | for (i = 0; i < numPasses; i++) |
| 77 | { |
| 78 | size_t outSizeProcessed = outSize2 - LZMA86_HEADER_SIZEcxx . . $ `compression/lzma/Lzma86Enc.c:21:9`!; |
| 79 | size_t outPropsSize = 5; |
| 80 | SRescxx . . $ SRes# curRes; |
| 81 | Boolcxx . . $ Bool# curModeIsFiltered = (numPasses > 1 && i == numPasses - 1); |
| 82 | if (curModeIsFiltered && !bestIsFiltered) |
| 83 | break; |
| 84 | if (useFilter && i == 0) |
| 85 | curModeIsFiltered = Truecxx . . $ `compression/lzma/Types.h:82:9`!; |
| 86 | |
| 87 | curRes = LzmaEncodecxx . . $ LzmaEncode(368176730a281172).(dest + LZMA86_HEADER_SIZEcxx . . $ `compression/lzma/Lzma86Enc.c:21:9`!, &outSizeProcessed, |
| 88 | curModeIsFiltered ? filteredStream : src, srcLen, |
| 89 | &props, dest + 1, &outPropsSize, 0, |
| 90 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &g_Alloccxx . . $ g_Alloc., &g_Alloccxx . . $ g_Alloc.); |
| 91 | |
| 92 | if (curRes != SZ_ERROR_OUTPUT_EOFcxx . . $ `compression/lzma/Types.h:21:9`!) |
| 93 | { |
| 94 | if (curRes != SZ_OKcxx . . $ `compression/lzma/Types.h:13:9`!) |
| 95 | { |
| 96 | mainResult = curRes; |
| 97 | break; |
| 98 | } |
| 99 | if (outSizeProcessed <= minSize || mainResult != SZ_OKcxx . . $ `compression/lzma/Types.h:13:9`!) |
| 100 | { |
| 101 | minSize = outSizeProcessed; |
| 102 | bestIsFiltered = curModeIsFiltered; |
| 103 | mainResult = SZ_OKcxx . . $ `compression/lzma/Types.h:13:9`!; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | dest[0] = (unsigned char) (bestIsFiltered ? 1 : 0); /*MAB: Cast to silence compiler */ |
| 108 | *destLen = LZMA86_HEADER_SIZEcxx . . $ `compression/lzma/Lzma86Enc.c:21:9`! + minSize; |
| 109 | } |
| 110 | if (useFilter) |
| 111 | MyFreecxx . . $ MyFree(5d22bdacc48458e8).(filteredStream); |
| 112 | return mainResult; |
| 113 | } |