mirror of
https://github.com/moparisthebest/curl
synced 2025-02-28 09:21:50 -05:00
zlib: ensure user provided memory functions are used by zlib, when given
As a bonus, this lets our MemoryTracking subsystem track zlib operations. And also fixes a shortcut some zlib 1.2.x versions took using malloc() instead of calloc(), which would trigger memory debuggers warnings on memory being used without having been initialized.
This commit is contained in:
parent
61ae7e9ce7
commit
1c400b4e5e
@ -49,6 +49,21 @@
|
|||||||
#define COMMENT 0x10 /* bit 4 set: file comment present */
|
#define COMMENT 0x10 /* bit 4 set: file comment present */
|
||||||
#define RESERVED 0xE0 /* bits 5..7: reserved */
|
#define RESERVED 0xE0 /* bits 5..7: reserved */
|
||||||
|
|
||||||
|
static voidpf
|
||||||
|
zalloc_cb(voidpf opaque, unsigned int items, unsigned int size)
|
||||||
|
{
|
||||||
|
(void) opaque;
|
||||||
|
/* not a typo, keep it calloc() */
|
||||||
|
return (voidpf) calloc(items, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
zfree_cb(voidpf opaque, voidpf ptr)
|
||||||
|
{
|
||||||
|
(void) opaque;
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
static CURLcode
|
static CURLcode
|
||||||
process_zlib_error(struct connectdata *conn, z_stream *z)
|
process_zlib_error(struct connectdata *conn, z_stream *z)
|
||||||
{
|
{
|
||||||
@ -159,6 +174,9 @@ Curl_unencode_deflate_write(struct connectdata *conn,
|
|||||||
/* Initialize zlib? */
|
/* Initialize zlib? */
|
||||||
if(k->zlib_init == ZLIB_UNINIT) {
|
if(k->zlib_init == ZLIB_UNINIT) {
|
||||||
memset(z, 0, sizeof(z_stream));
|
memset(z, 0, sizeof(z_stream));
|
||||||
|
z->zalloc = (alloc_func)zalloc_cb;
|
||||||
|
z->zfree = (free_func)zfree_cb;
|
||||||
|
|
||||||
if(inflateInit(z) != Z_OK)
|
if(inflateInit(z) != Z_OK)
|
||||||
return process_zlib_error(conn, z);
|
return process_zlib_error(conn, z);
|
||||||
k->zlib_init = ZLIB_INIT;
|
k->zlib_init = ZLIB_INIT;
|
||||||
@ -266,6 +284,8 @@ Curl_unencode_gzip_write(struct connectdata *conn,
|
|||||||
/* Initialize zlib? */
|
/* Initialize zlib? */
|
||||||
if(k->zlib_init == ZLIB_UNINIT) {
|
if(k->zlib_init == ZLIB_UNINIT) {
|
||||||
memset(z, 0, sizeof(z_stream));
|
memset(z, 0, sizeof(z_stream));
|
||||||
|
z->zalloc = (alloc_func)zalloc_cb;
|
||||||
|
z->zfree = (free_func)zfree_cb;
|
||||||
|
|
||||||
if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
|
if(strcmp(zlibVersion(), "1.2.0.4") >= 0) {
|
||||||
/* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
|
/* zlib ver. >= 1.2.0.4 supports transparent gzip decompressing */
|
||||||
|
@ -133,7 +133,6 @@ print <<HEAD
|
|||||||
* NEVER EVER edit this manually, fix the mkhelp.pl script instead!
|
* NEVER EVER edit this manually, fix the mkhelp.pl script instead!
|
||||||
* Generation time: $now
|
* Generation time: $now
|
||||||
*/
|
*/
|
||||||
#include "setup.h"
|
|
||||||
#ifdef USE_MANUAL
|
#ifdef USE_MANUAL
|
||||||
#include "hugehelp.h"
|
#include "hugehelp.h"
|
||||||
HEAD
|
HEAD
|
||||||
@ -141,6 +140,9 @@ HEAD
|
|||||||
if($c) {
|
if($c) {
|
||||||
print <<HEAD
|
print <<HEAD
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
#if defined(CURLDEBUG) && defined(CURLTOOLDEBUG)
|
||||||
|
#include "memdebug.h"
|
||||||
|
#endif
|
||||||
static const unsigned char hugehelpgz[] = {
|
static const unsigned char hugehelpgz[] = {
|
||||||
/* This mumbo-jumbo is the huge help text compressed with gzip.
|
/* This mumbo-jumbo is the huge help text compressed with gzip.
|
||||||
Thanks to this operation, the size of this data shrunk from $gzip
|
Thanks to this operation, the size of this data shrunk from $gzip
|
||||||
@ -165,6 +167,17 @@ HEAD
|
|||||||
|
|
||||||
print <<EOF
|
print <<EOF
|
||||||
#define BUF_SIZE 0x10000
|
#define BUF_SIZE 0x10000
|
||||||
|
static voidpf zalloc_func(voidpf opaque, unsigned int items, unsigned int size)
|
||||||
|
{
|
||||||
|
(void) opaque;
|
||||||
|
/* not a typo, keep it calloc() */
|
||||||
|
return (voidpf) calloc(items, size);
|
||||||
|
}
|
||||||
|
static void zfree_func(voidpf opaque, voidpf ptr)
|
||||||
|
{
|
||||||
|
(void) opaque;
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
/* Decompress and send to stdout a gzip-compressed buffer */
|
/* Decompress and send to stdout a gzip-compressed buffer */
|
||||||
void hugehelp(void)
|
void hugehelp(void)
|
||||||
{
|
{
|
||||||
@ -178,6 +191,8 @@ void hugehelp(void)
|
|||||||
|
|
||||||
headerlen = 10;
|
headerlen = 10;
|
||||||
memset(&z, 0, sizeof(z_stream));
|
memset(&z, 0, sizeof(z_stream));
|
||||||
|
z.zalloc = (alloc_func)zalloc_func;
|
||||||
|
z.zfree = (free_func)zfree_func;
|
||||||
z.avail_in = (unsigned int)(sizeof(hugehelpgz) - headerlen);
|
z.avail_in = (unsigned int)(sizeof(hugehelpgz) - headerlen);
|
||||||
z.next_in = (unsigned char *)hugehelpgz + headerlen;
|
z.next_in = (unsigned char *)hugehelpgz + headerlen;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user