Changeset View
Changeset View
Standalone View
Standalone View
sys/geom/uzip/g_uzip_zlib.c
Show All 27 Lines | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/systm.h> | #include <sys/systm.h> | ||||
#include <sys/malloc.h> | #include <sys/malloc.h> | ||||
#include <sys/zlib.h> | #include <zlib.h> | ||||
#include <geom/uzip/g_uzip.h> | #include <geom/uzip/g_uzip.h> | ||||
#include <geom/uzip/g_uzip_dapi.h> | #include <geom/uzip/g_uzip_dapi.h> | ||||
#include <geom/uzip/g_uzip_zlib.h> | #include <geom/uzip/g_uzip_zlib.h> | ||||
struct g_uzip_zlib { | struct g_uzip_zlib { | ||||
uint32_t blksz; | uint32_t blksz; | ||||
struct g_uzip_dapi pub; | struct g_uzip_dapi pub; | ||||
/* Zlib decoder structs */ | /* Zlib decoder structs */ | ||||
z_stream zs; | z_stream zs; | ||||
}; | }; | ||||
static void *z_alloc(void *, u_int, u_int); | static void *g_unzip_zalloc(void *, u_int, u_int); | ||||
ota_j.email.ne.jp: I've seen multiple implementation of z_alloc and zfree. Let's see how many there are and if… | |||||
static void z_free(void *, void *); | static void g_unzip_zfree(void *, void *); | ||||
static int g_uzip_zlib_rewind(struct g_uzip_dapi *, const char *); | static int g_uzip_zlib_rewind(struct g_uzip_dapi *, const char *); | ||||
static void | static void | ||||
g_uzip_zlib_free(struct g_uzip_dapi *zpp) | g_uzip_zlib_free(struct g_uzip_dapi *zpp) | ||||
{ | { | ||||
struct g_uzip_zlib *zp; | struct g_uzip_zlib *zp; | ||||
zp = (struct g_uzip_zlib *)zpp->pvt; | zp = (struct g_uzip_zlib *)zpp->pvt; | ||||
inflateEnd(&zp->zs); | z_inflateEnd(&zp->zs); | ||||
free(zp, M_GEOM_UZIP); | free(zp, M_GEOM_UZIP); | ||||
} | } | ||||
static int | static int | ||||
g_uzip_zlib_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *ibp, | g_uzip_zlib_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *ibp, | ||||
size_t ilen, void *obp) | size_t ilen, void *obp) | ||||
{ | { | ||||
int err; | int err; | ||||
struct g_uzip_zlib *zp; | struct g_uzip_zlib *zp; | ||||
zp = (struct g_uzip_zlib *)zpp->pvt; | zp = (struct g_uzip_zlib *)zpp->pvt; | ||||
zp->zs.next_in = ibp; | zp->zs.next_in = ibp; | ||||
zp->zs.avail_in = ilen; | zp->zs.avail_in = ilen; | ||||
zp->zs.next_out = obp; | zp->zs.next_out = obp; | ||||
zp->zs.avail_out = zp->blksz; | zp->zs.avail_out = zp->blksz; | ||||
err = (inflate(&zp->zs, Z_FINISH) != Z_STREAM_END) ? 1 : 0; | err = (z_inflate(&zp->zs, Z_FINISH) != Z_STREAM_END) ? 1 : 0; | ||||
if (err != 0) { | if (err != 0) { | ||||
printf("%s: UZIP(zlib) inflate() failed\n", gp_name); | printf("%s: UZIP(zlib) z_inflate() failed\n", gp_name); | ||||
} | } | ||||
return (err); | return (err); | ||||
} | } | ||||
static int | static int | ||||
g_uzip_zlib_rewind(struct g_uzip_dapi *zpp, const char *gp_name) | g_uzip_zlib_rewind(struct g_uzip_dapi *zpp, const char *gp_name) | ||||
{ | { | ||||
int err; | int err; | ||||
struct g_uzip_zlib *zp; | struct g_uzip_zlib *zp; | ||||
zp = (struct g_uzip_zlib *)zpp->pvt; | zp = (struct g_uzip_zlib *)zpp->pvt; | ||||
err = 0; | err = 0; | ||||
if (inflateReset(&zp->zs) != Z_OK) { | if (z_inflateReset(&zp->zs) != Z_OK) { | ||||
printf("%s: UZIP(zlib) decoder reset failed\n", gp_name); | printf("%s: UZIP(zlib) decoder reset failed\n", gp_name); | ||||
err = 1; | err = 1; | ||||
} | } | ||||
return (err); | return (err); | ||||
} | } | ||||
static int | |||||
z_compressBound(int len) | |||||
{ | |||||
return (len + (len >> 12) + (len >> 14) + 11); | |||||
} | |||||
struct g_uzip_dapi * | struct g_uzip_dapi * | ||||
Done Inline Actionsdelphij: @sobomax -- this was introduced in rS302284, and it appears that the value was from the zlib… | |||||
Done Inline ActionsYes, I think so. There was no such API in old zlib. sobomax: Yes, I think so. There was no such API in old zlib. | |||||
g_uzip_zlib_ctor(uint32_t blksz) | g_uzip_zlib_ctor(uint32_t blksz) | ||||
{ | { | ||||
struct g_uzip_zlib *zp; | struct g_uzip_zlib *zp; | ||||
zp = malloc(sizeof(struct g_uzip_zlib), M_GEOM_UZIP, M_WAITOK); | zp = malloc(sizeof(struct g_uzip_zlib), M_GEOM_UZIP, M_WAITOK); | ||||
zp->zs.zalloc = z_alloc; | zp->zs.zalloc = g_unzip_zalloc; | ||||
zp->zs.zfree = z_free; | zp->zs.zfree = g_unzip_zfree; | ||||
if (inflateInit(&zp->zs) != Z_OK) { | if (z_inflateInit(&zp->zs) != Z_OK) { | ||||
goto e1; | goto e1; | ||||
} | } | ||||
zp->blksz = blksz; | zp->blksz = blksz; | ||||
zp->pub.max_blen = z_compressBound(blksz); | zp->pub.max_blen = z_compressBound(blksz); | ||||
Done Inline Actions(Note that we should probably use the zlib provided z_compressBound here, see comment above). delphij: (Note that we should probably use the zlib provided z_compressBound here, see comment above). | |||||
Done Inline Actions/nod. sobomax: /nod. | |||||
Done Inline ActionsOk. ota_j.email.ne.jp: Ok. | |||||
zp->pub.decompress = &g_uzip_zlib_decompress; | zp->pub.decompress = &g_uzip_zlib_decompress; | ||||
zp->pub.free = &g_uzip_zlib_free; | zp->pub.free = &g_uzip_zlib_free; | ||||
zp->pub.rewind = &g_uzip_zlib_rewind; | zp->pub.rewind = &g_uzip_zlib_rewind; | ||||
zp->pub.pvt = (void *)zp; | zp->pub.pvt = (void *)zp; | ||||
return (&zp->pub); | return (&zp->pub); | ||||
e1: | e1: | ||||
free(zp, M_GEOM_UZIP); | free(zp, M_GEOM_UZIP); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
static void * | static void * | ||||
z_alloc(void *nil, u_int type, u_int size) | g_unzip_zalloc(void *nil, u_int type, u_int size) | ||||
{ | { | ||||
void *ptr; | void *ptr; | ||||
ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT); | ptr = malloc(type * size, M_GEOM_UZIP, M_NOWAIT); | ||||
return (ptr); | return (ptr); | ||||
} | } | ||||
static void | static void | ||||
z_free(void *nil, void *ptr) | g_unzip_zfree(void *nil, void *ptr) | ||||
{ | { | ||||
free(ptr, M_GEOM_UZIP); | free(ptr, M_GEOM_UZIP); | ||||
} | } |
I've seen multiple implementation of z_alloc and zfree. Let's see how many there are and if they are the same.