/*
This file is part of GNUnet.
- Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+
+/**
+ * Try to compress the given block of data using libz. Only returns
+ * the compressed block if compression worked and the new block is
+ * actually smaller. Decompress using #GNUNET_decompress().
+ *
+ * @param data block to compress; if compression
+ * resulted in a smaller block, the first
+ * bytes of data are updated to the compressed
+ * data
+ * @param old_size number of bytes in data
+ * @param[out] result set to the compressed data, if compression worked
+ * @param[out] new_size set to size of result, if compression worked
+ * @return #GNUNET_YES if compression reduce the size,
+ * #GNUNET_NO if compression did not help
+ */
+int
+GNUNET_try_compression (const char *data,
+ size_t old_size,
+ char **result,
+ size_t *new_size)
+{
+ char *tmp;
+ uLongf dlen;
+
+ *result = NULL;
+ *new_size = 0;
+#ifdef compressBound
+ dlen = compressBound (old_size);
+#else
+ dlen = old_size + (old_size / 100) + 20;
+ /* documentation says 100.1% oldSize + 12 bytes, but we
+ * should be able to overshoot by more to be safe */
+#endif
+ tmp = GNUNET_malloc (dlen);
+ if (Z_OK ==
+ compress2 ((Bytef *) tmp,
+ &dlen,
+ (const Bytef *) data,
+ old_size, 9))
+ {
+ if (dlen < old_size)
+ {
+ *result = tmp;
+ *new_size = dlen;
+ return GNUNET_YES;
+ }
+ }
+ GNUNET_free (tmp);
+ return GNUNET_NO;
+}
+
+
+/**
+ * Decompress input, return the decompressed data as output. Dual to
+ * #GNUNET_try_compression(). Caller must set @a output_size to the
+ * number of bytes that were originally compressed.
+ *
+ * @param input compressed data
+ * @param input_size number of bytes in input
+ * @param output_size expected size of the output
+ * @return NULL on error, buffer of @a output_size decompressed bytes otherwise
+ */
+char *
+GNUNET_decompress (const char *input,
+ size_t input_size,
+ size_t output_size)
+{
+ char *output;
+ uLongf olen;
+
+ olen = output_size;
+ output = GNUNET_malloc (olen);
+ if (Z_OK ==
+ uncompress ((Bytef *) output,
+ &olen,
+ (const Bytef *) input,
+ input_size))
+ return output;
+ GNUNET_free (output);
+ return NULL;
+}
+
+
/**
* Meta data item.
*/
(NULL == data_mime_type) ? NULL : GNUNET_strdup (data_mime_type);
mi->plugin_name = (NULL == plugin_name) ? NULL : GNUNET_strdup (plugin_name);
mi->data = GNUNET_malloc (data_size);
- memcpy (mi->data, data, data_size);
+ GNUNET_memcpy (mi->data, data, data_size);
/* change all dir separators to POSIX style ('/') */
if ( (EXTRACTOR_METATYPE_FILENAME == type) ||
(EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME == type) )
{
char *ret;
va_list args;
- enum EXTRACTOR_MetaType type;
+ int type;
if (NULL == md)
return NULL;
va_start (args, md);
while (1)
{
- type = va_arg (args, enum EXTRACTOR_MetaType);
+ type = va_arg (args, int);
if (-1 == type)
break;
if (NULL != (ret = GNUNET_CONTAINER_meta_data_get_by_type (md, type)))
if ((NULL == match) || (0 == match->data_size))
return 0;
*thumb = GNUNET_malloc (match->data_size);
- memcpy (*thumb, match->data, match->data_size);
+ GNUNET_memcpy (*thumb, match->data, match->data_size);
return match->data_size;
}
}
-
-/**
- * Try to compress the given block of data.
- *
- * @param data block to compress; if compression
- * resulted in a smaller block, the first
- * bytes of data are updated to the compressed
- * data
- * @param oldSize number of bytes in data
- * @param result set to the compressed data
- * @param newSize set to size of result
- * @return #GNUNET_YES if compression reduce the size,
- * #GNUNET_NO if compression did not help
- */
-static int
-try_compression (const char *data, size_t oldSize, char **result,
- size_t * newSize)
-{
- char *tmp;
- uLongf dlen;
-
-#ifdef compressBound
- dlen = compressBound (oldSize);
-#else
- dlen = oldSize + (oldSize / 100) + 20;
- /* documentation says 100.1% oldSize + 12 bytes, but we
- * should be able to overshoot by more to be safe */
-#endif
- tmp = GNUNET_malloc (dlen);
- if (Z_OK ==
- compress2 ((Bytef *) tmp, &dlen, (const Bytef *) data, oldSize, 9))
- {
- if (dlen < oldSize)
- {
- *result = tmp;
- *newSize = dlen;
- return GNUNET_YES;
- }
- }
- GNUNET_free (tmp);
- return GNUNET_NO;
-}
-
-
/**
* Flag in 'version' that indicates compressed meta-data.
*/
{
if (NULL == *target)
*target = GNUNET_malloc (md->sbuf_size);
- memcpy (*target, md->sbuf, md->sbuf_size);
+ GNUNET_memcpy (*target, md->sbuf, md->sbuf_size);
return md->sbuf_size;
}
if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_PART))
if ((EXTRACTOR_METAFORMAT_UTF8 == pos->format) ||
(EXTRACTOR_METAFORMAT_C_STRING == pos->format))
GNUNET_break ('\0' == pos->data[pos->data_size - 1]);
- memcpy (&mdata[off], pos->data, pos->data_size);
+ GNUNET_memcpy (&mdata[off], pos->data, pos->data_size);
off -= plen;
if (NULL != pos->plugin_name)
- memcpy (&mdata[off], pos->plugin_name, plen);
+ GNUNET_memcpy (&mdata[off], pos->plugin_name, plen);
off -= mlen;
if (NULL != pos->mime_type)
- memcpy (&mdata[off], pos->mime_type, mlen);
+ GNUNET_memcpy (&mdata[off], pos->mime_type, mlen);
i++;
}
GNUNET_assert (0 == off);
{
comp = GNUNET_NO;
if (0 == (opt & GNUNET_CONTAINER_META_DATA_SERIALIZE_NO_COMPRESS))
- comp = try_compression ((const char *) &ent[i], left, &cdata, &clen);
+ comp = GNUNET_try_compression ((const char *) &ent[i],
+ left,
+ &cdata,
+ &clen);
if ((NULL == md->sbuf) && (0 == i))
{
{
GNUNET_assert (clen < left);
hdr->version = htonl (2 | HEADER_COMPRESSED);
- memcpy (&hdr[1], cdata, clen);
+ GNUNET_memcpy (&hdr[1], cdata, clen);
vmd->sbuf_size = clen + sizeof (struct MetaDataHeader);
}
else
{
hdr->version = htonl (2);
- memcpy (&hdr[1], &ent[0], left);
+ GNUNET_memcpy (&hdr[1], &ent[0], left);
vmd->sbuf_size = left + sizeof (struct MetaDataHeader);
}
vmd->sbuf = (char *) hdr;
hdr->version = htonl (2 | HEADER_COMPRESSED);
hdr->size = htonl (left);
hdr->entries = htonl (md->item_count - i);
- memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen);
+ GNUNET_memcpy (&dst[sizeof (struct MetaDataHeader)], cdata, clen);
GNUNET_free (cdata);
cdata = NULL;
GNUNET_free (ent);
hdr->version = htonl (2);
hdr->entries = htonl (md->item_count - i);
hdr->size = htonl (left);
- memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left);
+ GNUNET_memcpy (&dst[sizeof (struct MetaDataHeader)], &ent[i], left);
GNUNET_free (ent);
rlen = left + sizeof (struct MetaDataHeader);
}
if (NULL != *target)
{
if (GNUNET_YES == comp)
- memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
+ GNUNET_memcpy (*target, dst, clen + sizeof (struct MetaDataHeader));
else
- memcpy (*target, dst, left + sizeof (struct MetaDataHeader));
+ GNUNET_memcpy (*target, dst, left + sizeof (struct MetaDataHeader));
GNUNET_free (dst);
}
else
ihdr.size = htonl (0);
if (NULL == *target)
*target = (char *) GNUNET_new (struct MetaDataHeader);
- memcpy (*target, &ihdr, sizeof (struct MetaDataHeader));
+ GNUNET_memcpy (*target, &ihdr, sizeof (struct MetaDataHeader));
return sizeof (struct MetaDataHeader);
}
}
-/**
- * Decompress input, return the decompressed data
- * as output, set outputSize to the number of bytes
- * that were found.
- *
- * @param input compressed data
- * @param inputSize number of bytes in input
- * @param outputSize expected size of the output
- * @return NULL on error
- */
-static char *
-decompress (const char *input, size_t inputSize, size_t outputSize)
-{
- char *output;
- uLongf olen;
-
- olen = outputSize;
- output = GNUNET_malloc (olen);
- if (Z_OK ==
- uncompress ((Bytef *) output, &olen, (const Bytef *) input, inputSize))
- return output;
- GNUNET_free (output);
- return NULL;
-}
-
-
/**
* Deserialize meta-data. Initializes md.
*
if (size < sizeof (struct MetaDataHeader))
return NULL;
- memcpy (&hdr, input, sizeof (struct MetaDataHeader));
+ GNUNET_memcpy (&hdr, input, sizeof (struct MetaDataHeader));
version = ntohl (hdr.version) & HEADER_VERSION_MASK;
compressed = (ntohl (hdr.version) & HEADER_COMPRESSED) != 0;
return NULL;
}
data =
- decompress ((const char *) &input[sizeof (struct MetaDataHeader)],
- size - sizeof (struct MetaDataHeader), dataSize);
+ GNUNET_decompress ((const char *) &input[sizeof (struct MetaDataHeader)],
+ size - sizeof (struct MetaDataHeader),
+ dataSize);
if (NULL == data)
{
GNUNET_break_op (0);
mdata = &cdata[ic * sizeof (struct MetaDataEntry)];
for (i = 0; i < ic; i++)
{
- memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)],
+ GNUNET_memcpy (&ent, &cdata[i * sizeof (struct MetaDataEntry)],
sizeof (struct MetaDataEntry));
format = (enum EXTRACTOR_MetaFormat) ntohl (ent.format);
if ((EXTRACTOR_METAFORMAT_UTF8 != format) &&