RetroArch/libretro-db/rmsgpack.c

639 lines
15 KiB
C
Raw Normal View History

2015-01-19 21:47:09 +00:00
/*
* An almost complete implementation of msgpack by
* Saggi Mizrahi <ficoos@gmail.com>
*
* TODO:
* - float types
* - ext types
*
* For more information http://msgpack.org/
*/
#include "rmsgpack.h"
#include <stdlib.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include <errno.h>
#include <stdint.h>
#include <string.h>
2015-01-23 04:59:47 +00:00
#include "libretrodb_endian.h"
2015-01-19 21:47:09 +00:00
2015-09-17 08:33:26 +00:00
#define _MPF_FIXMAP 0x80
#define _MPF_MAP16 0xde
#define _MPF_MAP32 0xdf
2015-01-19 21:47:09 +00:00
2015-09-17 08:33:26 +00:00
#define _MPF_FIXARRAY 0x90
#define _MPF_ARRAY16 0xdc
#define _MPF_ARRAY32 0xdd
#define _MPF_FIXSTR 0xa0
#define _MPF_STR8 0xd9
#define _MPF_STR16 0xda
#define _MPF_STR32 0xdb
#define _MPF_BIN8 0xc4
#define _MPF_BIN16 0xc5
#define _MPF_BIN32 0xc6
#define _MPF_FALSE 0xc2
#define _MPF_TRUE 0xc3
#define _MPF_INT8 0xd0
#define _MPF_INT16 0xd1
#define _MPF_INT32 0xd2
#define _MPF_INT64 0xd3
#define _MPF_UINT8 0xcc
#define _MPF_UINT16 0xcd
#define _MPF_UINT32 0xce
#define _MPF_UINT64 0xcf
2015-09-17 08:33:26 +00:00
#define _MPF_NIL 0xc0
static const uint8_t MPF_FIXMAP = _MPF_FIXMAP;
static const uint8_t MPF_MAP16 = _MPF_MAP16;
static const uint8_t MPF_MAP32 = _MPF_MAP32;
static const uint8_t MPF_FIXARRAY = _MPF_FIXARRAY;
static const uint8_t MPF_ARRAY16 = _MPF_ARRAY16;
static const uint8_t MPF_ARRAY32 = _MPF_ARRAY32;
2015-01-19 21:47:09 +00:00
static const uint8_t MPF_FIXSTR = _MPF_FIXSTR;
static const uint8_t MPF_STR8 = _MPF_STR8;
static const uint8_t MPF_STR16 = _MPF_STR16;
static const uint8_t MPF_STR32 = _MPF_STR32;
2015-01-19 21:47:09 +00:00
static const uint8_t MPF_BIN8 = _MPF_BIN8;
static const uint8_t MPF_BIN16 = _MPF_BIN16;
static const uint8_t MPF_BIN32 = _MPF_BIN32;
2015-01-19 21:47:09 +00:00
static const uint8_t MPF_FALSE = _MPF_FALSE;
static const uint8_t MPF_TRUE = _MPF_TRUE;
2015-01-19 21:47:09 +00:00
static const uint8_t MPF_INT8 = _MPF_INT8;
static const uint8_t MPF_INT16 = _MPF_INT16;
static const uint8_t MPF_INT32 = _MPF_INT32;
static const uint8_t MPF_INT64 = _MPF_INT64;
2015-01-19 21:47:09 +00:00
static const uint8_t MPF_UINT8 = _MPF_UINT8;
static const uint8_t MPF_UINT16 = _MPF_UINT16;
static const uint8_t MPF_UINT32 = _MPF_UINT32;
static const uint8_t MPF_UINT64 = _MPF_UINT64;
2015-01-19 21:47:09 +00:00
2015-09-17 08:33:26 +00:00
static const uint8_t MPF_NIL = _MPF_NIL;
2015-01-19 21:47:09 +00:00
int rmsgpack_write_array_header(RFILE *fd, uint32_t size)
2015-01-24 03:04:56 +00:00
{
uint16_t tmp_i16;
uint32_t tmp_i32;
if (size < 16)
{
size = (size | MPF_FIXARRAY);
if (retro_fwrite(fd, &size, sizeof(int8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-24 03:04:56 +00:00
return sizeof(int8_t);
}
else if (size == (uint16_t)size)
{
if (retro_fwrite(fd, &MPF_ARRAY16, sizeof(MPF_ARRAY16)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-24 03:04:56 +00:00
tmp_i16 = httobe16(size);
if (retro_fwrite(fd, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-24 03:04:56 +00:00
return sizeof(int8_t) + sizeof(uint16_t);
}
2015-01-27 03:02:10 +00:00
if (retro_fwrite(fd, &MPF_ARRAY32, sizeof(MPF_ARRAY32)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i32 = httobe32(size);
2015-09-17 08:33:26 +00:00
if (retro_fwrite(fd, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
return sizeof(int8_t) + sizeof(uint32_t);
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-27 03:02:10 +00:00
}
int rmsgpack_write_map_header(RFILE *fd, uint32_t size)
2015-01-27 03:02:10 +00:00
{
uint16_t tmp_i16;
uint32_t tmp_i32;
if (size < 16)
2015-01-24 03:04:56 +00:00
{
2015-01-27 03:02:10 +00:00
size = (size | MPF_FIXMAP);
if (retro_fwrite(fd, &size, sizeof(int8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
return sizeof(int8_t);
}
else if (size < (uint16_t)size)
{
if (retro_fwrite(fd, &MPF_MAP16, sizeof(MPF_MAP16)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i16 = httobe16(size);
if (retro_fwrite(fd, (void *)(&tmp_i16), sizeof(uint16_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
return sizeof(uint8_t) + sizeof(uint16_t);
2015-01-24 03:04:56 +00:00
}
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
tmp_i32 = httobe32(size);
if (retro_fwrite(fd, &MPF_MAP32, sizeof(MPF_MAP32)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
if (retro_fwrite(fd, (void *)(&tmp_i32), sizeof(uint32_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
return sizeof(int8_t) + sizeof(uint32_t);
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-19 21:47:09 +00:00
}
int rmsgpack_write_string(RFILE *fd, const char *s, uint32_t len)
2015-01-27 03:02:10 +00:00
{
uint16_t tmp_i16;
uint32_t tmp_i32;
2015-09-17 08:33:26 +00:00
int8_t fixlen = 0;
int written = sizeof(int8_t);
2015-01-27 03:02:10 +00:00
if (len < 32)
{
fixlen = len | MPF_FIXSTR;
if (retro_fwrite(fd, &fixlen, sizeof(int8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
}
2015-02-01 14:47:02 +00:00
else if (len < (1 << 8))
2015-01-27 03:02:10 +00:00
{
if (retro_fwrite(fd, &MPF_STR8, sizeof(MPF_STR8)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
if (retro_fwrite(fd, &len, sizeof(uint8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint8_t);
}
2015-02-01 14:47:02 +00:00
else if (len < (1 << 16))
2015-01-27 03:02:10 +00:00
{
if (retro_fwrite(fd, &MPF_STR16, sizeof(MPF_STR16)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i16 = httobe16(len);
if (retro_fwrite(fd, &tmp_i16, sizeof(uint16_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint16_t);
}
else
{
if (retro_fwrite(fd, &MPF_STR32, sizeof(MPF_STR32)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i32 = httobe32(len);
if (retro_fwrite(fd, &tmp_i32, sizeof(uint32_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint32_t);
}
if (retro_fwrite(fd, s, len) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += len;
2015-09-17 08:33:26 +00:00
2015-01-27 03:02:10 +00:00
return written;
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-19 21:47:09 +00:00
}
int rmsgpack_write_bin(RFILE *fd, const void *s, uint32_t len)
2015-01-27 03:02:10 +00:00
{
uint16_t tmp_i16;
uint32_t tmp_i32;
int written = sizeof(int8_t);
2015-02-01 14:47:02 +00:00
2015-01-27 03:02:10 +00:00
if (len == (uint8_t)len)
{
if (retro_fwrite(fd, &MPF_BIN8, sizeof(MPF_BIN8)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
if (retro_fwrite(fd, &len, sizeof(uint8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint8_t);
}
else if (len == (uint16_t)len)
{
if (retro_fwrite(fd, &MPF_BIN16, sizeof(MPF_BIN16)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i16 = httobe16(len);
if (retro_fwrite(fd, &tmp_i16, sizeof(uint16_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint16_t);
}
else
{
if (retro_fwrite(fd, &MPF_BIN32, sizeof(MPF_BIN32)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i32 = httobe32(len);
if (retro_fwrite(fd, &tmp_i32, sizeof(uint32_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint32_t);
}
if (retro_fwrite(fd, s, len) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += len;
2015-09-17 08:33:26 +00:00
2015-01-27 03:02:10 +00:00
return 0;
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-19 21:47:09 +00:00
}
int rmsgpack_write_nil(RFILE *fd)
2015-01-27 03:02:10 +00:00
{
if (retro_fwrite(fd, &MPF_NIL, sizeof(MPF_NIL)) == -1)
2015-01-27 03:02:10 +00:00
return -errno;
return sizeof(uint8_t);
2015-01-19 21:47:09 +00:00
}
int rmsgpack_write_bool(RFILE *fd, int value)
2015-01-27 03:02:10 +00:00
{
if (value)
{
if (retro_fwrite(fd, &MPF_TRUE, sizeof(MPF_TRUE)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
}
2015-02-01 14:47:02 +00:00
if (retro_fwrite(fd, &MPF_FALSE, sizeof(MPF_FALSE)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-02-01 14:47:02 +00:00
2015-01-27 03:02:10 +00:00
return sizeof(uint8_t);
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-19 21:47:09 +00:00
}
int rmsgpack_write_int(RFILE *fd, int64_t value)
2015-01-27 03:02:10 +00:00
{
int16_t tmp_i16;
int32_t tmp_i32;
uint8_t tmpval = 0;
int written = sizeof(uint8_t);
if (value >=0 && value < 128)
{
if (retro_fwrite(fd, &value, sizeof(int8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
}
else if (value < 0 && value > -32)
{
tmpval = (value) | 0xe0;
if (retro_fwrite(fd, &tmpval, sizeof(uint8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
}
else if (value == (int8_t)value)
{
if (retro_fwrite(fd, &MPF_INT8, sizeof(MPF_INT8)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
if (retro_fwrite(fd, &value, sizeof(int8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(int8_t);
}
else if (value == (int16_t)value)
{
if (retro_fwrite(fd, &MPF_INT16, sizeof(MPF_INT16)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i16 = httobe16(value);
if (retro_fwrite(fd, &tmp_i16, sizeof(int16_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(int16_t);
}
else if (value == (int32_t)value)
{
if (retro_fwrite(fd, &MPF_INT32, sizeof(MPF_INT32)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i32 = httobe32(value);
if (retro_fwrite(fd, &tmp_i32, sizeof(int32_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(int32_t);
}
else
{
if (retro_fwrite(fd, &MPF_INT64, sizeof(MPF_INT64)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
value = httobe64(value);
if (retro_fwrite(fd, &value, sizeof(int64_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(int64_t);
}
2015-09-17 08:33:26 +00:00
2015-01-27 03:02:10 +00:00
return written;
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-19 21:47:09 +00:00
}
int rmsgpack_write_uint(RFILE *fd, uint64_t value)
2015-01-27 03:02:10 +00:00
{
uint16_t tmp_i16;
uint32_t tmp_i32;
int written = sizeof(uint8_t);
if (value == (uint8_t)value)
{
if (retro_fwrite(fd, &MPF_UINT8, sizeof(MPF_UINT8)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
if (retro_fwrite(fd, &value, sizeof(uint8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint8_t);
}
else if (value == (uint16_t)value)
{
if (retro_fwrite(fd, &MPF_UINT16, sizeof(MPF_UINT16)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i16 = httobe16(value);
if (retro_fwrite(fd, &tmp_i16, sizeof(uint16_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint16_t);
}
else if (value == (uint32_t)value)
{
if (retro_fwrite(fd, &MPF_UINT32, sizeof(MPF_UINT32)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
tmp_i32 = httobe32(value);
if (retro_fwrite(fd, &tmp_i32, sizeof(uint32_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint32_t);
}
else
{
if (retro_fwrite(fd, &MPF_UINT64, sizeof(MPF_UINT64)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
value = httobe64(value);
if (retro_fwrite(fd, &value, sizeof(uint64_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
written += sizeof(uint64_t);
}
return written;
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-19 21:47:09 +00:00
}
static int read_uint(RFILE *fd, uint64_t *out, size_t size)
2015-01-27 03:02:10 +00:00
{
uint64_t tmp;
if (retro_fread(fd, &tmp, size) == -1)
2015-01-27 03:02:10 +00:00
return -errno;
switch (size)
{
case 1:
*out = *(uint8_t *)(&tmp);
break;
case 2:
*out = betoht16(tmp);
break;
case 4:
*out = betoht32(tmp);
break;
case 8:
*out = betoht64(tmp);
break;
}
return 0;
2015-01-19 21:47:09 +00:00
}
static int read_int(RFILE *fd, int64_t *out, size_t size)
2015-01-27 03:02:10 +00:00
{
uint8_t tmp8 = 0;
2015-01-27 03:02:10 +00:00
uint16_t tmp16;
uint32_t tmp32;
uint64_t tmp64;
2015-02-01 14:47:02 +00:00
if (retro_fread(fd, &tmp64, size) == -1)
2015-01-27 03:02:10 +00:00
return -errno;
2015-01-19 21:47:09 +00:00
(void)tmp8;
2015-01-27 03:02:10 +00:00
switch (size)
{
case 1:
*out = *((int8_t *)(&tmp64));
break;
case 2:
tmp16 = betoht16(tmp64);
*out = *((int16_t *)(&tmp16));
break;
case 4:
tmp32 = betoht32(tmp64);
*out = *((int32_t *)(&tmp32));
break;
case 8:
tmp64 = betoht64(tmp64);
*out = *((int64_t *)(&tmp64));
break;
}
return 0;
2015-01-19 21:47:09 +00:00
}
static int read_buff(RFILE *fd, size_t size, char **pbuff, uint64_t *len)
2015-02-01 14:47:02 +00:00
{
uint64_t tmp_len = 0;
if (read_uint(fd, &tmp_len, size) == -1)
return -errno;
2015-02-01 14:47:02 +00:00
2015-09-17 18:10:04 +00:00
*pbuff = (char *)calloc((size_t)(tmp_len + 1), sizeof(char));
2015-02-01 14:47:02 +00:00
2015-09-17 18:10:04 +00:00
if (retro_fread(fd, *pbuff, (size_t)tmp_len) == -1)
{
free(*pbuff);
return -errno;
}
2015-02-01 14:47:02 +00:00
*len = tmp_len;
return 0;
2015-01-19 21:47:09 +00:00
}
static int read_map(RFILE *fd, uint32_t len,
struct rmsgpack_read_callbacks *callbacks, void *data)
2015-02-01 14:47:02 +00:00
{
int rv;
unsigned i;
2015-01-19 21:47:09 +00:00
2015-02-01 14:47:02 +00:00
if (callbacks->read_map_start &&
(rv = callbacks->read_map_start(len, data)) < 0)
return rv;
2015-01-19 21:47:09 +00:00
2015-02-01 14:47:02 +00:00
for (i = 0; i < len; i++)
2015-01-27 03:02:10 +00:00
{
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
2015-02-01 14:47:02 +00:00
return rv;
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
2015-02-01 14:47:02 +00:00
return rv;
}
2015-01-19 21:47:09 +00:00
2015-02-01 14:47:02 +00:00
return 0;
2015-01-19 21:47:09 +00:00
}
static int read_array(RFILE *fd, uint32_t len,
2015-02-01 14:47:02 +00:00
struct rmsgpack_read_callbacks *callbacks, void *data)
2015-01-27 03:02:10 +00:00
{
int rv;
unsigned i;
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
if (callbacks->read_array_start &&
(rv = callbacks->read_array_start(len, data)) < 0)
return rv;
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
for (i = 0; i < len; i++)
{
if ((rv = rmsgpack_read(fd, callbacks, data)) < 0)
2015-01-27 03:02:10 +00:00
return rv;
}
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
return 0;
2015-01-19 21:47:09 +00:00
}
int rmsgpack_read(RFILE *fd,
2015-02-01 14:47:02 +00:00
struct rmsgpack_read_callbacks *callbacks, void *data)
2015-01-27 03:02:10 +00:00
{
int rv;
2015-02-01 14:47:02 +00:00
uint64_t tmp_len = 0;
2015-01-27 03:02:10 +00:00
uint64_t tmp_uint = 0;
2015-02-01 14:47:02 +00:00
int64_t tmp_int = 0;
uint8_t type = 0;
char *buff = NULL;
if (retro_fread(fd, &type, sizeof(uint8_t)) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
if (type < MPF_FIXMAP)
{
if (!callbacks->read_int)
return 0;
return callbacks->read_int(type, data);
}
else if (type < MPF_FIXARRAY)
{
tmp_len = type - MPF_FIXMAP;
2015-09-17 18:10:04 +00:00
return read_map(fd, (uint32_t)tmp_len, callbacks, data);
2015-01-27 03:02:10 +00:00
}
else if (type < MPF_FIXSTR)
{
tmp_len = type - MPF_FIXARRAY;
2015-09-17 18:10:04 +00:00
return read_array(fd, (uint32_t)tmp_len, callbacks, data);
2015-01-27 03:02:10 +00:00
}
else if (type < MPF_NIL)
{
tmp_len = type - MPF_FIXSTR;
2015-09-17 18:10:04 +00:00
buff = (char *)calloc((size_t)(tmp_len + 1), sizeof(char));
2015-01-27 03:02:10 +00:00
if (!buff)
return -ENOMEM;
2015-09-17 18:10:04 +00:00
if (retro_fread(fd, buff, (ssize_t)tmp_len) == -1)
2015-01-27 03:02:10 +00:00
{
free(buff);
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
}
buff[tmp_len] = '\0';
if (!callbacks->read_string)
{
free(buff);
return 0;
}
2015-09-17 18:10:04 +00:00
return callbacks->read_string(buff, (uint32_t)tmp_len, data);
2015-01-27 03:02:10 +00:00
}
else if (type > MPF_MAP32)
{
if (!callbacks->read_int)
return 0;
return callbacks->read_int(type - 0xff - 1, data);
}
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
switch (type)
{
2015-09-17 08:33:26 +00:00
case _MPF_NIL:
2015-01-27 03:02:10 +00:00
if (callbacks->read_nil)
return callbacks->read_nil(data);
break;
case _MPF_FALSE:
2015-01-27 03:02:10 +00:00
if (callbacks->read_bool)
return callbacks->read_bool(0, data);
break;
case _MPF_TRUE:
2015-01-27 03:02:10 +00:00
if (callbacks->read_bool)
return callbacks->read_bool(1, data);
break;
case _MPF_BIN8:
case _MPF_BIN16:
case _MPF_BIN32:
if ((rv = read_buff(fd, 1<<(type - _MPF_BIN8),
2015-02-01 14:47:02 +00:00
&buff, &tmp_len)) < 0)
2015-01-27 03:02:10 +00:00
return rv;
if (callbacks->read_bin)
2015-09-17 18:10:04 +00:00
return callbacks->read_bin(buff, (uint32_t)tmp_len, data);
2015-01-27 03:02:10 +00:00
break;
case _MPF_UINT8:
case _MPF_UINT16:
case _MPF_UINT32:
case _MPF_UINT64:
tmp_len = UINT32_C(1) << (type - _MPF_UINT8);
2015-01-27 03:02:10 +00:00
tmp_uint = 0;
2015-09-17 18:10:04 +00:00
if (read_uint(fd, &tmp_uint, (size_t)tmp_len) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
if (callbacks->read_uint)
return callbacks->read_uint(tmp_uint, data);
break;
case _MPF_INT8:
case _MPF_INT16:
case _MPF_INT32:
case _MPF_INT64:
tmp_len = UINT32_C(1) << (type - _MPF_INT8);
2015-01-27 03:02:10 +00:00
tmp_int = 0;
2015-09-17 18:10:04 +00:00
if (read_int(fd, &tmp_int, (size_t)tmp_len) == -1)
2015-09-17 08:33:26 +00:00
goto error;
2015-01-27 03:02:10 +00:00
if (callbacks->read_int)
return callbacks->read_int(tmp_int, data);
break;
case _MPF_STR8:
case _MPF_STR16:
case _MPF_STR32:
if ((rv = read_buff(fd, 1<<(type - _MPF_STR8), &buff, &tmp_len)) < 0)
2015-01-27 03:02:10 +00:00
return rv;
if (callbacks->read_string)
2015-09-17 18:10:04 +00:00
return callbacks->read_string(buff, (uint32_t)tmp_len, data);
2015-01-27 03:02:10 +00:00
break;
2015-09-17 08:33:26 +00:00
case _MPF_ARRAY16:
case _MPF_ARRAY32:
if (read_uint(fd, &tmp_len, 2<<(type - _MPF_ARRAY16)) == -1)
goto error;
2015-09-17 18:10:04 +00:00
return read_array(fd, (uint32_t)tmp_len, callbacks, data);
2015-09-17 08:33:26 +00:00
case _MPF_MAP16:
case _MPF_MAP32:
if (read_uint(fd, &tmp_len, 2<<(type - _MPF_MAP16)) == -1)
goto error;
2015-09-17 18:10:04 +00:00
return read_map(fd, (uint32_t)tmp_len, callbacks, data);
2015-01-27 03:02:10 +00:00
}
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
return 0;
2015-09-17 08:33:26 +00:00
error:
return -errno;
2015-01-19 21:47:09 +00:00
}