Compare commits

...

186 Commits

Author SHA1 Message Date
frsyuki
9684c8664f cpp: version 0.5.4 2010-08-29 18:27:10 +09:00
frsyuki
3c75361e5a cpp: updates README.md 2010-08-29 18:24:32 +09:00
frsyuki
c44c9ab74d cpp: adds msgpack_vc2008.vcproj file in source package 2010-08-29 18:23:16 +09:00
frsyuki
8cc9c871b7 Merge branch 'master' of github.com:msgpack/msgpack 2010-08-28 11:40:45 +09:00
UENISHI Kota
31d211cded Merge branch 'master' of ssh://github.com/msgpack/msgpack 2010-08-28 00:36:54 +09:00
UENISHI Kota
c42cba1d54 erlang: fixed bug around error case when serializing atom. 2010-08-27 23:02:16 +09:00
frsyuki
c87f7cb9ac cpp: fixes fix_int; updates test/fixint.cc 2010-08-27 20:52:40 +09:00
frsyuki
421bee3871 cpp: version 0.5.3 2010-08-27 17:53:19 +09:00
frsyuki
2c7573a032 cpp: updates msgpack_vc8.postbuild.bat 2010-08-27 17:53:02 +09:00
frsyuki
fe2a0f5089 cpp: adds fixed length serialization for integers 2010-08-27 17:42:05 +09:00
frsyuki
59ba8dec4e cpp: fixes include paths 2010-08-27 16:45:48 +09:00
tokuhirom
8de1f764fd Perl: bump up version to 0.14 2010-08-21 16:09:30 +09:00
tokuhirom
a91c1ec6d9 fixed segv on cyclic reference(patch by dankogai) 2010-08-21 16:02:23 +09:00
frsyuki
c8e351b31e java: adds TestMessageUnpackable test 2010-08-21 03:36:44 +09:00
frsyuki
b4c98584db java: adds Unpacker.unpackBigInteger() 2010-08-19 01:19:34 +09:00
frsyuki
1d17836b7d java: NilType::create() returns NilType's one and only instance 2010-08-19 00:54:23 +09:00
frsyuki
193a739749 java: updates TestDirectConversion 2010-08-19 00:05:48 +09:00
frsyuki
48da2b8353 java: adds Packer.pack(<primitive types>) 2010-08-18 23:47:31 +09:00
frsyuki
40dc9de6c9 java: supports packing/unpacking of BigInteger less than 0xffffffffffffffff 2010-08-18 23:37:47 +09:00
frsyuki
d7469e4694 java: fixes cross-language test case 2010-08-18 22:55:50 +09:00
frsyuki
fdfabc9f88 java: adds cross-language test case 2010-08-18 22:51:23 +09:00
frsyuki
5658ca5b90 java: adds ObjectEquals test 2010-08-18 22:24:51 +09:00
frsyuki
8b79e6d3c7 java: uses MessagePackObject instead of Object for type of deserialized objects 2010-08-18 18:10:30 +09:00
frsyuki
8c67087a15 java: adds MessagePackObject.bigIntegerValue(), asBigInteger() and equals() 2010-08-18 16:32:42 +09:00
frsyuki
057f73a73e java: implements MessagePackObject::hashCode() 2010-08-10 14:11:44 +09:00
frsyuki
d3bb37d113 java: fixes MapSchema 2010-08-10 14:11:25 +09:00
frsyuki
6c91b862c9 java: MessagePackObject implements Cloneable and MessagePackable interfaces 2010-07-28 01:17:20 +09:00
frsyuki
cba47b635a java: changed deploy path to ./target/website/maven2 directory. 2010-07-27 09:50:57 +09:00
Kazuki Ohta
cd83388f8b java: fixed repository location. msgpack.sourceforge.net => msgpack.org 2010-07-27 08:59:09 +09:00
frsyuki
02ae247536 java: adds MessagePackObject class 2 2010-07-24 18:20:00 +09:00
frsyuki
1621a68191 Merge branch 'master' of github.com:msgpack/msgpack 2010-07-24 18:08:19 +09:00
frsyuki
2aef495d62 java: adds MessagePackObject class 2010-07-24 18:08:00 +09:00
frsyuki
227c168b65 java: fixes fatal offset calculation bugs on BufferedUnpackerIMPL.unpackInt() 2010-07-24 18:07:22 +09:00
UENISHI Kota
dcbcf5842f erlang: msgpack:unpack_all/1 doc. 2010-07-18 23:55:07 +09:00
UENISHI Kota
8a3ac6d9bd erlang: omake menus added. 2010-07-18 23:50:29 +09:00
UENISHI Kota
6cabad19d5 erlang: unpack_all/1 improve, error handling added. 2010-07-18 23:48:20 +09:00
UENISHI Kota
dad7a03d19 erlang: stopped support for dict() type. 2010-07-18 23:42:23 +09:00
UENISHI Kota
7b152640d9 erlang: 'edoc' document generation 2010-07-18 23:40:25 +09:00
UENISHI Kota
78fddff34e erlang: merged vincent's contribution 2010-07-18 23:01:43 +09:00
frsyuki
f8a016edb5 Merge branch 'master' of github.com:msgpack/msgpack 2010-07-14 17:09:34 +09:00
frsyuki
f5453d38ec cpp: version 0.5.2 2010-07-14 17:06:16 +09:00
frsyuki
331bf0af21 cpp: type::raw_ref::str(), operator==, operator!=, operator< and operator> are now const 2010-07-14 17:04:41 +09:00
tokuhirom
9ac69337e8 perl: bump up version to 0.13! 2010-07-14 09:58:41 +09:00
tokuhirom
ca0c844f32 clearly specified this distribution requires requires C99. 2010-07-14 09:58:05 +09:00
Vincent de Phily
e629e8784f erlang: Improve documentation
The doc is in edoc format, generated from the source as an html file.
The makefile's default action now also generates the documentation.

I ignored unpack_all/1 and pack(dict()) for now because their future is still uncertain.
2010-07-12 14:08:22 +02:00
Vincent de Phily
e944c1ee93 erlang: Only handle throw() in pack/1 and unpack/1
Rationale: We only use throw/1 for error handling, never erlang:error/1.
           Caller bugs will get a nice {error,...} return while library bugs will
           bubble up in all their uglyness; that's the proper way to do things
           in erlang.
2010-07-09 20:37:06 +02:00
Vincent de Phily
02c882bda3 erlang: Make pack_map/1 api private 2010-07-09 20:34:38 +02:00
Vincent de Phily
2c29377abf erlang: s/short/incomplete/ and s/badarg/{badarg,Term}/
Nicer error returns.
2010-07-09 20:30:17 +02:00
Vincent de Phily
21992f1b9e erlang: fix receiving from port_command in unit tests
Ports can send data bit by bit; make sure we read all the port has to offer in one go.
This should fix the "broken pipe" error we sometime got during testing.
We did not previously check the return of compare_all/2, which is why the bug was not noticed.
Incidentally, this change fixes dialyzer warnings too.
2010-07-09 18:53:24 +02:00
Vincent de Phily
8a3f090684 erlang: Fix some existing specs and add a few other.
dialyzer still complains about dict() and ?assert(false), but I don't think they're real issues.
2010-07-09 17:36:36 +02:00
UENISHI Kota
a4258505a9 erlang: modified wrong testcase. 2010-07-09 23:23:00 +09:00
Vincent de Phily
ba4a971bfa erlang: Remove unecessary 'throw(short)' clause for unpack_{array,map}_/1
Unecessary because unpack_/1 will throw it anyway.
This does mean that we go a tiny bit deeper to find that we don't have enough data,
but that should be a rare code path. Keep the main code path fast and the code clean.

While at it, rename vars to match its sibling function and to avoid thinking that
RestLen is a byte count (it's an item count).
2010-07-09 13:44:02 +02:00
Vincent de Phily
6abc120279 erlang: Fix incomplete/invalid cases of unpack_/1
* fix list of invalid bytes was missing 3 possibilities (see type chart section of msgpack format spec)
* fix matching of invalid bytes to look at 1 byte instead of 2
* simplify 'incomplete' case : anything that's not complete or invalid is by definition incomplete
2010-07-09 13:29:47 +02:00
Vincent de Phily
64c36b7a8f Remove a couple of superfluous 'when' clauses.
The when clause for unpack_/1 has been moved to unpack/1 so that it is performed only once.
2010-07-09 13:06:57 +02:00
Vincent de Phily
64b9f0762c Merge branch 'master' of git://github.com/msgpack/msgpack
Reseting msgpack.erl to upstream version.
2010-07-09 12:35:22 +02:00
UENISHI Kota
e799082e5c erlang: better test cases, except 'Broken pipe' 2010-07-09 01:21:35 +09:00
UENISHI Kota
eab66a022e erlang: added try-catch clause for easy error handling 2010-07-09 01:04:09 +09:00
UENISHI Kota
485915c27a erlang: added simple performance test description. 2010-07-08 23:39:47 +09:00
UENISHI Kota
45fb482ab4 erlang: added simple performance test. 2010-07-08 23:36:18 +09:00
frsyuki
167e2475d8 cpp: generate version.h using AC_OUTPUT macro in ./configure 2010-07-06 23:30:15 +09:00
frsyuki
fe77251242 cpp: fixes missing dependency to generate version.h 2010-07-06 19:16:49 +09:00
frsyuki
0c331d2887 cpp: updates vcproj 2010-07-06 18:18:28 +09:00
frsyuki
39facd5dc6 cpp: version 0.5.1 2010-07-06 17:59:07 +09:00
frsyuki
a2bd5ae638 cpp: ./configure supports --disable-cxx option not to build/install C++ API 2010-07-06 17:45:15 +09:00
frsyuki
c57f616141 cpp: adds MSGPACK_VERSION{,_MAJOR,_MINOR} macros and msgpack{,_major,_minor} functions 2010-07-06 17:10:25 +09:00
frsyuki
3af10a1d00 cpp: adds MSGPACK_VERSION{,_MAJOR,_MINOR} macros and msgpack{,_major,_minor} functions 2010-07-06 17:00:58 +09:00
frsyuki
b3987e2402 Merge branch 'master' of github.com:msgpack/msgpack 2010-07-06 12:26:30 +09:00
frsyuki
71dd44f430 cpp: adds operator<<(std::ostream&, const tuple<Type...>&) (experimental) 2010-07-06 12:26:21 +09:00
UENISHI Kota
584462f9b9 erlang: improved spec. 2010-07-01 01:16:25 +09:00
UENISHI Kota
ff5d5d7cbc erlang: updated the comments 2010-07-01 01:14:38 +09:00
UENISHI Kota
370e92b1a6 erlang: just a golf. 2010-07-01 01:14:20 +09:00
UENISHI Kota
2469768a85 erlang: reducing unnecessary binary matching in unpack_/2
* more efficient unpack_/1 by Vincent de Phille's code. thanks.
2010-07-01 01:07:56 +09:00
UENISHI Kota
acb8fa613e erlang: adding shorthand fix for {more, undefined} problem 2010-07-01 01:02:19 +09:00
UENISHI Kota
83b4b7d83d erlang: more suitable variable name and removing unnecessary guards. 2010-07-01 00:58:48 +09:00
Vincent de Phily
33a7d56042 * Return {more,undefined} instead of {more,integer()}, as we can only know the "minimum bytes needed to continue" instead of the actually usefull "total packet size".
* Merge all {more,...} clauses of unpack_/1 into one.
* Reformat unpack_/1 for readability.
* Fix some specs, error values, and documentation.
2010-06-29 11:59:56 +02:00
frsyuki
20de730541 ruby: 0.4.3 2010-06-29 15:39:47 +09:00
frsyuki
134c27c900 Merge branch 'master' of github.com:msgpack/msgpack 2010-06-29 15:13:21 +09:00
frsyuki
123ae024c6 ruby: MessagePack::VERSION constant 2010-06-29 15:12:52 +09:00
frsyuki
34a29cd0a5 ruby: fixes SEGV problem caused by GC bug at MessagePack_Unpacker_mark. 2010-06-29 14:56:23 +09:00
frsyuki
9fffa9800a ruby: fixes RDoc of Unpacker#execute and Unpacker#execute_impl 2010-06-29 14:54:09 +09:00
Vincent de Phily
8f7f23a0e5 Rewrite unpack_/1 using pattern matching to get a 30-40% speedup.
Simplify pack_* and unpack_{array,map} function clauses to get more readability and a minor speedup.
2010-06-28 18:11:52 +02:00
UENISHI Kota
358457f49d erlang: bad wrong export.. 2010-06-29 00:25:58 +09:00
UENISHI Kota
90e305d789 erlang: explicit API for serializing proplists,
so as not to make wrong call of pack({proplists()}).
2010-06-29 00:23:49 +09:00
UENISHI Kota
b471e52e28 erlang: explicit API for serializing proplists,
so as not to make wrong call of pack({proplists()}).
2010-06-29 00:21:47 +09:00
UENISHI Kota
9b5fc37399 Merge branch 'master' of http://github.com/vincentdephily/msgpack 2010-06-29 00:10:10 +09:00
Vincent de Phily
537322e3b5 Big speedup (around 40%) of maps and arrays encoding by using proper tail recursion. 2010-06-28 14:17:44 +02:00
Vincent de Phily
279121f87f erlang: Use a simple proplist instead of a dict.
A dict is overkill (code, cpu, memory) in most cases, and proplist<->dict conversion can easily be done by the libray user if desired.
        This is in line with other erlang libraries I've seen for various encoding schemes.
        The map encoder had a bug until I looked at it (see previous commit), so I guess it wasn't used much yet and a change is ok at this stage.
        The chosen representation for maps is a tuple containing the proplist as the only element.
2010-06-28 11:56:12 +02:00
UENISHI Kota
a1b2b41cdc erlang: bugfix(serialization of -234 goes <<208,22>> while it should go int16 <<0xD1, ...>>) 2010-06-26 08:40:36 +09:00
Vincent de Phily
0cca90c21d Fix encoding of fixmap type.
The tag value was wrong, and a missing /binary flag caused an error.
2010-06-25 17:32:11 +02:00
UENISHI Kota
ad052cb510 updated readme 2010-06-25 01:26:57 +09:00
UENISHI Kota
57f0598373 erlang: code refined and tests added 2010-06-25 00:44:14 +09:00
UENISHI Kota
92d192277e erlang: unpack_map's silly bug fixed. use dict:store/3.... 2010-06-25 00:22:53 +09:00
UENISHI Kota
2cdfbd8970 erlang: testing pack_map/unpack_map with a silly bug 2010-06-24 07:26:34 +09:00
UENISHI Kota
bc0c5f0cdc erlang: (un)pack_map improved, incremental unpacking 2010-06-23 09:02:53 +09:00
UENISHI Kota
230ee3a03b erlang: too short binary to decode causes error {more, Int}. 2010-06-23 01:26:10 +09:00
UENISHI Kota
ab0bf37d30 Merge branch 'master' of ssh://github.com/msgpack/msgpack 2010-06-22 11:44:25 +09:00
UENISHI Kota
b1e66256ce erlang: external APIs' type/specs. 2010-06-22 11:28:36 +09:00
UENISHI Kota
fd80693420 erlang: tests improved and code refined. 2010-06-22 11:15:18 +09:00
Naoki INADA
f222f5ed9b Python: 0.1.4 2010-06-15 18:06:58 +09:00
INADA Naoki
59603b902a Python: add "load(s)/dump(s)" alias for compatibility to simplejson/marshal/pickle. 2010-06-15 17:51:24 +09:00
frsyuki
82a5dd6cf9 java: update 2010-06-10 14:02:58 -07:00
frsyuki
a97f9081a3 Merge branch 'master' of github.com:msgpack/msgpack 2010-06-03 22:04:36 +09:00
frsyuki
b3e0ad1303 ruby: 0.4.2 2010-06-03 22:00:15 +09:00
frsyuki
251090406a ruby: adds a test case for buffering 2010-06-03 21:52:01 +09:00
frsyuki
9c3ed173b1 ruby: fixes buffering routine 2010-06-03 21:51:40 +09:00
UENISHI Kota
7cd41aeb72 erlang: tracing crosslang.rb moving to ../test 2010-06-03 00:17:17 +09:00
UENISHI Kota
8ecaf7ad4c Merge branch 'master' of ssh://github.com/msgpack/msgpack 2010-06-03 00:14:19 +09:00
frsyuki
d4049fe593 ruby: add test/test_cases.rb 2010-06-01 16:35:21 +09:00
frsyuki
989b14b519 update test/README.md 2010-06-01 15:58:44 +09:00
frsyuki
fb3e11408c add test/cases.json 2010-06-01 15:56:29 +09:00
frsyuki
3d3af3284e cpp: adds Doxyfile 2010-06-01 08:43:30 +09:00
frsyuki
eabcf15790 cpp: update tests 2010-06-01 07:16:25 +09:00
frsyuki
684bca203a cpp: adds msgpack_unpacker_next and msgpack_unpack_next 2010-06-01 07:15:58 +09:00
frsyuki
d42ecccf6f cpp: msgpack::unpack returns void 2010-06-01 07:13:47 +09:00
frsyuki
5a92c861e3 cpp: adds msgpack_vrefbuffer_new and msgpack_vrefbuffer_free 2010-06-01 07:11:01 +09:00
frsyuki
103b14ea3c cpp: adds msgpack_zbuffer_new and msgpack_zbuffer_free 2010-06-01 07:10:39 +09:00
frsyuki
e49f091b4e cpp: adds msgpack_sbuffer_new and msgpack_sbuffer_free 2010-06-01 07:10:17 +09:00
frsyuki
6056f93910 cpp: add cases.mpac test 2010-06-01 05:15:36 +09:00
frsyuki
18fa2d1af4 Merge branch 'master' of github.com:msgpack/msgpack 2010-06-01 04:47:37 +09:00
frsyuki
062ed8a4c4 add test/cases.mpac and test/cases_compact.mpac 2010-06-01 04:47:28 +09:00
UENISHI Kota
49f3872d04 erlang: temporary documentation and .gitignore 2010-06-01 00:31:12 +09:00
UENISHI Kota
d9b467098a erlang: added more cross-language tests. better type specification. 2010-05-31 23:56:06 +09:00
UENISHI Kota
7d1e51437e erlang: added usage of cross-language test. 2010-05-31 23:13:32 +09:00
frsyuki
f5a7d444e2 Merge branch 'master' of github.com:msgpack/msgpack 2010-05-31 17:32:51 +09:00
frsyuki
a0071c2f9f add crosslang.rb 2010-05-31 17:27:51 +09:00
frsyuki
98a5e43883 add crosslang.cc 2010-05-31 17:16:40 +09:00
UENISHI Kota
f40ebe5b43 Merge branch 'master' of ssh://github.com/msgpack/msgpack 2010-05-31 00:26:49 +09:00
UENISHI Kota
d7d78d9a2b added more tests,
and OMake continuous building.
2010-05-31 00:25:53 +09:00
Hideyuki Tanaka
5a12d36a0a incr version 2010-05-30 19:45:00 +09:00
Hideyuki Tanaka
e61dc76ae1 fix peek object 2010-05-30 19:11:04 +09:00
Hideyuki Tanaka
0da22193bd fix typo 2010-05-30 17:20:49 +09:00
Hideyuki Tanaka
d43921823e fix initialize pointer 2010-05-30 17:19:43 +09:00
UENISHI Kota
6b5b76b0c9 initial import from http://bitbucket.org/kuenishi/messagepack-for-erlang 2010-05-30 15:01:10 +09:00
frsyuki
602971408b cpp: move source files into src/ directory 2010-05-30 03:02:40 +09:00
frsyuki
2f5d83f07d cpp: type::tuple& operator>>: fix conversion type 2010-05-30 01:45:07 +09:00
frsyuki
81b0c316cd java: Unpacker: rewind internal buffer on filled <= offset 2010-05-30 01:39:48 +09:00
frsyuki
6df86384ca java: update javadoc 2010-05-29 07:54:49 +09:00
frsyuki
3fbcde4bd7 ruby: don't use rb_enc_set/get on ruby 1.8 2010-05-26 18:11:09 +09:00
frsyuki
293293c23c ruby: set mp->user.source = Qnil before tempalte_execute_do on Unpacker#each 2010-05-26 18:01:27 +09:00
frsyuki
47185d757e ruby: version 0.4.0 2010-05-26 07:55:02 +09:00
frsyuki
94c3998507 ruby: update gemspec 2010-05-26 07:43:05 +09:00
frsyuki
5fa589691c ruby: use malloc/realloc for stream buffer 2010-05-26 07:01:28 +09:00
frsyuki
26bc835c7e ruby: buffer rewinding 2010-05-26 04:30:49 +09:00
frsyuki
fc7da17fa2 cpp: add sbuffer::clear() and vrefbuffer::clear() 2010-05-25 02:57:37 +09:00
frsyuki
dbebe9771b ruby: update rdoc 2010-05-25 02:55:58 +09:00
frsyuki
d0af8aa9f1 ruby: rdoc 2010-05-23 21:10:49 +09:00
Kazuki Ohta
f8173e93f5 java: version 0.3 (added CHANGES.txt and LICENSE.txt) 2010-05-23 01:48:20 +09:00
frsyuki
fa6ea6848f java: fixed problem that empty array and empty map don't check Schema 2010-05-23 01:38:01 +09:00
frsyuki
5982970e21 java: fixed problem that empty array and empty map don't check Schema 2010-05-23 01:34:45 +09:00
Kazuki Ohta
c43e5e0c95 java: added testcases for empty array and empty map 2010-05-23 01:31:15 +09:00
frsyuki
b4fc79c38e java: fixes compile error 2010-05-22 17:05:17 +09:00
frsyuki
b9cb270b8f java: add Unpacker.unpack(MessageUnpackable) and Unpacker.tryUnpackNil() 2010-05-22 03:34:43 +09:00
frsyuki
1fe35d7efe java: fix Packer.packByte 2010-05-22 03:34:17 +09:00
frsyuki
ec8c19b1f0 java: javadoc 2010-05-20 17:32:15 +09:00
frsyuki
c2525bcc05 java: add Unpacker.wrap method 2010-05-20 06:19:26 +09:00
frsyuki
985c31b378 java: add Unpacker.wrap method 2010-05-20 06:18:32 +09:00
frsyuki
135a9f5586 java: fix direct conversion API 2010-05-20 05:44:44 +09:00
frsyuki
979ff80982 java: redesign 2010-05-20 03:49:26 +09:00
frsyuki
6cde9f3a9d Merge branch 'master' of github.com:msgpack/msgpack 2010-05-19 16:31:16 +09:00
frsyuki
5cad81bf4c cpp: fix return type mismatch in zone.c 2010-05-18 14:48:36 +09:00
frsyuki
18967162cf cpp: fix return type mismatch in unpack.c 2010-05-18 14:48:23 +09:00
tokuhirom
6ea75f3a9f Perl: do not use done_testing 2010-05-17 05:52:32 +09:00
tokuhirom
f51123d009 oops 2010-05-17 05:49:39 +09:00
tokuhirom
be6376ee2d Perl: build_requires and requires are duped. 2010-05-08 12:02:31 +09:00
tokuhirom
120a85a3e5 Perl: releng for 0.12 2010-05-05 17:28:38 +09:00
tokuhirom
262fe96c29 Perl: PERL_NO_GET_CONTEXT makes horrible dTHXs. remove it. 2010-05-05 17:25:45 +09:00
tokuhirom
1864df5ed0 Perl: cleanup Makefile.PL 2010-05-05 17:25:25 +09:00
tokuhirom
09bae0a9e8 Perl: Test::Requires is not needed for this test. 2010-05-05 17:25:08 +09:00
tokuhirom
ebe41a24f1 perl: releng for data-messagepack 0.11 2010-05-05 16:59:31 +09:00
tokuhirom
f0f574a15b Perl: releng for 0.10 2010-05-05 16:55:06 +09:00
tokuhirom
9420436c09 Perl: added test case 2010-05-05 16:22:40 +09:00
tokuhirom
2b8f853b96 Perl: fixed some issues. thanks to gfx++
http://gist.github.com/387743
2010-05-05 16:17:57 +09:00
tokuhirom
2c2bf60d0c Perl: added README file. 2010-05-05 15:50:07 +09:00
Hideyuki Tanaka
62b82448d5 Merge branch 'master' of git@github.com:msgpack/msgpack 2010-05-05 04:33:11 +09:00
Hideyuki Tanaka
2f12e6c3d0 remove compiler warnings 2010-05-05 04:28:04 +09:00
Kazuki Ohta
8ce23f8e3e java: fixed pom.xml to work "mvn deploy" command 2010-05-04 18:33:29 +09:00
Hideyuki Tanaka
dbe760d6e2 make () to OBJECT instance (Nil) 2010-05-04 16:24:45 +09:00
Hideyuki Tanaka
674c26d9c7 fix feed function from Handle 2010-05-04 16:22:04 +09:00
tokuhirom
7b68b04efd Perl: change for release Data-MessagePack-0.09_01 2010-05-03 01:36:48 +09:00
tokuhirom
e0b65bf196 Merge branch 'master' of git@github.com:msgpack/msgpack 2010-05-03 01:20:00 +09:00
tokuhirom
77f5cb1f1f Perl: updated docs. 2010-05-03 01:09:21 +09:00
tokuhirom
2a222737f8 Merge branch 'master' of git@github.com:msgpack/msgpack 2010-05-03 01:04:50 +09:00
tokuhirom
77d48f9cee Perl: fixed memory leak issue 2010-05-03 00:46:15 +09:00
tokuhirom
c77eac325e Perl: added failing test case for memory leaks 2010-05-03 00:22:16 +09:00
tokuhirom
517ced2a54 Perl: added more test case for streaming unpacker 2010-05-03 00:08:02 +09:00
tokuhirom
70d2c47367 perl: added more test case for streaming deserializer. 2010-05-02 22:09:18 +09:00
178 changed files with 8644 additions and 3427 deletions

View File

@@ -1,4 +1,31 @@
2010-08-29 version 0.5.4:
* includes msgpack_vc2008.vcproj file in source package
* fixes type::fix_int types
2010-08-27 version 0.5.3:
* adds type::fix_{u,}int{8,16,32,64} types
* adds msgpack_pack_fix_{u,}int{8,16,32,64} functions
* adds packer<Stream>::pack_fix_{u,}int{8,16,32,64} functions
* fixes include paths
2010-07-14 version 0.5.2:
* type::raw::str(), operator==, operator!=, operator< and operator> are now const
* generates version.h using AC_OUTPUT macro in ./configure
2010-07-06 version 0.5.1:
* Add msgpack_vrefbuffer_new and msgpack_vrefbuffer_free
* Add msgpack_sbuffer_new and msgpack_sbuffer_free
* Add msgpack_unpacker_next and msgpack_unpack_next
* msgpack::unpack returns void
* Add MSGPACK_VERSION{,_MAJOR,_MINOR} macros to check header version
* Add msgpack_version{,_major,_minor} functions to check library version
* ./configure supports --disable-cxx option not to build C++ API
2010-04-29 version 0.5.0:
* msgpack_object_type is changed. MSGPACK_OBJECT_NIL is now 0x00.

1552
cpp/Doxyfile Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,93 +1,20 @@
SUBDIRS = src test
lib_LTLIBRARIES = libmsgpack.la
libmsgpack_la_SOURCES = \
unpack.c \
objectc.c \
vrefbuffer.c \
zone.c \
object.cpp
# -version-info CURRENT:REVISION:AGE
libmsgpack_la_LDFLAGS = -version-info 3:0:0
# backward compatibility
lib_LTLIBRARIES += libmsgpackc.la
libmsgpackc_la_SOURCES = \
unpack.c \
objectc.c \
vrefbuffer.c \
zone.c
libmsgpackc_la_LDFLAGS = -version-info 2:0:0
# work around for duplicated file name
kumo_manager_CFLAGS = $(AM_CFLAGS)
kumo_manager_CXXFLAGS = $(AM_CXXFLAGS)
nobase_include_HEADERS = \
msgpack/pack_define.h \
msgpack/pack_template.h \
msgpack/unpack_define.h \
msgpack/unpack_template.h \
msgpack/sysdep.h \
msgpack.h \
msgpack/sbuffer.h \
msgpack/vrefbuffer.h \
msgpack/zbuffer.h \
msgpack/pack.h \
msgpack/unpack.h \
msgpack/object.h \
msgpack/zone.h \
msgpack.hpp \
msgpack/sbuffer.hpp \
msgpack/vrefbuffer.hpp \
msgpack/zbuffer.hpp \
msgpack/pack.hpp \
msgpack/unpack.hpp \
msgpack/object.hpp \
msgpack/zone.hpp \
msgpack/type.hpp \
msgpack/type/bool.hpp \
msgpack/type/float.hpp \
msgpack/type/int.hpp \
msgpack/type/list.hpp \
msgpack/type/deque.hpp \
msgpack/type/map.hpp \
msgpack/type/nil.hpp \
msgpack/type/pair.hpp \
msgpack/type/raw.hpp \
msgpack/type/set.hpp \
msgpack/type/string.hpp \
msgpack/type/vector.hpp \
msgpack/type/tuple.hpp \
msgpack/type/define.hpp \
msgpack/type/tr1/unordered_map.hpp \
msgpack/type/tr1/unordered_set.hpp
EXTRA_DIST = \
DOC_FILES = \
README.md \
LICENSE \
NOTICE \
msgpack_vc8.vcproj \
msgpack_vc8.sln \
msgpack_vc8.postbuild.bat
msgpack_vc2008.vcproj \
msgpack_vc2008.sln \
msgpack_vc.postbuild.bat
SUBDIRS = test
EXTRA_DIST = \
$(DOC_FILES)
check_PROGRAMS = \
msgpackc_test \
msgpack_test
msgpackc_test_SOURCES = msgpackc_test.cpp
msgpackc_test_LDADD = libmsgpack.la -lgtest_main
msgpack_test_SOURCES = msgpack_test.cpp
msgpack_test_LDADD = libmsgpack.la -lgtest_main
TESTS = $(check_PROGRAMS)
doxygen:
./preprocess clean
cd src && $(MAKE) doxygen
./preprocess

View File

@@ -13,9 +13,10 @@ On UNIX-like platform, run ./configure && make && sudo make install:
$ make
$ sudo make install
On Windows, open msgpack_vc8.vcproj file and build it using batch build. DLLs are built on lib folder, and the headers are built on include folder.
On Windows, open msgpack_vc8.vcproj or msgpack_vc2008 file and build it using batch build. DLLs are built on lib folder,
and the headers are built on include folder.
To use the library in your program, include msgpack.hpp header and link msgpack and msgpackc library.
To use the library in your program, include msgpack.hpp header and link "msgpack" library.
## Example
@@ -34,15 +35,9 @@ To use the library in your program, include msgpack.hpp header and link msgpack
msgpack::pack(&buffer, target);
// Deserialize the serialized data.
msgpack::zone mempool; // this manages the life of deserialized object
msgpack::object obj;
msgpack::unpack_return ret =
msgpack::unpack(buffer.data, buffer.size, NULL, &mempool, &obj);
if(ret != msgapck::UNPACK_SUCCESS) {
// error check
exit(1);
}
msgpack::unpacked msg; // includes memory pool and deserialized object
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
msgpack::object obj = msg.get();
// Print the deserialized object to stdout.
std::cout << obj << std::endl; // ["Hello," "World!"]
@@ -55,7 +50,7 @@ To use the library in your program, include msgpack.hpp header and link msgpack
obj.as<int>(); // type is mismatched, msgpack::type_error is thrown
}
API document and other example codes are available at the [wiki.](http://msgpack.sourceforge.net/start)
API documents and other example codes are available at the [wiki.](http://redmine.msgpack.org/projects/msgpack/wiki)
## License

View File

@@ -1,6 +1,6 @@
AC_INIT(object.cpp)
AC_INIT(src/object.cpp)
AC_CONFIG_AUX_DIR(ac)
AM_INIT_AUTOMAKE(msgpack, 0.5.0)
AM_INIT_AUTOMAKE(msgpack, 0.5.4)
AC_CONFIG_HEADER(config.h)
AC_SUBST(CFLAGS)
@@ -9,17 +9,39 @@ CFLAGS="-O4 -Wall $CFLAGS"
AC_SUBST(CXXFLAGS)
CXXFLAGS="-O4 -Wall $CXXFLAGS"
AC_PROG_CC
AC_MSG_CHECKING([if C++ API is enabled])
AC_ARG_ENABLE(cxx,
AS_HELP_STRING([--disable-cxx],
[don't build C++ API]) )
AC_MSG_RESULT([$enable_cxx])
if test "$enable_cxx" != "no"; then
AC_PROG_CXX
AM_PROG_CC_C_O
fi
AM_CONDITIONAL(ENABLE_CXX, test "$enable_cxx" != "no")
AC_PROG_LIBTOOL
AM_PROG_AS
AM_PROG_CC_C_O
AC_LANG_PUSH([C++])
AC_CHECK_HEADERS(tr1/unordered_map)
AC_CHECK_HEADERS(tr1/unordered_set)
AC_LANG_POP([C++])
AC_MSG_CHECKING([if debug option is enabled])
AC_ARG_ENABLE(debug,
AS_HELP_STRING([--disable-debug],
[disable assert macros and omit -g option]) )
AC_MSG_RESULT([$enable_debug])
if test "$enable_debug" != "no"; then
CXXFLAGS="$CXXFLAGS -g"
CFLAGS="$CFLAGS -g"
else
CXXFLAGS="$CXXFLAGS -DNDEBUG"
CFLAGS="$CFLAGS -DNDEBUG"
fi
AC_CACHE_CHECK([for __sync_* atomic operations], msgpack_cv_atomic_ops, [
AC_TRY_LINK([
@@ -39,5 +61,15 @@ add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as f
])
fi
AC_OUTPUT([Makefile test/Makefile])
major=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\1/'`
minor=`echo $VERSION | sed 's/\([[0-9]]*\)\.\([[0-9]]*\).*/\2/'`
AC_SUBST(VERSION_MAJOR, $major)
AC_SUBST(VERSION_MINOR, $minor)
AC_OUTPUT([Makefile
src/Makefile
src/msgpack/version.h
test/Makefile])

View File

@@ -1,15 +0,0 @@
#include "msgpack/type/bool.hpp"
#include "msgpack/type/float.hpp"
#include "msgpack/type/int.hpp"
#include "msgpack/type/list.hpp"
#include "msgpack/type/deque.hpp"
#include "msgpack/type/map.hpp"
#include "msgpack/type/nil.hpp"
#include "msgpack/type/pair.hpp"
#include "msgpack/type/raw.hpp"
#include "msgpack/type/set.hpp"
#include "msgpack/type/string.hpp"
#include "msgpack/type/vector.hpp"
#include "msgpack/type/tuple.hpp"
#include "msgpack/type/define.hpp"

View File

@@ -1,123 +0,0 @@
/*
* MessagePack for C unpacking 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.
*/
#ifndef MSGPACK_UNPACKER_H__
#define MSGPACK_UNPACKER_H__
#include "msgpack/zone.h"
#include "msgpack/object.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct msgpack_unpacker {
char* buffer;
size_t used;
size_t free;
size_t off;
size_t parsed;
msgpack_zone* z;
size_t initial_buffer_size;
void* ctx;
} msgpack_unpacker;
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
void msgpack_unpacker_free(msgpack_unpacker* mpac);
static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac);
static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size);
int msgpack_unpacker_execute(msgpack_unpacker* mpac);
msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac);
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac);
void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac);
void msgpack_unpacker_reset(msgpack_unpacker* mpac);
static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
typedef enum {
MSGPACK_UNPACK_SUCCESS = 2,
MSGPACK_UNPACK_EXTRA_BYTES = 1,
MSGPACK_UNPACK_CONTINUE = 0,
MSGPACK_UNPACK_PARSE_ERROR = -1,
} msgpack_unpack_return;
msgpack_unpack_return
msgpack_unpack(const char* data, size_t len, size_t* off,
msgpack_zone* z, msgpack_object* result);
static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac);
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
{
if(mpac->free >= size) { return true; }
return msgpack_unpacker_expand_buffer(mpac, size);
}
char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
{
return mpac->buffer + mpac->used;
}
size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)
{
return mpac->free;
}
void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
{
mpac->used += size;
mpac->free -= size;
}
size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac)
{
return mpac->parsed - mpac->off + mpac->used;
}
size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
{
return mpac->parsed;
}
#ifdef __cplusplus
}
#endif
#endif /* msgpack/unpack.h */

View File

@@ -0,0 +1,45 @@
IF NOT EXIST include MKDIR include
IF NOT EXIST include\msgpack MKDIR include\msgpack
IF NOT EXIST include\msgpack\type MKDIR include\msgpack\type
IF NOT EXIST include\msgpack\type\tr1 MKDIR include\msgpack\type\tr1
copy src\msgpack\pack_define.h include\msgpack\
copy src\msgpack\pack_template.h include\msgpack\
copy src\msgpack\unpack_define.h include\msgpack\
copy src\msgpack\unpack_template.h include\msgpack\
copy src\msgpack\sysdep.h include\msgpack\
copy src\msgpack.h include\
copy src\msgpack\sbuffer.h include\msgpack\
copy src\msgpack\version.h include\msgpack\
copy src\msgpack\vrefbuffer.h include\msgpack\
copy src\msgpack\zbuffer.h include\msgpack\
copy src\msgpack\pack.h include\msgpack\
copy src\msgpack\unpack.h include\msgpack\
copy src\msgpack\object.h include\msgpack\
copy src\msgpack\zone.h include\msgpack\
copy src\msgpack.hpp include\
copy src\msgpack\sbuffer.hpp include\msgpack\
copy src\msgpack\vrefbuffer.hpp include\msgpack\
copy src\msgpack\zbuffer.hpp include\msgpack\
copy src\msgpack\pack.hpp include\msgpack\
copy src\msgpack\unpack.hpp include\msgpack\
copy src\msgpack\object.hpp include\msgpack\
copy src\msgpack\zone.hpp include\msgpack\
copy src\msgpack\type.hpp include\msgpack\type\
copy src\msgpack\type\bool.hpp include\msgpack\type\
copy src\msgpack\type\deque.hpp include\msgpack\type\
copy src\msgpack\type\fixint.hpp include\msgpack\type\
copy src\msgpack\type\float.hpp include\msgpack\type\
copy src\msgpack\type\int.hpp include\msgpack\type\
copy src\msgpack\type\list.hpp include\msgpack\type\
copy src\msgpack\type\map.hpp include\msgpack\type\
copy src\msgpack\type\nil.hpp include\msgpack\type\
copy src\msgpack\type\pair.hpp include\msgpack\type\
copy src\msgpack\type\raw.hpp include\msgpack\type\
copy src\msgpack\type\set.hpp include\msgpack\type\
copy src\msgpack\type\string.hpp include\msgpack\type\
copy src\msgpack\type\vector.hpp include\msgpack\type\
copy src\msgpack\type\tuple.hpp include\msgpack\type\
copy src\msgpack\type\define.hpp include\msgpack\type\
copy src\msgpack\type\tr1\unordered_map.hpp include\msgpack\type\
copy src\msgpack\type\tr1\unordered_set.hpp include\msgpack\type\

View File

@@ -1,43 +0,0 @@
IF NOT EXIST include MKDIR include
IF NOT EXIST include\msgpack MKDIR include\msgpack
IF NOT EXIST include\msgpack\type MKDIR include\msgpack\type
IF NOT EXIST include\msgpack\type\tr1 MKDIR include\msgpack\type\tr1
copy msgpack\pack_define.h include\msgpack\
copy msgpack\pack_template.h include\msgpack\
copy msgpack\unpack_define.h include\msgpack\
copy msgpack\unpack_template.h include\msgpack\
copy msgpack\sysdep.h include\msgpack\
copy msgpack.h include\
copy msgpack\sbuffer.h include\msgpack\
copy msgpack\vrefbuffer.h include\msgpack\
copy msgpack\zbuffer.h include\msgpack\
copy msgpack\pack.h include\msgpack\
copy msgpack\unpack.h include\msgpack\
copy msgpack\object.h include\msgpack\
copy msgpack\zone.h include\msgpack\
copy msgpack.hpp include\
copy msgpack\sbuffer.hpp include\msgpack\
copy msgpack\vrefbuffer.hpp include\msgpack\
copy msgpack\zbuffer.hpp include\msgpack\
copy msgpack\pack.hpp include\msgpack\
copy msgpack\unpack.hpp include\msgpack\
copy msgpack\object.hpp include\msgpack\
copy msgpack\zone.hpp include\msgpack\
copy msgpack\type.hpp include\msgpack\type\
copy msgpack\type\bool.hpp include\msgpack\type\
copy msgpack\type\float.hpp include\msgpack\type\
copy msgpack\type\int.hpp include\msgpack\type\
copy msgpack\type\list.hpp include\msgpack\type\
copy msgpack\type\deque.hpp include\msgpack\type\
copy msgpack\type\map.hpp include\msgpack\type\
copy msgpack\type\nil.hpp include\msgpack\type\
copy msgpack\type\pair.hpp include\msgpack\type\
copy msgpack\type\raw.hpp include\msgpack\type\
copy msgpack\type\set.hpp include\msgpack\type\
copy msgpack\type\string.hpp include\msgpack\type\
copy msgpack\type\vector.hpp include\msgpack\type\
copy msgpack\type\tuple.hpp include\msgpack\type\
copy msgpack\type\define.hpp include\msgpack\type\
copy msgpack\type\tr1\unordered_map.hpp include\msgpack\type\
copy msgpack\type\tr1\unordered_set.hpp include\msgpack\type\

View File

@@ -28,7 +28,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Gathering header files"
CommandLine="msgpack_vc8.postbuild.bat"
CommandLine="msgpack_vc.postbuild.bat"
Outputs="include"
/>
<Tool
@@ -96,7 +96,7 @@
<Tool
Name="VCCustomBuildTool"
Description="Gathering header files"
CommandLine="msgpack_vc8.postbuild.bat"
CommandLine="msgpack_vc.postbuild.bat"
Outputs="include"
/>
<Tool
@@ -157,7 +157,7 @@
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\objectc.c"
RelativePath=".\src\objectc.c"
>
<FileConfiguration
Name="Debug|Win32"
@@ -177,7 +177,7 @@
</FileConfiguration>
</File>
<File
RelativePath=".\unpack.c"
RelativePath=".\src\unpack.c"
>
<FileConfiguration
Name="Debug|Win32"
@@ -197,7 +197,7 @@
</FileConfiguration>
</File>
<File
RelativePath=".\vrefbuffer.c"
RelativePath=".\src\version.c"
>
<FileConfiguration
Name="Debug|Win32"
@@ -217,7 +217,7 @@
</FileConfiguration>
</File>
<File
RelativePath=".\zone.c"
RelativePath=".\src\vrefbuffer.c"
>
<FileConfiguration
Name="Debug|Win32"
@@ -237,7 +237,27 @@
</FileConfiguration>
</File>
<File
RelativePath=".\object.cpp"
RelativePath=".\src\zone.c"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
CompileAs="2"
/>
</FileConfiguration>
<FileConfiguration
Name="Release|Win32"
>
<Tool
Name="VCCLCompilerTool"
CompileAs="2"
/>
</FileConfiguration>
</File>
<File
RelativePath=".\src\object.cpp"
>
</File>
</Filter>
@@ -247,23 +267,23 @@
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\msgpack\pack_define.h"
RelativePath=".\src\msgpack\pack_define.h"
>
</File>
<File
RelativePath=".\msgpack\pack_template.h"
RelativePath=".\src\msgpack\pack_template.h"
>
</File>
<File
RelativePath=".\msgpack\sysdep.h"
RelativePath=".\src\msgpack\sysdep.h"
>
</File>
<File
RelativePath=".\msgpack\unpack_define.h"
RelativePath=".\src\msgpack\unpack_define.h"
>
</File>
<File
RelativePath=".\msgpack\unpack_template.h"
RelativePath=".\src\msgpack\unpack_template.h"
>
</File>
</Filter>

View File

@@ -6,17 +6,29 @@ preprocess() {
echo ""
echo "** preprocess failed **"
echo ""
exit 1
else
mv $1.tmp $1
fi
}
preprocess msgpack/type/tuple.hpp
preprocess msgpack/type/define.hpp
preprocess msgpack/zone.hpp
cp -f ../msgpack/sysdep.h msgpack/
cp -f ../msgpack/pack_define.h msgpack/
cp -f ../msgpack/pack_template.h msgpack/
cp -f ../msgpack/unpack_define.h msgpack/
cp -f ../msgpack/unpack_template.h msgpack/
if [ "$1" == "clean" ];then
rm -f src/msgpack/type/tuple.hpp
rm -f src/msgpack/type/define.hpp
rm -f src/msgpack/zone.hpp
else
preprocess src/msgpack/type/tuple.hpp
preprocess src/msgpack/type/define.hpp
preprocess src/msgpack/zone.hpp
fi
cp -f ../msgpack/sysdep.h src/msgpack/
cp -f ../msgpack/pack_define.h src/msgpack/
cp -f ../msgpack/pack_template.h src/msgpack/
cp -f ../msgpack/unpack_define.h src/msgpack/
cp -f ../msgpack/unpack_template.h src/msgpack/
cp -f ../test/cases.mpac test/
cp -f ../test/cases_compact.mpac test/
sed -e 's/8\.00/9.00/' < msgpack_vc8.vcproj > msgpack_vc2008.vcproj
sed -e 's/9\.00/10.00/' -e 's/msgpack_vc8/msgpack_vc2008/' < msgpack_vc8.sln > msgpack_vc2008.sln

101
cpp/src/Makefile.am Normal file
View File

@@ -0,0 +1,101 @@
lib_LTLIBRARIES = libmsgpack.la
libmsgpack_la_SOURCES = \
unpack.c \
objectc.c \
version.c \
vrefbuffer.c \
zone.c
if ENABLE_CXX
libmsgpack_la_SOURCES += \
object.cpp
endif
# -version-info CURRENT:REVISION:AGE
libmsgpack_la_LDFLAGS = -version-info 3:0:0
# backward compatibility
lib_LTLIBRARIES += libmsgpackc.la
libmsgpackc_la_SOURCES = \
unpack.c \
objectc.c \
version.c \
vrefbuffer.c \
zone.c
libmsgpackc_la_LDFLAGS = -version-info 2:0:0
nobase_include_HEADERS = \
msgpack/pack_define.h \
msgpack/pack_template.h \
msgpack/unpack_define.h \
msgpack/unpack_template.h \
msgpack/sysdep.h \
msgpack.h \
msgpack/sbuffer.h \
msgpack/version.h \
msgpack/vrefbuffer.h \
msgpack/zbuffer.h \
msgpack/pack.h \
msgpack/unpack.h \
msgpack/object.h \
msgpack/zone.h
if ENABLE_CXX
nobase_include_HEADERS += \
msgpack.hpp \
msgpack/sbuffer.hpp \
msgpack/vrefbuffer.hpp \
msgpack/zbuffer.hpp \
msgpack/pack.hpp \
msgpack/unpack.hpp \
msgpack/object.hpp \
msgpack/zone.hpp \
msgpack/type.hpp \
msgpack/type/bool.hpp \
msgpack/type/deque.hpp \
msgpack/type/float.hpp \
msgpack/type/fixint.hpp \
msgpack/type/int.hpp \
msgpack/type/list.hpp \
msgpack/type/map.hpp \
msgpack/type/nil.hpp \
msgpack/type/pair.hpp \
msgpack/type/raw.hpp \
msgpack/type/set.hpp \
msgpack/type/string.hpp \
msgpack/type/vector.hpp \
msgpack/type/tuple.hpp \
msgpack/type/define.hpp \
msgpack/type/tr1/unordered_map.hpp \
msgpack/type/tr1/unordered_set.hpp
endif
EXTRA_DIST = \
msgpack/version.h.in \
msgpack/zone.hpp.erb \
msgpack/type/define.hpp.erb \
msgpack/type/tuple.hpp.erb
doxygen_c:
cat ../Doxyfile > Doxyfile_c
echo "FILE_PATTERNS = *.h" >> Doxyfile_c
echo "OUTPUT_DIRECTORY = doc_c" >> Doxyfile_c
echo "PROJECT_NAME = \"MessagePack for C\"" >> Doxyfile_c
doxygen Doxyfile_c
doxygen_cpp:
cat ../Doxyfile > Doxyfile_cpp
echo "FILE_PATTERNS = *.hpp" >> Doxyfile_cpp
echo "OUTPUT_DIRECTORY = doc_cpp" >> Doxyfile_cpp
echo "PROJECT_NAME = \"MessagePack for C++\"" >> Doxyfile_cpp
doxygen Doxyfile_cpp
doxygen: doxygen_c doxygen_cpp

View File

@@ -15,9 +15,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @defgroup msgpack MessagePack C
* @{
* @}
*/
#include "msgpack/object.h"
#include "msgpack/zone.h"
#include "msgpack/pack.h"
#include "msgpack/unpack.h"
#include "msgpack/sbuffer.h"
#include "msgpack/vrefbuffer.h"
#include "msgpack/version.h"

View File

@@ -18,7 +18,7 @@
#ifndef MSGPACK_OBJECT_H__
#define MSGPACK_OBJECT_H__
#include "msgpack/zone.h"
#include "zone.h"
#include <stdio.h>
#ifdef __cplusplus
@@ -26,6 +26,12 @@ extern "C" {
#endif
/**
* @defgroup msgpack_object Dynamically typed object
* @ingroup msgpack
* @{
*/
typedef enum {
MSGPACK_OBJECT_NIL = 0x00,
MSGPACK_OBJECT_BOOLEAN = 0x01,
@@ -81,6 +87,8 @@ void msgpack_object_print(FILE* out, msgpack_object o);
bool msgpack_object_equal(const msgpack_object x, const msgpack_object y);
/** @} */
#ifdef __cplusplus
}

View File

@@ -18,9 +18,9 @@
#ifndef MSGPACK_OBJECT_HPP__
#define MSGPACK_OBJECT_HPP__
#include "msgpack/object.h"
#include "msgpack/pack.hpp"
#include "msgpack/zone.hpp"
#include "object.h"
#include "pack.hpp"
#include "zone.hpp"
#include <string.h>
#include <stdexcept>
#include <typeinfo>

View File

@@ -18,8 +18,8 @@
#ifndef MSGPACK_PACK_H__
#define MSGPACK_PACK_H__
#include "msgpack/pack_define.h"
#include "msgpack/object.h"
#include "pack_define.h"
#include "object.h"
#include <stdlib.h>
#ifdef __cplusplus
@@ -27,6 +27,19 @@ extern "C" {
#endif
/**
* @defgroup msgpack_buffer Buffers
* @ingroup msgpack
* @{
* @}
*/
/**
* @defgroup msgpack_pack Serializer
* @ingroup msgpack
* @{
*/
typedef int (*msgpack_packer_write)(void* data, const char* buf, unsigned int len);
typedef struct msgpack_packer {
@@ -57,6 +70,15 @@ static int msgpack_pack_int16(msgpack_packer* pk, int16_t d);
static int msgpack_pack_int32(msgpack_packer* pk, int32_t d);
static int msgpack_pack_int64(msgpack_packer* pk, int64_t d);
static int msgpack_pack_fix_uint8(msgpack_packer* pk, uint8_t d);
static int msgpack_pack_fix_uint16(msgpack_packer* pk, uint16_t d);
static int msgpack_pack_fix_uint32(msgpack_packer* pk, uint32_t d);
static int msgpack_pack_fix_uint64(msgpack_packer* pk, uint64_t d);
static int msgpack_pack_fix_int8(msgpack_packer* pk, int8_t d);
static int msgpack_pack_fix_int16(msgpack_packer* pk, int16_t d);
static int msgpack_pack_fix_int32(msgpack_packer* pk, int32_t d);
static int msgpack_pack_fix_int64(msgpack_packer* pk, int64_t d);
static int msgpack_pack_float(msgpack_packer* pk, float d);
static int msgpack_pack_double(msgpack_packer* pk, double d);
@@ -74,6 +96,8 @@ static int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l);
int msgpack_pack_object(msgpack_packer* pk, msgpack_object d);
/** @} */
#define msgpack_pack_inline_func(name) \
inline int msgpack_pack ## name
@@ -81,12 +105,18 @@ int msgpack_pack_object(msgpack_packer* pk, msgpack_object d);
#define msgpack_pack_inline_func_cint(name) \
inline int msgpack_pack ## name
#define msgpack_pack_inline_func_cint(name) \
inline int msgpack_pack ## name
#define msgpack_pack_inline_func_fixint(name) \
inline int msgpack_pack_fix ## name
#define msgpack_pack_user msgpack_packer*
#define msgpack_pack_append_buffer(user, buf, len) \
return (*(user)->callback)((user)->data, (const char*)buf, len)
#include "msgpack/pack_template.h"
#include "pack_template.h"
inline void msgpack_packer_init(msgpack_packer* pk, void* data, msgpack_packer_write callback)
{

View File

@@ -18,7 +18,7 @@
#ifndef MSGPACK_PACK_HPP__
#define MSGPACK_PACK_HPP__
#include "msgpack/pack_define.h"
#include "pack_define.h"
#include <stdexcept>
#include <limits.h>
@@ -45,6 +45,15 @@ public:
packer<Stream>& pack_int32(int32_t d);
packer<Stream>& pack_int64(int64_t d);
packer<Stream>& pack_fix_uint8(uint8_t d);
packer<Stream>& pack_fix_uint16(uint16_t d);
packer<Stream>& pack_fix_uint32(uint32_t d);
packer<Stream>& pack_fix_uint64(uint64_t d);
packer<Stream>& pack_fix_int8(int8_t d);
packer<Stream>& pack_fix_int16(int16_t d);
packer<Stream>& pack_fix_int32(int32_t d);
packer<Stream>& pack_fix_int64(int64_t d);
packer<Stream>& pack_short(short d);
packer<Stream>& pack_int(int d);
packer<Stream>& pack_long(long d);
@@ -78,6 +87,15 @@ private:
static void _pack_int32(Stream& x, int32_t d);
static void _pack_int64(Stream& x, int64_t d);
static void _pack_fix_uint8(Stream& x, uint8_t d);
static void _pack_fix_uint16(Stream& x, uint16_t d);
static void _pack_fix_uint32(Stream& x, uint32_t d);
static void _pack_fix_uint64(Stream& x, uint64_t d);
static void _pack_fix_int8(Stream& x, int8_t d);
static void _pack_fix_int16(Stream& x, int16_t d);
static void _pack_fix_int32(Stream& x, int32_t d);
static void _pack_fix_int64(Stream& x, int64_t d);
static void _pack_short(Stream& x, short d);
static void _pack_int(Stream& x, int d);
static void _pack_long(Stream& x, long d);
@@ -133,11 +151,15 @@ inline void pack(Stream& s, const T& v)
template <typename Stream> \
inline void packer<Stream>::_pack ## name
#define msgpack_pack_inline_func_fixint(name) \
template <typename Stream> \
inline void packer<Stream>::_pack_fix ## name
#define msgpack_pack_user Stream&
#define msgpack_pack_append_buffer append_buffer
#include "msgpack/pack_template.h"
#include "pack_template.h"
template <typename Stream>
@@ -149,6 +171,7 @@ packer<Stream>::packer(Stream& s) : m_stream(s) { }
template <typename Stream>
packer<Stream>::~packer() { }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_uint8(uint8_t d)
{ _pack_uint8(m_stream, d); return *this; }
@@ -182,6 +205,39 @@ inline packer<Stream>& packer<Stream>::pack_int64(int64_t d)
{ _pack_int64(m_stream, d); return *this;}
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_uint8(uint8_t d)
{ _pack_fix_uint8(m_stream, d); return *this; }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_uint16(uint16_t d)
{ _pack_fix_uint16(m_stream, d); return *this; }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_uint32(uint32_t d)
{ _pack_fix_uint32(m_stream, d); return *this; }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_uint64(uint64_t d)
{ _pack_fix_uint64(m_stream, d); return *this; }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_int8(int8_t d)
{ _pack_fix_int8(m_stream, d); return *this; }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_int16(int16_t d)
{ _pack_fix_int16(m_stream, d); return *this; }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_int32(int32_t d)
{ _pack_fix_int32(m_stream, d); return *this; }
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_fix_int64(int64_t d)
{ _pack_fix_int64(m_stream, d); return *this;}
template <typename Stream>
inline packer<Stream>& packer<Stream>::pack_short(short d)
{ _pack_short(m_stream, d); return *this; }

View File

@@ -21,15 +21,17 @@
#include <stdlib.h>
#include <string.h>
#ifndef MSGPACK_SBUFFER_INIT_SIZE
#define MSGPACK_SBUFFER_INIT_SIZE 8192
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup msgpack_sbuffer Simple buffer
* @ingroup msgpack_buffer
* @{
*/
typedef struct msgpack_sbuffer {
size_t size;
char* data;
@@ -46,6 +48,22 @@ static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf)
free(sbuf->data);
}
static inline msgpack_sbuffer* msgpack_sbuffer_new(void)
{
return (msgpack_sbuffer*)calloc(1, sizeof(msgpack_sbuffer));
}
static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf)
{
if(sbuf == NULL) { return; }
msgpack_sbuffer_destroy(sbuf);
free(sbuf);
}
#ifndef MSGPACK_SBUFFER_INIT_SIZE
#define MSGPACK_SBUFFER_INIT_SIZE 8192
#endif
static inline int msgpack_sbuffer_write(void* data, const char* buf, unsigned int len)
{
msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;
@@ -77,6 +95,13 @@ static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf)
return tmp;
}
static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf)
{
sbuf->size = 0;
}
/** @} */
#ifdef __cplusplus
}

View File

@@ -18,7 +18,7 @@
#ifndef MSGPACK_SBUFFER_HPP__
#define MSGPACK_SBUFFER_HPP__
#include "msgpack/sbuffer.h"
#include "sbuffer.h"
#include <stdexcept>
namespace msgpack {
@@ -72,6 +72,11 @@ public:
return msgpack_sbuffer_release(this);
}
void clear()
{
msgpack_sbuffer_clear(this);
}
private:
void expand_buffer(size_t len)
{

16
cpp/src/msgpack/type.hpp Normal file
View File

@@ -0,0 +1,16 @@
#include "type/bool.hpp"
#include "type/deque.hpp"
#include "type/fixint.hpp"
#include "type/float.hpp"
#include "type/int.hpp"
#include "type/list.hpp"
#include "type/map.hpp"
#include "type/nil.hpp"
#include "type/pair.hpp"
#include "type/raw.hpp"
#include "type/set.hpp"
#include "type/string.hpp"
#include "type/vector.hpp"
#include "type/tuple.hpp"
#include "type/define.hpp"

View File

@@ -0,0 +1,172 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2020 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.
//
#ifndef MSGPACK_TYPE_FIXINT_HPP__
#define MSGPACK_TYPE_FIXINT_HPP__
#include "msgpack/object.hpp"
#include "msgpack/type/int.hpp"
namespace msgpack {
namespace type {
template <typename T>
struct fix_int {
fix_int() : value(0) { }
fix_int(T value) : value(value) { }
operator T() const { return value; }
T get() const { return value; }
private:
T value;
};
typedef fix_int<uint8_t> fix_uint8;
typedef fix_int<uint16_t> fix_uint16;
typedef fix_int<uint32_t> fix_uint32;
typedef fix_int<uint64_t> fix_uint64;
typedef fix_int<int8_t> fix_int8;
typedef fix_int<int16_t> fix_int16;
typedef fix_int<int32_t> fix_int32;
typedef fix_int<int64_t> fix_int64;
} // namespace type
inline type::fix_int8& operator>> (object o, type::fix_int8& v)
{ v = type::detail::convert_integer<int8_t>(o); return v; }
inline type::fix_int16& operator>> (object o, type::fix_int16& v)
{ v = type::detail::convert_integer<int16_t>(o); return v; }
inline type::fix_int32& operator>> (object o, type::fix_int32& v)
{ v = type::detail::convert_integer<int32_t>(o); return v; }
inline type::fix_int64& operator>> (object o, type::fix_int64& v)
{ v = type::detail::convert_integer<int64_t>(o); return v; }
inline type::fix_uint8& operator>> (object o, type::fix_uint8& v)
{ v = type::detail::convert_integer<uint8_t>(o); return v; }
inline type::fix_uint16& operator>> (object o, type::fix_uint16& v)
{ v = type::detail::convert_integer<uint16_t>(o); return v; }
inline type::fix_uint32& operator>> (object o, type::fix_uint32& v)
{ v = type::detail::convert_integer<uint32_t>(o); return v; }
inline type::fix_uint64& operator>> (object o, type::fix_uint64& v)
{ v = type::detail::convert_integer<uint64_t>(o); return v; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int8& v)
{ o.pack_fix_int8(v); return o; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int16& v)
{ o.pack_fix_int16(v); return o; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int32& v)
{ o.pack_fix_int32(v); return o; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_int64& v)
{ o.pack_fix_int64(v); return o; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint8& v)
{ o.pack_fix_uint8(v); return o; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint16& v)
{ o.pack_fix_uint16(v); return o; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint32& v)
{ o.pack_fix_uint32(v); return o; }
template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const type::fix_uint64& v)
{ o.pack_fix_uint64(v); return o; }
inline void operator<< (object& o, type::fix_int8 v)
{ v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object& o, type::fix_int16 v)
{ v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object& o, type::fix_int32 v)
{ v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object& o, type::fix_int64 v)
{ v.get() < 0 ? o.type = type::NEGATIVE_INTEGER, o.via.i64 = v.get() : o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object& o, type::fix_uint8 v)
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object& o, type::fix_uint16 v)
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object& o, type::fix_uint32 v)
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object& o, type::fix_uint64 v)
{ o.type = type::POSITIVE_INTEGER, o.via.u64 = v.get(); }
inline void operator<< (object::with_zone& o, type::fix_int8 v)
{ static_cast<object&>(o) << v; }
inline void operator<< (object::with_zone& o, type::fix_int16 v)
{ static_cast<object&>(o) << v; }
inline void operator<< (object::with_zone& o, type::fix_int32 v)
{ static_cast<object&>(o) << v; }
inline void operator<< (object::with_zone& o, type::fix_int64 v)
{ static_cast<object&>(o) << v; }
inline void operator<< (object::with_zone& o, type::fix_uint8 v)
{ static_cast<object&>(o) << v; }
inline void operator<< (object::with_zone& o, type::fix_uint16 v)
{ static_cast<object&>(o) << v; }
inline void operator<< (object::with_zone& o, type::fix_uint32 v)
{ static_cast<object&>(o) << v; }
inline void operator<< (object::with_zone& o, type::fix_uint64 v)
{ static_cast<object&>(o) << v; }
} // namespace msgpack
#endif /* msgpack/type/fixint.hpp */

View File

@@ -51,6 +51,14 @@ inline void operator<< (object::with_zone& o, type::nil v)
{ static_cast<object&>(o) << v; }
template <>
inline void object::as<void>() const
{
msgpack::type::nil v;
convert(&v);
}
} // namespace msgpack
#endif /* msgpack/type/nil.hpp */

View File

@@ -33,25 +33,25 @@ struct raw_ref {
uint32_t size;
const char* ptr;
std::string str() { return std::string(ptr, size); }
std::string str() const { return std::string(ptr, size); }
bool operator== (const raw_ref& x)
bool operator== (const raw_ref& x) const
{
return size == x.size && memcmp(ptr, x.ptr, size) == 0;
}
bool operator!= (const raw_ref& x)
bool operator!= (const raw_ref& x) const
{
return !(*this != x);
}
bool operator< (const raw_ref& x)
bool operator< (const raw_ref& x) const
{
if(size == x.size) { return memcmp(ptr, x.ptr, size) < 0; }
else { return size < x.size; }
}
bool operator> (const raw_ref& x)
bool operator> (const raw_ref& x) const
{
if(size == x.size) { return memcmp(ptr, x.ptr, size) > 0; }
else { return size > x.size; }

View File

@@ -141,7 +141,7 @@ type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& operator>> (
if(o.type != type::ARRAY) { throw type_error(); }
if(o.via.array.size < <%=i+1%>) { throw type_error(); }
<%0.upto(i) {|j|%>
o.via.array.ptr[<%=j%>].convert<A<%=j%>>(&v.template get<<%=j%>>());<%}%>
o.via.array.ptr[<%=j%>].convert<typename type::tuple_type<A<%=j%>>::type>(&v.template get<<%=j%>>());<%}%>
return v;
}
<%}%>
@@ -187,5 +187,20 @@ inline void operator<< (
} // namespace msgpack
//inline std::ostream& operator<< (std::ostream& o, const msgpack::type::tuple<>& v) {
// return o << "[]";
//}
//<%0.upto(GENERATION_LIMIT) {|i|%>
//template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
//inline std::ostream& operator<< (std::ostream& o,
// const msgpack::type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) {
// return o << "["
// <%0.upto(i) {|j|%>
// <<<%if j != 0 then%> ", " <<<%end%> v.template get<<%=j%>>()<%}%>
// << "]";
//}
//<%}%>
#endif /* msgpack/type/tuple.hpp */

260
cpp/src/msgpack/unpack.h Normal file
View File

@@ -0,0 +1,260 @@
/*
* MessagePack for C unpacking 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.
*/
#ifndef MSGPACK_UNPACKER_H__
#define MSGPACK_UNPACKER_H__
#include "zone.h"
#include "object.h"
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup msgpack_unpack Deserializer
* @ingroup msgpack
* @{
*/
typedef struct msgpack_unpacked {
msgpack_zone* zone;
msgpack_object data;
} msgpack_unpacked;
bool msgpack_unpack_next(msgpack_unpacked* result,
const char* data, size_t len, size_t* off);
/** @} */
/**
* @defgroup msgpack_unpacker Streaming deserializer
* @ingroup msgpack
* @{
*/
typedef struct msgpack_unpacker {
char* buffer;
size_t used;
size_t free;
size_t off;
size_t parsed;
msgpack_zone* z;
size_t initial_buffer_size;
void* ctx;
} msgpack_unpacker;
#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
#endif
/**
* Initializes a streaming deserializer.
* The initialized deserializer must be destroyed by msgpack_unpacker_destroy(msgpack_unpacker*).
*/
bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
/**
* Destroys a streaming deserializer initialized by msgpack_unpacker_init(msgpack_unpacker*, size_t).
*/
void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
/**
* Creates a streaming deserializer.
* The created deserializer must be destroyed by msgpack_unpacker_free(msgpack_unpacker*).
*/
msgpack_unpacker* msgpack_unpacker_new(size_t initial_buffer_size);
/**
* Frees a streaming deserializer created by msgpack_unpacker_new(size_t).
*/
void msgpack_unpacker_free(msgpack_unpacker* mpac);
#ifndef MSGPACK_UNPACKER_RESERVE_SIZE
#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
#endif
/**
* Reserves free space of the internal buffer.
* Use this function to fill the internal buffer with
* msgpack_unpacker_buffer(msgpack_unpacker*),
* msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and
* msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
*/
static inline bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size);
/**
* Gets pointer to the free space of the internal buffer.
* Use this function to fill the internal buffer with
* msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
* msgpack_unpacker_buffer_capacity(const msgpack_unpacker*) and
* msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
*/
static inline char* msgpack_unpacker_buffer(msgpack_unpacker* mpac);
/**
* Gets size of the free space of the internal buffer.
* Use this function to fill the internal buffer with
* msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
* msgpack_unpacker_buffer(const msgpack_unpacker*) and
* msgpack_unpacker_buffer_consumed(msgpack_unpacker*).
*/
static inline size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac);
/**
* Notifies the deserializer that the internal buffer filled.
* Use this function to fill the internal buffer with
* msgpack_unpacker_reserve_buffer(msgpack_unpacker*, size_t),
* msgpack_unpacker_buffer(msgpack_unpacker*) and
* msgpack_unpacker_buffer_capacity(const msgpack_unpacker*).
*/
static inline void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size);
/**
* Deserializes one object.
* Returns true if it successes. Otherwise false is returned.
* @param pac pointer to an initialized msgpack_unpacked object.
*/
bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac);
/**
* Initializes a msgpack_unpacked object.
* The initialized object must be destroyed by msgpack_unpacked_destroy(msgpack_unpacker*).
* Use the object with msgpack_unpacker_next(msgpack_unpacker*, msgpack_unpacked*) or
* msgpack_unpack_next(msgpack_unpacked*, const char*, size_t, size_t*).
*/
static inline void msgpack_unpacked_init(msgpack_unpacked* result);
/**
* Destroys a streaming deserializer initialized by msgpack_unpacked().
*/
static inline void msgpack_unpacked_destroy(msgpack_unpacked* result);
/**
* Releases the memory zone from msgpack_unpacked object.
* The released zone must be freed by msgpack_zone_free(msgpack_zone*).
*/
static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result);
int msgpack_unpacker_execute(msgpack_unpacker* mpac);
msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac);
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac);
void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac);
void msgpack_unpacker_reset(msgpack_unpacker* mpac);
static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
/** @} */
// obsolete
typedef enum {
MSGPACK_UNPACK_SUCCESS = 2,
MSGPACK_UNPACK_EXTRA_BYTES = 1,
MSGPACK_UNPACK_CONTINUE = 0,
MSGPACK_UNPACK_PARSE_ERROR = -1,
} msgpack_unpack_return;
// obsolete
msgpack_unpack_return
msgpack_unpack(const char* data, size_t len, size_t* off,
msgpack_zone* result_zone, msgpack_object* result);
static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
bool msgpack_unpacker_flush_zone(msgpack_unpacker* mpac);
bool msgpack_unpacker_expand_buffer(msgpack_unpacker* mpac, size_t size);
bool msgpack_unpacker_reserve_buffer(msgpack_unpacker* mpac, size_t size)
{
if(mpac->free >= size) { return true; }
return msgpack_unpacker_expand_buffer(mpac, size);
}
char* msgpack_unpacker_buffer(msgpack_unpacker* mpac)
{
return mpac->buffer + mpac->used;
}
size_t msgpack_unpacker_buffer_capacity(const msgpack_unpacker* mpac)
{
return mpac->free;
}
void msgpack_unpacker_buffer_consumed(msgpack_unpacker* mpac, size_t size)
{
mpac->used += size;
mpac->free -= size;
}
size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac)
{
return mpac->parsed - mpac->off + mpac->used;
}
size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
{
return mpac->parsed;
}
void msgpack_unpacked_init(msgpack_unpacked* result)
{
memset(result, 0, sizeof(msgpack_unpacked));
}
void msgpack_unpacked_destroy(msgpack_unpacked* result)
{
if(result->zone != NULL) {
msgpack_zone_free(result->zone);
result->zone = NULL;
memset(&result->data, 0, sizeof(msgpack_object));
}
}
msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result)
{
if(result->zone != NULL) {
msgpack_zone* z = result->zone;
result->zone = NULL;
return z;
}
return NULL;
}
#ifdef __cplusplus
}
#endif
#endif /* msgpack/unpack.h */

View File

@@ -18,14 +18,15 @@
#ifndef MSGPACK_UNPACK_HPP__
#define MSGPACK_UNPACK_HPP__
#include "msgpack/unpack.h"
#include "msgpack/object.hpp"
#include "msgpack/zone.hpp"
#include "unpack.h"
#include "object.hpp"
#include "zone.hpp"
#include <memory>
#include <stdexcept>
// backward compatibility
#ifndef MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE
#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE (32*1024)
#define MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE MSGPACK_UNPACKER_INIT_BUFFER_SIZE
#endif
namespace msgpack {
@@ -64,12 +65,12 @@ private:
class unpacker : public msgpack_unpacker {
public:
unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_DEFAULT_INITIAL_BUFFER_SIZE);
unpacker(size_t init_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
~unpacker();
public:
/*! 1. reserve buffer. at least `size' bytes of capacity will be ready */
void reserve_buffer(size_t size);
void reserve_buffer(size_t size = MSGPACK_UNPACKER_RESERVE_SIZE);
/*! 2. read data to the buffer() up to buffer_capacity() bytes */
char* buffer();
@@ -160,7 +161,7 @@ private:
};
static bool unpack(unpacked* result,
static void unpack(unpacked* result,
const char* data, size_t len, size_t* offset = NULL);
@@ -312,7 +313,7 @@ inline void unpacker::remove_nonparsed_buffer()
}
inline bool unpack(unpacked* result,
inline void unpack(unpacked* result,
const char* data, size_t len, size_t* offset)
{
msgpack::object obj;
@@ -326,12 +327,12 @@ inline bool unpack(unpacked* result,
case UNPACK_SUCCESS:
result->get() = obj;
result->zone() = z;
return false;
return;
case UNPACK_EXTRA_BYTES:
result->get() = obj;
result->zone() = z;
return true;
return;
case UNPACK_CONTINUE:
throw unpack_error("insufficient bytes");

View File

@@ -0,0 +1,40 @@
/*
* MessagePack for C version information
*
* 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.
*/
#ifndef MSGPACK_VERSION_H__
#define MSGPACK_VERSION_H__
#ifdef __cplusplus
extern "C" {
#endif
const char* msgpack_version(void);
int msgpack_version_major(void);
int msgpack_version_minor(void);
#define MSGPACK_VERSION "@VERSION@"
#define MSGPACK_VERSION_MAJOR @VERSION_MAJOR@
#define MSGPACK_VERSION_MINOR @VERSION_MINOR@
#ifdef __cplusplus
}
#endif
#endif /* msgpack/version.h */

View File

@@ -18,7 +18,8 @@
#ifndef MSGPACK_VREFBUFFER_H__
#define MSGPACK_VREFBUFFER_H__
#include "msgpack/zone.h"
#include "zone.h"
#include <stdlib.h>
#ifndef _WIN32
#include <sys/uio.h>
@@ -29,19 +30,17 @@ struct iovec {
};
#endif
#ifndef MSGPACK_VREFBUFFER_REF_SIZE
#define MSGPACK_VREFBUFFER_REF_SIZE 32
#endif
#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup msgpack_vrefbuffer Vectored Referencing buffer
* @ingroup msgpack_buffer
* @{
*/
struct msgpack_vrefbuffer_chunk;
typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk;
@@ -63,10 +62,21 @@ typedef struct msgpack_vrefbuffer {
} msgpack_vrefbuffer;
#ifndef MSGPACK_VREFBUFFER_REF_SIZE
#define MSGPACK_VREFBUFFER_REF_SIZE 32
#endif
#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
#endif
bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
size_t ref_size, size_t chunk_size);
void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf);
static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size);
static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf);
static inline int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len);
static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref);
@@ -80,6 +90,28 @@ int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref);
/** @} */
msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
{
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer));
if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) {
free(vbuf);
return NULL;
}
return vbuf;
}
void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
{
if(vbuf == NULL) { return; }
msgpack_vrefbuffer_destroy(vbuf);
free(vbuf);
}
int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len)
{
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;

View File

@@ -18,7 +18,7 @@
#ifndef MSGPACK_VREFBUFFER_HPP__
#define MSGPACK_VREFBUFFER_HPP__
#include "msgpack/vrefbuffer.h"
#include "vrefbuffer.h"
#include <stdexcept>
namespace msgpack {
@@ -78,6 +78,11 @@ public:
}
}
void clear()
{
msgpack_vrefbuffer_clear(this);
}
private:
typedef msgpack_vrefbuffer base;

View File

@@ -18,35 +18,39 @@
#ifndef MSGPACK_ZBUFFER_H__
#define MSGPACK_ZBUFFER_H__
#include "msgpack/sysdep.h"
#include "sysdep.h"
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#ifndef MSGPACK_ZBUFFER_INIT_SIZE
#define MSGPACK_ZBUFFER_INIT_SIZE 8192
#endif
#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE
#define MSGPACK_ZBUFFER_RESERVE_SIZE 512
#endif
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup msgpack_zbuffer Compressed buffer
* @ingroup msgpack_buffer
* @{
*/
typedef struct msgpack_zbuffer {
z_stream stream;
char* data;
size_t init_size;
} msgpack_zbuffer;
#ifndef MSGPACK_ZBUFFER_INIT_SIZE
#define MSGPACK_ZBUFFER_INIT_SIZE 8192
#endif
static inline bool msgpack_zbuffer_init(msgpack_zbuffer* zbuf,
int level, size_t init_size);
static inline void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf);
static inline msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size);
static inline void msgpack_zbuffer_free(msgpack_zbuffer* zbuf);
static inline char* msgpack_zbuffer_flush(msgpack_zbuffer* zbuf);
static inline const char* msgpack_zbuffer_data(const msgpack_zbuffer* zbuf);
@@ -57,6 +61,10 @@ static inline void msgpack_zbuffer_reset_buffer(msgpack_zbuffer* zbuf);
static inline char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf);
#ifndef MSGPACK_ZBUFFER_RESERVE_SIZE
#define MSGPACK_ZBUFFER_RESERVE_SIZE 512
#endif
static inline int msgpack_zbuffer_write(void* data, const char* buf, unsigned int len);
static inline bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf);
@@ -80,6 +88,23 @@ void msgpack_zbuffer_destroy(msgpack_zbuffer* zbuf)
free(zbuf->data);
}
msgpack_zbuffer* msgpack_zbuffer_new(int level, size_t init_size)
{
msgpack_zbuffer* zbuf = (msgpack_zbuffer*)malloc(sizeof(msgpack_zbuffer));
if(!msgpack_zbuffer_init(zbuf, level, init_size)) {
free(zbuf);
return NULL;
}
return zbuf;
}
void msgpack_zbuffer_free(msgpack_zbuffer* zbuf)
{
if(zbuf == NULL) { return; }
msgpack_zbuffer_destroy(zbuf);
free(zbuf);
}
bool msgpack_zbuffer_expand(msgpack_zbuffer* zbuf)
{
size_t used = (char*)zbuf->stream.next_out - zbuf->data;
@@ -171,6 +196,8 @@ char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf)
return tmp;
}
/** @} */
#ifdef __cplusplus
}

View File

@@ -18,7 +18,7 @@
#ifndef MSGPACK_ZBUFFER_HPP__
#define MSGPACK_ZBUFFER_HPP__
#include "msgpack/zbuffer.h"
#include "zbuffer.h"
#include <stdexcept>
namespace msgpack {

View File

@@ -18,13 +18,19 @@
#ifndef MSGPACK_ZONE_H__
#define MSGPACK_ZONE_H__
#include "msgpack/sysdep.h"
#include "sysdep.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup msgpack_zone Memory zone
* @ingroup msgpack
* @{
*/
typedef struct msgpack_zone_finalizer {
void (*func)(void* data);
void* data;
@@ -71,6 +77,7 @@ bool msgpack_zone_is_empty(msgpack_zone* zone);
void msgpack_zone_clear(msgpack_zone* zone);
/** @} */
#ifndef MSGPACK_ZONE_ALIGN

View File

@@ -18,7 +18,7 @@
#ifndef MSGPACK_ZONE_HPP__
#define MSGPACK_ZONE_HPP__
#include "msgpack/zone.h"
#include "zone.h"
#include <cstdlib>
#include <memory>
#include <vector>

View File

@@ -323,7 +323,7 @@ msgpack_object msgpack_unpacker_data(msgpack_unpacker* mpac)
msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac)
{
if(!msgpack_unpacker_flush_zone(mpac)) {
return false;
return NULL;
}
msgpack_zone* r = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
@@ -363,20 +363,46 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac)
mpac->parsed = 0;
}
bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* result)
{
if(result->zone != NULL) {
msgpack_zone_free(result->zone);
}
int ret = msgpack_unpacker_execute(mpac);
if(ret <= 0) {
result->zone = NULL;
memset(&result->data, 0, sizeof(msgpack_object));
return false;
}
result->zone = msgpack_unpacker_release_zone(mpac);
result->data = msgpack_unpacker_data(mpac);
msgpack_unpacker_reset(mpac);
return true;
}
msgpack_unpack_return
msgpack_unpack(const char* data, size_t len, size_t* off,
msgpack_zone* z, msgpack_object* result)
msgpack_zone* result_zone, msgpack_object* result)
{
size_t noff = 0;
if(off != NULL) { noff = *off; }
if(len <= noff) {
// FIXME
return MSGPACK_UNPACK_CONTINUE;
}
template_context ctx;
template_init(&ctx);
ctx.user.z = z;
ctx.user.z = result_zone;
ctx.user.referenced = false;
size_t noff = 0;
if(off != NULL) { noff = *off; }
int e = template_execute(&ctx, data, len, &noff);
if(e < 0) {
return MSGPACK_UNPACK_PARSE_ERROR;
@@ -397,3 +423,37 @@ msgpack_unpack(const char* data, size_t len, size_t* off,
return MSGPACK_UNPACK_SUCCESS;
}
bool msgpack_unpack_next(msgpack_unpacked* result,
const char* data, size_t len, size_t* off)
{
msgpack_unpacked_destroy(result);
size_t noff = 0;
if(off != NULL) { noff = *off; }
if(len <= noff) {
return false;
}
msgpack_zone* z = msgpack_zone_new(MSGPACK_ZONE_CHUNK_SIZE);
template_context ctx;
template_init(&ctx);
ctx.user.z = z;
ctx.user.referenced = false;
int e = template_execute(&ctx, data, len, &noff);
if(e <= 0) {
msgpack_zone_free(z);
return false;
}
if(off != NULL) { *off = noff; }
result->zone = z;
result->data = template_data(&ctx);
return true;
}

17
cpp/src/version.c Normal file
View File

@@ -0,0 +1,17 @@
#include "msgpack.h"
const char* msgpack_version(void)
{
return MSGPACK_VERSION;
}
int msgpack_version_major(void)
{
return MSGPACK_VERSION_MAJOR;
}
int msgpack_version_minor(void)
{
return MSGPACK_VERSION_MINOR;
}

View File

@@ -75,6 +75,25 @@ void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf)
free(vbuf->array);
}
void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vbuf)
{
msgpack_vrefbuffer_chunk* c = vbuf->inner_buffer.head->next;
msgpack_vrefbuffer_chunk* n;
while(c != NULL) {
n = c->next;
free(c);
c = n;
}
msgpack_vrefbuffer_inner_buffer* const ib = &vbuf->inner_buffer;
msgpack_vrefbuffer_chunk* chunk = ib->head;
chunk->next = NULL;
ib->free = vbuf->chunk_size;
ib->ptr = ((char*)chunk) + sizeof(msgpack_vrefbuffer_chunk);
vbuf->tail = vbuf->array;
}
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
const char* buf, unsigned int len)
{

View File

@@ -204,7 +204,7 @@ msgpack_zone* msgpack_zone_new(size_t chunk_size)
if(!init_chunk_list(&zone->chunk_list, chunk_size)) {
free(zone);
return false;
return NULL;
}
init_finalizer_array(&zone->finalizer_array);
@@ -214,6 +214,7 @@ msgpack_zone* msgpack_zone_new(size_t chunk_size)
void msgpack_zone_free(msgpack_zone* zone)
{
if(zone == NULL) { return; }
msgpack_zone_destroy(zone);
free(zone);
}

View File

@@ -1,9 +0,0 @@
CXXFLAGS += -Wall -g -I. -I.. -O4
LDFLAGS +=
all: test
test: test.o unpack.o zone.o object.o pack.hpp unpack.hpp zone.hpp object.hpp
$(CXX) test.o unpack.o zone.o object.o $(CXXFLAGS) $(LDFLAGS) -o $@

View File

@@ -1,15 +1,23 @@
AM_CPPFLAGS = -I..
AM_C_CPPFLAGS = -I..
AM_LDFLAGS = ../libmsgpack.la -lgtest_main
AM_CPPFLAGS = -I../src
AM_C_CPPFLAGS = -I../src
AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main
check_PROGRAMS = \
zone \
pack_unpack \
pack_unpack_c \
streaming \
streaming_c \
object \
convert \
buffer
buffer \
cases \
fixint \
fixint_c \
version \
msgpackc_test \
msgpack_test
TESTS = $(check_PROGRAMS)
@@ -17,8 +25,12 @@ zone_SOURCES = zone.cc
pack_unpack_SOURCES = pack_unpack.cc
pack_unpack_c_SOURCES = pack_unpack_c.cc
streaming_SOURCES = streaming.cc
streaming_c_SOURCES = streaming_c.cc
object_SOURCES = object.cc
convert_SOURCES = convert.cc
@@ -26,3 +38,17 @@ convert_SOURCES = convert.cc
buffer_SOURCES = buffer.cc
buffer_LDADD = -lz
cases_SOURCES = cases.cc
fixint_SOURCES = fixint.cc
fixint_c_SOURCES = fixint_c.cc
version_SOURCES = version.cc
msgpackc_test_SOURCES = msgpackc_test.cpp
msgpack_test_SOURCES = msgpack_test.cpp
EXTRA_DIST = cases.mpac cases_compact.mpac

View File

@@ -12,6 +12,14 @@ TEST(buffer, sbuffer)
EXPECT_EQ(3, sbuf.size());
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
sbuf.clear();
sbuf.write("a", 1);
sbuf.write("a", 1);
sbuf.write("a", 1);
EXPECT_EQ(3, sbuf.size());
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
}
@@ -32,6 +40,23 @@ TEST(buffer, vrefbuffer)
EXPECT_EQ(3, sbuf.size());
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
vbuf.clear();
vbuf.write("a", 1);
vbuf.write("a", 1);
vbuf.write("a", 1);
vec = vbuf.vector();
veclen = vbuf.vector_size();
sbuf.clear();
for(size_t i=0; i < veclen; ++i) {
sbuf.write((const char*)vec[i].iov_base, vec[i].iov_len);
}
EXPECT_EQ(3, sbuf.size());
EXPECT_TRUE( memcmp(sbuf.data(), "aaa", 3) == 0 );
}

38
cpp/test/cases.cc Normal file
View File

@@ -0,0 +1,38 @@
#include <msgpack.hpp>
#include <fstream>
#include <gtest/gtest.h>
static void feed_file(msgpack::unpacker& pac, const char* path)
{
std::ifstream fin(path);
while(true) {
pac.reserve_buffer(32*1024);
fin.read(pac.buffer(), pac.buffer_capacity());
if(fin.bad()) {
throw std::runtime_error("read failed");
}
pac.buffer_consumed(fin.gcount());
if(fin.fail()) {
break;
}
}
}
TEST(cases, format)
{
msgpack::unpacker pac;
msgpack::unpacker pac_compact;
feed_file(pac, "cases.mpac");
feed_file(pac_compact, "cases_compact.mpac");
msgpack::unpacked result;
while(pac.next(&result)) {
msgpack::unpacked result_compact;
EXPECT_TRUE( pac_compact.next(&result_compact) );
EXPECT_EQ(result_compact.get(), result.get());
}
EXPECT_FALSE( pac_compact.next(&result) );
}

55
cpp/test/fixint.cc Normal file
View File

@@ -0,0 +1,55 @@
#include <msgpack.hpp>
#include <gtest/gtest.h>
template <typename T>
void check_size(size_t size) {
T v(0);
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, v);
EXPECT_EQ(size, sbuf.size());
}
TEST(fixint, size)
{
check_size<msgpack::type::fix_int8>(2);
check_size<msgpack::type::fix_int16>(3);
check_size<msgpack::type::fix_int32>(5);
check_size<msgpack::type::fix_int64>(9);
check_size<msgpack::type::fix_uint8>(2);
check_size<msgpack::type::fix_uint16>(3);
check_size<msgpack::type::fix_uint32>(5);
check_size<msgpack::type::fix_uint64>(9);
}
template <typename T>
void check_convert() {
T v1(-11);
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, v1);
msgpack::unpacked msg;
msgpack::unpack(&msg, sbuf.data(), sbuf.size());
T v2;
msg.get().convert(&v2);
EXPECT_EQ(v1.get(), v2.get());
EXPECT_EQ(msg.get(), msgpack::object(T(v1.get())));
}
TEST(fixint, convert)
{
check_convert<msgpack::type::fix_int8>();
check_convert<msgpack::type::fix_int16>();
check_convert<msgpack::type::fix_int32>();
check_convert<msgpack::type::fix_int64>();
check_convert<msgpack::type::fix_uint8>();
check_convert<msgpack::type::fix_uint16>();
check_convert<msgpack::type::fix_uint32>();
check_convert<msgpack::type::fix_uint64>();
}

32
cpp/test/fixint_c.cc Normal file
View File

@@ -0,0 +1,32 @@
#include <msgpack.hpp>
#include <gtest/gtest.h>
TEST(fixint, size)
{
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
size_t sum = 0;
EXPECT_EQ(0, msgpack_pack_fix_int8(pk, 0));
EXPECT_EQ(sum+=2, sbuf->size);
EXPECT_EQ(0, msgpack_pack_fix_int16(pk, 0));
EXPECT_EQ(sum+=3, sbuf->size);
EXPECT_EQ(0, msgpack_pack_fix_int32(pk, 0));
EXPECT_EQ(sum+=5, sbuf->size);
EXPECT_EQ(0, msgpack_pack_fix_int64(pk, 0));
EXPECT_EQ(sum+=9, sbuf->size);
EXPECT_EQ(0, msgpack_pack_fix_uint8(pk, 0));
EXPECT_EQ(sum+=2, sbuf->size);
EXPECT_EQ(0, msgpack_pack_fix_uint16(pk, 0));
EXPECT_EQ(sum+=3, sbuf->size);
EXPECT_EQ(0, msgpack_pack_fix_uint32(pk, 0));
EXPECT_EQ(sum+=5, sbuf->size);
EXPECT_EQ(0, msgpack_pack_fix_uint64(pk, 0));
EXPECT_EQ(sum+=9, sbuf->size);
msgpack_sbuffer_free(sbuf);
msgpack_packer_free(pk);
}

View File

@@ -77,21 +77,17 @@ TEST(unpack, sequence)
msgpack::pack(sbuf, 2);
msgpack::pack(sbuf, 3);
bool cont;
size_t offset = 0;
msgpack::unpacked msg;
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
EXPECT_TRUE(cont);
msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
EXPECT_EQ(1, msg.get().as<int>());
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
EXPECT_TRUE(cont);
msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
EXPECT_EQ(2, msg.get().as<int>());
cont = msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
EXPECT_FALSE(cont);
msgpack::unpack(&msg, sbuf.data(), sbuf.size(), &offset);
EXPECT_EQ(3, msg.get().as<int>());
}

70
cpp/test/pack_unpack_c.cc Normal file
View File

@@ -0,0 +1,70 @@
#include <msgpack.h>
#include <gtest/gtest.h>
#include <stdio.h>
TEST(pack, num)
{
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
msgpack_sbuffer_free(sbuf);
msgpack_packer_free(pk);
}
TEST(pack, array)
{
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
EXPECT_EQ(0, msgpack_pack_array(pk, 3));
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
msgpack_sbuffer_free(sbuf);
msgpack_packer_free(pk);
}
TEST(unpack, sequence)
{
msgpack_sbuffer* sbuf = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(sbuf, msgpack_sbuffer_write);
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
msgpack_packer_free(pk);
bool success;
size_t offset = 0;
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
EXPECT_TRUE(success);
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
EXPECT_EQ(1, msg.data.via.u64);
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
EXPECT_TRUE(success);
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
EXPECT_EQ(2, msg.data.via.u64);
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
EXPECT_TRUE(success);
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, msg.data.type);
EXPECT_EQ(3, msg.data.via.u64);
success = msgpack_unpack_next(&msg, sbuf->data, sbuf->size, &offset);
EXPECT_FALSE(success);
msgpack_sbuffer_free(sbuf);
msgpack_unpacked_destroy(&msg);
}

View File

@@ -2,28 +2,33 @@
#include <gtest/gtest.h>
#include <sstream>
TEST(streaming, basic)
{
std::ostringstream stream;
msgpack::packer<std::ostream> pk(&stream);
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> pk(&buffer);
pk.pack(1);
pk.pack(2);
pk.pack(3);
std::istringstream input(stream.str());
const char* input = buffer.data();
const char* const eof = input + buffer.size();
msgpack::unpacker pac;
msgpack::unpacked result;
int count = 0;
while(count < 3) {
pac.reserve_buffer(32*1024);
size_t len = input.readsome(pac.buffer(), pac.buffer_capacity());
// read buffer into pac.buffer() upto
// pac.buffer_capacity() bytes.
size_t len = 1;
memcpy(pac.buffer(), input, len);
input += len;
pac.buffer_consumed(len);
msgpack::unpacked result;
while(pac.next(&result)) {
msgpack::object obj = result.get();
switch(count++) {
@@ -38,6 +43,8 @@ TEST(streaming, basic)
return;
}
}
EXPECT_TRUE(input < eof);
}
}

57
cpp/test/streaming_c.cc Normal file
View File

@@ -0,0 +1,57 @@
#include <msgpack.h>
#include <gtest/gtest.h>
#include <stdio.h>
TEST(streaming, basic)
{
msgpack_sbuffer* buffer = msgpack_sbuffer_new();
msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
EXPECT_EQ(0, msgpack_pack_int(pk, 1));
EXPECT_EQ(0, msgpack_pack_int(pk, 2));
EXPECT_EQ(0, msgpack_pack_int(pk, 3));
msgpack_packer_free(pk);
const char* input = buffer->data;
const char* const eof = input + buffer->size;
msgpack_unpacker pac;
msgpack_unpacker_init(&pac, MSGPACK_UNPACKER_INIT_BUFFER_SIZE);
msgpack_unpacked result;
msgpack_unpacked_init(&result);
int count = 0;
while(count < 3) {
msgpack_unpacker_reserve_buffer(&pac, 32*1024);
/* read buffer into msgpack_unapcker_buffer(&pac) upto
* msgpack_unpacker_buffer_capacity(&pac) bytes. */
size_t len = 1;
memcpy(msgpack_unpacker_buffer(&pac), input, len);
input += len;
msgpack_unpacker_buffer_consumed(&pac, len);
while(msgpack_unpacker_next(&pac, &result)) {
msgpack_object obj = result.data;
switch(count++) {
case 0:
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
EXPECT_EQ(1, result.data.via.u64);
break;
case 1:
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
EXPECT_EQ(2, result.data.via.u64);
break;
case 2:
EXPECT_EQ(MSGPACK_OBJECT_POSITIVE_INTEGER, result.data.type);
EXPECT_EQ(3, result.data.via.u64);
return;
}
}
EXPECT_TRUE(input < eof);
}
}

13
cpp/test/version.cc Normal file
View File

@@ -0,0 +1,13 @@
#include <msgpack.hpp>
#include <gtest/gtest.h>
TEST(version, print)
{
printf("MSGPACK_VERSION : %s\n", MSGPACK_VERSION);
printf("MSGPACK_VERSION_MAJOR : %d\n", MSGPACK_VERSION_MAJOR);
printf("MSGPACK_VERSION_MINOR : %d\n", MSGPACK_VERSION_MINOR);
printf("msgpack_version() : %s\n", msgpack_version());
printf("msgpack_version_major() : %d\n", msgpack_version_major());
printf("msgpack_version_minor() : %d\n", msgpack_version_minor());
}

5
erlang/.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
MANIFEST
*.beam
.omakedb*
*.omc
*~

51
erlang/OMakefile Normal file
View File

@@ -0,0 +1,51 @@
########################################################################
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this file, to deal in the File without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the File, and to permit persons to whom the
# File is furnished to do so, subject to the following condition:
#
# THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR
# THE USE OR OTHER DEALINGS IN THE FILE.
########################################################################
# The standard OMakefile.
# You will usually need to modify this file for your project.
########################################################################
# Phony targets are scoped, so you probably want to declare them first.
#
.PHONY: all clean test edoc dialyzer #install
########################################################################
# Subdirectories.
# You may want to include some subdirectories in this project.
# If so, define the subdirectory targets and uncomment this section.
#
.DEFAULT: msgpack.beam
msgpack.beam: msgpack.erl
erlc -Wall +debug_info $<
msgpack.html: msgpack.erl
erl -noshell -run edoc_run file $<
test: msgpack.beam
erl -noshell -s msgpack test -s init stop
edoc: msgpack.erl
erl -noshell -eval 'ok=edoc:files(["msgpack.erl"], [{dir, "edoc"}]).' -s init stop
dialyzer: msgpack.erl
dialyzer --src $<
clean:
-rm -f *.beam *.html

45
erlang/OMakeroot Normal file
View File

@@ -0,0 +1,45 @@
########################################################################
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this file, to deal in the File without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the File, and to permit persons to whom the
# File is furnished to do so, subject to the following condition:
#
# THE FILE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE FILE OR
# THE USE OR OTHER DEALINGS IN THE FILE.
########################################################################
# The standard OMakeroot file.
# You will not normally need to modify this file.
# By default, your changes should be placed in the
# OMakefile in this directory.
#
# If you decide to modify this file, note that it uses exactly
# the same syntax as the OMakefile.
#
#
# Include the standard installed configuration files.
# Any of these can be deleted if you are not using them,
# but you probably want to keep the Common file.
#
open build/C
open build/OCaml
open build/LaTeX
#
# The command-line variables are defined *after* the
# standard configuration has been loaded.
#
DefineCommandVars()
#
# Include the OMakefile in this directory.
#
.SUBDIRS: .

9
erlang/README.md Normal file
View File

@@ -0,0 +1,9 @@
MessagePack for Erlang
======================
Binary-based efficient object serialization library.
see wiki ( http://redmine.msgpack.org/projects/msgpack/wiki/QuickStartErlang ) for details
# Status
0.1.0 released.

4
erlang/edoc/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
*.html
*.css
*.png
edoc-info

395
erlang/msgpack.erl Normal file
View File

@@ -0,0 +1,395 @@
%%
%% MessagePack for Erlang
%%
%% Copyright (C) 2009-2010 UENISHI Kota
%%
%% 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.
%% @doc <a href="http://msgpack.org/">MessagePack</a> codec for Erlang.
%%
%% APIs are almost compatible with <a href="http://redmine.msgpack.org/projects/msgpack/wiki/QuickStartC">C API</a>
%% except for buffering functions (both copying and zero-copying), which are unavailable.
%%
%% <table border="1">
%% <caption>Equivalence between Erlang and <a href="http://msgpack.sourceforge.jp/spec">Msgpack type</a> :</caption>
%% <tr><th> erlang </th><th> msgpack </th></tr>
%% <tr><td> integer() </td><td> pos_fixnum/neg_fixnum/uint8/uint16/uint32/uint64/int8/int16/int32/int64 </td></tr>
%% <tr><td> float() </td><td> float/double </td></tr>
%% <tr><td> nil </td><td> nil </td></tr>
%% <tr><td> boolean() </td><td> boolean </td></tr>
%% <tr><td> binary() </td><td> fix_raw/raw16/raw32 </td></tr>
%% <tr><td> list() </td><td> fix_array/array16/array32 </td></tr>
%% <tr><td> {proplist()} </td><td> fix_map/map16/map32 </td></tr>
%% </table>
%% @end
-module(msgpack).
-author('kuenishi+msgpack@gmail.com').
-export([pack/1, unpack/1, unpack_all/1]).
% @type msgpack_term() = [msgpack_term()]
% | {[{msgpack_term(),msgpack_term()}]}
% | integer() | float() | binary().
% Erlang representation of msgpack data.
-type msgpack_term() :: [msgpack_term()]
| {[{msgpack_term(),msgpack_term()}]}
| integer() | float() | binary().
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% external APIs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% @doc Encode an erlang term into an msgpack binary.
% Returns {error, {badarg, term()}} if the input is illegal.
% @spec pack(Term::msgpack_term()) -> binary() | {error, {badarg, term()}}
-spec pack(Term::msgpack_term()) -> binary() | {error, {badarg, term()}}.
pack(Term)->
try
pack_(Term)
catch
throw:Exception ->
{error, Exception}
end.
% @doc Decode an msgpack binary into an erlang term.
% It only decodes the first msgpack packet contained in the binary; the rest is returned as is.
% Returns {error, {badarg, term()}} if the input is corrupted.
% Returns {error, incomplete} if the input is not a full msgpack packet (caller should gather more data and try again).
% @spec unpack(Bin::binary()) -> {msgpack_term(), binary()} | {error, incomplete} | {error, {badarg, term()}}
-spec unpack(Bin::binary()) -> {msgpack_term(), binary()} | {error, incomplete} | {error, {badarg, term()}}.
unpack(Bin) when is_binary(Bin) ->
try
unpack_(Bin)
catch
throw:Exception ->
{error, Exception}
end;
unpack(Other) ->
{error, {badarg, Other}}.
% @doc Decode an msgpack binary into an erlang terms.
% It only decodes ALL msgpack packets contained in the binary. No packets should not remain.
% Returns {error, {badarg, term()}} if the input is corrupted.
% Returns {error, incomplete} if the input is not a full msgpack packet (caller should gather more data and try again).
% @spec unpack_all(binary()) -> [msgpack_term()] | {error, incomplete} | {error, {badarg, term()}}
-spec unpack_all(binary()) -> [msgpack_term()] | {error, incomplete} | {error, {badarg, term()}}.
unpack_all(Data)->
try
unpack_all_(Data)
catch
throw:Exception ->
{error, Exception}
end.
unpack_all_(Data)->
case unpack_(Data) of
{ Term, <<>> } ->
[Term];
{ Term, Binary } when is_binary(Binary) ->
[Term|unpack_all_(Binary)]
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% internal APIs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pack them all
-spec pack_(msgpack_term()) -> binary() | no_return().
pack_(I) when is_integer(I) andalso I < 0 ->
pack_int_(I);
pack_(I) when is_integer(I) ->
pack_uint_(I);
pack_(F) when is_float(F) ->
pack_double(F);
pack_(nil) ->
<< 16#C0:8 >>;
pack_(true) ->
<< 16#C3:8 >>;
pack_(false) ->
<< 16#C2:8 >>;
pack_(Bin) when is_binary(Bin) ->
pack_raw(Bin);
pack_(List) when is_list(List) ->
pack_array(List);
pack_({Map}) when is_list(Map) ->
pack_map(Map);
pack_(Other) ->
throw({badarg, Other}).
-spec pack_uint_(non_neg_integer()) -> binary().
% positive fixnum
pack_uint_(N) when N < 128 ->
<< 2#0:1, N:7 >>;
% uint 8
pack_uint_(N) when N < 256 ->
<< 16#CC:8, N:8 >>;
% uint 16
pack_uint_(N) when N < 65536 ->
<< 16#CD:8, N:16/big-unsigned-integer-unit:1 >>;
% uint 32
pack_uint_(N) when N < 16#FFFFFFFF->
<< 16#CE:8, N:32/big-unsigned-integer-unit:1 >>;
% uint 64
pack_uint_(N) ->
<< 16#CF:8, N:64/big-unsigned-integer-unit:1 >>.
-spec pack_int_(integer()) -> binary().
% negative fixnum
pack_int_(N) when N >= -32->
<< 2#111:3, N:5 >>;
% int 8
pack_int_(N) when N > -128 ->
<< 16#D0:8, N:8/big-signed-integer-unit:1 >>;
% int 16
pack_int_(N) when N > -32768 ->
<< 16#D1:8, N:16/big-signed-integer-unit:1 >>;
% int 32
pack_int_(N) when N > -16#FFFFFFFF ->
<< 16#D2:8, N:32/big-signed-integer-unit:1 >>;
% int 64
pack_int_(N) ->
<< 16#D3:8, N:64/big-signed-integer-unit:1 >>.
-spec pack_double(float()) -> binary().
% float : erlang's float is always IEEE 754 64bit format.
% pack_float(F) when is_float(F)->
% << 16#CA:8, F:32/big-float-unit:1 >>.
% pack_double(F).
% double
pack_double(F) ->
<< 16#CB:8, F:64/big-float-unit:1 >>.
-spec pack_raw(binary()) -> binary().
% raw bytes
pack_raw(Bin) ->
case byte_size(Bin) of
Len when Len < 6->
<< 2#101:3, Len:5, Bin/binary >>;
Len when Len < 16#10000 -> % 65536
<< 16#DA:8, Len:16/big-unsigned-integer-unit:1, Bin/binary >>;
Len ->
<< 16#DB:8, Len:32/big-unsigned-integer-unit:1, Bin/binary >>
end.
-spec pack_array([msgpack_term()]) -> binary() | no_return().
% list
pack_array(L) ->
case length(L) of
Len when Len < 16 ->
<< 2#1001:4, Len:4/integer-unit:1, (pack_array_(L, <<>>))/binary >>;
Len when Len < 16#10000 -> % 65536
<< 16#DC:8, Len:16/big-unsigned-integer-unit:1, (pack_array_(L, <<>>))/binary >>;
Len ->
<< 16#DD:8, Len:32/big-unsigned-integer-unit:1, (pack_array_(L, <<>>))/binary >>
end.
pack_array_([], Acc) -> Acc;
pack_array_([Head|Tail], Acc) ->
pack_array_(Tail, <<Acc/binary, (pack_(Head))/binary>>).
% Users SHOULD NOT send too long list: this uses lists:reverse/1
-spec unpack_array_(binary(), non_neg_integer(), [msgpack_term()]) -> {[msgpack_term()], binary()} | no_return().
unpack_array_(Bin, 0, Acc) -> {lists:reverse(Acc), Bin};
unpack_array_(Bin, Len, Acc) ->
{Term, Rest} = unpack_(Bin),
unpack_array_(Rest, Len-1, [Term|Acc]).
-spec pack_map(M::[{msgpack_term(),msgpack_term()}]) -> binary() | no_return().
pack_map(M)->
case length(M) of
Len when Len < 16 ->
<< 2#1000:4, Len:4/integer-unit:1, (pack_map_(M, <<>>))/binary >>;
Len when Len < 16#10000 -> % 65536
<< 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>;
Len ->
<< 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>
end.
pack_map_([], Acc) -> Acc;
pack_map_([{Key,Value}|Tail], Acc) ->
pack_map_(Tail, << Acc/binary, (pack_(Key))/binary, (pack_(Value))/binary>>).
% Users SHOULD NOT send too long list: this uses lists:reverse/1
-spec unpack_map_(binary(), non_neg_integer(), [{msgpack_term(), msgpack_term()}]) ->
{{[{msgpack_term(), msgpack_term()}]}, binary()} | no_return().
unpack_map_(Bin, 0, Acc) -> {{lists:reverse(Acc)}, Bin};
unpack_map_(Bin, Len, Acc) ->
{Key, Rest} = unpack_(Bin),
{Value, Rest2} = unpack_(Rest),
unpack_map_(Rest2, Len-1, [{Key,Value}|Acc]).
% unpack them all
-spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | no_return().
unpack_(Bin) ->
case Bin of
% ATOMS
<<16#C0, Rest/binary>> -> {nil, Rest};
<<16#C2, Rest/binary>> -> {false, Rest};
<<16#C3, Rest/binary>> -> {true, Rest};
% Floats
<<16#CA, V:32/float-unit:1, Rest/binary>> -> {V, Rest};
<<16#CB, V:64/float-unit:1, Rest/binary>> -> {V, Rest};
% Unsigned integers
<<16#CC, V:8/unsigned-integer, Rest/binary>> -> {V, Rest};
<<16#CD, V:16/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest};
<<16#CE, V:32/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest};
<<16#CF, V:64/big-unsigned-integer-unit:1, Rest/binary>> -> {V, Rest};
% Signed integers
<<16#D0, V:8/signed-integer, Rest/binary>> -> {V, Rest};
<<16#D1, V:16/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest};
<<16#D2, V:32/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest};
<<16#D3, V:64/big-signed-integer-unit:1, Rest/binary>> -> {V, Rest};
% Raw bytes
<<16#DA, L:16/unsigned-integer-unit:1, V:L/binary, Rest/binary>> -> {V, Rest};
<<16#DB, L:32/unsigned-integer-unit:1, V:L/binary, Rest/binary>> -> {V, Rest};
% Arrays
<<16#DC, L:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, L, []);
<<16#DD, L:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, L, []);
% Maps
<<16#DE, L:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, L, []);
<<16#DF, L:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, L, []);
% Tag-encoded lengths (kept last, for speed)
<<0:1, V:7, Rest/binary>> -> {V, Rest}; % positive int
<<2#111:3, V:5, Rest/binary>> -> {V - 2#100000, Rest}; % negative int
<<2#101:3, L:5, V:L/binary, Rest/binary>> -> {V, Rest}; % raw bytes
<<2#1001:4, L:4, Rest/binary>> -> unpack_array_(Rest, L, []); % array
<<2#1000:4, L:4, Rest/binary>> -> unpack_map_(Rest, L, []); % map
% Invalid data
<<F, R/binary>> when F==16#C1;
F==16#C4; F==16#C5; F==16#C6; F==16#C7; F==16#C8; F==16#C9;
F==16#D4; F==16#D5; F==16#D6; F==16#D7; F==16#D8; F==16#D9 ->
throw({badarg, <<F, R/binary>>});
% Incomplete data (we've covered every complete/invalid case; anything left is incomplete)
_ ->
throw(incomplete)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% unit tests
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-include_lib("eunit/include/eunit.hrl").
-ifdef(EUNIT).
compare_all([], [])-> ok;
compare_all([], R)-> {toomuchrhs, R};
compare_all(L, [])-> {toomuchlhs, L};
compare_all([LH|LTL], [RH|RTL]) ->
?assertEqual(LH, RH),
compare_all(LTL, RTL).
port_receive(Port) ->
port_receive(Port, <<>>).
port_receive(Port, Acc) ->
receive
{Port, {data, Data}} -> port_receive(Port, <<Acc/binary, Data/binary>>);
{Port, eof} -> Acc
after 1000 -> Acc
end.
test_([]) -> 0;
test_([Term|Rest])->
Pack = msgpack:pack(Term),
?assertEqual({Term, <<>>}, msgpack:unpack( Pack )),
1+test_(Rest).
test_data()->
[true, false, nil,
0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF,
-1, -23, -512, -1230, -567898, -16#FFFFFFFFFF,
123.123, -234.4355, 1.0e-34, 1.0e64,
[23, 234, 0.23],
<<"hogehoge">>, <<"243546rf7g68h798j", 0, 23, 255>>,
<<"hoasfdafdas][">>,
[0,42, <<"sum">>, [1,2]], [1,42, nil, [3]],
-234, -40000, -16#10000000, -16#100000000,
42
].
basic_test()->
Tests = test_data(),
Passed = test_(Tests),
Passed = length(Tests).
port_test()->
Tests = test_data(),
?assertEqual({[Tests],<<>>}, msgpack:unpack(msgpack:pack([Tests]))),
Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary, eof]),
true = port_command(Port, msgpack:pack(Tests)),
?assertEqual({Tests, <<>>}, msgpack:unpack(port_receive(Port))),
port_close(Port).
test_p(Len,Term,OrigBin,Len) ->
{Term, <<>>}=msgpack:unpack(OrigBin);
test_p(I,_,OrigBin,Len) when I < Len->
<<Bin:I/binary, _/binary>> = OrigBin,
?assertEqual({error,incomplete}, msgpack:unpack(Bin)).
partial_test()-> % error handling test.
Term = lists:seq(0, 45),
Bin=msgpack:pack(Term),
BinLen = byte_size(Bin),
[test_p(X, Term, Bin, BinLen) || X <- lists:seq(0,BinLen)].
long_test()->
Longer = lists:seq(0, 655),
{Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)).
map_test()->
Ints = lists:seq(0, 65),
Map = {[ {X, X*2} || X <- Ints ] ++ [{<<"hage">>, 324}, {43542, [nil, true, false]}]},
{Map2, <<>>} = msgpack:unpack(msgpack:pack(Map)),
?assertEqual(Map, Map2),
ok.
unknown_test()->
Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary, eof]),
Tests = [0, 1, 2, 123, 512, 1230, 678908,
-1, -23, -512, -1230, -567898,
<<"hogehoge">>, <<"243546rf7g68h798j">>,
123.123,
-234.4355, 1.0e-34, 1.0e64,
[23, 234, 0.23],
[0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
{[{1,2},{<<"hoge">>,nil}]}, % map
-234, -50000,
42
],
?assertEqual(ok, compare_all(Tests, msgpack:unpack_all(port_receive(Port)))),
port_close(Port).
other_test()->
?assertEqual({error,incomplete},msgpack:unpack(<<>>)).
benchmark_test()->
Data=[test_data() || _ <- lists:seq(0, 10000)],
S=?debugTime(" serialize", msgpack:pack(Data)),
{Data,<<>>}=?debugTime("deserialize", msgpack:unpack(S)),
?debugFmt("for ~p KB test data.", [byte_size(S) div 1024]).
error_test()->
?assertEqual({error,{badarg, atom}}, msgpack:pack(atom)),
Term = {"hoge", "hage", atom},
?assertEqual({error,{badarg, Term}}, msgpack:pack(Term)).
-endif.

View File

@@ -0,0 +1,65 @@
begin
require 'rubygems'
rescue LoadError
end
require 'msgpack'
def usage
puts <<EOF
Usage: #{$0} [out-file]
This tool is for testing of accepting MessagePack random-term.
This does following behavior:
1. serializes the objects in this file, using Ruby implementation
of MessagePack (Note that Ruby implementation is considered valid)
2. Writes the serialized binaries into <out-file> (default: stdout)
EOF
exit 1
end
code = 1
outio = $stdout
if ARGV.length > 2
usage
end
if fname = ARGV[0]
unless fname == "-"
begin
outio = File.open(fname, "w")
rescue
puts "can't open output file: #{$!}"
exit 1
end
end
end
objs = [0, 1, 2, 123, 512, 1230, 678908,
-1, -23, -512, -1230, -567898,
"hogehoge", "243546rf7g68h798j",
123.123,
-234.4355, 1.0e-34, 1.0e64,
[23, 234, 0.23],
[0,42,"sum", [1,2]], [1,42, nil, [3]],
{ 1 => 2, "hoge" => nil },
-234, -50000,
42
]
begin
objs.each do |obj|
outio.write MessagePack.pack(obj)
outio.flush
end
rescue EOFError
code=0
rescue
$stderr.puts $!
code=1
end
outio.close
exit code

View File

@@ -1,5 +1,5 @@
Name: msgpack
Version: 0.2.0
Version: 0.2.2
License: BSD3
License-File: LICENSE
Author: Hideyuki Tanaka

View File

@@ -297,7 +297,7 @@ foreign import ccall "msgpack_pack_raw_body_wrap" msgpack_pack_raw_body ::
-- | Pack a single byte stream. It calls 'packRAW' and 'packRAWBody'.
packRAW' :: Packer -> ByteString -> IO Int
packRAW' pc bs = do
packRAW pc (BS.length bs)
_ <- packRAW pc (BS.length bs)
packRAWBody pc bs
type Unpacker = ForeignPtr ()
@@ -475,7 +475,7 @@ peekObject ptr = do
(#const MSGPACK_OBJECT_MAP) ->
peekObjectMap ptr
_ ->
fail "peekObject: unknown object type"
fail $ "peekObject: unknown object type (" ++ show typ ++ ")"
peekObjectBool :: Ptr a -> IO Object
peekObjectBool ptr = do
@@ -506,7 +506,8 @@ peekObjectRAW ptr = do
peekObjectArray :: Ptr a -> IO Object
peekObjectArray ptr = do
size <- (#peek msgpack_object, via.array.size) ptr
csize <- (#peek msgpack_object, via.array.size) ptr
let size = fromIntegral (csize :: Word32)
p <- (#peek msgpack_object, via.array.ptr) ptr
objs <- mapM (\i -> peekObject $ p `plusPtr`
((#size msgpack_object) * i))
@@ -515,7 +516,8 @@ peekObjectArray ptr = do
peekObjectMap :: Ptr a -> IO Object
peekObjectMap ptr = do
size <- (#peek msgpack_object, via.map.size) ptr
csize <- (#peek msgpack_object, via.map.size) ptr
let size = fromIntegral (csize :: Word32)
p <- (#peek msgpack_object, via.map.ptr) ptr
dat <- mapM (\i -> peekObjectKV $ p `plusPtr`
((#size msgpack_object_kv) * i))
@@ -541,11 +543,11 @@ packObject pc (ObjectDouble d) = packDouble pc d >> return ()
packObject pc (ObjectRAW bs) = packRAW' pc bs >> return ()
packObject pc (ObjectArray ls) = do
packArray pc (length ls)
_ <- packArray pc (length ls)
mapM_ (packObject pc) ls
packObject pc (ObjectMap ls) = do
packMap pc (length ls)
_ <- packMap pc (length ls)
mapM_ (\(a, b) -> packObject pc a >> packObject pc b) ls
data UnpackReturn =
@@ -560,6 +562,7 @@ unpackObject z dat =
allocaBytes (#size msgpack_object) $ \ptr ->
BS.useAsCStringLen dat $ \(str, len) ->
alloca $ \poff -> do
poke poff 0
ret <- msgpack_unpack str (fromIntegral len) poff z ptr
case ret of
(#const MSGPACK_UNPACK_SUCCESS) -> do

View File

@@ -27,7 +27,6 @@ module Data.MessagePack.Class(
import Control.Monad.Error
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as C8
import Data.Either
import Data.MessagePack.Base
@@ -46,6 +45,11 @@ instance OBJECT Object where
fromObjectError :: String
fromObjectError = "fromObject: cannot cast"
instance OBJECT () where
toObject = const ObjectNil
fromObject ObjectNil = Right ()
fromObject _ = Left fromObjectError
instance OBJECT Int where
toObject = ObjectInteger
fromObject (ObjectInteger n) = Right n

View File

@@ -21,7 +21,6 @@ module Data.MessagePack.Feed(
feederFromString,
) where
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.IORef
@@ -33,9 +32,13 @@ type Feeder = IO (Maybe ByteString)
-- | Feeder from Handle
feederFromHandle :: Handle -> IO Feeder
feederFromHandle h = return $ do
bs <- BS.hGet h bufSize
bs <- BS.hGetNonBlocking h bufSize
if BS.length bs > 0
then return $ Just bs
then do return $ Just bs
else do
c <- BS.hGet h 1
if BS.length c > 0
then do return $ Just c
else do
hClose h
return Nothing

View File

@@ -79,10 +79,10 @@ packToString :: MonadIO m => PackerT m r -> m ByteString
packToString m = do
sb <- liftIO $ newSimpleBuffer
pc <- liftIO $ newPacker sb
runPackerT m pc
_ <- runPackerT m pc
liftIO $ simpleBufferData sb
-- | Execcute given serializer and write byte sequence to Handle.
-- | Execute given serializer and write byte sequence to Handle.
packToHandle :: MonadIO m => Handle -> PackerT m r -> m ()
packToHandle h m = do
sb <- packToString m
@@ -115,19 +115,22 @@ instance MonadIO m => MonadIO (UnpackerT m) where
instance MonadIO m => MonadUnpacker (UnpackerT m) where
get = UnpackerT $ \up feed -> liftIO $ do
resp <- unpackerExecute up
guard $ resp>=0
when (resp==0) $ do
Just bs <- feed
unpackerFeed up bs
resp2 <- unpackerExecute up
guard $ resp2==1
executeOne up feed
obj <- unpackerData up
freeZone =<< unpackerReleaseZone up
unpackerReset up
let Right r = fromObject obj
return r
where
executeOne up feed = do
resp <- unpackerExecute up
guard $ resp>=0
when (resp==0) $ do
Just bs <- feed
unpackerFeed up bs
executeOne up feed
-- | Execute deserializer using given feeder.
unpackFrom :: MonadIO m => Feeder -> UnpackerT m r -> m r
unpackFrom f m = do

View File

@@ -19,9 +19,7 @@ module Data.MessagePack.Stream(
unpackObjectsFromString,
) where
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import System.IO
import System.IO.Unsafe

9
java/CHANGES.txt Normal file
View File

@@ -0,0 +1,9 @@
Release 0.3 - 2010/05/23
NEW FEATURES
Added Unbuffered API + Direct Conversion API to the Unpacker.
BUG FIXES
Zero-length Array and Map is deserialized as List and Map, instead of the
array of the Object.
fixed the bug around Packer.packByte().

202
java/LICENSE.txt Normal file
View File

@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.

View File

@@ -3,10 +3,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
<name>MessagePack for Java</name>
<version>1.0-SNAPSHOT</version>
<version>0.3</version>
<description>MessagePack for Java</description>
<name>MessagePack for Java</name>
<url>http://msgpack.org/</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
@@ -16,9 +18,19 @@
</licenses>
<scm>
<connection>scm:git://github.com/msgpack/msgpack.git</connection>
<connection>scm:git:git://github.com/msgpack/msgpack.git</connection>
<url>scm:git:git://github.com/msgpack/msgpack.git</url>
</scm>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
@@ -83,27 +95,45 @@
</plugins>
</reporting>
<profiles>
<!-- for sending artifacts to sourceforge.net repository -->
<profile>
<id>sourceforge</id>
<repositories>
<repository>
<id>msgpack.org</id>
<name>MessagePack Maven2 Repository</name>
<url>http://msgpack.org/maven2</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>sourceforge.net</id>
<name>Repository at sourceforge.net</name>
<url>scpexe://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2/</url>
<uniqueVersion>false</uniqueVersion>
<id>msgpack.org</id>
<name>Repository at msgpack.org</name>
<url>file://${project.build.directory}/website/maven2/</url>
</repository>
<snapshotRepository>
<uniqueVersion>true</uniqueVersion>
<id>msgpack.org</id>
<name>Repository at msgpack.org</name>
<url>file://${project.build.directory}/website/maven2/</url>
</snapshotRepository>
</distributionManagement>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,445 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.math.BigInteger;
abstract class BufferedUnpackerImpl extends UnpackerImpl {
int offset = 0;
int filled = 0;
byte[] buffer = null;
private ByteBuffer castBuffer = ByteBuffer.allocate(8);
abstract boolean fill() throws IOException;
final boolean next(UnpackResult result) throws IOException, UnpackException {
if(filled == 0) {
if(!fill()) {
return false;
}
}
do {
int noffset = super.execute(buffer, offset, filled);
if(noffset <= offset) {
if(!fill()) {
return false;
}
continue;
}
offset = noffset;
} while(!super.isFinished());
MessagePackObject obj = super.getData();
super.reset();
result.done(obj);
return true;
}
private final void more(int require) throws IOException, UnpackException {
while(filled - offset < require) {
if(!fill()) {
// FIXME
throw new UnpackException("insufficient buffer");
}
}
}
private final boolean tryMore(int require) throws IOException, UnpackException {
while(filled - offset < require) {
if(!fill()) {
return false;
}
}
return true;
}
private final void advance(int length) {
offset += length;
}
final byte unpackByte() throws IOException, MessageTypeException {
int o = unpackInt();
if(0x7f < o || o < -0x80) {
throw new MessageTypeException();
}
return (byte)o;
}
final short unpackShort() throws IOException, MessageTypeException {
int o = unpackInt();
if(0x7fff < o || o < -0x8000) {
throw new MessageTypeException();
}
return (short)o;
}
final int unpackInt() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum
advance(1);
return (int)b;
}
switch(b & 0xff) {
case 0xcc: // unsigned int 8
more(2);
advance(2);
return (int)((short)(buffer[offset-1]) & 0xff);
case 0xcd: // unsigned int 16
more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
advance(3);
return (int)((int)castBuffer.getShort(0) & 0xffff);
case 0xce: // unsigned int 32
more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
{
int o = castBuffer.getInt(0);
if(o < 0) {
throw new MessageTypeException();
}
advance(5);
return o;
}
case 0xcf: // unsigned int 64
more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
{
long o = castBuffer.getLong(0);
if(o < 0 || o > 0x7fffffffL) {
throw new MessageTypeException();
}
advance(9);
return (int)o;
}
case 0xd0: // signed int 8
more(2);
advance(2);
return (int)buffer[offset-1];
case 0xd1: // signed int 16
more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
advance(3);
return (int)castBuffer.getShort(0);
case 0xd2: // signed int 32
more(4);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(4);
return (int)castBuffer.getInt(0);
case 0xd3: // signed int 64
more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
{
long o = castBuffer.getLong(0);
if(0x7fffffffL < o || o < -0x80000000L) {
throw new MessageTypeException();
}
advance(9);
return (int)o;
}
default:
throw new MessageTypeException();
}
}
final long unpackLong() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum
advance(1);
return (long)b;
}
switch(b & 0xff) {
case 0xcc: // unsigned int 8
more(2);
advance(2);
return (long)((short)(buffer[offset-1]) & 0xff);
case 0xcd: // unsigned int 16
more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
advance(3);
return (long)((int)castBuffer.getShort(0) & 0xffff);
case 0xce: // unsigned int 32
more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(5);
return ((long)castBuffer.getInt(0) & 0xffffffffL);
case 0xcf: // unsigned int 64
more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
{
long o = castBuffer.getLong(0);
if(o < 0) {
throw new MessageTypeException();
}
advance(9);
return o;
}
case 0xd0: // signed int 8
more(2);
advance(2);
return (long)buffer[offset-1];
case 0xd1: // signed int 16
more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
advance(3);
return (long)castBuffer.getShort(0);
case 0xd2: // signed int 32
more(4);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(4);
return (long)castBuffer.getInt(0);
case 0xd3: // signed int 64
more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
advance(9);
return (long)castBuffer.getLong(0);
default:
throw new MessageTypeException();
}
}
final BigInteger unpackBigInteger() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
if((b & 0xff) != 0xcf) {
return BigInteger.valueOf(unpackLong());
}
// unsigned int 64
more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
advance(9);
long o = castBuffer.getLong(0);
if(o < 0) {
return new BigInteger(1, castBuffer.array());
} else {
return BigInteger.valueOf(o);
}
}
final float unpackFloat() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
switch(b & 0xff) {
case 0xca: // float
more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(5);
return castBuffer.getFloat(0);
case 0xcb: // double
more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
advance(9);
// FIXME overflow check
return (float)castBuffer.getDouble(0);
default:
throw new MessageTypeException();
}
}
final double unpackDouble() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
switch(b & 0xff) {
case 0xca: // float
more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(5);
return (double)castBuffer.getFloat(0);
case 0xcb: // double
more(9);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 8);
advance(9);
return castBuffer.getDouble(0);
default:
throw new MessageTypeException();
}
}
final Object unpackNull() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset] & 0xff;
if(b != 0xc0) { // nil
throw new MessageTypeException();
}
advance(1);
return null;
}
final boolean tryUnpackNull() throws IOException {
if(!tryMore(1)) {
return false;
}
int b = buffer[offset] & 0xff;
if(b != 0xc0) { // nil
return false;
}
advance(1);
return true;
}
final boolean unpackBoolean() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset] & 0xff;
if(b == 0xc2) { // false
advance(1);
return false;
} else if(b == 0xc3) { // true
advance(1);
return true;
} else {
throw new MessageTypeException();
}
}
final int unpackArray() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
if((b & 0xf0) == 0x90) { // FixArray
advance(1);
return (int)(b & 0x0f);
}
switch(b & 0xff) {
case 0xdc: // array 16
more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
advance(3);
return (int)castBuffer.getShort(0) & 0xffff;
case 0xdd: // array 32
more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(5);
// FIXME overflow check
return castBuffer.getInt(0) & 0x7fffffff;
default:
throw new MessageTypeException();
}
}
final int unpackMap() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
if((b & 0xf0) == 0x80) { // FixMap
advance(1);
return (int)(b & 0x0f);
}
switch(b & 0xff) {
case 0xde: // map 16
more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
advance(3);
return (int)castBuffer.getShort(0) & 0xffff;
case 0xdf: // map 32
more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(5);
// FIXME overflow check
return castBuffer.getInt(0) & 0x7fffffff;
default:
throw new MessageTypeException();
}
}
final int unpackRaw() throws IOException, MessageTypeException {
more(1);
int b = buffer[offset];
if((b & 0xe0) == 0xa0) { // FixRaw
advance(1);
return (int)(b & 0x1f);
}
switch(b & 0xff) {
case 0xda: // raw 16
more(3);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 2);
advance(3);
return (int)castBuffer.getShort(0) & 0xffff;
case 0xdb: // raw 32
more(5);
castBuffer.rewind();
castBuffer.put(buffer, offset+1, 4);
advance(5);
// FIXME overflow check
return castBuffer.getInt(0) & 0x7fffffff;
default:
throw new MessageTypeException();
}
}
final byte[] unpackRawBody(int length) throws IOException {
more(length);
byte[] bytes = new byte[length];
System.arraycopy(buffer, offset, bytes, 0, length);
advance(length);
return bytes;
}
final byte[] unpackByteArray() throws IOException, MessageTypeException {
int length = unpackRaw();
return unpackRawBody(length);
}
final String unpackString() throws IOException, MessageTypeException {
int length = unpackRaw();
more(length);
String s;
try {
s = new String(buffer, offset, length, "UTF-8");
} catch (Exception e) {
throw new MessageTypeException();
}
advance(length);
return s;
}
final MessagePackObject unpackObject() throws IOException {
UnpackResult result = new UnpackResult();
if(!next(result)) {
super.reset();
throw new UnpackException("insufficient buffer");
}
return result.getData();
}
}

View File

@@ -17,7 +17,7 @@
//
package org.msgpack;
public interface MessageMergeable {
public void messageMerge(Object obj) throws MessageTypeException;
public interface MessageConvertable {
public void messageConvert(MessagePackObject obj) throws MessageTypeException;
}

View File

@@ -0,0 +1,136 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.math.BigInteger;
public abstract class MessagePackObject implements Cloneable, MessagePackable {
public boolean isNil() {
return false;
}
public boolean isBooleanType() {
return false;
}
public boolean isIntegerType() {
return false;
}
public boolean isFloatType() {
return false;
}
public boolean isArrayType() {
return false;
}
public boolean isMapType() {
return false;
}
public boolean isRawType() {
return false;
}
public boolean asBoolean() {
throw new MessageTypeException("type error");
}
public byte asByte() {
throw new MessageTypeException("type error");
}
public short asShort() {
throw new MessageTypeException("type error");
}
public int asInt() {
throw new MessageTypeException("type error");
}
public long asLong() {
throw new MessageTypeException("type error");
}
public BigInteger asBigInteger() {
throw new MessageTypeException("type error");
}
public float asFloat() {
throw new MessageTypeException("type error");
}
public double asDouble() {
throw new MessageTypeException("type error");
}
public byte[] asByteArray() {
throw new MessageTypeException("type error");
}
public String asString() {
throw new MessageTypeException("type error");
}
public MessagePackObject[] asArray() {
throw new MessageTypeException("type error");
}
public List<MessagePackObject> asList() {
throw new MessageTypeException("type error");
}
public Map<MessagePackObject, MessagePackObject> asMap() {
throw new MessageTypeException("type error");
}
public byte byteValue() {
throw new MessageTypeException("type error");
}
public short shortValue() {
throw new MessageTypeException("type error");
}
public int intValue() {
throw new MessageTypeException("type error");
}
public long longValue() {
throw new MessageTypeException("type error");
}
public BigInteger bigIntegerValue() {
throw new MessageTypeException("type error");
}
public float floatValue() {
throw new MessageTypeException("type error");
}
public double doubleValue() {
throw new MessageTypeException("type error");
}
abstract public Object clone();
}

View File

@@ -17,23 +17,11 @@
//
package org.msgpack;
import java.io.IOException;
public class MessageTypeException extends IOException {
public class MessageTypeException extends RuntimeException {
public MessageTypeException() { }
public MessageTypeException(String s) {
super(s);
}
public static MessageTypeException invalidConvert(Object from, Schema to) {
return new MessageTypeException(from.getClass().getName()+" cannot be convert to "+to.getExpression());
}
/* FIXME
public static MessageTypeException schemaMismatch(Schema to) {
return new MessageTypeException("schema mismatch "+to.getExpression());
}
*/
}

View File

@@ -15,12 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.schema;
package org.msgpack;
import org.msgpack.Schema;
import java.io.IOException;
public interface IArraySchema {
public Schema getElementSchema(int index);
public Object createFromArray(Object[] obj);
public interface MessageUnpackable {
public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException;
}

View File

@@ -22,7 +22,24 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
/**
* Packer enables you to serialize objects into OutputStream.
*
* <pre>
* // create a packer with output stream
* Packer pk = new Packer(System.out);
*
* // store an object with pack() method.
* pk.pack(1);
*
* // you can store String, List, Map, byte[] and primitive types.
* pk.pack(new ArrayList());
* </pre>
*
* You can serialize objects that implements {@link MessagePackable} interface.
*/
public class Packer {
protected byte[] castBytes = new byte[9];
protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes);
@@ -34,7 +51,7 @@ public class Packer {
public Packer packByte(byte d) throws IOException {
if(d < -(1<<5)) {
castBytes[0] = (byte)0xd1;
castBytes[0] = (byte)0xd0;
castBytes[1] = d;
out.write(castBytes, 0, 2);
} else {
@@ -178,6 +195,27 @@ public class Packer {
return this;
}
public Packer packBigInteger(BigInteger d) throws IOException {
if(d.bitLength() <= 63) {
return packLong(d.longValue());
} else if(d.bitLength() <= 64 && d.signum() >= 0) {
castBytes[0] = (byte)0xcf;
byte[] barray = d.toByteArray();
castBytes[1] = barray[barray.length-8];
castBytes[2] = barray[barray.length-7];
castBytes[3] = barray[barray.length-6];
castBytes[4] = barray[barray.length-5];
castBytes[5] = barray[barray.length-4];
castBytes[6] = barray[barray.length-3];
castBytes[7] = barray[barray.length-2];
castBytes[8] = barray[barray.length-1];
out.write(castBytes);
return this;
} else {
throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff");
}
}
public Packer packFloat(float d) throws IOException {
castBytes[0] = (byte)0xca;
castBuffer.putFloat(1, d);
@@ -207,6 +245,10 @@ public class Packer {
return this;
}
public Packer packBoolean(boolean d) throws IOException {
return d ? packTrue() : packFalse();
}
public Packer packArray(int n) throws IOException {
if(n < 16) {
final int d = 0x90 | n;
@@ -266,12 +308,6 @@ public class Packer {
}
public Packer packWithSchema(Object o, Schema s) throws IOException {
s.pack(this, o);
return this;
}
public Packer packString(String s) throws IOException {
byte[] b = ((String)s).getBytes("UTF-8");
packRaw(b.length);
@@ -279,39 +315,36 @@ public class Packer {
}
public Packer pack(String o) throws IOException {
if(o == null) { return packNil(); }
return packString(o);
public Packer pack(boolean o) throws IOException {
if(o) {
return packTrue();
} else {
return packFalse();
}
}
public Packer pack(MessagePackable o) throws IOException {
if(o == null) { return packNil(); }
o.messagePack(this);
return this;
public Packer pack(byte o) throws IOException {
return packByte(o);
}
public Packer pack(byte[] o) throws IOException {
if(o == null) { return packNil(); }
packRaw(o.length);
return packRawBody(o);
public Packer pack(short o) throws IOException {
return packShort(o);
}
public Packer pack(List o) throws IOException {
if(o == null) { return packNil(); }
packArray(o.size());
for(Object i : o) { pack(i); }
return this;
public Packer pack(int o) throws IOException {
return packInt(o);
}
@SuppressWarnings("unchecked")
public Packer pack(Map o) throws IOException {
if(o == null) { return packNil(); }
packMap(o.size());
for(Map.Entry e : ((Map<Object,Object>)o).entrySet()) {
pack(e.getKey());
pack(e.getValue());
public Packer pack(long o) throws IOException {
return packLong(o);
}
return this;
public Packer pack(float o) throws IOException {
return packFloat(o);
}
public Packer pack(double o) throws IOException {
return packDouble(o);
}
public Packer pack(Boolean o) throws IOException {
@@ -343,6 +376,11 @@ public class Packer {
return packLong(o);
}
public Packer pack(BigInteger o) throws IOException {
if(o == null) { return packNil(); }
return packBigInteger(o);
}
public Packer pack(Float o) throws IOException {
if(o == null) { return packNil(); }
return packFloat(o);
@@ -353,8 +391,41 @@ public class Packer {
return packDouble(o);
}
public Packer pack(String o) throws IOException {
if(o == null) { return packNil(); }
return packString(o);
}
public Packer pack(MessagePackable o) throws IOException {
if(o == null) { return packNil(); }
o.messagePack(this);
return this;
}
public Packer pack(byte[] o) throws IOException {
if(o == null) { return packNil(); }
packRaw(o.length);
return packRawBody(o);
}
public Packer pack(List o) throws IOException {
if(o == null) { return packNil(); }
packArray(o.size());
for(Object i : o) { pack(i); }
return this;
}
public Packer pack(Map o) throws IOException {
if(o == null) { return packNil(); }
packMap(o.size());
for(Map.Entry<Object,Object> e : ((Map<Object,Object>)o).entrySet()) {
pack(e.getKey());
pack(e.getValue());
}
return this;
}
@SuppressWarnings("unchecked")
public Packer pack(Object o) throws IOException {
if(o == null) {
return packNil();
@@ -377,7 +448,7 @@ public class Packer {
} else if(o instanceof Map) {
Map<Object,Object> m = (Map<Object,Object>)o;
packMap(m.size());
for(Map.Entry e : m.entrySet()) {
for(Map.Entry<Object,Object> e : m.entrySet()) {
pack(e.getKey());
pack(e.getValue());
}
@@ -400,8 +471,10 @@ public class Packer {
return packFloat((Float)o);
} else if(o instanceof Double) {
return packDouble((Double)o);
} else if(o instanceof BigInteger) {
return packBigInteger((BigInteger)o);
} else {
throw new IOException("unknown object "+o+" ("+o.getClass()+")");
throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")");
}
}
}

View File

@@ -1,133 +0,0 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack;
import java.io.Writer;
import java.io.IOException;
import org.msgpack.schema.SSchemaParser;
import org.msgpack.schema.ClassGenerator;
public abstract class Schema {
private String expression;
private String name;
public Schema(String name) {
this.expression = expression;
this.name = name;
}
public String getName() {
return name;
}
public String getFullName() {
return name;
}
public String getExpression() {
return name;
}
public static Schema parse(String source) {
return SSchemaParser.parse(source);
}
public static Schema load(String source) {
return SSchemaParser.load(source);
}
public void write(Writer output) throws IOException {
ClassGenerator.write(this, output);
}
public abstract void pack(Packer pk, Object obj) throws IOException;
public abstract Object convert(Object obj) throws MessageTypeException;
public Object createFromNil() {
return null;
}
public Object createFromBoolean(boolean v) {
throw new RuntimeException("type error");
}
public Object createFromByte(byte v) {
throw new RuntimeException("type error");
}
public Object createFromShort(short v) {
throw new RuntimeException("type error");
}
public Object createFromInt(int v) {
throw new RuntimeException("type error");
}
public Object createFromLong(long v) {
throw new RuntimeException("type error");
}
public Object createFromFloat(float v) {
throw new RuntimeException("type error");
}
public Object createFromDouble(double v) {
throw new RuntimeException("type error");
}
public Object createFromRaw(byte[] b, int offset, int length) {
throw new RuntimeException("type error");
}
/* FIXME
public Object createFromBoolean(boolean v) {
throw MessageTypeException.schemaMismatch(this);
}
public Object createFromByte(byte v) {
throw MessageTypeException.schemaMismatch(this);
}
public Object createFromShort(short v) {
throw MessageTypeException.schemaMismatch(this);
}
public Object createFromInt(int v) {
throw MessageTypeException.schemaMismatch(this);
}
public Object createFromLong(long v) {
throw MessageTypeException.schemaMismatch(this);
}
public Object createFromFloat(float v) {
throw MessageTypeException.schemaMismatch(this);
}
public Object createFromDouble(double v) {
throw MessageTypeException.schemaMismatch(this);
}
public Object createFromRaw(byte[] b, int offset, int length) {
throw MessageTypeException.schemaMismatch(this);
}
*/
}

View File

@@ -1,82 +0,0 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack;
import java.lang.Iterable;
import java.io.InputStream;
import java.io.IOException;
import java.util.Iterator;
import org.msgpack.impl.UnpackerImpl;
public class UnbufferedUnpacker extends UnpackerImpl {
private int offset;
private boolean finished;
private Object data;
public UnbufferedUnpacker() {
super();
this.offset = 0;
this.finished = false;
}
public UnbufferedUnpacker useSchema(Schema s) {
super.setSchema(s);
return this;
}
public Object getData() {
return data;
}
public boolean isFinished() {
return finished;
}
public void reset() {
super.reset();
this.offset = 0;
}
int getOffset() {
return offset;
}
void setOffset(int offset) {
this.offset = offset;
}
public int execute(byte[] buffer) throws UnpackException {
return execute(buffer, 0, buffer.length);
}
// FIXME
public int execute(byte[] buffer, int offset, int length) throws UnpackException
{
int noffset = super.execute(buffer, offset + this.offset, length);
this.offset = noffset - offset;
if(super.isFinished()) {
this.data = super.getData();
this.finished = true;
super.reset();
} else {
this.finished = false;
}
return noffset;
}
}

View File

@@ -21,41 +21,28 @@ import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class UnpackIterator implements Iterator<Object> {
public class UnpackIterator extends UnpackResult implements Iterator<MessagePackObject> {
private Unpacker pac;
private boolean have;
private Object data;
UnpackIterator(Unpacker pac) {
super();
this.pac = pac;
this.have = false;
}
public boolean hasNext() {
if(have) { return true; }
if(finished) { return true; }
try {
while(true) {
if(pac.execute()) {
data = pac.getData();
pac.reset();
have = true;
return true;
}
if(!pac.fill()) {
return false;
}
}
return pac.next(this);
} catch (IOException e) {
return false;
}
}
public Object next() {
if(!have && !hasNext()) {
public MessagePackObject next() {
if(!finished && !hasNext()) {
throw new NoSuchElementException();
}
have = false;
finished = false;
return data;
}

View File

@@ -15,29 +15,28 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.schema;
package org.msgpack;
import org.msgpack.Schema;
public class UnpackResult {
protected boolean finished = false;
protected MessagePackObject data = null;
public class FieldSchema {
private String name;
private Schema schema;
public FieldSchema(String name, Schema schema) {
this.name = name;
this.schema = schema;
public boolean isFinished() {
return finished;
}
public final String getName() {
return name;
public MessagePackObject getData() {
return data;
}
public final Schema getSchema() {
return schema;
public void reset() {
finished = false;
data = null;
}
public String getExpression() {
return "(field "+name+" "+schema.getExpression()+")";
void done(MessagePackObject obj) {
finished = true;
data = obj;
}
}

View File

@@ -22,144 +22,382 @@ import java.io.InputStream;
import java.io.IOException;
import java.util.Iterator;
import java.nio.ByteBuffer;
import org.msgpack.impl.UnpackerImpl;
import java.math.BigInteger;
public class Unpacker extends UnpackerImpl implements Iterable<Object> {
public static final int DEFAULT_BUFFER_SIZE = 32*1024;
private int used;
private int offset;
private int parsed;
private byte[] buffer;
private int bufferReserveSize;
private InputStream stream;
public Unpacker() {
this(DEFAULT_BUFFER_SIZE);
}
public Unpacker(int bufferReserveSize) {
this(null, bufferReserveSize);
}
public Unpacker(InputStream stream) {
this(stream, DEFAULT_BUFFER_SIZE);
}
public Unpacker(InputStream stream, int bufferReserveSize) {
super();
this.used = 0;
this.offset = 0;
this.parsed = 0;
this.buffer = new byte[bufferReserveSize];
this.bufferReserveSize = bufferReserveSize/2;
this.stream = stream;
}
public Unpacker useSchema(Schema s) {
super.setSchema(s);
return this;
}
public void reserveBuffer(int size) {
if(buffer.length - used >= size) {
return;
}
/*
if(used == parsed && buffer.length >= size) {
// rewind buffer
used = 0;
offset = 0;
return;
}
/**
* Unpacker enables you to deserialize objects from stream.
*
* Unpacker provides Buffered API, Unbuffered API and
* Direct Conversion API.
*
* Buffered API uses the internal buffer of the Unpacker.
* Following code uses Buffered API with an InputStream:
* <pre>
* // create an unpacker with input stream
* Unpacker pac = new Unpacker(System.in);
*
* // take a object out using next() method, or ...
* UnpackResult result = pac.next();
*
* // use an iterator.
* for(MessagePackObject obj : pac) {
* // use MessageConvertable interface to convert the
* // the generic object to the specific type.
* }
* </pre>
*
* Following code doesn't use the input stream and feeds buffer
* using {@link feed(byte[])} method. This is useful to use
* special stream like zlib or event-driven I/O library.
* <pre>
* // create an unpacker without input stream
* Unpacker pac = new Unpacker();
*
* // feed buffer to the internal buffer.
* pac.feed(input_bytes);
*
* // use next() method or iterators.
* for(MessagePackObject obj : pac) {
* // ...
* }
* </pre>
*
* The combination of {@link reserveBuffer()}, {@link getBuffer()},
* {@link getBufferOffset()}, {@link getBufferCapacity()} and
* {@link bufferConsumed()} is useful to omit copying.
* <pre>
* // create an unpacker without input stream
* Unpacker pac = new Unpacker();
*
* // reserve internal buffer at least 1024 bytes.
* pac.reserveBuffer(1024);
*
* // feed buffer to the internal buffer upto pac.getBufferCapacity() bytes.
* System.in.read(pac.getBuffer(), pac.getBufferOffset(), pac.getBufferCapacity());
*
* // use next() method or iterators.
* for(MessagePackObject obj : pac) {
* // ...
* }
* </pre>
*
* Unbuffered API doesn't initialize the internal buffer.
* You can manage the buffer manually.
* <pre>
* // create an unpacker with input stream
* Unpacker pac = new Unpacker(System.in);
*
* // manage the buffer manually.
* byte[] buffer = new byte[1024];
* int filled = System.in.read(buffer);
* int offset = 0;
*
* // deserialize objects using execute() method.
* int nextOffset = pac.execute(buffer, offset, filled);
*
* // take out object if deserialized object is ready.
* if(pac.isFinished()) {
* MessagePackObject obj = pac.getData();
* // ...
* }
* </pre>
*/
public class Unpacker implements Iterable<MessagePackObject> {
int nextSize = buffer.length * 2;
while(nextSize < size + used) {
nextSize *= 2;
}
// buffer:
// +---------------------------------------------+
// | [object] | [obje| unparsed ... | unused ...|
// +---------------------------------------------+
// ^ parsed
// ^ offset
// ^ filled
// ^ buffer.length
byte[] tmp = new byte[nextSize];
System.arraycopy(buffer, offset, tmp, 0, used - offset);
private static final int DEFAULT_BUFFER_SIZE = 32*1024;
buffer = tmp;
used -= offset;
offset = 0;
}
protected int parsed;
protected int bufferReserveSize;
protected InputStream stream;
public byte[] getBuffer() {
return buffer;
}
public int getBufferOffset() {
return used;
}
public int getBufferCapacity() {
return buffer.length - used;
}
public void bufferConsumed(int size) {
used += size;
}
public void feed(ByteBuffer buffer) {
int length = buffer.remaining();
if (length == 0) return;
reserveBuffer(length);
buffer.get(this.buffer, this.offset, length);
bufferConsumed(length);
}
public void feed(byte[] buffer) {
feed(buffer, 0, buffer.length);
}
public void feed(byte[] buffer, int offset, int length) {
reserveBuffer(length);
System.arraycopy(buffer, offset, this.buffer, this.offset, length);
bufferConsumed(length);
}
public boolean fill() throws IOException {
final class BufferedUnpackerMixin extends BufferedUnpackerImpl {
boolean fill() throws IOException {
if(stream == null) {
return false;
}
reserveBuffer(bufferReserveSize);
int rl = stream.read(getBuffer(), getBufferOffset(), getBufferCapacity());
int rl = stream.read(buffer, filled, buffer.length - filled);
// equals: stream.read(getBuffer(), getBufferOffset(), getBufferCapacity());
if(rl <= 0) {
return false;
}
bufferConsumed(rl);
return true;
}
};
public Iterator<Object> iterator() {
final BufferedUnpackerMixin impl = new BufferedUnpackerMixin();
/**
* Calls {@link Unpacker(DEFAULT_BUFFER_SIZE)}
*/
public Unpacker() {
this(DEFAULT_BUFFER_SIZE);
}
/**
* Calls {@link Unpacker(null, bufferReserveSize)}
*/
public Unpacker(int bufferReserveSize) {
this(null, bufferReserveSize);
}
/**
* Calls {@link Unpacker(stream, DEFAULT_BUFFER_SIZE)}
*/
public Unpacker(InputStream stream) {
this(stream, DEFAULT_BUFFER_SIZE);
}
/**
* Constructs the unpacker.
* The stream is used to fill the buffer when more buffer is required by {@link next()} or {@link UnpackIterator#hasNext()} method.
* @param stream input stream to fill the buffer
* @param bufferReserveSize threshold size to expand the size of buffer
*/
public Unpacker(InputStream stream, int bufferReserveSize) {
this.parsed = 0;
this.bufferReserveSize = bufferReserveSize/2;
this.stream = stream;
}
/**
* Gets the input stream.
* @return the input stream. it may be null.
*/
public InputStream getStream() {
return this.stream;
}
/**
* Sets the input stream.
* @param stream the input stream to set.
*/
public void setStream(InputStream stream) {
this.stream = stream;
}
/**
* Fills the buffer with the specified buffer.
*/
public void feed(byte[] buffer) {
feed(buffer, 0, buffer.length);
}
/**
* Fills the buffer with the specified buffer.
*/
public void feed(byte[] buffer, int offset, int length) {
reserveBuffer(length);
System.arraycopy(buffer, offset, impl.buffer, impl.offset, length);
bufferConsumed(length);
}
/**
* Fills the buffer with the specified buffer.
*/
public void feed(ByteBuffer buffer) {
int length = buffer.remaining();
if (length == 0) return;
reserveBuffer(length);
buffer.get(impl.buffer, impl.offset, length);
bufferConsumed(length);
}
/**
* Swaps the internal buffer with the specified buffer.
* This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method.
*/
public void wrap(byte[] buffer) {
wrap(buffer, 0, buffer.length);
}
/**
* Swaps the internal buffer with the specified buffer.
* This method doesn't copy the buffer and the its contents will be rewritten by {@link fill()} or {@link feed(byte[])} method.
*/
public void wrap(byte[] buffer, int offset, int length) {
impl.buffer = buffer;
impl.offset = offset;
impl.filled = length;
}
/**
* Fills the internal using the input stream.
* @return false if the stream is null or stream.read returns <= 0.
*/
public boolean fill() throws IOException {
return impl.fill();
}
/**
* Returns the iterator that calls {@link next()} method repeatedly.
*/
public Iterator<MessagePackObject> iterator() {
return new UnpackIterator(this);
}
/**
* Deserializes one object and returns it.
* @return {@link UnpackResult#isFinished()} returns false if the buffer is insufficient to deserialize one object.
*/
public UnpackResult next() throws IOException, UnpackException {
UnpackResult result = new UnpackResult();
impl.next(result);
return result;
}
/**
* Deserializes one object and returns it.
* @return false if the buffer is insufficient to deserialize one object.
*/
public boolean next(UnpackResult result) throws IOException, UnpackException {
return impl.next(result);
}
/**
* Reserve free space of the internal buffer at least specified size and expands {@link getBufferCapacity()}.
*/
public void reserveBuffer(int require) {
if(impl.buffer == null) {
int nextSize = (bufferReserveSize < require) ? require : bufferReserveSize;
impl.buffer = new byte[nextSize];
return;
}
if(impl.filled <= impl.offset) {
// rewind the buffer
impl.filled = 0;
impl.offset = 0;
}
if(impl.buffer.length - impl.filled >= require) {
return;
}
int nextSize = impl.buffer.length * 2;
int notParsed = impl.filled - impl.offset;
while(nextSize < require + notParsed) {
nextSize *= 2;
}
byte[] tmp = new byte[nextSize];
System.arraycopy(impl.buffer, impl.offset, tmp, 0, impl.filled - impl.offset);
impl.buffer = tmp;
impl.filled = notParsed;
impl.offset = 0;
}
/**
* Returns the internal buffer.
*/
public byte[] getBuffer() {
return impl.buffer;
}
/**
* Returns the size of free space of the internal buffer.
*/
public int getBufferCapacity() {
return impl.buffer.length - impl.filled;
}
/**
* Returns the offset of free space in the internal buffer.
*/
public int getBufferOffset() {
return impl.filled;
}
/**
* Moves front the offset of the free space in the internal buffer.
* Call this method after fill the buffer manually using {@link reserveBuffer()}, {@link getBuffer()}, {@link getBufferOffset()} and {@link getBufferCapacity()} methods.
*/
public void bufferConsumed(int size) {
impl.filled += size;
}
/**
* Deserializes one object upto the offset of the internal buffer.
* Call {@link reset()} method before calling this method again.
* @return true if one object is deserialized. Use {@link getData()} to get the deserialized object.
*/
public boolean execute() throws UnpackException {
int noffset = super.execute(buffer, offset, used);
if(noffset <= offset) {
int noffset = impl.execute(impl.buffer, impl.offset, impl.filled);
if(noffset <= impl.offset) {
return false;
}
parsed += noffset - offset;
offset = noffset;
return super.isFinished();
parsed += noffset - impl.offset;
impl.offset = noffset;
return impl.isFinished();
}
public Object getData() {
return super.getData();
/**
* Deserializes one object over the specified buffer.
* This method doesn't use the internal buffer.
* Use {@link isFinished()} method to known a object is ready to get.
* Call {@link reset()} method before calling this method again.
* @return offset position that is parsed.
*/
public int execute(byte[] buffer) throws UnpackException {
return execute(buffer, 0, buffer.length);
}
/**
* Deserializes one object over the specified buffer.
* This method doesn't use the internal buffer.
* Use {@link isFinished()} method to known a object is ready to get.
* Call {@link reset()} method before calling this method again.
* @return offset position that is parsed.
*/
public int execute(byte[] buffer, int offset, int length) throws UnpackException {
int noffset = impl.execute(buffer, offset + impl.offset, length);
impl.offset = noffset - offset;
if(impl.isFinished()) {
impl.resetState();
}
return noffset;
}
/**
* Gets the object deserialized by {@link execute(byte[])} method.
*/
public MessagePackObject getData() {
return impl.getData();
}
/**
* Returns true if an object is ready to get with {@link getData()} method.
*/
public boolean isFinished() {
return impl.isFinished();
}
/**
* Resets the internal state of the unpacker.
*/
public void reset() {
super.reset();
parsed = 0;
impl.reset();
}
public int getMessageSize() {
return parsed - offset + used;
return parsed - impl.offset + impl.filled;
}
public int getParsedSize() {
@@ -167,88 +405,168 @@ public class Unpacker extends UnpackerImpl implements Iterable<Object> {
}
public int getNonParsedSize() {
return used - offset;
return impl.filled - impl.offset;
}
public void skipNonparsedBuffer(int size) {
offset += size;
impl.offset += size;
}
public void removeNonparsedBuffer() {
used = offset;
impl.filled = impl.offset;
}
/*
public static class Context {
private boolean finished;
private Object data;
private int offset;
private UnpackerImpl impl;
public Context()
{
this.finished = false;
this.impl = new UnpackerImpl();
}
public boolean isFinished()
{
return finished;
}
public Object getData()
{
return data;
}
int getOffset()
{
return offset;
}
void setFinished(boolean finished)
{
this.finished = finished;
}
void setData(Object data)
{
this.data = data;
}
void setOffset(int offset)
{
this.offset = offset;
}
UnpackerImpl getImpl()
{
return impl;
}
}
public static int unpack(Context ctx, byte[] buffer) throws UnpackException
{
return unpack(ctx, buffer, 0, buffer.length);
}
public static int unpack(Context ctx, byte[] buffer, int offset, int length) throws UnpackException
{
UnpackerImpl impl = ctx.getImpl();
int noffset = impl.execute(buffer, offset + ctx.getOffset(), length);
ctx.setOffset(noffset - offset);
if(impl.isFinished()) {
ctx.setData(impl.getData());
ctx.setFinished(false);
impl.reset();
} else {
ctx.setData(null);
ctx.setFinished(true);
}
int parsed = noffset - offset;
ctx.setOffset(parsed);
return noffset;
}
/**
* Gets one {@code byte} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code byte}.
*/
public byte unpackByte() throws IOException, MessageTypeException {
return impl.unpackByte();
}
/**
* Gets one {@code short} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code short}.
*/
public short unpackShort() throws IOException, MessageTypeException {
return impl.unpackShort();
}
/**
* Gets one {@code int} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code int}.
*/
public int unpackInt() throws IOException, MessageTypeException {
return impl.unpackInt();
}
/**
* Gets one {@code long} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code long}.
*/
public long unpackLong() throws IOException, MessageTypeException {
return impl.unpackLong();
}
/**
* Gets one {@code BigInteger} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code BigInteger}.
*/
public BigInteger unpackBigInteger() throws IOException, MessageTypeException {
return impl.unpackBigInteger();
}
/**
* Gets one {@code float} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code float}.
*/
public float unpackFloat() throws IOException, MessageTypeException {
return impl.unpackFloat();
}
/**
* Gets one {@code double} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code double}.
*/
public double unpackDouble() throws IOException, MessageTypeException {
return impl.unpackDouble();
}
/**
* Gets one {@code null} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code null}.
*/
public Object unpackNull() throws IOException, MessageTypeException {
return impl.unpackNull();
}
/**
* Gets one {@code boolean} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code boolean}.
*/
public boolean unpackBoolean() throws IOException, MessageTypeException {
return impl.unpackBoolean();
}
/**
* Gets one array header from the buffer.
* This method calls {@link fill()} method if needed.
* @return the length of the map. There are {@code retval} objects to get.
* @throws MessageTypeException the first value of the buffer is not a array.
*/
public int unpackArray() throws IOException, MessageTypeException {
return impl.unpackArray();
}
/**
* Gets one map header from the buffer.
* This method calls {@link fill()} method if needed.
* @return the length of the map. There are {@code retval * 2} objects to get.
* @throws MessageTypeException the first value of the buffer is not a map.
*/
public int unpackMap() throws IOException, MessageTypeException {
return impl.unpackMap();
}
/**
* Gets one raw header from the buffer.
* This method calls {@link fill()} method if needed.
* @return the length of the raw bytes. There are {@code retval} bytes to get.
* @throws MessageTypeException the first value of the buffer is not a raw bytes.
*/
public int unpackRaw() throws IOException, MessageTypeException {
return impl.unpackRaw();
}
/**
* Gets one raw body from the buffer.
* This method calls {@link fill()} method if needed.
*/
public byte[] unpackRawBody(int length) throws IOException {
return impl.unpackRawBody(length);
}
/**
* Gets one raw bytes from the buffer.
* This method calls {@link fill()} method if needed.
*/
public byte[] unpackByteArray() throws IOException {
return impl.unpackByteArray();
}
/**
* Gets one {@code String} value from the buffer.
* This method calls {@link fill()} method if needed.
* @throws MessageTypeException the first value of the buffer is not a {@code String}.
*/
final public String unpackString() throws IOException, MessageTypeException {
return impl.unpackString();
}
/**
* Gets one {@code Object} value from the buffer.
* This method calls {@link fill()} method if needed.
*/
final public MessagePackObject unpackObject() throws IOException {
return impl.unpackObject();
}
final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
obj.messageUnpack(this);
}
final public boolean tryUnpackNull() throws IOException {
return impl.tryUnpackNull();
}
}

View File

@@ -15,14 +15,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.impl;
package org.msgpack;
import java.nio.ByteBuffer;
//import java.math.BigInteger;
import org.msgpack.*;
import org.msgpack.schema.GenericSchema;
import org.msgpack.schema.IMapSchema;
import org.msgpack.schema.IArraySchema;
import java.math.BigInteger;
import org.msgpack.object.*;
public class UnpackerImpl {
static final int CS_HEADER = 0x00;
@@ -47,7 +44,7 @@ public class UnpackerImpl {
static final int CT_MAP_KEY = 0x01;
static final int CT_MAP_VALUE = 0x02;
static final int MAX_STACK_SIZE = 16;
static final int MAX_STACK_SIZE = 32;
private int cs;
private int trail;
@@ -55,53 +52,45 @@ public class UnpackerImpl {
private int[] stack_ct = new int[MAX_STACK_SIZE];
private int[] stack_count = new int[MAX_STACK_SIZE];
private Object[] stack_obj = new Object[MAX_STACK_SIZE];
private Schema[] stack_schema = new Schema[MAX_STACK_SIZE];
private int top_ct;
private int top_count;
private Object top_obj;
private Schema top_schema;
private ByteBuffer castBuffer = ByteBuffer.allocate(8);
private boolean finished = false;
private Object data = null;
private MessagePackObject data = null;
private static final Schema GENERIC_SCHEMA = new GenericSchema();
private Schema rootSchema;
protected UnpackerImpl()
public UnpackerImpl()
{
setSchema(GENERIC_SCHEMA);
}
protected void setSchema(Schema schema)
{
this.rootSchema = schema;
reset();
}
protected Object getData()
public final MessagePackObject getData()
{
return data;
}
protected boolean isFinished()
public final boolean isFinished()
{
return finished;
}
protected void reset()
{
public final void resetState() {
cs = CS_HEADER;
top = -1;
finished = false;
data = null;
top_ct = 0;
top_count = 0;
top_obj = null;
top_schema = rootSchema;
}
public final void reset()
{
resetState();
finished = false;
data = null;
}
@SuppressWarnings("unchecked")
protected int execute(byte[] src, int off, int length) throws UnpackException
public final int execute(byte[] src, int off, int length) throws UnpackException
{
if(off >= length) { return off; }
@@ -123,20 +112,20 @@ public class UnpackerImpl {
if((b & 0x80) == 0) { // Positive Fixnum
//System.out.println("positive fixnum "+b);
obj = top_schema.createFromByte((byte)b);
obj = IntegerType.create((byte)b);
break _push;
}
if((b & 0xe0) == 0xe0) { // Negative Fixnum
//System.out.println("negative fixnum "+b);
obj = top_schema.createFromByte((byte)b);
obj = IntegerType.create((byte)b);
break _push;
}
if((b & 0xe0) == 0xa0) { // FixRaw
trail = b & 0x1f;
if(trail == 0) {
obj = top_schema.createFromRaw(new byte[0], 0, 0);
obj = RawType.create(new byte[0]);
break _push;
}
cs = ACS_RAW_VALUE;
@@ -147,22 +136,20 @@ public class UnpackerImpl {
if(top >= MAX_STACK_SIZE) {
throw new UnpackException("parse error");
}
if(!(top_schema instanceof IArraySchema)) {
throw new RuntimeException("type error");
}
count = b & 0x0f;
//System.out.println("fixarray count:"+count);
obj = new Object[count];
if(count == 0) { break _push; } // FIXME check IArraySchema
obj = new MessagePackObject[count];
if(count == 0) {
obj = ArrayType.create((MessagePackObject[])obj);
break _push;
}
++top;
stack_obj[top] = top_obj;
stack_ct[top] = top_ct;
stack_count[top] = top_count;
stack_schema[top] = top_schema;
top_obj = obj;
top_ct = CT_ARRAY_ITEM;
top_count = count;
top_schema = ((IArraySchema)top_schema).getElementSchema(0);
break _header_again;
}
@@ -170,34 +157,32 @@ public class UnpackerImpl {
if(top >= MAX_STACK_SIZE) {
throw new UnpackException("parse error");
}
if(!(top_schema instanceof IMapSchema)) {
throw new RuntimeException("type error");
}
count = b & 0x0f;
obj = new Object[count*2];
if(count == 0) { break _push; } // FIXME check IMapSchema
obj = new MessagePackObject[count*2];
if(count == 0) {
obj = MapType.create((MessagePackObject[])obj);
break _push;
}
//System.out.println("fixmap count:"+count);
++top;
stack_obj[top] = top_obj;
stack_ct[top] = top_ct;
stack_count[top] = top_count;
stack_schema[top] = top_schema;
top_obj = obj;
top_ct = CT_MAP_KEY;
top_count = count;
top_schema = ((IMapSchema)top_schema).getKeySchema();
break _header_again;
}
switch(b & 0xff) { // FIXME
case 0xc0: // nil
obj = top_schema.createFromNil();
obj = NilType.create();
break _push;
case 0xc2: // false
obj = top_schema.createFromBoolean(false);
obj = BooleanType.create(false);
break _push;
case 0xc3: // true
obj = top_schema.createFromBoolean(true);
obj = BooleanType.create(true);
break _push;
case 0xca: // float
case 0xcb: // double
@@ -241,13 +226,13 @@ public class UnpackerImpl {
case CS_FLOAT:
castBuffer.rewind();
castBuffer.put(src, n, 4);
obj = top_schema.createFromFloat( castBuffer.getFloat(0) );
obj = FloatType.create( castBuffer.getFloat(0) );
//System.out.println("float "+obj);
break _push;
case CS_DOUBLE:
castBuffer.rewind();
castBuffer.put(src, n, 8);
obj = top_schema.createFromDouble( castBuffer.getDouble(0) );
obj = FloatType.create( castBuffer.getDouble(0) );
//System.out.println("double "+obj);
break _push;
case CS_UINT_8:
@@ -255,7 +240,7 @@ public class UnpackerImpl {
//System.out.println(src[n]);
//System.out.println(src[n+1]);
//System.out.println(src[n-1]);
obj = top_schema.createFromShort( (short)((src[n]) & 0xff) );
obj = IntegerType.create( (short)((src[n]) & 0xff) );
//System.out.println("uint8 "+obj);
break _push;
case CS_UINT_16:
@@ -263,13 +248,13 @@ public class UnpackerImpl {
//System.out.println(src[n+1]);
castBuffer.rewind();
castBuffer.put(src, n, 2);
obj = top_schema.createFromInt( ((int)castBuffer.getShort(0)) & 0xffff );
obj = IntegerType.create( ((int)castBuffer.getShort(0)) & 0xffff );
//System.out.println("uint 16 "+obj);
break _push;
case CS_UINT_32:
castBuffer.rewind();
castBuffer.put(src, n, 4);
obj = top_schema.createFromLong( ((long)castBuffer.getInt(0)) & 0xffffffffL );
obj = IntegerType.create( ((long)castBuffer.getInt(0)) & 0xffffffffL );
//System.out.println("uint 32 "+obj);
break _push;
case CS_UINT_64:
@@ -278,38 +263,36 @@ public class UnpackerImpl {
{
long o = castBuffer.getLong(0);
if(o < 0) {
// FIXME
//obj = GenericBigInteger.valueOf(o & 0x7fffffffL).setBit(31);
throw new UnpackException("uint 64 bigger than 0x7fffffff is not supported");
obj = IntegerType.create(new BigInteger(1, castBuffer.array()));
} else {
obj = top_schema.createFromLong( o );
obj = IntegerType.create(o);
}
}
break _push;
case CS_INT_8:
obj = top_schema.createFromByte( src[n] );
obj = IntegerType.create( src[n] );
break _push;
case CS_INT_16:
castBuffer.rewind();
castBuffer.put(src, n, 2);
obj = top_schema.createFromShort( castBuffer.getShort(0) );
obj = IntegerType.create( castBuffer.getShort(0) );
break _push;
case CS_INT_32:
castBuffer.rewind();
castBuffer.put(src, n, 4);
obj = top_schema.createFromInt( castBuffer.getInt(0) );
obj = IntegerType.create( castBuffer.getInt(0) );
break _push;
case CS_INT_64:
castBuffer.rewind();
castBuffer.put(src, n, 8);
obj = top_schema.createFromLong( castBuffer.getLong(0) );
obj = IntegerType.create( castBuffer.getLong(0) );
break _push;
case CS_RAW_16:
castBuffer.rewind();
castBuffer.put(src, n, 2);
trail = ((int)castBuffer.getShort(0)) & 0xffff;
if(trail == 0) {
obj = top_schema.createFromRaw(new byte[0], 0, 0);
obj = RawType.create(new byte[0]);
break _push;
}
cs = ACS_RAW_VALUE;
@@ -320,104 +303,100 @@ public class UnpackerImpl {
// FIXME overflow check
trail = castBuffer.getInt(0) & 0x7fffffff;
if(trail == 0) {
obj = top_schema.createFromRaw(new byte[0], 0, 0);
obj = RawType.create(new byte[0]);
break _push;
}
cs = ACS_RAW_VALUE;
case ACS_RAW_VALUE:
obj = top_schema.createFromRaw(src, n, trail);
break _fixed_trail_again;
case ACS_RAW_VALUE: {
byte[] raw = new byte[trail];
System.arraycopy(src, n, raw, 0, trail);
obj = RawType.create(raw);
}
break _push;
case CS_ARRAY_16:
if(top >= MAX_STACK_SIZE) {
throw new UnpackException("parse error");
}
if(!(top_schema instanceof IArraySchema)) {
throw new RuntimeException("type error");
}
castBuffer.rewind();
castBuffer.put(src, n, 2);
count = ((int)castBuffer.getShort(0)) & 0xffff;
obj = new Object[count];
if(count == 0) { break _push; } // FIXME check IArraySchema
obj = new MessagePackObject[count];
if(count == 0) {
obj = ArrayType.create((MessagePackObject[])obj);
break _push;
}
++top;
stack_obj[top] = top_obj;
stack_ct[top] = top_ct;
stack_count[top] = top_count;
stack_schema[top] = top_schema;
top_obj = obj;
top_ct = CT_ARRAY_ITEM;
top_count = count;
top_schema = ((IArraySchema)top_schema).getElementSchema(0);
break _header_again;
case CS_ARRAY_32:
if(top >= MAX_STACK_SIZE) {
throw new UnpackException("parse error");
}
if(!(top_schema instanceof IArraySchema)) {
throw new RuntimeException("type error");
}
castBuffer.rewind();
castBuffer.put(src, n, 4);
// FIXME overflow check
count = castBuffer.getInt(0) & 0x7fffffff;
obj = new Object[count];
if(count == 0) { break _push; } // FIXME check IArraySchema
obj = new MessagePackObject[count];
if(count == 0) {
obj = ArrayType.create((MessagePackObject[])obj);
break _push;
}
++top;
stack_obj[top] = top_obj;
stack_ct[top] = top_ct;
stack_count[top] = top_count;
stack_schema[top] = top_schema;
top_obj = obj;
top_ct = CT_ARRAY_ITEM;
top_count = count;
top_schema = ((IArraySchema)top_schema).getElementSchema(0);
break _header_again;
case CS_MAP_16:
if(top >= MAX_STACK_SIZE) {
throw new UnpackException("parse error");
}
if(!(top_schema instanceof IMapSchema)) {
throw new RuntimeException("type error");
}
castBuffer.rewind();
castBuffer.put(src, n, 2);
count = ((int)castBuffer.getShort(0)) & 0xffff;
obj = new Object[count*2];
if(count == 0) { break _push; } // FIXME check IMapSchema
obj = new MessagePackObject[count*2];
if(count == 0) {
obj = MapType.create((MessagePackObject[])obj);
break _push;
}
//System.out.println("fixmap count:"+count);
++top;
stack_obj[top] = top_obj;
stack_ct[top] = top_ct;
stack_count[top] = top_count;
stack_schema[top] = top_schema;
top_obj = obj;
top_ct = CT_MAP_KEY;
top_count = count;
top_schema = ((IMapSchema)top_schema).getKeySchema();
break _header_again;
case CS_MAP_32:
if(top >= MAX_STACK_SIZE) {
throw new UnpackException("parse error");
}
if(!(top_schema instanceof IMapSchema)) {
throw new RuntimeException("type error");
}
castBuffer.rewind();
castBuffer.put(src, n, 4);
// FIXME overflow check
count = castBuffer.getInt(0) & 0x7fffffff;
obj = new Object[count*2];
if(count == 0) { break _push; } // FIXME check IMapSchema
obj = new MessagePackObject[count*2];
if(count == 0) {
obj = MapType.create((MessagePackObject[])obj);
break _push;
}
//System.out.println("fixmap count:"+count);
++top;
stack_obj[top] = top_obj;
stack_ct[top] = top_ct;
stack_count[top] = top_count;
stack_schema[top] = top_schema;
top_obj = obj;
top_ct = CT_MAP_KEY;
top_count = count;
top_schema = ((IMapSchema)top_schema).getKeySchema();
break _header_again;
default:
throw new UnpackException("parse error");
@@ -432,7 +411,7 @@ public class UnpackerImpl {
//System.out.println("push top:"+top);
if(top == -1) {
++i;
data = obj;
data = (MessagePackObject)obj;
finished = true;
break _out;
}
@@ -446,14 +425,10 @@ public class UnpackerImpl {
top_obj = stack_obj[top];
top_ct = stack_ct[top];
top_count = stack_count[top];
top_schema = stack_schema[top];
obj = ((IArraySchema)top_schema).createFromArray(ar);
obj = ArrayType.create((MessagePackObject[])ar);
stack_obj[top] = null;
stack_schema[top] = null;
--top;
break _push;
} else {
top_schema = ((IArraySchema)stack_schema[top]).getElementSchema(ar.length - top_count);
}
break _header_again;
}
@@ -462,7 +437,6 @@ public class UnpackerImpl {
Object[] mp = (Object[])top_obj;
mp[mp.length - top_count*2] = obj;
top_ct = CT_MAP_VALUE;
top_schema = ((IMapSchema)stack_schema[top]).getValueSchema();
break _header_again;
}
case CT_MAP_VALUE: {
@@ -473,10 +447,8 @@ public class UnpackerImpl {
top_obj = stack_obj[top];
top_ct = stack_ct[top];
top_count = stack_count[top];
top_schema = stack_schema[top];
obj = ((IMapSchema)top_schema).createFromMap(mp);
obj = MapType.create((MessagePackObject[])mp);
stack_obj[top] = null;
stack_schema[top] = null;
--top;
break _push;
}

View File

@@ -0,0 +1,81 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack.object;
import java.util.List;
import java.util.Arrays;
import java.io.IOException;
import org.msgpack.*;
public class ArrayType extends MessagePackObject {
private MessagePackObject[] array;
ArrayType(MessagePackObject[] array) {
this.array = array;
}
public static ArrayType create(MessagePackObject[] array) {
return new ArrayType(array);
}
@Override
public boolean isArrayType() {
return true;
}
@Override
public MessagePackObject[] asArray() {
return array;
}
@Override
public List<MessagePackObject> asList() {
return Arrays.asList(array);
}
@Override
public void messagePack(Packer pk) throws IOException {
pk.packArray(array.length);
for(int i=0; i < array.length; i++) {
array[i].messagePack(pk);
}
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()) {
return false;
}
return Arrays.equals(((ArrayType)obj).array, array);
}
@Override
public int hashCode() {
return array.hashCode();
}
@Override
public Object clone() {
MessagePackObject[] copy = new MessagePackObject[array.length];
for(int i=0; i < array.length; i++) {
copy[i] = (MessagePackObject)array[i].clone();
}
return copy;
}
}

View File

@@ -0,0 +1,131 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack.object;
import java.math.BigInteger;
import java.io.IOException;
import org.msgpack.*;
class BigIntegerTypeIMPL extends IntegerType {
private BigInteger value;
BigIntegerTypeIMPL(BigInteger value) {
this.value = value;
}
@Override
public byte asByte() {
if(value.compareTo(BigInteger.valueOf((long)Byte.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.byteValue();
}
@Override
public short asShort() {
if(value.compareTo(BigInteger.valueOf((long)Short.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.shortValue();
}
@Override
public int asInt() {
if(value.compareTo(BigInteger.valueOf((long)Integer.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.intValue();
}
@Override
public long asLong() {
if(value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
throw new MessageTypeException("type error");
}
return value.longValue();
}
@Override
public BigInteger asBigInteger() {
return value;
}
@Override
public byte byteValue() {
return value.byteValue();
}
@Override
public short shortValue() {
return value.shortValue();
}
@Override
public int intValue() {
return value.intValue();
}
@Override
public long longValue() {
return value.longValue();
}
@Override
public BigInteger bigIntegerValue() {
return value;
}
@Override
public float floatValue() {
return value.floatValue();
}
@Override
public double doubleValue() {
return value.doubleValue();
}
@Override
public void messagePack(Packer pk) throws IOException {
pk.packBigInteger(value);
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()) {
if(obj.getClass() == ShortIntegerTypeIMPL.class) {
return BigInteger.valueOf(((ShortIntegerTypeIMPL)obj).longValue()).equals(value);
} else if(obj.getClass() == LongIntegerTypeIMPL.class) {
return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value);
}
return false;
}
return ((BigIntegerTypeIMPL)obj).value.equals(value);
}
@Override
public int hashCode() {
return value.hashCode();
}
@Override
public Object clone() {
return new BigIntegerTypeIMPL(value);
}
}

View File

@@ -0,0 +1,71 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack.object;
import java.io.IOException;
import org.msgpack.*;
public class BooleanType extends MessagePackObject {
private boolean value;
BooleanType(boolean value) {
this.value = value;
}
public static BooleanType create(boolean value) {
return new BooleanType(value);
}
@Override
public boolean isBooleanType() {
return true;
}
@Override
public boolean asBoolean() {
return value;
}
@Override
public void messagePack(Packer pk) throws IOException {
pk.packBoolean(value);
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()) {
return false;
}
return ((BooleanType)obj).value == value;
}
@Override
public int hashCode() {
if(value) {
return 1231;
} else {
return 1237;
}
}
@Override
public Object clone() {
return new BooleanType(value);
}
}

View File

@@ -0,0 +1,101 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack.object;
import java.math.BigInteger;
import java.io.IOException;
import org.msgpack.*;
class DoubleTypeIMPL extends FloatType {
private double value;
public DoubleTypeIMPL(double value) {
this.value = value;
}
@Override
public float asFloat() {
// FIXME check overflow, underflow?
return (float)value;
}
@Override
public double asDouble() {
return value;
}
@Override
public byte byteValue() {
return (byte)value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public BigInteger bigIntegerValue() {
return BigInteger.valueOf((long)value);
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
@Override
public void messagePack(Packer pk) throws IOException {
pk.packDouble(value);
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()) {
return false;
}
return ((DoubleTypeIMPL)obj).value == value;
}
@Override
public int hashCode() {
long v = Double.doubleToLongBits(value);
return (int)(v^(v>>>32));
}
@Override
public Object clone() {
return new DoubleTypeIMPL(value);
}
}

View File

@@ -15,13 +15,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.schema;
package org.msgpack.object;
import org.msgpack.Schema;
import org.msgpack.*;
public interface IMapSchema {
public Schema getKeySchema();
public Schema getValueSchema();
public Object createFromMap(Object[] obj);
public abstract class FloatType extends MessagePackObject {
@Override
public boolean isFloatType() {
return true;
}
public static FloatType create(float value) {
return new FloatTypeIMPL(value);
}
public static FloatType create(double value) {
return new DoubleTypeIMPL(value);
}
}

View File

@@ -0,0 +1,94 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 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.
//
package org.msgpack.object;
import java.math.BigInteger;
import java.io.IOException;
import org.msgpack.*;
class FloatTypeIMPL extends FloatType {
private float value;
public FloatTypeIMPL(float value) {
this.value = value;
}
@Override
public float asFloat() {
return value;
}
@Override
public double asDouble() {
return (double)value;
}
@Override
public byte byteValue() {
return (byte)value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
@Override
public void messagePack(Packer pk) throws IOException {
pk.packFloat(value);
}
@Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()) {
return false;
}
return ((FloatTypeIMPL)obj).value == value;
}
@Override
public int hashCode() {
return Float.floatToIntBits(value);
}
@Override
public Object clone() {
return new FloatTypeIMPL(value);
}
}

Some files were not shown because too many files have changed in this diff Show More