xbps/lib/portableproplib/prop_zlib.c

130 lines
4.3 KiB
C
Raw Normal View History

2012-07-11 03:03:19 +05:30
/*-
* Copyright (c) 2010-2014 Juan Romero Pardines.
2012-07-11 03:03:19 +05:30
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <prop/proplib.h>
#include "prop_object_impl.h"
#include <errno.h>
#include <zlib.h>
#define _READ_CHUNK 8192
2013-03-09 21:11:40 +05:30
#define TEMPLATE(type, objtype) \
2012-07-11 03:03:19 +05:30
bool \
prop ## type ## _externalize_to_zfile(prop ## type ## _t obj, const char *fname) \
{ \
char *xml; \
bool rv; \
int save_errno = 0; \
\
xml = prop ## type ## _externalize(obj); \
if (xml == NULL) \
return false; \
rv = _prop_object_externalize_write_file(fname, xml, strlen(xml), true); \
if (rv == false) \
save_errno = errno; \
_PROP_FREE(xml, M_TEMP); \
if (rv == false) \
errno = save_errno; \
\
return rv; \
} \
\
prop ## type ## _t \
prop ## type ## _internalize_from_zfile(const char *fname) \
{ \
struct _prop_object_internalize_mapped_file *mf; \
2013-03-09 21:11:40 +05:30
prop ## type ## _t obj = NULL; \
2012-07-11 03:03:19 +05:30
z_stream strm; \
unsigned char out[_READ_CHUNK+1]; \
2012-07-11 03:03:19 +05:30
char *uncomp_xml = NULL; \
size_t have; \
ssize_t totalsize = 0; \
int rv = 0; \
\
mf = _prop_object_internalize_map_file(fname); \
if (mf == NULL) \
return NULL; \
\
2013-03-09 21:11:40 +05:30
/* If it's an ordinary uncompressed plist we are done */ \
obj = prop ## type ## _internalize(mf->poimf_xml); \
if (prop_object_type(obj) == PROP_TYPE_## objtype) \
goto out; \
\
2012-07-11 03:03:19 +05:30
/* Output buffer (uncompressed) */ \
uncomp_xml = _PROP_MALLOC(_READ_CHUNK, M_TEMP); \
2013-03-09 21:11:40 +05:30
if (uncomp_xml == NULL) \
goto out; \
2012-07-11 03:03:19 +05:30
\
/* Decompress the mmap'ed buffer with zlib */ \
strm.zalloc = Z_NULL; \
strm.zfree = Z_NULL; \
strm.opaque = Z_NULL; \
strm.avail_in = 0; \
strm.next_in = Z_NULL; \
\
/* 15+16 to use gzip method */ \
2013-03-09 21:11:40 +05:30
if (inflateInit2(&strm, 15+16) != Z_OK) \
goto out2; \
\
2012-07-11 03:03:19 +05:30
strm.avail_in = mf->poimf_mapsize; \
strm.next_in = (unsigned char *)mf->poimf_xml; \
\
/* Inflate the input buffer and copy into 'uncomp_xml' */ \
do { \
strm.avail_out = _READ_CHUNK; \
strm.next_out = out; \
rv = inflate(&strm, Z_NO_FLUSH); \
switch (rv) { \
case Z_DATA_ERROR: \
case Z_STREAM_ERROR: \
case Z_NEED_DICT: \
case Z_MEM_ERROR: \
2013-03-09 21:11:40 +05:30
errno = EINVAL; \
goto out1; \
2012-07-11 03:03:19 +05:30
} \
have = _READ_CHUNK - strm.avail_out; \
totalsize += have; \
uncomp_xml = _PROP_REALLOC(uncomp_xml, totalsize, M_TEMP); \
memcpy(uncomp_xml + totalsize - have, out, have); \
} while (strm.avail_out == 0); \
\
/* we are done */ \
2013-03-09 21:11:40 +05:30
out2: \
2012-07-11 03:03:19 +05:30
(void)inflateEnd(&strm); \
2013-03-09 21:11:40 +05:30
out1: \
2012-07-11 03:03:19 +05:30
obj = prop ## type ## _internalize(uncomp_xml); \
_PROP_FREE(uncomp_xml, M_TEMP); \
2013-03-09 21:11:40 +05:30
out: \
2012-07-11 03:03:19 +05:30
_prop_object_internalize_unmap_file(mf); \
\
return obj; \
}
2013-03-09 21:11:40 +05:30
TEMPLATE(_array, ARRAY)
TEMPLATE(_dictionary, DICTIONARY)
2012-07-11 03:03:19 +05:30
#undef TEMPLATE