libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
zstd.c
Go to the documentation of this file.
1/*
2 * This file is part of the Aaru Data Preservation Suite.
3 * Copyright (c) 2019-2026 Natalia Portillo.
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stddef.h>
20#include <stdint.h>
21
22#include <aaruformat.h>
23#include <zstd.h>
24
34AARU_EXPORT size_t AARU_CALL aaruf_zstd_decode_buffer(uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer,
35 size_t src_size)
36{
37 size_t result = ZSTD_decompress(dst_buffer, dst_size, src_buffer, src_size);
38
39 if(ZSTD_isError(result)) return 0;
40
41 return result;
42}
43
59AARU_EXPORT size_t AARU_CALL aaruf_zstd_encode_buffer(uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer,
60 size_t src_size, int level, int num_threads)
61{
62 ZSTD_CCtx *cctx = ZSTD_createCCtx();
63 if(cctx == NULL) return 0;
64
65 ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, level);
66
67 if(num_threads > 1) ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, num_threads);
68
69 size_t result = ZSTD_compress2(cctx, dst_buffer, dst_size, src_buffer, src_size);
70
71 ZSTD_freeCCtx(cctx);
72
73 if(ZSTD_isError(result)) return 0;
74
75 return result;
76}
#define AARU_CALL
Definition decls.h:46
#define AARU_EXPORT
Definition decls.h:55
size_t aaruf_zstd_encode_buffer(uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer, size_t src_size, int level, int num_threads)
Encodes a buffer using Zstandard compression.
Definition zstd.c:59
size_t aaruf_zstd_decode_buffer(uint8_t *dst_buffer, size_t dst_size, const uint8_t *src_buffer, size_t src_size)
Decodes a Zstandard-compressed buffer.
Definition zstd.c:34