port to avr

This commit is contained in:
Martin Schröder 2016-09-10 01:22:17 +02:00
parent 4b3b52eed3
commit d8f7e32338
10 changed files with 157 additions and 169 deletions

View file

@ -24,12 +24,6 @@
#define unpack754_32(i) (unpack754((i), 32, 8))
#define unpack754_64(i) (unpack754((i), 64, 11))
extern void blob_field_fill_pad(struct blob_field *attr);
extern void blob_field_set_raw_len(struct blob_field *attr, unsigned int len);
uint64_t pack754(long double f, unsigned bits, unsigned expbits);
long double unpack754(uint64_t i, unsigned bits, unsigned expbits);
static inline struct blob_field *blob_offset_to_attr(struct blob *buf, blob_offset_t offset){
void *ptr = (char *)buf->buf + (size_t)offset;
return ptr;
@ -49,18 +43,18 @@ static void blob_field_init(struct blob_field *attr, uint32_t id, uint32_t len){
}
//! Attepts to reallocate the buffer to fit the new payload data
bool blob_resize(struct blob *buf, int minlen){
bool blob_resize(struct blob *buf, uint32_t minlen){
assert(minlen > 0 && minlen < BLOB_MAX_SIZE);
char *new = 0;
int newsize = ((minlen / 256) + 1) * 256;
int cur_size = blob_size(buf);
uint32_t newsize = ((minlen / 256) + 1) * 256;
uint32_t cur_size = blob_size(buf);
// reallocate the memory of the buffer if we no longer have any memory left
if(newsize > buf->memlen){
new = realloc(buf->buf, newsize);
if (new) {
buf->buf = new;
memset(buf->buf + cur_size, 0, newsize - cur_size);
memset((char*)buf->buf + cur_size, 0, newsize - cur_size);
buf->memlen = newsize;
} else {
return false;
@ -122,7 +116,7 @@ static struct blob_field *blob_new_attr(struct blob *buf, int id, int payload){
//DEBUG("adding attr at offset %d - ", cur_len);
struct blob_field *attr = (struct blob_field*)(buf->buf + cur_len);
struct blob_field *attr = (struct blob_field*)((char*)buf->buf + cur_len);
blob_field_init(attr, id, attr_raw_len);
blob_field_fill_pad(attr);
@ -166,7 +160,7 @@ static struct blob_field *blob_put(struct blob *buf, int id, const void *ptr, un
}
if (ptr)
memcpy(blob_field_data(attr), ptr, len);
memcpy(attr->data, ptr, len);
return attr;
}
@ -176,21 +170,21 @@ struct blob_field *blob_put_string(struct blob *buf, const char *str){
return blob_put(buf, BLOB_FIELD_STRING, str, strlen(str) + 1);
}
struct blob_field *blob_put_u8(struct blob *buf, uint8_t val){
static struct blob_field *blob_put_u8(struct blob *buf, uint8_t val){
return blob_put(buf, BLOB_FIELD_INT8, &val, sizeof(val));
}
struct blob_field *blob_put_u16(struct blob *buf, uint16_t val){
static struct blob_field *blob_put_u16(struct blob *buf, uint16_t val){
val = cpu_to_be16(val);
return blob_put(buf, BLOB_FIELD_INT16, &val, sizeof(val));
}
struct blob_field *blob_put_u32(struct blob *buf, uint32_t val){
static struct blob_field *blob_put_u32(struct blob *buf, uint32_t val){
val = cpu_to_be32(val);
return blob_put(buf, BLOB_FIELD_INT32, &val, sizeof(val));
}
struct blob_field *blob_put_u64(struct blob *buf, uint64_t val){
static struct blob_field *blob_put_u64(struct blob *buf, uint64_t val){
val = cpu_to_be64(val);
return blob_put(buf, BLOB_FIELD_INT64, &val, sizeof(val));
}
@ -215,7 +209,7 @@ blob_offset_t blob_open_array(struct blob *buf){
void blob_close_array(struct blob *buf, blob_offset_t offset){
if((long)offset > (long)blob_size(buf)) return;
struct blob_field *attr = blob_offset_to_attr(buf, offset);
int len = (buf->buf + blob_field_raw_len(blob_head(buf))) - (void*)attr;
int len = ((char*)buf->buf + blob_field_raw_len(blob_head(buf))) - (char*)attr;
blob_field_set_raw_len(attr, len);
}
@ -227,16 +221,16 @@ blob_offset_t blob_open_table(struct blob *buf){
void blob_close_table(struct blob *buf, blob_offset_t offset){
if((long)offset > (long)blob_size(buf)) return;
struct blob_field *attr = blob_offset_to_attr(buf, offset);
int len = (buf->buf + blob_field_raw_len(blob_head(buf))) - (void*)attr;
int len = ((char*)buf->buf + blob_field_raw_len(blob_head(buf))) - (char*)attr;
blob_field_set_raw_len(attr, len);
}
struct blob_field *blob_put_float(struct blob *buf, double value){
static struct blob_field *blob_put_float(struct blob *buf, double value){
uint32_t val = cpu_to_be32(pack754_32((float)value));
return blob_put(buf, BLOB_FIELD_FLOAT32, &val, sizeof(val));
}
struct blob_field *blob_put_double(struct blob *buf, double value){
static struct blob_field *blob_put_double(struct blob *buf, double value){
uint64_t val = cpu_to_be64(pack754_64(value));
return blob_put(buf, BLOB_FIELD_FLOAT64, &val, sizeof(val));
}
@ -246,7 +240,7 @@ struct blob_field *blob_put_real(struct blob *buf, double value){
return blob_put_double(buf, value);
}
struct blob_field *blob_put_attr(struct blob *buf, struct blob_field *attr){
struct blob_field *blob_put_attr(struct blob *buf, const struct blob_field *attr){
if(!attr) return NULL;
size_t s = blob_field_data_len(attr);
@ -255,7 +249,7 @@ struct blob_field *blob_put_attr(struct blob *buf, struct blob_field *attr){
return f;
}
static void __attribute__((unused)) _blob_field_dump(struct blob_field *node, int indent){
static void __attribute__((unused)) _blob_field_dump(const struct blob_field *node, uint32_t indent){
static const char *names[] = {
[BLOB_FIELD_INVALID] = "BLOB_FIELD_INVALID",
[BLOB_FIELD_BINARY] = "BLOB_FIELD_BINARY",
@ -270,19 +264,19 @@ static void __attribute__((unused)) _blob_field_dump(struct blob_field *node, in
[BLOB_FIELD_TABLE] = "BLOB_FIELD_TABLE"
};
for(struct blob_field *attr = blob_field_first_child(node); attr; attr = blob_field_next_child(node, attr)){
char *data = (char*)attr; //blob_field_data(attr);
for(const struct blob_field *attr = blob_field_first_child(node); attr; attr = blob_field_next_child(node, attr)){
const unsigned char *data = (const unsigned char*)attr; //blob_field_data(attr);
int id = blob_field_type(attr);
int len = blob_field_raw_pad_len(attr);
uint32_t id = blob_field_type(attr);
uint32_t len = blob_field_raw_pad_len(attr);
int offset = (node)?((int)((char*)attr - (char*)node)):0;
for(int c = 0; c < indent; c++) printf("\t");
uint32_t offset = (node)?((uint32_t)((const char*)attr - (const char*)node)):0;
for(uint32_t c = 0; c < indent; c++) printf("\t");
printf("[ field (");
for(int c = 0; c < sizeof(struct blob_field); c++){
printf("%02x", (int)*((char*)attr + c) & 0xff);
for(uint32_t c = 0; c < sizeof(struct blob_field); c++){
printf("%02x", (int)*((const char*)attr + c) & 0xff);
}
printf(") type=%s offset=%d full padded len: %d, header+data: %d, data len: %d ]\n", names[(id < BLOB_FIELD_LAST)?id:0], offset, len, blob_field_raw_len(attr), blob_field_data_len(attr));
printf(") type=%s offset=%d full padded len: %d, header+data: %d, data len: %d ]\n", names[(id < BLOB_FIELD_LAST)?id:0], (int)offset, (int)len, (int)blob_field_raw_len(attr), blob_field_data_len(attr));
if(id == BLOB_FIELD_ARRAY || id == BLOB_FIELD_TABLE) {
_blob_field_dump(attr, indent+1);
@ -290,7 +284,7 @@ static void __attribute__((unused)) _blob_field_dump(struct blob_field *node, in
}
printf("\t");
for(int c = 0; c < blob_field_raw_pad_len(attr); c++){
for(uint32_t c = 0; c < blob_field_raw_pad_len(attr); c++){
if(c > 0 && c % 10 == 0)
printf("\n\t");
printf(" %02x(%c)", data[c] & 0xff, (data[c] > 0x10 && data[c] < 128)?data[c]:'.');
@ -299,7 +293,7 @@ static void __attribute__((unused)) _blob_field_dump(struct blob_field *node, in
}
}
void blob_field_dump(struct blob_field *self){
void blob_field_dump(const struct blob_field *self){
_blob_field_dump(self, 0);
}

View file

@ -61,7 +61,7 @@ struct blob {
struct blob_policy {
const char *name;
int type;
struct blob_field *value;
const struct blob_field *value;
};
//! Initializes a blob structure. Optionally takes memory area to be copied into the buffer which must represent a valid blob buf.
@ -71,13 +71,17 @@ void blob_free(struct blob *buf);
//! Resets header but does not deallocate any memory.
void blob_reset(struct blob *buf);
//! Resizes the buffer. Can only be used to increase size.
bool blob_resize(struct blob *buf, int newsize);
bool blob_resize(struct blob *buf, uint32_t newsize);
//! Returns pointer to header attribute (the first element) which is also raw buffer
static inline struct blob_field *blob_head(struct blob *self){
return (struct blob_field*)self->buf;
}
static inline const struct blob_field *blob_head_const(const struct blob *self){
return (const struct blob_field*)self->buf;
}
//! returns size of the whole buffer (including header element and padding)
static inline uint32_t blob_size(struct blob *self){ return blob_field_raw_pad_len(blob_head(self)); }
@ -135,7 +139,7 @@ struct blob_field *blob_put_int(struct blob *buf, long long val);
struct blob_field *blob_put_real(struct blob *buf, double value);
//! write a raw attribute into the buffer
struct blob_field *blob_put_attr(struct blob *buf, struct blob_field *attr);
struct blob_field *blob_put_attr(struct blob *buf, const struct blob_field *attr);
//! print out the whole buffer
void blob_dump(struct blob *self);

View file

@ -15,7 +15,6 @@ static const int blob_type_minlen[BLOB_FIELD_LAST] = {
struct blob_name_hdr *hdr = (struct blob_name_hdr*)attr->data;
return (char*)hdr->name;
}*/
void blob_field_fill_pad(struct blob_field *attr) {
if(!attr) return;
char *buf = (char *) attr;
@ -26,14 +25,13 @@ void blob_field_fill_pad(struct blob_field *attr) {
memset(buf + len - delta, 0, delta);
}
void blob_field_set_raw_len(struct blob_field *attr, unsigned int len){
void blob_field_set_raw_len(struct blob_field *attr, uint32_t len){
if(!attr) return;
if(len < sizeof(struct blob_field)) len = sizeof(struct blob_field);
len &= BLOB_FIELD_LEN_MASK;
attr->id_len &= ~cpu_to_be32(BLOB_FIELD_LEN_MASK);
attr->id_len |= cpu_to_be32(len);
}
/*
bool blob_field_check_type(const void *ptr, unsigned int len, int type){
const char *data = ptr;
@ -74,9 +72,9 @@ blob_field_equal(const struct blob_field *a1, const struct blob_field *a2){
}
//! returns the data of the attribute
void *blob_field_data(const struct blob_field *attr){
const void *blob_field_data(const struct blob_field *attr){
if(!attr) return NULL;
return (void *) attr->data;
return (const void *) attr->data;
}
/*
@ -94,10 +92,9 @@ struct blob_field *blob_field_copy(struct blob_field *attr){
}
*/
uint8_t
blob_field_get_u8(const struct blob_field *attr){
static uint8_t blob_field_get_u8(const struct blob_field *attr){
assert(attr);
return *((uint8_t *) attr->data);
return *((const uint8_t *) attr->data);
}
/*
void blob_field_set_u8(const struct blob_field *attr, uint8_t val){
@ -105,10 +102,9 @@ void blob_field_set_u8(const struct blob_field *attr, uint8_t val){
*((uint8_t *) attr->data) = val;
}
*/
uint16_t
blob_field_get_u16(const struct blob_field *attr){
static uint16_t blob_field_get_u16(const struct blob_field *attr){
assert(attr);
uint16_t *tmp = (uint16_t*)attr->data;
const uint16_t *tmp = (const uint16_t*)attr->data;
return be16_to_cpu(*tmp);
}
/*
@ -119,10 +115,9 @@ blob_field_set_u16(const struct blob_field *attr, uint16_t val){
*tmp = cpu_to_be16(val);
}
*/
uint32_t
blob_field_get_u32(const struct blob_field *attr){
static uint32_t blob_field_get_u32(const struct blob_field *attr){
assert(attr);
uint32_t *tmp = (uint32_t*)attr->data;
const uint32_t *tmp = (const uint32_t*)attr->data;
return be32_to_cpu(*tmp);
}
/*
@ -133,17 +128,15 @@ blob_field_set_u32(const struct blob_field *attr, uint32_t val){
*tmp = cpu_to_be32(val);
}
*/
uint64_t
blob_field_get_u64(const struct blob_field *attr){
static uint64_t blob_field_get_u64(const struct blob_field *attr){
assert(attr);
uint32_t *ptr = (uint32_t *) blob_field_data(attr);
const uint32_t *ptr = (const uint32_t *) blob_field_data(attr);
uint64_t tmp = ((uint64_t) be32_to_cpu(ptr[0])) << 32;
tmp |= be32_to_cpu(ptr[1]);
return tmp;
}
int8_t
blob_field_get_i8(const struct blob_field *attr){
static int8_t blob_field_get_i8(const struct blob_field *attr){
assert(attr);
return blob_field_get_u8(attr);
}
@ -154,8 +147,7 @@ blob_field_set_i8(const struct blob_field *attr, int8_t val){
blob_field_set_u8(attr, val);
}
*/
int16_t
blob_field_get_i16(const struct blob_field *attr){
static int16_t blob_field_get_i16(const struct blob_field *attr){
assert(attr);
return blob_field_get_u16(attr);
}
@ -166,8 +158,7 @@ blob_field_set_i16(const struct blob_field *attr, int16_t val){
blob_field_set_u16(attr, val);
}
*/
int32_t
blob_field_get_i32(const struct blob_field *attr){
static int32_t blob_field_get_i32(const struct blob_field *attr){
assert(attr);
return blob_field_get_u32(attr);
}
@ -178,18 +169,17 @@ blob_field_set_i32(const struct blob_field *attr, int32_t val){
blob_field_set_u32(attr, val);
}
*/
int64_t
blob_field_get_i64(const struct blob_field *attr){
static int64_t blob_field_get_i64(const struct blob_field *attr){
assert(attr);
return blob_field_get_u64(attr);
}
float blob_field_get_f32(const struct blob_field *attr){
static float blob_field_get_f32(const struct blob_field *attr){
assert(attr);
return unpack754_32(blob_field_get_u32(attr));
}
double blob_field_get_f64(const struct blob_field *attr){
static double blob_field_get_f64(const struct blob_field *attr){
assert(attr);
return unpack754_64(blob_field_get_u64(attr));
}
@ -255,7 +245,7 @@ size_t blob_field_get_raw(const struct blob_field *attr, uint8_t *data, size_t d
*/
//! returns the type of the attribute
unsigned int blob_field_type(const struct blob_field *attr){
uint8_t blob_field_type(const struct blob_field *attr){
if(!attr) return BLOB_FIELD_INVALID;
int id = (be32_to_cpu(attr->id_len) & BLOB_FIELD_ID_MASK) >> BLOB_FIELD_ID_SHIFT;
return id;
@ -291,24 +281,24 @@ blob_field_raw_pad_len(const struct blob_field *attr){
return len;
}
struct blob_field *blob_field_first_child(const struct blob_field *self){
const struct blob_field *blob_field_first_child(const struct blob_field *self){
if(!self) return NULL;
if(blob_field_raw_len(self) <= sizeof(struct blob_field)) return NULL;
return (struct blob_field*)blob_field_data(self);
return (const struct blob_field*)blob_field_data(self);
}
struct blob_field *blob_field_next_child(const struct blob_field *self, const struct blob_field *child){
const struct blob_field *blob_field_next_child(const struct blob_field *self, const struct blob_field *child){
if(!child) return NULL;
struct blob_field *ret = (struct blob_field *) ((char *) child + blob_field_raw_pad_len(child));
const struct blob_field *ret = (const struct blob_field *) ((const char *) child + blob_field_raw_pad_len(child));
// check if we are still within bounds
size_t offset = (char*)ret - (char*)self;
size_t offset = (const char*)ret - (const char*)self;
if(offset >= blob_field_raw_pad_len(self)) return NULL;
return ret;
}
bool _blob_field_validate(struct blob_field *attr, const char *signature, const char **nk){
static bool _blob_field_validate(const struct blob_field *attr, const char *signature, const char **nk){
const char *k = signature;
//printf("validating %s\n", signature);
struct blob_field *field = blob_field_first_child(attr);
const struct blob_field *field = blob_field_first_child(attr);
if(!field) return false; // correctly handle empty message!
while(*k && field){
//printf("KEY: %c\n", *k);
@ -380,16 +370,16 @@ bool _blob_field_validate(struct blob_field *attr, const char *signature, const
return true;
}
bool blob_field_validate(struct blob_field *attr, const char *signature){
bool blob_field_validate(const struct blob_field *attr, const char *signature){
if(!attr) return false;
return _blob_field_validate(attr, signature, NULL);
}
bool blob_field_parse(struct blob_field *attr, const char *signature, struct blob_field **out, int out_size){
bool blob_field_parse(const struct blob_field *attr, const char *signature, const struct blob_field **out, int out_size){
if(!attr) return false;
memset(out, 0, sizeof(struct blob_field*) * out_size);
if(!blob_field_validate(attr, signature)) return false;
for(struct blob_field *a = blob_field_first_child(attr); a && out_size; a = blob_field_next_child(attr, a)){
for(const struct blob_field *a = blob_field_first_child(attr); a && out_size; a = blob_field_next_child(attr, a)){
*out = a;
out++;
out_size--;
@ -397,11 +387,11 @@ bool blob_field_parse(struct blob_field *attr, const char *signature, struct blo
return true;
}
bool blob_field_parse_values(struct blob_field *attr, struct blob_policy *policy, int policy_size){
bool blob_field_parse_values(const struct blob_field *attr, struct blob_policy *policy, int policy_size){
if(!attr) return false;
bool valid = true;
if(blob_field_type(attr) == BLOB_FIELD_TABLE){
struct blob_field *key, *value;
const struct blob_field *key, *value;
int processed = 0;
blob_field_for_each_kv(attr, key, value){
if(processed == policy_size) return false;
@ -416,7 +406,7 @@ bool blob_field_parse_values(struct blob_field *attr, struct blob_policy *policy
}
}
} else if(blob_field_type(attr) == BLOB_FIELD_ARRAY){
struct blob_field *child;
const struct blob_field *child;
int pidx = 0;
blob_field_for_each_child(attr, child){
if(!policy_size) return false;

View file

@ -17,18 +17,19 @@ struct blob_field {
struct blob_field_policy {
const char *name;
int type;
uint8_t type;
};
//! returns the data of the attribute
void *blob_field_data(const struct blob_field *attr);
const void *blob_field_data(const struct blob_field *attr);
//! returns the type of the attribute
unsigned int blob_field_type(const struct blob_field *attr);
uint8_t blob_field_type(const struct blob_field *attr);
void blob_field_set_type(struct blob_field *self, int type);
//! returns full length of attribute
unsigned int blob_field_raw_len(const struct blob_field *attr);
void blob_field_set_raw_len(struct blob_field *attr, uint32_t raw_len);
//! includes length of data of the attribute
unsigned int blob_field_data_len(const struct blob_field *attr);
@ -64,23 +65,23 @@ float blob_field_get_float(const struct blob_field *attr);
double blob_field_get_double(const struct blob_field *attr);
*/
//extern void blob_field_fill_pad(struct blob_field *attr);
void blob_field_fill_pad(struct blob_field *attr);
//extern void blob_field_set_raw_len(struct blob_field *attr, unsigned int len);
bool blob_field_equal(const struct blob_field *a1, const struct blob_field *a2);
struct blob_field *blob_field_copy(struct blob_field *attr);
//bool blob_field_check_type(const void *ptr, unsigned int len, int type);
struct blob_field *blob_field_first_child(const struct blob_field *self);
struct blob_field *blob_field_next_child(const struct blob_field *self, const struct blob_field *child);
const struct blob_field *blob_field_first_child(const struct blob_field *self);
const struct blob_field *blob_field_next_child(const struct blob_field *self, const struct blob_field *child);
void blob_field_dump(struct blob_field *self);
void blob_field_dump(const struct blob_field *self);
bool blob_field_validate(struct blob_field *attr, const char *signature);
bool blob_field_validate(const struct blob_field *attr, const char *signature);
bool blob_field_parse(struct blob_field *attr, const char *signature, struct blob_field **out, int out_size);
bool blob_field_parse(const struct blob_field *attr, const char *signature, const struct blob_field **out, int out_size);
struct blob_policy;
bool blob_field_parse_values(struct blob_field *attr, struct blob_policy *policy, int policy_size);
bool blob_field_parse_values(const struct blob_field *attr, struct blob_policy *policy, int policy_size);
#define blob_field_for_each_kv(attr, key, value) \
for(key = blob_field_first_child(attr), value = blob_field_next_child(attr, key); \

View file

@ -19,7 +19,7 @@
//#include <json-c/json.h>
typedef const char *(*blob_json_format_t)(void *priv, struct blob_field *attr);
typedef const char *(*blob_json_format_t)(void *priv, const struct blob_field *attr);
/*
bool blob_put_json_object(struct blob *b, json_object *obj)
{
@ -152,9 +152,9 @@ static void blob_format_string(struct strbuf *s, const char *str)
const unsigned char *p, *last, *end;
char buf[8] = "\\u00";
end = (unsigned char *) str + strlen(str);
end = (const unsigned char *) str + strlen(str);
blob_puts(s, "\"", 1);
for (p = (unsigned char *) str, last = p; *p; p++) {
for (p = (const unsigned char *) str, last = p; *p; p++) {
char escape = '\0';
int len;
@ -186,7 +186,7 @@ static void blob_format_string(struct strbuf *s, const char *str)
continue;
if (p > last)
blob_puts(s, (char *) last, p - last);
blob_puts(s, (const char *) last, p - last);
last = p + 1;
buf[1] = escape;
@ -199,17 +199,17 @@ static void blob_format_string(struct strbuf *s, const char *str)
blob_puts(s, buf, len);
}
blob_puts(s, (char *) last, end - last);
blob_puts(s, (const char *) last, end - last);
blob_puts(s, "\"", 1);
}
static void blob_format_json_list(struct strbuf *s, struct blob_field *attr, bool array);
static void blob_format_json_list(struct strbuf *s, const struct blob_field *attr, bool array);
static void blob_format_element(struct strbuf *s, struct blob_field *attr, bool array, bool head)
static void blob_format_element(struct strbuf *s, const struct blob_field *attr, bool array, bool head)
{
const char *data_str;
char buf[32];
void *data;
const void *data;
//if (!blob_check_attr(attr, false))
// return;
@ -233,22 +233,22 @@ static void blob_format_element(struct strbuf *s, struct blob_field *attr, bool
// sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false");
// break;
case BLOB_FIELD_INT8:
sprintf(buf, "%d", *(uint8_t*)data);
sprintf(buf, "%d", *(const uint8_t*)data);
break;
case BLOB_FIELD_INT16:
sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data));
sprintf(buf, "%d", be16_to_cpu(*(const uint16_t *)data));
break;
case BLOB_FIELD_INT32:
sprintf(buf, "%d", (int) be32_to_cpu(*(uint32_t *)data));
sprintf(buf, "%d", (int) be32_to_cpu(*(const uint32_t *)data));
break;
case BLOB_FIELD_INT64:
sprintf(buf, "%lld", (long long int) be64_to_cpu(*(uint64_t *)data));
sprintf(buf, "%lld", (long long int) be64_to_cpu(*(const uint64_t *)data));
break;
case BLOB_FIELD_FLOAT32:
sprintf(buf, "%f", (double) unpack754_32(be32_to_cpu(*(uint32_t*)data)));
sprintf(buf, "%f", (double) unpack754_32(be32_to_cpu(*(const uint32_t*)data)));
break;
case BLOB_FIELD_FLOAT64:
sprintf(buf, "%Le", unpack754_64(be64_to_cpu(*(uint64_t*)data)));
sprintf(buf, "%Le", unpack754_64(be64_to_cpu(*(const uint64_t*)data)));
break;
case BLOB_FIELD_STRING:
blob_format_string(s, blob_field_data(attr));
@ -265,8 +265,8 @@ out:
blob_puts(s, data_str, strlen(data_str));
}
static void blob_format_json_list(struct strbuf *s, struct blob_field *attr, bool array){
struct blob_field *pos;
static void blob_format_json_list(struct strbuf *s, const struct blob_field *attr, bool array){
const struct blob_field *pos;
bool first = true;
blob_puts(s, (array ? "[" : "{" ), 1);
@ -293,7 +293,7 @@ static void blob_format_json_list(struct strbuf *s, struct blob_field *attr, boo
blob_puts(s, (array ? "]" : "}"), 1);
}
char *blob_format_json_with_cb(struct blob_field *attr, bool list, blob_json_format_t cb, void *priv, int indent)
static char *blob_format_json_with_cb(const struct blob_field *attr, bool list, blob_json_format_t cb, void *priv, int indent)
{
struct strbuf s;
bool array;
@ -328,15 +328,15 @@ char *blob_format_json_with_cb(struct blob_field *attr, bool list, blob_json_for
return s.buf;
}
char *blob_field_to_json(struct blob_field *attr){
char *blob_field_to_json(const struct blob_field *attr){
return blob_format_json_with_cb(attr, false, NULL, NULL, -1);
}
char *blob_field_to_json_pretty(struct blob_field *attr){
static char *blob_field_to_json_pretty(const struct blob_field *attr){
return blob_format_json_with_cb(attr, false, NULL, NULL, 1);
}
static void _blob_field_dump_json(struct blob_field *self, int indent){
static void _blob_field_dump_json(const struct blob_field *self, int indent){
assert(self);
char *json = NULL;
if(indent == 0)
@ -347,12 +347,12 @@ static void _blob_field_dump_json(struct blob_field *self, int indent){
free(json);
}
void blob_field_dump_json(struct blob_field *self){
void blob_field_dump_json(const struct blob_field *self){
assert(self);
_blob_field_dump_json(self, 0);
}
void blob_field_dump_json_pretty(struct blob_field *self){
void blob_field_dump_json_pretty(const struct blob_field *self){
assert(self);
_blob_field_dump_json(self, 1);
}

View file

@ -29,13 +29,13 @@ char *blob_format_json(struct blob_field *attr, bool list);
char *blob_format_json_indent(struct blob_field *attr, bool list, int indent);
*/
void blob_field_dump_json(struct blob_field *self);
void blob_field_dump_json_pretty(struct blob_field *self);
void blob_field_dump_json(const struct blob_field *self);
void blob_field_dump_json_pretty(const struct blob_field *self);
static inline void blob_dump_json(struct blob *self){ blob_field_dump_json(blob_head(self)); }
static inline void blob_dump_json(const struct blob *self){ blob_field_dump_json(blob_head_const(self)); }
char *blob_field_to_json(struct blob_field *self);
static inline char *blob_to_json(struct blob *self){ return blob_field_to_json(blob_head(self)); }
char *blob_field_to_json(const struct blob_field *self);
static inline char *blob_to_json(const struct blob *self){ return blob_field_to_json(blob_head_const(self)); }
bool blob_init_from_json(struct blob *self, const char *json);

View file

@ -48,16 +48,16 @@ http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms
#define DEBUG(...) {}
void Object_objectAddKey(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value){
static void Object_objectAddKey(void *prv, JSOBJ obj, JSOBJ name, JSOBJ value){
DEBUG("new key %p %p %p\n", obj, name, value);
//blob_put_string(prv, name);
}
void Object_arrayAddItem(void *prv, JSOBJ obj, JSOBJ value){
static void Object_arrayAddItem(void *prv, JSOBJ obj, JSOBJ value){
DEBUG("new array item \n");
}
JSOBJ Object_newString(void *prv, char *start, char *end){
static JSOBJ Object_newString(void *prv, char *start, char *end){
size_t len = end - start + 1;
char *str = malloc(len);
memset(str, 0, len);
@ -68,47 +68,47 @@ JSOBJ Object_newString(void *prv, char *start, char *end){
return ret;
}
JSOBJ Object_newTrue(void *prv){
static JSOBJ Object_newTrue(void *prv){
DEBUG("new true\n");
return blob_put_int(prv, 1);
}
JSOBJ Object_newFalse(void *prv){
static JSOBJ Object_newFalse(void *prv){
DEBUG("new false\n");
return blob_put_int(prv, 0);
}
JSOBJ Object_newNull(void *prv){
static JSOBJ Object_newNull(void *prv){
DEBUG("new null\n");
return blob_put_int(prv, 0);
}
JSOBJ Object_newObject(void *prv){
static JSOBJ Object_newObject(void *prv){
DEBUG("new object\n");
return (JSOBJ)blob_open_table(prv);
}
JSOBJ Object_newArray(void *prv){
static JSOBJ Object_newArray(void *prv){
DEBUG("new array\n");
return (JSOBJ)blob_open_array(prv);
}
JSOBJ Object_newInteger(void *prv, JSINT32 value){
static JSOBJ Object_newInteger(void *prv, JSINT32 value){
DEBUG("new int\n");
return blob_put_int(prv, value);
}
JSOBJ Object_newLong(void *prv, JSINT64 value){
static JSOBJ Object_newLong(void *prv, JSINT64 value){
DEBUG("new long\n");
return blob_put_int(prv, value);
}
JSOBJ Object_newUnsignedLong(void *prv, JSUINT64 value){
static JSOBJ Object_newUnsignedLong(void *prv, JSUINT64 value){
DEBUG("new ulong\n");
return blob_put_int(prv, value);
}
JSOBJ Object_newDouble(void *prv, double value){
static JSOBJ Object_newDouble(void *prv, double value){
DEBUG("new double\n");
return blob_put_real(prv, value);
}
@ -177,7 +177,7 @@ bool blob_init_from_json(struct blob *self, const char *json){
return false;
}
blob_init(self, 0, 0);
struct blob_field *child;
const struct blob_field *child;
blob_field_for_each_child(blob_field_first_child(blob_head(&b)), child){
blob_put_attr(self, child);
}

View file

@ -315,8 +315,8 @@ typedef struct __JSONObjectDecoder
JSPFN_MALLOC malloc;
JSPFN_FREE free;
JSPFN_REALLOC realloc;
char *errorStr;
char *errorOffset;
const char *errorStr;
const char *errorOffset;
int preciseFloat;
void *prv;
} JSONObjectDecoder;

View file

@ -54,8 +54,8 @@ http://www.opensource.apple.com/source/tcl/tcl-14/tcl/license.terms
struct DecoderState
{
char *start;
char *end;
const char *start;
const char *end;
char *escStart;
char *escEnd;
int escHeap;
@ -71,17 +71,17 @@ typedef JSOBJ (*PFN_DECODER)( struct DecoderState *ds);
static JSOBJ SetError( struct DecoderState *ds, int offset, const char *message)
{
ds->dec->errorOffset = ds->start + offset;
ds->dec->errorStr = (char *) message;
ds->dec->errorStr = (const char *) message;
return NULL;
}
double createDouble(double intNeg, double intValue, double frcValue, int frcDecimalCount)
static double createDouble(double intNeg, double intValue, double frcValue, int frcDecimalCount)
{
static const double g_pow10[] = {1.0, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001,0.0000001, 0.00000001, 0.000000001, 0.0000000001, 0.00000000001, 0.000000000001, 0.0000000000001, 0.00000000000001, 0.000000000000001};
return (intValue + (frcValue * g_pow10[frcDecimalCount])) * intNeg;
}
JSOBJ decodePreciseFloat(struct DecoderState *ds)
static JSOBJ decodePreciseFloat(struct DecoderState *ds)
{
char *end;
double value;
@ -98,7 +98,7 @@ JSOBJ decodePreciseFloat(struct DecoderState *ds)
return ds->dec->newDouble(ds->prv, value);
}
JSOBJ decode_numeric (struct DecoderState *ds)
static JSOBJ decode_numeric (struct DecoderState *ds)
{
int intNeg = 1;
int mantSize = 0;
@ -109,7 +109,7 @@ JSOBJ decode_numeric (struct DecoderState *ds)
double frcValue = 0.0;
double expNeg;
double expValue;
char *offset = ds->start;
const char *offset = ds->start;
JSUINT64 overflowLimit = LLONG_MAX;
@ -308,9 +308,9 @@ BREAK_EXP_LOOP:
return ds->dec->newDouble (ds->prv, createDouble( (double) intNeg, (double) intValue , frcValue, decimalCount) * pow(10.0, expValue * expNeg));
}
JSOBJ decode_true ( struct DecoderState *ds)
static JSOBJ decode_true ( struct DecoderState *ds)
{
char *offset = ds->start;
const char *offset = ds->start;
offset ++;
if (*(offset++) != 'r')
@ -328,9 +328,9 @@ SETERROR:
return SetError(ds, -1, "Unexpected character found when decoding 'true'");
}
JSOBJ decode_false ( struct DecoderState *ds)
static JSOBJ decode_false ( struct DecoderState *ds)
{
char *offset = ds->start;
const char *offset = ds->start;
offset ++;
if (*(offset++) != 'a')
@ -350,9 +350,9 @@ SETERROR:
return SetError(ds, -1, "Unexpected character found when decoding 'false'");
}
JSOBJ decode_null ( struct DecoderState *ds)
static JSOBJ decode_null ( struct DecoderState *ds)
{
char *offset = ds->start;
const char *offset = ds->start;
offset ++;
if (*(offset++) != 'u')
@ -370,9 +370,9 @@ SETERROR:
return SetError(ds, -1, "Unexpected character found when decoding 'null'");
}
void SkipWhitespace(struct DecoderState *ds)
static void SkipWhitespace(struct DecoderState *ds)
{
char *offset = ds->start;
const char *offset = ds->start;
for (;;)
{
@ -421,7 +421,7 @@ static const JSUINT8 g_decoderLookup[256] =
/* 0xf0 */ 4, 4, 4, 4, 4, 4, 4, 4, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR, DS_UTFLENERROR,
};
JSOBJ decode_string ( struct DecoderState *ds)
static JSOBJ decode_string ( struct DecoderState *ds)
{
JSUTF16 sur[2] = { 0 };
int iSur = 0;
@ -429,9 +429,9 @@ JSOBJ decode_string ( struct DecoderState *ds)
char *escOffset;
char *escStart;
size_t escLen = (ds->escEnd - ds->escStart);
JSUINT8 *inputOffset;
const JSUINT8 *inputOffset;
JSUINT8 oct;
JSUTF32 ucs;
//JSUTF32 ucs;
ds->lastType = JT_INVALID;
ds->start ++;
@ -473,7 +473,7 @@ JSOBJ decode_string ( struct DecoderState *ds)
}
escOffset = ds->escStart;
inputOffset = (JSUINT8 *) ds->start;
inputOffset = (const JSUINT8 *) ds->start;
for (;;)
{
@ -487,7 +487,7 @@ JSOBJ decode_string ( struct DecoderState *ds)
{
ds->lastType = JT_UTF8;
inputOffset ++;
ds->start += ( (char *) inputOffset - (ds->start));
ds->start += ( (const char *) inputOffset - (ds->start));
return ds->dec->newString(ds->prv, ds->escStart, escOffset);
}
case DS_UTFLENERROR:
@ -509,10 +509,9 @@ JSOBJ decode_string ( struct DecoderState *ds)
case 'u':
{
int index;
inputOffset ++;
for (index = 0; index < 4; index ++)
for (int i = 0; i < 4; i ++)
{
switch (*inputOffset)
{
@ -596,7 +595,7 @@ JSOBJ decode_string ( struct DecoderState *ds)
case 2:
{
ucs = (*inputOffset++) & 0x1f;
JSUTF32 ucs = (*inputOffset++) & 0x1f;
ucs <<= 6;
if (((*inputOffset) & 0x80) != 0x80)
{
@ -613,7 +612,7 @@ JSOBJ decode_string ( struct DecoderState *ds)
JSUTF32 ucs = 0;
ucs |= (*inputOffset++) & 0x0f;
for (index = 0; index < 2; index ++)
for (int i = 0; i < 2; i ++)
{
ucs <<= 6;
oct = (*inputOffset++);
@ -671,7 +670,7 @@ JSOBJ decode_string ( struct DecoderState *ds)
}
}
JSOBJ decode_array(struct DecoderState *ds)
static JSOBJ decode_array(struct DecoderState *ds)
{
JSOBJ itemValue;
JSOBJ newObj;
@ -734,7 +733,7 @@ JSOBJ decode_array(struct DecoderState *ds)
}
}
JSOBJ decode_object( struct DecoderState *ds)
static JSOBJ decode_object( struct DecoderState *ds)
{
JSOBJ itemName;
JSOBJ itemValue;
@ -868,7 +867,7 @@ JSOBJ JSON_DecodeObject(JSONObjectDecoder *dec, const char *buffer, size_t cbBuf
char escBuffer[(JSON_MAX_STACK_BUFFER_SIZE / sizeof(char))];
JSOBJ ret;
ds.start = (char *) buffer;
ds.start = (const char *) buffer;
ds.end = ds.start + cbBuffer;
ds.escStart = escBuffer;

View file

@ -116,7 +116,7 @@ static void SetError (JSOBJ obj, JSONObjectEncoder *enc, const char *message)
/*
FIXME: Keep track of how big these get across several encoder calls and try to make an estimate
That way we won't run our head into the wall each call */
void Buffer_Realloc (JSONObjectEncoder *enc, size_t cbNeeded)
static void Buffer_Realloc (JSONObjectEncoder *enc, size_t cbNeeded)
{
size_t curSize = enc->end - enc->start;
size_t newSize = curSize * 2;
@ -152,14 +152,14 @@ void Buffer_Realloc (JSONObjectEncoder *enc, size_t cbNeeded)
enc->end = enc->start + newSize;
}
void FASTCALL_MSVC Buffer_AppendShortHexUnchecked (char *outputOffset, unsigned short value){
static void FASTCALL_MSVC Buffer_AppendShortHexUnchecked (char *outputOffset, unsigned short value){
*(outputOffset++) = g_hexChars[(value & 0xf000) >> 12];
*(outputOffset++) = g_hexChars[(value & 0x0f00) >> 8];
*(outputOffset++) = g_hexChars[(value & 0x00f0) >> 4];
*(outputOffset++) = g_hexChars[(value & 0x000f) >> 0];
}
int Buffer_EscapeStringUnvalidated (JSONObjectEncoder *enc, const char *io, const char *end)
static int Buffer_EscapeStringUnvalidated (JSONObjectEncoder *enc, const char *io, const char *end)
{
char *of = (char *) enc->offset;
@ -262,7 +262,7 @@ int Buffer_EscapeStringUnvalidated (JSONObjectEncoder *enc, const char *io, cons
}
}
int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char *io, const char *end)
static int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char *io, const char *end)
{
JSUTF32 ucs;
char *of = (char *) enc->offset;
@ -440,8 +440,8 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
{
if (enc->escapeForwardSlashes)
{
*(of++) = *( (char *) (g_escapeChars + utflen + 0));
*(of++) = *( (char *) (g_escapeChars + utflen + 1));
*(of++) = *( (const char *) (g_escapeChars + utflen + 0));
*(of++) = *( (const char *) (g_escapeChars + utflen + 1));
io ++;
}
else
@ -500,12 +500,12 @@ static void strreverse(char* begin, char* end){
aux = *end, *end-- = *begin, *begin++ = aux;
}
void Buffer_AppendIndentNewlineUnchecked(JSONObjectEncoder *enc)
static void Buffer_AppendIndentNewlineUnchecked(JSONObjectEncoder *enc)
{
if (enc->indent > 0) Buffer_AppendCharUnchecked(enc, '\n');
}
void Buffer_AppendIndentUnchecked(JSONObjectEncoder *enc, JSINT32 value)
static void Buffer_AppendIndentUnchecked(JSONObjectEncoder *enc, JSINT32 value)
{
int i;
if (enc->indent > 0)
@ -514,7 +514,7 @@ void Buffer_AppendIndentUnchecked(JSONObjectEncoder *enc, JSINT32 value)
Buffer_AppendCharUnchecked(enc, ' ');
}
void Buffer_AppendIntUnchecked(JSONObjectEncoder *enc, JSINT32 value)
static void Buffer_AppendIntUnchecked(JSONObjectEncoder *enc, JSINT32 value)
{
char* wstr;
JSUINT32 uvalue = (value < 0) ? -value : value;
@ -530,7 +530,7 @@ void Buffer_AppendIntUnchecked(JSONObjectEncoder *enc, JSINT32 value)
enc->offset += (wstr - (enc->offset));
}
void Buffer_AppendLongUnchecked(JSONObjectEncoder *enc, JSINT64 value)
static void Buffer_AppendLongUnchecked(JSONObjectEncoder *enc, JSINT64 value)
{
char* wstr;
JSUINT64 uvalue = (value < 0) ? -value : value;
@ -546,7 +546,7 @@ void Buffer_AppendLongUnchecked(JSONObjectEncoder *enc, JSINT64 value)
enc->offset += (wstr - (enc->offset));
}
void Buffer_AppendUnsignedLongUnchecked(JSONObjectEncoder *enc, JSUINT64 value)
static void Buffer_AppendUnsignedLongUnchecked(JSONObjectEncoder *enc, JSUINT64 value)
{
char* wstr;
JSUINT64 uvalue = value;
@ -561,7 +561,7 @@ void Buffer_AppendUnsignedLongUnchecked(JSONObjectEncoder *enc, JSUINT64 value)
enc->offset += (wstr - (enc->offset));
}
int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc, double value)
static int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc, double value)
{
/* if input is larger than thres_max, revert to exponential */
const double thres_max = (double) 1e16 - 1;
@ -709,7 +709,7 @@ Handle integration functions returning NULL here */
FIXME:
Perhaps implement recursion detection */
void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
static void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
{
const char *value;
char *objName;