lz4_compress.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * LZ4 - Fast LZ compression algorithm
  3. * Copyright (C) 2011 - 2016, Yann Collet.
  4. * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. * You can contact the author at :
  26. * - LZ4 homepage : http://www.lz4.org
  27. * - LZ4 source repository : https://github.com/lz4/lz4
  28. *
  29. * Changed for kernel usage by:
  30. * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
  31. */
  32. /*-************************************
  33. * Dependencies
  34. **************************************/
  35. #include <linux/lz4.h>
  36. #include "lz4defs.h"
  37. #include <linux/module.h>
  38. #include <linux/kernel.h>
  39. #include <asm/unaligned.h>
  40. static const int LZ4_minLength = (MFLIMIT + 1);
  41. static const int LZ4_64Klimit = ((64 * KB) + (MFLIMIT - 1));
  42. /*-******************************
  43. * Compression functions
  44. ********************************/
  45. static FORCE_INLINE U32 LZ4_hash4(
  46. U32 sequence,
  47. tableType_t const tableType)
  48. {
  49. if (tableType == byU16)
  50. return ((sequence * 2654435761U)
  51. >> ((MINMATCH * 8) - (LZ4_HASHLOG + 1)));
  52. else
  53. return ((sequence * 2654435761U)
  54. >> ((MINMATCH * 8) - LZ4_HASHLOG));
  55. }
  56. static FORCE_INLINE U32 LZ4_hash5(
  57. U64 sequence,
  58. tableType_t const tableType)
  59. {
  60. const U32 hashLog = (tableType == byU16)
  61. ? LZ4_HASHLOG + 1
  62. : LZ4_HASHLOG;
  63. #if LZ4_LITTLE_ENDIAN
  64. static const U64 prime5bytes = 889523592379ULL;
  65. return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
  66. #else
  67. static const U64 prime8bytes = 11400714785074694791ULL;
  68. return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
  69. #endif
  70. }
  71. static FORCE_INLINE U32 LZ4_hashPosition(
  72. const void *p,
  73. tableType_t const tableType)
  74. {
  75. #if LZ4_ARCH64
  76. if (tableType == byU32)
  77. return LZ4_hash5(LZ4_read_ARCH(p), tableType);
  78. #endif
  79. return LZ4_hash4(LZ4_read32(p), tableType);
  80. }
  81. static void LZ4_putPositionOnHash(
  82. const BYTE *p,
  83. U32 h,
  84. void *tableBase,
  85. tableType_t const tableType,
  86. const BYTE *srcBase)
  87. {
  88. switch (tableType) {
  89. case byPtr:
  90. {
  91. const BYTE **hashTable = (const BYTE **)tableBase;
  92. hashTable[h] = p;
  93. return;
  94. }
  95. case byU32:
  96. {
  97. U32 *hashTable = (U32 *) tableBase;
  98. hashTable[h] = (U32)(p - srcBase);
  99. return;
  100. }
  101. case byU16:
  102. {
  103. U16 *hashTable = (U16 *) tableBase;
  104. hashTable[h] = (U16)(p - srcBase);
  105. return;
  106. }
  107. }
  108. }
  109. static FORCE_INLINE void LZ4_putPosition(
  110. const BYTE *p,
  111. void *tableBase,
  112. tableType_t tableType,
  113. const BYTE *srcBase)
  114. {
  115. U32 const h = LZ4_hashPosition(p, tableType);
  116. LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
  117. }
  118. static const BYTE *LZ4_getPositionOnHash(
  119. U32 h,
  120. void *tableBase,
  121. tableType_t tableType,
  122. const BYTE *srcBase)
  123. {
  124. if (tableType == byPtr) {
  125. const BYTE **hashTable = (const BYTE **) tableBase;
  126. return hashTable[h];
  127. }
  128. if (tableType == byU32) {
  129. const U32 * const hashTable = (U32 *) tableBase;
  130. return hashTable[h] + srcBase;
  131. }
  132. {
  133. /* default, to ensure a return */
  134. const U16 * const hashTable = (U16 *) tableBase;
  135. return hashTable[h] + srcBase;
  136. }
  137. }
  138. static FORCE_INLINE const BYTE *LZ4_getPosition(
  139. const BYTE *p,
  140. void *tableBase,
  141. tableType_t tableType,
  142. const BYTE *srcBase)
  143. {
  144. U32 const h = LZ4_hashPosition(p, tableType);
  145. return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
  146. }
  147. /*
  148. * LZ4_compress_generic() :
  149. * inlined, to ensure branches are decided at compilation time
  150. */
  151. static FORCE_INLINE int LZ4_compress_generic(
  152. LZ4_stream_t_internal * const dictPtr,
  153. const char * const source,
  154. char * const dest,
  155. const int inputSize,
  156. const int maxOutputSize,
  157. const limitedOutput_directive outputLimited,
  158. const tableType_t tableType,
  159. const dict_directive dict,
  160. const dictIssue_directive dictIssue,
  161. const U32 acceleration)
  162. {
  163. const BYTE *ip = (const BYTE *) source;
  164. const BYTE *base;
  165. const BYTE *lowLimit;
  166. const BYTE * const lowRefLimit = ip - dictPtr->dictSize;
  167. const BYTE * const dictionary = dictPtr->dictionary;
  168. const BYTE * const dictEnd = dictionary + dictPtr->dictSize;
  169. const size_t dictDelta = dictEnd - (const BYTE *)source;
  170. const BYTE *anchor = (const BYTE *) source;
  171. const BYTE * const iend = ip + inputSize;
  172. const BYTE * const mflimit = iend - MFLIMIT;
  173. const BYTE * const matchlimit = iend - LASTLITERALS;
  174. BYTE *op = (BYTE *) dest;
  175. BYTE * const olimit = op + maxOutputSize;
  176. U32 forwardH;
  177. size_t refDelta = 0;
  178. /* Init conditions */
  179. if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) {
  180. /* Unsupported inputSize, too large (or negative) */
  181. return 0;
  182. }
  183. switch (dict) {
  184. case noDict:
  185. default:
  186. base = (const BYTE *)source;
  187. lowLimit = (const BYTE *)source;
  188. break;
  189. case withPrefix64k:
  190. base = (const BYTE *)source - dictPtr->currentOffset;
  191. lowLimit = (const BYTE *)source - dictPtr->dictSize;
  192. break;
  193. case usingExtDict:
  194. base = (const BYTE *)source - dictPtr->currentOffset;
  195. lowLimit = (const BYTE *)source;
  196. break;
  197. }
  198. if ((tableType == byU16)
  199. && (inputSize >= LZ4_64Klimit)) {
  200. /* Size too large (not within 64K limit) */
  201. return 0;
  202. }
  203. if (inputSize < LZ4_minLength) {
  204. /* Input too small, no compression (all literals) */
  205. goto _last_literals;
  206. }
  207. /* First Byte */
  208. LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
  209. ip++;
  210. forwardH = LZ4_hashPosition(ip, tableType);
  211. /* Main Loop */
  212. for ( ; ; ) {
  213. const BYTE *match;
  214. BYTE *token;
  215. /* Find a match */
  216. {
  217. const BYTE *forwardIp = ip;
  218. unsigned int step = 1;
  219. unsigned int searchMatchNb = acceleration << LZ4_SKIPTRIGGER;
  220. do {
  221. U32 const h = forwardH;
  222. ip = forwardIp;
  223. forwardIp += step;
  224. step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
  225. if (unlikely(forwardIp > mflimit))
  226. goto _last_literals;
  227. match = LZ4_getPositionOnHash(h,
  228. dictPtr->hashTable,
  229. tableType, base);
  230. if (dict == usingExtDict) {
  231. if (match < (const BYTE *)source) {
  232. refDelta = dictDelta;
  233. lowLimit = dictionary;
  234. } else {
  235. refDelta = 0;
  236. lowLimit = (const BYTE *)source;
  237. } }
  238. forwardH = LZ4_hashPosition(forwardIp,
  239. tableType);
  240. LZ4_putPositionOnHash(ip, h, dictPtr->hashTable,
  241. tableType, base);
  242. } while (((dictIssue == dictSmall)
  243. ? (match < lowRefLimit)
  244. : 0)
  245. || ((tableType == byU16)
  246. ? 0
  247. : (match + MAX_DISTANCE < ip))
  248. || (LZ4_read32(match + refDelta)
  249. != LZ4_read32(ip)));
  250. }
  251. /* Catch up */
  252. while (((ip > anchor) & (match + refDelta > lowLimit))
  253. && (unlikely(ip[-1] == match[refDelta - 1]))) {
  254. ip--;
  255. match--;
  256. }
  257. /* Encode Literals */
  258. {
  259. unsigned const int litLength = (unsigned int)(ip - anchor);
  260. token = op++;
  261. if ((outputLimited) &&
  262. /* Check output buffer overflow */
  263. (unlikely(op + litLength +
  264. (2 + 1 + LASTLITERALS) +
  265. (litLength / 255) > olimit)))
  266. return 0;
  267. if (litLength >= RUN_MASK) {
  268. int len = (int)litLength - RUN_MASK;
  269. *token = (RUN_MASK << ML_BITS);
  270. for (; len >= 255; len -= 255)
  271. *op++ = 255;
  272. *op++ = (BYTE)len;
  273. } else
  274. *token = (BYTE)(litLength << ML_BITS);
  275. /* Copy Literals */
  276. LZ4_wildCopy(op, anchor, op + litLength);
  277. op += litLength;
  278. }
  279. _next_match:
  280. /* Encode Offset */
  281. LZ4_writeLE16(op, (U16)(ip - match));
  282. op += 2;
  283. /* Encode MatchLength */
  284. {
  285. unsigned int matchCode;
  286. if ((dict == usingExtDict)
  287. && (lowLimit == dictionary)) {
  288. const BYTE *limit;
  289. match += refDelta;
  290. limit = ip + (dictEnd - match);
  291. if (limit > matchlimit)
  292. limit = matchlimit;
  293. matchCode = LZ4_count(ip + MINMATCH,
  294. match + MINMATCH, limit);
  295. ip += MINMATCH + matchCode;
  296. if (ip == limit) {
  297. unsigned const int more = LZ4_count(ip,
  298. (const BYTE *)source,
  299. matchlimit);
  300. matchCode += more;
  301. ip += more;
  302. }
  303. } else {
  304. matchCode = LZ4_count(ip + MINMATCH,
  305. match + MINMATCH, matchlimit);
  306. ip += MINMATCH + matchCode;
  307. }
  308. if (outputLimited &&
  309. /* Check output buffer overflow */
  310. (unlikely(op +
  311. (1 + LASTLITERALS) +
  312. (matchCode >> 8) > olimit)))
  313. return 0;
  314. if (matchCode >= ML_MASK) {
  315. *token += ML_MASK;
  316. matchCode -= ML_MASK;
  317. LZ4_write32(op, 0xFFFFFFFF);
  318. while (matchCode >= 4 * 255) {
  319. op += 4;
  320. LZ4_write32(op, 0xFFFFFFFF);
  321. matchCode -= 4 * 255;
  322. }
  323. op += matchCode / 255;
  324. *op++ = (BYTE)(matchCode % 255);
  325. } else
  326. *token += (BYTE)(matchCode);
  327. }
  328. anchor = ip;
  329. /* Test end of chunk */
  330. if (ip > mflimit)
  331. break;
  332. /* Fill table */
  333. LZ4_putPosition(ip - 2, dictPtr->hashTable, tableType, base);
  334. /* Test next position */
  335. match = LZ4_getPosition(ip, dictPtr->hashTable,
  336. tableType, base);
  337. if (dict == usingExtDict) {
  338. if (match < (const BYTE *)source) {
  339. refDelta = dictDelta;
  340. lowLimit = dictionary;
  341. } else {
  342. refDelta = 0;
  343. lowLimit = (const BYTE *)source;
  344. }
  345. }
  346. LZ4_putPosition(ip, dictPtr->hashTable, tableType, base);
  347. if (((dictIssue == dictSmall) ? (match >= lowRefLimit) : 1)
  348. && (match + MAX_DISTANCE >= ip)
  349. && (LZ4_read32(match + refDelta) == LZ4_read32(ip))) {
  350. token = op++;
  351. *token = 0;
  352. goto _next_match;
  353. }
  354. /* Prepare next loop */
  355. forwardH = LZ4_hashPosition(++ip, tableType);
  356. }
  357. _last_literals:
  358. /* Encode Last Literals */
  359. {
  360. size_t const lastRun = (size_t)(iend - anchor);
  361. if ((outputLimited) &&
  362. /* Check output buffer overflow */
  363. ((op - (BYTE *)dest) + lastRun + 1 +
  364. ((lastRun + 255 - RUN_MASK) / 255) > (U32)maxOutputSize))
  365. return 0;
  366. if (lastRun >= RUN_MASK) {
  367. size_t accumulator = lastRun - RUN_MASK;
  368. *op++ = RUN_MASK << ML_BITS;
  369. for (; accumulator >= 255; accumulator -= 255)
  370. *op++ = 255;
  371. *op++ = (BYTE) accumulator;
  372. } else {
  373. *op++ = (BYTE)(lastRun << ML_BITS);
  374. }
  375. memcpy(op, anchor, lastRun);
  376. op += lastRun;
  377. }
  378. /* End */
  379. return (int) (((char *)op) - dest);
  380. }
  381. static int LZ4_compress_fast_extState(
  382. void *state,
  383. const char *source,
  384. char *dest,
  385. int inputSize,
  386. int maxOutputSize,
  387. int acceleration)
  388. {
  389. LZ4_stream_t_internal *ctx = &((LZ4_stream_t *)state)->internal_donotuse;
  390. #if LZ4_ARCH64
  391. const tableType_t tableType = byU32;
  392. #else
  393. const tableType_t tableType = byPtr;
  394. #endif
  395. LZ4_resetStream((LZ4_stream_t *)state);
  396. if (acceleration < 1)
  397. acceleration = LZ4_ACCELERATION_DEFAULT;
  398. if (maxOutputSize >= LZ4_COMPRESSBOUND(inputSize)) {
  399. if (inputSize < LZ4_64Klimit)
  400. return LZ4_compress_generic(ctx, source,
  401. dest, inputSize, 0,
  402. noLimit, byU16, noDict,
  403. noDictIssue, acceleration);
  404. else
  405. return LZ4_compress_generic(ctx, source,
  406. dest, inputSize, 0,
  407. noLimit, tableType, noDict,
  408. noDictIssue, acceleration);
  409. } else {
  410. if (inputSize < LZ4_64Klimit)
  411. return LZ4_compress_generic(ctx, source,
  412. dest, inputSize,
  413. maxOutputSize, limitedOutput, byU16, noDict,
  414. noDictIssue, acceleration);
  415. else
  416. return LZ4_compress_generic(ctx, source,
  417. dest, inputSize,
  418. maxOutputSize, limitedOutput, tableType, noDict,
  419. noDictIssue, acceleration);
  420. }
  421. }
  422. int LZ4_compress_fast(const char *source, char *dest, int inputSize,
  423. int maxOutputSize, int acceleration, void *wrkmem)
  424. {
  425. return LZ4_compress_fast_extState(wrkmem, source, dest, inputSize,
  426. maxOutputSize, acceleration);
  427. }
  428. EXPORT_SYMBOL(LZ4_compress_fast);
  429. int LZ4_compress_default(const char *source, char *dest, int inputSize,
  430. int maxOutputSize, void *wrkmem)
  431. {
  432. return LZ4_compress_fast(source, dest, inputSize,
  433. maxOutputSize, LZ4_ACCELERATION_DEFAULT, wrkmem);
  434. }
  435. EXPORT_SYMBOL(LZ4_compress_default);
  436. /*-******************************
  437. * *_destSize() variant
  438. ********************************/
  439. static int LZ4_compress_destSize_generic(
  440. LZ4_stream_t_internal * const ctx,
  441. const char * const src,
  442. char * const dst,
  443. int * const srcSizePtr,
  444. const int targetDstSize,
  445. const tableType_t tableType)
  446. {
  447. const BYTE *ip = (const BYTE *) src;
  448. const BYTE *base = (const BYTE *) src;
  449. const BYTE *lowLimit = (const BYTE *) src;
  450. const BYTE *anchor = ip;
  451. const BYTE * const iend = ip + *srcSizePtr;
  452. const BYTE * const mflimit = iend - MFLIMIT;
  453. const BYTE * const matchlimit = iend - LASTLITERALS;
  454. BYTE *op = (BYTE *) dst;
  455. BYTE * const oend = op + targetDstSize;
  456. BYTE * const oMaxLit = op + targetDstSize - 2 /* offset */
  457. - 8 /* because 8 + MINMATCH == MFLIMIT */ - 1 /* token */;
  458. BYTE * const oMaxMatch = op + targetDstSize
  459. - (LASTLITERALS + 1 /* token */);
  460. BYTE * const oMaxSeq = oMaxLit - 1 /* token */;
  461. U32 forwardH;
  462. /* Init conditions */
  463. /* Impossible to store anything */
  464. if (targetDstSize < 1)
  465. return 0;
  466. /* Unsupported input size, too large (or negative) */
  467. if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE)
  468. return 0;
  469. /* Size too large (not within 64K limit) */
  470. if ((tableType == byU16) && (*srcSizePtr >= LZ4_64Klimit))
  471. return 0;
  472. /* Input too small, no compression (all literals) */
  473. if (*srcSizePtr < LZ4_minLength)
  474. goto _last_literals;
  475. /* First Byte */
  476. *srcSizePtr = 0;
  477. LZ4_putPosition(ip, ctx->hashTable, tableType, base);
  478. ip++; forwardH = LZ4_hashPosition(ip, tableType);
  479. /* Main Loop */
  480. for ( ; ; ) {
  481. const BYTE *match;
  482. BYTE *token;
  483. /* Find a match */
  484. {
  485. const BYTE *forwardIp = ip;
  486. unsigned int step = 1;
  487. unsigned int searchMatchNb = 1 << LZ4_SKIPTRIGGER;
  488. do {
  489. U32 h = forwardH;
  490. ip = forwardIp;
  491. forwardIp += step;
  492. step = (searchMatchNb++ >> LZ4_SKIPTRIGGER);
  493. if (unlikely(forwardIp > mflimit))
  494. goto _last_literals;
  495. match = LZ4_getPositionOnHash(h, ctx->hashTable,
  496. tableType, base);
  497. forwardH = LZ4_hashPosition(forwardIp,
  498. tableType);
  499. LZ4_putPositionOnHash(ip, h,
  500. ctx->hashTable, tableType,
  501. base);
  502. } while (((tableType == byU16)
  503. ? 0
  504. : (match + MAX_DISTANCE < ip))
  505. || (LZ4_read32(match) != LZ4_read32(ip)));
  506. }
  507. /* Catch up */
  508. while ((ip > anchor)
  509. && (match > lowLimit)
  510. && (unlikely(ip[-1] == match[-1]))) {
  511. ip--;
  512. match--;
  513. }
  514. /* Encode Literal length */
  515. {
  516. unsigned int litLength = (unsigned int)(ip - anchor);
  517. token = op++;
  518. if (op + ((litLength + 240) / 255)
  519. + litLength > oMaxLit) {
  520. /* Not enough space for a last match */
  521. op--;
  522. goto _last_literals;
  523. }
  524. if (litLength >= RUN_MASK) {
  525. unsigned int len = litLength - RUN_MASK;
  526. *token = (RUN_MASK<<ML_BITS);
  527. for (; len >= 255; len -= 255)
  528. *op++ = 255;
  529. *op++ = (BYTE)len;
  530. } else
  531. *token = (BYTE)(litLength << ML_BITS);
  532. /* Copy Literals */
  533. LZ4_wildCopy(op, anchor, op + litLength);
  534. op += litLength;
  535. }
  536. _next_match:
  537. /* Encode Offset */
  538. LZ4_writeLE16(op, (U16)(ip - match)); op += 2;
  539. /* Encode MatchLength */
  540. {
  541. size_t matchLength = LZ4_count(ip + MINMATCH,
  542. match + MINMATCH, matchlimit);
  543. if (op + ((matchLength + 240)/255) > oMaxMatch) {
  544. /* Match description too long : reduce it */
  545. matchLength = (15 - 1) + (oMaxMatch - op) * 255;
  546. }
  547. ip += MINMATCH + matchLength;
  548. if (matchLength >= ML_MASK) {
  549. *token += ML_MASK;
  550. matchLength -= ML_MASK;
  551. while (matchLength >= 255) {
  552. matchLength -= 255;
  553. *op++ = 255;
  554. }
  555. *op++ = (BYTE)matchLength;
  556. } else
  557. *token += (BYTE)(matchLength);
  558. }
  559. anchor = ip;
  560. /* Test end of block */
  561. if (ip > mflimit)
  562. break;
  563. if (op > oMaxSeq)
  564. break;
  565. /* Fill table */
  566. LZ4_putPosition(ip - 2, ctx->hashTable, tableType, base);
  567. /* Test next position */
  568. match = LZ4_getPosition(ip, ctx->hashTable, tableType, base);
  569. LZ4_putPosition(ip, ctx->hashTable, tableType, base);
  570. if ((match + MAX_DISTANCE >= ip)
  571. && (LZ4_read32(match) == LZ4_read32(ip))) {
  572. token = op++; *token = 0;
  573. goto _next_match;
  574. }
  575. /* Prepare next loop */
  576. forwardH = LZ4_hashPosition(++ip, tableType);
  577. }
  578. _last_literals:
  579. /* Encode Last Literals */
  580. {
  581. size_t lastRunSize = (size_t)(iend - anchor);
  582. if (op + 1 /* token */
  583. + ((lastRunSize + 240) / 255) /* litLength */
  584. + lastRunSize /* literals */ > oend) {
  585. /* adapt lastRunSize to fill 'dst' */
  586. lastRunSize = (oend - op) - 1;
  587. lastRunSize -= (lastRunSize + 240) / 255;
  588. }
  589. ip = anchor + lastRunSize;
  590. if (lastRunSize >= RUN_MASK) {
  591. size_t accumulator = lastRunSize - RUN_MASK;
  592. *op++ = RUN_MASK << ML_BITS;
  593. for (; accumulator >= 255; accumulator -= 255)
  594. *op++ = 255;
  595. *op++ = (BYTE) accumulator;
  596. } else {
  597. *op++ = (BYTE)(lastRunSize<<ML_BITS);
  598. }
  599. memcpy(op, anchor, lastRunSize);
  600. op += lastRunSize;
  601. }
  602. /* End */
  603. *srcSizePtr = (int) (((const char *)ip) - src);
  604. return (int) (((char *)op) - dst);
  605. }
  606. static int LZ4_compress_destSize_extState(
  607. LZ4_stream_t *state,
  608. const char *src,
  609. char *dst,
  610. int *srcSizePtr,
  611. int targetDstSize)
  612. {
  613. #if LZ4_ARCH64
  614. const tableType_t tableType = byU32;
  615. #else
  616. const tableType_t tableType = byPtr;
  617. #endif
  618. LZ4_resetStream(state);
  619. if (targetDstSize >= LZ4_COMPRESSBOUND(*srcSizePtr)) {
  620. /* compression success is guaranteed */
  621. return LZ4_compress_fast_extState(
  622. state, src, dst, *srcSizePtr,
  623. targetDstSize, 1);
  624. } else {
  625. if (*srcSizePtr < LZ4_64Klimit)
  626. return LZ4_compress_destSize_generic(
  627. &state->internal_donotuse,
  628. src, dst, srcSizePtr,
  629. targetDstSize, byU16);
  630. else
  631. return LZ4_compress_destSize_generic(
  632. &state->internal_donotuse,
  633. src, dst, srcSizePtr,
  634. targetDstSize, tableType);
  635. }
  636. }
  637. int LZ4_compress_destSize(
  638. const char *src,
  639. char *dst,
  640. int *srcSizePtr,
  641. int targetDstSize,
  642. void *wrkmem)
  643. {
  644. return LZ4_compress_destSize_extState(wrkmem, src, dst, srcSizePtr,
  645. targetDstSize);
  646. }
  647. EXPORT_SYMBOL(LZ4_compress_destSize);
  648. /*-******************************
  649. * Streaming functions
  650. ********************************/
  651. void LZ4_resetStream(LZ4_stream_t *LZ4_stream)
  652. {
  653. memset(LZ4_stream, 0, sizeof(LZ4_stream_t));
  654. }
  655. int LZ4_loadDict(LZ4_stream_t *LZ4_dict,
  656. const char *dictionary, int dictSize)
  657. {
  658. LZ4_stream_t_internal *dict = &LZ4_dict->internal_donotuse;
  659. const BYTE *p = (const BYTE *)dictionary;
  660. const BYTE * const dictEnd = p + dictSize;
  661. const BYTE *base;
  662. if ((dict->initCheck)
  663. || (dict->currentOffset > 1 * GB)) {
  664. /* Uninitialized structure, or reuse overflow */
  665. LZ4_resetStream(LZ4_dict);
  666. }
  667. if (dictSize < (int)HASH_UNIT) {
  668. dict->dictionary = NULL;
  669. dict->dictSize = 0;
  670. return 0;
  671. }
  672. if ((dictEnd - p) > 64 * KB)
  673. p = dictEnd - 64 * KB;
  674. dict->currentOffset += 64 * KB;
  675. base = p - dict->currentOffset;
  676. dict->dictionary = p;
  677. dict->dictSize = (U32)(dictEnd - p);
  678. dict->currentOffset += dict->dictSize;
  679. while (p <= dictEnd - HASH_UNIT) {
  680. LZ4_putPosition(p, dict->hashTable, byU32, base);
  681. p += 3;
  682. }
  683. return dict->dictSize;
  684. }
  685. EXPORT_SYMBOL(LZ4_loadDict);
  686. static void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict,
  687. const BYTE *src)
  688. {
  689. if ((LZ4_dict->currentOffset > 0x80000000) ||
  690. ((uptrval)LZ4_dict->currentOffset > (uptrval)src)) {
  691. /* address space overflow */
  692. /* rescale hash table */
  693. U32 const delta = LZ4_dict->currentOffset - 64 * KB;
  694. const BYTE *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize;
  695. int i;
  696. for (i = 0; i < LZ4_HASH_SIZE_U32; i++) {
  697. if (LZ4_dict->hashTable[i] < delta)
  698. LZ4_dict->hashTable[i] = 0;
  699. else
  700. LZ4_dict->hashTable[i] -= delta;
  701. }
  702. LZ4_dict->currentOffset = 64 * KB;
  703. if (LZ4_dict->dictSize > 64 * KB)
  704. LZ4_dict->dictSize = 64 * KB;
  705. LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize;
  706. }
  707. }
  708. int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize)
  709. {
  710. LZ4_stream_t_internal * const dict = &LZ4_dict->internal_donotuse;
  711. const BYTE * const previousDictEnd = dict->dictionary + dict->dictSize;
  712. if ((U32)dictSize > 64 * KB) {
  713. /* useless to define a dictionary > 64 * KB */
  714. dictSize = 64 * KB;
  715. }
  716. if ((U32)dictSize > dict->dictSize)
  717. dictSize = dict->dictSize;
  718. memmove(safeBuffer, previousDictEnd - dictSize, dictSize);
  719. dict->dictionary = (const BYTE *)safeBuffer;
  720. dict->dictSize = (U32)dictSize;
  721. return dictSize;
  722. }
  723. EXPORT_SYMBOL(LZ4_saveDict);
  724. int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source,
  725. char *dest, int inputSize, int maxOutputSize, int acceleration)
  726. {
  727. LZ4_stream_t_internal *streamPtr = &LZ4_stream->internal_donotuse;
  728. const BYTE * const dictEnd = streamPtr->dictionary
  729. + streamPtr->dictSize;
  730. const BYTE *smallest = (const BYTE *) source;
  731. if (streamPtr->initCheck) {
  732. /* Uninitialized structure detected */
  733. return 0;
  734. }
  735. if ((streamPtr->dictSize > 0) && (smallest > dictEnd))
  736. smallest = dictEnd;
  737. LZ4_renormDictT(streamPtr, smallest);
  738. if (acceleration < 1)
  739. acceleration = LZ4_ACCELERATION_DEFAULT;
  740. /* Check overlapping input/dictionary space */
  741. {
  742. const BYTE *sourceEnd = (const BYTE *) source + inputSize;
  743. if ((sourceEnd > streamPtr->dictionary)
  744. && (sourceEnd < dictEnd)) {
  745. streamPtr->dictSize = (U32)(dictEnd - sourceEnd);
  746. if (streamPtr->dictSize > 64 * KB)
  747. streamPtr->dictSize = 64 * KB;
  748. if (streamPtr->dictSize < 4)
  749. streamPtr->dictSize = 0;
  750. streamPtr->dictionary = dictEnd - streamPtr->dictSize;
  751. }
  752. }
  753. /* prefix mode : source data follows dictionary */
  754. if (dictEnd == (const BYTE *)source) {
  755. int result;
  756. if ((streamPtr->dictSize < 64 * KB) &&
  757. (streamPtr->dictSize < streamPtr->currentOffset)) {
  758. result = LZ4_compress_generic(
  759. streamPtr, source, dest, inputSize,
  760. maxOutputSize, limitedOutput, byU32,
  761. withPrefix64k, dictSmall, acceleration);
  762. } else {
  763. result = LZ4_compress_generic(
  764. streamPtr, source, dest, inputSize,
  765. maxOutputSize, limitedOutput, byU32,
  766. withPrefix64k, noDictIssue, acceleration);
  767. }
  768. streamPtr->dictSize += (U32)inputSize;
  769. streamPtr->currentOffset += (U32)inputSize;
  770. return result;
  771. }
  772. /* external dictionary mode */
  773. {
  774. int result;
  775. if ((streamPtr->dictSize < 64 * KB) &&
  776. (streamPtr->dictSize < streamPtr->currentOffset)) {
  777. result = LZ4_compress_generic(
  778. streamPtr, source, dest, inputSize,
  779. maxOutputSize, limitedOutput, byU32,
  780. usingExtDict, dictSmall, acceleration);
  781. } else {
  782. result = LZ4_compress_generic(
  783. streamPtr, source, dest, inputSize,
  784. maxOutputSize, limitedOutput, byU32,
  785. usingExtDict, noDictIssue, acceleration);
  786. }
  787. streamPtr->dictionary = (const BYTE *)source;
  788. streamPtr->dictSize = (U32)inputSize;
  789. streamPtr->currentOffset += (U32)inputSize;
  790. return result;
  791. }
  792. }
  793. EXPORT_SYMBOL(LZ4_compress_fast_continue);
  794. MODULE_LICENSE("Dual BSD/GPL");
  795. MODULE_DESCRIPTION("LZ4 compressor");