summaryrefslogtreecommitdiff
path: root/c/object.c
blob: 7a41064093f7e9ad9d7aaf49e93ed0ea92dda7c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * MessagePack for C dynamic typing routine
 *
 * Copyright (C) 2008-2009 FURUHASHI Sadayuki
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
#include "msgpack/unpack.h"
#include "msgpack/unpack_define.h"
#include "msgpack/object.h"

typedef struct {
	msgpack_zone* z;
	bool referenced;
	bool failed;
} unpack_user;

#define msgpack_unpack_struct(name) \
	struct msgpack_unpacker ## name

#define msgpack_unpack_func(ret, name) \
	ret msgpack_unpacker ## name

#define msgpack_unpack_callback(name) \
	msgpack_unpack ## name

#define msgpack_unpack_object msgpack_object

#define msgpack_unpack_user unpack_user


struct msgpack_unpacker_context;

static void msgpack_unpacker_init(struct msgpack_unpacker_context* ctx);

static msgpack_object msgpack_unpacker_data(struct msgpack_unpacker_context* ctx);

static int msgpack_unpacker_execute(struct msgpack_unpacker_context* ctx,
		const char* data, size_t len, size_t* off);

static inline msgpack_object msgpack_unpack_init(unpack_user* u)
{ msgpack_object o; return o; }

static inline msgpack_object msgpack_unpack_uint8(unpack_user* u, uint8_t d)
{ msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }

static inline msgpack_object msgpack_unpack_uint16(unpack_user* u, uint16_t d)
{ msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }

static inline msgpack_object msgpack_unpack_uint32(unpack_user* u, uint32_t d)
{ msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }

static inline msgpack_object msgpack_unpack_uint64(unpack_user* u, uint64_t d)
{ msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }

static inline msgpack_object msgpack_unpack_int8(unpack_user* u, int8_t d)
{ if(d >= 0) { msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }
		else { msgpack_object o; o.type = MSGPACK_OBJECT_NEGATIVE_INTEGER; o.via.i64 = d; return o; } }

static inline msgpack_object msgpack_unpack_int16(unpack_user* u, int16_t d)
{ if(d >= 0) { msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }
		else { msgpack_object o; o.type = MSGPACK_OBJECT_NEGATIVE_INTEGER; o.via.i64 = d; return o; } }

static inline msgpack_object msgpack_unpack_int32(unpack_user* u, int32_t d)
{ if(d >= 0) { msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }
		else { msgpack_object o; o.type = MSGPACK_OBJECT_NEGATIVE_INTEGER; o.via.i64 = d; return o; } }

static inline msgpack_object msgpack_unpack_int64(unpack_user* u, int64_t d)
{ if(d >= 0) { msgpack_object o; o.type = MSGPACK_OBJECT_POSITIVE_INTEGER; o.via.u64 = d; return o; }
		else { msgpack_object o; o.type = MSGPACK_OBJECT_NEGATIVE_INTEGER; o.via.i64 = d; return o; } }

static inline msgpack_object msgpack_unpack_float(unpack_user* u, float d)
{ msgpack_object o; o.type = MSGPACK_OBJECT_DOUBLE; o.via.dec = d; return o; }

static inline msgpack_object msgpack_unpack_double(unpack_user* u, double d)
{ msgpack_object o; o.type = MSGPACK_OBJECT_DOUBLE; o.via.dec = d; return o; }

static inline msgpack_object msgpack_unpack_nil(unpack_user* u)
{ msgpack_object o; o.type = MSGPACK_OBJECT_NIL; return o; }

static inline msgpack_object msgpack_unpack_true(unpack_user* u)
{ msgpack_object o; o.type = MSGPACK_OBJECT_BOOLEAN; o.via.boolean = true; return o; }

static inline msgpack_object msgpack_unpack_false(unpack_user* u)
{ msgpack_object o; o.type = MSGPACK_OBJECT_BOOLEAN; o.via.boolean = false; return o; }

static inline msgpack_object msgpack_unpack_array(unpack_user* u, unsigned int n)
{
	msgpack_object o;
	o.type = MSGPACK_OBJECT_ARRAY;
	o.via.container.size = 0;
	o.via.container.ptr = msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
	if(o.via.container.ptr == NULL) { u->failed = true; }
	return o;
}

static inline void msgpack_unpack_array_item(unpack_user* u, msgpack_object* c, msgpack_object o)
{
	if(u->failed) { return; }
	c->via.container.ptr[ c->via.container.size++ ] = o;
}

static inline msgpack_object msgpack_unpack_map(unpack_user* u, unsigned int n)
{
	msgpack_object o;
	o.type = MSGPACK_OBJECT_MAP;
	o.via.container.size = 0;
	o.via.container.ptr = msgpack_zone_malloc(u->z, n*2*sizeof(msgpack_object));
	if(o.via.container.ptr == NULL) { u->failed = true; }
	return o;
}

static inline void msgpack_unpack_map_item(unpack_user* u, msgpack_object* c, msgpack_object k, msgpack_object v)
{
	if(u->failed) { return; }
	c->via.container.ptr[ c->via.container.size   ] = k;
	c->via.container.ptr[ c->via.container.size+1 ] = v;
	++c->via.container.size;
}

static inline msgpack_object msgpack_unpack_raw(unpack_user* u, const char* b, const char* p, unsigned int l)
{
	msgpack_object o;
	o.type = MSGPACK_OBJECT_RAW;
	o.via.ref.ptr = p;
	o.via.ref.size = l;
	u->referenced = true;
	return o;
}

#include "msgpack/unpack_template.h"

msgpack_object_unpack_return
msgpack_object_unpack(const char* data, size_t len, size_t* off,
		msgpack_zone* z, msgpack_object* result)
{
	struct msgpack_unpacker_context ctx;
	msgpack_unpacker_init(&ctx);
	unpack_user u = {z, false, false};
	ctx.user = u;

	size_t noff = (off ? *off : 0);
	int ret = msgpack_unpacker_execute(&ctx, data, len, &noff);
	if(ret < 0 || ctx.user.failed) {
		return MSGPACK_OBJECT_PARSE_ERROR;
	} else if(ret == 0) {
		return MSGPACK_OBJECT_INSUFFICIENT_BYTES;
	}
	*result = msgpack_unpacker_data(&ctx);
	if(off) { *off = noff; }
	if(ret == 0) {
		return MSGPACK_OBJECT_EXTRA_BYTES;
	}
	return MSGPACK_OBJECT_PARSE_SUCCESS;
}