jflz.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. /*
  14. FastLZ - lightning-fast lossless compression library
  15. Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
  16. Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
  17. Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
  18. Permission is hereby granted, free of charge, to any person obtaining a copy
  19. of this software and associated documentation files (the "Software"), to deal
  20. in the Software without restriction, including without limitation the rights
  21. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. copies of the Software, and to permit persons to whom the Software is
  23. furnished to do so, subject to the following conditions:
  24. The above copyright notice and this permission notice shall be included in
  25. all copies or substantial portions of the Software.
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. THE SOFTWARE.
  33. */
  34. #if !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR)
  35. // adapted for jlib
  36. #include "platform.h"
  37. #include "jfcmp.hpp"
  38. #include "jflz.hpp"
  39. #include "jcrc.hpp"
  40. /*
  41. * Always check for bound when decompressing.
  42. * Generally it is best to leave it defined.
  43. */
  44. #define FASTLZ_SAFE
  45. /*
  46. * Give hints to the compiler for branch prediction optimization.
  47. */
  48. #if defined(__GNUC__) && (__GNUC__ > 2)
  49. #define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
  50. #define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
  51. #else
  52. #define FASTLZ_EXPECT_CONDITIONAL(c) (c)
  53. #define FASTLZ_UNEXPECT_CONDITIONAL(c) (c)
  54. #endif
  55. /*
  56. * Use inlined functions for supported systems.
  57. */
  58. #if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C)
  59. #define FASTLZ_INLINE inline
  60. #elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__)
  61. #define FASTLZ_INLINE __inline
  62. #else
  63. #define FASTLZ_INLINE
  64. #endif
  65. /*
  66. * Prevent accessing more than 8-bit at once, except on x86 architectures.
  67. */
  68. #if !defined(FASTLZ_STRICT_ALIGN)
  69. #define FASTLZ_STRICT_ALIGN
  70. #if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */
  71. #undef FASTLZ_STRICT_ALIGN
  72. #elif defined(__i486__) || defined(__i586__) || defined(__i686__) /* GNU C */
  73. #undef FASTLZ_STRICT_ALIGN
  74. #elif defined(_M_IX86) /* Intel, MSVC */
  75. #undef FASTLZ_STRICT_ALIGN
  76. #elif defined(__386)
  77. #undef FASTLZ_STRICT_ALIGN
  78. #elif defined(_X86_) /* MinGW */
  79. #undef FASTLZ_STRICT_ALIGN
  80. #elif defined(__I86__) /* Digital Mars */
  81. #undef FASTLZ_STRICT_ALIGN
  82. #endif
  83. #endif
  84. /*
  85. * FIXME: use preprocessor magic to set this on different platforms!
  86. */
  87. typedef byte flzuint8;
  88. typedef unsigned short flzuint16;
  89. typedef unsigned int flzuint32;
  90. /* prototypes */
  91. //int fastlz_compress(const void* input, int length, void* output);
  92. //int fastlz_compress_level(int level, const void* input, int length, void* output);
  93. //int fastlz_decompress(const void* input, int length, void* output, int maxout);
  94. #define MAX_COPY 32
  95. #define MAX_LEN 264 /* 256 + 8 */
  96. #define MAX_DISTANCE 8192
  97. #if !defined(FASTLZ_STRICT_ALIGN)
  98. #define FASTLZ_READU16(p) *((const flzuint16*)(p))
  99. #else
  100. #define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8)
  101. #endif
  102. #define HASH_LOG 13
  103. #define HASH_SIZE (1<< HASH_LOG)
  104. #define HASH_MASK (HASH_SIZE-1)
  105. #define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; }
  106. typedef const flzuint8* HTAB_T[HASH_SIZE];
  107. #undef FASTLZ_LEVEL
  108. #define FASTLZ_LEVEL 1
  109. #undef FASTLZ_COMPRESSOR
  110. #undef FASTLZ_DECOMPRESSOR
  111. #define FASTLZ_COMPRESSOR fastlz1_compress
  112. #define FASTLZ_DECOMPRESSOR fastlz1_decompress
  113. static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output, HTAB_T &htab);
  114. static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
  115. #include "jflz.cpp"
  116. #undef FASTLZ_LEVEL
  117. #define FASTLZ_LEVEL 2
  118. #undef MAX_DISTANCE
  119. #define MAX_DISTANCE 8191
  120. #define MAX_FARDISTANCE (65535+MAX_DISTANCE-1)
  121. #undef FASTLZ_COMPRESSOR
  122. #undef FASTLZ_DECOMPRESSOR
  123. #define FASTLZ_COMPRESSOR fastlz2_compress
  124. #define FASTLZ_DECOMPRESSOR fastlz2_decompress
  125. static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output, HTAB_T &htab);
  126. static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
  127. #include "jflz.cpp"
  128. #define FASTLZ__JLIBCOMPRESSOR 1
  129. inline size32_t fastlz_compress(const void* input, size32_t length, void* output, HTAB_T &htab)
  130. {
  131. /* for short block, choose fastlz1 */
  132. if(length < 65536)
  133. return fastlz1_compress(input, length, output, htab);
  134. /* else... */
  135. return fastlz2_compress(input, length, output, htab);
  136. }
  137. size32_t fastlz_compress(const void* input, size32_t length, void* output)
  138. {
  139. MemoryAttr ma;
  140. HTAB_T *ht = (HTAB_T *)ma.allocate(sizeof(HTAB_T)); // HTAB_T too big for stack really
  141. return fastlz_compress(input,length,output,*ht);
  142. }
  143. size32_t fastlz_decompress(const void* input, size32_t length, void* output, size32_t maxout)
  144. {
  145. /* magic identifier for compression level */
  146. int level = ((*(const flzuint8*)input) >> 5) + 1;
  147. if(level == 1)
  148. return fastlz1_decompress(input, length, output, maxout);
  149. if(level == 2)
  150. return fastlz2_decompress(input, length, output, maxout);
  151. /* unknown level, trigger error */
  152. return 0;
  153. }
  154. int fastlz_compress_level(int level, const void* input, size32_t length, void* output)
  155. {
  156. MemoryAttr ma;
  157. HTAB_T *ht = (HTAB_T *)ma.allocate(sizeof(HTAB_T)); // HTAB_T too big for stack really
  158. if(level == 1)
  159. return fastlz1_compress(input, length, output, *ht);
  160. if(level == 2)
  161. return fastlz2_compress(input, length, output, *ht);
  162. return 0;
  163. }
  164. #else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
  165. static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output, HTAB_T &htab)
  166. {
  167. const flzuint8* ip = (const flzuint8*) input;
  168. const flzuint8* ip_bound = ip + length - 2;
  169. const flzuint8* ip_limit = ip + length - 12;
  170. flzuint8* op = (flzuint8*) output;
  171. const flzuint8** hslot;
  172. flzuint32 hval;
  173. flzuint32 copy;
  174. /* sanity check */
  175. if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4))
  176. {
  177. if(length)
  178. {
  179. /* create literal copy only */
  180. *op++ = length-1;
  181. ip_bound++;
  182. while(ip <= ip_bound)
  183. *op++ = *ip++;
  184. return length+1;
  185. }
  186. else
  187. return 0;
  188. }
  189. /* initializes hash table */
  190. for (hslot = htab; hslot < htab + HASH_SIZE; hslot++)
  191. *hslot = ip;
  192. /* we start with literal copy */
  193. copy = 2;
  194. *op++ = MAX_COPY-1;
  195. *op++ = *ip++;
  196. *op++ = *ip++;
  197. /* main loop */
  198. while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
  199. {
  200. const flzuint8* ref;
  201. flzuint32 distance;
  202. /* minimum match length */
  203. flzuint32 len = 3;
  204. /* comparison starting-point */
  205. const flzuint8* anchor = ip;
  206. /* check for a run */
  207. #if FASTLZ_LEVEL==2
  208. if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1))
  209. {
  210. distance = 1;
  211. ip += 3;
  212. ref = anchor - 1 + 3;
  213. goto match;
  214. }
  215. #endif
  216. /* find potential match */
  217. HASH_FUNCTION(hval,ip);
  218. hslot = htab + hval;
  219. ref = htab[hval];
  220. /* calculate distance to the match */
  221. distance = anchor - ref;
  222. /* update hash table */
  223. *hslot = anchor;
  224. /* is this a match? check the first 3 bytes */
  225. if(distance==0 ||
  226. #if FASTLZ_LEVEL==1
  227. (distance >= MAX_DISTANCE) ||
  228. #else
  229. (distance >= MAX_FARDISTANCE) ||
  230. #endif
  231. *ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++)
  232. goto literal;
  233. #if FASTLZ_LEVEL==2
  234. /* far, needs at least 5-byte match */
  235. if(distance >= MAX_DISTANCE)
  236. {
  237. if(*ip++ != *ref++ || *ip++!= *ref++)
  238. goto literal;
  239. len += 2;
  240. }
  241. match:
  242. #endif
  243. /* last matched byte */
  244. ip = anchor + len;
  245. /* distance is biased */
  246. distance--;
  247. if(!distance)
  248. {
  249. /* zero distance means a run */
  250. flzuint8 x = ip[-1];
  251. while(ip < ip_bound)
  252. if(*ref++ != x) break; else ip++;
  253. }
  254. else
  255. for(;;)
  256. {
  257. /* safe because the outer check against ip limit */
  258. if(*ref++ != *ip++) break;
  259. if(*ref++ != *ip++) break;
  260. if(*ref++ != *ip++) break;
  261. if(*ref++ != *ip++) break;
  262. if(*ref++ != *ip++) break;
  263. if(*ref++ != *ip++) break;
  264. if(*ref++ != *ip++) break;
  265. if(*ref++ != *ip++) break;
  266. while(ip < ip_bound)
  267. if(*ref++ != *ip++) break;
  268. break;
  269. }
  270. /* if we have copied something, adjust the copy count */
  271. if(copy)
  272. /* copy is biased, '0' means 1 byte copy */
  273. *(op-copy-1) = copy-1;
  274. else
  275. /* back, to overwrite the copy count */
  276. op--;
  277. /* reset literal counter */
  278. copy = 0;
  279. /* length is biased, '1' means a match of 3 bytes */
  280. ip -= 3;
  281. len = ip - anchor;
  282. /* encode the match */
  283. #if FASTLZ_LEVEL==2
  284. if(distance < MAX_DISTANCE)
  285. {
  286. if(len < 7)
  287. {
  288. *op++ = (len << 5) + (distance >> 8);
  289. *op++ = (distance & 255);
  290. }
  291. else
  292. {
  293. *op++ = (7 << 5) + (distance >> 8);
  294. for(len-=7; len >= 255; len-= 255)
  295. *op++ = 255;
  296. *op++ = len;
  297. *op++ = (distance & 255);
  298. }
  299. }
  300. else
  301. {
  302. /* far away, but not yet in the another galaxy... */
  303. if(len < 7)
  304. {
  305. distance -= MAX_DISTANCE;
  306. *op++ = (len << 5) + 31;
  307. *op++ = 255;
  308. *op++ = distance >> 8;
  309. *op++ = distance & 255;
  310. }
  311. else
  312. {
  313. distance -= MAX_DISTANCE;
  314. *op++ = (7 << 5) + 31;
  315. for(len-=7; len >= 255; len-= 255)
  316. *op++ = 255;
  317. *op++ = len;
  318. *op++ = 255;
  319. *op++ = distance >> 8;
  320. *op++ = distance & 255;
  321. }
  322. }
  323. #else
  324. if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2))
  325. while(len > MAX_LEN-2)
  326. {
  327. *op++ = (7 << 5) + (distance >> 8);
  328. *op++ = MAX_LEN - 2 - 7 -2;
  329. *op++ = (distance & 255);
  330. len -= MAX_LEN-2;
  331. }
  332. if(len < 7)
  333. {
  334. *op++ = (len << 5) + (distance >> 8);
  335. *op++ = (distance & 255);
  336. }
  337. else
  338. {
  339. *op++ = (7 << 5) + (distance >> 8);
  340. *op++ = len - 7;
  341. *op++ = (distance & 255);
  342. }
  343. #endif
  344. /* update the hash at match boundary */
  345. HASH_FUNCTION(hval,ip);
  346. htab[hval] = ip++;
  347. HASH_FUNCTION(hval,ip);
  348. htab[hval] = ip++;
  349. /* assuming literal copy */
  350. *op++ = MAX_COPY-1;
  351. continue;
  352. literal:
  353. *op++ = *anchor++;
  354. ip = anchor;
  355. copy++;
  356. if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY))
  357. {
  358. copy = 0;
  359. *op++ = MAX_COPY-1;
  360. }
  361. }
  362. /* left-over as literal copy */
  363. ip_bound++;
  364. while(ip <= ip_bound)
  365. {
  366. *op++ = *ip++;
  367. copy++;
  368. if(copy == MAX_COPY)
  369. {
  370. copy = 0;
  371. *op++ = MAX_COPY-1;
  372. }
  373. }
  374. /* if we have copied something, adjust the copy length */
  375. if(copy)
  376. *(op-copy-1) = copy-1;
  377. else
  378. op--;
  379. #if FASTLZ_LEVEL==2
  380. /* marker for fastlz2 */
  381. *(flzuint8*)output |= (1 << 5);
  382. #endif
  383. return op - (flzuint8*)output;
  384. }
  385. static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout)
  386. {
  387. const flzuint8* ip = (const flzuint8*) input;
  388. const flzuint8* ip_limit = ip + length;
  389. flzuint8* op = (flzuint8*) output;
  390. flzuint8* op_limit = op + maxout;
  391. flzuint32 ctrl = (*ip++) & 31;
  392. int loopidx = 1;
  393. do
  394. {
  395. const flzuint8* ref = op;
  396. flzuint32 len = ctrl >> 5;
  397. flzuint32 ofs = (ctrl & 31) << 8;
  398. if(ctrl >= 32)
  399. {
  400. #if FASTLZ_LEVEL==2
  401. flzuint8 code;
  402. #endif
  403. len--;
  404. ref -= ofs;
  405. if (len == 7-1)
  406. #if FASTLZ_LEVEL==1
  407. len += *ip++;
  408. ref -= *ip++;
  409. #else
  410. do
  411. {
  412. code = *ip++;
  413. len += code;
  414. } while (code==255);
  415. code = *ip++;
  416. ref -= code;
  417. /* match from 16-bit distance */
  418. if(FASTLZ_UNEXPECT_CONDITIONAL(code==255))
  419. if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8)))
  420. {
  421. ofs = (*ip++) << 8;
  422. ofs += *ip++;
  423. ref = op - ofs - MAX_DISTANCE;
  424. }
  425. #endif
  426. #ifdef FASTLZ_SAFE
  427. if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit))
  428. return 0;
  429. if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output))
  430. return 0;
  431. #endif
  432. if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
  433. ctrl = *ip++;
  434. else
  435. loopidx = 0;
  436. if(ref == op)
  437. {
  438. /* optimize copy for a run */
  439. flzuint8 b = ref[-1];
  440. *op++ = b;
  441. *op++ = b;
  442. *op++ = b;
  443. for(; len; --len)
  444. *op++ = b;
  445. }
  446. else
  447. {
  448. #if !defined(FASTLZ_STRICT_ALIGN)
  449. const flzuint16* p;
  450. flzuint16* q;
  451. #endif
  452. /* copy from reference */
  453. ref--;
  454. *op++ = *ref++;
  455. *op++ = *ref++;
  456. *op++ = *ref++;
  457. #if !defined(FASTLZ_STRICT_ALIGN)
  458. /* copy a byte, so that now it's word aligned */
  459. if(len & 1)
  460. {
  461. *op++ = *ref++;
  462. len--;
  463. }
  464. /* copy 16-bit at once */
  465. q = (flzuint16*) op;
  466. op += len;
  467. p = (const flzuint16*) ref;
  468. for(len>>=1; len > 4; len-=4)
  469. {
  470. *q++ = *p++;
  471. *q++ = *p++;
  472. *q++ = *p++;
  473. *q++ = *p++;
  474. }
  475. for(; len; --len)
  476. *q++ = *p++;
  477. #else
  478. for(; len; --len)
  479. *op++ = *ref++;
  480. #endif
  481. }
  482. }
  483. else
  484. {
  485. ctrl++;
  486. #ifdef FASTLZ_SAFE
  487. if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit))
  488. return 0;
  489. if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit))
  490. return 0;
  491. #endif
  492. *op++ = *ip++;
  493. for(--ctrl; ctrl; ctrl--)
  494. *op++ = *ip++;
  495. loopidx = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit);
  496. if(loopidx)
  497. ctrl = *ip++;
  498. }
  499. }
  500. while(FASTLZ_EXPECT_CONDITIONAL(loopidx));
  501. return op - (flzuint8*)output;
  502. }
  503. #undef FASTLZ__JLIBCOMPRESSOR // avoid being compiled twice!!
  504. #endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
  505. #if defined(FASTLZ__JLIBCOMPRESSOR)
  506. /* Format:
  507. size32_t totalexpsize;
  508. { size32_t subcmpsize; bytes subcmpdata; }
  509. size32_t trailsize; bytes traildata; // unexpanded
  510. */
  511. class jlib_decl CFastLZCompressor : public CFcmpCompressor
  512. {
  513. HTAB_T ht;
  514. virtual void setinmax()
  515. {
  516. inmax = blksz-outlen-sizeof(size32_t);
  517. if (inmax<256)
  518. trailing = true; // too small to bother compressing
  519. else
  520. {
  521. trailing = false;
  522. inmax -= (fastlzSlack(inmax) + sizeof(size32_t));
  523. }
  524. }
  525. virtual void flushcommitted()
  526. {
  527. // only does non trailing
  528. if (trailing)
  529. return;
  530. size32_t toflush = (inlenblk==COMMITTED)?inlen:inlenblk;
  531. if (toflush == 0)
  532. return;
  533. size32_t outSzRequired = outlen+sizeof(size32_t)*2+toflush+fastlzSlack(toflush);
  534. if (!dynamicOutSz)
  535. assertex(outSzRequired<=blksz);
  536. else
  537. {
  538. if (outSzRequired>dynamicOutSz)
  539. {
  540. verifyex(outBufMb->ensureCapacity(outBufStart+outSzRequired));
  541. dynamicOutSz = outBufMb->capacity();
  542. outbuf = ((byte *)outBufMb->bufferBase()+outBufStart);
  543. }
  544. }
  545. size32_t *cmpsize = (size32_t *)(outbuf+outlen);
  546. byte *out = (byte *)(cmpsize+1);
  547. *cmpsize = (size32_t)fastlz_compress(inbuf, (int)toflush, out, ht);
  548. if (*cmpsize<toflush)
  549. {
  550. *(size32_t *)outbuf += toflush;
  551. outlen += *cmpsize+sizeof(size32_t);
  552. if (inlenblk==COMMITTED)
  553. inlen = 0;
  554. else
  555. {
  556. inlen -= inlenblk;
  557. memmove(inbuf,inbuf+toflush,inlen);
  558. }
  559. setinmax();
  560. return;
  561. }
  562. trailing = true;
  563. }
  564. };
  565. class jlib_decl CFastLZExpander : public CFcmpExpander
  566. {
  567. public:
  568. virtual void expand(void *buf)
  569. {
  570. if (!outlen)
  571. return;
  572. if (buf) {
  573. if (bufalloc)
  574. free(outbuf);
  575. bufalloc = 0;
  576. outbuf = (unsigned char *)buf;
  577. }
  578. else if (outlen>bufalloc) {
  579. if (bufalloc)
  580. free(outbuf);
  581. bufalloc = outlen;
  582. outbuf = (unsigned char *)malloc(bufalloc);
  583. if (!outbuf)
  584. throw MakeStringException(MSGAUD_operator,0, "Out of memory in FastLZExpander::expand, requesting %d bytes", bufalloc);
  585. }
  586. size32_t done = 0;
  587. for (;;) {
  588. const size32_t szchunk = *in;
  589. in++;
  590. if (szchunk+done<outlen) {
  591. size32_t written = fastlz_decompress(in,szchunk,(byte *)buf+done,outlen-done);
  592. done += written;
  593. if (!written||(done>outlen))
  594. throw MakeStringException(0, "FastLZExpander - corrupt data(1) %d %d",written,szchunk);
  595. }
  596. else {
  597. if (szchunk+done!=outlen)
  598. throw MakeStringException(0, "FastLZExpander - corrupt data(2) %d %d",szchunk,outlen);
  599. memcpy((byte *)buf+done,in,szchunk);
  600. break;
  601. }
  602. in = (const size32_t *)(((const byte *)in)+szchunk);
  603. }
  604. }
  605. };
  606. void fastLZCompressToBuffer(MemoryBuffer & out, size32_t len, const void * src)
  607. {
  608. size32_t outbase = out.length();
  609. out.append(len);
  610. DelayedMarker<size32_t> cmpSzMarker(out);
  611. void *cmpData = out.reserve(len+fastlzSlack(len));
  612. size32_t sz = (len>16)?fastlz_compress(src, (int)len, cmpData):16;
  613. if (sz>=len)
  614. {
  615. sz = len;
  616. memcpy(cmpData, src, len);
  617. }
  618. cmpSzMarker.write(sz);
  619. out.setLength(outbase+sz+sizeof(size32_t)*2);
  620. }
  621. void fastLZDecompressToBuffer(MemoryBuffer & out, const void * src)
  622. {
  623. size32_t *sz = (size32_t *)src;
  624. size32_t expsz = *(sz++);
  625. size32_t cmpsz = *(sz++);
  626. void *o = out.reserve(expsz);
  627. if (cmpsz!=expsz) {
  628. size32_t written = fastlz_decompress(sz,cmpsz,o,expsz);
  629. if (written!=expsz)
  630. throw MakeStringException(0, "fastLZDecompressToBuffer - corrupt data(1) %d %d",written,expsz);
  631. }
  632. else
  633. memcpy(o,sz,expsz);
  634. }
  635. void fastLZDecompressToBuffer(MemoryBuffer & out, MemoryBuffer & in)
  636. {
  637. size32_t expsz;
  638. size32_t cmpsz;
  639. in.read(expsz).read(cmpsz);
  640. void *o = out.reserve(expsz);
  641. if (cmpsz!=expsz) {
  642. size32_t written = fastlz_decompress(in.readDirect(cmpsz),cmpsz,o,expsz);
  643. if (written!=expsz)
  644. throw MakeStringException(0, "fastLZDecompressToBuffer - corrupt data(3) %d %d",written,expsz);
  645. }
  646. else
  647. memcpy(o,in.readDirect(cmpsz),expsz);
  648. }
  649. void fastLZDecompressToAttr(MemoryAttr & out, const void * src)
  650. {
  651. size32_t *sz = (size32_t *)src;
  652. size32_t expsz = *(sz++);
  653. size32_t cmpsz = *(sz++);
  654. void *o = out.allocate(expsz);
  655. if (cmpsz!=expsz) {
  656. size32_t written = fastlz_decompress(sz,cmpsz,o,expsz);
  657. if (written!=expsz)
  658. throw MakeStringException(0, "fastLZDecompressToBuffer - corrupt data(2) %d %d",written,expsz);
  659. }
  660. else
  661. memcpy(o,sz,expsz);
  662. }
  663. void fastLZDecompressToBuffer(MemoryAttr & out, MemoryBuffer & in)
  664. {
  665. size32_t expsz;
  666. size32_t cmpsz;
  667. in.read(expsz).read(cmpsz);
  668. void *o = out.allocate(expsz);
  669. if (cmpsz!=expsz) {
  670. size32_t written = fastlz_decompress(in.readDirect(cmpsz),cmpsz,o,expsz);
  671. if (written!=expsz)
  672. throw MakeStringException(0, "fastLZDecompressToBuffer - corrupt data(4) %d %d",written,expsz);
  673. }
  674. else
  675. memcpy(o,in.readDirect(cmpsz),expsz);
  676. }
  677. ICompressor *createFastLZCompressor()
  678. {
  679. return new CFastLZCompressor;
  680. }
  681. IExpander *createFastLZExpander()
  682. {
  683. return new CFastLZExpander;
  684. }
  685. static const __uint64 FLZSTRMCOMPRESSEDFILEFLAG = I64C(0xc3518de42f15da57);
  686. class CFastLZStream : public CFcmpStream
  687. {
  688. bool load()
  689. {
  690. bufpos = 0;
  691. bufsize = 0;
  692. if (expOffset==expSize)
  693. return false;
  694. size32_t sz[2];
  695. if (baseio->read(cmpOffset,sizeof(size32_t)*2,&sz)!=sizeof(size32_t)*2)
  696. return false;
  697. bufsize = sz[0];
  698. if (!bufsize)
  699. return false;
  700. cmpOffset += sizeof(size32_t)*2;
  701. if (ma.length()<bufsize)
  702. ma.allocate(bufsize);
  703. MemoryAttr cmpma;
  704. byte *cmpbuf = (byte *)cmpma.allocate(sz[1]);
  705. if (baseio->read(cmpOffset,sz[1],cmpbuf)!=sz[1])
  706. throw MakeStringException(-1,"CFastLZStream: file corrupt.1");
  707. if (fastlz_decompress(cmpbuf,sz[1],ma.bufferBase(),bufsize)!=bufsize)
  708. throw MakeStringException(-1,"CFastLZStream: file corrupt.2");
  709. cmpOffset += sz[1];
  710. return true;
  711. }
  712. void save()
  713. {
  714. if (bufsize) {
  715. MemoryAttr dstma;
  716. byte *dst = (byte *)dstma.allocate(sizeof(size32_t)*2+bufsize+fastlzSlack(bufsize));
  717. size32_t sz = fastlz_compress(ma.get(),bufsize,sizeof(size32_t)*2+dst);
  718. memcpy(dst,&bufsize,sizeof(size32_t));
  719. memcpy(dst+sizeof(size32_t),&sz,sizeof(size32_t));
  720. baseio->write(cmpOffset,sz+sizeof(size32_t)*2,dst);
  721. cmpOffset += sz+sizeof(size32_t)*2;
  722. }
  723. bufsize = 0;
  724. }
  725. public:
  726. CFastLZStream() { compType = FLZSTRMCOMPRESSEDFILEFLAG; }
  727. virtual ~CFastLZStream() { flush(); }
  728. };
  729. IFileIOStream *createFastLZStreamRead(IFileIO *base)
  730. {
  731. Owned<CFastLZStream> strm = new CFastLZStream();
  732. if (strm->attach(base))
  733. return strm.getClear();
  734. return NULL;
  735. }
  736. IFileIOStream *createFastLZStreamWrite(IFileIO *base)
  737. {
  738. Owned<CFastLZStream> strm = new CFastLZStream();
  739. strm->create(base);
  740. return strm.getClear();
  741. }
  742. #endif