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
149
150
151
152
153
154
|
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "log.h"
//#include "zbxcompress.h"
#ifdef HAVE_ZLIB
#include "zlib.h"
#define ZBX_COMPRESS_STRERROR_LEN 512
static int zbx_zlib_errno = 0;
/******************************************************************************
* *
* Function: zbx_compress_strerror *
* *
* Purpose: returns last conversion error message *
* *
******************************************************************************/
const char *zbx_compress_strerror(void)
{
static char message[ZBX_COMPRESS_STRERROR_LEN];
switch (zbx_zlib_errno)
{
case Z_ERRNO:
zbx_strlcpy(message, zbx_strerror(errno), sizeof(message));
break;
case Z_MEM_ERROR:
zbx_strlcpy(message, "not enough memory", sizeof(message));
break;
case Z_BUF_ERROR:
zbx_strlcpy(message, "not enough space in output buffer", sizeof(message));
break;
case Z_DATA_ERROR:
zbx_strlcpy(message, "corrupted input data", sizeof(message));
break;
default:
zbx_snprintf(message, sizeof(message), "unknown error (%d)", zbx_zlib_errno);
break;
}
return message;
}
/******************************************************************************
* *
* Function: zbx_compress *
* *
* Purpose: compress data *
* *
* Parameters: in - [IN] the data to compress *
* size_in - [IN] the input data size *
* out - [OUT] the compressed data *
* size_out - [OUT] the compressed data size *
* *
* Return value: SUCCEED - the data was compressed successfully *
* FAIL - otherwise *
* *
* Comments: In the case of success the output buffer must be freed by the *
* caller. *
* *
******************************************************************************/
int zbx_compress(const char *in, size_t size_in, char **out, size_t *size_out)
{
Bytef *buf;
uLongf buf_size;
buf_size = compressBound(size_in);
buf = (Bytef *)zbx_malloc(NULL, buf_size);
if (Z_OK != (zbx_zlib_errno = compress(buf, &buf_size, (const Bytef *)in, size_in)))
{
zbx_free(buf);
return FAIL;
}
*out = (char *)buf;
*size_out = buf_size;
return SUCCEED;
}
/******************************************************************************
* *
* Function: zbx_uncompress *
* *
* Purpose: uncompress data *
* *
* Parameters: in - [IN] the data to uncompress *
* size_in - [IN] the input data size *
* out - [OUT] the uncompressed data *
* size_out - [IN/OUT] the buffer and uncompressed data size *
* *
* Return value: SUCCEED - the data was uncompressed successfully *
* FAIL - otherwise *
* *
******************************************************************************/
int zbx_uncompress(const char *in, size_t size_in, char *out, size_t *size_out)
{
uLongf size_o = *size_out;
if (Z_OK != (zbx_zlib_errno = uncompress((Bytef *)out, &size_o, (const Bytef *)in, size_in)))
return FAIL;
*size_out = size_o;
return SUCCEED;
}
#else
int zbx_compress(const char *in, size_t size_in, char **out, size_t *size_out)
{
ZBX_UNUSED(in);
ZBX_UNUSED(size_in);
ZBX_UNUSED(out);
ZBX_UNUSED(size_out);
return FAIL;
}
int zbx_uncompress(const char *in, size_t size_in, char *out, size_t *size_out)
{
ZBX_UNUSED(in);
ZBX_UNUSED(size_in);
ZBX_UNUSED(out);
ZBX_UNUSED(size_out);
return FAIL;
}
const char *zbx_compress_strerror(void)
{
return "";
}
#endif
|