1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
// SPDX-License-Identifier: GPL-2.0-or-later
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/zlib.h>
#include "backend_deflate.h"
/* Use the same value as crypto API */
#define DEFLATE_DEF_WINBITS (-11)
#define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
struct deflate_ctx {
struct z_stream_s cctx;
struct z_stream_s dctx;
};
static void deflate_release_params(struct zcomp_params *params)
{
}
static int deflate_setup_params(struct zcomp_params *params)
{
if (params->level == ZCOMP_PARAM_NOT_SET)
params->level = Z_DEFAULT_COMPRESSION;
if (params->deflate.winbits == ZCOMP_PARAM_NOT_SET)
params->deflate.winbits = DEFLATE_DEF_WINBITS;
return 0;
}
static void deflate_destroy(struct zcomp_ctx *ctx)
{
struct deflate_ctx *zctx = ctx->context;
if (!zctx)
return;
if (zctx->cctx.workspace) {
zlib_deflateEnd(&zctx->cctx);
vfree(zctx->cctx.workspace);
}
if (zctx->dctx.workspace) {
zlib_inflateEnd(&zctx->dctx);
vfree(zctx->dctx.workspace);
}
kfree(zctx);
}
static int deflate_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
{
struct deflate_ctx *zctx;
size_t sz;
int ret;
zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
if (!zctx)
return -ENOMEM;
ctx->context = zctx;
sz = zlib_deflate_workspacesize(params->deflate.winbits, MAX_MEM_LEVEL);
zctx->cctx.workspace = vzalloc(sz);
if (!zctx->cctx.workspace)
goto error;
ret = zlib_deflateInit2(&zctx->cctx, params->level, Z_DEFLATED,
params->deflate.winbits, DEFLATE_DEF_MEMLEVEL,
Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
goto error;
sz = zlib_inflate_workspacesize();
zctx->dctx.workspace = vzalloc(sz);
if (!zctx->dctx.workspace)
goto error;
ret = zlib_inflateInit2(&zctx->dctx, params->deflate.winbits);
if (ret != Z_OK)
goto error;
return 0;
error:
deflate_destroy(ctx);
return -EINVAL;
}
static int deflate_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
struct zcomp_req *req)
{
struct deflate_ctx *zctx = ctx->context;
struct z_stream_s *deflate;
int ret;
deflate = &zctx->cctx;
ret = zlib_deflateReset(deflate);
if (ret != Z_OK)
return -EINVAL;
deflate->next_in = (u8 *)req->src;
deflate->avail_in = req->src_len;
deflate->next_out = (u8 *)req->dst;
deflate->avail_out = req->dst_len;
ret = zlib_deflate(deflate, Z_FINISH);
if (ret != Z_STREAM_END)
return -EINVAL;
req->dst_len = deflate->total_out;
return 0;
}
static int deflate_decompress(struct zcomp_params *params,
struct zcomp_ctx *ctx,
struct zcomp_req *req)
{
struct deflate_ctx *zctx = ctx->context;
struct z_stream_s *inflate;
int ret;
inflate = &zctx->dctx;
ret = zlib_inflateReset(inflate);
if (ret != Z_OK)
return -EINVAL;
inflate->next_in = (u8 *)req->src;
inflate->avail_in = req->src_len;
inflate->next_out = (u8 *)req->dst;
inflate->avail_out = req->dst_len;
ret = zlib_inflate(inflate, Z_SYNC_FLUSH);
if (ret != Z_STREAM_END)
return -EINVAL;
return 0;
}
const struct zcomp_ops backend_deflate = {
.compress = deflate_compress,
.decompress = deflate_decompress,
.create_ctx = deflate_create,
.destroy_ctx = deflate_destroy,
.setup_params = deflate_setup_params,
.release_params = deflate_release_params,
.name = "deflate",
};
|