aboutsummaryrefslogtreecommitdiffstats
path: root/src/zlib.c
blob: 76f049e8c26e18d4262e69a3e070cdf27c28a914 (plain) (blame)
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
/*
 * Copyright (c) 2020-2021, yzrh <yzrh@noema.org>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdlib.h>
#include <string.h>

#include <zlib.h>

int
strinflate(char **dst, int dst_size,
	const char * restrict src, int src_size)
{
	*dst = malloc(dst_size);

	if (*dst == NULL)
		return 1;

	unsigned long size = dst_size;

	if (uncompress((Bytef *) *dst,
		&size, (const Bytef *) src, src_size) != Z_OK) {
		free(*dst);
		return 1;
	}

	return 0;
}

int
strdeflate(char **dst, int *dst_size,
	const char * restrict src, int src_size)
{
	*dst_size = compressBound(src_size);
	*dst = malloc(*dst_size);

	if (*dst == NULL)
		return 1;

	unsigned long size = *dst_size;

	if (compress((Bytef *) *dst, &size,
		(const Bytef *) src, src_size) != Z_OK) {
		free(*dst);
		return 1;
	}

	*dst_size = size;

	return 0;
}