167
167
* Copyright (c) 1999-2009, PostgreSQL Global Development Group
169
* $PostgreSQL: pgsql/src/backend/utils/adt/pg_lzcompress.c,v 1.33 2009/01/06 15:51:38 tgl Exp $
169
* $PostgreSQL: pgsql/src/backend/utils/adt/pg_lzcompress.c,v 1.34 2009/06/11 14:49:03 momjian Exp $
172
172
#include "postgres.h"
212
212
static const PGLZ_Strategy strategy_default_data = {
213
32, /* Data chunks less than 32 bytes are not compressed */
214
INT_MAX, /* No upper limit on what we'll try to compress */
215
25, /* Require 25% compression rate, or not worth it */
216
1024, /* Give up if no compression in the first 1KB */
217
128, /* Stop history lookup if a match of 128 bytes is found */
218
10 /* Lower good match size by 10% at every loop iteration */
213
32, /* Data chunks less than 32 bytes are not
215
INT_MAX, /* No upper limit on what we'll try to
217
25, /* Require 25% compression rate, or not worth
219
1024, /* Give up if no compression in the first 1KB */
220
128, /* Stop history lookup if a match of 128 bytes
222
10 /* Lower good match size by 10% at every loop
220
225
const PGLZ_Strategy *const PGLZ_strategy_default = &strategy_default_data;
223
228
static const PGLZ_Strategy strategy_always_data = {
224
0, /* Chunks of any size are compressed */
229
0, /* Chunks of any size are compressed */
226
0, /* It's enough to save one single byte */
227
INT_MAX, /* Never give up early */
228
128, /* Stop history lookup if a match of 128 bytes is found */
229
6 /* Look harder for a good match */
231
0, /* It's enough to save one single byte */
232
INT_MAX, /* Never give up early */
233
128, /* Stop history lookup if a match of 128 bytes
235
6 /* Look harder for a good match */
231
237
const PGLZ_Strategy *const PGLZ_strategy_always = &strategy_always_data;
502
508
strategy = PGLZ_strategy_default;
505
* If the strategy forbids compression (at all or if source chunk size
506
* out of range), fail.
511
* If the strategy forbids compression (at all or if source chunk size out
508
514
if (strategy->match_size_good <= 0 ||
509
515
slen < strategy->min_input_size ||
540
* Compute the maximum result size allowed by the strategy, namely
541
* the input size minus the minimum wanted compression rate. This had
542
* better be <= slen, else we might overrun the provided output buffer.
546
* Compute the maximum result size allowed by the strategy, namely the
547
* input size minus the minimum wanted compression rate. This had better
548
* be <= slen, else we might overrun the provided output buffer.
544
if (slen > (INT_MAX/100))
550
if (slen > (INT_MAX / 100))
546
552
/* Approximate to avoid overflow */
547
553
result_max = (slen / 100) * (100 - need_rate);
574
580
* If we've emitted more than first_success_by bytes without finding
575
* anything compressible at all, fail. This lets us fall out
581
* anything compressible at all, fail. This lets us fall out
576
582
* reasonably quickly when looking at incompressible input (such as
577
583
* pre-compressed data).
654
660
while (sp < srcend && dp < destend)
657
* Read one control byte and process the next 8 items (or as many
658
* as remain in the compressed input).
663
* Read one control byte and process the next 8 items (or as many as
664
* remain in the compressed input).
660
666
unsigned char ctrl = *sp++;
663
669
for (ctrlc = 0; ctrlc < 8 && sp < srcend; ctrlc++)
684
* Check for output buffer overrun, to ensure we don't
685
* clobber memory in case of corrupt input. Note: we must
686
* advance dp here to ensure the error is detected below
687
* the loop. We don't simply put the elog inside the loop
688
* since that will probably interfere with optimization.
690
* Check for output buffer overrun, to ensure we don't clobber
691
* memory in case of corrupt input. Note: we must advance dp
692
* here to ensure the error is detected below the loop. We
693
* don't simply put the elog inside the loop since that will
694
* probably interfere with optimization.
690
696
if (dp + len > destend)
711
717
* An unset control bit means LITERAL BYTE. So we just copy
712
718
* one from INPUT to OUTPUT.
714
if (dp >= destend) /* check for buffer overrun */
715
break; /* do not clobber memory */
720
if (dp >= destend) /* check for buffer overrun */
721
break; /* do not clobber memory */