Go to the source code of this file.
Data Structures | |
struct | PGLZ_Header |
struct | PGLZ_Strategy |
Defines | |
#define | PGLZ_MAX_OUTPUT(_dlen) ((_dlen) + 4 + sizeof(PGLZ_Header)) |
#define | PGLZ_RAW_SIZE(_lzdata) ((_lzdata)->rawsize) |
Typedefs | |
typedef struct PGLZ_Header | PGLZ_Header |
typedef struct PGLZ_Strategy | PGLZ_Strategy |
Functions | |
bool | pglz_compress (const char *source, int32 slen, PGLZ_Header *dest, const PGLZ_Strategy *strategy) |
void | pglz_decompress (const PGLZ_Header *source, char *dest) |
Variables | |
const PGLZ_Strategy *const | PGLZ_strategy_default |
const PGLZ_Strategy *const | PGLZ_strategy_always |
#define PGLZ_MAX_OUTPUT | ( | _dlen | ) | ((_dlen) + 4 + sizeof(PGLZ_Header)) |
Definition at line 34 of file pg_lzcompress.h.
Referenced by toast_compress_datum().
#define PGLZ_RAW_SIZE | ( | _lzdata | ) | ((_lzdata)->rawsize) |
Definition at line 43 of file pg_lzcompress.h.
Referenced by heap_tuple_untoast_attr(), and heap_tuple_untoast_attr_slice().
typedef struct PGLZ_Header PGLZ_Header |
typedef struct PGLZ_Strategy PGLZ_Strategy |
bool pglz_compress | ( | const char * | source, | |
int32 | slen, | |||
PGLZ_Header * | dest, | |||
const PGLZ_Strategy * | strategy | |||
) |
Definition at line 482 of file pg_lzcompress.c.
References PGLZ_Strategy::first_success_by, PGLZ_Strategy::match_size_drop, PGLZ_Strategy::match_size_good, PGLZ_Strategy::max_input_size, PGLZ_Strategy::min_comp_rate, NULL, pglz_find_match(), pglz_hist_add, PGLZ_MAX_MATCH, pglz_out_literal, pglz_out_tag, PGLZ_Header::rawsize, and SET_VARSIZE_COMPRESSED.
Referenced by toast_compress_datum().
{ unsigned char *bp = ((unsigned char *) dest) + sizeof(PGLZ_Header); unsigned char *bstart = bp; int hist_next = 0; bool hist_recycle = false; const char *dp = source; const char *dend = source + slen; unsigned char ctrl_dummy = 0; unsigned char *ctrlp = &ctrl_dummy; unsigned char ctrlb = 0; unsigned char ctrl = 0; bool found_match = false; int32 match_len; int32 match_off; int32 good_match; int32 good_drop; int32 result_size; int32 result_max; int32 need_rate; /* * Our fallback strategy is the default. */ if (strategy == NULL) strategy = PGLZ_strategy_default; /* * If the strategy forbids compression (at all or if source chunk size out * of range), fail. */ if (strategy->match_size_good <= 0 || slen < strategy->min_input_size || slen > strategy->max_input_size) return false; /* * Save the original source size in the header. */ dest->rawsize = slen; /* * Limit the match parameters to the supported range. */ good_match = strategy->match_size_good; if (good_match > PGLZ_MAX_MATCH) good_match = PGLZ_MAX_MATCH; else if (good_match < 17) good_match = 17; good_drop = strategy->match_size_drop; if (good_drop < 0) good_drop = 0; else if (good_drop > 100) good_drop = 100; need_rate = strategy->min_comp_rate; if (need_rate < 0) need_rate = 0; else if (need_rate > 99) need_rate = 99; /* * Compute the maximum result size allowed by the strategy, namely the * input size minus the minimum wanted compression rate. This had better * be <= slen, else we might overrun the provided output buffer. */ if (slen > (INT_MAX / 100)) { /* Approximate to avoid overflow */ result_max = (slen / 100) * (100 - need_rate); } else result_max = (slen * (100 - need_rate)) / 100; /* * Initialize the history lists to empty. We do not need to zero the * hist_entries[] array; its entries are initialized as they are used. */ memset(hist_start, 0, sizeof(hist_start)); /* * Compress the source directly into the output buffer. */ while (dp < dend) { /* * If we already exceeded the maximum result size, fail. * * We check once per loop; since the loop body could emit as many as 4 * bytes (a control byte and 3-byte tag), PGLZ_MAX_OUTPUT() had better * allow 4 slop bytes. */ if (bp - bstart >= result_max) return false; /* * If we've emitted more than first_success_by bytes without finding * anything compressible at all, fail. This lets us fall out * reasonably quickly when looking at incompressible input (such as * pre-compressed data). */ if (!found_match && bp - bstart >= strategy->first_success_by) return false; /* * Try to find a match in the history */ if (pglz_find_match(hist_start, dp, dend, &match_len, &match_off, good_match, good_drop)) { /* * Create the tag and add history entries for all matched * characters. */ pglz_out_tag(ctrlp, ctrlb, ctrl, bp, match_len, match_off); while (match_len--) { pglz_hist_add(hist_start, hist_entries, hist_next, hist_recycle, dp, dend); dp++; /* Do not do this ++ in the line above! */ /* The macro would do it four times - Jan. */ } found_match = true; } else { /* * No match found. Copy one literal byte. */ pglz_out_literal(ctrlp, ctrlb, ctrl, bp, *dp); pglz_hist_add(hist_start, hist_entries, hist_next, hist_recycle, dp, dend); dp++; /* Do not do this ++ in the line above! */ /* The macro would do it four times - Jan. */ } } /* * Write out the last control byte and check that we haven't overrun the * output size allowed by the strategy. */ *ctrlp = ctrlb; result_size = bp - bstart; if (result_size >= result_max) return false; /* * Success - need only fill in the actual length of the compressed datum. */ SET_VARSIZE_COMPRESSED(dest, result_size + sizeof(PGLZ_Header)); return true; }
void pglz_decompress | ( | const PGLZ_Header * | source, | |
char * | dest | |||
) |
Definition at line 648 of file pg_lzcompress.c.
References elog, ERROR, PGLZ_Header::rawsize, and VARSIZE.
Referenced by heap_tuple_untoast_attr(), and heap_tuple_untoast_attr_slice().
{ const unsigned char *sp; const unsigned char *srcend; unsigned char *dp; unsigned char *destend; sp = ((const unsigned char *) source) + sizeof(PGLZ_Header); srcend = ((const unsigned char *) source) + VARSIZE(source); dp = (unsigned char *) dest; destend = dp + source->rawsize; while (sp < srcend && dp < destend) { /* * Read one control byte and process the next 8 items (or as many as * remain in the compressed input). */ unsigned char ctrl = *sp++; int ctrlc; for (ctrlc = 0; ctrlc < 8 && sp < srcend; ctrlc++) { if (ctrl & 1) { /* * Otherwise it contains the match length minus 3 and the * upper 4 bits of the offset. The next following byte * contains the lower 8 bits of the offset. If the length is * coded as 18, another extension tag byte tells how much * longer the match really was (0-255). */ int32 len; int32 off; len = (sp[0] & 0x0f) + 3; off = ((sp[0] & 0xf0) << 4) | sp[1]; sp += 2; if (len == 18) len += *sp++; /* * Check for output buffer overrun, to ensure we don't clobber * memory in case of corrupt input. Note: we must advance dp * here to ensure the error is detected below the loop. We * don't simply put the elog inside the loop since that will * probably interfere with optimization. */ if (dp + len > destend) { dp += len; break; } /* * Now we copy the bytes specified by the tag from OUTPUT to * OUTPUT. It is dangerous and platform dependent to use * memcpy() here, because the copied areas could overlap * extremely! */ while (len--) { *dp = dp[-off]; dp++; } } else { /* * An unset control bit means LITERAL BYTE. So we just copy * one from INPUT to OUTPUT. */ if (dp >= destend) /* check for buffer overrun */ break; /* do not clobber memory */ *dp++ = *sp++; } /* * Advance the control bit */ ctrl >>= 1; } } /* * Check we decompressed the right amount. */ if (dp != destend || sp != srcend) elog(ERROR, "compressed data is corrupt"); /* * That's it. */ }
const PGLZ_Strategy* const PGLZ_strategy_always |
Definition at line 237 of file pg_lzcompress.c.
const PGLZ_Strategy* const PGLZ_strategy_default |
Definition at line 225 of file pg_lzcompress.c.
Referenced by toast_compress_datum().