~ubuntu-branches/ubuntu/trusty/postgresql-8.4/trusty

« back to all changes in this revision

Viewing changes to src/backend/utils/adt/pg_lzcompress.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
166
166
 *
167
167
 * Copyright (c) 1999-2009, PostgreSQL Global Development Group
168
168
 *
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 $
170
170
 * ----------
171
171
 */
172
172
#include "postgres.h"
210
210
 * ----------
211
211
 */
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
 
214
                                                                 * compressed */
 
215
        INT_MAX,                                        /* No upper limit on what we'll try to
 
216
                                                                 * compress */
 
217
        25,                                                     /* Require 25% compression rate, or not worth
 
218
                                                                 * it */
 
219
        1024,                                           /* Give up if no compression in the first 1KB */
 
220
        128,                                            /* Stop history lookup if a match of 128 bytes
 
221
                                                                 * is found */
 
222
        10                                                      /* Lower good match size by 10% at every loop
 
223
                                                                 * iteration */
219
224
};
220
225
const PGLZ_Strategy *const PGLZ_strategy_default = &strategy_default_data;
221
226
 
222
227
 
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 */
225
230
        INT_MAX,
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
 
234
                                                                 * is found */
 
235
        6                                                       /* Look harder for a good match */
230
236
};
231
237
const PGLZ_Strategy *const PGLZ_strategy_always = &strategy_always_data;
232
238
 
502
508
                strategy = PGLZ_strategy_default;
503
509
 
504
510
        /*
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
 
512
         * of range), fail.
507
513
         */
508
514
        if (strategy->match_size_good <= 0 ||
509
515
                slen < strategy->min_input_size ||
537
543
                need_rate = 99;
538
544
 
539
545
        /*
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.
543
549
         */
544
 
        if (slen > (INT_MAX/100))
 
550
        if (slen > (INT_MAX / 100))
545
551
        {
546
552
                /* Approximate to avoid overflow */
547
553
                result_max = (slen / 100) * (100 - need_rate);
572
578
 
573
579
                /*
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).
578
584
                 */
654
660
        while (sp < srcend && dp < destend)
655
661
        {
656
662
                /*
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).
659
665
                 */
660
666
                unsigned char ctrl = *sp++;
661
 
                int             ctrlc;
 
667
                int                     ctrlc;
662
668
 
663
669
                for (ctrlc = 0; ctrlc < 8 && sp < srcend; ctrlc++)
664
670
                {
681
687
                                        len += *sp++;
682
688
 
683
689
                                /*
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.
689
695
                                 */
690
696
                                if (dp + len > destend)
691
697
                                {
711
717
                                 * An unset control bit means LITERAL BYTE. So we just copy
712
718
                                 * one from INPUT to OUTPUT.
713
719
                                 */
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 */
716
722
 
717
723
                                *dp++ = *sp++;
718
724
                        }