summaryrefslogtreecommitdiff
path: root/cpp/unpack.cpp
blob: 6a6a4e8587aa9ec941eb623e43a5fbc3d1da7f79 (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
168
169
170
171
#include "msgpack/unpack.hpp"
#include "unpack_context.hpp"
#include <stdlib.h>

namespace msgpack {

struct unpacker::context {
	context(zone* z)
	{
		msgpack_unpacker_init(&m_ctx);
		m_ctx.user = z;
	}

	~context() { }

	int execute(const void* data, size_t len, size_t* off)
	{
		return msgpack_unpacker_execute(&m_ctx, (const char*)data, len, off);
	}

	object_class* data()
	{
		return msgpack_unpacker_data(&m_ctx);
	}

	void reset()
	{
		zone* z = m_ctx.user;
		msgpack_unpacker_init(&m_ctx);
		m_ctx.user = z;
	}

	void reset(zone* z)
	{
		msgpack_unpacker_init(&m_ctx);
		m_ctx.user = z;
	}

	zone* user()
	{
		return m_ctx.user;
	}

	void user(zone* z)
	{
		m_ctx.user = z;
	}

private:
	msgpack_unpacker m_ctx;

private:
	context();
	context(const context&);
};


unpacker::unpacker() :
	m_zone(new zone()),
	m_ctx(new context(m_zone)),
	m_buffer(NULL),
	m_used(0),
	m_free(0),
	m_off(0)
{ }


unpacker::~unpacker()
{
	free(m_buffer);
	delete m_ctx;
	delete m_zone;
}


void unpacker::expand_buffer(size_t len)
{
	if(m_off == 0) {
		size_t next_size;
		if(m_free != 0) { next_size = m_free * 2; }
		else { next_size = UNPACKER_INITIAL_BUFFER_SIZE; }
		while(next_size < len + m_used) { next_size *= 2; }

		// FIXME realloc?
		void* tmp = malloc(next_size);
		if(!tmp) { throw std::bad_alloc(); }
		memcpy(tmp, m_buffer, m_used);
		free(m_buffer);

		m_buffer = tmp;
		m_free = next_size - m_used;

	} else {
		size_t next_size = UNPACKER_INITIAL_BUFFER_SIZE;
		while(next_size < len + m_used - m_off) { next_size *= 2; }

		void* tmp = malloc(next_size);
		if(!tmp) { throw std::bad_alloc(); }
		memcpy(tmp, ((char*)m_buffer)+m_off, m_used-m_off);

		try {
			m_zone->push_finalizer<void>(&zone::finalize_free, NULL, m_buffer);
		} catch (...) {
			free(tmp);
			throw;
		}

		m_buffer = tmp;
		m_used = m_used - m_off;
		m_free = next_size - m_used;
		m_off = 0;
	}
}

bool unpacker::execute()
{
	int ret = m_ctx->execute(m_buffer, m_used, &m_off);
	if(ret < 0) {
		throw unpack_error("parse error");
	} else if(ret == 0) {
		return false;
	} else {
		expand_buffer(0);
		return true;
	}
}

zone* unpacker::release_zone()
{
	zone* nz = new zone();
	zone* z = m_zone;
	m_zone = nz;
	m_ctx->user(m_zone);
	return z;
}

object unpacker::data()
{
	return object(m_ctx->data());
}

void unpacker::reset()
{
	if(m_off != 0) { expand_buffer(0); }
	if(!m_zone->empty()) {
		delete m_zone;
		m_zone = NULL;
		m_zone = new zone();
	}
	m_ctx->reset();
}


object unpacker::unpack(const void* data, size_t len, zone& z)
{
	context ctx(&z);
	size_t off = 0;
	int ret = ctx.execute(data, len, &off);
	if(ret < 0) {
		throw unpack_error("parse error");
	} else if(ret == 0) {
		throw unpack_error("insufficient bytes");
	} else if(off < len) {
		throw unpack_error("extra bytes");
	}
	return ctx.data();
}


}  // namespace msgpack