2010-10-13 21:17:51 +02:00
|
|
|
/*
|
|
|
|
* blob - library for generating/parsing tagged binary data
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
|
|
|
|
*
|
2011-11-10 09:05:33 +01:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2010-10-13 21:17:51 +02:00
|
|
|
*
|
2011-11-10 09:05:33 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2010-10-13 21:17:51 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BLOB_H__
|
|
|
|
#define _BLOB_H__
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2015-12-11 15:01:34 +01:00
|
|
|
#include <assert.h>
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2015-12-29 00:11:37 +01:00
|
|
|
#include "blob_field.h"
|
2013-02-11 21:46:41 +01:00
|
|
|
#include "utils.h"
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2015-12-28 21:44:37 +01:00
|
|
|
// arbitrary max size just to make sure we don't try to resize to a crazy value (should be something large yet reasonable!)
|
|
|
|
#define BLOB_MAX_SIZE (10000000)
|
|
|
|
|
2015-12-15 11:55:50 +01:00
|
|
|
/*
|
|
|
|
Blob attributes can have any of the following types.
|
|
|
|
*/
|
2013-10-16 01:22:02 +02:00
|
|
|
|
2010-10-13 21:17:51 +02:00
|
|
|
enum {
|
2015-12-29 00:11:37 +01:00
|
|
|
BLOB_FIELD_INVALID,
|
|
|
|
BLOB_FIELD_BINARY, // a binary blob. We don't care about content.
|
|
|
|
BLOB_FIELD_STRING, // a null terminated string
|
|
|
|
BLOB_FIELD_INT8, // an 8 bit signed/unsigned integer
|
|
|
|
BLOB_FIELD_INT16, // a 16 bit signed/unsigned integer
|
|
|
|
BLOB_FIELD_INT32, // a 32 bit signed/unsigned int
|
|
|
|
BLOB_FIELD_INT64, // a 64 bit signed/unsigned int
|
|
|
|
BLOB_FIELD_FLOAT32, // a packed 32 bit float
|
|
|
|
BLOB_FIELD_FLOAT64, // a packed 64 bit float
|
|
|
|
BLOB_FIELD_ARRAY, // only unnamed elements
|
|
|
|
BLOB_FIELD_TABLE, // only named elements
|
2016-02-16 13:07:44 +01:00
|
|
|
BLOB_FIELD_ANY, // to be used only as a wildcard
|
2015-12-29 00:11:37 +01:00
|
|
|
BLOB_FIELD_LAST
|
2010-10-13 21:17:51 +02:00
|
|
|
};
|
|
|
|
|
2015-12-29 00:11:37 +01:00
|
|
|
struct blob {
|
2015-12-07 16:01:33 +01:00
|
|
|
size_t memlen; // total length of the allocated memory area
|
2015-12-06 20:54:33 +01:00
|
|
|
void *buf; // raw buffer data
|
2010-10-13 21:17:51 +02:00
|
|
|
};
|
|
|
|
|
2016-02-16 13:07:44 +01:00
|
|
|
struct blob_policy {
|
|
|
|
const char *name;
|
|
|
|
int type;
|
|
|
|
struct blob_field *value;
|
|
|
|
};
|
|
|
|
|
2015-12-29 00:11:37 +01:00
|
|
|
//! Initializes a blob structure. Optionally takes memory area to be copied into the buffer which must represent a valid blob buf.
|
|
|
|
void blob_init(struct blob *buf, const char *data, size_t size);
|
2015-12-15 11:55:50 +01:00
|
|
|
//! Frees the memory allocated with the buffer
|
2015-12-29 00:11:37 +01:00
|
|
|
void blob_free(struct blob *buf);
|
2015-12-15 11:55:50 +01:00
|
|
|
//! Resets header but does not deallocate any memory.
|
2015-12-29 00:11:37 +01:00
|
|
|
void blob_reset(struct blob *buf);
|
2015-12-15 11:55:50 +01:00
|
|
|
//! Resizes the buffer. Can only be used to increase size.
|
2015-12-29 00:11:37 +01:00
|
|
|
bool blob_resize(struct blob *buf, int newsize);
|
2015-12-15 11:55:50 +01:00
|
|
|
|
|
|
|
//! Returns pointer to header attribute (the first element) which is also raw buffer
|
2015-12-29 00:11:37 +01:00
|
|
|
static inline struct blob_field *blob_head(struct blob *self){
|
|
|
|
return (struct blob_field*)self->buf;
|
2015-12-14 16:29:39 +01:00
|
|
|
}
|
|
|
|
|
2015-12-15 11:55:50 +01:00
|
|
|
//! returns size of the whole buffer (including header element and padding)
|
2016-01-14 14:58:17 +01:00
|
|
|
static inline uint32_t blob_size(struct blob *self){ return blob_field_raw_pad_len(blob_head(self)); }
|
2015-12-14 16:29:39 +01:00
|
|
|
|
2015-12-15 11:55:50 +01:00
|
|
|
typedef void* blob_offset_t;
|
|
|
|
|
|
|
|
/********************************
|
|
|
|
** NESTED ELEMENTS
|
|
|
|
********************************/
|
|
|
|
|
|
|
|
//! opens an array element
|
2015-12-29 00:11:37 +01:00
|
|
|
blob_offset_t blob_open_array(struct blob *buf);
|
2015-12-15 11:55:50 +01:00
|
|
|
//! closes an array element
|
2015-12-29 00:11:37 +01:00
|
|
|
void blob_close_array(struct blob *buf, blob_offset_t);
|
2015-12-15 11:55:50 +01:00
|
|
|
//! opens an table element
|
2015-12-29 00:11:37 +01:00
|
|
|
blob_offset_t blob_open_table(struct blob *buf);
|
2015-12-15 11:55:50 +01:00
|
|
|
//! closes an table element
|
2015-12-29 00:11:37 +01:00
|
|
|
void blob_close_table(struct blob *buf, blob_offset_t);
|
2015-12-15 11:55:50 +01:00
|
|
|
|
|
|
|
/********************************
|
|
|
|
** WRITING FUNCTIONS
|
|
|
|
********************************/
|
|
|
|
|
|
|
|
//! Write a string at the end of the buffer
|
2015-12-29 00:11:37 +01:00
|
|
|
/*
|
|
|
|
struct blob_field *blob_put_u8(struct blob *buf, uint8_t val);
|
|
|
|
struct blob_field *blob_put_u16(struct blob *buf, uint16_t val);
|
|
|
|
struct blob_field *blob_put_u32(struct blob *buf, uint32_t val);
|
|
|
|
struct blob_field *blob_put_u64(struct blob *buf, uint64_t val);
|
2015-12-15 11:55:50 +01:00
|
|
|
|
2015-12-29 00:11:37 +01:00
|
|
|
#define blob_put_i8 blob_put_u8
|
|
|
|
#define blob_put_i16 blob_put_u16
|
|
|
|
#define blob_put_i32 blob_put_u32
|
|
|
|
#define blob_put_i64 blob_put_u64
|
|
|
|
*/
|
2015-12-15 11:55:50 +01:00
|
|
|
|
2016-01-11 09:39:05 +01:00
|
|
|
//! write a boolean into the buffer
|
2015-12-29 00:11:37 +01:00
|
|
|
struct blob_field *blob_put_bool(struct blob *buf, bool val);
|
2016-01-11 09:39:05 +01:00
|
|
|
|
|
|
|
//! write a string into the buffer
|
2015-12-29 00:11:37 +01:00
|
|
|
struct blob_field *blob_put_string(struct blob *buf, const char *str);
|
2015-12-16 15:24:40 +01:00
|
|
|
|
2016-01-11 09:39:05 +01:00
|
|
|
//! write binary data into the buffer
|
|
|
|
struct blob_field *blob_put_binary(struct blob *buf, const void *data, unsigned int size);
|
|
|
|
|
|
|
|
//! write a number into the buffer
|
|
|
|
struct blob_field *blob_put_int(struct blob *buf, long long val);
|
|
|
|
|
|
|
|
//! write a real into the buffer
|
|
|
|
struct blob_field *blob_put_real(struct blob *buf, double value);
|
|
|
|
|
|
|
|
//! write a raw attribute into the buffer
|
2015-12-29 00:11:37 +01:00
|
|
|
struct blob_field *blob_put_attr(struct blob *buf, struct blob_field *attr);
|
2015-12-15 11:55:50 +01:00
|
|
|
|
|
|
|
//! print out the whole buffer
|
2015-12-29 00:11:37 +01:00
|
|
|
void blob_dump(struct blob *self);
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
#endif
|