/* CFS zlib-wrapped DEFLATE binding with explicit stream preparation. */ #include "postgres.h" #include #ifdef HAVE_LIBZ #include #endif #include "storage/cfs/cfs_codec.h" #ifdef HAVE_LIBZ /* Backend-local, non-reentrant streams. No allocator activity during I/O. */ static z_stream zlib_encoder; static z_stream zlib_decoder; static bool zlib_prepared; static bool zlib_may_allocate; static voidpf cfs_zlib_alloc(voidpf opaque, uInt count, uInt size) { (void) opaque; if (!zlib_may_allocate || size == 0 || count > SIZE_MAX / size) return Z_NULL; return malloc((size_t) count * size); } static void cfs_zlib_free(voidpf opaque, voidpf ptr) { (void) opaque; free(ptr); } bool cfs_codec_zlib_prepare(void) { Bytef *raw = NULL; Bytef *restored = NULL; Bytef *compressed = NULL; Size capacity; const uInt warm_len = 32768; int rc; if (zlib_prepared) return true; memset(&zlib_encoder, 0, sizeof(zlib_encoder)); memset(&zlib_decoder, 0, sizeof(zlib_decoder)); zlib_encoder.zalloc = cfs_zlib_alloc; zlib_encoder.zfree = cfs_zlib_free; zlib_decoder.zalloc = cfs_zlib_alloc; zlib_decoder.zfree = cfs_zlib_free; zlib_may_allocate = true; if (deflateInit(&zlib_encoder, Z_DEFAULT_COMPRESSION) != Z_OK) goto failed; if (inflateInit(&zlib_decoder) != Z_OK) goto failed; /* Exercise full-window inflate while allocations are still permitted. */ capacity = (Size) compressBound(warm_len); raw = calloc(warm_len, 1); restored = malloc(warm_len); compressed = malloc(capacity); if (raw == NULL || restored == NULL || compressed == NULL) goto failed; zlib_encoder.next_in = raw; zlib_encoder.avail_in = warm_len; zlib_encoder.next_out = compressed; zlib_encoder.avail_out = (uInt) capacity; rc = deflate(&zlib_encoder, Z_FINISH); if (rc != Z_STREAM_END) goto failed; zlib_decoder.next_in = compressed; zlib_decoder.avail_in = (uInt) zlib_encoder.total_out; zlib_decoder.next_out = restored; zlib_decoder.avail_out = warm_len; rc = inflate(&zlib_decoder, Z_FINISH); if (rc != Z_STREAM_END || zlib_decoder.total_out != warm_len) goto failed; if (deflateReset(&zlib_encoder) != Z_OK || inflateReset(&zlib_decoder) != Z_OK) goto failed; zlib_may_allocate = false; zlib_prepared = true; free(raw); free(restored); free(compressed); return true; failed: if (zlib_encoder.state != NULL) deflateEnd(&zlib_encoder); if (zlib_decoder.state != NULL) inflateEnd(&zlib_decoder); zlib_may_allocate = false; free(raw); free(restored); free(compressed); memset(&zlib_encoder, 0, sizeof(zlib_encoder)); memset(&zlib_decoder, 0, sizeof(zlib_decoder)); return false; } #endif bool cfs_codec_zlib_available(void) { #ifdef HAVE_LIBZ return true; #else return false; #endif } Size cfs_codec_zlib_bound(Size srclen) { #ifdef HAVE_LIBZ if (srclen > (Size) BLCKSZ) return 0; return (Size) compressBound((uLong) srclen); #else (void) srclen; return 0; #endif } CfsCodecResult cfs_codec_zlib_compress(const char *src, Size srclen, char *dst, Size dstlen, int level, Size *result_len) { #ifdef HAVE_LIBZ int rc; if (src == NULL || dst == NULL || result_len == NULL || srclen > (Size) BLCKSZ || dstlen > (Size) UINT_MAX || level > 9) return CFS_CODEC_ERROR; if (!zlib_prepared) return CFS_CODEC_ERROR; /* cfs_codec_prepare() required */ if (srclen == 0 || dstlen == 0) return CFS_CODEC_OVERFLOW; if (level <= 0) level = Z_DEFAULT_COMPRESSION; rc = deflateReset(&zlib_encoder); if (rc != Z_OK) return CFS_CODEC_ERROR; zlib_encoder.next_in = Z_NULL; zlib_encoder.avail_in = 0; zlib_encoder.next_out = (Bytef *) dst; zlib_encoder.avail_out = (uInt) dstlen; rc = deflateParams(&zlib_encoder, level, Z_DEFAULT_STRATEGY); if (rc == Z_BUF_ERROR) return CFS_CODEC_OVERFLOW; if (rc != Z_OK) return CFS_CODEC_ERROR; zlib_encoder.next_in = (Bytef *) src; zlib_encoder.avail_in = (uInt) srclen; rc = deflate(&zlib_encoder, Z_FINISH); if (rc != Z_STREAM_END) return rc == Z_OK || rc == Z_BUF_ERROR ? CFS_CODEC_OVERFLOW : CFS_CODEC_ERROR; if (zlib_encoder.total_in != srclen || zlib_encoder.total_out >= srclen) return CFS_CODEC_OVERFLOW; *result_len = (Size) zlib_encoder.total_out; return CFS_CODEC_OK; #else (void) src; (void) srclen; (void) dst; (void) dstlen; (void) level; (void) result_len; return CFS_CODEC_UNAVAIL; #endif } CfsCodecResult cfs_codec_zlib_decompress(const char *src, Size srclen, char *dst, Size dstlen) { #ifdef HAVE_LIBZ int rc; if (src == NULL || dst == NULL || srclen == 0 || srclen > (Size) BLCKSZ || dstlen > (Size) BLCKSZ || !zlib_prepared) return CFS_CODEC_ERROR; if (inflateReset(&zlib_decoder) != Z_OK) return CFS_CODEC_ERROR; zlib_decoder.next_in = (Bytef *) src; zlib_decoder.avail_in = (uInt) srclen; zlib_decoder.next_out = (Bytef *) dst; zlib_decoder.avail_out = (uInt) dstlen; rc = inflate(&zlib_decoder, Z_FINISH); return rc == Z_STREAM_END && zlib_decoder.total_in == srclen && zlib_decoder.total_out == dstlen ? CFS_CODEC_OK : CFS_CODEC_ERROR; #else (void) src; (void) srclen; (void) dst; (void) dstlen; return CFS_CODEC_UNAVAIL; #endif } /* Release the backend-local prepared streams at worker/backend exit. */ void cfs_codec_zlib_release(void) { #ifdef HAVE_LIBZ if (zlib_encoder.state != NULL) deflateEnd(&zlib_encoder); if (zlib_decoder.state != NULL) inflateEnd(&zlib_decoder); memset(&zlib_encoder, 0, sizeof(zlib_encoder)); memset(&zlib_decoder, 0, sizeof(zlib_decoder)); zlib_prepared = false; zlib_may_allocate = false; #endif }