LzmaEnc.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (c) 1999-2008 Igor Pavlov
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * This code was taken from LZMA SDK 4.58 beta, and was slightly modified
  21. * to adapt it to GRUB's requirement.
  22. *
  23. * See <http://www.7-zip.org>, for more information about LZMA.
  24. */
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <grub/lib/LzmaEnc.h>
  29. #include <grub/lib/LzFind.h>
  30. #ifdef COMPRESS_MF_MT
  31. #include <grub/lib/LzFindMt.h>
  32. #endif
  33. /* #define SHOW_STAT */
  34. /* #define SHOW_STAT2 */
  35. #ifdef SHOW_STAT
  36. static int ttt = 0;
  37. #endif
  38. #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
  39. #define kBlockSize (9 << 10)
  40. #define kUnpackBlockSize (1 << 18)
  41. #define kMatchArraySize (1 << 21)
  42. #define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
  43. #define kNumMaxDirectBits (31)
  44. #define kNumTopBits 24
  45. #define kTopValue ((UInt32)1 << kNumTopBits)
  46. #define kNumBitModelTotalBits 11
  47. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  48. #define kNumMoveBits 5
  49. #define kProbInitValue (kBitModelTotal >> 1)
  50. #define kNumMoveReducingBits 4
  51. #define kNumBitPriceShiftBits 4
  52. #define kBitPrice (1 << kNumBitPriceShiftBits)
  53. void LzmaEncProps_Init(CLzmaEncProps *p)
  54. {
  55. p->level = 5;
  56. p->dictSize = p->mc = 0;
  57. p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
  58. p->writeEndMark = 0;
  59. }
  60. void LzmaEncProps_Normalize(CLzmaEncProps *p)
  61. {
  62. int level = p->level;
  63. if (level < 0) level = 5;
  64. p->level = level;
  65. if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
  66. if (p->lc < 0) p->lc = 3;
  67. if (p->lp < 0) p->lp = 0;
  68. if (p->pb < 0) p->pb = 2;
  69. if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
  70. if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
  71. if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
  72. if (p->numHashBytes < 0) p->numHashBytes = 4;
  73. if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
  74. if (p->numThreads < 0) p->numThreads = ((p->btMode && p->algo) ? 2 : 1);
  75. }
  76. UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
  77. {
  78. CLzmaEncProps props = *props2;
  79. LzmaEncProps_Normalize(&props);
  80. return props.dictSize;
  81. }
  82. /* #define LZMA_LOG_BSR */
  83. /* Define it for Intel's CPU */
  84. #ifdef LZMA_LOG_BSR
  85. #define kDicLogSizeMaxCompress 30
  86. #define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
  87. UInt32 GetPosSlot1(UInt32 pos)
  88. {
  89. UInt32 res;
  90. BSR2_RET(pos, res);
  91. return res;
  92. }
  93. #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
  94. #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
  95. #else
  96. #define kNumLogBits (9 + (int)sizeof(size_t) / 2)
  97. #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
  98. static void LzmaEnc_FastPosInit(Byte *g_FastPos)
  99. {
  100. int c = 2, slotFast;
  101. g_FastPos[0] = 0;
  102. g_FastPos[1] = 1;
  103. for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
  104. {
  105. UInt32 k = (1 << ((slotFast >> 1) - 1));
  106. UInt32 j;
  107. for (j = 0; j < k; j++, c++)
  108. g_FastPos[c] = (Byte)slotFast;
  109. }
  110. }
  111. #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
  112. (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
  113. res = p->g_FastPos[pos >> i] + (i * 2); }
  114. /*
  115. #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
  116. p->g_FastPos[pos >> 6] + 12 : \
  117. p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
  118. */
  119. #define GetPosSlot1(pos) p->g_FastPos[pos]
  120. #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
  121. #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
  122. #endif
  123. #define LZMA_NUM_REPS 4
  124. typedef unsigned CState;
  125. typedef struct _COptimal
  126. {
  127. UInt32 price;
  128. CState state;
  129. int prev1IsChar;
  130. int prev2;
  131. UInt32 posPrev2;
  132. UInt32 backPrev2;
  133. UInt32 posPrev;
  134. UInt32 backPrev;
  135. UInt32 backs[LZMA_NUM_REPS];
  136. } COptimal;
  137. #define kNumOpts (1 << 12)
  138. #define kNumLenToPosStates 4
  139. #define kNumPosSlotBits 6
  140. #define kDicLogSizeMin 0
  141. #define kDicLogSizeMax 32
  142. #define kDistTableSizeMax (kDicLogSizeMax * 2)
  143. #define kNumAlignBits 4
  144. #define kAlignTableSize (1 << kNumAlignBits)
  145. #define kAlignMask (kAlignTableSize - 1)
  146. #define kStartPosModelIndex 4
  147. #define kEndPosModelIndex 14
  148. #define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
  149. #define kNumFullDistances (1 << (kEndPosModelIndex / 2))
  150. #ifdef _LZMA_PROB32
  151. #define CLzmaProb UInt32
  152. #else
  153. #define CLzmaProb UInt16
  154. #endif
  155. #define LZMA_PB_MAX 4
  156. #define LZMA_LC_MAX 8
  157. #define LZMA_LP_MAX 4
  158. #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
  159. #define kLenNumLowBits 3
  160. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  161. #define kLenNumMidBits 3
  162. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  163. #define kLenNumHighBits 8
  164. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  165. #define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
  166. #define LZMA_MATCH_LEN_MIN 2
  167. #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
  168. #define kNumStates 12
  169. typedef struct
  170. {
  171. CLzmaProb choice;
  172. CLzmaProb choice2;
  173. CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
  174. CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
  175. CLzmaProb high[kLenNumHighSymbols];
  176. } CLenEnc;
  177. typedef struct
  178. {
  179. CLenEnc p;
  180. UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
  181. UInt32 tableSize;
  182. UInt32 counters[LZMA_NUM_PB_STATES_MAX];
  183. } CLenPriceEnc;
  184. typedef struct _CRangeEnc
  185. {
  186. UInt32 range;
  187. Byte cache;
  188. UInt64 low;
  189. UInt64 cacheSize;
  190. Byte *buf;
  191. Byte *bufLim;
  192. Byte *bufBase;
  193. ISeqOutStream *outStream;
  194. UInt64 processed;
  195. SRes res;
  196. } CRangeEnc;
  197. typedef struct _CSeqInStreamBuf
  198. {
  199. ISeqInStream funcTable;
  200. const Byte *data;
  201. SizeT rem;
  202. } CSeqInStreamBuf;
  203. static SRes MyRead(void *pp, void *data, size_t *size)
  204. {
  205. size_t curSize = *size;
  206. CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp;
  207. if (p->rem < curSize)
  208. curSize = p->rem;
  209. memcpy(data, p->data, curSize);
  210. p->rem -= curSize;
  211. p->data += curSize;
  212. *size = curSize;
  213. return SZ_OK;
  214. }
  215. typedef struct
  216. {
  217. CLzmaProb *litProbs;
  218. CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
  219. CLzmaProb isRep[kNumStates];
  220. CLzmaProb isRepG0[kNumStates];
  221. CLzmaProb isRepG1[kNumStates];
  222. CLzmaProb isRepG2[kNumStates];
  223. CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
  224. CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
  225. CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
  226. CLzmaProb posAlignEncoder[1 << kNumAlignBits];
  227. CLenPriceEnc lenEnc;
  228. CLenPriceEnc repLenEnc;
  229. UInt32 reps[LZMA_NUM_REPS];
  230. UInt32 state;
  231. } CSaveState;
  232. typedef struct _CLzmaEnc
  233. {
  234. IMatchFinder matchFinder;
  235. void *matchFinderObj;
  236. #ifdef COMPRESS_MF_MT
  237. Bool mtMode;
  238. CMatchFinderMt matchFinderMt;
  239. #endif
  240. CMatchFinder matchFinderBase;
  241. #ifdef COMPRESS_MF_MT
  242. Byte pad[128];
  243. #endif
  244. UInt32 optimumEndIndex;
  245. UInt32 optimumCurrentIndex;
  246. Bool longestMatchWasFound;
  247. UInt32 longestMatchLength;
  248. UInt32 numDistancePairs;
  249. COptimal opt[kNumOpts];
  250. #ifndef LZMA_LOG_BSR
  251. Byte g_FastPos[1 << kNumLogBits];
  252. #endif
  253. UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
  254. UInt32 matchDistances[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
  255. UInt32 numFastBytes;
  256. UInt32 additionalOffset;
  257. UInt32 reps[LZMA_NUM_REPS];
  258. UInt32 state;
  259. UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
  260. UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
  261. UInt32 alignPrices[kAlignTableSize];
  262. UInt32 alignPriceCount;
  263. UInt32 distTableSize;
  264. unsigned lc, lp, pb;
  265. unsigned lpMask, pbMask;
  266. CLzmaProb *litProbs;
  267. CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
  268. CLzmaProb isRep[kNumStates];
  269. CLzmaProb isRepG0[kNumStates];
  270. CLzmaProb isRepG1[kNumStates];
  271. CLzmaProb isRepG2[kNumStates];
  272. CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
  273. CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
  274. CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
  275. CLzmaProb posAlignEncoder[1 << kNumAlignBits];
  276. CLenPriceEnc lenEnc;
  277. CLenPriceEnc repLenEnc;
  278. unsigned lclp;
  279. Bool fastMode;
  280. CRangeEnc rc;
  281. Bool writeEndMark;
  282. UInt64 nowPos64;
  283. UInt32 matchPriceCount;
  284. Bool finished;
  285. Bool multiThread;
  286. SRes result;
  287. UInt32 dictSize;
  288. UInt32 matchFinderCycles;
  289. ISeqInStream *inStream;
  290. CSeqInStreamBuf seqBufInStream;
  291. CSaveState saveState;
  292. } CLzmaEnc;
  293. SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
  294. {
  295. CLzmaEnc *p = (CLzmaEnc *)pp;
  296. CLzmaEncProps props = *props2;
  297. LzmaEncProps_Normalize(&props);
  298. if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
  299. props.dictSize > (1U << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30))
  300. return SZ_ERROR_PARAM;
  301. p->dictSize = props.dictSize;
  302. p->matchFinderCycles = props.mc;
  303. {
  304. unsigned fb = props.fb;
  305. if (fb < 5)
  306. fb = 5;
  307. if (fb > LZMA_MATCH_LEN_MAX)
  308. fb = LZMA_MATCH_LEN_MAX;
  309. p->numFastBytes = fb;
  310. }
  311. p->lc = props.lc;
  312. p->lp = props.lp;
  313. p->pb = props.pb;
  314. p->fastMode = (props.algo == 0);
  315. p->matchFinderBase.btMode = props.btMode;
  316. {
  317. UInt32 numHashBytes = 4;
  318. if (props.btMode)
  319. {
  320. if (props.numHashBytes < 2)
  321. numHashBytes = 2;
  322. else if (props.numHashBytes < 4)
  323. numHashBytes = props.numHashBytes;
  324. }
  325. p->matchFinderBase.numHashBytes = numHashBytes;
  326. }
  327. p->matchFinderBase.cutValue = props.mc;
  328. p->writeEndMark = props.writeEndMark;
  329. #ifdef COMPRESS_MF_MT
  330. /*
  331. if (newMultiThread != _multiThread)
  332. {
  333. ReleaseMatchFinder();
  334. _multiThread = newMultiThread;
  335. }
  336. */
  337. p->multiThread = (props.numThreads > 1);
  338. #endif
  339. return SZ_OK;
  340. }
  341. static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
  342. static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
  343. static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
  344. static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
  345. /*
  346. void UpdateChar() { Index = kLiteralNextStates[Index]; }
  347. void UpdateMatch() { Index = kMatchNextStates[Index]; }
  348. void UpdateRep() { Index = kRepNextStates[Index]; }
  349. void UpdateShortRep() { Index = kShortRepNextStates[Index]; }
  350. */
  351. #define IsCharState(s) ((s) < 7)
  352. #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
  353. #define kInfinityPrice (1 << 30)
  354. static void RangeEnc_Construct(CRangeEnc *p)
  355. {
  356. p->outStream = 0;
  357. p->bufBase = 0;
  358. }
  359. #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
  360. #define RC_BUF_SIZE (1 << 16)
  361. static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
  362. {
  363. if (p->bufBase == 0)
  364. {
  365. p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
  366. if (p->bufBase == 0)
  367. return 0;
  368. p->bufLim = p->bufBase + RC_BUF_SIZE;
  369. }
  370. return 1;
  371. }
  372. static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
  373. {
  374. alloc->Free(alloc, p->bufBase);
  375. p->bufBase = 0;
  376. }
  377. static void RangeEnc_Init(CRangeEnc *p)
  378. {
  379. /* Stream.Init(); */
  380. p->low = 0;
  381. p->range = 0xFFFFFFFF;
  382. p->cacheSize = 1;
  383. p->cache = 0;
  384. p->buf = p->bufBase;
  385. p->processed = 0;
  386. p->res = SZ_OK;
  387. }
  388. static void RangeEnc_FlushStream(CRangeEnc *p)
  389. {
  390. size_t num;
  391. if (p->res != SZ_OK)
  392. return;
  393. num = p->buf - p->bufBase;
  394. if (num != p->outStream->Write(p->outStream, p->bufBase, num))
  395. p->res = SZ_ERROR_WRITE;
  396. p->processed += num;
  397. p->buf = p->bufBase;
  398. }
  399. static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
  400. {
  401. if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0)
  402. {
  403. Byte temp = p->cache;
  404. do
  405. {
  406. Byte *buf = p->buf;
  407. *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
  408. p->buf = buf;
  409. if (buf == p->bufLim)
  410. RangeEnc_FlushStream(p);
  411. temp = 0xFF;
  412. }
  413. while (--p->cacheSize != 0);
  414. p->cache = (Byte)((UInt32)p->low >> 24);
  415. }
  416. p->cacheSize++;
  417. p->low = (UInt32)p->low << 8;
  418. }
  419. static void RangeEnc_FlushData(CRangeEnc *p)
  420. {
  421. int i;
  422. for (i = 0; i < 5; i++)
  423. RangeEnc_ShiftLow(p);
  424. }
  425. static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits)
  426. {
  427. do
  428. {
  429. p->range >>= 1;
  430. p->low += p->range & (0 - ((value >> --numBits) & 1));
  431. if (p->range < kTopValue)
  432. {
  433. p->range <<= 8;
  434. RangeEnc_ShiftLow(p);
  435. }
  436. }
  437. while (numBits != 0);
  438. }
  439. static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
  440. {
  441. UInt32 ttt = *prob;
  442. UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
  443. if (symbol == 0)
  444. {
  445. p->range = newBound;
  446. ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
  447. }
  448. else
  449. {
  450. p->low += newBound;
  451. p->range -= newBound;
  452. ttt -= ttt >> kNumMoveBits;
  453. }
  454. *prob = (CLzmaProb)ttt;
  455. if (p->range < kTopValue)
  456. {
  457. p->range <<= 8;
  458. RangeEnc_ShiftLow(p);
  459. }
  460. }
  461. static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
  462. {
  463. symbol |= 0x100;
  464. do
  465. {
  466. RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
  467. symbol <<= 1;
  468. }
  469. while (symbol < 0x10000);
  470. }
  471. static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
  472. {
  473. UInt32 offs = 0x100;
  474. symbol |= 0x100;
  475. do
  476. {
  477. matchByte <<= 1;
  478. RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
  479. symbol <<= 1;
  480. offs &= ~(matchByte ^ symbol);
  481. }
  482. while (symbol < 0x10000);
  483. }
  484. static void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
  485. {
  486. UInt32 i;
  487. for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
  488. {
  489. const int kCyclesBits = kNumBitPriceShiftBits;
  490. UInt32 w = i;
  491. UInt32 bitCount = 0;
  492. int j;
  493. for (j = 0; j < kCyclesBits; j++)
  494. {
  495. w = w * w;
  496. bitCount <<= 1;
  497. while (w >= ((UInt32)1 << 16))
  498. {
  499. w >>= 1;
  500. bitCount++;
  501. }
  502. }
  503. ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
  504. }
  505. }
  506. #define GET_PRICE(prob, symbol) \
  507. p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
  508. #define GET_PRICEa(prob, symbol) \
  509. ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
  510. #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
  511. #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
  512. #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
  513. #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
  514. static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
  515. {
  516. UInt32 price = 0;
  517. symbol |= 0x100;
  518. do
  519. {
  520. price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
  521. symbol <<= 1;
  522. }
  523. while (symbol < 0x10000);
  524. return price;
  525. };
  526. static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
  527. {
  528. UInt32 price = 0;
  529. UInt32 offs = 0x100;
  530. symbol |= 0x100;
  531. do
  532. {
  533. matchByte <<= 1;
  534. price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
  535. symbol <<= 1;
  536. offs &= ~(matchByte ^ symbol);
  537. }
  538. while (symbol < 0x10000);
  539. return price;
  540. };
  541. static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
  542. {
  543. UInt32 m = 1;
  544. int i;
  545. for (i = numBitLevels; i != 0 ;)
  546. {
  547. UInt32 bit;
  548. i--;
  549. bit = (symbol >> i) & 1;
  550. RangeEnc_EncodeBit(rc, probs + m, bit);
  551. m = (m << 1) | bit;
  552. }
  553. };
  554. static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
  555. {
  556. UInt32 m = 1;
  557. int i;
  558. for (i = 0; i < numBitLevels; i++)
  559. {
  560. UInt32 bit = symbol & 1;
  561. RangeEnc_EncodeBit(rc, probs + m, bit);
  562. m = (m << 1) | bit;
  563. symbol >>= 1;
  564. }
  565. }
  566. static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
  567. {
  568. UInt32 price = 0;
  569. symbol |= (1 << numBitLevels);
  570. while (symbol != 1)
  571. {
  572. price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
  573. symbol >>= 1;
  574. }
  575. return price;
  576. }
  577. static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
  578. {
  579. UInt32 price = 0;
  580. UInt32 m = 1;
  581. int i;
  582. for (i = numBitLevels; i != 0; i--)
  583. {
  584. UInt32 bit = symbol & 1;
  585. symbol >>= 1;
  586. price += GET_PRICEa(probs[m], bit);
  587. m = (m << 1) | bit;
  588. }
  589. return price;
  590. }
  591. static void LenEnc_Init(CLenEnc *p)
  592. {
  593. unsigned i;
  594. p->choice = p->choice2 = kProbInitValue;
  595. for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
  596. p->low[i] = kProbInitValue;
  597. for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
  598. p->mid[i] = kProbInitValue;
  599. for (i = 0; i < kLenNumHighSymbols; i++)
  600. p->high[i] = kProbInitValue;
  601. }
  602. static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
  603. {
  604. if (symbol < kLenNumLowSymbols)
  605. {
  606. RangeEnc_EncodeBit(rc, &p->choice, 0);
  607. RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
  608. }
  609. else
  610. {
  611. RangeEnc_EncodeBit(rc, &p->choice, 1);
  612. if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
  613. {
  614. RangeEnc_EncodeBit(rc, &p->choice2, 0);
  615. RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
  616. }
  617. else
  618. {
  619. RangeEnc_EncodeBit(rc, &p->choice2, 1);
  620. RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
  621. }
  622. }
  623. }
  624. static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
  625. {
  626. UInt32 a0 = GET_PRICE_0a(p->choice);
  627. UInt32 a1 = GET_PRICE_1a(p->choice);
  628. UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
  629. UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
  630. UInt32 i = 0;
  631. for (i = 0; i < kLenNumLowSymbols; i++)
  632. {
  633. if (i >= numSymbols)
  634. return;
  635. prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
  636. }
  637. for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
  638. {
  639. if (i >= numSymbols)
  640. return;
  641. prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
  642. }
  643. for (; i < numSymbols; i++)
  644. prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
  645. }
  646. static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
  647. {
  648. LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
  649. p->counters[posState] = p->tableSize;
  650. }
  651. static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
  652. {
  653. UInt32 posState;
  654. for (posState = 0; posState < numPosStates; posState++)
  655. LenPriceEnc_UpdateTable(p, posState, ProbPrices);
  656. }
  657. static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
  658. {
  659. LenEnc_Encode(&p->p, rc, symbol, posState);
  660. if (updatePrice)
  661. if (--p->counters[posState] == 0)
  662. LenPriceEnc_UpdateTable(p, posState, ProbPrices);
  663. }
  664. static void MovePos(CLzmaEnc *p, UInt32 num)
  665. {
  666. #ifdef SHOW_STAT
  667. ttt += num;
  668. printf("\n MovePos %d", num);
  669. #endif
  670. if (num != 0)
  671. {
  672. p->additionalOffset += num;
  673. p->matchFinder.Skip(p->matchFinderObj, num);
  674. }
  675. }
  676. static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
  677. {
  678. UInt32 lenRes = 0, numDistancePairs;
  679. numDistancePairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matchDistances);
  680. #ifdef SHOW_STAT
  681. printf("\n i = %d numPairs = %d ", ttt, numDistancePairs / 2);
  682. if (ttt >= 61994)
  683. ttt = ttt;
  684. ttt++;
  685. {
  686. UInt32 i;
  687. for (i = 0; i < numDistancePairs; i += 2)
  688. printf("%2d %6d | ", p->matchDistances[i], p->matchDistances[i + 1]);
  689. }
  690. #endif
  691. if (numDistancePairs > 0)
  692. {
  693. lenRes = p->matchDistances[numDistancePairs - 2];
  694. if (lenRes == p->numFastBytes)
  695. {
  696. UInt32 numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) + 1;
  697. const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  698. UInt32 distance = p->matchDistances[numDistancePairs - 1] + 1;
  699. if (numAvail > LZMA_MATCH_LEN_MAX)
  700. numAvail = LZMA_MATCH_LEN_MAX;
  701. {
  702. const Byte *pby2 = pby - distance;
  703. for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
  704. }
  705. }
  706. }
  707. p->additionalOffset++;
  708. *numDistancePairsRes = numDistancePairs;
  709. return lenRes;
  710. }
  711. #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
  712. #define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
  713. #define IsShortRep(p) ((p)->backPrev == 0)
  714. static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
  715. {
  716. return
  717. GET_PRICE_0(p->isRepG0[state]) +
  718. GET_PRICE_0(p->isRep0Long[state][posState]);
  719. }
  720. static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
  721. {
  722. UInt32 price;
  723. if (repIndex == 0)
  724. {
  725. price = GET_PRICE_0(p->isRepG0[state]);
  726. price += GET_PRICE_1(p->isRep0Long[state][posState]);
  727. }
  728. else
  729. {
  730. price = GET_PRICE_1(p->isRepG0[state]);
  731. if (repIndex == 1)
  732. price += GET_PRICE_0(p->isRepG1[state]);
  733. else
  734. {
  735. price += GET_PRICE_1(p->isRepG1[state]);
  736. price += GET_PRICE(p->isRepG2[state], repIndex - 2);
  737. }
  738. }
  739. return price;
  740. }
  741. static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
  742. {
  743. return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
  744. GetPureRepPrice(p, repIndex, state, posState);
  745. }
  746. static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
  747. {
  748. UInt32 posMem = p->opt[cur].posPrev;
  749. UInt32 backMem = p->opt[cur].backPrev;
  750. p->optimumEndIndex = cur;
  751. do
  752. {
  753. if (p->opt[cur].prev1IsChar)
  754. {
  755. MakeAsChar(&p->opt[posMem])
  756. p->opt[posMem].posPrev = posMem - 1;
  757. if (p->opt[cur].prev2)
  758. {
  759. p->opt[posMem - 1].prev1IsChar = False;
  760. p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
  761. p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
  762. }
  763. }
  764. {
  765. UInt32 posPrev = posMem;
  766. UInt32 backCur = backMem;
  767. backMem = p->opt[posPrev].backPrev;
  768. posMem = p->opt[posPrev].posPrev;
  769. p->opt[posPrev].backPrev = backCur;
  770. p->opt[posPrev].posPrev = cur;
  771. cur = posPrev;
  772. }
  773. }
  774. while (cur != 0);
  775. *backRes = p->opt[0].backPrev;
  776. p->optimumCurrentIndex = p->opt[0].posPrev;
  777. return p->optimumCurrentIndex;
  778. }
  779. #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
  780. #pragma GCC diagnostic ignored "-Wshadow"
  781. static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
  782. {
  783. UInt32 numAvailableBytes, lenMain, numDistancePairs;
  784. const Byte *data;
  785. UInt32 reps[LZMA_NUM_REPS];
  786. UInt32 repLens[LZMA_NUM_REPS];
  787. UInt32 repMaxIndex, i;
  788. UInt32 *matchDistances;
  789. Byte currentByte, matchByte;
  790. UInt32 posState;
  791. UInt32 matchPrice, repMatchPrice;
  792. UInt32 lenEnd;
  793. UInt32 len;
  794. UInt32 normalMatchPrice;
  795. UInt32 cur;
  796. if (p->optimumEndIndex != p->optimumCurrentIndex)
  797. {
  798. const COptimal *opt = &p->opt[p->optimumCurrentIndex];
  799. UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
  800. *backRes = opt->backPrev;
  801. p->optimumCurrentIndex = opt->posPrev;
  802. return lenRes;
  803. }
  804. p->optimumCurrentIndex = p->optimumEndIndex = 0;
  805. numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  806. if (!p->longestMatchWasFound)
  807. {
  808. lenMain = ReadMatchDistances(p, &numDistancePairs);
  809. }
  810. else
  811. {
  812. lenMain = p->longestMatchLength;
  813. numDistancePairs = p->numDistancePairs;
  814. p->longestMatchWasFound = False;
  815. }
  816. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  817. if (numAvailableBytes < 2)
  818. {
  819. *backRes = (UInt32)(-1);
  820. return 1;
  821. }
  822. if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
  823. numAvailableBytes = LZMA_MATCH_LEN_MAX;
  824. repMaxIndex = 0;
  825. for (i = 0; i < LZMA_NUM_REPS; i++)
  826. {
  827. UInt32 lenTest;
  828. const Byte *data2;
  829. reps[i] = p->reps[i];
  830. data2 = data - (reps[i] + 1);
  831. if (data[0] != data2[0] || data[1] != data2[1])
  832. {
  833. repLens[i] = 0;
  834. continue;
  835. }
  836. for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
  837. repLens[i] = lenTest;
  838. if (lenTest > repLens[repMaxIndex])
  839. repMaxIndex = i;
  840. }
  841. if (repLens[repMaxIndex] >= p->numFastBytes)
  842. {
  843. UInt32 lenRes;
  844. *backRes = repMaxIndex;
  845. lenRes = repLens[repMaxIndex];
  846. MovePos(p, lenRes - 1);
  847. return lenRes;
  848. }
  849. matchDistances = p->matchDistances;
  850. if (lenMain >= p->numFastBytes)
  851. {
  852. *backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
  853. MovePos(p, lenMain - 1);
  854. return lenMain;
  855. }
  856. currentByte = *data;
  857. matchByte = *(data - (reps[0] + 1));
  858. if (lenMain < 2 && currentByte != matchByte && repLens[repMaxIndex] < 2)
  859. {
  860. *backRes = (UInt32)-1;
  861. return 1;
  862. }
  863. p->opt[0].state = (CState)p->state;
  864. posState = (position & p->pbMask);
  865. {
  866. const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  867. p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
  868. (!IsCharState(p->state) ?
  869. LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
  870. LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
  871. }
  872. MakeAsChar(&p->opt[1]);
  873. matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
  874. repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
  875. if (matchByte == currentByte)
  876. {
  877. UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
  878. if (shortRepPrice < p->opt[1].price)
  879. {
  880. p->opt[1].price = shortRepPrice;
  881. MakeAsShortRep(&p->opt[1]);
  882. }
  883. }
  884. lenEnd = ((lenMain >= repLens[repMaxIndex]) ? lenMain : repLens[repMaxIndex]);
  885. if (lenEnd < 2)
  886. {
  887. *backRes = p->opt[1].backPrev;
  888. return 1;
  889. }
  890. p->opt[1].posPrev = 0;
  891. for (i = 0; i < LZMA_NUM_REPS; i++)
  892. p->opt[0].backs[i] = reps[i];
  893. len = lenEnd;
  894. do
  895. p->opt[len--].price = kInfinityPrice;
  896. while (len >= 2);
  897. for (i = 0; i < LZMA_NUM_REPS; i++)
  898. {
  899. UInt32 repLen = repLens[i];
  900. UInt32 price;
  901. if (repLen < 2)
  902. continue;
  903. price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
  904. do
  905. {
  906. UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
  907. COptimal *opt = &p->opt[repLen];
  908. if (curAndLenPrice < opt->price)
  909. {
  910. opt->price = curAndLenPrice;
  911. opt->posPrev = 0;
  912. opt->backPrev = i;
  913. opt->prev1IsChar = False;
  914. }
  915. }
  916. while (--repLen >= 2);
  917. }
  918. normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
  919. len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
  920. if (len <= lenMain)
  921. {
  922. UInt32 offs = 0;
  923. while (len > matchDistances[offs])
  924. offs += 2;
  925. for (; ; len++)
  926. {
  927. COptimal *opt;
  928. UInt32 distance = matchDistances[offs + 1];
  929. UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
  930. UInt32 lenToPosState = GetLenToPosState(len);
  931. if (distance < kNumFullDistances)
  932. curAndLenPrice += p->distancesPrices[lenToPosState][distance];
  933. else
  934. {
  935. UInt32 slot;
  936. GetPosSlot2(distance, slot);
  937. curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
  938. }
  939. opt = &p->opt[len];
  940. if (curAndLenPrice < opt->price)
  941. {
  942. opt->price = curAndLenPrice;
  943. opt->posPrev = 0;
  944. opt->backPrev = distance + LZMA_NUM_REPS;
  945. opt->prev1IsChar = False;
  946. }
  947. if (len == matchDistances[offs])
  948. {
  949. offs += 2;
  950. if (offs == numDistancePairs)
  951. break;
  952. }
  953. }
  954. }
  955. cur = 0;
  956. #ifdef SHOW_STAT2
  957. if (position >= 0)
  958. {
  959. unsigned i;
  960. printf("\n pos = %4X", position);
  961. for (i = cur; i <= lenEnd; i++)
  962. printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
  963. }
  964. #endif
  965. for (;;)
  966. {
  967. UInt32 numAvailableBytesFull, newLen, numDistancePairs;
  968. COptimal *curOpt;
  969. UInt32 posPrev;
  970. UInt32 state;
  971. UInt32 curPrice;
  972. Bool nextIsChar;
  973. const Byte *data;
  974. Byte currentByte, matchByte;
  975. UInt32 posState;
  976. UInt32 curAnd1Price;
  977. COptimal *nextOpt;
  978. UInt32 matchPrice, repMatchPrice;
  979. UInt32 numAvailableBytes;
  980. UInt32 startLen;
  981. cur++;
  982. if (cur == lenEnd)
  983. return Backward(p, backRes, cur);
  984. numAvailableBytesFull = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  985. newLen = ReadMatchDistances(p, &numDistancePairs);
  986. if (newLen >= p->numFastBytes)
  987. {
  988. p->numDistancePairs = numDistancePairs;
  989. p->longestMatchLength = newLen;
  990. p->longestMatchWasFound = True;
  991. return Backward(p, backRes, cur);
  992. }
  993. position++;
  994. curOpt = &p->opt[cur];
  995. posPrev = curOpt->posPrev;
  996. if (curOpt->prev1IsChar)
  997. {
  998. posPrev--;
  999. if (curOpt->prev2)
  1000. {
  1001. state = p->opt[curOpt->posPrev2].state;
  1002. if (curOpt->backPrev2 < LZMA_NUM_REPS)
  1003. state = kRepNextStates[state];
  1004. else
  1005. state = kMatchNextStates[state];
  1006. }
  1007. else
  1008. state = p->opt[posPrev].state;
  1009. state = kLiteralNextStates[state];
  1010. }
  1011. else
  1012. state = p->opt[posPrev].state;
  1013. if (posPrev == cur - 1)
  1014. {
  1015. if (IsShortRep(curOpt))
  1016. state = kShortRepNextStates[state];
  1017. else
  1018. state = kLiteralNextStates[state];
  1019. }
  1020. else
  1021. {
  1022. UInt32 pos;
  1023. const COptimal *prevOpt;
  1024. if (curOpt->prev1IsChar && curOpt->prev2)
  1025. {
  1026. posPrev = curOpt->posPrev2;
  1027. pos = curOpt->backPrev2;
  1028. state = kRepNextStates[state];
  1029. }
  1030. else
  1031. {
  1032. pos = curOpt->backPrev;
  1033. if (pos < LZMA_NUM_REPS)
  1034. state = kRepNextStates[state];
  1035. else
  1036. state = kMatchNextStates[state];
  1037. }
  1038. prevOpt = &p->opt[posPrev];
  1039. if (pos < LZMA_NUM_REPS)
  1040. {
  1041. UInt32 i;
  1042. reps[0] = prevOpt->backs[pos];
  1043. for (i = 1; i < pos + 1; i++)
  1044. reps[i] = prevOpt->backs[i - 1];
  1045. for (; i < LZMA_NUM_REPS; i++)
  1046. reps[i] = prevOpt->backs[i];
  1047. }
  1048. else
  1049. {
  1050. UInt32 i;
  1051. reps[0] = (pos - LZMA_NUM_REPS);
  1052. for (i = 1; i < LZMA_NUM_REPS; i++)
  1053. reps[i] = prevOpt->backs[i - 1];
  1054. }
  1055. }
  1056. curOpt->state = (CState)state;
  1057. curOpt->backs[0] = reps[0];
  1058. curOpt->backs[1] = reps[1];
  1059. curOpt->backs[2] = reps[2];
  1060. curOpt->backs[3] = reps[3];
  1061. curPrice = curOpt->price;
  1062. nextIsChar = False;
  1063. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1064. currentByte = *data;
  1065. matchByte = *(data - (reps[0] + 1));
  1066. posState = (position & p->pbMask);
  1067. curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
  1068. {
  1069. const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  1070. curAnd1Price +=
  1071. (!IsCharState(state) ?
  1072. LitEnc_GetPriceMatched(probs, currentByte, matchByte, p->ProbPrices) :
  1073. LitEnc_GetPrice(probs, currentByte, p->ProbPrices));
  1074. }
  1075. nextOpt = &p->opt[cur + 1];
  1076. if (curAnd1Price < nextOpt->price)
  1077. {
  1078. nextOpt->price = curAnd1Price;
  1079. nextOpt->posPrev = cur;
  1080. MakeAsChar(nextOpt);
  1081. nextIsChar = True;
  1082. }
  1083. matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
  1084. repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
  1085. if (matchByte == currentByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
  1086. {
  1087. UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
  1088. if (shortRepPrice <= nextOpt->price)
  1089. {
  1090. nextOpt->price = shortRepPrice;
  1091. nextOpt->posPrev = cur;
  1092. MakeAsShortRep(nextOpt);
  1093. nextIsChar = True;
  1094. }
  1095. }
  1096. {
  1097. UInt32 temp = kNumOpts - 1 - cur;
  1098. if (temp < numAvailableBytesFull)
  1099. numAvailableBytesFull = temp;
  1100. }
  1101. numAvailableBytes = numAvailableBytesFull;
  1102. if (numAvailableBytes < 2)
  1103. continue;
  1104. if (numAvailableBytes > p->numFastBytes)
  1105. numAvailableBytes = p->numFastBytes;
  1106. if (!nextIsChar && matchByte != currentByte) /* speed optimization */
  1107. {
  1108. /* try Literal + rep0 */
  1109. UInt32 temp;
  1110. UInt32 lenTest2;
  1111. const Byte *data2 = data - (reps[0] + 1);
  1112. UInt32 limit = p->numFastBytes + 1;
  1113. if (limit > numAvailableBytesFull)
  1114. limit = numAvailableBytesFull;
  1115. for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
  1116. lenTest2 = temp - 1;
  1117. if (lenTest2 >= 2)
  1118. {
  1119. UInt32 state2 = kLiteralNextStates[state];
  1120. UInt32 posStateNext = (position + 1) & p->pbMask;
  1121. UInt32 nextRepMatchPrice = curAnd1Price +
  1122. GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1123. GET_PRICE_1(p->isRep[state2]);
  1124. /* for (; lenTest2 >= 2; lenTest2--) */
  1125. {
  1126. UInt32 curAndLenPrice;
  1127. COptimal *opt;
  1128. UInt32 offset = cur + 1 + lenTest2;
  1129. while (lenEnd < offset)
  1130. p->opt[++lenEnd].price = kInfinityPrice;
  1131. curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1132. opt = &p->opt[offset];
  1133. if (curAndLenPrice < opt->price)
  1134. {
  1135. opt->price = curAndLenPrice;
  1136. opt->posPrev = cur + 1;
  1137. opt->backPrev = 0;
  1138. opt->prev1IsChar = True;
  1139. opt->prev2 = False;
  1140. }
  1141. }
  1142. }
  1143. }
  1144. startLen = 2; /* speed optimization */
  1145. {
  1146. UInt32 repIndex;
  1147. for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
  1148. {
  1149. UInt32 lenTest;
  1150. UInt32 lenTestTemp;
  1151. UInt32 price;
  1152. const Byte *data2 = data - (reps[repIndex] + 1);
  1153. if (data[0] != data2[0] || data[1] != data2[1])
  1154. continue;
  1155. for (lenTest = 2; lenTest < numAvailableBytes && data[lenTest] == data2[lenTest]; lenTest++);
  1156. while (lenEnd < cur + lenTest)
  1157. p->opt[++lenEnd].price = kInfinityPrice;
  1158. lenTestTemp = lenTest;
  1159. price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
  1160. do
  1161. {
  1162. UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
  1163. COptimal *opt = &p->opt[cur + lenTest];
  1164. if (curAndLenPrice < opt->price)
  1165. {
  1166. opt->price = curAndLenPrice;
  1167. opt->posPrev = cur;
  1168. opt->backPrev = repIndex;
  1169. opt->prev1IsChar = False;
  1170. }
  1171. }
  1172. while (--lenTest >= 2);
  1173. lenTest = lenTestTemp;
  1174. if (repIndex == 0)
  1175. startLen = lenTest + 1;
  1176. /* if (_maxMode) */
  1177. {
  1178. UInt32 lenTest2 = lenTest + 1;
  1179. UInt32 limit = lenTest2 + p->numFastBytes;
  1180. UInt32 nextRepMatchPrice;
  1181. if (limit > numAvailableBytesFull)
  1182. limit = numAvailableBytesFull;
  1183. for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  1184. lenTest2 -= lenTest + 1;
  1185. if (lenTest2 >= 2)
  1186. {
  1187. UInt32 state2 = kRepNextStates[state];
  1188. UInt32 posStateNext = (position + lenTest) & p->pbMask;
  1189. UInt32 curAndLenCharPrice =
  1190. price + p->repLenEnc.prices[posState][lenTest - 2] +
  1191. GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  1192. LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  1193. data[lenTest], data2[lenTest], p->ProbPrices);
  1194. state2 = kLiteralNextStates[state2];
  1195. posStateNext = (position + lenTest + 1) & p->pbMask;
  1196. nextRepMatchPrice = curAndLenCharPrice +
  1197. GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1198. GET_PRICE_1(p->isRep[state2]);
  1199. /* for (; lenTest2 >= 2; lenTest2--) */
  1200. {
  1201. UInt32 curAndLenPrice;
  1202. COptimal *opt;
  1203. UInt32 offset = cur + lenTest + 1 + lenTest2;
  1204. while (lenEnd < offset)
  1205. p->opt[++lenEnd].price = kInfinityPrice;
  1206. curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1207. opt = &p->opt[offset];
  1208. if (curAndLenPrice < opt->price)
  1209. {
  1210. opt->price = curAndLenPrice;
  1211. opt->posPrev = cur + lenTest + 1;
  1212. opt->backPrev = 0;
  1213. opt->prev1IsChar = True;
  1214. opt->prev2 = True;
  1215. opt->posPrev2 = cur;
  1216. opt->backPrev2 = repIndex;
  1217. }
  1218. }
  1219. }
  1220. }
  1221. }
  1222. }
  1223. /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
  1224. if (newLen > numAvailableBytes)
  1225. {
  1226. newLen = numAvailableBytes;
  1227. for (numDistancePairs = 0; newLen > matchDistances[numDistancePairs]; numDistancePairs += 2);
  1228. matchDistances[numDistancePairs] = newLen;
  1229. numDistancePairs += 2;
  1230. }
  1231. if (newLen >= startLen)
  1232. {
  1233. UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
  1234. UInt32 offs, curBack, posSlot;
  1235. UInt32 lenTest;
  1236. while (lenEnd < cur + newLen)
  1237. p->opt[++lenEnd].price = kInfinityPrice;
  1238. offs = 0;
  1239. while (startLen > matchDistances[offs])
  1240. offs += 2;
  1241. curBack = matchDistances[offs + 1];
  1242. GetPosSlot2(curBack, posSlot);
  1243. for (lenTest = /*2*/ startLen; ; lenTest++)
  1244. {
  1245. UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
  1246. UInt32 lenToPosState = GetLenToPosState(lenTest);
  1247. COptimal *opt;
  1248. if (curBack < kNumFullDistances)
  1249. curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
  1250. else
  1251. curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
  1252. opt = &p->opt[cur + lenTest];
  1253. if (curAndLenPrice < opt->price)
  1254. {
  1255. opt->price = curAndLenPrice;
  1256. opt->posPrev = cur;
  1257. opt->backPrev = curBack + LZMA_NUM_REPS;
  1258. opt->prev1IsChar = False;
  1259. }
  1260. if (/*_maxMode && */lenTest == matchDistances[offs])
  1261. {
  1262. /* Try Match + Literal + Rep0 */
  1263. const Byte *data2 = data - (curBack + 1);
  1264. UInt32 lenTest2 = lenTest + 1;
  1265. UInt32 limit = lenTest2 + p->numFastBytes;
  1266. UInt32 nextRepMatchPrice;
  1267. if (limit > numAvailableBytesFull)
  1268. limit = numAvailableBytesFull;
  1269. for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  1270. lenTest2 -= lenTest + 1;
  1271. if (lenTest2 >= 2)
  1272. {
  1273. UInt32 state2 = kMatchNextStates[state];
  1274. UInt32 posStateNext = (position + lenTest) & p->pbMask;
  1275. UInt32 curAndLenCharPrice = curAndLenPrice +
  1276. GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  1277. LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  1278. data[lenTest], data2[lenTest], p->ProbPrices);
  1279. state2 = kLiteralNextStates[state2];
  1280. posStateNext = (posStateNext + 1) & p->pbMask;
  1281. nextRepMatchPrice = curAndLenCharPrice +
  1282. GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1283. GET_PRICE_1(p->isRep[state2]);
  1284. /* for (; lenTest2 >= 2; lenTest2--) */
  1285. {
  1286. UInt32 offset = cur + lenTest + 1 + lenTest2;
  1287. UInt32 curAndLenPrice;
  1288. COptimal *opt;
  1289. while (lenEnd < offset)
  1290. p->opt[++lenEnd].price = kInfinityPrice;
  1291. curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1292. opt = &p->opt[offset];
  1293. if (curAndLenPrice < opt->price)
  1294. {
  1295. opt->price = curAndLenPrice;
  1296. opt->posPrev = cur + lenTest + 1;
  1297. opt->backPrev = 0;
  1298. opt->prev1IsChar = True;
  1299. opt->prev2 = True;
  1300. opt->posPrev2 = cur;
  1301. opt->backPrev2 = curBack + LZMA_NUM_REPS;
  1302. }
  1303. }
  1304. }
  1305. offs += 2;
  1306. if (offs == numDistancePairs)
  1307. break;
  1308. curBack = matchDistances[offs + 1];
  1309. if (curBack >= kNumFullDistances)
  1310. GetPosSlot2(curBack, posSlot);
  1311. }
  1312. }
  1313. }
  1314. }
  1315. }
  1316. #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
  1317. static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
  1318. {
  1319. UInt32 numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  1320. UInt32 lenMain, numDistancePairs;
  1321. const Byte *data;
  1322. UInt32 repLens[LZMA_NUM_REPS];
  1323. UInt32 repMaxIndex, i;
  1324. UInt32 *matchDistances;
  1325. UInt32 backMain;
  1326. if (!p->longestMatchWasFound)
  1327. {
  1328. lenMain = ReadMatchDistances(p, &numDistancePairs);
  1329. }
  1330. else
  1331. {
  1332. lenMain = p->longestMatchLength;
  1333. numDistancePairs = p->numDistancePairs;
  1334. p->longestMatchWasFound = False;
  1335. }
  1336. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1337. if (numAvailableBytes > LZMA_MATCH_LEN_MAX)
  1338. numAvailableBytes = LZMA_MATCH_LEN_MAX;
  1339. if (numAvailableBytes < 2)
  1340. {
  1341. *backRes = (UInt32)(-1);
  1342. return 1;
  1343. }
  1344. repMaxIndex = 0;
  1345. for (i = 0; i < LZMA_NUM_REPS; i++)
  1346. {
  1347. const Byte *data2 = data - (p->reps[i] + 1);
  1348. UInt32 len;
  1349. if (data[0] != data2[0] || data[1] != data2[1])
  1350. {
  1351. repLens[i] = 0;
  1352. continue;
  1353. }
  1354. for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
  1355. if (len >= p->numFastBytes)
  1356. {
  1357. *backRes = i;
  1358. MovePos(p, len - 1);
  1359. return len;
  1360. }
  1361. repLens[i] = len;
  1362. if (len > repLens[repMaxIndex])
  1363. repMaxIndex = i;
  1364. }
  1365. matchDistances = p->matchDistances;
  1366. if (lenMain >= p->numFastBytes)
  1367. {
  1368. *backRes = matchDistances[numDistancePairs - 1] + LZMA_NUM_REPS;
  1369. MovePos(p, lenMain - 1);
  1370. return lenMain;
  1371. }
  1372. backMain = 0; /* for GCC */
  1373. if (lenMain >= 2)
  1374. {
  1375. backMain = matchDistances[numDistancePairs - 1];
  1376. while (numDistancePairs > 2 && lenMain == matchDistances[numDistancePairs - 4] + 1)
  1377. {
  1378. if (!ChangePair(matchDistances[numDistancePairs - 3], backMain))
  1379. break;
  1380. numDistancePairs -= 2;
  1381. lenMain = matchDistances[numDistancePairs - 2];
  1382. backMain = matchDistances[numDistancePairs - 1];
  1383. }
  1384. if (lenMain == 2 && backMain >= 0x80)
  1385. lenMain = 1;
  1386. }
  1387. if (repLens[repMaxIndex] >= 2)
  1388. {
  1389. if (repLens[repMaxIndex] + 1 >= lenMain ||
  1390. (repLens[repMaxIndex] + 2 >= lenMain && (backMain > (1 << 9))) ||
  1391. (repLens[repMaxIndex] + 3 >= lenMain && (backMain > (1 << 15))))
  1392. {
  1393. UInt32 lenRes;
  1394. *backRes = repMaxIndex;
  1395. lenRes = repLens[repMaxIndex];
  1396. MovePos(p, lenRes - 1);
  1397. return lenRes;
  1398. }
  1399. }
  1400. if (lenMain >= 2 && numAvailableBytes > 2)
  1401. {
  1402. UInt32 i;
  1403. numAvailableBytes = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  1404. p->longestMatchLength = ReadMatchDistances(p, &p->numDistancePairs);
  1405. if (p->longestMatchLength >= 2)
  1406. {
  1407. UInt32 newDistance = matchDistances[p->numDistancePairs - 1];
  1408. if ((p->longestMatchLength >= lenMain && newDistance < backMain) ||
  1409. (p->longestMatchLength == lenMain + 1 && !ChangePair(backMain, newDistance)) ||
  1410. (p->longestMatchLength > lenMain + 1) ||
  1411. (p->longestMatchLength + 1 >= lenMain && lenMain >= 3 && ChangePair(newDistance, backMain)))
  1412. {
  1413. p->longestMatchWasFound = True;
  1414. *backRes = (UInt32)(-1);
  1415. return 1;
  1416. }
  1417. }
  1418. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1419. for (i = 0; i < LZMA_NUM_REPS; i++)
  1420. {
  1421. UInt32 len;
  1422. const Byte *data2 = data - (p->reps[i] + 1);
  1423. if (data[1] != data2[1] || data[2] != data2[2])
  1424. {
  1425. repLens[i] = 0;
  1426. continue;
  1427. }
  1428. for (len = 2; len < numAvailableBytes && data[len] == data2[len]; len++);
  1429. if (len + 1 >= lenMain)
  1430. {
  1431. p->longestMatchWasFound = True;
  1432. *backRes = (UInt32)(-1);
  1433. return 1;
  1434. }
  1435. }
  1436. *backRes = backMain + LZMA_NUM_REPS;
  1437. MovePos(p, lenMain - 2);
  1438. return lenMain;
  1439. }
  1440. *backRes = (UInt32)(-1);
  1441. return 1;
  1442. }
  1443. static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
  1444. {
  1445. UInt32 len;
  1446. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  1447. RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  1448. p->state = kMatchNextStates[p->state];
  1449. len = LZMA_MATCH_LEN_MIN;
  1450. LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1451. RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
  1452. RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
  1453. RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
  1454. }
  1455. static SRes CheckErrors(CLzmaEnc *p)
  1456. {
  1457. if (p->result != SZ_OK)
  1458. return p->result;
  1459. if (p->rc.res != SZ_OK)
  1460. p->result = SZ_ERROR_WRITE;
  1461. if (p->matchFinderBase.result != SZ_OK)
  1462. p->result = SZ_ERROR_READ;
  1463. if (p->result != SZ_OK)
  1464. p->finished = True;
  1465. return p->result;
  1466. }
  1467. static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
  1468. {
  1469. /* ReleaseMFStream(); */
  1470. p->finished = True;
  1471. if (p->writeEndMark)
  1472. WriteEndMarker(p, nowPos & p->pbMask);
  1473. RangeEnc_FlushData(&p->rc);
  1474. RangeEnc_FlushStream(&p->rc);
  1475. return CheckErrors(p);
  1476. }
  1477. static void FillAlignPrices(CLzmaEnc *p)
  1478. {
  1479. UInt32 i;
  1480. for (i = 0; i < kAlignTableSize; i++)
  1481. p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
  1482. p->alignPriceCount = 0;
  1483. }
  1484. static void FillDistancesPrices(CLzmaEnc *p)
  1485. {
  1486. UInt32 tempPrices[kNumFullDistances];
  1487. UInt32 i, lenToPosState;
  1488. for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
  1489. {
  1490. UInt32 posSlot = GetPosSlot1(i);
  1491. UInt32 footerBits = ((posSlot >> 1) - 1);
  1492. UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  1493. tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
  1494. }
  1495. for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
  1496. {
  1497. UInt32 posSlot;
  1498. const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
  1499. UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
  1500. for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
  1501. posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
  1502. for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
  1503. posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
  1504. {
  1505. UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
  1506. UInt32 i;
  1507. for (i = 0; i < kStartPosModelIndex; i++)
  1508. distancesPrices[i] = posSlotPrices[i];
  1509. for (; i < kNumFullDistances; i++)
  1510. distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i];
  1511. }
  1512. }
  1513. p->matchPriceCount = 0;
  1514. }
  1515. static void LzmaEnc_Construct(CLzmaEnc *p)
  1516. {
  1517. RangeEnc_Construct(&p->rc);
  1518. MatchFinder_Construct(&p->matchFinderBase);
  1519. #ifdef COMPRESS_MF_MT
  1520. MatchFinderMt_Construct(&p->matchFinderMt);
  1521. p->matchFinderMt.MatchFinder = &p->matchFinderBase;
  1522. #endif
  1523. {
  1524. CLzmaEncProps props;
  1525. LzmaEncProps_Init(&props);
  1526. LzmaEnc_SetProps(p, &props);
  1527. }
  1528. #ifndef LZMA_LOG_BSR
  1529. LzmaEnc_FastPosInit(p->g_FastPos);
  1530. #endif
  1531. LzmaEnc_InitPriceTables(p->ProbPrices);
  1532. p->litProbs = 0;
  1533. p->saveState.litProbs = 0;
  1534. }
  1535. CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
  1536. {
  1537. void *p;
  1538. p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
  1539. if (p != 0)
  1540. LzmaEnc_Construct((CLzmaEnc *)p);
  1541. return p;
  1542. }
  1543. static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
  1544. {
  1545. alloc->Free(alloc, p->litProbs);
  1546. alloc->Free(alloc, p->saveState.litProbs);
  1547. p->litProbs = 0;
  1548. p->saveState.litProbs = 0;
  1549. }
  1550. static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
  1551. {
  1552. #ifdef COMPRESS_MF_MT
  1553. MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
  1554. #endif
  1555. MatchFinder_Free(&p->matchFinderBase, allocBig);
  1556. LzmaEnc_FreeLits(p, alloc);
  1557. RangeEnc_Free(&p->rc, alloc);
  1558. }
  1559. void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
  1560. {
  1561. LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
  1562. alloc->Free(alloc, p);
  1563. }
  1564. static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
  1565. {
  1566. UInt32 nowPos32, startPos32;
  1567. if (p->inStream != 0)
  1568. {
  1569. p->matchFinderBase.stream = p->inStream;
  1570. p->matchFinder.Init(p->matchFinderObj);
  1571. p->inStream = 0;
  1572. }
  1573. if (p->finished)
  1574. return p->result;
  1575. RINOK(CheckErrors(p));
  1576. nowPos32 = (UInt32)p->nowPos64;
  1577. startPos32 = nowPos32;
  1578. if (p->nowPos64 == 0)
  1579. {
  1580. UInt32 numDistancePairs;
  1581. Byte curByte;
  1582. if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  1583. return Flush(p, nowPos32);
  1584. ReadMatchDistances(p, &numDistancePairs);
  1585. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
  1586. p->state = kLiteralNextStates[p->state];
  1587. curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
  1588. LitEnc_Encode(&p->rc, p->litProbs, curByte);
  1589. p->additionalOffset--;
  1590. nowPos32++;
  1591. }
  1592. if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
  1593. for (;;)
  1594. {
  1595. UInt32 pos, len, posState;
  1596. if (p->fastMode)
  1597. len = GetOptimumFast(p, &pos);
  1598. else
  1599. len = GetOptimum(p, nowPos32, &pos);
  1600. #ifdef SHOW_STAT2
  1601. printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
  1602. #endif
  1603. posState = nowPos32 & p->pbMask;
  1604. if (len == 1 && pos == 0xFFFFFFFF)
  1605. {
  1606. Byte curByte;
  1607. CLzmaProb *probs;
  1608. const Byte *data;
  1609. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
  1610. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
  1611. curByte = *data;
  1612. probs = LIT_PROBS(nowPos32, *(data - 1));
  1613. if (IsCharState(p->state))
  1614. LitEnc_Encode(&p->rc, probs, curByte);
  1615. else
  1616. LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
  1617. p->state = kLiteralNextStates[p->state];
  1618. }
  1619. else
  1620. {
  1621. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  1622. if (pos < LZMA_NUM_REPS)
  1623. {
  1624. RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
  1625. if (pos == 0)
  1626. {
  1627. RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
  1628. RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
  1629. }
  1630. else
  1631. {
  1632. UInt32 distance = p->reps[pos];
  1633. RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
  1634. if (pos == 1)
  1635. RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
  1636. else
  1637. {
  1638. RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
  1639. RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
  1640. if (pos == 3)
  1641. p->reps[3] = p->reps[2];
  1642. p->reps[2] = p->reps[1];
  1643. }
  1644. p->reps[1] = p->reps[0];
  1645. p->reps[0] = distance;
  1646. }
  1647. if (len == 1)
  1648. p->state = kShortRepNextStates[p->state];
  1649. else
  1650. {
  1651. LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1652. p->state = kRepNextStates[p->state];
  1653. }
  1654. }
  1655. else
  1656. {
  1657. UInt32 posSlot, lenToPosState;
  1658. RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  1659. p->state = kMatchNextStates[p->state];
  1660. LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1661. pos -= LZMA_NUM_REPS;
  1662. GetPosSlot(pos, posSlot);
  1663. lenToPosState = GetLenToPosState(len);
  1664. if (lenToPosState >= kNumLenToPosStates)
  1665. {
  1666. p->result = SZ_ERROR_DATA;
  1667. return CheckErrors(p);
  1668. }
  1669. RcTree_Encode(&p->rc, p->posSlotEncoder[lenToPosState], kNumPosSlotBits, posSlot);
  1670. if (posSlot >= kStartPosModelIndex)
  1671. {
  1672. UInt32 footerBits = ((posSlot >> 1) - 1);
  1673. UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  1674. UInt32 posReduced = pos - base;
  1675. if (posSlot < kEndPosModelIndex)
  1676. RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
  1677. else
  1678. {
  1679. RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
  1680. RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
  1681. p->alignPriceCount++;
  1682. }
  1683. }
  1684. p->reps[3] = p->reps[2];
  1685. p->reps[2] = p->reps[1];
  1686. p->reps[1] = p->reps[0];
  1687. p->reps[0] = pos;
  1688. p->matchPriceCount++;
  1689. }
  1690. }
  1691. p->additionalOffset -= len;
  1692. nowPos32 += len;
  1693. if (p->additionalOffset == 0)
  1694. {
  1695. UInt32 processed;
  1696. if (!p->fastMode)
  1697. {
  1698. if (p->matchPriceCount >= (1 << 7))
  1699. FillDistancesPrices(p);
  1700. if (p->alignPriceCount >= kAlignTableSize)
  1701. FillAlignPrices(p);
  1702. }
  1703. if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  1704. break;
  1705. processed = nowPos32 - startPos32;
  1706. if (useLimits)
  1707. {
  1708. if (processed + kNumOpts + 300 >= maxUnpackSize ||
  1709. RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
  1710. break;
  1711. }
  1712. else if (processed >= (1 << 15))
  1713. {
  1714. p->nowPos64 += nowPos32 - startPos32;
  1715. return CheckErrors(p);
  1716. }
  1717. }
  1718. }
  1719. p->nowPos64 += nowPos32 - startPos32;
  1720. return Flush(p, nowPos32);
  1721. }
  1722. #define kBigHashDicLimit ((UInt32)1 << 24)
  1723. static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1724. {
  1725. UInt32 beforeSize = kNumOpts;
  1726. #ifdef COMPRESS_MF_MT
  1727. Bool btMode;
  1728. #endif
  1729. if (!RangeEnc_Alloc(&p->rc, alloc))
  1730. return SZ_ERROR_MEM;
  1731. #ifdef COMPRESS_MF_MT
  1732. btMode = (p->matchFinderBase.btMode != 0);
  1733. p->mtMode = (p->multiThread && !p->fastMode && btMode);
  1734. #endif
  1735. {
  1736. unsigned lclp = p->lc + p->lp;
  1737. if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
  1738. {
  1739. LzmaEnc_FreeLits(p, alloc);
  1740. p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  1741. p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  1742. if (p->litProbs == 0 || p->saveState.litProbs == 0)
  1743. {
  1744. LzmaEnc_FreeLits(p, alloc);
  1745. return SZ_ERROR_MEM;
  1746. }
  1747. p->lclp = lclp;
  1748. }
  1749. }
  1750. p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
  1751. if (beforeSize + p->dictSize < keepWindowSize)
  1752. beforeSize = keepWindowSize - p->dictSize;
  1753. #ifdef COMPRESS_MF_MT
  1754. if (p->mtMode)
  1755. {
  1756. RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
  1757. p->matchFinderObj = &p->matchFinderMt;
  1758. MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
  1759. }
  1760. else
  1761. #endif
  1762. {
  1763. if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
  1764. return SZ_ERROR_MEM;
  1765. p->matchFinderObj = &p->matchFinderBase;
  1766. MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
  1767. }
  1768. return SZ_OK;
  1769. }
  1770. static void LzmaEnc_Init(CLzmaEnc *p)
  1771. {
  1772. UInt32 i;
  1773. p->state = 0;
  1774. for(i = 0 ; i < LZMA_NUM_REPS; i++)
  1775. p->reps[i] = 0;
  1776. RangeEnc_Init(&p->rc);
  1777. for (i = 0; i < kNumStates; i++)
  1778. {
  1779. UInt32 j;
  1780. for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
  1781. {
  1782. p->isMatch[i][j] = kProbInitValue;
  1783. p->isRep0Long[i][j] = kProbInitValue;
  1784. }
  1785. p->isRep[i] = kProbInitValue;
  1786. p->isRepG0[i] = kProbInitValue;
  1787. p->isRepG1[i] = kProbInitValue;
  1788. p->isRepG2[i] = kProbInitValue;
  1789. }
  1790. {
  1791. UInt32 num = 0x300 << (p->lp + p->lc);
  1792. for (i = 0; i < num; i++)
  1793. p->litProbs[i] = kProbInitValue;
  1794. }
  1795. {
  1796. for (i = 0; i < kNumLenToPosStates; i++)
  1797. {
  1798. CLzmaProb *probs = p->posSlotEncoder[i];
  1799. UInt32 j;
  1800. for (j = 0; j < (1 << kNumPosSlotBits); j++)
  1801. probs[j] = kProbInitValue;
  1802. }
  1803. }
  1804. {
  1805. for(i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
  1806. p->posEncoders[i] = kProbInitValue;
  1807. }
  1808. LenEnc_Init(&p->lenEnc.p);
  1809. LenEnc_Init(&p->repLenEnc.p);
  1810. for (i = 0; i < (1 << kNumAlignBits); i++)
  1811. p->posAlignEncoder[i] = kProbInitValue;
  1812. p->longestMatchWasFound = False;
  1813. p->optimumEndIndex = 0;
  1814. p->optimumCurrentIndex = 0;
  1815. p->additionalOffset = 0;
  1816. p->pbMask = (1 << p->pb) - 1;
  1817. p->lpMask = (1 << p->lp) - 1;
  1818. }
  1819. static void LzmaEnc_InitPrices(CLzmaEnc *p)
  1820. {
  1821. if (!p->fastMode)
  1822. {
  1823. FillDistancesPrices(p);
  1824. FillAlignPrices(p);
  1825. }
  1826. p->lenEnc.tableSize =
  1827. p->repLenEnc.tableSize =
  1828. p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
  1829. LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
  1830. LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
  1831. }
  1832. static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1833. {
  1834. UInt32 i;
  1835. for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
  1836. if (p->dictSize <= ((UInt32)1 << i))
  1837. break;
  1838. p->distTableSize = i * 2;
  1839. p->finished = False;
  1840. p->result = SZ_OK;
  1841. RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
  1842. LzmaEnc_Init(p);
  1843. LzmaEnc_InitPrices(p);
  1844. p->nowPos64 = 0;
  1845. return SZ_OK;
  1846. }
  1847. static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream,
  1848. ISzAlloc *alloc, ISzAlloc *allocBig)
  1849. {
  1850. CLzmaEnc *p = (CLzmaEnc *)pp;
  1851. p->inStream = inStream;
  1852. p->rc.outStream = outStream;
  1853. return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
  1854. }
  1855. static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
  1856. {
  1857. p->seqBufInStream.funcTable.Read = MyRead;
  1858. p->seqBufInStream.data = src;
  1859. p->seqBufInStream.rem = srcLen;
  1860. }
  1861. static void LzmaEnc_Finish(CLzmaEncHandle pp)
  1862. {
  1863. #ifdef COMPRESS_MF_MT
  1864. CLzmaEnc *p = (CLzmaEnc *)pp;
  1865. if (p->mtMode)
  1866. MatchFinderMt_ReleaseStream(&p->matchFinderMt);
  1867. #else
  1868. (void)pp;
  1869. #endif
  1870. }
  1871. typedef struct _CSeqOutStreamBuf
  1872. {
  1873. ISeqOutStream funcTable;
  1874. Byte *data;
  1875. SizeT rem;
  1876. Bool overflow;
  1877. } CSeqOutStreamBuf;
  1878. static size_t MyWrite(void *pp, const void *data, size_t size)
  1879. {
  1880. CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
  1881. if (p->rem < size)
  1882. {
  1883. size = p->rem;
  1884. p->overflow = True;
  1885. }
  1886. memcpy(p->data, data, size);
  1887. p->rem -= size;
  1888. p->data += size;
  1889. return size;
  1890. }
  1891. SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
  1892. ISzAlloc *alloc, ISzAlloc *allocBig)
  1893. {
  1894. CLzmaEnc *p = (CLzmaEnc *)pp;
  1895. SRes res = SZ_OK;
  1896. #ifdef COMPRESS_MF_MT
  1897. Byte allocaDummy[0x300];
  1898. int i = 0;
  1899. for (i = 0; i < 16; i++)
  1900. allocaDummy[i] = (Byte)i;
  1901. #endif
  1902. RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig));
  1903. for (;;)
  1904. {
  1905. res = LzmaEnc_CodeOneBlock(pp, False, 0, 0);
  1906. if (res != SZ_OK || p->finished != 0)
  1907. break;
  1908. if (progress != 0)
  1909. {
  1910. res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
  1911. if (res != SZ_OK)
  1912. {
  1913. res = SZ_ERROR_PROGRESS;
  1914. break;
  1915. }
  1916. }
  1917. }
  1918. LzmaEnc_Finish(pp);
  1919. return res;
  1920. }
  1921. SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
  1922. {
  1923. CLzmaEnc *p = (CLzmaEnc *)pp;
  1924. int i;
  1925. UInt32 dictSize = p->dictSize;
  1926. if (*size < LZMA_PROPS_SIZE)
  1927. return SZ_ERROR_PARAM;
  1928. *size = LZMA_PROPS_SIZE;
  1929. props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
  1930. for (i = 11; i <= 30; i++)
  1931. {
  1932. if (dictSize <= ((UInt32)2 << i))
  1933. {
  1934. dictSize = (2 << i);
  1935. break;
  1936. }
  1937. if (dictSize <= ((UInt32)3 << i))
  1938. {
  1939. dictSize = (3 << i);
  1940. break;
  1941. }
  1942. }
  1943. for (i = 0; i < 4; i++)
  1944. props[1 + i] = (Byte)(dictSize >> (8 * i));
  1945. return SZ_OK;
  1946. }
  1947. SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  1948. int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  1949. {
  1950. SRes res;
  1951. CLzmaEnc *p = (CLzmaEnc *)pp;
  1952. CSeqOutStreamBuf outStream;
  1953. LzmaEnc_SetInputBuf(p, src, srcLen);
  1954. outStream.funcTable.Write = MyWrite;
  1955. outStream.data = dest;
  1956. outStream.rem = *destLen;
  1957. outStream.overflow = False;
  1958. p->writeEndMark = writeEndMark;
  1959. res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable,
  1960. progress, alloc, allocBig);
  1961. *destLen -= outStream.rem;
  1962. if (outStream.overflow)
  1963. return SZ_ERROR_OUTPUT_EOF;
  1964. return res;
  1965. }
  1966. SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  1967. const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
  1968. ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  1969. {
  1970. CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
  1971. SRes res;
  1972. if (p == 0)
  1973. return SZ_ERROR_MEM;
  1974. res = LzmaEnc_SetProps(p, props);
  1975. if (res == SZ_OK)
  1976. {
  1977. res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
  1978. if (res == SZ_OK)
  1979. res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
  1980. writeEndMark, progress, alloc, allocBig);
  1981. }
  1982. LzmaEnc_Destroy(p, alloc, allocBig);
  1983. return res;
  1984. }