lz4opt.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. lz4opt.h - Optimal Mode of LZ4
  3. Copyright (C) 2015-2016, Przemyslaw Skibinski <[email protected]>
  4. Note : this file is intended to be included within lz4hc.c
  5. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. You can contact the author at :
  27. - LZ4 source repository : https://github.com/lz4/lz4
  28. - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
  29. */
  30. #define LZ4_OPT_NUM (1<<12)
  31. typedef struct
  32. {
  33. int off;
  34. int len;
  35. } LZ4HC_match_t;
  36. typedef struct
  37. {
  38. int price;
  39. int off;
  40. int mlen;
  41. int litlen;
  42. } LZ4HC_optimal_t;
  43. /* price in bits */
  44. FORCE_INLINE size_t LZ4HC_literalsPrice(size_t litlen)
  45. {
  46. size_t price = 8*litlen;
  47. if (litlen >= (size_t)RUN_MASK) price+=8*(1+(litlen-RUN_MASK)/255);
  48. return price;
  49. }
  50. /* requires mlen >= MINMATCH */
  51. FORCE_INLINE size_t LZ4HC_sequencePrice(size_t litlen, size_t mlen)
  52. {
  53. size_t price = 16 + 8; /* 16-bit offset + token */
  54. price += LZ4HC_literalsPrice(litlen);
  55. mlen -= MINMATCH;
  56. if (mlen >= (size_t)ML_MASK) price+=8*(1+(mlen-ML_MASK)/255);
  57. return price;
  58. }
  59. /*-*************************************
  60. * Binary Tree search
  61. ***************************************/
  62. FORCE_INLINE int LZ4HC_BinTree_InsertAndGetAllMatches (
  63. LZ4HC_CCtx_internal* ctx,
  64. const BYTE* const ip,
  65. const BYTE* const iHighLimit,
  66. size_t best_mlen,
  67. LZ4HC_match_t* matches,
  68. int* matchNum)
  69. {
  70. U16* const chainTable = ctx->chainTable;
  71. U32* const HashTable = ctx->hashTable;
  72. const BYTE* const base = ctx->base;
  73. const U32 dictLimit = ctx->dictLimit;
  74. const U32 current = (U32)(ip - base);
  75. const U32 lowLimit = (ctx->lowLimit + MAX_DISTANCE > current) ? ctx->lowLimit : current - (MAX_DISTANCE - 1);
  76. const BYTE* const dictBase = ctx->dictBase;
  77. const BYTE* match;
  78. int nbAttempts = ctx->searchNum;
  79. int mnum = 0;
  80. U16 *ptr0, *ptr1, delta0, delta1;
  81. U32 matchIndex;
  82. size_t matchLength = 0;
  83. U32* HashPos;
  84. if (ip + MINMATCH > iHighLimit) return 1;
  85. /* HC4 match finder */
  86. HashPos = &HashTable[LZ4HC_hashPtr(ip)];
  87. matchIndex = *HashPos;
  88. *HashPos = current;
  89. ptr0 = &DELTANEXTMAXD(current*2+1);
  90. ptr1 = &DELTANEXTMAXD(current*2);
  91. delta0 = delta1 = (U16)(current - matchIndex);
  92. while ((matchIndex < current) && (matchIndex>=lowLimit) && (nbAttempts)) {
  93. nbAttempts--;
  94. if (matchIndex >= dictLimit) {
  95. match = base + matchIndex;
  96. matchLength = LZ4_count(ip, match, iHighLimit);
  97. } else {
  98. const BYTE* vLimit = ip + (dictLimit - matchIndex);
  99. match = dictBase + matchIndex;
  100. if (vLimit > iHighLimit) vLimit = iHighLimit;
  101. matchLength = LZ4_count(ip, match, vLimit);
  102. if ((ip+matchLength == vLimit) && (vLimit < iHighLimit))
  103. matchLength += LZ4_count(ip+matchLength, base+dictLimit, iHighLimit);
  104. }
  105. if (matchLength > best_mlen) {
  106. best_mlen = matchLength;
  107. if (matches) {
  108. if (matchIndex >= dictLimit)
  109. matches[mnum].off = (int)(ip - match);
  110. else
  111. matches[mnum].off = (int)(ip - (base + matchIndex)); /* virtual matchpos */
  112. matches[mnum].len = (int)matchLength;
  113. mnum++;
  114. }
  115. if (best_mlen > LZ4_OPT_NUM) break;
  116. }
  117. if (ip+matchLength >= iHighLimit) /* equal : no way to know if inf or sup */
  118. break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt the tree */
  119. if (*(ip+matchLength) < *(match+matchLength)) {
  120. *ptr0 = delta0;
  121. ptr0 = &DELTANEXTMAXD(matchIndex*2);
  122. if (*ptr0 == (U16)-1) break;
  123. delta0 = *ptr0;
  124. delta1 += delta0;
  125. matchIndex -= delta0;
  126. } else {
  127. *ptr1 = delta1;
  128. ptr1 = &DELTANEXTMAXD(matchIndex*2+1);
  129. if (*ptr1 == (U16)-1) break;
  130. delta1 = *ptr1;
  131. delta0 += delta1;
  132. matchIndex -= delta1;
  133. }
  134. }
  135. *ptr0 = (U16)-1;
  136. *ptr1 = (U16)-1;
  137. if (matchNum) *matchNum = mnum;
  138. /* if (best_mlen > 8) return best_mlen-8; */
  139. if (!matchNum) return 1;
  140. return 1;
  141. }
  142. FORCE_INLINE void LZ4HC_updateBinTree(LZ4HC_CCtx_internal* ctx, const BYTE* const ip, const BYTE* const iHighLimit)
  143. {
  144. const BYTE* const base = ctx->base;
  145. const U32 target = (U32)(ip - base);
  146. U32 idx = ctx->nextToUpdate;
  147. while(idx < target)
  148. idx += LZ4HC_BinTree_InsertAndGetAllMatches(ctx, base+idx, iHighLimit, 8, NULL, NULL);
  149. }
  150. /** Tree updater, providing best match */
  151. FORCE_INLINE int LZ4HC_BinTree_GetAllMatches (
  152. LZ4HC_CCtx_internal* ctx,
  153. const BYTE* const ip, const BYTE* const iHighLimit,
  154. size_t best_mlen, LZ4HC_match_t* matches, const int fullUpdate)
  155. {
  156. int mnum = 0;
  157. if (ip < ctx->base + ctx->nextToUpdate) return 0; /* skipped area */
  158. if (fullUpdate) LZ4HC_updateBinTree(ctx, ip, iHighLimit);
  159. best_mlen = LZ4HC_BinTree_InsertAndGetAllMatches(ctx, ip, iHighLimit, best_mlen, matches, &mnum);
  160. ctx->nextToUpdate = (U32)(ip - ctx->base + best_mlen);
  161. return mnum;
  162. }
  163. #define SET_PRICE(pos, mlen, offset, litlen, price) \
  164. { \
  165. while (last_pos < pos) { opt[last_pos+1].price = 1<<30; last_pos++; } \
  166. opt[pos].mlen = (int)mlen; \
  167. opt[pos].off = (int)offset; \
  168. opt[pos].litlen = (int)litlen; \
  169. opt[pos].price = (int)price; \
  170. }
  171. static int LZ4HC_compress_optimal (
  172. LZ4HC_CCtx_internal* ctx,
  173. const char* const source,
  174. char* dest,
  175. int inputSize,
  176. int maxOutputSize,
  177. limitedOutput_directive limit,
  178. const size_t sufficient_len,
  179. const int fullUpdate
  180. )
  181. {
  182. LZ4HC_optimal_t opt[LZ4_OPT_NUM + 1];
  183. LZ4HC_match_t matches[LZ4_OPT_NUM + 1];
  184. const BYTE *inr = NULL;
  185. size_t res, cur, cur2;
  186. size_t i, llen, litlen, mlen, best_mlen, price, offset, best_off, match_num, last_pos;
  187. const BYTE* ip = (const BYTE*) source;
  188. const BYTE* anchor = ip;
  189. const BYTE* const iend = ip + inputSize;
  190. const BYTE* const mflimit = iend - MFLIMIT;
  191. const BYTE* const matchlimit = (iend - LASTLITERALS);
  192. BYTE* op = (BYTE*) dest;
  193. BYTE* const oend = op + maxOutputSize;
  194. /* init */
  195. ctx->end += inputSize;
  196. ip++;
  197. /* Main Loop */
  198. while (ip < mflimit) {
  199. memset(opt, 0, sizeof(LZ4HC_optimal_t));
  200. last_pos = 0;
  201. llen = ip - anchor;
  202. match_num = LZ4HC_BinTree_GetAllMatches(ctx, ip, matchlimit, MINMATCH-1, matches, fullUpdate);
  203. if (!match_num) { ip++; continue; }
  204. if ((size_t)matches[match_num-1].len > sufficient_len) {
  205. best_mlen = matches[match_num-1].len;
  206. best_off = matches[match_num-1].off;
  207. cur = 0;
  208. last_pos = 1;
  209. goto encode;
  210. }
  211. /* set prices using matches at position = 0 */
  212. for (i = 0; i < match_num; i++) {
  213. mlen = (i>0) ? (size_t)matches[i-1].len+1 : MINMATCH;
  214. best_mlen = (matches[i].len < LZ4_OPT_NUM) ? matches[i].len : LZ4_OPT_NUM;
  215. while (mlen <= best_mlen) {
  216. litlen = 0;
  217. price = LZ4HC_sequencePrice(llen + litlen, mlen) - LZ4HC_literalsPrice(llen);
  218. SET_PRICE(mlen, mlen, matches[i].off, litlen, price);
  219. mlen++;
  220. }
  221. }
  222. if (last_pos < MINMATCH) { ip++; continue; }
  223. /* check further positions */
  224. opt[0].mlen = opt[1].mlen = 1;
  225. for (cur = 1; cur <= last_pos; cur++) {
  226. inr = ip + cur;
  227. if (opt[cur-1].mlen == 1) {
  228. litlen = opt[cur-1].litlen + 1;
  229. if (cur != litlen) {
  230. price = opt[cur - litlen].price + LZ4HC_literalsPrice(litlen);
  231. } else {
  232. price = LZ4HC_literalsPrice(llen + litlen) - LZ4HC_literalsPrice(llen);
  233. }
  234. } else {
  235. litlen = 1;
  236. price = opt[cur - 1].price + LZ4HC_literalsPrice(litlen);
  237. }
  238. mlen = 1;
  239. best_mlen = 0;
  240. if (cur > last_pos || price < (size_t)opt[cur].price)
  241. SET_PRICE(cur, mlen, best_mlen, litlen, price);
  242. if (cur == last_pos || inr >= mflimit) break;
  243. match_num = LZ4HC_BinTree_GetAllMatches(ctx, inr, matchlimit, MINMATCH-1, matches, fullUpdate);
  244. if (match_num > 0 && (size_t)matches[match_num-1].len > sufficient_len) {
  245. best_mlen = matches[match_num-1].len;
  246. best_off = matches[match_num-1].off;
  247. last_pos = cur + 1;
  248. goto encode;
  249. }
  250. /* set prices using matches at position = cur */
  251. for (i = 0; i < match_num; i++) {
  252. mlen = (i>0) ? (size_t)matches[i-1].len+1 : MINMATCH;
  253. cur2 = cur;
  254. best_mlen = (cur2 + matches[i].len < LZ4_OPT_NUM) ? (size_t)matches[i].len : LZ4_OPT_NUM - cur2;
  255. while (mlen <= best_mlen) {
  256. if (opt[cur2].mlen == 1) {
  257. litlen = opt[cur2].litlen;
  258. if (cur2 != litlen)
  259. price = opt[cur2 - litlen].price + LZ4HC_sequencePrice(litlen, mlen);
  260. else
  261. price = LZ4HC_sequencePrice(llen + litlen, mlen) - LZ4HC_literalsPrice(llen);
  262. } else {
  263. litlen = 0;
  264. price = opt[cur2].price + LZ4HC_sequencePrice(litlen, mlen);
  265. }
  266. if (cur2 + mlen > last_pos || price < (size_t)opt[cur2 + mlen].price) { // || (((int)price == opt[cur2 + mlen].price) && (opt[cur2 + mlen-1].mlen == 1))) {
  267. SET_PRICE(cur2 + mlen, mlen, matches[i].off, litlen, price);
  268. }
  269. mlen++;
  270. }
  271. }
  272. } /* for (cur = 1; cur <= last_pos; cur++) */
  273. best_mlen = opt[last_pos].mlen;
  274. best_off = opt[last_pos].off;
  275. cur = last_pos - best_mlen;
  276. encode: /* cur, last_pos, best_mlen, best_off have to be set */
  277. opt[0].mlen = 1;
  278. while (1) {
  279. mlen = opt[cur].mlen;
  280. offset = opt[cur].off;
  281. opt[cur].mlen = (int)best_mlen;
  282. opt[cur].off = (int)best_off;
  283. best_mlen = mlen;
  284. best_off = offset;
  285. if (mlen > cur) break;
  286. cur -= mlen;
  287. }
  288. cur = 0;
  289. while (cur < last_pos) {
  290. mlen = opt[cur].mlen;
  291. if (mlen == 1) { ip++; cur++; continue; }
  292. offset = opt[cur].off;
  293. cur += mlen;
  294. res = LZ4HC_encodeSequence(&ip, &op, &anchor, (int)mlen, ip - offset, limit, oend);
  295. if (res) return 0;
  296. }
  297. }
  298. /* Encode Last Literals */
  299. { int lastRun = (int)(iend - anchor);
  300. if ((limit) && (((char*)op - dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize)) return 0; /* Check output limit */
  301. if (lastRun>=(int)RUN_MASK) { *op++=(RUN_MASK<<ML_BITS); lastRun-=RUN_MASK; for(; lastRun > 254 ; lastRun-=255) *op++ = 255; *op++ = (BYTE) lastRun; }
  302. else *op++ = (BYTE)(lastRun<<ML_BITS);
  303. memcpy(op, anchor, iend - anchor);
  304. op += iend-anchor;
  305. }
  306. /* End */
  307. return (int) ((char*)op-dest);
  308. }