msgpack 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/License.txt +13 -0
- data/Manifest.txt +26 -0
- data/README.txt +20 -0
- data/Rakefile +4 -0
- data/config/hoe.rb +75 -0
- data/config/requirements.rb +15 -0
- data/ext/extconf.rb +4 -0
- data/ext/pack.c +131 -0
- data/ext/pack.h +26 -0
- data/ext/pack_inline.h +33 -0
- data/ext/rbinit.c +29 -0
- data/ext/unpack.c +202 -0
- data/ext/unpack.h +26 -0
- data/ext/unpack_context.h +35 -0
- data/ext/unpack_inline.c +81 -0
- data/lib/msgpack/version.rb +9 -0
- data/msgpack/pack/inline_context.h +2 -0
- data/msgpack/pack/inline_impl.h +287 -0
- data/msgpack/unpack/inline_context.h +52 -0
- data/msgpack/unpack/inline_impl.h +438 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/setup.rb +1585 -0
- data/tasks/deployment.rake +34 -0
- data/tasks/environment.rake +7 -0
- metadata +94 -0
data/License.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 2008 Furuhashi Sadayuki
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
License.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.txt
|
4
|
+
Rakefile
|
5
|
+
config/hoe.rb
|
6
|
+
config/requirements.rb
|
7
|
+
ext/extconf.rb
|
8
|
+
ext/pack.c
|
9
|
+
ext/pack.h
|
10
|
+
ext/pack_inline.h
|
11
|
+
ext/rbinit.c
|
12
|
+
ext/unpack.c
|
13
|
+
ext/unpack.h
|
14
|
+
ext/unpack_context.h
|
15
|
+
ext/unpack_inline.c
|
16
|
+
msgpack/pack/inline_context.h
|
17
|
+
msgpack/pack/inline_impl.h
|
18
|
+
msgpack/unpack/inline_context.h
|
19
|
+
msgpack/unpack/inline_impl.h
|
20
|
+
lib/msgpack/version.rb
|
21
|
+
script/console
|
22
|
+
script/destroy
|
23
|
+
script/generate
|
24
|
+
setup.rb
|
25
|
+
tasks/deployment.rake
|
26
|
+
tasks/environment.rake
|
data/README.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
MessagePack
|
2
|
+
-----------
|
3
|
+
Binary-based efficient data interchange format.
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
Copyright (C) 2008 FURUHASHI Sadayuki
|
8
|
+
|
9
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
+
you may not use this file except in compliance with the License.
|
11
|
+
You may obtain a copy of the License at
|
12
|
+
|
13
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
14
|
+
|
15
|
+
Unless required by applicable law or agreed to in writing, software
|
16
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
+
See the License for the specific language governing permissions and
|
19
|
+
limitations under the License.
|
20
|
+
|
data/Rakefile
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'msgpack/version'
|
2
|
+
|
3
|
+
AUTHOR = 'FURUHASHI Sadayuki' # can also be an array of Authors
|
4
|
+
EMAIL = "fr _at_ syuki.skr.jp"
|
5
|
+
DESCRIPTION = "An object-oriented parser generator based on Parser Expression Grammar"
|
6
|
+
GEM_NAME = 'msgpack' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'msgpack' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
EXTRA_DEPENDENCIES = [
|
11
|
+
# ['activesupport', '>= 1.3.1']
|
12
|
+
] # An array of rubygem dependencies [name, version]
|
13
|
+
|
14
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
15
|
+
@config = nil
|
16
|
+
RUBYFORGE_USERNAME = "unknown"
|
17
|
+
def rubyforge_username
|
18
|
+
unless @config
|
19
|
+
begin
|
20
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
21
|
+
rescue
|
22
|
+
puts <<-EOS
|
23
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
24
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
25
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
26
|
+
EOS
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
REV = nil
|
35
|
+
# UNCOMMENT IF REQUIRED:
|
36
|
+
# REV = YAML.load(`svn info`)['Revision']
|
37
|
+
VERS = MessagePack::VERSION::STRING + (REV ? ".#{REV}" : "")
|
38
|
+
RDOC_OPTS = ['--quiet', '--title', 'msgpack documentation',
|
39
|
+
"--opname", "index.html",
|
40
|
+
"--line-numbers",
|
41
|
+
"--main", "README",
|
42
|
+
"--inline-source"]
|
43
|
+
|
44
|
+
class Hoe
|
45
|
+
def extra_deps
|
46
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
47
|
+
@extra_deps
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate all the Rake tasks
|
52
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
53
|
+
$hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
54
|
+
p.developer(AUTHOR, EMAIL)
|
55
|
+
p.description = DESCRIPTION
|
56
|
+
p.summary = DESCRIPTION
|
57
|
+
p.url = HOMEPATH
|
58
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
59
|
+
p.test_globs = ["test/**/test_*.rb"]
|
60
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
61
|
+
|
62
|
+
# == Optional
|
63
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
64
|
+
#p.extra_deps = EXTRA_DEPENDENCIES
|
65
|
+
|
66
|
+
p.spec_extras = { # A hash of extra values to set in the gemspec.
|
67
|
+
:extensions => %w[ext/extconf.rb]
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
72
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
73
|
+
$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
74
|
+
$hoe.rsync_args = '-av --delete --ignore-errors'
|
75
|
+
$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
data/ext/extconf.rb
ADDED
data/ext/pack.c
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack packing routine for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008 FURUHASHI Sadayuki
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#include "pack_inline.h"
|
19
|
+
|
20
|
+
#ifndef RUBY_VM
|
21
|
+
#include "st.h" // ruby hash
|
22
|
+
#endif
|
23
|
+
|
24
|
+
static ID s_to_msgpack;
|
25
|
+
|
26
|
+
#define ARG_BUFFER(name, argc, argv) \
|
27
|
+
VALUE name; \
|
28
|
+
if(argc == 1) { \
|
29
|
+
name = argv[0]; \
|
30
|
+
} else if(argc == 0) { \
|
31
|
+
name = rb_str_buf_new(0); \
|
32
|
+
} else { \
|
33
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); \
|
34
|
+
}
|
35
|
+
|
36
|
+
static VALUE MessagePack_NilClass_to_msgpack(int argc, VALUE *argv, VALUE self)
|
37
|
+
{
|
38
|
+
ARG_BUFFER(out, argc, argv);
|
39
|
+
msgpack_pack_nil(out);
|
40
|
+
return out;
|
41
|
+
}
|
42
|
+
|
43
|
+
static VALUE MessagePack_TrueClass_to_msgpack(int argc, VALUE *argv, VALUE self)
|
44
|
+
{
|
45
|
+
ARG_BUFFER(out, argc, argv);
|
46
|
+
msgpack_pack_true(out);
|
47
|
+
return out;
|
48
|
+
}
|
49
|
+
|
50
|
+
static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self)
|
51
|
+
{
|
52
|
+
ARG_BUFFER(out, argc, argv);
|
53
|
+
msgpack_pack_false(out);
|
54
|
+
return out;
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self)
|
59
|
+
{
|
60
|
+
ARG_BUFFER(out, argc, argv);
|
61
|
+
msgpack_pack_int(out, FIX2INT(self));
|
62
|
+
return out;
|
63
|
+
}
|
64
|
+
|
65
|
+
static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self)
|
66
|
+
{
|
67
|
+
ARG_BUFFER(out, argc, argv);
|
68
|
+
msgpack_pack_double(out, rb_num2dbl(self));
|
69
|
+
return out;
|
70
|
+
}
|
71
|
+
|
72
|
+
static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self)
|
73
|
+
{
|
74
|
+
ARG_BUFFER(out, argc, argv);
|
75
|
+
msgpack_pack_raw(out, RSTRING_PTR(self), RSTRING_LEN(self));
|
76
|
+
return out;
|
77
|
+
}
|
78
|
+
|
79
|
+
static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self)
|
80
|
+
{
|
81
|
+
ARG_BUFFER(out, argc, argv);
|
82
|
+
msgpack_pack_array(out, RARRAY_LEN(self));
|
83
|
+
VALUE* p = RARRAY_PTR(self);
|
84
|
+
VALUE* const pend = p + RARRAY_LEN(self);
|
85
|
+
for(;p != pend; ++p) {
|
86
|
+
rb_funcall(*p, s_to_msgpack, 1, out);
|
87
|
+
}
|
88
|
+
return out;
|
89
|
+
}
|
90
|
+
|
91
|
+
#ifndef RHASH_SIZE // Ruby 1.8
|
92
|
+
#define RHASH_SIZE(h) (RHASH(h)->tbl ? RHASH(h)->tbl->num_entries : 0)
|
93
|
+
#endif
|
94
|
+
|
95
|
+
static int MessagePack_Hash_to_msgpack_foreach(VALUE key, VALUE value, VALUE out)
|
96
|
+
{
|
97
|
+
if (key == Qundef) { return ST_CONTINUE; }
|
98
|
+
rb_funcall(key, s_to_msgpack, 1, out);
|
99
|
+
rb_funcall(value, s_to_msgpack, 1, out);
|
100
|
+
return ST_CONTINUE;
|
101
|
+
}
|
102
|
+
|
103
|
+
static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self)
|
104
|
+
{
|
105
|
+
ARG_BUFFER(out, argc, argv);
|
106
|
+
msgpack_pack_map(out, RHASH_SIZE(self));
|
107
|
+
rb_hash_foreach(self, MessagePack_Hash_to_msgpack_foreach, out);
|
108
|
+
return out;
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
static VALUE MessagePack_pack(VALUE self, VALUE data)
|
113
|
+
{
|
114
|
+
return rb_funcall(data, s_to_msgpack, 0);
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
void Init_msgpack_pack(VALUE mMessagePack)
|
119
|
+
{
|
120
|
+
s_to_msgpack = rb_intern("to_msgpack");
|
121
|
+
rb_define_method_id(rb_cNilClass, s_to_msgpack, MessagePack_NilClass_to_msgpack, -1);
|
122
|
+
rb_define_method_id(rb_cTrueClass, s_to_msgpack, MessagePack_TrueClass_to_msgpack, -1);
|
123
|
+
rb_define_method_id(rb_cFalseClass, s_to_msgpack, MessagePack_FalseClass_to_msgpack, -1);
|
124
|
+
rb_define_method_id(rb_cFixnum, s_to_msgpack, MessagePack_Fixnum_to_msgpack, -1);
|
125
|
+
rb_define_method_id(rb_cFloat, s_to_msgpack, MessagePack_Float_to_msgpack, -1);
|
126
|
+
rb_define_method_id(rb_cString, s_to_msgpack, MessagePack_String_to_msgpack, -1);
|
127
|
+
rb_define_method_id(rb_cArray, s_to_msgpack, MessagePack_Array_to_msgpack, -1);
|
128
|
+
rb_define_method_id(rb_cHash, s_to_msgpack, MessagePack_Hash_to_msgpack, -1);
|
129
|
+
rb_define_module_function(mMessagePack, "pack", MessagePack_pack, 1);
|
130
|
+
}
|
131
|
+
|
data/ext/pack.h
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack packing routine for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008 FURUHASHI Sadayuki
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#ifndef PACK_H__
|
19
|
+
#define PACK_H__
|
20
|
+
|
21
|
+
#include "ruby.h"
|
22
|
+
|
23
|
+
void Init_msgpack_pack(VALUE mMessagePack);
|
24
|
+
|
25
|
+
#endif /* pack.h */
|
26
|
+
|
data/ext/pack_inline.h
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack packing routine for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008 FURUHASHI Sadayuki
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#ifndef PACK_INLINE_H__
|
19
|
+
#define PACK_INLINE_H__
|
20
|
+
|
21
|
+
#include "ruby.h"
|
22
|
+
|
23
|
+
typedef VALUE msgpack_pack_context;
|
24
|
+
|
25
|
+
static inline void msgpack_pack_append_buffer(VALUE x, const unsigned char* b, unsigned int l)
|
26
|
+
{
|
27
|
+
rb_str_buf_cat(x, (const void*)b, l);
|
28
|
+
}
|
29
|
+
|
30
|
+
#include "msgpack/pack/inline_impl.h"
|
31
|
+
|
32
|
+
#endif /* pack_inline.h */
|
33
|
+
|
data/ext/rbinit.c
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008 FURUHASHI Sadayuki
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#include "pack.h"
|
19
|
+
#include "unpack.h"
|
20
|
+
|
21
|
+
static VALUE mMessagePack;
|
22
|
+
|
23
|
+
void Init_msgpack(void)
|
24
|
+
{
|
25
|
+
mMessagePack = rb_define_module("MessagePack");
|
26
|
+
Init_msgpack_unpack(mMessagePack);
|
27
|
+
Init_msgpack_pack(mMessagePack);
|
28
|
+
}
|
29
|
+
|
data/ext/unpack.c
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
/*
|
2
|
+
* MessagePack unpacking routine for Ruby
|
3
|
+
*
|
4
|
+
* Copyright (C) 2008 FURUHASHI Sadayuki
|
5
|
+
*
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
* you may not use this file except in compliance with the License.
|
8
|
+
* You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
* See the License for the specific language governing permissions and
|
16
|
+
* limitations under the License.
|
17
|
+
*/
|
18
|
+
#include "ruby.h"
|
19
|
+
#include "unpack_context.h"
|
20
|
+
#include <stdio.h>
|
21
|
+
|
22
|
+
#define UNPACKER(from, name) \
|
23
|
+
msgpack_unpacker *name = NULL; \
|
24
|
+
Data_Get_Struct(from, msgpack_unpacker, name); \
|
25
|
+
if(name == NULL) { \
|
26
|
+
rb_raise(rb_eArgError, "NULL found for " # name " when shouldn't be."); \
|
27
|
+
}
|
28
|
+
|
29
|
+
#define CHECK_STRING_TYPE(value) \
|
30
|
+
value = rb_check_string_type(value); \
|
31
|
+
if( NIL_P(value) ) { \
|
32
|
+
rb_raise(rb_eTypeError, "instance of String needed"); \
|
33
|
+
}
|
34
|
+
|
35
|
+
static VALUE cUnpacker;
|
36
|
+
static VALUE eUnpackError;
|
37
|
+
|
38
|
+
static void MessagePack_Unpacker_free(void* data)
|
39
|
+
{
|
40
|
+
if(data) { free(data); }
|
41
|
+
}
|
42
|
+
|
43
|
+
static void MessagePack_Unpacker_mark(msgpack_unpacker *mp)
|
44
|
+
{
|
45
|
+
unsigned int i;
|
46
|
+
for(i=0; i < mp->top; ++i) {
|
47
|
+
rb_gc_mark(mp->stack[i].obj);
|
48
|
+
rb_gc_mark(mp->stack[i].tmp.map_key);
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
static VALUE MessagePack_Unpacker_alloc(VALUE klass)
|
53
|
+
{
|
54
|
+
VALUE obj;
|
55
|
+
msgpack_unpacker* mp = ALLOC_N(msgpack_unpacker, 1);
|
56
|
+
obj = Data_Wrap_Struct(klass, MessagePack_Unpacker_mark,
|
57
|
+
MessagePack_Unpacker_free, mp);
|
58
|
+
return obj;
|
59
|
+
}
|
60
|
+
|
61
|
+
static VALUE MessagePack_Unpacker_reset(VALUE self)
|
62
|
+
{
|
63
|
+
UNPACKER(self, mp);
|
64
|
+
mp->user.finished = false;
|
65
|
+
msgpack_unpacker_init(mp);
|
66
|
+
return self;
|
67
|
+
}
|
68
|
+
|
69
|
+
static VALUE MessagePack_Unpacker_initialize(VALUE self)
|
70
|
+
{
|
71
|
+
return MessagePack_Unpacker_reset(self);
|
72
|
+
}
|
73
|
+
|
74
|
+
|
75
|
+
static VALUE MessagePack_Unpacker_execute_impl(VALUE args)
|
76
|
+
{
|
77
|
+
VALUE self = ((VALUE*)args)[0];
|
78
|
+
VALUE data = ((VALUE*)args)[1];
|
79
|
+
VALUE off = ((VALUE*)args)[2];
|
80
|
+
|
81
|
+
UNPACKER(self, mp);
|
82
|
+
size_t from = NUM2UINT(off);
|
83
|
+
char* dptr = RSTRING_PTR(data);
|
84
|
+
long dlen = RSTRING_LEN(data);
|
85
|
+
int ret;
|
86
|
+
|
87
|
+
if(from >= dlen) {
|
88
|
+
rb_raise(eUnpackError, "Requested start is after data buffer end.");
|
89
|
+
}
|
90
|
+
|
91
|
+
ret = msgpack_unpacker_execute(mp, dptr, (size_t)dlen, &from);
|
92
|
+
|
93
|
+
if(ret < 0) {
|
94
|
+
rb_raise(eUnpackError, "Parse error.");
|
95
|
+
} else if(ret > 0) {
|
96
|
+
mp->user.finished = true;
|
97
|
+
return ULONG2NUM(from);
|
98
|
+
} else {
|
99
|
+
mp->user.finished = false;
|
100
|
+
return ULONG2NUM(from);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
static VALUE MessagePack_Unpacker_execute_rescue(VALUE nouse)
|
105
|
+
{
|
106
|
+
rb_gc_enable();
|
107
|
+
#ifdef RUBY_VM
|
108
|
+
rb_exc_raise(rb_errinfo());
|
109
|
+
#else
|
110
|
+
rb_exc_raise(ruby_errinfo);
|
111
|
+
#endif
|
112
|
+
}
|
113
|
+
|
114
|
+
static VALUE MessagePack_Unpacker_execute(VALUE self, VALUE data, VALUE off)
|
115
|
+
{
|
116
|
+
// FIXME execute実行中はmp->topが更新されないのでGC markが機能しない
|
117
|
+
rb_gc_disable();
|
118
|
+
VALUE args[3] = {self, data, off};
|
119
|
+
VALUE ret = rb_rescue(MessagePack_Unpacker_execute_impl, (VALUE)args,
|
120
|
+
MessagePack_Unpacker_execute_rescue, Qnil);
|
121
|
+
rb_gc_enable();
|
122
|
+
return ret;
|
123
|
+
}
|
124
|
+
|
125
|
+
static VALUE MessagePack_Unpacker_finished_p(VALUE self)
|
126
|
+
{
|
127
|
+
UNPACKER(self, mp);
|
128
|
+
if(mp->user.finished) {
|
129
|
+
return Qtrue;
|
130
|
+
}
|
131
|
+
return Qfalse;
|
132
|
+
}
|
133
|
+
|
134
|
+
static VALUE MessagePack_Unpacker_data(VALUE self)
|
135
|
+
{
|
136
|
+
UNPACKER(self, mp);
|
137
|
+
return msgpack_unpacker_data(mp);
|
138
|
+
}
|
139
|
+
|
140
|
+
|
141
|
+
static VALUE MessagePack_unpack_impl(VALUE args)
|
142
|
+
{
|
143
|
+
msgpack_unpacker* mp = (msgpack_unpacker*)((VALUE*)args)[0];
|
144
|
+
VALUE data = ((VALUE*)args)[1];
|
145
|
+
|
146
|
+
size_t from = 0;
|
147
|
+
char* dptr = RSTRING_PTR(data);
|
148
|
+
long dlen = RSTRING_LEN(data);
|
149
|
+
int ret;
|
150
|
+
|
151
|
+
ret = msgpack_unpacker_execute(mp, dptr, (size_t)dlen, &from);
|
152
|
+
|
153
|
+
if(ret < 0) {
|
154
|
+
rb_raise(eUnpackError, "Parse error.");
|
155
|
+
} else if(ret == 0) {
|
156
|
+
rb_raise(eUnpackError, "Insufficient bytes.");
|
157
|
+
} else {
|
158
|
+
if(from < dlen) {
|
159
|
+
rb_raise(eUnpackError, "Extra bytes.");
|
160
|
+
}
|
161
|
+
return msgpack_unpacker_data(mp);
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
static VALUE MessagePack_unpack_rescue(VALUE args)
|
166
|
+
{
|
167
|
+
rb_gc_enable();
|
168
|
+
#ifdef RUBY_VM
|
169
|
+
rb_exc_raise(rb_errinfo());
|
170
|
+
#else
|
171
|
+
rb_exc_raise(ruby_errinfo);
|
172
|
+
#endif
|
173
|
+
}
|
174
|
+
|
175
|
+
static VALUE MessagePack_unpack(VALUE self, VALUE data)
|
176
|
+
{
|
177
|
+
CHECK_STRING_TYPE(data);
|
178
|
+
msgpack_unpacker mp;
|
179
|
+
msgpack_unpacker_init(&mp);
|
180
|
+
rb_gc_disable();
|
181
|
+
VALUE args[2] = {(VALUE)&mp, data};
|
182
|
+
VALUE ret = rb_rescue(MessagePack_unpack_impl, (VALUE)args,
|
183
|
+
MessagePack_unpack_rescue, Qnil);
|
184
|
+
rb_gc_enable();
|
185
|
+
return ret;
|
186
|
+
}
|
187
|
+
|
188
|
+
|
189
|
+
void Init_msgpack_unpack(VALUE mMessagePack)
|
190
|
+
{
|
191
|
+
eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError);
|
192
|
+
cUnpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject);
|
193
|
+
rb_define_alloc_func(cUnpacker, MessagePack_Unpacker_alloc);
|
194
|
+
rb_define_method(cUnpacker, "initialize", MessagePack_Unpacker_initialize, 0);
|
195
|
+
rb_define_method(cUnpacker, "execute", MessagePack_Unpacker_execute, 2);
|
196
|
+
rb_define_method(cUnpacker, "finished?", MessagePack_Unpacker_finished_p, 0);
|
197
|
+
rb_define_method(cUnpacker, "data", MessagePack_Unpacker_data, 0);
|
198
|
+
rb_define_method(cUnpacker, "reset", MessagePack_Unpacker_reset, 0);
|
199
|
+
rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack, 1);
|
200
|
+
}
|
201
|
+
|
202
|
+
|