LCOV - code coverage report
Current view: top level - contrib/pgcrypto - pgp-compress.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 106 122 86.9 %
Date: 2020-06-01 09:07:10 Functions: 11 11 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * pgp-compress.c
       3             :  *    ZIP and ZLIB compression via zlib.
       4             :  *
       5             :  * Copyright (c) 2005 Marko Kreen
       6             :  * All rights reserved.
       7             :  *
       8             :  * Redistribution and use in source and binary forms, with or without
       9             :  * modification, are permitted provided that the following conditions
      10             :  * are met:
      11             :  * 1. Redistributions of source code must retain the above copyright
      12             :  *    notice, this list of conditions and the following disclaimer.
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
      18             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      19             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      20             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
      21             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      22             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      23             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      24             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      25             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      26             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      27             :  * SUCH DAMAGE.
      28             :  *
      29             :  * contrib/pgcrypto/pgp-compress.c
      30             :  */
      31             : 
      32             : #include "postgres.h"
      33             : 
      34             : #include "pgp.h"
      35             : #include "px.h"
      36             : 
      37             : /*
      38             :  * Compressed pkt writer
      39             :  */
      40             : 
      41             : #ifdef HAVE_LIBZ
      42             : 
      43             : #include <zlib.h>
      44             : 
      45             : #define ZIP_OUT_BUF 8192
      46             : #define ZIP_IN_BLOCK 8192
      47             : 
      48             : struct ZipStat
      49             : {
      50             :     uint8       type;
      51             :     int         buf_len;
      52             :     int         hdr_done;
      53             :     z_stream    stream;
      54             :     uint8       buf[ZIP_OUT_BUF];
      55             : };
      56             : 
      57             : static void *
      58          30 : z_alloc(void *priv, unsigned n_items, unsigned item_len)
      59             : {
      60          30 :     return px_alloc(n_items * item_len);
      61             : }
      62             : 
      63             : static void
      64          30 : z_free(void *priv, void *addr)
      65             : {
      66          30 :     px_free(addr);
      67          30 : }
      68             : 
      69             : static int
      70           4 : compress_init(PushFilter *next, void *init_arg, void **priv_p)
      71             : {
      72             :     int         res;
      73             :     struct ZipStat *st;
      74           4 :     PGP_Context *ctx = init_arg;
      75           4 :     uint8       type = ctx->compress_algo;
      76             : 
      77           4 :     if (type != PGP_COMPR_ZLIB && type != PGP_COMPR_ZIP)
      78           0 :         return PXE_PGP_UNSUPPORTED_COMPR;
      79             : 
      80             :     /*
      81             :      * init
      82             :      */
      83           4 :     st = px_alloc(sizeof(*st));
      84           4 :     memset(st, 0, sizeof(*st));
      85           4 :     st->buf_len = ZIP_OUT_BUF;
      86           4 :     st->stream.zalloc = z_alloc;
      87           4 :     st->stream.zfree = z_free;
      88             : 
      89           4 :     if (type == PGP_COMPR_ZIP)
      90           2 :         res = deflateInit2(&st->stream, ctx->compress_level,
      91             :                            Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
      92             :     else
      93           2 :         res = deflateInit(&st->stream, ctx->compress_level);
      94           4 :     if (res != Z_OK)
      95             :     {
      96           0 :         px_free(st);
      97           0 :         return PXE_PGP_COMPRESSION_ERROR;
      98             :     }
      99           4 :     *priv_p = st;
     100             : 
     101           4 :     return ZIP_IN_BLOCK;
     102             : }
     103             : 
     104             : /* writes compressed data packet */
     105             : 
     106             : /* can handle zero-len incoming data, but shouldn't */
     107             : static int
     108           4 : compress_process(PushFilter *next, void *priv, const uint8 *data, int len)
     109             : {
     110             :     int         res,
     111             :                 n_out;
     112           4 :     struct ZipStat *st = priv;
     113             : 
     114             :     /*
     115             :      * process data
     116             :      */
     117           8 :     while (len > 0)
     118             :     {
     119           4 :         st->stream.next_in = unconstify(uint8 *, data);
     120           4 :         st->stream.avail_in = len;
     121           4 :         st->stream.next_out = st->buf;
     122           4 :         st->stream.avail_out = st->buf_len;
     123           4 :         res = deflate(&st->stream, 0);
     124           4 :         if (res != Z_OK)
     125           0 :             return PXE_PGP_COMPRESSION_ERROR;
     126             : 
     127           4 :         n_out = st->buf_len - st->stream.avail_out;
     128           4 :         if (n_out > 0)
     129             :         {
     130           2 :             res = pushf_write(next, st->buf, n_out);
     131           2 :             if (res < 0)
     132           0 :                 return res;
     133             :         }
     134           4 :         len = st->stream.avail_in;
     135             :     }
     136             : 
     137           4 :     return 0;
     138             : }
     139             : 
     140             : static int
     141           4 : compress_flush(PushFilter *next, void *priv)
     142             : {
     143             :     int         res,
     144             :                 zres,
     145             :                 n_out;
     146           4 :     struct ZipStat *st = priv;
     147             : 
     148           4 :     st->stream.next_in = NULL;
     149           4 :     st->stream.avail_in = 0;
     150             :     while (1)
     151             :     {
     152           0 :         st->stream.next_out = st->buf;
     153           4 :         st->stream.avail_out = st->buf_len;
     154           4 :         zres = deflate(&st->stream, Z_FINISH);
     155           4 :         if (zres != Z_STREAM_END && zres != Z_OK)
     156           0 :             return PXE_PGP_COMPRESSION_ERROR;
     157           4 :         n_out = st->buf_len - st->stream.avail_out;
     158           4 :         if (n_out > 0)
     159             :         {
     160           4 :             res = pushf_write(next, st->buf, n_out);
     161           4 :             if (res < 0)
     162           0 :                 return res;
     163             :         }
     164           4 :         if (zres == Z_STREAM_END)
     165           4 :             break;
     166             :     }
     167           4 :     return 0;
     168             : }
     169             : 
     170             : static void
     171           4 : compress_free(void *priv)
     172             : {
     173           4 :     struct ZipStat *st = priv;
     174             : 
     175           4 :     deflateEnd(&st->stream);
     176           4 :     px_memset(st, 0, sizeof(*st));
     177           4 :     px_free(st);
     178           4 : }
     179             : 
     180             : static const PushFilterOps
     181             :             compress_filter = {
     182             :     compress_init, compress_process, compress_flush, compress_free
     183             : };
     184             : 
     185             : int
     186           4 : pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
     187             : {
     188           4 :     return pushf_create(res, &compress_filter, ctx, dst);
     189             : }
     190             : 
     191             : /*
     192             :  * Decompress
     193             :  */
     194             : struct DecomprData
     195             : {
     196             :     int         buf_len;        /* = ZIP_OUT_BUF */
     197             :     int         buf_data;       /* available data */
     198             :     uint8      *pos;
     199             :     z_stream    stream;
     200             :     int         eof;
     201             :     uint8       buf[ZIP_OUT_BUF];
     202             : };
     203             : 
     204             : static int
     205           6 : decompress_init(void **priv_p, void *arg, PullFilter *src)
     206             : {
     207           6 :     PGP_Context *ctx = arg;
     208             :     struct DecomprData *dec;
     209             :     int         res;
     210             : 
     211           6 :     if (ctx->compress_algo != PGP_COMPR_ZLIB
     212           4 :         && ctx->compress_algo != PGP_COMPR_ZIP)
     213           0 :         return PXE_PGP_UNSUPPORTED_COMPR;
     214             : 
     215           6 :     dec = px_alloc(sizeof(*dec));
     216           6 :     memset(dec, 0, sizeof(*dec));
     217           6 :     dec->buf_len = ZIP_OUT_BUF;
     218           6 :     *priv_p = dec;
     219             : 
     220           6 :     dec->stream.zalloc = z_alloc;
     221           6 :     dec->stream.zfree = z_free;
     222             : 
     223           6 :     if (ctx->compress_algo == PGP_COMPR_ZIP)
     224           4 :         res = inflateInit2(&dec->stream, -15);
     225             :     else
     226           2 :         res = inflateInit(&dec->stream);
     227           6 :     if (res != Z_OK)
     228             :     {
     229           0 :         px_free(dec);
     230           0 :         px_debug("decompress_init: inflateInit error");
     231           0 :         return PXE_PGP_COMPRESSION_ERROR;
     232             :     }
     233             : 
     234           6 :     return 0;
     235             : }
     236             : 
     237             : static int
     238          42 : decompress_read(void *priv, PullFilter *src, int len,
     239             :                 uint8 **data_p, uint8 *buf, int buflen)
     240             : {
     241             :     int         res;
     242             :     int         flush;
     243          42 :     struct DecomprData *dec = priv;
     244             : 
     245          48 : restart:
     246          48 :     if (dec->buf_data > 0)
     247             :     {
     248          36 :         if (len > dec->buf_data)
     249           0 :             len = dec->buf_data;
     250          36 :         *data_p = dec->pos;
     251          36 :         dec->pos += len;
     252          36 :         dec->buf_data -= len;
     253          36 :         return len;
     254             :     }
     255             : 
     256          12 :     if (dec->eof)
     257           6 :         return 0;
     258             : 
     259           6 :     if (dec->stream.avail_in == 0)
     260             :     {
     261             :         uint8      *tmp;
     262             : 
     263           6 :         res = pullf_read(src, 8192, &tmp);
     264           6 :         if (res < 0)
     265           0 :             return res;
     266           6 :         dec->stream.next_in = tmp;
     267           6 :         dec->stream.avail_in = res;
     268             :     }
     269             : 
     270           6 :     dec->stream.next_out = dec->buf;
     271           6 :     dec->stream.avail_out = dec->buf_len;
     272           6 :     dec->pos = dec->buf;
     273             : 
     274             :     /*
     275             :      * Z_SYNC_FLUSH is tell zlib to output as much as possible. It should do
     276             :      * it anyway (Z_NO_FLUSH), but seems to reserve the right not to.  So lets
     277             :      * follow the API.
     278             :      */
     279           6 :     flush = dec->stream.avail_in ? Z_SYNC_FLUSH : Z_FINISH;
     280           6 :     res = inflate(&dec->stream, flush);
     281           6 :     if (res != Z_OK && res != Z_STREAM_END)
     282             :     {
     283           0 :         px_debug("decompress_read: inflate error: %d", res);
     284           0 :         return PXE_PGP_CORRUPT_DATA;
     285             :     }
     286             : 
     287           6 :     dec->buf_data = dec->buf_len - dec->stream.avail_out;
     288           6 :     if (res == Z_STREAM_END)
     289           6 :         dec->eof = 1;
     290           6 :     goto restart;
     291             : }
     292             : 
     293             : static void
     294           6 : decompress_free(void *priv)
     295             : {
     296           6 :     struct DecomprData *dec = priv;
     297             : 
     298           6 :     inflateEnd(&dec->stream);
     299           6 :     px_memset(dec, 0, sizeof(*dec));
     300           6 :     px_free(dec);
     301           6 : }
     302             : 
     303             : static const PullFilterOps
     304             :             decompress_filter = {
     305             :     decompress_init, decompress_read, decompress_free
     306             : };
     307             : 
     308             : int
     309           6 : pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src)
     310             : {
     311           6 :     return pullf_create(res, &decompress_filter, ctx, src);
     312             : }
     313             : #else                           /* !HAVE_LIBZ */
     314             : 
     315             : int
     316             : pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
     317             : {
     318             :     return PXE_PGP_UNSUPPORTED_COMPR;
     319             : }
     320             : 
     321             : int
     322             : pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src)
     323             : {
     324             :     return PXE_PGP_UNSUPPORTED_COMPR;
     325             : }
     326             : 
     327             : #endif

Generated by: LCOV version 1.13