/*
This file is part of GNUnet.
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 Affero General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
GNUnet 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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file util/container_meta_data.c
* @brief Storing of meta data
* @author Christian Grothoff
*/
#include "platform.h"
#include "gnunet_util_lib.h"
#if HAVE_EXTRACTOR_H
#include <extractor.h>
#endif
#include <zlib.h>
#define LOG(kind,...) GNUNET_log_from (kind, "util-container-meta-data", __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.
*/
struct MetaItem
{
/**
* This is a doubly linked list.
*/
struct MetaItem *next;
/**
* This is a doubly linked list.
*/
struct MetaItem *prev;
/**
* Name of the extracting plugin.
*/
char *plugin_name;
/**
* Mime-type of data.
*/
char *mime_type;
/**
* The actual meta data.
*/
char *data;
/**
* Number of bytes in 'data'.
*/
size_t data_size;
/**
* Type of the meta data.
*/
enum EXTRACTOR_MetaType type;
/**
* Format of the meta data.
*/
enum EXTRACTOR_MetaFormat format;
};
/**
* Meta data to associate with a file, directory or namespace.
*/
struct GNUNET_CONTAINER_MetaData
{
/**
* Head of linked list of the meta data items.
*/
struct MetaItem *items_head;
/**
* Tail of linked list of the meta data items.
*/
struct MetaItem *items_tail;
/**
* Complete serialized and compressed buffer of the items.
* NULL if we have not computed that buffer yet.
*/
char *sbuf;
/**
* Number of bytes in 'sbuf'. 0 if the buffer is stale.
*/
size_t sbuf_size;
/**
* Number of items in the linked list.
*/
unsigned int item_count;
};
/**
* Create a fresh struct CONTAINER_MetaData token.
*
* @return empty meta-data container
*