8
8
*
9
9
* Entry routines:
10
10
*
11
- * bool
12
- * pglz_compress(const char *source, int32 slen, PGLZ_Header *dest,
11
+ * int32
12
+ * pglz_compress(const char *source, int32 slen, char *dest,
13
13
* const PGLZ_Strategy *strategy);
14
14
*
15
15
* source is the input data to be compressed.
23
23
* the compression algorithm. If NULL, the compiled
24
24
* in default strategy is used.
25
25
*
26
- * The return value is TRUE if compression succeeded,
27
- * FALSE if not ; in the latter case the contents of dest
28
- * are undefined.
26
+ * The return value is the number of bytes written in the
27
+ * buffer dest, or -1 if compression fails ; in the latter
28
+ * case the contents of dest are undefined.
29
29
*
30
- * void
31
- * pglz_decompress(const PGLZ_Header *source, char *dest)
30
+ * int32
31
+ * pglz_decompress(const char *source, int32 slen, char *dest,
32
+ * int32 rawsize)
32
33
*
33
34
* source is the compressed input.
34
35
*
36
+ * slen is the length of the compressed input.
37
+ *
35
38
* dest is the area where the uncompressed data will be
36
39
* written to. It is the callers responsibility to
37
- * provide enough space. The required amount can be
38
- * obtained with the macro PGLZ_RAW_SIZE(source).
40
+ * provide enough space.
39
41
*
40
42
* The data is written to buff exactly as it was handed
41
43
* to pglz_compress(). No terminating zero byte is added.
42
44
*
43
- * The decompression algorithm and internal data format:
45
+ * rawsize is the length of the uncompressed data.
44
46
*
45
- * PGLZ_Header is defined as
47
+ * The return value is the number of bytes written in the
48
+ * buffer dest, or -1 if decompression fails.
46
49
*
47
- * typedef struct PGLZ_Header {
48
- * int32 vl_len_;
49
- * int32 rawsize;
50
- * }
50
+ * The decompression algorithm and internal data format:
51
51
*
52
- * The header is followed by the compressed data itself.
52
+ * It is made with the compressed data itself.
53
53
*
54
54
* The data representation is easiest explained by describing
55
55
* the process of decompression.
56
56
*
57
- * If VARSIZE(x) == rawsize + sizeof(PGLZ_Header) , then the data
57
+ * If compressed_size == rawsize, then the data
58
58
* is stored uncompressed as plain bytes. Thus, the decompressor
59
- * simply copies rawsize bytes from the location after the
60
- * header to the destination.
59
+ * simply copies rawsize bytes to the destination.
61
60
*
62
- * Otherwise the first byte after the header tells what to do
63
- * the next 8 times. We call this the control byte.
61
+ * Otherwise the first byte tells what to do the next 8 times.
62
+ * We call this the control byte.
64
63
*
65
64
* An unset bit in the control byte means, that one uncompressed
66
65
* byte follows, which is copied from input to output.
169
168
*
170
169
* Copyright (c) 1999-2015, PostgreSQL Global Development Group
171
170
*
172
- * src/backend/utils/adt /pg_lzcompress.c
171
+ * src/common /pg_lzcompress.c
173
172
* ----------
174
173
*/
174
+ #ifndef FRONTEND
175
175
#include "postgres.h"
176
+ #else
177
+ #include "postgres_fe.h"
178
+ #endif
176
179
177
180
#include <limits.h>
178
181
179
- #include "utils /pg_lzcompress.h"
182
+ #include "common /pg_lzcompress.h"
180
183
181
184
182
185
/* ----------
@@ -492,14 +495,15 @@ pglz_find_match(int16 *hstart, const char *input, const char *end,
492
495
/* ----------
493
496
* pglz_compress -
494
497
*
495
- * Compresses source into dest using strategy.
498
+ * Compresses source into dest using strategy. Returns the number of
499
+ * bytes written in buffer dest, or -1 if compression fails.
496
500
* ----------
497
501
*/
498
- bool
499
- pglz_compress (const char * source , int32 slen , PGLZ_Header * dest ,
502
+ int32
503
+ pglz_compress (const char * source , int32 slen , char * dest ,
500
504
const PGLZ_Strategy * strategy )
501
505
{
502
- unsigned char * bp = (( unsigned char * ) dest ) + sizeof ( PGLZ_Header ) ;
506
+ unsigned char * bp = (unsigned char * ) dest ;
503
507
unsigned char * bstart = bp ;
504
508
int hist_next = 1 ;
505
509
bool hist_recycle = false;
@@ -533,12 +537,7 @@ pglz_compress(const char *source, int32 slen, PGLZ_Header *dest,
533
537
if (strategy -> match_size_good <= 0 ||
534
538
slen < strategy -> min_input_size ||
535
539
slen > strategy -> max_input_size )
536
- return false;
537
-
538
- /*
539
- * Save the original source size in the header.
540
- */
541
- dest -> rawsize = slen ;
540
+ return -1 ;
542
541
543
542
/*
544
543
* Limit the match parameters to the supported range.
@@ -611,7 +610,7 @@ pglz_compress(const char *source, int32 slen, PGLZ_Header *dest,
611
610
* allow 4 slop bytes.
612
611
*/
613
612
if (bp - bstart >= result_max )
614
- return false ;
613
+ return -1 ;
615
614
616
615
/*
617
616
* If we've emitted more than first_success_by bytes without finding
@@ -620,7 +619,7 @@ pglz_compress(const char *source, int32 slen, PGLZ_Header *dest,
620
619
* pre-compressed data).
621
620
*/
622
621
if (!found_match && bp - bstart >= strategy -> first_success_by )
623
- return false ;
622
+ return -1 ;
624
623
625
624
/*
626
625
* Try to find a match in the history
@@ -664,35 +663,34 @@ pglz_compress(const char *source, int32 slen, PGLZ_Header *dest,
664
663
* ctrlp = ctrlb ;
665
664
result_size = bp - bstart ;
666
665
if (result_size >= result_max )
667
- return false;
668
-
669
- /*
670
- * Success - need only fill in the actual length of the compressed datum.
671
- */
672
- SET_VARSIZE_COMPRESSED (dest , result_size + sizeof (PGLZ_Header ));
666
+ return -1 ;
673
667
674
- return true;
668
+ /* success */
669
+ return result_size ;
675
670
}
676
671
677
672
678
673
/* ----------
679
674
* pglz_decompress -
680
675
*
681
- * Decompresses source into dest.
676
+ * Decompresses source into dest. Returns the number of bytes
677
+ * decompressed in the destination buffer, or -1 if decompression
678
+ * fails.
682
679
* ----------
683
680
*/
684
- void
685
- pglz_decompress (const PGLZ_Header * source , char * dest )
681
+ int32
682
+ pglz_decompress (const char * source , int32 slen , char * dest ,
683
+ int32 rawsize )
686
684
{
687
685
const unsigned char * sp ;
688
686
const unsigned char * srcend ;
689
687
unsigned char * dp ;
690
688
unsigned char * destend ;
691
689
692
- sp = (( const unsigned char * ) source ) + sizeof ( PGLZ_Header ) ;
693
- srcend = ((const unsigned char * ) source ) + VARSIZE ( source ) ;
690
+ sp = (const unsigned char * ) source ;
691
+ srcend = ((const unsigned char * ) source ) + slen ;
694
692
dp = (unsigned char * ) dest ;
695
- destend = dp + source -> rawsize ;
693
+ destend = dp + rawsize ;
696
694
697
695
while (sp < srcend && dp < destend )
698
696
{
@@ -771,9 +769,10 @@ pglz_decompress(const PGLZ_Header *source, char *dest)
771
769
* Check we decompressed the right amount.
772
770
*/
773
771
if (dp != destend || sp != srcend )
774
- elog ( ERROR , "compressed data is corrupt" ) ;
772
+ return -1 ;
775
773
776
774
/*
777
775
* That's it.
778
776
*/
777
+ return rawsize ;
779
778
}
0 commit comments