msgpack-inspect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a5cf0e5a244873a5044ae948a088da716bd90e0
4
+ data.tar.gz: 661802f26ee3610e488309e7598421639fd06b9b
5
+ SHA512:
6
+ metadata.gz: 40ccb02a952f8a28d232dd350a0960df82c7ebfea8acd27a348c8c1240567c6be4ae3fb754ac51f6fc61601d514f7b0127c754830f37aa2a920300065bf35466
7
+ data.tar.gz: 77475e7f3304c92bdb02e8b80720432da64433c1c411deb26ea2f5e476275d7a690b9297c3189d03da3c0333144fbce5834ea2d1cab5f27bc45c2eaf75df0839
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in msgpack-inspect.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # msgpack-inspect
2
+
3
+ This is a command line tool to inspect/show a data serialized by [MessagePack](http://msgpack.org/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'msgpack-inspect'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install msgpack-inspect
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ Usage: msgpack-inspect [options] FILE
25
+
26
+ Options:
27
+
28
+ -f, --format FORMAT output format of inspection result (yaml/json) [default: yaml]
29
+ -r, --require LIB ruby file path to require (to load ext type definitions)
30
+ -h, --help Show this message
31
+ ```
32
+
33
+ FILE is a file which msgpack binary stored. Specify `-` to inspect data from STDIN.
34
+ This command shows the all data contained in specified format (YAML in default).
35
+
36
+ ```
37
+ ---
38
+ - :format: :false
39
+ :header: c2
40
+ :data: c2
41
+ :value: false
42
+ - :format: :true
43
+ :header: c3
44
+ :data: c3
45
+ :value: true
46
+ ```
47
+
48
+ TODO: show more example
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/tafomoris/msgpack-inspect].
53
+
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'fileutils'
4
+ require 'rake/testtask'
5
+ require 'rake/clean'
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ # To run test for only one file (or file path pattern)
9
+ # $ bundle exec rake test TEST=test/test_specified_path.rb
10
+ # $ bundle exec rake test TEST=test/test_*.rb
11
+ t.libs << "test"
12
+ t.test_files = Dir["test/**/test_*.rb"].sort
13
+ t.verbose = true
14
+ t.warning = true
15
+ t.ruby_opts = ["-Eascii-8bit:ascii-8bit"]
16
+ end
17
+
18
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ here = File.dirname(__FILE__)
4
+ $LOAD_PATH << File.expand_path(File.join(here, '..', 'lib'))
5
+ require 'msgpack/inspect/command'
6
+ MessagePack::Inspect::Command.execute(ARGV.dup)
@@ -0,0 +1,13 @@
1
+ require "msgpack/inspect/version"
2
+ require "msgpack/inspect/inspector"
3
+
4
+ module MessagePack
5
+ module Inspect
6
+ FORMATS = MessagePack::Inspect::Inspector::FORMATS
7
+
8
+ # MessagePack::Inspect.inspect(io).dump(format)
9
+ def self.inspect(io)
10
+ MessagePack::Inspect::Inspector.new(io)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ require 'optparse'
2
+ require 'msgpack/inspect'
3
+
4
+ module MessagePack
5
+ module Inspect
6
+ module Command
7
+ def self.execute(argv)
8
+ format = :yaml
9
+
10
+ opts = OptionParser.new
11
+ opts.banner = "Usage: msgpack-inspect [options] FILE"
12
+ opts.separator ""
13
+ opts.separator "Options:"
14
+ opts.separator ""
15
+ opts.on("-f", "--format FORMAT", "output format of inspection result (yaml/json) [default: yaml]") do |v|
16
+ format = v.to_sym
17
+ end
18
+ opts.on("-r", "--require LIB", "ruby file path to require (to load ext type definitions)") do |path|
19
+ require path
20
+ end
21
+ opts.on_tail("-h", "--help", "Show this message") do
22
+ puts opts
23
+ exit
24
+ end
25
+ opts.parse!(argv)
26
+
27
+ filename = argv.first
28
+ io = if filename == '-'
29
+ STDIN.binmode
30
+ else
31
+ File.open(filename).binmode
32
+ end
33
+ unless MessagePack::Inspect::FORMATS.include?(format)
34
+ puts opts
35
+ puts "Unsupported format: #{format}"
36
+ exit 1
37
+ end
38
+
39
+ puts MessagePack::Inspect.inspect(io).dump(format)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,371 @@
1
+ module MessagePack
2
+ module Inspect
3
+ # Node(Hash)
4
+ # header: hex (binary)
5
+ # exttype: hex (value)
6
+ # length: numeric
7
+ # data: hex
8
+ # value: object
9
+ # children: (array of Node for array, array of Hash, which has keys of :key and :value for map)
10
+
11
+ class Inspector
12
+ FORMATS = [:yaml, :json]
13
+
14
+ attr_reader :data
15
+
16
+ def initialize(io)
17
+ @io = io
18
+ @data = [] # top level objects
19
+ until io.eof?
20
+ dig(io){|obj| @data << obj }
21
+ end
22
+ end
23
+
24
+ def hex(str)
25
+ str.unpack("H*").first
26
+ end
27
+
28
+ def node(fmt, header)
29
+ case fmt
30
+ when :fixint, :uint8, :uint16, :uint32, :uint64, :int8, :int16, :int32, :int64
31
+ {format: fmt, header: hex(header), data: nil, value: nil}
32
+ when :fixmap, :map16, :map32
33
+ {format: fmt, header: hex(header), length: nil, children: []}
34
+ when :fixarray, :array16, :array32
35
+ {format: fmt, header: hex(header), length: nil, children: []}
36
+ when :fixstr, :str8, :str16, :str32
37
+ {format: fmt, header: hex(header), length: nil, data: nil, value: nil}
38
+ when :nil
39
+ {format: fmt, header: hex(header), data: hex(header), value: nil}
40
+ when :false
41
+ {format: fmt, header: hex(header), data: hex(header), value: false}
42
+ when :true
43
+ {format: fmt, header: hex(header), data: hex(header), value: true}
44
+ when :bin8, :bin16, :bin32
45
+ {format: fmt, header: hex(header), length: nil, data: nil, value: nil}
46
+ when :ext8, :ext16, :ext32, :fixext1, :fixext2, :fixext4, :fixext8, :fixext16
47
+ {format: fmt, header: hex(header), exttype: nil, length: nil, data: nil} # value will be set if MessagePack is installed
48
+ when :float32, :float64
49
+ {format: fmt, header: hex(header), data: nil, value: nil}
50
+ when :never_used
51
+ {format: fmt, header: hex(header), data: hex(header), error: "msgpack format 'NEVER USED' specified"}
52
+ else
53
+ raise "unknown format specifier: #{fmt}"
54
+ end
55
+ end
56
+
57
+ def dump(format)
58
+ case format
59
+ when :yaml
60
+ require 'yaml'
61
+ YAML.dump(@data)
62
+ when :json
63
+ begin
64
+ require 'yajl'
65
+ Yajl::Encoder.encode(@data)
66
+ rescue LoadError
67
+ require 'json'
68
+ JSON.dump(@data)
69
+ end
70
+ else
71
+ raise "unknown format: #{format}"
72
+ end
73
+ end
74
+
75
+ def dig(io, &block)
76
+ header = io.read(1).b
77
+ fmt = parse_header(header)
78
+ yield generate(io, fmt, header, node(fmt, header))
79
+ end
80
+
81
+ HEADER_FIXINT_POSITIVE = Range.new(0x00, 0x7f)
82
+ HEADER_FIXMAP = Range.new(0x80, 0x8f)
83
+ HEADER_FIXARRAY = Range.new(0x90, 0x9f)
84
+ HEADER_FIXSTR = Range.new(0xa0, 0xbf)
85
+ HEADER_FIXINT_NEGATIVE = Range.new(0xe0, 0xff)
86
+ HEADER_NIL = 0xc0
87
+ HEADER_NEVER_USED = 0xc1
88
+ HEADER_FALSE = 0xc2
89
+ HEADER_TRUE = 0xc3
90
+ HEADER_BIN8 = 0xc4
91
+ HEADER_BIN16 = 0xc5
92
+ HEADER_BIN32 = 0xc6
93
+ HEADER_EXT8 = 0xc7
94
+ HEADER_EXT16 = 0xc8
95
+ HEADER_EXT32 = 0xc9
96
+ HEADER_FLOAT32 = 0xca
97
+ HEADER_FLOAT64 = 0xcb
98
+ HEADER_UINT8 = 0xcc
99
+ HEADER_UINT16 = 0xcd
100
+ HEADER_UINT32 = 0xce
101
+ HEADER_UINT64 = 0xcf
102
+ HEADER_INT8 = 0xd0
103
+ HEADER_INT16 = 0xd1
104
+ HEADER_INT32 = 0xd2
105
+ HEADER_INT64 = 0xd3
106
+ HEADER_FIXEXT1 = 0xd4
107
+ HEADER_FIXEXT2 = 0xd5
108
+ HEADER_FIXEXT4 = 0xd6
109
+ HEADER_FIXEXT8 = 0xd7
110
+ HEADER_FIXEXT16 = 0xd8
111
+ HEADER_STR8 = 0xd9
112
+ HEADER_STR16 = 0xda
113
+ HEADER_STR32 = 0xdb
114
+ HEADER_ARRAY16 = 0xdc
115
+ HEADER_ARRAY32 = 0xdd
116
+ HEADER_MAP16 = 0xde
117
+ HEADER_MAP32 = 0xdf
118
+
119
+ def parse_header(byte)
120
+ value = byte.unpack('C').first
121
+ case value
122
+ when HEADER_FIXINT_POSITIVE, HEADER_FIXINT_NEGATIVE then :fixint
123
+ when HEADER_FIXMAP then :fixmap
124
+ when HEADER_FIXARRAY then :fixarray
125
+ when HEADER_FIXSTR then :fixstr
126
+ when HEADER_NIL then :nil
127
+ when HEADER_NEVER_USED then :never_used
128
+ when HEADER_FALSE then :false
129
+ when HEADER_TRUE then :true
130
+ when HEADER_BIN8 then :bin8
131
+ when HEADER_BIN16 then :bin16
132
+ when HEADER_BIN32 then :bin32
133
+ when HEADER_EXT8 then :ext8
134
+ when HEADER_EXT16 then :ext16
135
+ when HEADER_EXT32 then :ext32
136
+ when HEADER_FLOAT32 then :float32
137
+ when HEADER_FLOAT64 then :float64
138
+ when HEADER_UINT8 then :uint8
139
+ when HEADER_UINT16 then :uint16
140
+ when HEADER_UINT32 then :uint32
141
+ when HEADER_UINT64 then :uint64
142
+ when HEADER_INT8 then :int8
143
+ when HEADER_INT16 then :int16
144
+ when HEADER_INT32 then :int32
145
+ when HEADER_INT64 then :int64
146
+ when HEADER_FIXEXT1 then :fixext1
147
+ when HEADER_FIXEXT2 then :fixext2
148
+ when HEADER_FIXEXT4 then :fixext4
149
+ when HEADER_FIXEXT8 then :fixext8
150
+ when HEADER_FIXEXT16 then :fixext16
151
+ when HEADER_STR8 then :str8
152
+ when HEADER_STR16 then :str16
153
+ when HEADER_STR32 then :str32
154
+ when HEADER_ARRAY16 then :array16
155
+ when HEADER_ARRAY32 then :array32
156
+ when HEADER_MAP16 then :map16
157
+ when HEADER_MAP32 then :map32
158
+ else
159
+ raise "never reach here."
160
+ end
161
+ end
162
+
163
+ def generate_array(io, fmt, header, current)
164
+ length = case fmt
165
+ when :fixarray
166
+ header.unpack('C').first & 0x0f
167
+ when :array16
168
+ io.read(2).unpack('n').first
169
+ when :array32
170
+ io.read(4).unpack('N').first
171
+ else
172
+ raise "unknown array fmt #{fmt}"
173
+ end
174
+ current[:length] = length
175
+ length.times do |i|
176
+ dig(io){|obj| current[:children] << obj }
177
+ end
178
+ current
179
+ end
180
+
181
+ def generate_map(io, fmt, header, current)
182
+ length = case fmt
183
+ when :fixmap
184
+ header.unpack('C').first & 0x0f
185
+ when :map16
186
+ io.read(2).unpack('n').first
187
+ when :map32
188
+ io.read(4).unpack('N').first
189
+ else
190
+ raise "unknown map fmt #{fmt}"
191
+ end
192
+ current[:length] = length
193
+ length.times do |i|
194
+ pair = {}
195
+ dig(io){|key| pair[:key] = key }
196
+ dig(io){|value| pair[:value] = value }
197
+ current[:children] << pair
198
+ end
199
+ current
200
+ end
201
+
202
+ MAX_INT16 = 2 ** 16
203
+ MAX_INT32 = 2 ** 32
204
+ MAX_INT64 = 2 ** 64
205
+
206
+ def generate_int(io, fmt, header, current)
207
+ if :fixint
208
+ current[:data] = hex(header)
209
+ v = header.unpack('C').first
210
+ if v & 0b11100000 > 0 # negative fixint
211
+ current[:value] = header.unpack('c').first
212
+ else # positive fixint
213
+ current[:value] = header.unpack('C').first
214
+ end
215
+ return current
216
+ end
217
+
218
+ case fmt
219
+ when :uint8
220
+ v = io.read(1)
221
+ current[:data] = hex(v)
222
+ current[:value] = v.unpack('C').first
223
+ when :uint16
224
+ v = io.read(2)
225
+ current[:data] = hex(v)
226
+ current[:value] = v.unpack('n').first
227
+ when :uint32
228
+ v = io.read(4)
229
+ current[:data] = hex(v)
230
+ current[:value] = v.unpack('N').first
231
+ when :uint64
232
+ v1 = io.read(4)
233
+ v2 = io.read(4)
234
+ current[:data] = hex(v1) + hex(v2)
235
+ current[:value] = (v1.unpack('N').first << 32) & v2.unpack('N').first
236
+ when :int8
237
+ v = io.read(1)
238
+ current[:data] = hex(v)
239
+ current[:value] = v.unpack('c').first
240
+ when :int16
241
+ v = io.read(1)
242
+ current[:data] = hex(v)
243
+ current[:value] = v.unpack('n').first - MAX_INT16
244
+ when :int32
245
+ v = io.read(2)
246
+ current[:data] = hex(v)
247
+ current[:value] = v.unpack('N').first - MAX_INT32
248
+ when :int64
249
+ v1 = io.read(4)
250
+ v2 = io.read(4)
251
+ current[:data] = hex(v1) + hex(v2)
252
+ current[:value] = (v1.unpack('N').first << 32) & v2.unpack('N').first - MAX_INT64
253
+ else
254
+ raise "unknown int format #{fmt}"
255
+ end
256
+ current
257
+ end
258
+
259
+ def generate_float(io, fmt, header, current)
260
+ case fmt
261
+ when :float32
262
+ v = io.read(4)
263
+ current[:data] = hex(v)
264
+ current[:value] = v.unpack('g').first
265
+ when :float64
266
+ v = io.read(8)
267
+ current[:data] = hex(v)
268
+ current[:value] = v.unpack('G').first
269
+ else
270
+ raise "unknown float format #{fmt}"
271
+ end
272
+ current
273
+ end
274
+
275
+ def generate_string(io, fmt, header, current)
276
+ length = case fmt
277
+ when :fixstr
278
+ header.unpack('C').first & 0b00011111
279
+ when :str8, :bin8
280
+ io.read(1).unpack('C').first
281
+ when :str16, :bin16
282
+ io.read(2).unpack('n').first
283
+ when :str32, :bin32
284
+ io.read(4).unpack('N').first
285
+ else
286
+ raise "unknown string format #{fmt}"
287
+ end
288
+ current[:length] = length
289
+ v = io.read(length)
290
+ current[:data] = hex(v)
291
+ if [:fixstr, :str8, :str16, :str32].include?(fmt)
292
+ begin
293
+ current[:value] = v.encode('UTF-8')
294
+ rescue
295
+ current[:error] = $!.message
296
+ end
297
+ else
298
+ current[:value] = v.b
299
+ end
300
+ current
301
+ end
302
+
303
+ begin
304
+ require 'msgpack'
305
+ rescue LoadError
306
+ # ignore
307
+ end
308
+ MSGPACK_LOADED = MessagePack.const_defined?('Unpacker')
309
+
310
+ def generate_ext(io, fmt, header, current)
311
+ data = ''.b
312
+ length = case fmt
313
+ when :fixext1 then 1
314
+ when :fixext2 then 2
315
+ when :fixext4 then 4
316
+ when :fixext8 then 8
317
+ when :fixext16 then 16
318
+ when :ext8
319
+ v = io.read(1)
320
+ data << v
321
+ v.unpack('C').first
322
+ when :ext16
323
+ v = io.read(2)
324
+ data << v
325
+ v.unpack('n').first
326
+ when :ext32
327
+ v = io.read(4)
328
+ data << v
329
+ v.unpack('N').first
330
+ else
331
+ raise "unknown ext format #{fmt}"
332
+ end
333
+ v = io.read(1)
334
+ data << v
335
+ type = v.unpack('c').first
336
+ current[:length] = length
337
+ current[:exttype] = type
338
+ val = io.read(length)
339
+ data << val
340
+ current[:data] = hex(val)
341
+ if MSGPACK_LOADED
342
+ current[:value] = MessagePack.unpack(val)
343
+ end
344
+ current
345
+ end
346
+
347
+ def generate(io, fmt, header, current)
348
+ case fmt
349
+ when :fixarray, :array16, :array32
350
+ generate_array(io, fmt, header, current)
351
+ when :fixmap, :map16, :map32
352
+ generate_map(io, fmt, header, current)
353
+ when :nil, :false, :true, :never_used
354
+ # nothing to do...
355
+ when :fixint, :uint8, :uint16, :uint32, :uint64, :int8, :int16, :int32, :int64
356
+ generate_int(io, fmt, header, current)
357
+ when :float32, :float64
358
+ generate_float(io, fmt, header, current)
359
+ when :fixstr, :str8, :str16, :str32, :bin8, :bin16, :bin32
360
+ generate_string(io, fmt, header, current)
361
+ when :ext8, :ext16, :ext32, :fixext1, :fixext2, :fixext4, :fixext8, :fixext16
362
+ generate_ext(io, fmt, header, current)
363
+ else
364
+ raise "unknown format #{fmt}"
365
+ end
366
+
367
+ current
368
+ end
369
+ end
370
+ end
371
+ end
@@ -0,0 +1,5 @@
1
+ module MessagePack
2
+ module Inspect
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
Binary file
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'msgpack/inspect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "msgpack-inspect"
8
+ spec.version = MessagePack::Inspect::VERSION
9
+ spec.authors = ["TAGOMORI Satoshi"]
10
+ spec.email = ["tagomoris@gmail.com"]
11
+
12
+ spec.summary = %q{Utility to inspect MessagePack binary}
13
+ spec.description = %q{This is a command line tool to inspect MessagePack binary, and show the results in YAML or JSON.}
14
+ spec.homepage = "https://github.com/tagomoris/msgpack-inspect"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+ spec.has_rdoc = false
21
+ spec.license = "Apache-2.0"
22
+
23
+ spec.add_development_dependency "msgpack", "~> 1.0"
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "test-unit"
27
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: msgpack-inspect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TAGOMORI Satoshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: msgpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This is a command line tool to inspect MessagePack binary, and show the
70
+ results in YAML or JSON.
71
+ email:
72
+ - tagomoris@gmail.com
73
+ executables:
74
+ - msgpack-inspect
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - exe/msgpack-inspect
84
+ - lib/msgpack/inspect.rb
85
+ - lib/msgpack/inspect/command.rb
86
+ - lib/msgpack/inspect/inspector.rb
87
+ - lib/msgpack/inspect/version.rb
88
+ - msgpack-example.bin
89
+ - msgpack-inspect.gemspec
90
+ homepage: https://github.com/tagomoris/msgpack-inspect
91
+ licenses:
92
+ - Apache-2.0
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Utility to inspect MessagePack binary
114
+ test_files: []
115
+ has_rdoc: false