From f51123d009ebe285b91442d03e220560ee75ef21 Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Mon, 17 May 2010 05:49:39 +0900 Subject: [PATCH 001/152] oops --- perl/Makefile.PL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perl/Makefile.PL b/perl/Makefile.PL index 5bf43f3..58ab7c7 100644 --- a/perl/Makefile.PL +++ b/perl/Makefile.PL @@ -32,7 +32,7 @@ if ($Module::Install::AUTHOR && -d File::Spec->catfile('..', 'msgpack')) { } } -requires 'Test::More' => 0.95; # done_testing +requires 'Test::More' => 0.94; # done_testing test_requires('Test::Requires'); auto_set_repository(); From 6ea75f3a9f1888d70bfaf97468922e30a7e7e661 Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Mon, 17 May 2010 05:52:32 +0900 Subject: [PATCH 002/152] Perl: do not use done_testing --- perl/t/06_stream_unpack2.t | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/perl/t/06_stream_unpack2.t b/perl/t/06_stream_unpack2.t index dc82c41..eaf2cb4 100644 --- a/perl/t/06_stream_unpack2.t +++ b/perl/t/06_stream_unpack2.t @@ -1,7 +1,7 @@ use strict; use warnings; use Data::MessagePack; -use Test::More; +use Test::More tests => 6; my $input = [(undef)x16]; my $packed = Data::MessagePack->pack($input); @@ -22,5 +22,4 @@ is_deeply(Data::MessagePack->unpack($packed), $input); is_deeply $up->data, $input; } -done_testing; From 18967162cfc9737764b0e4862936d32364f2fc84 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 18 May 2010 14:48:23 +0900 Subject: [PATCH 003/152] cpp: fix return type mismatch in unpack.c --- cpp/unpack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/unpack.c b/cpp/unpack.c index 34016fd..4a42526 100644 --- a/cpp/unpack.c +++ b/cpp/unpack.c @@ -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); From 5cad81bf4ce2bc46f5b3ec4663c0eda8d3dff469 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 18 May 2010 14:48:36 +0900 Subject: [PATCH 004/152] cpp: fix return type mismatch in zone.c --- cpp/zone.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/zone.c b/cpp/zone.c index 3d0634e..85de765 100644 --- a/cpp/zone.c +++ b/cpp/zone.c @@ -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); From 979ff809827ab25005364dad41d2fd043b8eaa4d Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 20 May 2010 03:49:26 +0900 Subject: [PATCH 005/152] java: redesign --- cpp/msgpack/type/nil.hpp | 8 + .../org/msgpack/BufferedUnpackerImpl.java | 384 ++++++++++++++++++ ...Mergeable.java => MessageConvertable.java} | 4 +- .../org/msgpack/MessageTypeException.java | 4 +- .../java/org/msgpack/MessageUnpackable.java | 25 ++ java/src/main/java/org/msgpack/Packer.java | 2 +- .../java/org/msgpack/UnbufferedUnpacker.java | 82 ---- .../main/java/org/msgpack/UnpackCursor.java | 96 +++++ .../main/java/org/msgpack/UnpackIterator.java | 24 +- .../main/java/org/msgpack/UnpackResult.java | 42 ++ java/src/main/java/org/msgpack/Unpacker.java | 269 ++++++------ .../org/msgpack/{impl => }/UnpackerImpl.java | 26 +- .../org/msgpack/TestDirectConversion.java | 154 +++++++ .../test/java/org/msgpack/TestPackUnpack.java | 2 +- 14 files changed, 864 insertions(+), 258 deletions(-) create mode 100644 java/src/main/java/org/msgpack/BufferedUnpackerImpl.java rename java/src/main/java/org/msgpack/{MessageMergeable.java => MessageConvertable.java} (86%) create mode 100644 java/src/main/java/org/msgpack/MessageUnpackable.java delete mode 100644 java/src/main/java/org/msgpack/UnbufferedUnpacker.java create mode 100644 java/src/main/java/org/msgpack/UnpackCursor.java create mode 100644 java/src/main/java/org/msgpack/UnpackResult.java rename java/src/main/java/org/msgpack/{impl => }/UnpackerImpl.java (97%) create mode 100644 java/src/test/java/org/msgpack/TestDirectConversion.java diff --git a/cpp/msgpack/type/nil.hpp b/cpp/msgpack/type/nil.hpp index e58bc9d..f44e45e 100644 --- a/cpp/msgpack/type/nil.hpp +++ b/cpp/msgpack/type/nil.hpp @@ -51,6 +51,14 @@ inline void operator<< (object::with_zone& o, type::nil v) { static_cast(o) << v; } +template <> +inline void object::as() const +{ + msgpack::type::nil v; + convert(&v); +} + + } // namespace msgpack #endif /* msgpack/type/nil.hpp */ diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java new file mode 100644 index 0000000..0ef9c12 --- /dev/null +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -0,0 +1,384 @@ +// +// 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 filled = 0; + byte[] buffer = null; + private ByteBuffer castBuffer = ByteBuffer.allocate(8); + + abstract boolean fill() throws IOException; + + final int next(int offset, UnpackResult result) throws IOException, UnpackException { + if(filled == 0) { + if(!fill()) { + return offset; + } + } + + do { + int noffset = super.execute(buffer, offset, filled); + if(noffset <= offset) { + if(!fill()) { + return offset; + } + } + offset = noffset; + } while(!super.isFinished()); + + Object obj = super.getData(); + super.reset(); + result.done(obj); + + return offset; + } + + private final void more(int offset, int require) throws IOException, UnpackException { + while(filled - offset < require) { + if(!fill()) { + // FIXME + throw new UnpackException("insufficient buffer"); + } + } + } + + final byte unpackByte(UnpackCursor c, int offset) throws IOException, MessageTypeException { + int o = unpackInt(c, offset); + if(0x7f < o || o < -0x80) { + throw new MessageTypeException(); + } + return (byte)o; + } + + final short unpackShort(UnpackCursor c, int offset) throws IOException, MessageTypeException { + int o = unpackInt(c, offset); + if(0x7fff < o || o < -0x8000) { + throw new MessageTypeException(); + } + return (short)o; + } + + final int unpackInt(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset]; + if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum + return (int)b; + } + switch(b & 0xff) { + case 0xcc: // unsigned int 8 + more(offset, 2); + c.advance(2); + return (int)((short)buffer[offset+1] & 0xff); + case 0xcd: // unsigned int 16 + more(offset, 3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + c.advance(3); + return (int)((int)castBuffer.getShort(0) & 0xffff); + case 0xce: // unsigned int 32 + more(offset, 5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + { + int o = castBuffer.getInt(0); + if(o < 0) { + throw new MessageTypeException(); + } + c.advance(5); + return o; + } + case 0xcf: // unsigned int 64 + more(offset, 9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + { + long o = castBuffer.getLong(0); + if(o < 0 || o > 0x7fffffffL) { + throw new MessageTypeException(); + } + c.advance(9); + return (int)o; + } + case 0xd0: // signed int 8 + more(offset, 2); + c.advance(2); + return (int)buffer[offset+1]; + case 0xd1: // signed int 16 + more(offset, 3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + c.advance(3); + return (int)castBuffer.getShort(0); + case 0xd2: // signed int 32 + more(offset, 4); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(4); + return (int)castBuffer.getInt(0); + case 0xd3: // signed int 64 + more(offset, 9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + { + long o = castBuffer.getLong(0); + if(0x7fffffffL < o || o < -0x80000000L) { + throw new MessageTypeException(); + } + c.advance(9); + return (int)o; + } + default: + throw new MessageTypeException(); + } + } + + final long unpackLong(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset]; + if((b & 0x80) == 0 || (b & 0xe0) == 0xe0) { // Fixnum + return (long)b; + } + switch(b & 0xff) { + case 0xcc: // unsigned int 8 + more(offset, 2); + c.advance(2); + return (long)((short)buffer[offset+1] & 0xff); + case 0xcd: // unsigned int 16 + more(offset, 3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + c.advance(3); + return (long)((int)castBuffer.getShort(0) & 0xffff); + case 0xce: // unsigned int 32 + more(offset, 5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(5); + return ((long)castBuffer.getInt(0) & 0xffffffffL); + case 0xcf: // unsigned int 64 + more(offset, 9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + { + long o = castBuffer.getLong(0); + if(o < 0) { + // FIXME + throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported"); + } + c.advance(9); + return o; + } + case 0xd0: // signed int 8 + more(offset, 2); + c.advance(2); + return (long)buffer[offset+1]; + case 0xd1: // signed int 16 + more(offset, 3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + c.advance(3); + return (long)castBuffer.getShort(0); + case 0xd2: // signed int 32 + more(offset, 4); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(4); + return (long)castBuffer.getInt(0); + case 0xd3: // signed int 64 + more(offset, 9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + c.advance(9); + return (long)castBuffer.getLong(0); + default: + throw new MessageTypeException(); + } + } + + final float unpackFloat(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset]; + switch(b & 0xff) { + case 0xca: // float + more(offset, 5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(5); + return castBuffer.getFloat(0); + case 0xcb: // double + more(offset, 9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + c.advance(9); + // FIXME overflow check + return (float)castBuffer.getDouble(0); + default: + throw new MessageTypeException(); + } + } + + final double unpackDouble(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset]; + switch(b & 0xff) { + case 0xca: // float + more(offset, 5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(5); + return (double)castBuffer.getFloat(0); + case 0xcb: // double + more(offset, 9); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 8); + c.advance(9); + return castBuffer.getDouble(0); + default: + throw new MessageTypeException(); + } + } + + final Object unpackNull(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset] & 0xff; + if(b != 0xc0) { // nil + throw new MessageTypeException(); + } + c.advance(1); + return null; + } + + final boolean unpackBoolean(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset] & 0xff; + if(b == 0xc2) { // false + c.advance(1); + return false; + } else if(b == 0xc3) { // true + c.advance(1); + return true; + } else { + throw new MessageTypeException(); + } + } + + final int unpackArray(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset]; + if((b & 0xf0) == 0x90) { // FixArray + c.advance(1); + return (int)(b & 0x0f); + } + switch(b & 0xff) { + case 0xdc: // array 16 + more(offset, 3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + c.advance(3); + return (int)castBuffer.getShort(0) & 0xffff; + case 0xdd: // array 32 + more(offset, 5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(5); + // FIXME overflow check + return castBuffer.getInt(0) & 0x7fffffff; + default: + throw new MessageTypeException(); + } + } + + final int unpackMap(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset]; + if((b & 0xf0) == 0x80) { // FixMap + c.advance(1); + return (int)(b & 0x0f); + } + switch(b & 0xff) { + case 0xde: // map 16 + more(offset, 3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + c.advance(3); + return (int)castBuffer.getShort(0) & 0xffff; + case 0xdf: // map 32 + more(offset, 5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(5); + // FIXME overflow check + return castBuffer.getInt(0) & 0x7fffffff; + default: + throw new MessageTypeException(); + } + } + + final int unpackRaw(UnpackCursor c, int offset) throws IOException, MessageTypeException { + more(offset, 1); + int b = buffer[offset]; + if((b & 0xe0) == 0xa0) { // FixRaw + c.advance(1); + return (int)(b & 0x0f); + } + switch(b & 0xff) { + case 0xda: // raw 16 + more(offset, 3); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 2); + c.advance(3); + return (int)castBuffer.getShort(0) & 0xffff; + case 0xdb: // raw 32 + more(offset, 5); + castBuffer.rewind(); + castBuffer.put(buffer, offset+1, 4); + c.advance(5); + // FIXME overflow check + return castBuffer.getInt(0) & 0x7fffffff; + default: + throw new MessageTypeException(); + } + } + + final byte[] unpackRawBody(UnpackCursor c, int offset, int length) throws IOException, MessageTypeException { + more(offset, length); + byte[] bytes = new byte[length]; + System.arraycopy(buffer, offset, bytes, 0, length); + c.advance(length); + return bytes; + } + + final String unpackString(UnpackCursor c, int offset) throws IOException, MessageTypeException { + int length = unpackRaw(c, offset); + offset = c.getOffset(); + more(offset, length); + String s; + try { + s = new String(buffer, offset, length, "UTF-8"); + } catch (Exception e) { + throw new MessageTypeException(); + } + c.advance(length); + return s; + } +} + diff --git a/java/src/main/java/org/msgpack/MessageMergeable.java b/java/src/main/java/org/msgpack/MessageConvertable.java similarity index 86% rename from java/src/main/java/org/msgpack/MessageMergeable.java rename to java/src/main/java/org/msgpack/MessageConvertable.java index e5a5b45..da251dc 100644 --- a/java/src/main/java/org/msgpack/MessageMergeable.java +++ b/java/src/main/java/org/msgpack/MessageConvertable.java @@ -17,7 +17,7 @@ // package org.msgpack; -public interface MessageMergeable { - public void messageMerge(Object obj) throws MessageTypeException; +public interface MessageConvertable { + public void messageConvert(Object obj) throws MessageTypeException; } diff --git a/java/src/main/java/org/msgpack/MessageTypeException.java b/java/src/main/java/org/msgpack/MessageTypeException.java index feb6c08..0fa37b7 100644 --- a/java/src/main/java/org/msgpack/MessageTypeException.java +++ b/java/src/main/java/org/msgpack/MessageTypeException.java @@ -17,9 +17,7 @@ // package org.msgpack; -import java.io.IOException; - -public class MessageTypeException extends IOException { +public class MessageTypeException extends RuntimeException { public MessageTypeException() { } public MessageTypeException(String s) { diff --git a/java/src/main/java/org/msgpack/MessageUnpackable.java b/java/src/main/java/org/msgpack/MessageUnpackable.java new file mode 100644 index 0000000..20e4d56 --- /dev/null +++ b/java/src/main/java/org/msgpack/MessageUnpackable.java @@ -0,0 +1,25 @@ +// +// 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; + +public interface MessageUnpackable { + public void messageUnpack(Unpacker pk) throws IOException, MessageTypeException; +} + diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 935728d..6d79414 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -401,7 +401,7 @@ public class Packer { } else if(o instanceof Double) { return packDouble((Double)o); } else { - throw new IOException("unknown object "+o+" ("+o.getClass()+")"); + throw new MessageTypeException("unknown object "+o+" ("+o.getClass()+")"); } } } diff --git a/java/src/main/java/org/msgpack/UnbufferedUnpacker.java b/java/src/main/java/org/msgpack/UnbufferedUnpacker.java deleted file mode 100644 index b427973..0000000 --- a/java/src/main/java/org/msgpack/UnbufferedUnpacker.java +++ /dev/null @@ -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; - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackCursor.java b/java/src/main/java/org/msgpack/UnpackCursor.java new file mode 100644 index 0000000..33f4258 --- /dev/null +++ b/java/src/main/java/org/msgpack/UnpackCursor.java @@ -0,0 +1,96 @@ +// +// 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; + +public class UnpackCursor { + private Unpacker pac; + private int offset; + + UnpackCursor(Unpacker pac, int offset) + { + this.pac = pac; + this.offset = offset; + } + + final void advance(int length) { + offset += length; + } + + final int getOffset() { + return offset; + } + + public byte unpackByte() throws IOException, MessageTypeException { + return pac.impl.unpackByte(this, offset); + } + + public short unpackShort() throws IOException, MessageTypeException { + return pac.impl.unpackShort(this, offset); + } + + public int unpackInt() throws IOException, MessageTypeException { + return pac.impl.unpackInt(this, offset); + } + + public long unpackLong() throws IOException, MessageTypeException { + return pac.impl.unpackLong(this, offset); + } + + public float unpackFloat() throws IOException, MessageTypeException { + return pac.impl.unpackFloat(this, offset); + } + + public double unpackDouble() throws IOException, MessageTypeException { + return pac.impl.unpackDouble(this, offset); + } + + public Object unpackNull() throws IOException, MessageTypeException { + return pac.impl.unpackNull(this, offset); + } + + public boolean unpackBoolean() throws IOException, MessageTypeException { + return pac.impl.unpackBoolean(this, offset); + } + + public int unpackArray() throws IOException, MessageTypeException { + return pac.impl.unpackArray(this, offset); + } + + public int unpackMap() throws IOException, MessageTypeException { + return pac.impl.unpackMap(this, offset); + } + + public int unpackRaw() throws IOException, MessageTypeException { + return pac.impl.unpackRaw(this, offset); + } + + public byte[] unpackRawBody(int length) throws IOException, MessageTypeException { + return pac.impl.unpackRawBody(this, offset, length); + } + + public String unpackString() throws IOException, MessageTypeException { + return pac.impl.unpackString(this, offset); + } + + public void commit() { + pac.setOffset(offset); + } +} + diff --git a/java/src/main/java/org/msgpack/UnpackIterator.java b/java/src/main/java/org/msgpack/UnpackIterator.java index 0a78e83..5cc994d 100644 --- a/java/src/main/java/org/msgpack/UnpackIterator.java +++ b/java/src/main/java/org/msgpack/UnpackIterator.java @@ -21,41 +21,27 @@ import java.io.IOException; import java.util.Iterator; import java.util.NoSuchElementException; -public class UnpackIterator implements Iterator { +public class UnpackIterator extends UnpackResult implements Iterator { 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; } 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()) { + if(!finished && !hasNext()) { throw new NoSuchElementException(); } - have = false; + finished = false; return data; } diff --git a/java/src/main/java/org/msgpack/UnpackResult.java b/java/src/main/java/org/msgpack/UnpackResult.java new file mode 100644 index 0000000..cec18a1 --- /dev/null +++ b/java/src/main/java/org/msgpack/UnpackResult.java @@ -0,0 +1,42 @@ +// +// 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; + +public class UnpackResult { + protected boolean finished = false; + protected Object data = null; + + public boolean isFinished() { + return finished; + } + + public Object getData() { + return data; + } + + public void reset() { + finished = false; + data = null; + } + + void done(Object obj) { + finished = true; + data = obj; + } +} + diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index c8a8823..20a8c4a 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -22,18 +22,43 @@ import java.io.InputStream; import java.io.IOException; import java.util.Iterator; import java.nio.ByteBuffer; -import org.msgpack.impl.UnpackerImpl; -public class Unpacker extends UnpackerImpl implements Iterable { +public class Unpacker implements Iterable { - public static final int DEFAULT_BUFFER_SIZE = 32*1024; + // buffer: + // +---------------------------------------------+ + // | [object] | [obje| unparsed ... | unused ...| + // +---------------------------------------------+ + // ^ parsed + // ^ offset + // ^ filled + // ^ buffer.length + + private static final int DEFAULT_BUFFER_SIZE = 32*1024; + + protected int offset; + protected int parsed; + protected int bufferReserveSize; + protected InputStream stream; + + class BufferedUnpackerMixin extends BufferedUnpackerImpl { + boolean fill() throws IOException { + if(stream == null) { + return false; + } + reserveBuffer(bufferReserveSize); + int rl = stream.read(buffer, filled, buffer.length - filled); + // equals: stream.read(getBuffer(), getBufferOffset(), getBufferCapacity()); + if(rl <= 0) { + return false; + } + bufferConsumed(rl); + return true; + } + }; + + final BufferedUnpackerMixin impl = new BufferedUnpackerMixin(); - private int used; - private int offset; - private int parsed; - private byte[] buffer; - private int bufferReserveSize; - private InputStream stream; public Unpacker() { this(DEFAULT_BUFFER_SIZE); @@ -48,67 +73,31 @@ public class Unpacker extends UnpackerImpl implements Iterable { } 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); + impl.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; - } - */ - int nextSize = buffer.length * 2; - while(nextSize < size + used) { - nextSize *= 2; - } - - byte[] tmp = new byte[nextSize]; - System.arraycopy(buffer, offset, tmp, 0, used - offset); - - buffer = tmp; - used -= offset; - offset = 0; + public InputStream getStream() { + return this.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 setStream(InputStream stream) { + this.stream = stream; } public void feed(ByteBuffer buffer) { int length = buffer.remaining(); if (length == 0) return; reserveBuffer(length); - buffer.get(this.buffer, this.offset, length); + buffer.get(impl.buffer, this.offset, length); bufferConsumed(length); } @@ -118,48 +107,116 @@ public class Unpacker extends UnpackerImpl implements Iterable { public void feed(byte[] buffer, int offset, int length) { reserveBuffer(length); - System.arraycopy(buffer, offset, this.buffer, this.offset, length); + System.arraycopy(buffer, offset, impl.buffer, this.offset, length); bufferConsumed(length); } public boolean fill() throws IOException { - if(stream == null) { - return false; - } - reserveBuffer(bufferReserveSize); - int rl = stream.read(getBuffer(), getBufferOffset(), getBufferCapacity()); - if(rl <= 0) { - return false; - } - bufferConsumed(rl); - return true; + return impl.fill(); } public Iterator iterator() { return new UnpackIterator(this); } + public UnpackResult next() throws IOException, UnpackException { + UnpackResult result = new UnpackResult(); + this.offset = impl.next(this.offset, result); + return result; + } + + public boolean next(UnpackResult result) throws IOException, UnpackException { + this.offset = impl.next(this.offset, result); + return result.isFinished(); + } + + + public void reserveBuffer(int require) { + if(impl.buffer == null) { + int nextSize = (bufferReserveSize < require) ? require : bufferReserveSize; + impl.buffer = new byte[nextSize]; + return; + } + + if(impl.buffer.length - impl.filled >= require) { + return; + } + + int nextSize = impl.buffer.length * 2; + int notParsed = impl.filled - this.offset; + while(nextSize < require + notParsed) { + nextSize *= 2; + } + + byte[] tmp = new byte[nextSize]; + System.arraycopy(impl.buffer, this.offset, tmp, 0, impl.filled - this.offset); + + impl.buffer = tmp; + impl.filled = notParsed; + this.offset = 0; + } + + public byte[] getBuffer() { + return impl.buffer; + } + + public int getBufferOffset() { + return impl.filled; + } + + public int getBufferCapacity() { + return impl.buffer.length - impl.filled; + } + + public void bufferConsumed(int size) { + impl.filled += size; + } + public boolean execute() throws UnpackException { - int noffset = super.execute(buffer, offset, used); + int noffset = impl.execute(impl.buffer, offset, impl.filled); if(noffset <= offset) { return false; } parsed += noffset - offset; offset = noffset; - return super.isFinished(); + return impl.isFinished(); + } + + + public int execute(byte[] buffer) throws UnpackException { + return execute(buffer, 0, buffer.length); + } + + public int execute(byte[] buffer, int offset, int length) throws UnpackException { + int noffset = impl.execute(buffer, offset + this.offset, length); + this.offset = noffset - offset; + if(impl.isFinished()) { + impl.resetState(); + } + return noffset; + } + + public boolean isFinished() { + return impl.isFinished(); } public Object getData() { - return super.getData(); + return impl.getData(); } public void reset() { - super.reset(); - parsed = 0; + impl.reset(); } + + public UnpackCursor begin() + { + return new UnpackCursor(this, offset); + } + + public int getMessageSize() { - return parsed - offset + used; + return parsed - offset + impl.filled; } public int getParsedSize() { @@ -167,7 +224,7 @@ public class Unpacker extends UnpackerImpl implements Iterable { } public int getNonParsedSize() { - return used - offset; + return impl.filled - offset; } public void skipNonparsedBuffer(int size) { @@ -175,80 +232,14 @@ public class Unpacker extends UnpackerImpl implements Iterable { } public void removeNonparsedBuffer() { - used = offset; + impl.filled = 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 + void setOffset(int offset) { - return unpack(ctx, buffer, 0, buffer.length); + parsed += offset - this.offset; + this.offset = offset; } - - 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; - } - */ } diff --git a/java/src/main/java/org/msgpack/impl/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java similarity index 97% rename from java/src/main/java/org/msgpack/impl/UnpackerImpl.java rename to java/src/main/java/org/msgpack/UnpackerImpl.java index adc62b0..ae01289 100644 --- a/java/src/main/java/org/msgpack/impl/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -15,7 +15,7 @@ // 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; @@ -47,7 +47,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; @@ -67,41 +67,45 @@ public class UnpackerImpl { private static final Schema GENERIC_SCHEMA = new GenericSchema(); private Schema rootSchema; - protected UnpackerImpl() + public UnpackerImpl() { setSchema(GENERIC_SCHEMA); } - protected void setSchema(Schema schema) + public void setSchema(Schema schema) { this.rootSchema = schema; reset(); } - protected Object getData() + public final Object 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; } diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java new file mode 100644 index 0000000..d77fe13 --- /dev/null +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -0,0 +1,154 @@ +package org.msgpack; + +import org.msgpack.*; +import java.io.*; +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestDirectConversion { + private UnpackCursor prepareCursor(ByteArrayOutputStream out) { + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + return upk.begin(); + } + + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } + public void testInt(int val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + UnpackCursor c = prepareCursor(out); + assertEquals(val, c.unpackInt()); + c.commit(); + } + + @Test + public void testFloat() throws Exception { + testFloat((float)0.0); + testFloat((float)-0.0); + testFloat((float)1.0); + testFloat((float)-1.0); + testFloat((float)Float.MAX_VALUE); + testFloat((float)Float.MIN_VALUE); + testFloat((float)Float.NaN); + testFloat((float)Float.NEGATIVE_INFINITY); + testFloat((float)Float.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testFloat(rand.nextFloat()); + } + public void testFloat(float val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + UnpackCursor c = prepareCursor(out); + float f = c.unpackFloat(); + if(Float.isNaN(val)) { + assertTrue(Float.isNaN(f)); + } else { + assertEquals(val, f, 10e-10); + } + c.commit(); + } + + @Test + public void testDouble() throws Exception { + testDouble((double)0.0); + testDouble((double)-0.0); + testDouble((double)1.0); + testDouble((double)-1.0); + testDouble((double)Double.MAX_VALUE); + testDouble((double)Double.MIN_VALUE); + testDouble((double)Double.NaN); + testDouble((double)Double.NEGATIVE_INFINITY); + testDouble((double)Double.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testDouble(rand.nextDouble()); + } + public void testDouble(double val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + UnpackCursor c = prepareCursor(out); + double f = c.unpackDouble(); + if(Double.isNaN(val)) { + assertTrue(Double.isNaN(f)); + } else { + assertEquals(val, f, 10e-10); + } + c.commit(); + } + + @Test + public void testNil() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).packNil(); + UnpackCursor c = prepareCursor(out); + assertEquals(null, c.unpackNull()); + c.commit(); + } + + @Test + public void testBoolean() throws Exception { + testBoolean(false); + testBoolean(true); + } + public void testBoolean(boolean val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + UnpackCursor c = prepareCursor(out); + assertEquals(val, c.unpackBoolean()); + c.commit(); + } + + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + // small size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 31 + 1; + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + // medium size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 15); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + // large size string + for (int i = 0; i < 10; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 31); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + } + public void testString(String val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + UnpackCursor c = prepareCursor(out); + assertEquals(val, c.unpackString()); + c.commit(); + } + + // FIXME container types +}; diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 6877853..a16b5b1 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -237,5 +237,5 @@ public class TestPackUnpack { System.out.println("Got unexpected class: " + obj.getClass()); assertTrue(false); } - } + } }; From 135a9f558600ddbd4cd0d07a57ae1f7fb5b8634a Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 20 May 2010 05:44:44 +0900 Subject: [PATCH 006/152] java: fix direct conversion API --- .../org/msgpack/BufferedUnpackerImpl.java | 205 ++++++++++-------- .../java/org/msgpack/MessageUnpackable.java | 2 +- .../main/java/org/msgpack/UnpackCursor.java | 96 -------- .../main/java/org/msgpack/UnpackIterator.java | 1 + java/src/main/java/org/msgpack/Unpacker.java | 102 ++++++--- .../org/msgpack/schema/ClassGenerator.java | 10 +- .../msgpack/schema/SpecificClassSchema.java | 4 +- .../org/msgpack/TestDirectConversion.java | 42 ++-- java/test/README | 2 +- .../tpc/src/serializers/BenchmarkRunner.java | 2 + .../src/serializers/msgpack/MediaContent.java | 78 ++++++- .../msgpack/MessagePackDirectSerializer.java | 68 ++++++ .../msgpack/MessagePackDynamicSerializer.java | 2 +- .../msgpack/MessagePackGenericSerializer.java | 2 +- .../MessagePackIndirectSerializer.java | 2 +- .../MessagePackSpecificSerializer.java | 2 +- 16 files changed, 356 insertions(+), 264 deletions(-) delete mode 100644 java/src/main/java/org/msgpack/UnpackCursor.java create mode 100644 java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index 0ef9c12..5fde4e1 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -22,16 +22,17 @@ 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 int next(int offset, UnpackResult result) throws IOException, UnpackException { + final boolean next(UnpackResult result) throws IOException, UnpackException { if(filled == 0) { if(!fill()) { - return offset; + return false; } } @@ -39,8 +40,9 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { int noffset = super.execute(buffer, offset, filled); if(noffset <= offset) { if(!fill()) { - return offset; + return false; } + continue; } offset = noffset; } while(!super.isFinished()); @@ -49,10 +51,10 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { super.reset(); result.done(obj); - return offset; + return true; } - private final void more(int offset, int require) throws IOException, UnpackException { + private final void more(int require) throws IOException, UnpackException { while(filled - offset < require) { if(!fill()) { // FIXME @@ -61,41 +63,46 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - final byte unpackByte(UnpackCursor c, int offset) throws IOException, MessageTypeException { - int o = unpackInt(c, offset); + 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(UnpackCursor c, int offset) throws IOException, MessageTypeException { - int o = unpackInt(c, offset); + final short unpackShort() throws IOException, MessageTypeException { + int o = unpackInt(); if(0x7fff < o || o < -0x8000) { throw new MessageTypeException(); } return (short)o; } - final int unpackInt(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + 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(offset, 2); - c.advance(2); + more(2); + advance(2); return (int)((short)buffer[offset+1] & 0xff); case 0xcd: // unsigned int 16 - more(offset, 3); + more(3); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 2); - c.advance(3); + advance(3); return (int)((int)castBuffer.getShort(0) & 0xffff); case 0xce: // unsigned int 32 - more(offset, 5); + more(5); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); { @@ -103,11 +110,11 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { if(o < 0) { throw new MessageTypeException(); } - c.advance(5); + advance(5); return o; } case 0xcf: // unsigned int 64 - more(offset, 9); + more(9); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 8); { @@ -115,27 +122,27 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { if(o < 0 || o > 0x7fffffffL) { throw new MessageTypeException(); } - c.advance(9); + advance(9); return (int)o; } case 0xd0: // signed int 8 - more(offset, 2); - c.advance(2); + more(2); + advance(2); return (int)buffer[offset+1]; case 0xd1: // signed int 16 - more(offset, 3); + more(3); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 2); - c.advance(3); + advance(3); return (int)castBuffer.getShort(0); case 0xd2: // signed int 32 - more(offset, 4); + more(4); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(4); + advance(4); return (int)castBuffer.getInt(0); case 0xd3: // signed int 64 - more(offset, 9); + more(9); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 8); { @@ -143,7 +150,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { if(0x7fffffffL < o || o < -0x80000000L) { throw new MessageTypeException(); } - c.advance(9); + advance(9); return (int)o; } default: @@ -151,31 +158,32 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - final long unpackLong(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + 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(offset, 2); - c.advance(2); + more(2); + advance(2); return (long)((short)buffer[offset+1] & 0xff); case 0xcd: // unsigned int 16 - more(offset, 3); + more(3); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 2); - c.advance(3); + advance(3); return (long)((int)castBuffer.getShort(0) & 0xffff); case 0xce: // unsigned int 32 - more(offset, 5); + more(5); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(5); + advance(5); return ((long)castBuffer.getInt(0) & 0xffffffffL); case 0xcf: // unsigned int 64 - more(offset, 9); + more(9); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 8); { @@ -184,51 +192,51 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { // FIXME throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported"); } - c.advance(9); + advance(9); return o; } case 0xd0: // signed int 8 - more(offset, 2); - c.advance(2); + more(2); + advance(2); return (long)buffer[offset+1]; case 0xd1: // signed int 16 - more(offset, 3); + more(3); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 2); - c.advance(3); + advance(3); return (long)castBuffer.getShort(0); case 0xd2: // signed int 32 - more(offset, 4); + more(4); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(4); + advance(4); return (long)castBuffer.getInt(0); case 0xd3: // signed int 64 - more(offset, 9); + more(9); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 8); - c.advance(9); + advance(9); return (long)castBuffer.getLong(0); default: throw new MessageTypeException(); } } - final float unpackFloat(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + final float unpackFloat() throws IOException, MessageTypeException { + more(1); int b = buffer[offset]; switch(b & 0xff) { case 0xca: // float - more(offset, 5); + more(5); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(5); + advance(5); return castBuffer.getFloat(0); case 0xcb: // double - more(offset, 9); + more(9); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 8); - c.advance(9); + advance(9); // FIXME overflow check return (float)castBuffer.getDouble(0); default: @@ -236,70 +244,70 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - final double unpackDouble(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + final double unpackDouble() throws IOException, MessageTypeException { + more(1); int b = buffer[offset]; switch(b & 0xff) { case 0xca: // float - more(offset, 5); + more(5); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(5); + advance(5); return (double)castBuffer.getFloat(0); case 0xcb: // double - more(offset, 9); + more(9); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 8); - c.advance(9); + advance(9); return castBuffer.getDouble(0); default: throw new MessageTypeException(); } } - final Object unpackNull(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + final Object unpackNull() throws IOException, MessageTypeException { + more(1); int b = buffer[offset] & 0xff; if(b != 0xc0) { // nil throw new MessageTypeException(); } - c.advance(1); + advance(1); return null; } - final boolean unpackBoolean(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + final boolean unpackBoolean() throws IOException, MessageTypeException { + more(1); int b = buffer[offset] & 0xff; if(b == 0xc2) { // false - c.advance(1); + advance(1); return false; } else if(b == 0xc3) { // true - c.advance(1); + advance(1); return true; } else { throw new MessageTypeException(); } } - final int unpackArray(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + final int unpackArray() throws IOException, MessageTypeException { + more(1); int b = buffer[offset]; if((b & 0xf0) == 0x90) { // FixArray - c.advance(1); + advance(1); return (int)(b & 0x0f); } switch(b & 0xff) { case 0xdc: // array 16 - more(offset, 3); + more(3); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 2); - c.advance(3); + advance(3); return (int)castBuffer.getShort(0) & 0xffff; case 0xdd: // array 32 - more(offset, 5); + more(5); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(5); + advance(5); // FIXME overflow check return castBuffer.getInt(0) & 0x7fffffff; default: @@ -307,25 +315,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - final int unpackMap(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + final int unpackMap() throws IOException, MessageTypeException { + more(1); int b = buffer[offset]; if((b & 0xf0) == 0x80) { // FixMap - c.advance(1); + advance(1); return (int)(b & 0x0f); } switch(b & 0xff) { case 0xde: // map 16 - more(offset, 3); + more(3); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 2); - c.advance(3); + advance(3); return (int)castBuffer.getShort(0) & 0xffff; case 0xdf: // map 32 - more(offset, 5); + more(5); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(5); + advance(5); // FIXME overflow check return castBuffer.getInt(0) & 0x7fffffff; default: @@ -333,25 +341,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - final int unpackRaw(UnpackCursor c, int offset) throws IOException, MessageTypeException { - more(offset, 1); + final int unpackRaw() throws IOException, MessageTypeException { + more(1); int b = buffer[offset]; if((b & 0xe0) == 0xa0) { // FixRaw - c.advance(1); - return (int)(b & 0x0f); + advance(1); + return (int)(b & 0x1f); } switch(b & 0xff) { case 0xda: // raw 16 - more(offset, 3); + more(3); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 2); - c.advance(3); + advance(3); return (int)castBuffer.getShort(0) & 0xffff; case 0xdb: // raw 32 - more(offset, 5); + more(5); castBuffer.rewind(); castBuffer.put(buffer, offset+1, 4); - c.advance(5); + advance(5); // FIXME overflow check return castBuffer.getInt(0) & 0x7fffffff; default: @@ -359,26 +367,35 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - final byte[] unpackRawBody(UnpackCursor c, int offset, int length) throws IOException, MessageTypeException { - more(offset, length); + final byte[] unpackRawBody(int length) throws IOException, MessageTypeException { + more(length); byte[] bytes = new byte[length]; System.arraycopy(buffer, offset, bytes, 0, length); - c.advance(length); + advance(length); return bytes; } - final String unpackString(UnpackCursor c, int offset) throws IOException, MessageTypeException { - int length = unpackRaw(c, offset); - offset = c.getOffset(); - more(offset, 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(); } - c.advance(length); + advance(length); return s; } + + final Object unpackObject() throws IOException, MessageTypeException { + // FIXME save state, restore state + UnpackResult result = new UnpackResult(); + if(!next(result)) { + super.reset(); + throw new MessageTypeException(); + } + return result.getData(); + } } diff --git a/java/src/main/java/org/msgpack/MessageUnpackable.java b/java/src/main/java/org/msgpack/MessageUnpackable.java index 20e4d56..cc206e7 100644 --- a/java/src/main/java/org/msgpack/MessageUnpackable.java +++ b/java/src/main/java/org/msgpack/MessageUnpackable.java @@ -20,6 +20,6 @@ package org.msgpack; import java.io.IOException; public interface MessageUnpackable { - public void messageUnpack(Unpacker pk) throws IOException, MessageTypeException; + public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException; } diff --git a/java/src/main/java/org/msgpack/UnpackCursor.java b/java/src/main/java/org/msgpack/UnpackCursor.java deleted file mode 100644 index 33f4258..0000000 --- a/java/src/main/java/org/msgpack/UnpackCursor.java +++ /dev/null @@ -1,96 +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.IOException; - -public class UnpackCursor { - private Unpacker pac; - private int offset; - - UnpackCursor(Unpacker pac, int offset) - { - this.pac = pac; - this.offset = offset; - } - - final void advance(int length) { - offset += length; - } - - final int getOffset() { - return offset; - } - - public byte unpackByte() throws IOException, MessageTypeException { - return pac.impl.unpackByte(this, offset); - } - - public short unpackShort() throws IOException, MessageTypeException { - return pac.impl.unpackShort(this, offset); - } - - public int unpackInt() throws IOException, MessageTypeException { - return pac.impl.unpackInt(this, offset); - } - - public long unpackLong() throws IOException, MessageTypeException { - return pac.impl.unpackLong(this, offset); - } - - public float unpackFloat() throws IOException, MessageTypeException { - return pac.impl.unpackFloat(this, offset); - } - - public double unpackDouble() throws IOException, MessageTypeException { - return pac.impl.unpackDouble(this, offset); - } - - public Object unpackNull() throws IOException, MessageTypeException { - return pac.impl.unpackNull(this, offset); - } - - public boolean unpackBoolean() throws IOException, MessageTypeException { - return pac.impl.unpackBoolean(this, offset); - } - - public int unpackArray() throws IOException, MessageTypeException { - return pac.impl.unpackArray(this, offset); - } - - public int unpackMap() throws IOException, MessageTypeException { - return pac.impl.unpackMap(this, offset); - } - - public int unpackRaw() throws IOException, MessageTypeException { - return pac.impl.unpackRaw(this, offset); - } - - public byte[] unpackRawBody(int length) throws IOException, MessageTypeException { - return pac.impl.unpackRawBody(this, offset, length); - } - - public String unpackString() throws IOException, MessageTypeException { - return pac.impl.unpackString(this, offset); - } - - public void commit() { - pac.setOffset(offset); - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackIterator.java b/java/src/main/java/org/msgpack/UnpackIterator.java index 5cc994d..f17e229 100644 --- a/java/src/main/java/org/msgpack/UnpackIterator.java +++ b/java/src/main/java/org/msgpack/UnpackIterator.java @@ -30,6 +30,7 @@ public class UnpackIterator extends UnpackResult implements Iterator { } public boolean hasNext() { + if(finished) { return true; } try { return pac.next(this); } catch (IOException e) { diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 20a8c4a..4d8da7b 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -36,7 +36,6 @@ public class Unpacker implements Iterable { private static final int DEFAULT_BUFFER_SIZE = 32*1024; - protected int offset; protected int parsed; protected int bufferReserveSize; protected InputStream stream; @@ -73,7 +72,6 @@ public class Unpacker implements Iterable { } public Unpacker(InputStream stream, int bufferReserveSize) { - this.offset = 0; this.parsed = 0; this.bufferReserveSize = bufferReserveSize/2; this.stream = stream; @@ -97,7 +95,7 @@ public class Unpacker implements Iterable { int length = buffer.remaining(); if (length == 0) return; reserveBuffer(length); - buffer.get(impl.buffer, this.offset, length); + buffer.get(impl.buffer, impl.offset, length); bufferConsumed(length); } @@ -107,7 +105,7 @@ public class Unpacker implements Iterable { public void feed(byte[] buffer, int offset, int length) { reserveBuffer(length); - System.arraycopy(buffer, offset, impl.buffer, this.offset, length); + System.arraycopy(buffer, offset, impl.buffer, impl.offset, length); bufferConsumed(length); } @@ -121,13 +119,12 @@ public class Unpacker implements Iterable { public UnpackResult next() throws IOException, UnpackException { UnpackResult result = new UnpackResult(); - this.offset = impl.next(this.offset, result); + impl.next(result); return result; } public boolean next(UnpackResult result) throws IOException, UnpackException { - this.offset = impl.next(this.offset, result); - return result.isFinished(); + return impl.next(result); } @@ -143,17 +140,17 @@ public class Unpacker implements Iterable { } int nextSize = impl.buffer.length * 2; - int notParsed = impl.filled - this.offset; + int notParsed = impl.filled - impl.offset; while(nextSize < require + notParsed) { nextSize *= 2; } byte[] tmp = new byte[nextSize]; - System.arraycopy(impl.buffer, this.offset, tmp, 0, impl.filled - this.offset); + System.arraycopy(impl.buffer, impl.offset, tmp, 0, impl.filled - impl.offset); impl.buffer = tmp; impl.filled = notParsed; - this.offset = 0; + impl.offset = 0; } public byte[] getBuffer() { @@ -173,12 +170,12 @@ public class Unpacker implements Iterable { } public boolean execute() throws UnpackException { - int noffset = impl.execute(impl.buffer, offset, impl.filled); - if(noffset <= offset) { + int noffset = impl.execute(impl.buffer, impl.offset, impl.filled); + if(noffset <= impl.offset) { return false; } - parsed += noffset - offset; - offset = noffset; + parsed += noffset - impl.offset; + impl.offset = noffset; return impl.isFinished(); } @@ -188,8 +185,8 @@ public class Unpacker implements Iterable { } public int execute(byte[] buffer, int offset, int length) throws UnpackException { - int noffset = impl.execute(buffer, offset + this.offset, length); - this.offset = noffset - offset; + int noffset = impl.execute(buffer, offset + impl.offset, length); + impl.offset = noffset - offset; if(impl.isFinished()) { impl.resetState(); } @@ -208,15 +205,8 @@ public class Unpacker implements Iterable { impl.reset(); } - - public UnpackCursor begin() - { - return new UnpackCursor(this, offset); - } - - public int getMessageSize() { - return parsed - offset + impl.filled; + return parsed - impl.offset + impl.filled; } public int getParsedSize() { @@ -224,22 +214,72 @@ public class Unpacker implements Iterable { } public int getNonParsedSize() { - return impl.filled - offset; + return impl.filled - impl.offset; } public void skipNonparsedBuffer(int size) { - offset += size; + impl.offset += size; } public void removeNonparsedBuffer() { - impl.filled = offset; + impl.filled = impl.offset; } - void setOffset(int offset) - { - parsed += offset - this.offset; - this.offset = offset; + final public byte unpackByte() throws IOException, MessageTypeException { + return impl.unpackByte(); + } + + final public short unpackShort() throws IOException, MessageTypeException { + return impl.unpackShort(); + } + + final public int unpackInt() throws IOException, MessageTypeException { + return impl.unpackInt(); + } + + final public long unpackLong() throws IOException, MessageTypeException { + return impl.unpackLong(); + } + + final public float unpackFloat() throws IOException, MessageTypeException { + return impl.unpackFloat(); + } + + final public double unpackDouble() throws IOException, MessageTypeException { + return impl.unpackDouble(); + } + + final public Object unpackNull() throws IOException, MessageTypeException { + return impl.unpackNull(); + } + + final public boolean unpackBoolean() throws IOException, MessageTypeException { + return impl.unpackBoolean(); + } + + final public int unpackArray() throws IOException, MessageTypeException { + return impl.unpackArray(); + } + + final public int unpackMap() throws IOException, MessageTypeException { + return impl.unpackMap(); + } + + final public int unpackRaw() throws IOException, MessageTypeException { + return impl.unpackRaw(); + } + + final public byte[] unpackRawBody(int length) throws IOException, MessageTypeException { + return impl.unpackRawBody(length); + } + + final public String unpackString() throws IOException, MessageTypeException { + return impl.unpackString(); + } + + final public Object unpackObject() throws IOException, MessageTypeException { + return impl.unpackObject(); } } diff --git a/java/src/main/java/org/msgpack/schema/ClassGenerator.java b/java/src/main/java/org/msgpack/schema/ClassGenerator.java index 061dcbb..f8a13fa 100644 --- a/java/src/main/java/org/msgpack/schema/ClassGenerator.java +++ b/java/src/main/java/org/msgpack/schema/ClassGenerator.java @@ -105,7 +105,7 @@ public class ClassGenerator { private void writeClass() throws IOException { line(); - line("public final class "+schema.getName()+" implements MessagePackable, MessageMergeable"); + line("public final class "+schema.getName()+" implements MessagePackable, MessageConvertable"); line("{"); pushIndent(); writeSchema(); @@ -117,7 +117,7 @@ public class ClassGenerator { private void writeSubclass() throws IOException { line(); - line("final class "+schema.getName()+" implements MessagePackable, MessageMergeable"); + line("final class "+schema.getName()+" implements MessagePackable, MessageConvertable"); line("{"); pushIndent(); writeSchema(); @@ -150,7 +150,7 @@ public class ClassGenerator { writeConstructors(); writeAccessors(); writePackFunction(); - writeMergeFunction(); + writeConvertFunction(); writeFactoryFunction(); } @@ -184,11 +184,11 @@ public class ClassGenerator { line("}"); } - private void writeMergeFunction() throws IOException { + private void writeConvertFunction() throws IOException { line(); line("@Override"); line("@SuppressWarnings(\"unchecked\")"); - line("public void messageMerge(Object obj) throws MessageTypeException"); + line("public void messageConvert(Object obj) throws MessageTypeException"); line("{"); pushIndent(); line("Object[] _source = ((List)obj).toArray();"); diff --git a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java b/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java index 30bd9e1..850f621 100644 --- a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java +++ b/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java @@ -59,8 +59,8 @@ public class SpecificClassSchema extends ClassSchema { cacheConstructor(); } try { - MessageMergeable o = (MessageMergeable)constructorCache.newInstance((Object[])null); - o.messageMerge(obj); + MessageConvertable o = (MessageConvertable)constructorCache.newInstance((Object[])null); + o.messageConvert(obj); return o; } catch (InvocationTargetException e) { throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java index d77fe13..77bbc58 100644 --- a/java/src/test/java/org/msgpack/TestDirectConversion.java +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -8,12 +8,6 @@ import org.junit.Test; import static org.junit.Assert.*; public class TestDirectConversion { - private UnpackCursor prepareCursor(ByteArrayOutputStream out) { - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - return upk.begin(); - } - @Test public void testInt() throws Exception { testInt(0); @@ -28,9 +22,9 @@ public class TestDirectConversion { public void testInt(int val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); - UnpackCursor c = prepareCursor(out); - assertEquals(val, c.unpackInt()); - c.commit(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + assertEquals(val, upk.unpackInt()); } @Test @@ -51,14 +45,14 @@ public class TestDirectConversion { public void testFloat(float val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); - UnpackCursor c = prepareCursor(out); - float f = c.unpackFloat(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + float f = upk.unpackFloat(); if(Float.isNaN(val)) { assertTrue(Float.isNaN(f)); } else { assertEquals(val, f, 10e-10); } - c.commit(); } @Test @@ -79,23 +73,23 @@ public class TestDirectConversion { public void testDouble(double val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); - UnpackCursor c = prepareCursor(out); - double f = c.unpackDouble(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + double f = upk.unpackDouble(); if(Double.isNaN(val)) { assertTrue(Double.isNaN(f)); } else { assertEquals(val, f, 10e-10); } - c.commit(); } @Test public void testNil() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).packNil(); - UnpackCursor c = prepareCursor(out); - assertEquals(null, c.unpackNull()); - c.commit(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + assertEquals(null, upk.unpackNull()); } @Test @@ -106,9 +100,9 @@ public class TestDirectConversion { public void testBoolean(boolean val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); - UnpackCursor c = prepareCursor(out); - assertEquals(val, c.unpackBoolean()); - c.commit(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + assertEquals(val, upk.unpackBoolean()); } @Test @@ -145,9 +139,9 @@ public class TestDirectConversion { public void testString(String val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); - UnpackCursor c = prepareCursor(out); - assertEquals(val, c.unpackString()); - c.commit(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + assertEquals(val, upk.unpackString()); } // FIXME container types diff --git a/java/test/README b/java/test/README index 4e16454..9b98d91 100644 --- a/java/test/README +++ b/java/test/README @@ -1,7 +1,7 @@ #!/bin/sh svn checkout -r114 http://thrift-protobuf-compare.googlecode.com/svn/trunk/ thrift-protobuf-compare-base cp -rf thrift-protobuf-compare/tpc thrift-protobuf-compare-base -cp ../dist/msgpack.jar thrift-protobuf-compare-base/tpc/lib/ +cp ../target/msgpack*.jar thrift-protobuf-compare-base/tpc/lib/msgpack.jar cd thrift-protobuf-compare-base/tpc/ ant compile ./run-benchmark.sh diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java index b17dfb2..fa88b6b 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/BenchmarkRunner.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.Set; import java.util.Map.Entry; +import serializers.msgpack.MessagePackDirectSerializer; import serializers.msgpack.MessagePackSpecificSerializer; import serializers.msgpack.MessagePackIndirectSerializer; import serializers.msgpack.MessagePackDynamicSerializer; @@ -39,6 +40,7 @@ public class BenchmarkRunner BenchmarkRunner runner = new BenchmarkRunner(); // binary codecs first + runner.addObjectSerializer(new MessagePackDirectSerializer()); runner.addObjectSerializer(new MessagePackSpecificSerializer()); runner.addObjectSerializer(new MessagePackIndirectSerializer()); runner.addObjectSerializer(new MessagePackDynamicSerializer()); diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java index 5dfbc8d..e750b5a 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MediaContent.java @@ -6,7 +6,7 @@ import org.msgpack.*; import org.msgpack.schema.ClassSchema; import org.msgpack.schema.FieldSchema; -public final class MediaContent implements MessagePackable, MessageMergeable +public final class MediaContent implements MessagePackable, MessageConvertable, MessageUnpackable { private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class MediaContent (package serializers.msgpack) (field image (array (class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int)))) (field media (class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))))"); public static ClassSchema getSchema() { return _SCHEMA; } @@ -27,7 +27,7 @@ public final class MediaContent implements MessagePackable, MessageMergeable @Override @SuppressWarnings("unchecked") - public void messageMerge(Object obj) throws MessageTypeException + public void messageConvert(Object obj) throws MessageTypeException { Object[] _source = ((List)obj).toArray(); FieldSchema[] _fields = _SCHEMA.getFields(); @@ -35,6 +35,23 @@ public final class MediaContent implements MessagePackable, MessageMergeable if(_source.length <= 1) { return; } this.media = (Media)_fields[1].getSchema().convert(_source[1]); } + @Override + public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException { + int _length = _pac.unpackArray(); + if(_length <= 0) { return; } + int _image_length = _pac.unpackArray(); + this.image = new ArrayList(_image_length); + for(int _i=0; _i < _image_length; ++_i) { + Image _image_i = new Image(); + _image_i.messageUnpack(_pac); + this.image.add(_image_i); + } + if(_length <= 1) { return; } + this.media = new Media(); + this.media.messageUnpack(_pac); + for(int _i=2; _i < _length; ++_i) { _pac.unpackObject(); } + } + @SuppressWarnings("unchecked") public static MediaContent createFromMessage(Object[] _message) { @@ -45,7 +62,7 @@ public final class MediaContent implements MessagePackable, MessageMergeable } } -final class Image implements MessagePackable, MessageMergeable +final class Image implements MessagePackable, MessageConvertable, MessageUnpackable { private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Image (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field size int))"); public static ClassSchema getSchema() { return _SCHEMA; } @@ -72,7 +89,7 @@ final class Image implements MessagePackable, MessageMergeable @Override @SuppressWarnings("unchecked") - public void messageMerge(Object obj) throws MessageTypeException + public void messageConvert(Object obj) throws MessageTypeException { Object[] _source = ((List)obj).toArray(); FieldSchema[] _fields = _SCHEMA.getFields(); @@ -83,6 +100,22 @@ final class Image implements MessagePackable, MessageMergeable if(_source.length <= 4) { return; } this.size = (Integer)_fields[4].getSchema().convert(_source[4]); } + @Override + public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException { + int _length = _pac.unpackArray(); + if(_length <= 0) { return; } + this.uri = _pac.unpackString(); + if(_length <= 1) { return; } + this.title = _pac.unpackString(); + if(_length <= 2) { return; } + this.width = _pac.unpackInt(); + if(_length <= 3) { return; } + this.height = _pac.unpackInt(); + if(_length <= 4) { return; } + this.size = _pac.unpackInt(); + for(int _i=5; _i < _length; ++_i) { _pac.unpackObject(); } + } + @SuppressWarnings("unchecked") public static Image createFromMessage(Object[] _message) { @@ -96,7 +129,7 @@ final class Image implements MessagePackable, MessageMergeable } } -final class Media implements MessagePackable, MessageMergeable +final class Media implements MessagePackable, MessageConvertable, MessageUnpackable { private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load("(class Media (package serializers.msgpack) (field uri string) (field title string) (field width int) (field height int) (field format string) (field duration long) (field size long) (field bitrate int) (field person (array string)) (field player int) (field copyright string))"); public static ClassSchema getSchema() { return _SCHEMA; } @@ -135,7 +168,7 @@ final class Media implements MessagePackable, MessageMergeable @Override @SuppressWarnings("unchecked") - public void messageMerge(Object obj) throws MessageTypeException + public void messageConvert(Object obj) throws MessageTypeException { Object[] _source = ((List)obj).toArray(); FieldSchema[] _fields = _SCHEMA.getFields(); @@ -152,6 +185,39 @@ final class Media implements MessagePackable, MessageMergeable if(_source.length <= 10) { return; } this.copyright = (String)_fields[10].getSchema().convert(_source[10]); } + @Override + public void messageUnpack(Unpacker _pac) throws IOException, MessageTypeException { + int _length = _pac.unpackArray(); + if(_length <= 0) { return; } + this.uri = _pac.unpackString(); + if(_length <= 1) { return; } + this.title = _pac.unpackString(); + if(_length <= 2) { return; } + this.width = _pac.unpackInt(); + if(_length <= 3) { return; } + this.height = _pac.unpackInt(); + if(_length <= 4) { return; } + this.format = _pac.unpackString(); + if(_length <= 5) { return; } + this.duration = _pac.unpackLong(); + if(_length <= 6) { return; } + this.size = _pac.unpackLong(); + if(_length <= 7) { return; } + this.bitrate = _pac.unpackInt(); + if(_length <= 8) { return; } + int _person_length = _pac.unpackArray(); + this.person = new ArrayList(_person_length); + for(int _i=0; _i < _person_length; ++_i) { + String _person_i = _pac.unpackString(); + this.person.add(_person_i); + } + if(_length <= 9) { return; } + this.player = _pac.unpackInt(); + if(_length <= 10) { return; } + this.copyright = _pac.unpackString(); + for(int _i=11; _i < _length; ++_i) { _pac.unpackObject(); } + } + @SuppressWarnings("unchecked") public static Media createFromMessage(Object[] _message) { diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java new file mode 100644 index 0000000..721e95b --- /dev/null +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java @@ -0,0 +1,68 @@ +package serializers.msgpack; + +import java.io.*; +import java.util.*; +import org.msgpack.*; +import serializers.ObjectSerializer; + +public class MessagePackDirectSerializer implements ObjectSerializer +{ + public String getName() { + return "msgpack-direct"; + } + + public MediaContent create() throws Exception { + Media media = new Media(); + media.uri = "http://javaone.com/keynote.mpg"; + media.format = "video/mpg4"; + media.title = "Javaone Keynote"; + media.duration = 1234567L; + media.bitrate = 0; + media.person = new ArrayList(2); + media.person.add("Bill Gates"); + media.person.add("Steve Jobs"); + media.player = 0; + media.height = 0; + media.width = 0; + media.size = 123L; + media.copyright = ""; + + Image image1 = new Image(); + image1.uri = "http://javaone.com/keynote_large.jpg"; + image1.width = 0; + image1.height = 0; + image1.size = 2; + image1.title = "Javaone Keynote"; + + Image image2 = new Image(); + image2.uri = "http://javaone.com/keynote_thumbnail.jpg"; + image2.width = 0; + image2.height = 0; + image2.size = 1; + image2.title = "Javaone Keynote"; + + MediaContent content = new MediaContent(); + content.media = media; + content.image = new ArrayList(2); + content.image.add(image1); + content.image.add(image2); + + return content; + } + + public byte[] serialize(MediaContent content) throws Exception { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + Packer pk = new Packer(os); + pk.pack(content); + return os.toByteArray(); + } + + public MediaContent deserialize(byte[] array) throws Exception { + Unpacker pac = new Unpacker(); + pac.feed(array); + MediaContent obj = new MediaContent(); + obj.messageUnpack(pac); + return obj; + } +} + diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java index c8a88ac..9c8ccbe 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDynamicSerializer.java @@ -60,7 +60,7 @@ public class MessagePackDynamicSerializer implements ObjectSerializer } public Object deserialize(byte[] array) throws Exception { - UnbufferedUnpacker pac = new UnbufferedUnpacker(); + Unpacker pac = new Unpacker(); pac.execute(array); return (Object)pac.getData(); } diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java index 4935899..316389a 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackGenericSerializer.java @@ -62,7 +62,7 @@ public class MessagePackGenericSerializer implements ObjectSerializer } public Object deserialize(byte[] array) throws Exception { - UnbufferedUnpacker pac = new UnbufferedUnpacker().useSchema(MEDIA_CONTENT_SCHEMA); + Unpacker pac = new Unpacker().useSchema(MEDIA_CONTENT_SCHEMA); pac.execute(array); return (Object)pac.getData(); } diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java index 2767474..e24472b 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackIndirectSerializer.java @@ -58,7 +58,7 @@ public class MessagePackIndirectSerializer implements ObjectSerializer Date: Thu, 20 May 2010 06:18:32 +0900 Subject: [PATCH 007/152] java: add Unpacker.wrap method --- java/src/main/java/org/msgpack/Unpacker.java | 36 +++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 4d8da7b..7fd6fcd 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -40,7 +40,7 @@ public class Unpacker implements Iterable { protected int bufferReserveSize; protected InputStream stream; - class BufferedUnpackerMixin extends BufferedUnpackerImpl { + final class BufferedUnpackerMixin extends BufferedUnpackerImpl { boolean fill() throws IOException { if(stream == null) { return false; @@ -109,6 +109,16 @@ public class Unpacker implements Iterable { bufferConsumed(length); } + public void wrap(byte[] buffer) { + wrap(buffer, 0, buffer.length); + } + + public void wrap(byte[] buffer, int offset, int length) { + impl.buffer = buffer; + impl.offset = offset; + impl.filled = length; + } + public boolean fill() throws IOException { return impl.fill(); } @@ -226,51 +236,51 @@ public class Unpacker implements Iterable { } - final public byte unpackByte() throws IOException, MessageTypeException { + public byte unpackByte() throws IOException, MessageTypeException { return impl.unpackByte(); } - final public short unpackShort() throws IOException, MessageTypeException { + public short unpackShort() throws IOException, MessageTypeException { return impl.unpackShort(); } - final public int unpackInt() throws IOException, MessageTypeException { + public int unpackInt() throws IOException, MessageTypeException { return impl.unpackInt(); } - final public long unpackLong() throws IOException, MessageTypeException { + public long unpackLong() throws IOException, MessageTypeException { return impl.unpackLong(); } - final public float unpackFloat() throws IOException, MessageTypeException { + public float unpackFloat() throws IOException, MessageTypeException { return impl.unpackFloat(); } - final public double unpackDouble() throws IOException, MessageTypeException { + public double unpackDouble() throws IOException, MessageTypeException { return impl.unpackDouble(); } - final public Object unpackNull() throws IOException, MessageTypeException { + public Object unpackNull() throws IOException, MessageTypeException { return impl.unpackNull(); } - final public boolean unpackBoolean() throws IOException, MessageTypeException { + public boolean unpackBoolean() throws IOException, MessageTypeException { return impl.unpackBoolean(); } - final public int unpackArray() throws IOException, MessageTypeException { + public int unpackArray() throws IOException, MessageTypeException { return impl.unpackArray(); } - final public int unpackMap() throws IOException, MessageTypeException { + public int unpackMap() throws IOException, MessageTypeException { return impl.unpackMap(); } - final public int unpackRaw() throws IOException, MessageTypeException { + public int unpackRaw() throws IOException, MessageTypeException { return impl.unpackRaw(); } - final public byte[] unpackRawBody(int length) throws IOException, MessageTypeException { + public byte[] unpackRawBody(int length) throws IOException, MessageTypeException { return impl.unpackRawBody(length); } From c2525bcc05477600f8c093483df179d89e8f0707 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 20 May 2010 06:19:26 +0900 Subject: [PATCH 008/152] java: add Unpacker.wrap method --- .../src/serializers/msgpack/MessagePackDirectSerializer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java index 721e95b..25f932b 100644 --- a/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java +++ b/java/test/thrift-protobuf-compare/tpc/src/serializers/msgpack/MessagePackDirectSerializer.java @@ -59,7 +59,7 @@ public class MessagePackDirectSerializer implements ObjectSerializer Date: Thu, 20 May 2010 17:32:15 +0900 Subject: [PATCH 009/152] java: javadoc --- .../org/msgpack/BufferedUnpackerImpl.java | 6 +- java/src/main/java/org/msgpack/Unpacker.java | 293 ++++++++++++++++-- 2 files changed, 276 insertions(+), 23 deletions(-) diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index 5fde4e1..0aba62b 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -367,7 +367,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - final byte[] unpackRawBody(int length) throws IOException, MessageTypeException { + final byte[] unpackRawBody(int length) throws IOException { more(length); byte[] bytes = new byte[length]; System.arraycopy(buffer, offset, bytes, 0, length); @@ -388,12 +388,12 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { return s; } - final Object unpackObject() throws IOException, MessageTypeException { + final Object unpackObject() throws IOException { // FIXME save state, restore state UnpackResult result = new UnpackResult(); if(!next(result)) { super.reset(); - throw new MessageTypeException(); + throw new UnpackException("insufficient buffer"); } return result.getData(); } diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 7fd6fcd..7563b39 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -23,6 +23,82 @@ import java.io.IOException; import java.util.Iterator; import java.nio.ByteBuffer; +/** + * Deserializer class that includes Buffered API, Unbuffered API, + * Schema API and Direct Conversion API. + * + * Buffered API uses the internal buffer of the Unpacker. + * Following code uses Buffered API with an input stream: + *
+ * // 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(Object obj : pac) {
+ *   // use MessageConvertable interface to convert the
+ *   // the generic object to the specific type.
+ * }
+ * 
+ * + * 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. + *
+ * // 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(Object obj : pac) {
+ *   // ...
+ * }
+ * 
+ * + * The combination of {@link reserveBuffer()}, {@link getBuffer()}, + * {@link getBufferOffset()}, {@link getBufferCapacity()} and + * {@link bufferConsumed()} is useful to omit copying. + *
+ * // 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(Object obj : pac) {
+ *     // ...
+ * }
+ * 
+ * + * Unbuffered API doesn't initialize the internal buffer. + * You can manage the buffer manually. + *
+ * // 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()) {
+ *     Object obj = pac.getData();
+ *     // ...
+ * }
+ * 
+ */ public class Unpacker implements Iterable { // buffer: @@ -59,38 +135,86 @@ public class Unpacker implements Iterable { 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; } + /** + * Sets schema to convert deserialized object into specific type. + * Default schema is {@link GenericSchema} that leaves objects for generic type. Use {@link MessageConvertable#messageConvert(Object)} method to convert the generic object. + * @param s schem to use + */ public Unpacker useSchema(Schema s) { impl.setSchema(s); return this; } + /** + * 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; @@ -99,45 +223,62 @@ public class Unpacker implements Iterable { 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, 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 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; @@ -163,22 +304,40 @@ public class Unpacker implements Iterable { impl.offset = 0; } + /** + * Returns the internal buffer. + */ public byte[] getBuffer() { return impl.buffer; } - public int getBufferOffset() { - return impl.filled; - } - + /** + * 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 = impl.execute(impl.buffer, impl.offset, impl.filled); if(noffset <= impl.offset) { @@ -190,10 +349,24 @@ public class Unpacker implements Iterable { } + /** + * 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; @@ -203,14 +376,23 @@ public class Unpacker implements Iterable { return noffset; } - public boolean isFinished() { - return impl.isFinished(); - } - + /** + * Gets the object deserialized by {@link execute(byte[])} method. + */ public Object 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() { impl.reset(); } @@ -236,59 +418,130 @@ public class Unpacker implements Iterable { } + /** + * 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 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(); } - public byte[] unpackRawBody(int length) throws IOException, MessageTypeException { + /** + * Gets one raw header from the buffer. + * This method calls {@link fill()} method if needed. + */ + public byte[] unpackRawBody(int length) throws IOException { return impl.unpackRawBody(length); } + /** + * 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(); } - final public Object unpackObject() throws IOException, MessageTypeException { + /** + * Gets one {@code Object} value from the buffer. + * This method calls {@link fill()} method if needed. + */ + final public Object unpackObject() throws IOException { return impl.unpackObject(); } } From 1fe35d7efe4d5a4993d2392cc259dec17fc744e4 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 22 May 2010 03:34:17 +0900 Subject: [PATCH 010/152] java: fix Packer.packByte --- java/src/main/java/org/msgpack/Packer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 6d79414..3fa421f 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -34,7 +34,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 { From b9cb270b8f771e5504e36469112daa6a32b24d63 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 22 May 2010 03:34:43 +0900 Subject: [PATCH 011/152] java: add Unpacker.unpack(MessageUnpackable) and Unpacker.tryUnpackNil() --- .../org/msgpack/BufferedUnpackerImpl.java | 22 ++++++++++++++++++- java/src/main/java/org/msgpack/Unpacker.java | 8 +++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index 0aba62b..4b2f302 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -63,6 +63,15 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } + 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; } @@ -275,6 +284,18 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { 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 1; + } + final boolean unpackBoolean() throws IOException, MessageTypeException { more(1); int b = buffer[offset] & 0xff; @@ -389,7 +410,6 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } final Object unpackObject() throws IOException { - // FIXME save state, restore state UnpackResult result = new UnpackResult(); if(!next(result)) { super.reset(); diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 7563b39..1917b9f 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -544,5 +544,13 @@ public class Unpacker implements Iterable { final public Object unpackObject() throws IOException { return impl.unpackObject(); } + + final void unpack(MessageUnpackable obj) throws IOException, MessageTypeException { + obj.unpackMessage(this); + } + + final boolean tryUnpackNull() throws IOException { + return impl.tryUnpackNull(); + } } From b4fc79c38ee44a1da5c2973fa213753a2d309666 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 22 May 2010 17:05:17 +0900 Subject: [PATCH 012/152] java: fixes compile error --- .../main/java/org/msgpack/BufferedUnpackerImpl.java | 7 ++++++- java/src/main/java/org/msgpack/Unpacker.java | 12 ++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index 4b2f302..cc6604d 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -293,7 +293,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { return false; } advance(1); - return 1; + return true; } final boolean unpackBoolean() throws IOException, MessageTypeException { @@ -396,6 +396,11 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { 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); diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 1917b9f..e84aff9 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -521,13 +521,21 @@ public class Unpacker implements Iterable { } /** - * Gets one raw header from the buffer. + * 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. @@ -546,7 +554,7 @@ public class Unpacker implements Iterable { } final void unpack(MessageUnpackable obj) throws IOException, MessageTypeException { - obj.unpackMessage(this); + obj.messageUnpack(this); } final boolean tryUnpackNull() throws IOException { From c43e5e0c95105c0dbf17e41d15068bfdb08450ce Mon Sep 17 00:00:00 2001 From: Kazuki Ohta Date: Sun, 23 May 2010 01:31:15 +0900 Subject: [PATCH 013/152] java: added testcases for empty array and empty map --- java/src/test/java/org/msgpack/TestPackUnpack.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index a16b5b1..b02bbb4 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -177,6 +177,9 @@ public class TestPackUnpack { @Test public void testArray() throws Exception { + List emptyList = new ArrayList(); + testArray(emptyList, Schema.parse("(array int)")); + for (int i = 0; i < 1000; i++) { Schema schema = Schema.parse("(array int)"); List l = new ArrayList(); @@ -209,6 +212,9 @@ public class TestPackUnpack { @Test public void testMap() throws Exception { + Map emptyMap = new HashMap(); + testMap(emptyMap, Schema.parse("(map int int)")); + for (int i = 0; i < 1000; i++) { Schema schema = Schema.parse("(map int int)"); Map m = new HashMap(); From 5982970e21d9bab7ea2bd507b360317f40628260 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 23 May 2010 01:34:45 +0900 Subject: [PATCH 014/152] java: fixed problem that empty array and empty map don't check Schema --- java/src/main/java/org/msgpack/UnpackerImpl.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java index ae01289..9b885a0 100644 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -157,7 +157,10 @@ public class UnpackerImpl { count = b & 0x0f; //System.out.println("fixarray count:"+count); obj = new Object[count]; - if(count == 0) { break _push; } // FIXME check IArraySchema + if(count == 0) { + obj = ((IArraySchema)top_schema).createFromArray((Object[])obj); + break _push; + } ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; @@ -179,7 +182,10 @@ public class UnpackerImpl { } count = b & 0x0f; obj = new Object[count*2]; - if(count == 0) { break _push; } // FIXME check IMapSchema + if(count == 0) { + obj = ((IMapSchema)top_schema).createFromMap((Object[])obj); + break _push; + } //System.out.println("fixmap count:"+count); ++top; stack_obj[top] = top_obj; From fa6ea6848f3c639d64e223afe31d0fa2ba13d333 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 23 May 2010 01:38:01 +0900 Subject: [PATCH 015/152] java: fixed problem that empty array and empty map don't check Schema --- .../main/java/org/msgpack/UnpackerImpl.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java index 9b885a0..10cf5f0 100644 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -348,7 +348,10 @@ public class UnpackerImpl { castBuffer.put(src, n, 2); count = ((int)castBuffer.getShort(0)) & 0xffff; obj = new Object[count]; - if(count == 0) { break _push; } // FIXME check IArraySchema + if(count == 0) { + obj = ((IArraySchema)top_schema).createFromArray((Object[])obj); + break _push; + } ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; @@ -371,7 +374,10 @@ public class UnpackerImpl { // FIXME overflow check count = castBuffer.getInt(0) & 0x7fffffff; obj = new Object[count]; - if(count == 0) { break _push; } // FIXME check IArraySchema + if(count == 0) { + obj = ((IArraySchema)top_schema).createFromArray((Object[])obj); + break _push; + } ++top; stack_obj[top] = top_obj; stack_ct[top] = top_ct; @@ -393,7 +399,10 @@ public class UnpackerImpl { castBuffer.put(src, n, 2); count = ((int)castBuffer.getShort(0)) & 0xffff; obj = new Object[count*2]; - if(count == 0) { break _push; } // FIXME check IMapSchema + if(count == 0) { + obj = ((IMapSchema)top_schema).createFromMap((Object[])obj); + break _push; + } //System.out.println("fixmap count:"+count); ++top; stack_obj[top] = top_obj; @@ -417,7 +426,10 @@ public class UnpackerImpl { // FIXME overflow check count = castBuffer.getInt(0) & 0x7fffffff; obj = new Object[count*2]; - if(count == 0) { break _push; } // FIXME check IMapSchema + if(count == 0) { + obj = ((IMapSchema)top_schema).createFromMap((Object[])obj); + break _push; + } //System.out.println("fixmap count:"+count); ++top; stack_obj[top] = top_obj; From f8173e93f5df4c6c0289d58b92a4acde816f1cf3 Mon Sep 17 00:00:00 2001 From: Kazuki Ohta Date: Sun, 23 May 2010 01:48:20 +0900 Subject: [PATCH 016/152] java: version 0.3 (added CHANGES.txt and LICENSE.txt) --- java/CHANGES.txt | 9 +++ java/LICENSE.txt | 202 +++++++++++++++++++++++++++++++++++++++++++++++ java/pom.xml | 2 +- 3 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 java/CHANGES.txt create mode 100644 java/LICENSE.txt diff --git a/java/CHANGES.txt b/java/CHANGES.txt new file mode 100644 index 0000000..77e9318 --- /dev/null +++ b/java/CHANGES.txt @@ -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(). diff --git a/java/LICENSE.txt b/java/LICENSE.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/java/LICENSE.txt @@ -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. diff --git a/java/pom.xml b/java/pom.xml index d1f6c34..9b74b4c 100755 --- a/java/pom.xml +++ b/java/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.msgpack msgpack - 0.2 + 0.3 MessagePack for Java MessagePack for Java From d0af8aa9f11bfd9c6ded625f5591db25d42153ad Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 23 May 2010 21:10:49 +0900 Subject: [PATCH 017/152] ruby: rdoc --- ruby/pack.c | 132 ++++++++++++++++++++++++--- ruby/rbinit.c | 12 +++ ruby/unpack.c | 244 ++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 370 insertions(+), 18 deletions(-) diff --git a/ruby/pack.c b/ruby/pack.c index 387bab6..7f56923 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -51,6 +51,16 @@ static ID s_append; rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); \ } + +/* + * Document-method: NilClass#to_msgpack + * + * call-seq: + * nil.to_msgpack(out = '') -> String + * + * Serializes the nil into raw bytes. + * This calls to_msgpack reflectively for internal elements. + */ static VALUE MessagePack_NilClass_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -58,6 +68,16 @@ static VALUE MessagePack_NilClass_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: TrueClass#to_msgpack + * + * call-seq: + * true.to_msgpack(out = '') -> String + * + * Serializes the true into raw bytes. + * This calls to_msgpack reflectively for internal elements. + */ static VALUE MessagePack_TrueClass_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -65,6 +85,16 @@ static VALUE MessagePack_TrueClass_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: FalseClass#to_msgpack + * + * call-seq: + * false.to_msgpack(out = '') -> String + * + * Serializes false into raw bytes. + * This calls to_msgpack reflectively for internal elements. + */ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -73,6 +103,15 @@ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self } +/* + * Document-method: Fixnum#to_msgpack + * + * call-seq: + * fixnum.to_msgpack(out = '') -> String + * + * Serializes the Fixnum into raw bytes. + * This calls to_msgpack reflectively for internal elements. + */ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -85,6 +124,15 @@ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) #define RBIGNUM_SIGN(b) (RBIGNUM(b)->sign) #endif +/* + * Document-method: Bignum#to_msgpack + * + * call-seq: + * bignum.to_msgpack(out = '') -> String + * + * Serializes the Bignum into raw bytes. + * This calls to_msgpack reflectively for internal elements. + */ static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -97,6 +145,15 @@ static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: Float#to_msgpack + * + * call-seq: + * float.to_msgpack(out = '') -> String + * + * Serializes the Float into raw bytes. + */ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -104,6 +161,15 @@ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: String#to_msgpack + * + * call-seq: + * string.to_msgpack(out = '') -> String + * + * Serializes the String into raw bytes. + */ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -112,6 +178,15 @@ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: Symbol#to_msgpack + * + * call-seq: + * symbol.to_msgpack(out = '') -> String + * + * Serializes the Symbol into raw bytes. + */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -122,6 +197,16 @@ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + +/* + * Document-method: Array#to_msgpack + * + * call-seq: + * array.to_msgpack(out = '') -> String + * + * Serializes the Array into raw bytes. + * This calls to_msgpack method reflectively for internal elements. + */ static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -134,6 +219,7 @@ static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) return out; } + #ifndef RHASH_SIZE // Ruby 1.8 #define RHASH_SIZE(h) (RHASH(h)->tbl ? RHASH(h)->tbl->num_entries : 0) #endif @@ -146,6 +232,15 @@ static int MessagePack_Hash_to_msgpack_foreach(VALUE key, VALUE value, VALUE out return ST_CONTINUE; } +/* + * Document-method: Hash#to_msgpack + * + * call-seq: + * hash.to_msgpack(out = '') -> String + * + * Serializes the Hash into raw bytes. + * This calls to_msgpack method reflectively for internal keys and values. + */ static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); @@ -155,6 +250,17 @@ static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self) } +/** + * Document-method: MessagePack.pack + * + * call-seq: + * MessagePack.pack(object, out = '') -> String + * + * Serializes the object into raw bytes. The encoding of the string is ASCII-8BIT on Ruby 1.9. + * This method is same as object.to_msgpack(out = ''). + * + * _out_ is an object that implements *<<* method like String or IO. + */ static VALUE MessagePack_pack(int argc, VALUE* argv, VALUE self) { VALUE out; @@ -173,16 +279,22 @@ void Init_msgpack_pack(VALUE mMessagePack) { s_to_msgpack = rb_intern("to_msgpack"); s_append = rb_intern("<<"); - rb_define_method_id(rb_cNilClass, s_to_msgpack, MessagePack_NilClass_to_msgpack, -1); - rb_define_method_id(rb_cTrueClass, s_to_msgpack, MessagePack_TrueClass_to_msgpack, -1); - rb_define_method_id(rb_cFalseClass, s_to_msgpack, MessagePack_FalseClass_to_msgpack, -1); - rb_define_method_id(rb_cFixnum, s_to_msgpack, MessagePack_Fixnum_to_msgpack, -1); - rb_define_method_id(rb_cBignum, s_to_msgpack, MessagePack_Bignum_to_msgpack, -1); - rb_define_method_id(rb_cFloat, s_to_msgpack, MessagePack_Float_to_msgpack, -1); - rb_define_method_id(rb_cString, s_to_msgpack, MessagePack_String_to_msgpack, -1); - rb_define_method_id(rb_cArray, s_to_msgpack, MessagePack_Array_to_msgpack, -1); - rb_define_method_id(rb_cHash, s_to_msgpack, MessagePack_Hash_to_msgpack, -1); - rb_define_method_id(rb_cSymbol, s_to_msgpack, MessagePack_Symbol_to_msgpack, -1); + + rb_define_method(rb_cNilClass, "to_msgpack", MessagePack_NilClass_to_msgpack, -1); + rb_define_method(rb_cTrueClass, "to_msgpack", MessagePack_TrueClass_to_msgpack, -1); + rb_define_method(rb_cFalseClass, "to_msgpack", MessagePack_FalseClass_to_msgpack, -1); + rb_define_method(rb_cFixnum, "to_msgpack", MessagePack_Fixnum_to_msgpack, -1); + rb_define_method(rb_cBignum, "to_msgpack", MessagePack_Bignum_to_msgpack, -1); + rb_define_method(rb_cFloat, "to_msgpack", MessagePack_Float_to_msgpack, -1); + rb_define_method(rb_cString, "to_msgpack", MessagePack_String_to_msgpack, -1); + rb_define_method(rb_cArray, "to_msgpack", MessagePack_Array_to_msgpack, -1); + rb_define_method(rb_cHash, "to_msgpack", MessagePack_Hash_to_msgpack, -1); + rb_define_method(rb_cSymbol, "to_msgpack", MessagePack_Symbol_to_msgpack, -1); + + /** + * MessagePack module is defined in rbinit.c file. + * mMessagePack = rb_define_module("MessagePack"); + */ rb_define_module_function(mMessagePack, "pack", MessagePack_pack, -1); } diff --git a/ruby/rbinit.c b/ruby/rbinit.c index 80d1d8c..050abde 100644 --- a/ruby/rbinit.c +++ b/ruby/rbinit.c @@ -20,6 +20,18 @@ static VALUE mMessagePack; +/** + * Document-module: MessagePack + * + * MessagePack is a binary-based efficient object serialization library. + * It enables to exchange structured objects between many languages like JSON. + * But unlike JSON, it is very fast and small. + * + * require 'msgpack' + * msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03" + * MessagePack.unpack(msg) #=> [1,2,3] + * + */ void Init_msgpack(void) { mMessagePack = rb_define_module("MessagePack"); diff --git a/ruby/unpack.c b/ruby/unpack.c index 3a95e5a..9a2c457 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -205,6 +205,12 @@ static int template_execute_wrap(msgpack_unpack_t* mp, static VALUE cUnpacker; + + +/** + * Document-module: MessagePack::UnpackerError + * + */ static VALUE eUnpackError; @@ -243,6 +249,22 @@ static ID append_method_of(VALUE stream) } } +/** + * Document-method: MessagePack::Unpacker#initialize + * + * call-seq: + * MessagePack::Unpacker.new(stream = nil) + * + * Creates instance of MessagePack::Unpacker. + * + * You can specify a _stream_ for input stream. + * It is required to implement *sysread* or *readpartial* method. + * + * With the input stream, buffers will be feeded into the deserializer automatically. + * + * Without the input stream, use *feed* method manually. Or you can manage the buffer manually + * with *execute*, *finished?*, *data* and *reset* methods. + */ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self) { VALUE stream; @@ -268,12 +290,29 @@ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self) return self; } + +/** + * Document-method: MessagePack::Unpacker#stream + * + * call-seq: + * unpacker.stream + * + * Gets the input stream. + */ static VALUE MessagePack_Unpacker_stream_get(VALUE self) { UNPACKER(self, mp); return mp->user.stream; } +/** + * Document-method: MessagePack::Unpacker#stream= + * + * call-seq: + * unpacker.stream = stream + * + * Resets the input stream. You can set nil not to use input stream. + */ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val) { UNPACKER(self, mp); @@ -282,6 +321,15 @@ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val) return val; } + +/** + * Document-method: MessagePack::Unpacker#feed + * + * call-seq: + * unpacker.feed(data) + * + * Fills the internal buffer with the specified buffer. + */ static VALUE MessagePack_Unpacker_feed(VALUE self, VALUE data) { UNPACKER(self, mp); @@ -290,6 +338,20 @@ static VALUE MessagePack_Unpacker_feed(VALUE self, VALUE data) return Qnil; } +/** + * Document-method: MessagePack::Unpacker#fill + * + * call-seq: + * unpacker.fill -> length of read data + * + * Fills the internal buffer using the input stream. + * + * If the input stream is not specified, it returns nil. + * You can set it on *initialize* or *stream=* methods. + * + * This methods raises exceptions that _stream.sysread_ or + * _stream.readpartial_ method raises. + */ static VALUE MessagePack_Unpacker_fill(VALUE self) { UNPACKER(self, mp); @@ -313,6 +375,18 @@ static VALUE MessagePack_Unpacker_fill(VALUE self) return LONG2FIX(len); } + +/** + * Document-method: MessagePack::Unpacker#each + * + * call-seq: + * unpacker.each {|object| } + * + * Deserializes objects repeatedly. This calls *fill* method automatically. + * + * UnpackError is throw when parse error is occured. + * This method raises exceptions that *fill* method raises. + */ static VALUE MessagePack_Unpacker_each(VALUE self) { UNPACKER(self, mp); @@ -352,6 +426,7 @@ static VALUE MessagePack_Unpacker_each(VALUE self) return Qnil; } + static inline VALUE MessagePack_unpack_impl(VALUE self, VALUE data, unsigned long dlen) { msgpack_unpack_t mp; @@ -376,12 +451,34 @@ static inline VALUE MessagePack_unpack_impl(VALUE self, VALUE data, unsigned lon } } +/** + * Document-method: MessagePack::Unpacker.unpack_limit + * + * call-seq: + * MessagePack::Unpacker.unpack_limit(data, limit) -> object + * + * Deserializes one object over the specified buffer upto _limit_ bytes. + * + * UnpackError is throw when parse error is occured, the buffer is insufficient + * to deserialize one object or there are extra bytes. + */ static VALUE MessagePack_unpack_limit(VALUE self, VALUE data, VALUE limit) { CHECK_STRING_TYPE(data); return MessagePack_unpack_impl(self, data, NUM2ULONG(limit)); } +/** + * Document-method: MessagePack::Unpacker.unpack + * + * call-seq: + * MessagePack::Unpacker.unpack(data) -> object + * + * Deserializes one object over the specified buffer. + * + * UnpackError is throw when parse error is occured, the buffer is insufficient + * to deserialize one object or there are extra bytes. + */ static VALUE MessagePack_unpack(VALUE self, VALUE data) { CHECK_STRING_TYPE(data); @@ -411,7 +508,20 @@ static VALUE MessagePack_Unpacker_execute_impl(VALUE self, VALUE data, } } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#execute_limit + * + * call-seq: + * unpacker.unpack_limit(data, offset, limit) -> next offset + * + * Deserializes one object over the specified buffer from _offset_ bytes upto _limit_ bytes. + * + * This method doesn't use the internal buffer. + * + * Call *reset()* method before calling this method again. + * + * UnpackError is throw when parse error is occured. + */ static VALUE MessagePack_Unpacker_execute_limit(VALUE self, VALUE data, VALUE off, VALUE limit) { @@ -420,7 +530,24 @@ static VALUE MessagePack_Unpacker_execute_limit(VALUE self, VALUE data, (size_t)NUM2ULONG(off), (size_t)NUM2ULONG(limit)); } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#execute + * + * call-seq: + * unpacker.unpack(data, offset) -> next offset + * + * Deserializes one object over the specified buffer from _offset_ bytes. + * + * This method doesn't use the internal buffer. + * + * Call *reset()* method before calling this method again. + * + * This returns offset that was parsed to. + * Use *finished?* method to check an object is deserialized and call *data* + * method if it returns true. + * + * UnpackError is throw when parse error is occured. + */ static VALUE MessagePack_Unpacker_execute(VALUE self, VALUE data, VALUE off) { CHECK_STRING_TYPE(data); @@ -428,7 +555,16 @@ static VALUE MessagePack_Unpacker_execute(VALUE self, VALUE data, VALUE off) (size_t)NUM2ULONG(off), (size_t)RSTRING_LEN(data)); } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#finished? + * + * call-seq: + * unpacker.finished? + * + * Returns true if an object is ready to get with data method. + * + * Use this method with execute method. + */ static VALUE MessagePack_Unpacker_finished_p(VALUE self) { UNPACKER(self, mp); @@ -438,14 +574,30 @@ static VALUE MessagePack_Unpacker_finished_p(VALUE self) return Qfalse; } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#data + * + * call-seq: + * unpacker.data + * + * Gets the object deserialized by execute method. + * + * Use this method with execute method. + */ static VALUE MessagePack_Unpacker_data(VALUE self) { UNPACKER(self, mp); return template_data(mp); } -/* compat */ +/** + * Document-method: MessagePack::Unpacker#reset + * + * call-seq: + * unpacker.reset + * + * Resets the internal state of the unpacker. + */ static VALUE MessagePack_Unpacker_reset(VALUE self) { UNPACKER(self, mp); @@ -467,20 +619,96 @@ void Init_msgpack_unpack(VALUE mMessagePack) eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError); cUnpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject); rb_define_alloc_func(cUnpacker, MessagePack_Unpacker_alloc); + rb_define_method(cUnpacker, "initialize", MessagePack_Unpacker_initialize, -1); + + /* Buffered API */ rb_define_method(cUnpacker, "feed", MessagePack_Unpacker_feed, 1); rb_define_method(cUnpacker, "fill", MessagePack_Unpacker_fill, 0); rb_define_method(cUnpacker, "each", MessagePack_Unpacker_each, 0); rb_define_method(cUnpacker, "stream", MessagePack_Unpacker_stream_get, 0); rb_define_method(cUnpacker, "stream=", MessagePack_Unpacker_stream_set, 1); - rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack, 1); - rb_define_module_function(mMessagePack, "unpack_limit", MessagePack_unpack_limit, 2); - /* backward compatibility */ + /* Unbuffered API */ rb_define_method(cUnpacker, "execute", MessagePack_Unpacker_execute, 2); rb_define_method(cUnpacker, "execute_limit", MessagePack_Unpacker_execute_limit, 3); rb_define_method(cUnpacker, "finished?", MessagePack_Unpacker_finished_p, 0); rb_define_method(cUnpacker, "data", MessagePack_Unpacker_data, 0); rb_define_method(cUnpacker, "reset", MessagePack_Unpacker_reset, 0); + + /** + * MessagePack module is defined in rbinit.c file. + * mMessagePack = rb_define_module("MessagePack"); + */ + rb_define_module_function(mMessagePack, "unpack", MessagePack_unpack, 1); + rb_define_module_function(mMessagePack, "unpack_limit", MessagePack_unpack_limit, 2); } +/** + * Document-module: MessagePack::Unpacker + * + * Deserializer class that includes Buffered API and Unbuffered API. + * + * + * Buffered API uses the internal buffer of the Unpacker. + * Following code uses Buffered API with an input stream: + * + * # create an unpacker with input stream. + * pac = MessagePack::Unpacker.new(stdin) + * + * # deserialize object one after another. + * pac.each {|obj| + * # ... + * } + * + * + * Following code doesn't use the input stream and feeds buffer + * using *fill* method. This is useful to use special stream + * or with event-driven I/O library. + * + * # create an unpacker without input stream. + * pac = MessagePack::Unpacker.new() + * + * # feed buffer to the internal buffer. + * pac.feed(input_bytes) + * + * # deserialize object one after another. + * pac.each {|obj| + * # ... + * } + * + * You can manage the buffer manually with the combination of + * *execute*, *finished?*, *data* and *reset* method. + * + * # create an unpacker. + * pac = MessagePack::Unpacker.new() + * + * # manage buffer and offset manually. + * offset = 0 + * buffer = '' + * + * # read some data into the buffer. + * buffer << [1,2,3].to_msgpack + * buffer << [4,5,6].to_msgpack + * + * while true + * offset = pac.execute(buffer, offset) + * + * if pac.finished? + * obj = pac.data + * + * buffer.slice!(0, offset) + * offset = 0 + * pac.reset + * + * # do something with the object + * # ... + * + * # repeat execution if there are more data. + * next unless buffer.empty? + * end + * + * break + * end + */ + From dbebe9771b276bd286b2ccdb8ac88dd17a83524c Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 25 May 2010 02:55:58 +0900 Subject: [PATCH 018/152] ruby: update rdoc --- ruby/pack.c | 5 ----- ruby/rbinit.c | 8 ++++++++ ruby/unpack.c | 7 ++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ruby/pack.c b/ruby/pack.c index 7f56923..bbeac4a 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -59,7 +59,6 @@ static ID s_append; * nil.to_msgpack(out = '') -> String * * Serializes the nil into raw bytes. - * This calls to_msgpack reflectively for internal elements. */ static VALUE MessagePack_NilClass_to_msgpack(int argc, VALUE *argv, VALUE self) { @@ -76,7 +75,6 @@ static VALUE MessagePack_NilClass_to_msgpack(int argc, VALUE *argv, VALUE self) * true.to_msgpack(out = '') -> String * * Serializes the true into raw bytes. - * This calls to_msgpack reflectively for internal elements. */ static VALUE MessagePack_TrueClass_to_msgpack(int argc, VALUE *argv, VALUE self) { @@ -93,7 +91,6 @@ static VALUE MessagePack_TrueClass_to_msgpack(int argc, VALUE *argv, VALUE self) * false.to_msgpack(out = '') -> String * * Serializes false into raw bytes. - * This calls to_msgpack reflectively for internal elements. */ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self) { @@ -110,7 +107,6 @@ static VALUE MessagePack_FalseClass_to_msgpack(int argc, VALUE *argv, VALUE self * fixnum.to_msgpack(out = '') -> String * * Serializes the Fixnum into raw bytes. - * This calls to_msgpack reflectively for internal elements. */ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) { @@ -131,7 +127,6 @@ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) * bignum.to_msgpack(out = '') -> String * * Serializes the Bignum into raw bytes. - * This calls to_msgpack reflectively for internal elements. */ static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) { diff --git a/ruby/rbinit.c b/ruby/rbinit.c index 050abde..4ad6beb 100644 --- a/ruby/rbinit.c +++ b/ruby/rbinit.c @@ -27,10 +27,18 @@ static VALUE mMessagePack; * It enables to exchange structured objects between many languages like JSON. * But unlike JSON, it is very fast and small. * + * You can install MessagePack with rubygems. + * + * gem install msgpack + * + * Simple usage is as follows. + * * require 'msgpack' * msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03" * MessagePack.unpack(msg) #=> [1,2,3] * + * Use Unpacker class for streaming deserialization. + * */ void Init_msgpack(void) { diff --git a/ruby/unpack.c b/ruby/unpack.c index 9a2c457..dec40c6 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -654,7 +654,7 @@ void Init_msgpack_unpack(VALUE mMessagePack) * Following code uses Buffered API with an input stream: * * # create an unpacker with input stream. - * pac = MessagePack::Unpacker.new(stdin) + * pac = MessagePack::Unpacker.new(STDIN) * * # deserialize object one after another. * pac.each {|obj| @@ -663,8 +663,8 @@ void Init_msgpack_unpack(VALUE mMessagePack) * * * Following code doesn't use the input stream and feeds buffer - * using *fill* method. This is useful to use special stream - * or with event-driven I/O library. + * manually. This is useful to use special stream or with + * event-driven I/O library. * * # create an unpacker without input stream. * pac = MessagePack::Unpacker.new() @@ -677,6 +677,7 @@ void Init_msgpack_unpack(VALUE mMessagePack) * # ... * } * + * * You can manage the buffer manually with the combination of * *execute*, *finished?*, *data* and *reset* method. * From fc7da17fa2dbdc6385ad95f6848ee59598164440 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 25 May 2010 02:57:37 +0900 Subject: [PATCH 019/152] cpp: add sbuffer::clear() and vrefbuffer::clear() --- cpp/msgpack/sbuffer.h | 4 ++++ cpp/msgpack/sbuffer.hpp | 5 +++++ cpp/msgpack/vrefbuffer.h | 3 +++ cpp/msgpack/vrefbuffer.hpp | 5 +++++ cpp/test/buffer.cc | 25 +++++++++++++++++++++++++ cpp/vrefbuffer.c | 19 +++++++++++++++++++ 6 files changed, 61 insertions(+) diff --git a/cpp/msgpack/sbuffer.h b/cpp/msgpack/sbuffer.h index bc0a8fd..57f424a 100644 --- a/cpp/msgpack/sbuffer.h +++ b/cpp/msgpack/sbuffer.h @@ -77,6 +77,10 @@ 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 } diff --git a/cpp/msgpack/sbuffer.hpp b/cpp/msgpack/sbuffer.hpp index ca06884e..e4a3f96 100644 --- a/cpp/msgpack/sbuffer.hpp +++ b/cpp/msgpack/sbuffer.hpp @@ -72,6 +72,11 @@ public: return msgpack_sbuffer_release(this); } + void clear() + { + msgpack_sbuffer_clear(this); + } + private: void expand_buffer(size_t len) { diff --git a/cpp/msgpack/vrefbuffer.h b/cpp/msgpack/vrefbuffer.h index 38ead67..a08e0d0 100644 --- a/cpp/msgpack/vrefbuffer.h +++ b/cpp/msgpack/vrefbuffer.h @@ -80,6 +80,9 @@ 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); + + int msgpack_vrefbuffer_write(void* data, const char* buf, unsigned int len) { msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data; diff --git a/cpp/msgpack/vrefbuffer.hpp b/cpp/msgpack/vrefbuffer.hpp index c8eca7b..7e0ffb2 100644 --- a/cpp/msgpack/vrefbuffer.hpp +++ b/cpp/msgpack/vrefbuffer.hpp @@ -78,6 +78,11 @@ public: } } + void clear() + { + msgpack_vrefbuffer_clear(this); + } + private: typedef msgpack_vrefbuffer base; diff --git a/cpp/test/buffer.cc b/cpp/test/buffer.cc index a2e9037..aff0699 100644 --- a/cpp/test/buffer.cc +++ b/cpp/test/buffer.cc @@ -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 ); } diff --git a/cpp/vrefbuffer.c b/cpp/vrefbuffer.c index 136372f..a27b138 100644 --- a/cpp/vrefbuffer.c +++ b/cpp/vrefbuffer.c @@ -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) { From 26bc835c7e702769375201391bf4965b08c52516 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 26 May 2010 04:30:49 +0900 Subject: [PATCH 020/152] ruby: buffer rewinding --- ruby/unpack.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/ruby/unpack.c b/ruby/unpack.c index dec40c6..ba20ae6 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -27,6 +27,11 @@ static ID s_readpartial; int s_ascii_8bit; #endif +static ID s_slice_bang; +#ifdef RUBY_VM +static ID s_clear; +#endif + typedef struct { int finished; VALUE source; @@ -125,6 +130,7 @@ static inline int template_callback_map_item(unpack_user* u, VALUE* c, VALUE k, static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, VALUE* o) { *o = (l <= COW_MIN_SIZE) ? rb_str_new(p, l) : rb_str_substr(u->source, p - b, l); return 0; } +//{ *o = rb_str_new(p, l); return 0; } #include "msgpack/unpack_template.h" @@ -249,6 +255,14 @@ static ID append_method_of(VALUE stream) } } +#ifndef MSGPACK_UNPACKER_BUFFER_INIT_SIZE +#define MSGPACK_UNPACKER_BUFFER_INIT_SIZE (32*1024) +#endif + +#ifndef MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE +#define MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE (8*1024) +#endif + /** * Document-method: MessagePack::Unpacker#initialize * @@ -283,9 +297,9 @@ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self) template_init(mp); mp->user.finished = 0; mp->user.offset = 0; - mp->user.buffer = rb_str_new("",0); + mp->user.buffer = rb_str_buf_new(MSGPACK_UNPACKER_BUFFER_INIT_SIZE); mp->user.stream = stream; - mp->user.streambuf = rb_str_new("",0); + mp->user.streambuf = rb_str_buf_new(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE); mp->user.stream_append_method = append_method_of(stream); return self; } @@ -322,6 +336,63 @@ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val) } +#ifdef RUBY_VM +# ifndef STR_SHARED +# define STR_SHARED FL_USER2 +# endif +# ifndef STR_NOEMBED +# define STR_NOEMBED FL_USER1 +# endif +# ifndef STR_ASSOC +# define STR_ASSOC FL_USER3 +# endif +# ifndef STR_NOCAPA_P +# define STR_NOCAPA_P(s) (FL_TEST(s,STR_NOEMBED) && FL_ANY(s,STR_SHARED|STR_ASSOC)) +# endif +# define NEED_MORE_CAPA(s,size) (!STR_NOCAPA_P(s) && RSTRING(s)->as.heap.aux.capa < size) +#else +# ifndef STR_NOCAPA +# ifndef STR_ASSOC +# define STR_ASSOC FL_USER3 +# endif +# ifndef ELTS_SHARED +# define ELTS_SHARED FL_USER2 +# endif +# define STR_NOCAPA (ELTS_SHARED|STR_ASSOC) +# endif +# define NEED_MORE_CAPA(s,size) (!FL_TEST(s,STR_NOCAPA) && RSTRING(s)->aux.capa < size) +#endif + +static void try_rewind_buffer(msgpack_unpack_t* mp, size_t required) +{ + VALUE buffer = mp->user.buffer; + + size_t need_capa = RSTRING_LEN(buffer) + required; + + if(NEED_MORE_CAPA(buffer, need_capa)) { + /* FIXME +#ifdef RUBY_VM + if(RSTRING_LEN(buffer) <= mp->user.offset) { + rb_funcall(buffer, s_clear, 0); + mp->user.offset = 0; + return; + } +#endif + rb_funcall(buffer, s_slice_bang, 2, LONG2FIX(0), LONG2FIX(mp->user.offset)); + mp->user.offset = 0; + */ + size_t not_parsed = RSTRING_LEN(buffer) - mp->user.offset; + size_t nsize = MSGPACK_UNPACKER_BUFFER_INIT_SIZE * 2; + while(nsize < not_parsed + required) { + nsize *= 2; + } + VALUE nbuffer = rb_str_buf_new(nsize); + rb_str_buf_cat(nbuffer, RSTRING_PTR(buffer)+mp->user.offset, not_parsed); + mp->user.buffer = nbuffer; + mp->user.offset = 0; + } +} + /** * Document-method: MessagePack::Unpacker#feed * @@ -334,6 +405,7 @@ static VALUE MessagePack_Unpacker_feed(VALUE self, VALUE data) { UNPACKER(self, mp); StringValue(data); + try_rewind_buffer(mp, RSTRING_LEN(data)); rb_str_cat(mp->user.buffer, RSTRING_PTR(data), RSTRING_LEN(data)); return Qnil; } @@ -363,12 +435,13 @@ static VALUE MessagePack_Unpacker_fill(VALUE self) long len; if(RSTRING_LEN(mp->user.buffer) == 0) { rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, - LONG2FIX(64*1024), mp->user.buffer); + LONG2FIX(MSGPACK_UNPACKER_BUFFER_INIT_SIZE), mp->user.buffer); len = RSTRING_LEN(mp->user.buffer); } else { rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, - LONG2FIX(64*1024), mp->user.streambuf); + LONG2FIX(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE), mp->user.streambuf); len = RSTRING_LEN(mp->user.streambuf); + try_rewind_buffer(mp, len); rb_str_cat(mp->user.buffer, RSTRING_PTR(mp->user.streambuf), RSTRING_LEN(mp->user.streambuf)); } @@ -616,6 +689,11 @@ void Init_msgpack_unpack(VALUE mMessagePack) s_ascii_8bit = rb_enc_find_index("ASCII-8BIT"); #endif + s_slice_bang = rb_intern("slice!"); +#ifdef RUBY_VM + s_clear = rb_intern("clear"); +#endif + eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError); cUnpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject); rb_define_alloc_func(cUnpacker, MessagePack_Unpacker_alloc); From 5fa589691c780735fae153e7e6907906663349a1 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 26 May 2010 07:01:28 +0900 Subject: [PATCH 021/152] ruby: use malloc/realloc for stream buffer --- ruby/unpack.c | 180 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 118 insertions(+), 62 deletions(-) diff --git a/ruby/unpack.c b/ruby/unpack.c index ba20ae6..c4b5e29 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -27,16 +27,17 @@ static ID s_readpartial; int s_ascii_8bit; #endif -static ID s_slice_bang; -#ifdef RUBY_VM -static ID s_clear; -#endif +struct unpack_buffer { + size_t size; + size_t free; + char* ptr; +}; typedef struct { int finished; VALUE source; size_t offset; - VALUE buffer; + struct unpack_buffer buffer; VALUE stream; VALUE streambuf; ID stream_append_method; @@ -129,8 +130,16 @@ static inline int template_callback_map_item(unpack_user* u, VALUE* c, VALUE k, #endif static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, VALUE* o) -{ *o = (l <= COW_MIN_SIZE) ? rb_str_new(p, l) : rb_str_substr(u->source, p - b, l); return 0; } +//{ *o = (l <= COW_MIN_SIZE) ? rb_str_new(p, l) : rb_str_substr(u->source, p - b, l); return 0; } //{ *o = rb_str_new(p, l); return 0; } +{ + if(u->source == Qnil || l <= COW_MIN_SIZE) { + *o = rb_str_new(p, l); + } else { + *o = rb_str_substr(u->source, p - b, l); + } + return 0; +} #include "msgpack/unpack_template.h" @@ -209,6 +218,27 @@ static int template_execute_wrap(msgpack_unpack_t* mp, return ret; } +static int template_execute_wrap_each(msgpack_unpack_t* mp, + const char* ptr, size_t dlen, size_t* from) +{ + VALUE args[4] = { + (VALUE)mp, + (VALUE)ptr, + (VALUE)dlen, + (VALUE)from, + }; + + // FIXME execute実行中はmp->topが更新されないのでGC markが機能しない + rb_gc_disable(); + + int ret = (int)rb_rescue(template_execute_do, (VALUE)args, + template_execute_rescue, Qnil); + + rb_gc_enable(); + + return ret; +} + static VALUE cUnpacker; @@ -220,15 +250,26 @@ static VALUE cUnpacker; static VALUE eUnpackError; +#ifndef MSGPACK_UNPACKER_BUFFER_INIT_SIZE +#define MSGPACK_UNPACKER_BUFFER_INIT_SIZE (32*1024) +#endif + +#ifndef MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE +#define MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE (8*1024) +#endif + static void MessagePack_Unpacker_free(void* data) { - if(data) { free(data); } + if(data) { + msgpack_unpack_t* mp = (msgpack_unpack_t*)data; + free(mp->user.buffer.ptr); + free(mp); + } } static void MessagePack_Unpacker_mark(msgpack_unpack_t *mp) { unsigned int i; - rb_gc_mark(mp->user.buffer); rb_gc_mark(mp->user.stream); rb_gc_mark(mp->user.streambuf); for(i=0; i < mp->top; ++i) { @@ -255,14 +296,6 @@ static ID append_method_of(VALUE stream) } } -#ifndef MSGPACK_UNPACKER_BUFFER_INIT_SIZE -#define MSGPACK_UNPACKER_BUFFER_INIT_SIZE (32*1024) -#endif - -#ifndef MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE -#define MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE (8*1024) -#endif - /** * Document-method: MessagePack::Unpacker#initialize * @@ -297,7 +330,9 @@ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self) template_init(mp); mp->user.finished = 0; mp->user.offset = 0; - mp->user.buffer = rb_str_buf_new(MSGPACK_UNPACKER_BUFFER_INIT_SIZE); + mp->user.buffer.size = 0; + mp->user.buffer.free = 0; + mp->user.buffer.ptr = NULL; mp->user.stream = stream; mp->user.streambuf = rb_str_buf_new(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE); mp->user.stream_append_method = append_method_of(stream); @@ -363,34 +398,66 @@ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val) # define NEED_MORE_CAPA(s,size) (!FL_TEST(s,STR_NOCAPA) && RSTRING(s)->aux.capa < size) #endif -static void try_rewind_buffer(msgpack_unpack_t* mp, size_t required) +static void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len) { - VALUE buffer = mp->user.buffer; + struct unpack_buffer* buffer = &mp->user.buffer; - size_t need_capa = RSTRING_LEN(buffer) + required; + if(buffer->size == 0) { + char* tmp = (char*)malloc(MSGPACK_UNPACKER_BUFFER_INIT_SIZE); + // FIXME check tmp == NULL + buffer->ptr = tmp; + buffer->free = MSGPACK_UNPACKER_BUFFER_INIT_SIZE; + buffer->size = 0; - if(NEED_MORE_CAPA(buffer, need_capa)) { - /* FIXME -#ifdef RUBY_VM - if(RSTRING_LEN(buffer) <= mp->user.offset) { - rb_funcall(buffer, s_clear, 0); - mp->user.offset = 0; - return; - } -#endif - rb_funcall(buffer, s_slice_bang, 2, LONG2FIX(0), LONG2FIX(mp->user.offset)); - mp->user.offset = 0; - */ - size_t not_parsed = RSTRING_LEN(buffer) - mp->user.offset; - size_t nsize = MSGPACK_UNPACKER_BUFFER_INIT_SIZE * 2; - while(nsize < not_parsed + required) { - nsize *= 2; - } - VALUE nbuffer = rb_str_buf_new(nsize); - rb_str_buf_cat(nbuffer, RSTRING_PTR(buffer)+mp->user.offset, not_parsed); - mp->user.buffer = nbuffer; + } else if(buffer->size <= mp->user.offset) { + /* clear buffer and rewind offset */ + buffer->free += buffer->size; + buffer->size = 0; mp->user.offset = 0; } + + if(len <= buffer->free) { + /* enough free space: just copy */ + memcpy(buffer->ptr+buffer->size, ptr, len); + buffer->size += len; + buffer->free -= len; + return; + } + + size_t csize = buffer->size + buffer->free; + + if(mp->user.offset <= buffer->size / 2) { + /* parsed less than half: realloc and copy */ + csize *= 2; + while(csize < buffer->size + len) { + csize *= 2; + } + char* tmp = (char*)realloc(buffer->ptr, csize); + // FIXME check tmp == NULL + memcpy(tmp + buffer->size, ptr, len); + buffer->ptr = tmp; + buffer->free = csize - buffer->size; + return; + } + + size_t not_parsed = buffer->size - mp->user.offset; + + if(csize < not_parsed + len) { + /* more buffer size */ + csize *= 2; + while(csize < not_parsed + len) { + csize *= 2; + } + char* tmp = (char*)realloc(buffer->ptr, csize); + // FIXME check tmp == NULL + buffer->ptr = tmp; + } + + memcpy(buffer->ptr+not_parsed, ptr, not_parsed); + buffer->size = not_parsed; + buffer->free = csize - buffer->size; + buffer->ptr = buffer->ptr; + mp->user.offset = 0; } /** @@ -405,8 +472,7 @@ static VALUE MessagePack_Unpacker_feed(VALUE self, VALUE data) { UNPACKER(self, mp); StringValue(data); - try_rewind_buffer(mp, RSTRING_LEN(data)); - rb_str_cat(mp->user.buffer, RSTRING_PTR(data), RSTRING_LEN(data)); + feed_buffer(mp, RSTRING_PTR(data), RSTRING_LEN(data)); return Qnil; } @@ -432,18 +498,12 @@ static VALUE MessagePack_Unpacker_fill(VALUE self) return Qnil; } - long len; - if(RSTRING_LEN(mp->user.buffer) == 0) { - rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, - LONG2FIX(MSGPACK_UNPACKER_BUFFER_INIT_SIZE), mp->user.buffer); - len = RSTRING_LEN(mp->user.buffer); - } else { - rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, - LONG2FIX(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE), mp->user.streambuf); - len = RSTRING_LEN(mp->user.streambuf); - try_rewind_buffer(mp, len); - rb_str_cat(mp->user.buffer, RSTRING_PTR(mp->user.streambuf), RSTRING_LEN(mp->user.streambuf)); - } + rb_funcall(mp->user.stream, mp->user.stream_append_method, 2, + LONG2FIX(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE), + mp->user.streambuf); + + size_t len = RSTRING_LEN(mp->user.streambuf); + feed_buffer(mp, RSTRING_PTR(mp->user.streambuf), len); return LONG2FIX(len); } @@ -470,7 +530,7 @@ static VALUE MessagePack_Unpacker_each(VALUE self) #endif while(1) { - if(RSTRING_LEN(mp->user.buffer) <= mp->user.offset) { + if(mp->user.buffer.size <= mp->user.offset) { do_fill: { VALUE len = MessagePack_Unpacker_fill(self); @@ -480,8 +540,9 @@ static VALUE MessagePack_Unpacker_each(VALUE self) } } - ret = template_execute_wrap(mp, mp->user.buffer, - RSTRING_LEN(mp->user.buffer), &mp->user.offset); + ret = template_execute_wrap_each(mp, + mp->user.buffer.ptr, mp->user.buffer.size, + &mp->user.offset); if(ret < 0) { rb_raise(eUnpackError, "parse error."); @@ -689,11 +750,6 @@ void Init_msgpack_unpack(VALUE mMessagePack) s_ascii_8bit = rb_enc_find_index("ASCII-8BIT"); #endif - s_slice_bang = rb_intern("slice!"); -#ifdef RUBY_VM - s_clear = rb_intern("clear"); -#endif - eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError); cUnpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject); rb_define_alloc_func(cUnpacker, MessagePack_Unpacker_alloc); From 94c39985079cff23d85d333ea69d554216ac79e0 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 26 May 2010 07:43:05 +0900 Subject: [PATCH 022/152] ruby: update gemspec --- ruby/README | 28 ++++++++++++++++++---------- ruby/msgpack.gemspec | 6 +++--- ruby/pack.h | 2 +- ruby/rbinit.c | 4 ++-- ruby/unpack.h | 2 +- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ruby/README b/ruby/README index 859ae2b..051a769 100644 --- a/ruby/README +++ b/ruby/README @@ -1,29 +1,37 @@ = MessagePack - == Description +MessagePack is a binary-based efficient object serialization library. +It enables to exchange structured objects between many languages like JSON. +But unlike JSON, it is very fast and small. + +Simple usage is as follows: + + require 'msgpack' + msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03" + MessagePack.unpack(msg) #=> [1,2,3] + +Use MessagePack::Unpacker for streaming deserialization. + == Installation === Archive Installation - rake install + ruby extconf.rb + make + make install === Gem Installation - gem install msgpack - - -== Features/Problems - - -== Synopsis + gem install msgpack == Copyright Author:: frsyuki -Copyright:: Copyright (c) 2008-2009 frsyuki +Copyright:: Copyright (c) 2008-2010 FURUHASHI Sadayuki License:: Apache License, Version 2.0 + diff --git a/ruby/msgpack.gemspec b/ruby/msgpack.gemspec index c5e8c8c..0f08628 100644 --- a/ruby/msgpack.gemspec +++ b/ruby/msgpack.gemspec @@ -1,14 +1,14 @@ Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = "msgpack" - s.version = "0.3.9" + s.version = "0.4.0" s.summary = "MessagePack, a binary-based efficient data interchange format." s.author = "FURUHASHI Sadayuki" s.email = "frsyuki@users.sourceforge.jp" s.homepage = "http://msgpack.sourceforge.net/" s.rubyforge_project = "msgpack" - s.has_rdoc = false - s.extra_rdoc_files = ["README", "ChangeLog", "AUTHORS"] + s.has_rdoc = true + s.rdoc_options = ["ext"] s.require_paths = ["lib"] s.files = Dir["ext/**/*", "msgpack/**/*", "test/**/*"] s.test_files = Dir["test/test_*.rb"] diff --git a/ruby/pack.h b/ruby/pack.h index c9b08a4..f162a86 100644 --- a/ruby/pack.h +++ b/ruby/pack.h @@ -1,7 +1,7 @@ /* * MessagePack for Ruby packing routine * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * Copyright (C) 2008-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. diff --git a/ruby/rbinit.c b/ruby/rbinit.c index 4ad6beb..ad51f6b 100644 --- a/ruby/rbinit.c +++ b/ruby/rbinit.c @@ -1,7 +1,7 @@ /* * MessagePack for Ruby * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * Copyright (C) 2008-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. @@ -31,7 +31,7 @@ static VALUE mMessagePack; * * gem install msgpack * - * Simple usage is as follows. + * Simple usage is as follows: * * require 'msgpack' * msg = [1,2,3].to_msgpack #=> "\x93\x01\x02\x03" diff --git a/ruby/unpack.h b/ruby/unpack.h index ce2a8de..91d3eb7 100644 --- a/ruby/unpack.h +++ b/ruby/unpack.h @@ -1,7 +1,7 @@ /* * MessagePack for Ruby unpacking routine * - * Copyright (C) 2008-2009 FURUHASHI Sadayuki + * Copyright (C) 2008-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. From 47185d757ebee52cc06775a843b3bf06292d8bf1 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 26 May 2010 07:55:02 +0900 Subject: [PATCH 023/152] ruby: version 0.4.0 --- ruby/unpack.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ruby/unpack.c b/ruby/unpack.c index c4b5e29..c93af35 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -130,8 +130,6 @@ static inline int template_callback_map_item(unpack_user* u, VALUE* c, VALUE k, #endif static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, VALUE* o) -//{ *o = (l <= COW_MIN_SIZE) ? rb_str_new(p, l) : rb_str_substr(u->source, p - b, l); return 0; } -//{ *o = rb_str_new(p, l); return 0; } { if(u->source == Qnil || l <= COW_MIN_SIZE) { *o = rb_str_new(p, l); From 293293c23cf6672827248910f133b22d0d58fcf3 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 26 May 2010 18:01:27 +0900 Subject: [PATCH 024/152] ruby: set mp->user.source = Qnil before tempalte_execute_do on Unpacker#each --- ruby/unpack.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/ruby/unpack.c b/ruby/unpack.c index c93af35..65852b9 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -156,15 +156,25 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha rb_raise(rb_eTypeError, "instance of String needed"); \ } +#ifdef RUBY_VM +#define RERAISE rb_exc_raise(rb_errinfo()) +#else +#define RERAISE rb_exc_raise(ruby_errinfo) +#endif -static VALUE template_execute_rescue(VALUE nouse) + +static VALUE template_execute_rescue(VALUE data) { rb_gc_enable(); -#ifdef RUBY_VM - rb_exc_raise(rb_errinfo()); -#else - rb_exc_raise(ruby_errinfo); -#endif + VALUE* resc = (VALUE*)data; + rb_enc_set_index(resc[0], (int)resc[1]); + RERAISE; +} + +static VALUE template_execute_rescue_each(VALUE nouse) +{ + rb_gc_enable(); + RERAISE; } static VALUE template_execute_do(VALUE argv) @@ -192,7 +202,6 @@ static int template_execute_wrap(msgpack_unpack_t* mp, }; #ifdef HAVE_RUBY_ENCODING_H - // FIXME encodingをASCII-8BITにする int enc_orig = rb_enc_get_index(str); rb_enc_set_index(str, s_ascii_8bit); #endif @@ -202,10 +211,10 @@ static int template_execute_wrap(msgpack_unpack_t* mp, mp->user.source = str; - int ret = (int)rb_rescue(template_execute_do, (VALUE)args, - template_execute_rescue, Qnil); + VALUE resc[2] = {str, enc_orig}; - mp->user.source = Qnil; + int ret = (int)rb_rescue(template_execute_do, (VALUE)args, + template_execute_rescue, (VALUE)resc); rb_gc_enable(); @@ -229,8 +238,10 @@ static int template_execute_wrap_each(msgpack_unpack_t* mp, // FIXME execute実行中はmp->topが更新されないのでGC markが機能しない rb_gc_disable(); + mp->user.source = Qnil; + int ret = (int)rb_rescue(template_execute_do, (VALUE)args, - template_execute_rescue, Qnil); + template_execute_rescue_each, Qnil); rb_gc_enable(); @@ -401,8 +412,7 @@ static void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len) struct unpack_buffer* buffer = &mp->user.buffer; if(buffer->size == 0) { - char* tmp = (char*)malloc(MSGPACK_UNPACKER_BUFFER_INIT_SIZE); - // FIXME check tmp == NULL + char* tmp = ALLOC_N(char, MSGPACK_UNPACKER_BUFFER_INIT_SIZE); buffer->ptr = tmp; buffer->free = MSGPACK_UNPACKER_BUFFER_INIT_SIZE; buffer->size = 0; @@ -430,8 +440,7 @@ static void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len) while(csize < buffer->size + len) { csize *= 2; } - char* tmp = (char*)realloc(buffer->ptr, csize); - // FIXME check tmp == NULL + char* tmp = REALLOC_N(buffer->ptr, char, csize); memcpy(tmp + buffer->size, ptr, len); buffer->ptr = tmp; buffer->free = csize - buffer->size; @@ -446,8 +455,7 @@ static void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len) while(csize < not_parsed + len) { csize *= 2; } - char* tmp = (char*)realloc(buffer->ptr, csize); - // FIXME check tmp == NULL + char* tmp = REALLOC_N(buffer->ptr, char, csize); buffer->ptr = tmp; } From 3fbcde4bd74e00e208b10aa00c389256de0ba317 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 26 May 2010 18:11:09 +0900 Subject: [PATCH 025/152] ruby: don't use rb_enc_set/get on ruby 1.8 --- ruby/msgpack.gemspec | 2 +- ruby/unpack.c | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ruby/msgpack.gemspec b/ruby/msgpack.gemspec index 0f08628..e10622f 100644 --- a/ruby/msgpack.gemspec +++ b/ruby/msgpack.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = "msgpack" - s.version = "0.4.0" + s.version = "0.4.1" s.summary = "MessagePack, a binary-based efficient data interchange format." s.author = "FURUHASHI Sadayuki" s.email = "frsyuki@users.sourceforge.jp" diff --git a/ruby/unpack.c b/ruby/unpack.c index 65852b9..0f7b9f0 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -163,15 +163,17 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha #endif -static VALUE template_execute_rescue(VALUE data) +#ifdef HAVE_RUBY_ENCODING_H +static VALUE template_execute_rescue_enc(VALUE data) { rb_gc_enable(); VALUE* resc = (VALUE*)data; rb_enc_set_index(resc[0], (int)resc[1]); RERAISE; } +#endif -static VALUE template_execute_rescue_each(VALUE nouse) +static VALUE template_execute_rescue(VALUE nouse) { rb_gc_enable(); RERAISE; @@ -211,10 +213,14 @@ static int template_execute_wrap(msgpack_unpack_t* mp, mp->user.source = str; +#ifdef HAVE_RUBY_ENCODING_H VALUE resc[2] = {str, enc_orig}; - int ret = (int)rb_rescue(template_execute_do, (VALUE)args, - template_execute_rescue, (VALUE)resc); + template_execute_rescue_enc, (VALUE)resc); +#else + int ret = (int)rb_rescue(template_execute_do, (VALUE)args, + template_execute_rescue, Qnil); +#endif rb_gc_enable(); @@ -241,7 +247,7 @@ static int template_execute_wrap_each(msgpack_unpack_t* mp, mp->user.source = Qnil; int ret = (int)rb_rescue(template_execute_do, (VALUE)args, - template_execute_rescue_each, Qnil); + template_execute_rescue, Qnil); rb_gc_enable(); From 6df86384ca2e20c34b78aeb5d9f72a885bd50a16 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 29 May 2010 07:54:49 +0900 Subject: [PATCH 026/152] java: update javadoc --- java/src/main/java/org/msgpack/Packer.java | 16 ++++++++++++++++ java/src/main/java/org/msgpack/Unpacker.java | 8 +++++--- java/src/main/java/org/msgpack/package-info.java | 8 ++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 java/src/main/java/org/msgpack/package-info.java diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 3fa421f..a92d2b6 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -23,6 +23,22 @@ import java.nio.ByteBuffer; import java.util.List; import java.util.Map; +/** + * Packer enables you to serialize objects into OutputStream. + * + *
+ * // 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());
+ * 
+ * + * You can serialize objects that implements {@link MessagePackable} interface. + */ public class Packer { protected byte[] castBytes = new byte[9]; protected ByteBuffer castBuffer = ByteBuffer.wrap(castBytes); diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index e84aff9..39bd8fa 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -24,11 +24,13 @@ import java.util.Iterator; import java.nio.ByteBuffer; /** - * Deserializer class that includes Buffered API, Unbuffered API, - * Schema API and Direct Conversion API. + * Unpacker enables you to deserialize objects from stream. + * + * Unpacker provides Buffered API, Unbuffered API, Schema API + * and Direct Conversion API. * * Buffered API uses the internal buffer of the Unpacker. - * Following code uses Buffered API with an input stream: + * Following code uses Buffered API with an InputStream: *
  * // create an unpacker with input stream
  * Unpacker pac = new Unpacker(System.in);
diff --git a/java/src/main/java/org/msgpack/package-info.java b/java/src/main/java/org/msgpack/package-info.java
new file mode 100644
index 0000000..7e9b8a2
--- /dev/null
+++ b/java/src/main/java/org/msgpack/package-info.java
@@ -0,0 +1,8 @@
+/**
+ * MessagePack is a binary-based efficient object serialization library.
+ * It enables to exchange structured objects between many languages like JSON.
+ * But unlike JSON, it is very fast and small.
+ *
+ * Use {@link Packer} to serialize and {@link Unpacker} to deserialize.
+ */
+package org.msgpack;

From 81b0c316cda14629821005cba5ce34c80ccd61d2 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Sun, 30 May 2010 01:39:48 +0900
Subject: [PATCH 027/152] java: Unpacker: rewind internal buffer on filled <=
 offset

---
 java/src/main/java/org/msgpack/Unpacker.java | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java
index 39bd8fa..f22c58b 100644
--- a/java/src/main/java/org/msgpack/Unpacker.java
+++ b/java/src/main/java/org/msgpack/Unpacker.java
@@ -288,6 +288,12 @@ public class Unpacker implements Iterable {
 			return;
 		}
 
+		if(impl.filled <= impl.offset) {
+			// rewind the buffer
+			impl.filled = 0;
+			impl.offset = 0;
+		}
+
 		if(impl.buffer.length - impl.filled >= require) {
 			return;
 		}

From 2f5d83f07d7a50bfb7287e1fa28bc7a7fd7e7d49 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Sun, 30 May 2010 01:45:07 +0900
Subject: [PATCH 028/152] cpp: type::tuple& operator>>: fix conversion type

---
 cpp/msgpack/type/tuple.hpp.erb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cpp/msgpack/type/tuple.hpp.erb b/cpp/msgpack/type/tuple.hpp.erb
index 1b0c172..0d9ae91 100644
--- a/cpp/msgpack/type/tuple.hpp.erb
+++ b/cpp/msgpack/type/tuple.hpp.erb
@@ -141,7 +141,7 @@ type::tuple, 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>(&v.template get<<%=j%>>());<%}%>
+	o.via.array.ptr[<%=j%>].convert>::type>(&v.template get<<%=j%>>());<%}%>
 	return v;
 }
 <%}%>

From 602971408ba8c2c1490bd87d2987ab65900a5297 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Sun, 30 May 2010 03:02:40 +0900
Subject: [PATCH 029/152] cpp: move source files into src/ directory

---
 cpp/Makefile.am                               | 88 +------------------
 cpp/configure.in                              | 19 +++-
 cpp/preprocess                                | 16 ++--
 cpp/src/Makefile.am                           | 75 ++++++++++++++++
 cpp/{ => src}/msgpack.h                       |  0
 cpp/{ => src}/msgpack.hpp                     |  0
 cpp/{ => src}/msgpack/object.h                |  0
 cpp/{ => src}/msgpack/object.hpp              |  0
 cpp/{ => src}/msgpack/pack.h                  |  0
 cpp/{ => src}/msgpack/pack.hpp                |  0
 cpp/{ => src}/msgpack/sbuffer.h               |  0
 cpp/{ => src}/msgpack/sbuffer.hpp             |  0
 cpp/{ => src}/msgpack/type.hpp                |  0
 cpp/{ => src}/msgpack/type/bool.hpp           |  0
 cpp/{ => src}/msgpack/type/define.hpp.erb     |  0
 cpp/{ => src}/msgpack/type/deque.hpp          |  0
 cpp/{ => src}/msgpack/type/float.hpp          |  0
 cpp/{ => src}/msgpack/type/int.hpp            |  0
 cpp/{ => src}/msgpack/type/list.hpp           |  0
 cpp/{ => src}/msgpack/type/map.hpp            |  0
 cpp/{ => src}/msgpack/type/nil.hpp            |  0
 cpp/{ => src}/msgpack/type/pair.hpp           |  0
 cpp/{ => src}/msgpack/type/raw.hpp            |  0
 cpp/{ => src}/msgpack/type/set.hpp            |  0
 cpp/{ => src}/msgpack/type/string.hpp         |  0
 .../msgpack/type/tr1/unordered_map.hpp        |  0
 .../msgpack/type/tr1/unordered_set.hpp        |  0
 cpp/{ => src}/msgpack/type/tuple.hpp.erb      |  0
 cpp/{ => src}/msgpack/type/vector.hpp         |  0
 cpp/{ => src}/msgpack/unpack.h                |  0
 cpp/{ => src}/msgpack/unpack.hpp              |  0
 cpp/{ => src}/msgpack/vrefbuffer.h            |  0
 cpp/{ => src}/msgpack/vrefbuffer.hpp          |  0
 cpp/{ => src}/msgpack/zbuffer.h               |  0
 cpp/{ => src}/msgpack/zbuffer.hpp             |  0
 cpp/{ => src}/msgpack/zone.h                  |  0
 cpp/{ => src}/msgpack/zone.hpp.erb            |  0
 cpp/{ => src}/object.cpp                      |  0
 cpp/{ => src}/objectc.c                       |  0
 cpp/{ => src}/unpack.c                        |  0
 cpp/{ => src}/vrefbuffer.c                    |  0
 cpp/{ => src}/zone.c                          |  0
 cpp/test.mk                                   |  9 --
 cpp/test/Makefile.am                          | 14 ++-
 cpp/{ => test}/msgpack_test.cpp               |  0
 cpp/{ => test}/msgpackc_test.cpp              |  0
 46 files changed, 114 insertions(+), 107 deletions(-)
 create mode 100644 cpp/src/Makefile.am
 rename cpp/{ => src}/msgpack.h (100%)
 rename cpp/{ => src}/msgpack.hpp (100%)
 rename cpp/{ => src}/msgpack/object.h (100%)
 rename cpp/{ => src}/msgpack/object.hpp (100%)
 rename cpp/{ => src}/msgpack/pack.h (100%)
 rename cpp/{ => src}/msgpack/pack.hpp (100%)
 rename cpp/{ => src}/msgpack/sbuffer.h (100%)
 rename cpp/{ => src}/msgpack/sbuffer.hpp (100%)
 rename cpp/{ => src}/msgpack/type.hpp (100%)
 rename cpp/{ => src}/msgpack/type/bool.hpp (100%)
 rename cpp/{ => src}/msgpack/type/define.hpp.erb (100%)
 rename cpp/{ => src}/msgpack/type/deque.hpp (100%)
 rename cpp/{ => src}/msgpack/type/float.hpp (100%)
 rename cpp/{ => src}/msgpack/type/int.hpp (100%)
 rename cpp/{ => src}/msgpack/type/list.hpp (100%)
 rename cpp/{ => src}/msgpack/type/map.hpp (100%)
 rename cpp/{ => src}/msgpack/type/nil.hpp (100%)
 rename cpp/{ => src}/msgpack/type/pair.hpp (100%)
 rename cpp/{ => src}/msgpack/type/raw.hpp (100%)
 rename cpp/{ => src}/msgpack/type/set.hpp (100%)
 rename cpp/{ => src}/msgpack/type/string.hpp (100%)
 rename cpp/{ => src}/msgpack/type/tr1/unordered_map.hpp (100%)
 rename cpp/{ => src}/msgpack/type/tr1/unordered_set.hpp (100%)
 rename cpp/{ => src}/msgpack/type/tuple.hpp.erb (100%)
 rename cpp/{ => src}/msgpack/type/vector.hpp (100%)
 rename cpp/{ => src}/msgpack/unpack.h (100%)
 rename cpp/{ => src}/msgpack/unpack.hpp (100%)
 rename cpp/{ => src}/msgpack/vrefbuffer.h (100%)
 rename cpp/{ => src}/msgpack/vrefbuffer.hpp (100%)
 rename cpp/{ => src}/msgpack/zbuffer.h (100%)
 rename cpp/{ => src}/msgpack/zbuffer.hpp (100%)
 rename cpp/{ => src}/msgpack/zone.h (100%)
 rename cpp/{ => src}/msgpack/zone.hpp.erb (100%)
 rename cpp/{ => src}/object.cpp (100%)
 rename cpp/{ => src}/objectc.c (100%)
 rename cpp/{ => src}/unpack.c (100%)
 rename cpp/{ => src}/vrefbuffer.c (100%)
 rename cpp/{ => src}/zone.c (100%)
 delete mode 100644 cpp/test.mk
 rename cpp/{ => test}/msgpack_test.cpp (100%)
 rename cpp/{ => test}/msgpackc_test.cpp (100%)

diff --git a/cpp/Makefile.am b/cpp/Makefile.am
index 08eb7a5..6b37803 100644
--- a/cpp/Makefile.am
+++ b/cpp/Makefile.am
@@ -1,75 +1,6 @@
+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 \
@@ -77,17 +8,6 @@ EXTRA_DIST = \
 		msgpack_vc8.sln \
 		msgpack_vc8.postbuild.bat
 
-SUBDIRS = test
-
-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)
+EXTRA_DIST = \
+		$(DOC_FILES)
 
diff --git a/cpp/configure.in b/cpp/configure.in
index 61fde4f..0895be4 100644
--- a/cpp/configure.in
+++ b/cpp/configure.in
@@ -1,4 +1,4 @@
-AC_INIT(object.cpp)
+AC_INIT(src/object.cpp)
 AC_CONFIG_AUX_DIR(ac)
 AM_INIT_AUTOMAKE(msgpack, 0.5.0)
 AC_CONFIG_HEADER(config.h)
@@ -21,6 +21,21 @@ 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.]) )
+if test "$enable_debug" != "no"; then
+	CXXFLAGS="$CXXFLAGS -g"
+	CFLAGS="$CFLAGS -g"
+else
+	CXXFLAGS="$CXXFLAGS -DNDEBUG"
+	CFLAGS="$CFLAGS -DNDEBUG"
+fi
+AC_MSG_RESULT($enable_debug)
+
+
 AC_CACHE_CHECK([for __sync_* atomic operations], msgpack_cv_atomic_ops, [
 	AC_TRY_LINK([
 		int atomic_sub(int i) { return __sync_sub_and_fetch(&i, 1); }
@@ -39,5 +54,5 @@ add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as f
 ])
 fi
 
-AC_OUTPUT([Makefile test/Makefile])
+AC_OUTPUT([Makefile src/Makefile test/Makefile])
 
diff --git a/cpp/preprocess b/cpp/preprocess
index 80a8357..452006a 100755
--- a/cpp/preprocess
+++ b/cpp/preprocess
@@ -11,12 +11,12 @@ preprocess() {
 	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/
+preprocess src/msgpack/type/tuple.hpp
+preprocess src/msgpack/type/define.hpp
+preprocess src/msgpack/zone.hpp
+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/
 
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
new file mode 100644
index 0000000..4cfa87e
--- /dev/null
+++ b/cpp/src/Makefile.am
@@ -0,0 +1,75 @@
+
+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 = \
+		msgpack/zone.hpp.erb \
+		msgpack/type/define.hpp.erb \
+		msgpack/type/tuple.hpp.erb
+
diff --git a/cpp/msgpack.h b/cpp/src/msgpack.h
similarity index 100%
rename from cpp/msgpack.h
rename to cpp/src/msgpack.h
diff --git a/cpp/msgpack.hpp b/cpp/src/msgpack.hpp
similarity index 100%
rename from cpp/msgpack.hpp
rename to cpp/src/msgpack.hpp
diff --git a/cpp/msgpack/object.h b/cpp/src/msgpack/object.h
similarity index 100%
rename from cpp/msgpack/object.h
rename to cpp/src/msgpack/object.h
diff --git a/cpp/msgpack/object.hpp b/cpp/src/msgpack/object.hpp
similarity index 100%
rename from cpp/msgpack/object.hpp
rename to cpp/src/msgpack/object.hpp
diff --git a/cpp/msgpack/pack.h b/cpp/src/msgpack/pack.h
similarity index 100%
rename from cpp/msgpack/pack.h
rename to cpp/src/msgpack/pack.h
diff --git a/cpp/msgpack/pack.hpp b/cpp/src/msgpack/pack.hpp
similarity index 100%
rename from cpp/msgpack/pack.hpp
rename to cpp/src/msgpack/pack.hpp
diff --git a/cpp/msgpack/sbuffer.h b/cpp/src/msgpack/sbuffer.h
similarity index 100%
rename from cpp/msgpack/sbuffer.h
rename to cpp/src/msgpack/sbuffer.h
diff --git a/cpp/msgpack/sbuffer.hpp b/cpp/src/msgpack/sbuffer.hpp
similarity index 100%
rename from cpp/msgpack/sbuffer.hpp
rename to cpp/src/msgpack/sbuffer.hpp
diff --git a/cpp/msgpack/type.hpp b/cpp/src/msgpack/type.hpp
similarity index 100%
rename from cpp/msgpack/type.hpp
rename to cpp/src/msgpack/type.hpp
diff --git a/cpp/msgpack/type/bool.hpp b/cpp/src/msgpack/type/bool.hpp
similarity index 100%
rename from cpp/msgpack/type/bool.hpp
rename to cpp/src/msgpack/type/bool.hpp
diff --git a/cpp/msgpack/type/define.hpp.erb b/cpp/src/msgpack/type/define.hpp.erb
similarity index 100%
rename from cpp/msgpack/type/define.hpp.erb
rename to cpp/src/msgpack/type/define.hpp.erb
diff --git a/cpp/msgpack/type/deque.hpp b/cpp/src/msgpack/type/deque.hpp
similarity index 100%
rename from cpp/msgpack/type/deque.hpp
rename to cpp/src/msgpack/type/deque.hpp
diff --git a/cpp/msgpack/type/float.hpp b/cpp/src/msgpack/type/float.hpp
similarity index 100%
rename from cpp/msgpack/type/float.hpp
rename to cpp/src/msgpack/type/float.hpp
diff --git a/cpp/msgpack/type/int.hpp b/cpp/src/msgpack/type/int.hpp
similarity index 100%
rename from cpp/msgpack/type/int.hpp
rename to cpp/src/msgpack/type/int.hpp
diff --git a/cpp/msgpack/type/list.hpp b/cpp/src/msgpack/type/list.hpp
similarity index 100%
rename from cpp/msgpack/type/list.hpp
rename to cpp/src/msgpack/type/list.hpp
diff --git a/cpp/msgpack/type/map.hpp b/cpp/src/msgpack/type/map.hpp
similarity index 100%
rename from cpp/msgpack/type/map.hpp
rename to cpp/src/msgpack/type/map.hpp
diff --git a/cpp/msgpack/type/nil.hpp b/cpp/src/msgpack/type/nil.hpp
similarity index 100%
rename from cpp/msgpack/type/nil.hpp
rename to cpp/src/msgpack/type/nil.hpp
diff --git a/cpp/msgpack/type/pair.hpp b/cpp/src/msgpack/type/pair.hpp
similarity index 100%
rename from cpp/msgpack/type/pair.hpp
rename to cpp/src/msgpack/type/pair.hpp
diff --git a/cpp/msgpack/type/raw.hpp b/cpp/src/msgpack/type/raw.hpp
similarity index 100%
rename from cpp/msgpack/type/raw.hpp
rename to cpp/src/msgpack/type/raw.hpp
diff --git a/cpp/msgpack/type/set.hpp b/cpp/src/msgpack/type/set.hpp
similarity index 100%
rename from cpp/msgpack/type/set.hpp
rename to cpp/src/msgpack/type/set.hpp
diff --git a/cpp/msgpack/type/string.hpp b/cpp/src/msgpack/type/string.hpp
similarity index 100%
rename from cpp/msgpack/type/string.hpp
rename to cpp/src/msgpack/type/string.hpp
diff --git a/cpp/msgpack/type/tr1/unordered_map.hpp b/cpp/src/msgpack/type/tr1/unordered_map.hpp
similarity index 100%
rename from cpp/msgpack/type/tr1/unordered_map.hpp
rename to cpp/src/msgpack/type/tr1/unordered_map.hpp
diff --git a/cpp/msgpack/type/tr1/unordered_set.hpp b/cpp/src/msgpack/type/tr1/unordered_set.hpp
similarity index 100%
rename from cpp/msgpack/type/tr1/unordered_set.hpp
rename to cpp/src/msgpack/type/tr1/unordered_set.hpp
diff --git a/cpp/msgpack/type/tuple.hpp.erb b/cpp/src/msgpack/type/tuple.hpp.erb
similarity index 100%
rename from cpp/msgpack/type/tuple.hpp.erb
rename to cpp/src/msgpack/type/tuple.hpp.erb
diff --git a/cpp/msgpack/type/vector.hpp b/cpp/src/msgpack/type/vector.hpp
similarity index 100%
rename from cpp/msgpack/type/vector.hpp
rename to cpp/src/msgpack/type/vector.hpp
diff --git a/cpp/msgpack/unpack.h b/cpp/src/msgpack/unpack.h
similarity index 100%
rename from cpp/msgpack/unpack.h
rename to cpp/src/msgpack/unpack.h
diff --git a/cpp/msgpack/unpack.hpp b/cpp/src/msgpack/unpack.hpp
similarity index 100%
rename from cpp/msgpack/unpack.hpp
rename to cpp/src/msgpack/unpack.hpp
diff --git a/cpp/msgpack/vrefbuffer.h b/cpp/src/msgpack/vrefbuffer.h
similarity index 100%
rename from cpp/msgpack/vrefbuffer.h
rename to cpp/src/msgpack/vrefbuffer.h
diff --git a/cpp/msgpack/vrefbuffer.hpp b/cpp/src/msgpack/vrefbuffer.hpp
similarity index 100%
rename from cpp/msgpack/vrefbuffer.hpp
rename to cpp/src/msgpack/vrefbuffer.hpp
diff --git a/cpp/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h
similarity index 100%
rename from cpp/msgpack/zbuffer.h
rename to cpp/src/msgpack/zbuffer.h
diff --git a/cpp/msgpack/zbuffer.hpp b/cpp/src/msgpack/zbuffer.hpp
similarity index 100%
rename from cpp/msgpack/zbuffer.hpp
rename to cpp/src/msgpack/zbuffer.hpp
diff --git a/cpp/msgpack/zone.h b/cpp/src/msgpack/zone.h
similarity index 100%
rename from cpp/msgpack/zone.h
rename to cpp/src/msgpack/zone.h
diff --git a/cpp/msgpack/zone.hpp.erb b/cpp/src/msgpack/zone.hpp.erb
similarity index 100%
rename from cpp/msgpack/zone.hpp.erb
rename to cpp/src/msgpack/zone.hpp.erb
diff --git a/cpp/object.cpp b/cpp/src/object.cpp
similarity index 100%
rename from cpp/object.cpp
rename to cpp/src/object.cpp
diff --git a/cpp/objectc.c b/cpp/src/objectc.c
similarity index 100%
rename from cpp/objectc.c
rename to cpp/src/objectc.c
diff --git a/cpp/unpack.c b/cpp/src/unpack.c
similarity index 100%
rename from cpp/unpack.c
rename to cpp/src/unpack.c
diff --git a/cpp/vrefbuffer.c b/cpp/src/vrefbuffer.c
similarity index 100%
rename from cpp/vrefbuffer.c
rename to cpp/src/vrefbuffer.c
diff --git a/cpp/zone.c b/cpp/src/zone.c
similarity index 100%
rename from cpp/zone.c
rename to cpp/src/zone.c
diff --git a/cpp/test.mk b/cpp/test.mk
deleted file mode 100644
index f1beac5..0000000
--- a/cpp/test.mk
+++ /dev/null
@@ -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 $@
-
diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am
index 2b96669..3670d7f 100644
--- a/cpp/test/Makefile.am
+++ b/cpp/test/Makefile.am
@@ -1,7 +1,7 @@
 
-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 \
@@ -9,7 +9,9 @@ check_PROGRAMS = \
 		streaming \
 		object \
 		convert \
-		buffer
+		buffer \
+		msgpackc_test \
+		msgpack_test
 
 TESTS = $(check_PROGRAMS)
 
@@ -26,3 +28,7 @@ convert_SOURCES = convert.cc
 buffer_SOURCES = buffer.cc
 buffer_LDADD = -lz
 
+msgpackc_test_SOURCES = msgpackc_test.cpp
+
+msgpack_test_SOURCES = msgpack_test.cpp
+
diff --git a/cpp/msgpack_test.cpp b/cpp/test/msgpack_test.cpp
similarity index 100%
rename from cpp/msgpack_test.cpp
rename to cpp/test/msgpack_test.cpp
diff --git a/cpp/msgpackc_test.cpp b/cpp/test/msgpackc_test.cpp
similarity index 100%
rename from cpp/msgpackc_test.cpp
rename to cpp/test/msgpackc_test.cpp

From 6b5b76b0c908c338dbc4aaa86aa42722da4adca3 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Sun, 30 May 2010 15:01:10 +0900
Subject: [PATCH 030/152] initial import from
 http://bitbucket.org/kuenishi/messagepack-for-erlang

---
 erlang/.gitignore  |   3 +
 erlang/msgpack.erl | 377 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 380 insertions(+)
 create mode 100644 erlang/.gitignore
 create mode 100644 erlang/msgpack.erl

diff --git a/erlang/.gitignore b/erlang/.gitignore
new file mode 100644
index 0000000..911d6e3
--- /dev/null
+++ b/erlang/.gitignore
@@ -0,0 +1,3 @@
+MANIFEST
+*.beam
+
diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
new file mode 100644
index 0000000..4b8da47
--- /dev/null
+++ b/erlang/msgpack.erl
@@ -0,0 +1,377 @@
+%%
+%% MessagePack for Erlang
+%%
+%% Copyright (C) 2009 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.
+%
+% Thanks to id:frsyuki for his sophiscated binary format specification.
+%
+
+-module(msgpack).
+-author('kuenishi+msgpack@gmail.com').
+
+-export([pack/1, unpack/1, unpack_all/1, test/0]).
+
+% compile:
+% erl> c(msgpack).
+% erl> S = .
+% erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
+
+%% tuples, atoms are not supported.
+%% lists, integers, double, and so on.
+%% see http://msgpack.sourceforge.jp/spec for
+%% supported formats. APIs are almost compatible
+%% for C API (http://msgpack.sourceforge.jp/c:doc)
+%% except buffering functions (both copying and zero-copying).
+-export([
+	 pack_fixnum/1,
+	 pack_nfixnum/1,
+	 pack_uint8/1,
+	 pack_uint16/1,
+	 pack_uint32/1,
+	 pack_uint64/1,
+	 pack_short/1,
+	 pack_int8/1,
+	 pack_int/1,
+	 pack_long/1,
+	 pack_long_long/1,
+	 pack_unsigned_short/1,
+	 pack_unsigned_int/1,
+	 pack_unsigned_long/1,
+	 pack_unsigned_long_long/1,
+	 pack_nil/0,
+	 pack_bool/1,
+	 pack_float/1,
+	 pack_double/1,
+	 pack_raw/1,
+	 pack_array/1,
+	 pack_map/1,
+	 pack_object/1	]).
+
+
+%  packing functions
+
+pack_short(N) when is_integer(N)->
+    pack_int16(N).
+pack_int(N) when is_integer(N)->
+    pack_int32(N).
+pack_long(N) when is_integer(N)->
+    pack_int32(N).
+pack_long_long(N) when is_integer(N)->
+    pack_int64(N).
+pack_unsigned_short(N) when is_integer(N)->
+    pack_uint16(N).
+pack_unsigned_int(N) when is_integer(N)->
+    pack_uint32(N).
+pack_unsigned_long(N) when is_integer(N)->
+    pack_uint32(N).
+pack_unsigned_long_long(N) when is_integer(N)->
+    pack_uint64(N).
+
+% positive fixnum
+pack_fixnum( N ) when is_integer( N ) and  N >= 0 ,  N < 128 ->  
+    << 2#0:1, N:7 >>.
+% negative fixnum
+pack_nfixnum( N ) when is_integer( N ) and N >= -32 , N < 0 ->
+    << 2#111:3, N:5 >>.
+% uint 8
+pack_uint8( N ) when is_integer( N )->
+    << 16#CC:8, N:8 >>.
+% uint 16
+pack_uint16( N ) when is_integer( N )->
+    << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>.
+% uint 32
+pack_uint32( N ) when is_integer( N )->
+    << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>.
+% uint 64
+pack_uint64( N ) when is_integer( N )->
+    << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>.
+% int 8
+pack_int8( N ) when is_integer( N )->
+    << 16#D0:8, N:8 >>.
+% int 16
+pack_int16( N ) when is_integer( N )->
+    << 16#D1:8, N:16/big-signed-integer-unit:1 >>.
+% int 32
+pack_int32( N ) when is_integer( N )->
+    << 16#D2:8, N:32/big-signed-integer-unit:1 >>.
+% int 64
+pack_int64( N ) when is_integer( N )->
+    << 16#D3:8, N:64/big-signed-integer-unit:1 >>.
+
+% nil
+pack_nil()->
+    << 16#C0:8 >>.
+% pack_true / pack_false
+pack_bool(true)->    << 16#C3:8 >>;
+pack_bool(false)->   << 16#C2:8 >>.
+
+% 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) when is_float(F)->
+    << 16#CB:8, F:64/big-float-unit:1 >>.
+
+power(N,0) when is_integer(N) -> 1;
+power(N,D) when is_integer(N) and is_integer(D) -> N * power(N, D-1).
+
+% raw bytes
+pack_raw(Bin) when is_binary(Bin)->
+    MaxLen = power(2,16),
+    case byte_size(Bin) of
+	Len when Len < 6->
+	    << 2#101:3, Len:5, Bin/binary >>;
+	Len when Len < MaxLen ->
+	    << 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.
+
+% list / tuple
+pack_array(L) when is_list(L)->
+    MaxLen = power(2,16),
+    case length(L) of
+ 	Len when Len < 16 ->
+ 	    << 2#1001:4, Len:4/integer-unit:1, (pack_array_(L))/binary >>;
+	Len when Len < MaxLen ->
+	    << 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_([])-> <<>>;
+pack_array_([Head|Tail])->
+    << (pack_object(Head))/binary, (pack_array_(Tail))/binary >>.
+
+unpack_array_(<<>>, 0)-> [];
+unpack_array_(Remain, 0) when is_binary(Remain)-> [Remain];
+unpack_array_(Bin, RestLen) when is_binary(Bin)->
+    {Term, Rest} = unpack(Bin),
+    [Term|unpack_array_(Rest, RestLen-1)].
+    
+pack_map({dict,M})->
+    MaxLen = power(2,16),
+    case dict:size(M) of
+	Len when Len < 16 ->
+ 	    << 2#1001:4, Len:4/integer-unit:1, (pack_map_(dict:to_list(M))) >>;
+	Len when Len < MaxLen ->
+	    << 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M))) >>;
+	Len ->
+	    << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M))) >>
+    end.
+
+pack_map_([])-> <<>>;
+pack_map_([{Key,Value}|Tail]) ->
+    << (pack_object(Key)),(pack_object(Value)),(pack_map_(Tail)) >>.
+
+unpack_map_(<<>>, 0)-> [];
+unpack_map_(Bin,  0) when is_binary(Bin)-> [Bin];
+unpack_map_(Bin, Len) when is_binary(Bin) and is_integer(Len) ->
+    { Key, Rest } = unpack(Bin),
+    { Value, Rest2 } = unpack(Rest),
+    [{Key,Value}|unpack_map_(Rest2,Len-1)].
+
+pack_object(O) when is_integer(O)->
+    pack_long_long(O);
+pack_object(O) when is_float(O)->
+    pack_double(O);
+pack_object(nil) ->
+    pack_nil();
+pack_object(Bool) when is_atom(Bool) ->
+    pack_bool(Bool);
+pack_object(Bin) when is_binary(Bin)->
+    pack_raw(Bin);
+pack_object(List)  when is_list(List)->
+    pack_array(List);
+pack_object({dict, Map})->
+    pack_map({dict, Map});
+pack_object(_) ->
+    undefined.
+
+pack(Obj)->
+    pack_object(Obj).
+
+
+% unpacking.
+% if failed in decoding and not end, get more data 
+% and feed more Bin into this function.
+% TODO: error case for imcomplete format when short for any type formats.
+-spec unpack( binary() )-> {term(), binary()}.
+unpack(Bin) when bit_size(Bin) >= 8 ->
+    << Flag:8, Payload/binary >> = Bin,
+    case Flag of 
+	16#C0 ->
+	    {nil, Payload};
+	16#C2 ->
+	    {false, Payload};
+	16#C3 ->
+	    {true, Payload};
+	16#CA -> % 32bit float
+	    << Return:32/float-unit:1, Rest/binary >> = Payload,
+	    {Return, Rest};
+	16#CB -> % 64bit float
+	    << Return:64/float-unit:1, Rest/binary >> = Payload,
+	    {Return, Rest};
+	16#CC ->
+	    << Int:8/unsigned-integer, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#CD ->
+	    << Int:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#CE ->
+	    << Int:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#CF ->
+	    << Int:64/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#D0 ->
+	    << Int:8/big-signed-integer-unit:1, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#D1 ->
+	    << Int:16/big-signed-integer-unit:1, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#D2 ->
+	    << Int:32/big-signed-integer-unit:1, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#D3 ->
+	    << Int:64/big-signed-integer-unit:1, Rest/binary >> = Payload,
+	    {Int, Rest};
+	16#DA -> % raw 16
+	    << Len:16/unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    << Return:Len/binary, Remain/binary >> = Rest,
+	    {Return, Remain};
+	16#DB -> % raw 32
+	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    << Return:Len/binary, Remain/binary >> = Rest,
+	    {Return, Remain};
+	16#DC -> % array 16
+	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    Array=unpack_array_(Rest, Len),
+	    case length(Array) of
+		Len -> {Array, <<>>};
+		_ -> 
+		    {Return, RemainRest} = lists:split(Len, Array),
+		    [Remain] = RemainRest,
+		    {Return, Remain}
+	    end;
+	16#DD -> % array 32
+	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    Array=unpack_array_(Rest, Len),
+	    case length(Array) of
+		Len -> {Array, <<>>};
+		_ -> 
+		    {Return, RemainRest} = lists:split(Len, Array),
+		    [Remain] = RemainRest,
+		    {Return, Remain}
+	    end;
+	16#DE -> % map 16
+	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    Array=unpack_map_(Rest, Len),
+	    case length(Array) of
+		Len -> { dict:from_list(Array), <<>>};
+		_ -> 
+		    {Return, RemainRest} = lists:split(Len, Array),
+		    [Remain] = RemainRest,
+		    {dict:from_list(Return), Remain}
+	    end;
+	16#DF -> % map 32
+	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
+	    Array=unpack_map_(Rest, Len),
+	    case length(Array) of
+		Len -> { dict:from_list(Array), <<>>};
+		_ -> 
+		    {Return, RemainRest} = lists:split(Len, Array),
+		    [Remain] = RemainRest,
+		    {dict:from_list(Return), Remain}
+	    end;
+
+	Code when Code >= 2#10100000 , Code < 2#11000000 ->
+%	 101XXXXX for FixRaw 
+	    Len = Code rem 2#10100000,
+	    << Return:Len/binary, Remain/binary >> = Payload,
+	    {Return, Remain};
+
+	Code when Code >= 2#10010000 , Code < 2#10100000 ->
+%        1001XXXX for FixArray
+	    Len = Code rem 2#10010000,
+	    Array=unpack_array_(Payload, Len),
+	    case length(Array) of
+		Len -> { Array, <<>>};
+		_ -> 
+		    {Return, RemainRest} = lists:split(Len, Array),
+		    [Remain] = RemainRest,
+		    {Return, Remain}
+	    end;
+
+	Code when Code >= 2#10000000 , Code < 2#10010000 ->
+%        1000XXXX for FixMap
+	    Len = Code rem 2#10000000,
+	    Array=unpack_map_(Payload, Len),
+	    case length(Array) of
+		Len -> { dict:from_list(Array), <<>>};
+		_ -> 
+		    {Return, RemainRest} = lists:split(Len, Array),
+		    [Remain] = RemainRest,
+		    {dict:from_list(Return), Remain}
+	    end;
+
+	_ ->
+	    {error, no_code_matches}
+    end.
+    
+unpack_all(Data)->
+    case unpack(Data) of
+	{ Term, Binary } when bit_size(Binary) =:= 0 -> 
+	    [Term];
+	{ Term, Binary } when is_binary(Binary) ->
+	    [Term|unpack_all(Binary)]
+    end.
+
+test()->
+    Tests = [0, 1, 2, 123, 123.123, [23, 234, 0.23], "hogehoge", <<"hoasfdafdas][">>],
+    Passed = test_(Tests),
+    Passed = length(Tests).
+%%     Port = open_port({spawn, "./a.out"}, [stream]),
+%%     receive {Port, {data, Data}}->
+%%     io:format("~p~n", [unpack_all( list_to_binary(Data) )])
+%%     after 1024-> timeout end,
+%%     Passed2 = test_(Tests, Port),
+%%     Passed2 = length(Tests),
+%%     Port ! {self(), close},
+%%     receive {Port, closed}-> ok 
+%%     after 1024 -> timeout end.
+
+test_([]) -> 0;
+test_([S|Rest])->
+%    io:format("testing: ~p~n", [S]),
+    {S, <<>>} = msgpack:unpack( msgpack:pack(S) ),
+    test_(Rest) + 1.
+
+test_([], _)-> 0;
+test_([S|Rest], Port) ->
+    Bin = msgpack:pack(S),
+    io:format("sending: ~p  -  ", [S]),
+    Port ! {self(), {command, Bin}},
+    receive
+	{Port, {data, Data}}->
+	    io:format("~p~n", [Data]),
+	    test_(Rest, Port) + 1;
+	Other->
+	    io:format("fail: ~p~n", [Other])
+    after 1024->
+	    test_(Rest, Port)
+    end.
+     
+

From d43921823ea5a20f0677410e965a54dd19c2effe Mon Sep 17 00:00:00 2001
From: Hideyuki Tanaka 
Date: Sun, 30 May 2010 17:19:43 +0900
Subject: [PATCH 031/152] fix initialize pointer

---
 haskell/src/Data/MessagePack/Base.hsc | 1 +
 1 file changed, 1 insertion(+)

diff --git a/haskell/src/Data/MessagePack/Base.hsc b/haskell/src/Data/MessagePack/Base.hsc
index 72c421c..8c7b9f5 100644
--- a/haskell/src/Data/MessagePack/Base.hsc
+++ b/haskell/src/Data/MessagePack/Base.hsc
@@ -560,6 +560,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

From 0da22193bd628690bfc58d4c36d6d48493fa7f51 Mon Sep 17 00:00:00 2001
From: Hideyuki Tanaka 
Date: Sun, 30 May 2010 17:20:49 +0900
Subject: [PATCH 032/152] fix typo

---
 haskell/src/Data/MessagePack/Monad.hs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/haskell/src/Data/MessagePack/Monad.hs b/haskell/src/Data/MessagePack/Monad.hs
index c718b8a..15f21fe 100644
--- a/haskell/src/Data/MessagePack/Monad.hs
+++ b/haskell/src/Data/MessagePack/Monad.hs
@@ -82,7 +82,7 @@ packToString m = do
   _ <- 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

From e61dc76ae1abb6235d09b52ad24383ccd75d5222 Mon Sep 17 00:00:00 2001
From: Hideyuki Tanaka 
Date: Sun, 30 May 2010 19:11:04 +0900
Subject: [PATCH 033/152] fix peek object

---
 haskell/src/Data/MessagePack/Base.hsc | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/haskell/src/Data/MessagePack/Base.hsc b/haskell/src/Data/MessagePack/Base.hsc
index 8c7b9f5..b6cdc28 100644
--- a/haskell/src/Data/MessagePack/Base.hsc
+++ b/haskell/src/Data/MessagePack/Base.hsc
@@ -506,20 +506,22 @@ peekObjectRAW ptr = do
 
 peekObjectArray :: Ptr a -> IO Object
 peekObjectArray ptr = do
-  size <- (#peek msgpack_object, via.array.size) ptr
-  p    <- (#peek msgpack_object, via.array.ptr) ptr
-  objs <- mapM (\i -> peekObject $ p `plusPtr`
+  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))
-          [0..size-1]
+           [0..size-1]
   return $ ObjectArray objs
 
 peekObjectMap :: Ptr a -> IO Object
 peekObjectMap ptr = do
-  size <- (#peek msgpack_object, via.map.size) ptr
-  p    <- (#peek msgpack_object, via.map.ptr) ptr
-  dat  <- mapM (\i -> peekObjectKV $ p `plusPtr`
+  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))
-          [0..size-1]
+           [0..size-1]
   return $ ObjectMap dat
 
 peekObjectKV :: Ptr a -> IO (Object, Object)

From 5a12d36a0a68e97dcdd2e030486f6a67138c8b44 Mon Sep 17 00:00:00 2001
From: Hideyuki Tanaka 
Date: Sun, 30 May 2010 19:45:00 +0900
Subject: [PATCH 034/152] incr version

---
 haskell/msgpack.cabal | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/haskell/msgpack.cabal b/haskell/msgpack.cabal
index 31cad3b..82cdb52 100644
--- a/haskell/msgpack.cabal
+++ b/haskell/msgpack.cabal
@@ -1,5 +1,5 @@
 Name:                msgpack
-Version:             0.2.1
+Version:             0.2.2
 License:             BSD3
 License-File:        LICENSE
 Author:              Hideyuki Tanaka

From d7d78d9a2b77df0661fb0cd6b0be7b565e6f7a25 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Mon, 31 May 2010 00:25:53 +0900
Subject: [PATCH 035/152] added more tests, and OMake continuous building.

---
 erlang/OMakefile   |  42 +++++++++++++
 erlang/OMakeroot   |  45 ++++++++++++++
 erlang/msgpack.erl | 152 +++++++++++++++++++--------------------------
 3 files changed, 151 insertions(+), 88 deletions(-)
 create mode 100644 erlang/OMakefile
 create mode 100644 erlang/OMakeroot

diff --git a/erlang/OMakefile b/erlang/OMakefile
new file mode 100644
index 0000000..ee72f78
--- /dev/null
+++ b/erlang/OMakefile
@@ -0,0 +1,42 @@
+########################################################################
+# 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 #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 $<
+
+test: msgpack.beam
+	erl -s msgpack test -s init stop
+
+clean:
+	-rm *.beam
diff --git a/erlang/OMakeroot b/erlang/OMakeroot
new file mode 100644
index 0000000..35c219d
--- /dev/null
+++ b/erlang/OMakeroot
@@ -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: .
diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 4b8da47..df7974d 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -14,42 +14,25 @@
 %%    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.
-%
-% Thanks to id:frsyuki for his sophiscated binary format specification.
-%
 
 -module(msgpack).
 -author('kuenishi+msgpack@gmail.com').
 
 -export([pack/1, unpack/1, unpack_all/1, test/0]).
 
+-include_lib("eunit/include/eunit.hrl").
+
 % compile:
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
 
-%% tuples, atoms are not supported.
-%% lists, integers, double, and so on.
+%% tuples, atoms are not supported.  lists, integers, double, and so on.
 %% see http://msgpack.sourceforge.jp/spec for
 %% supported formats. APIs are almost compatible
 %% for C API (http://msgpack.sourceforge.jp/c:doc)
 %% except buffering functions (both copying and zero-copying).
 -export([
-	 pack_fixnum/1,
-	 pack_nfixnum/1,
-	 pack_uint8/1,
-	 pack_uint16/1,
-	 pack_uint32/1,
-	 pack_uint64/1,
-	 pack_short/1,
-	 pack_int8/1,
-	 pack_int/1,
-	 pack_long/1,
-	 pack_long_long/1,
-	 pack_unsigned_short/1,
-	 pack_unsigned_int/1,
-	 pack_unsigned_long/1,
-	 pack_unsigned_long_long/1,
 	 pack_nil/0,
 	 pack_bool/1,
 	 pack_float/1,
@@ -59,55 +42,39 @@
 	 pack_map/1,
 	 pack_object/1	]).
 
-
-%  packing functions
-
-pack_short(N) when is_integer(N)->
-    pack_int16(N).
-pack_int(N) when is_integer(N)->
-    pack_int32(N).
-pack_long(N) when is_integer(N)->
-    pack_int32(N).
-pack_long_long(N) when is_integer(N)->
-    pack_int64(N).
-pack_unsigned_short(N) when is_integer(N)->
-    pack_uint16(N).
-pack_unsigned_int(N) when is_integer(N)->
-    pack_uint32(N).
-pack_unsigned_long(N) when is_integer(N)->
-    pack_uint32(N).
-pack_unsigned_long_long(N) when is_integer(N)->
-    pack_uint64(N).
-
 % positive fixnum
-pack_fixnum( N ) when is_integer( N ) and  N >= 0 ,  N < 128 ->  
-    << 2#0:1, N:7 >>.
-% negative fixnum
-pack_nfixnum( N ) when is_integer( N ) and N >= -32 , N < 0 ->
-    << 2#111:3, N:5 >>.
+pack_uint_(N) when is_integer( N ) , N < 128 ->  
+    << 2#0:1, N:7 >>;
 % uint 8
-pack_uint8( N ) when is_integer( N )->
-    << 16#CC:8, N:8 >>.
+pack_uint_( N ) when is_integer( N ) andalso N < 256 ->
+    << 16#CC:8, N:8 >>;
+
 % uint 16
-pack_uint16( N ) when is_integer( N )->
-    << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>.
+pack_uint_( N ) when is_integer( N ) andalso N < 65536 ->
+    << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>;
+
 % uint 32
-pack_uint32( N ) when is_integer( N )->
-    << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>.
+pack_uint_( N ) when is_integer( N ) andalso N < 16#FFFFFFFF->
+    << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>;
+
 % uint 64
-pack_uint64( N ) when is_integer( N )->
+pack_uint_( N ) when is_integer( N )->
     << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>.
+
+% negative fixnum
+pack_int_( N ) when is_integer( N ) , N >= -32->
+    << 2#111:3, N:5 >>;
 % int 8
-pack_int8( N ) when is_integer( N )->
-    << 16#D0:8, N:8 >>.
+pack_int_( N ) when is_integer( N ) , N >= -256 ->
+    << 16#D0:8, N:8 >>;
 % int 16
-pack_int16( N ) when is_integer( N )->
-    << 16#D1:8, N:16/big-signed-integer-unit:1 >>.
+pack_int_( N ) when is_integer( N ), N >= -65536 ->
+    << 16#D1:8, N:16/big-signed-integer-unit:1 >>;
 % int 32
-pack_int32( N ) when is_integer( N )->
-    << 16#D2:8, N:32/big-signed-integer-unit:1 >>.
+pack_int_( N ) when is_integer( N ), N >= -16#FFFFFFFF ->
+    << 16#D2:8, N:32/big-signed-integer-unit:1 >>;
 % int 64
-pack_int64( N ) when is_integer( N )->
+pack_int_( N ) when is_integer( N )->
     << 16#D3:8, N:64/big-signed-integer-unit:1 >>.
 
 % nil
@@ -183,8 +150,10 @@ unpack_map_(Bin, Len) when is_binary(Bin) and is_integer(Len) ->
     { Value, Rest2 } = unpack(Rest),
     [{Key,Value}|unpack_map_(Rest2,Len-1)].
 
-pack_object(O) when is_integer(O)->
-    pack_long_long(O);
+pack_object(O) when is_integer(O) andalso O < 0 ->
+    pack_int_(O);
+pack_object(O) when is_integer(O) ->
+    pack_uint_(O);
 pack_object(O) when is_float(O)->
     pack_double(O);
 pack_object(nil) ->
@@ -210,7 +179,7 @@ pack(Obj)->
 % TODO: error case for imcomplete format when short for any type formats.
 -spec unpack( binary() )-> {term(), binary()}.
 unpack(Bin) when bit_size(Bin) >= 8 ->
-    << Flag:8, Payload/binary >> = Bin,
+    << Flag:8/unsigned-integer, Payload/binary >> = Bin,
     case Flag of 
 	16#C0 ->
 	    {nil, Payload};
@@ -218,16 +187,18 @@ unpack(Bin) when bit_size(Bin) >= 8 ->
 	    {false, Payload};
 	16#C3 ->
 	    {true, Payload};
+
 	16#CA -> % 32bit float
 	    << Return:32/float-unit:1, Rest/binary >> = Payload,
 	    {Return, Rest};
 	16#CB -> % 64bit float
 	    << Return:64/float-unit:1, Rest/binary >> = Payload,
 	    {Return, Rest};
-	16#CC ->
+
+	16#CC -> % uint 8
 	    << Int:8/unsigned-integer, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#CD ->
+	16#CD -> % uint 16
 	    << Int:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
 	16#CE ->
@@ -236,16 +207,17 @@ unpack(Bin) when bit_size(Bin) >= 8 ->
 	16#CF ->
 	    << Int:64/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#D0 ->
+
+	16#D0 -> % int 8
 	    << Int:8/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#D1 ->
+	16#D1 -> % int 16
 	    << Int:16/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#D2 ->
+	16#D2 -> % int 32
 	    << Int:32/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#D3 ->
+	16#D3 -> % int 64
 	    << Int:64/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
 	16#DA -> % raw 16
@@ -297,6 +269,14 @@ unpack(Bin) when bit_size(Bin) >= 8 ->
 		    {dict:from_list(Return), Remain}
 	    end;
 
+	% positive fixnum
+	Code when Code >= 2#00000000, Code < 2#10000000->
+	    {Code, Payload};
+
+	% negative fixnum
+	Code when Code >= 2#11100000 ->
+	    {(Code - 16#100), Payload};
+
 	Code when Code >= 2#10100000 , Code < 2#11000000 ->
 %	 101XXXXX for FixRaw 
 	    Len = Code rem 2#10100000,
@@ -327,7 +307,8 @@ unpack(Bin) when bit_size(Bin) >= 8 ->
 		    {dict:from_list(Return), Remain}
 	    end;
 
-	_ ->
+	_Other ->
+	    erlang:display(_Other),
 	    {error, no_code_matches}
     end.
     
@@ -339,8 +320,17 @@ unpack_all(Data)->
 	    [Term|unpack_all(Binary)]
     end.
 
+-ifdef(EUNIT).
+
 test()->
-    Tests = [0, 1, 2, 123, 123.123, [23, 234, 0.23], "hogehoge", <<"hoasfdafdas][">>],
+    Tests = [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",
+	     <<"hoasfdafdas][">>,
+	     [0,42,"sum", [1,2]], [1,42, nil, [3]]
+	    ],
     Passed = test_(Tests),
     Passed = length(Tests).
 %%     Port = open_port({spawn, "./a.out"}, [stream]),
@@ -355,23 +345,9 @@ test()->
 
 test_([]) -> 0;
 test_([S|Rest])->
-%    io:format("testing: ~p~n", [S]),
-    {S, <<>>} = msgpack:unpack( msgpack:pack(S) ),
+    Pack = msgpack:pack(S),
+%    io:format("testing: ~p => ~p~n", [S, Pack]),
+    {S, <<>>} = msgpack:unpack( Pack ),
     test_(Rest) + 1.
 
-test_([], _)-> 0;
-test_([S|Rest], Port) ->
-    Bin = msgpack:pack(S),
-    io:format("sending: ~p  -  ", [S]),
-    Port ! {self(), {command, Bin}},
-    receive
-	{Port, {data, Data}}->
-	    io:format("~p~n", [Data]),
-	    test_(Rest, Port) + 1;
-	Other->
-	    io:format("fail: ~p~n", [Other])
-    after 1024->
-	    test_(Rest, Port)
-    end.
-     
-
+-endif.

From 98a5e438834b222f478e1af41ecd0e9590f6b08e Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Mon, 31 May 2010 17:16:40 +0900
Subject: [PATCH 036/152] add crosslang.cc

---
 cpp/preprocess |  12 +++--
 crosslang.cc   | 128 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 3 deletions(-)
 create mode 100644 crosslang.cc

diff --git a/cpp/preprocess b/cpp/preprocess
index 452006a..aab89f5 100755
--- a/cpp/preprocess
+++ b/cpp/preprocess
@@ -11,9 +11,15 @@ preprocess() {
 	fi
 }
 
-preprocess src/msgpack/type/tuple.hpp
-preprocess src/msgpack/type/define.hpp
-preprocess src/msgpack/zone.hpp
+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/
diff --git a/crosslang.cc b/crosslang.cc
new file mode 100644
index 0000000..3b22b74
--- /dev/null
+++ b/crosslang.cc
@@ -0,0 +1,128 @@
+//
+// How to compile:
+// $ g++ -Wall -lmsgpack crosslang.cc
+//
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+static int run(int infd, int outfd)
+try {
+	msgpack::unpacker pac;
+
+	while(true) {
+		pac.reserve_buffer(32*1024);
+
+		ssize_t count =
+			read(infd, pac.buffer(), pac.buffer_capacity());
+
+		if(count <= 0) {
+			if(count == 0) {
+				return 0;
+			}
+			if(errno == EAGAIN || errno == EINTR) {
+				continue;
+			}
+			return 1;
+		}
+
+		pac.buffer_consumed(count);
+
+		msgpack::unpacked result;
+		while(pac.next(&result)) {
+			msgpack::sbuffer sbuf;
+			msgpack::pack(sbuf, result.get());
+
+			const char* p = sbuf.data();
+			const char* const pend = p + sbuf.size();
+			while(p < pend) {
+				ssize_t bytes = write(outfd, p, pend-p);
+
+				if(bytes <= 0) {
+					if(count == 0) {
+						return 0;
+					}
+					if(errno == EAGAIN || errno == EINTR) {
+						continue;
+					}
+					return 1;
+				}
+
+				p += bytes;
+			}
+
+		}
+	}
+
+	return 0;
+
+} catch (std::exception& e) {
+	std::cerr << e.what() << std::endl;
+	return 1;
+}
+
+static void usage(const char* prog)
+{
+	printf(
+		"Usage: %s [in-file] [out-file]\n"
+		"\n"
+		"This tool is for testing of MessagePack implementation.\n"
+		"This does following behavior:\n"
+		"\n"
+		"  1. Reads objects serialized by MessagePack from  (default: stdin)\n"
+		"  2. Re-serializes the objects using C++ implementation of MessagePack (Note that C++ implementation is considered valid)\n"
+		"  3. Writes the re-serialized objects into  (default: stdout)\n"
+		"\n"
+		, prog);
+	exit(0);
+}
+
+int main(int argc, char* argv[])
+{
+	int infd = 0;
+	int outfd = 1;
+
+	if(argc < 1 || argc > 3) {
+		usage(argv[0]);
+	}
+
+	for(int i=1; i < argc; ++i) {
+		if(strlen(argv[i]) > 1 && argv[i][0] == '-') {
+			usage(argv[0]);
+		}
+	}
+
+	if(argc >= 2) {
+		const char* fname = argv[1];
+		if(strcmp(fname, "-") != 0) {
+			infd = open(fname, O_RDONLY);
+			if(infd < 0) {
+				perror("can't open input file");
+				exit(1);
+			}
+		}
+	}
+
+	if(argc >= 3) {
+		const char* fname = argv[2];
+		if(strcmp(fname, "-") != 0) {
+			outfd = open(fname, O_WRONLY | O_CREAT| O_TRUNC, 0666);
+			if(outfd < 0) {
+				perror("can't open output file");
+				exit(1);
+			}
+		}
+	}
+
+	int code = run(infd, outfd);
+
+	close(infd);
+	close(outfd);
+
+	return code;
+}
+

From a0071c2f9f1760b6eb1a40c1b6179f02c8ce7cb6 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Mon, 31 May 2010 17:27:51 +0900
Subject: [PATCH 037/152] add crosslang.rb

---
 crosslang.cc |  2 +-
 crosslang.rb | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 crosslang.rb

diff --git a/crosslang.cc b/crosslang.cc
index 3b22b74..9ec7c0f 100644
--- a/crosslang.cc
+++ b/crosslang.cc
@@ -78,7 +78,7 @@ static void usage(const char* prog)
 		"  3. Writes the re-serialized objects into  (default: stdout)\n"
 		"\n"
 		, prog);
-	exit(0);
+	exit(1);
 }
 
 int main(int argc, char* argv[])
diff --git a/crosslang.rb b/crosslang.rb
new file mode 100644
index 0000000..af36dd1
--- /dev/null
+++ b/crosslang.rb
@@ -0,0 +1,81 @@
+begin
+require 'rubygems'
+rescue LoadError
+end
+require 'msgpack'
+
+def run(inio, outio)
+	pac = MessagePack::Unpacker.new(inio)
+
+	begin
+		pac.each {|obj|
+			outio.write MessagePack.pack(obj)
+			outio.flush
+		}
+	rescue EOFError
+		return 0
+	rescue
+		$stderr.puts $!
+		return 1
+	end
+
+	return 0
+end
+
+def usage
+	puts < (default: stdin)
+  2. Re-serializes the objects using Ruby implementation of MessagePack (Note that Ruby implementation is considered valid)
+  3. Writes the re-serialized objects into  (default: stdout)
+
+EOF
+	exit 1
+end
+
+inio = $stdin
+outio = $stdout
+
+if ARGV.length > 2
+	usage
+end
+
+ARGV.each {|str|
+	if str.size > 1 && str[0] == ?-
+		usage
+	end
+}
+
+if fname = ARGV[0]
+	unless fname == "-"
+		begin
+			inio = File.open(fname)
+		rescue
+			puts "can't open output file: #{$!}"
+			exit 1
+		end
+	end
+end
+
+if fname = ARGV[1]
+	unless fname == "-"
+		begin
+			outio = File.open(fname, "w")
+		rescue
+			puts "can't open output file: #{$!}"
+			exit 1
+		end
+	end
+end
+
+code = run(inio, outio)
+
+inio.close
+outio.close
+
+exit code
+

From 7d1e51437e6b9e562420c13d18b2f7aedaa26b13 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Mon, 31 May 2010 23:13:32 +0900
Subject: [PATCH 038/152] erlang: added usage of cross-language test.

---
 erlang/msgpack.erl | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index df7974d..90ddb76 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -1,7 +1,7 @@
 %%
 %% MessagePack for Erlang
 %%
-%% Copyright (C) 2009 UENISHI Kota
+%% 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.
@@ -332,16 +332,15 @@ test()->
 	     [0,42,"sum", [1,2]], [1,42, nil, [3]]
 	    ],
     Passed = test_(Tests),
-    Passed = length(Tests).
-%%     Port = open_port({spawn, "./a.out"}, [stream]),
-%%     receive {Port, {data, Data}}->
-%%     io:format("~p~n", [unpack_all( list_to_binary(Data) )])
-%%     after 1024-> timeout end,
-%%     Passed2 = test_(Tests, Port),
-%%     Passed2 = length(Tests),
-%%     Port ! {self(), close},
-%%     receive {Port, closed}-> ok 
-%%     after 1024 -> timeout end.
+    Passed = length(Tests),
+    {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
+    Port = open_port({spawn, "ruby ../crosslang.rb"}, [binary]),
+    true = port_command(Port, msgpack:pack(Tests) ),
+     %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner
+    receive
+	{Port, {data, Data}}->  {Tests, <<>>}=msgpack:unpack(Data)
+    after 1024-> ?assert(false)   end,
+    port_close(Port).
 
 test_([]) -> 0;
 test_([S|Rest])->

From d9b467098a8cc5c4521717d58c2561f92222b333 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Mon, 31 May 2010 23:56:06 +0900
Subject: [PATCH 039/152] erlang: added more cross-language tests. better type
 specification.

---
 erlang/msgpack.erl           | 80 ++++++++++++++++++++++--------------
 erlang/testcase_generator.rb | 61 +++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 30 deletions(-)
 create mode 100644 erlang/testcase_generator.rb

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 90ddb76..f23ec09 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -18,6 +18,11 @@
 -module(msgpack).
 -author('kuenishi+msgpack@gmail.com').
 
+%% tuples, atoms are not supported.  lists, integers, double, and so on.
+%% see http://msgpack.sourceforge.jp/spec for
+%% supported formats. APIs are almost compatible
+%% for C API (http://msgpack.sourceforge.jp/c:doc)
+%% except buffering functions (both copying and zero-copying).
 -export([pack/1, unpack/1, unpack_all/1, test/0]).
 
 -include_lib("eunit/include/eunit.hrl").
@@ -27,20 +32,8 @@
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
 
-%% tuples, atoms are not supported.  lists, integers, double, and so on.
-%% see http://msgpack.sourceforge.jp/spec for
-%% supported formats. APIs are almost compatible
-%% for C API (http://msgpack.sourceforge.jp/c:doc)
-%% except buffering functions (both copying and zero-copying).
--export([
-	 pack_nil/0,
-	 pack_bool/1,
-	 pack_float/1,
-	 pack_double/1,
-	 pack_raw/1,
-	 pack_array/1,
-	 pack_map/1,
-	 pack_object/1	]).
+
+-type reason() ::  enomem.
 
 % positive fixnum
 pack_uint_(N) when is_integer( N ) , N < 128 ->  
@@ -85,9 +78,9 @@ pack_bool(true)->    << 16#C3:8 >>;
 pack_bool(false)->   << 16#C2:8 >>.
 
 % float : erlang's float is always IEEE 754 64bit format.
-pack_float(F) when is_float(F)->
+%pack_float(F) when is_float(F)->
 %    << 16#CA:8, F:32/big-float-unit:1 >>.
-    pack_double(F).
+%    pack_double(F).
 % double
 pack_double(F) when is_float(F)->
     << 16#CB:8, F:64/big-float-unit:1 >>.
@@ -177,7 +170,9 @@ pack(Obj)->
 % if failed in decoding and not end, get more data 
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
--spec unpack( binary() )-> {term(), binary()}.
+-spec unpack( binary() )-> {term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
+unpack(Bin) when not is_binary(Bin)->
+    {error, badard};
 unpack(Bin) when bit_size(Bin) >= 8 ->
     << Flag:8/unsigned-integer, Payload/binary >> = Bin,
     case Flag of 
@@ -310,7 +305,9 @@ unpack(Bin) when bit_size(Bin) >= 8 ->
 	_Other ->
 	    erlang:display(_Other),
 	    {error, no_code_matches}
-    end.
+    end;
+unpack(_)-> % when bit_size(Bin) < 8 ->
+    {more, 8}.
     
 unpack_all(Data)->
     case unpack(Data) of
@@ -322,31 +319,54 @@ unpack_all(Data)->
 
 -ifdef(EUNIT).
 
-test()->
-    Tests = [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",
-	     <<"hoasfdafdas][">>,
-	     [0,42,"sum", [1,2]], [1,42, nil, [3]]
-	    ],
+test_data()->
+    [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",
+     <<"hoasfdafdas][">>,
+     [0,42,"sum", [1,2]], [1,42, nil, [3]]
+    ].
+
+basic_test()->
+    Tests = test_data(),
     Passed = test_(Tests),
-    Passed = length(Tests),
+    Passed = length(Tests).
+
+port_test()->
+    Tests = test_data(),
     {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
     Port = open_port({spawn, "ruby ../crosslang.rb"}, [binary]),
     true = port_command(Port, msgpack:pack(Tests) ),
-     %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner
+    %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner
     receive
 	{Port, {data, Data}}->  {Tests, <<>>}=msgpack:unpack(Data)
     after 1024-> ?assert(false)   end,
     port_close(Port).
 
+unknown_test()->
+    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]]
+	    ],
+    Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
+    %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner
+    receive
+	{Port, {data, Data}}->
+	    Tests=msgpack:unpack_all(Data)
+%	    io:format("~p~n", [Tests])
+    after 1024-> ?assert(false)   end,
+    port_close(Port).
+
 test_([]) -> 0;
 test_([S|Rest])->
     Pack = msgpack:pack(S),
 %    io:format("testing: ~p => ~p~n", [S, Pack]),
     {S, <<>>} = msgpack:unpack( Pack ),
-    test_(Rest) + 1.
+    1+test_(Rest).
 
 -endif.
diff --git a/erlang/testcase_generator.rb b/erlang/testcase_generator.rb
new file mode 100644
index 0000000..a173790
--- /dev/null
+++ b/erlang/testcase_generator.rb
@@ -0,0 +1,61 @@
+begin
+require 'rubygems'
+rescue LoadError
+end
+require 'msgpack'
+
+def usage
+	puts < (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]]
+       ]
+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
+

From 49f3872d047624b1995b8c60edec8bad35429fd3 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Tue, 1 Jun 2010 00:31:12 +0900
Subject: [PATCH 040/152] erlang: temporary documentation and .gitignore

---
 erlang/.gitignore |  3 ++-
 erlang/README.md  | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 erlang/README.md

diff --git a/erlang/.gitignore b/erlang/.gitignore
index 911d6e3..7d6d324 100644
--- a/erlang/.gitignore
+++ b/erlang/.gitignore
@@ -1,3 +1,4 @@
 MANIFEST
 *.beam
-
+.omakedb*
+*.omc
diff --git a/erlang/README.md b/erlang/README.md
new file mode 100644
index 0000000..50d446d
--- /dev/null
+++ b/erlang/README.md
@@ -0,0 +1,19 @@
+MessagePack for Erlang
+======================
+Binary-based efficient object serialization library.
+
+## Status 
+
+still in development.
+
+TODOs:
+ - decide string specification.
+
+## Installation
+
+## Example
+
+## License
+
+
+ - 

From 062ed8a4c46be31da26538a4144fd441e453f30c Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 04:47:28 +0900
Subject: [PATCH 041/152] add test/cases.mpac and test/cases_compact.mpac

---
 test/cases.mpac                   | Bin 0 -> 213 bytes
 test/cases_compact.mpac           | Bin 0 -> 116 bytes
 test/cases_gen.rb                 |  76 ++++++++++++++++++++++++++++++
 crosslang.cc => test/crosslang.cc |   7 ++-
 crosslang.rb => test/crosslang.rb |   7 +++
 5 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 test/cases.mpac
 create mode 100644 test/cases_compact.mpac
 create mode 100644 test/cases_gen.rb
 rename crosslang.cc => test/crosslang.cc (94%)
 rename crosslang.rb => test/crosslang.rb (91%)

diff --git a/test/cases.mpac b/test/cases.mpac
new file mode 100644
index 0000000000000000000000000000000000000000..5ec08c6a9af42d9568eb429a209a639616e94711
GIT binary patch
literal 213
zcmXYp!4<+V3`3R4n8lOSY|v~#CV>aXw2-v7Qc6c)17ihrkbe}**V_dHM&J(DgGLop
zU?R;l%8FI9$y_sy>V|HFdDW~{neAn-roN|Wd+OcH160;F91fo!97} FixMap
+de 00 01 a1 61 61           # {"a"=>97} map 16
+df 00 00 00 01 a1 61 61     # {"a"=>97} map 32
+91 90                       # [[]]
+91 91 a1 61                 # [["a"]]
+EOF
+
+source.gsub!(/\#.+$/,'')
+bytes = source.strip.split(/\s+/).map {|x| x.to_i(16) }.pack('C*')
+
+compact_bytes = ""
+pac = MessagePack::Unpacker.new
+pac.feed(bytes)
+pac.each {|obj|
+	p obj
+	compact_bytes << obj.to_msgpack
+}
+
+File.open("cases.mpac","w") {|f| f.write(bytes) }
+File.open("cases_compact.mpac","w") {|f| f.write(compact_bytes) }
+
diff --git a/crosslang.cc b/test/crosslang.cc
similarity index 94%
rename from crosslang.cc
rename to test/crosslang.cc
index 9ec7c0f..be521b3 100644
--- a/crosslang.cc
+++ b/test/crosslang.cc
@@ -1,5 +1,10 @@
 //
-// How to compile:
+// MessagePack cross-language test tool
+//
+// $ cd ../cpp && ./configure && make && make install
+// or
+// $ port install msgpack  # MacPorts
+//
 // $ g++ -Wall -lmsgpack crosslang.cc
 //
 #include 
diff --git a/crosslang.rb b/test/crosslang.rb
similarity index 91%
rename from crosslang.rb
rename to test/crosslang.rb
index af36dd1..7aa4482 100644
--- a/crosslang.rb
+++ b/test/crosslang.rb
@@ -1,3 +1,10 @@
+#
+# MessagePack cross-language test tool
+#
+# $ gem install msgpack
+# or
+# $ port install rb_msgpack   # MacPorts
+#
 begin
 require 'rubygems'
 rescue LoadError

From 6056f939103624d21092a5e4a4d8ffaf9204c191 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 05:15:36 +0900
Subject: [PATCH 042/152] cpp: add cases.mpac test

---
 cpp/preprocess       |  2 ++
 cpp/test/Makefile.am |  5 +++++
 cpp/test/cases.cc    | 36 ++++++++++++++++++++++++++++++++++++
 test/cases_gen.rb    | 15 +++++++++++++++
 4 files changed, 58 insertions(+)
 create mode 100644 cpp/test/cases.cc

diff --git a/cpp/preprocess b/cpp/preprocess
index aab89f5..1a5e6a3 100755
--- a/cpp/preprocess
+++ b/cpp/preprocess
@@ -25,4 +25,6 @@ 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/
 
diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am
index 3670d7f..f9c5f22 100644
--- a/cpp/test/Makefile.am
+++ b/cpp/test/Makefile.am
@@ -10,6 +10,7 @@ check_PROGRAMS = \
 		object \
 		convert \
 		buffer \
+		cases \
 		msgpackc_test \
 		msgpack_test
 
@@ -28,7 +29,11 @@ convert_SOURCES = convert.cc
 buffer_SOURCES = buffer.cc
 buffer_LDADD = -lz
 
+cases_SOURCES = cases.cc
+
 msgpackc_test_SOURCES = msgpackc_test.cpp
 
 msgpack_test_SOURCES = msgpack_test.cpp
 
+EXTRA_DIST = cases.mpac cases_compact.mpac
+
diff --git a/cpp/test/cases.cc b/cpp/test/cases.cc
new file mode 100644
index 0000000..b408876
--- /dev/null
+++ b/cpp/test/cases.cc
@@ -0,0 +1,36 @@
+#include 
+#include 
+#include 
+
+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());
+	}
+}
+
diff --git a/test/cases_gen.rb b/test/cases_gen.rb
index 2fb7273..7efbfe7 100644
--- a/test/cases_gen.rb
+++ b/test/cases_gen.rb
@@ -63,14 +63,29 @@ EOF
 source.gsub!(/\#.+$/,'')
 bytes = source.strip.split(/\s+/).map {|x| x.to_i(16) }.pack('C*')
 
+objs = []
 compact_bytes = ""
 pac = MessagePack::Unpacker.new
 pac.feed(bytes)
 pac.each {|obj|
 	p obj
+	objs << obj
 	compact_bytes << obj.to_msgpack
 }
 
+# self check
+cpac = MessagePack::Unpacker.new
+cpac.feed(compact_bytes)
+cpac.each {|cobj|
+	obj = objs.shift
+	if obj != cobj
+		puts "** SELF CHECK FAILED **"
+		puts "expected: #{obj.inspect}"
+		puts "actual: #{cobj.inspect}"
+		exit 1
+	end
+}
+
 File.open("cases.mpac","w") {|f| f.write(bytes) }
 File.open("cases_compact.mpac","w") {|f| f.write(compact_bytes) }
 

From e49f091b4ebc7abbad92848502a6e06d4e9d6cfa Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 07:10:17 +0900
Subject: [PATCH 043/152] cpp: adds msgpack_sbuffer_new and
 msgpack_sbuffer_free

---
 cpp/src/msgpack/sbuffer.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/cpp/src/msgpack/sbuffer.h b/cpp/src/msgpack/sbuffer.h
index 57f424a..caff2e8 100644
--- a/cpp/src/msgpack/sbuffer.h
+++ b/cpp/src/msgpack/sbuffer.h
@@ -46,6 +46,18 @@ 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);
+}
+
 static inline int msgpack_sbuffer_write(void* data, const char* buf, unsigned int len)
 {
 	msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data;

From 103b14ea3c8a3f72213295d975328dc2399725eb Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 07:10:39 +0900
Subject: [PATCH 044/152] cpp: adds msgpack_zbuffer_new and
 msgpack_zbuffer_free

---
 cpp/src/msgpack/zbuffer.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h
index 2a32206..0ff9675 100644
--- a/cpp/src/msgpack/zbuffer.h
+++ b/cpp/src/msgpack/zbuffer.h
@@ -47,6 +47,9 @@ 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);
@@ -80,6 +83,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;

From 5a92c861e377c7db3b240dd3661feed0b58f85e0 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 07:11:01 +0900
Subject: [PATCH 045/152] cpp: adds msgpack_vrefbuffer_new and
 msgpack_vrefbuffer_free

---
 cpp/src/msgpack/vrefbuffer.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/cpp/src/msgpack/vrefbuffer.h b/cpp/src/msgpack/vrefbuffer.h
index a08e0d0..ffb2302 100644
--- a/cpp/src/msgpack/vrefbuffer.h
+++ b/cpp/src/msgpack/vrefbuffer.h
@@ -19,6 +19,7 @@
 #define MSGPACK_VREFBUFFER_H__
 
 #include "msgpack/zone.h"
+#include 
 
 #ifndef _WIN32
 #include 
@@ -67,6 +68,9 @@ 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);
@@ -83,6 +87,23 @@ 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;

From d42ecccf6f201eb92b6c04b0dde1b86a8861e58e Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 07:13:47 +0900
Subject: [PATCH 046/152] cpp: msgpack::unpack returns void

---
 cpp/src/msgpack/unpack.hpp |  8 ++++----
 cpp/test/pack_unpack.cc    | 10 +++-------
 2 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/cpp/src/msgpack/unpack.hpp b/cpp/src/msgpack/unpack.hpp
index 56ce0f6..e1617ef 100644
--- a/cpp/src/msgpack/unpack.hpp
+++ b/cpp/src/msgpack/unpack.hpp
@@ -160,7 +160,7 @@ private:
 };
 
 
-static bool unpack(unpacked* result,
+static void unpack(unpacked* result,
 		const char* data, size_t len, size_t* offset = NULL);
 
 
@@ -312,7 +312,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 +326,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");
diff --git a/cpp/test/pack_unpack.cc b/cpp/test/pack_unpack.cc
index ca9b7d5..fe4625a 100644
--- a/cpp/test/pack_unpack.cc
+++ b/cpp/test/pack_unpack.cc
@@ -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());
 
-	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());
 
-	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());
 }
 

From 684bca203ad862f30558429081d1a27681fe4559 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 07:15:58 +0900
Subject: [PATCH 047/152] cpp: adds msgpack_unpacker_next and
 msgpack_unpack_next

---
 cpp/src/msgpack/unpack.h   | 50 ++++++++++++++++++++++++++-
 cpp/src/msgpack/unpack.hpp |  7 ++--
 cpp/src/unpack.c           | 70 +++++++++++++++++++++++++++++++++++---
 cpp/src/zone.c             |  1 +
 4 files changed, 119 insertions(+), 9 deletions(-)

diff --git a/cpp/src/msgpack/unpack.h b/cpp/src/msgpack/unpack.h
index e17d0d8..1f43ab7 100644
--- a/cpp/src/msgpack/unpack.h
+++ b/cpp/src/msgpack/unpack.h
@@ -20,6 +20,15 @@
 
 #include "msgpack/zone.h"
 #include "msgpack/object.h"
+#include 
+
+#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
+#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
+#endif
+
+#ifndef MSGPACK_UNPACKER_RESERVE_SIZE
+#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
+#endif
 
 #ifdef __cplusplus
 extern "C" {
@@ -37,6 +46,10 @@ typedef struct msgpack_unpacker {
 	void* ctx;
 } msgpack_unpacker;
 
+typedef struct msgpack_unpacked {
+	msgpack_zone* zone;
+	msgpack_object data;
+} msgpack_unpacked;
 
 bool msgpack_unpacker_init(msgpack_unpacker* mpac, size_t initial_buffer_size);
 void msgpack_unpacker_destroy(msgpack_unpacker* mpac);
@@ -49,6 +62,13 @@ 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);
 
+bool msgpack_unpacker_next(msgpack_unpacker* mpac, msgpack_unpacked* pac);
+
+static inline void msgpack_unpacked_init(msgpack_unpacked* result);
+static inline void msgpack_unpacked_destroy(msgpack_unpacked* result);
+
+static inline msgpack_zone* msgpack_unpacked_release_zone(msgpack_unpacked* result);
+
 
 int msgpack_unpacker_execute(msgpack_unpacker* mpac);
 
@@ -63,6 +83,9 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac);
 static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
 
 
+bool msgpack_unpack_next(msgpack_unpacked* result,
+		const char* data, size_t len, size_t* off);
+
 
 typedef enum {
 	MSGPACK_UNPACK_SUCCESS				=  2,
@@ -73,7 +96,7 @@ typedef enum {
 
 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);
 
 
 static inline size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac);
@@ -115,6 +138,31 @@ size_t msgpack_unpacker_parsed_size(const msgpack_unpacker* mpac)
 }
 
 
+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
diff --git a/cpp/src/msgpack/unpack.hpp b/cpp/src/msgpack/unpack.hpp
index e1617ef..7b45017 100644
--- a/cpp/src/msgpack/unpack.hpp
+++ b/cpp/src/msgpack/unpack.hpp
@@ -24,8 +24,9 @@
 #include 
 #include 
 
+// 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();
diff --git a/cpp/src/unpack.c b/cpp/src/unpack.c
index 4a42526..0c21780 100644
--- a/cpp/src/unpack.c
+++ b/cpp/src/unpack.c
@@ -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;
+}
+
diff --git a/cpp/src/zone.c b/cpp/src/zone.c
index 85de765..8cc8b0d 100644
--- a/cpp/src/zone.c
+++ b/cpp/src/zone.c
@@ -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);
 }

From eabcf15790a774ce718ae1738c6ddb407e1cd279 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 07:16:25 +0900
Subject: [PATCH 048/152] cpp: update tests

---
 cpp/test/Makefile.am      |  6 ++++
 cpp/test/pack_unpack_c.cc | 70 +++++++++++++++++++++++++++++++++++++++
 cpp/test/streaming.cc     | 19 +++++++----
 cpp/test/streaming_c.cc   | 57 +++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+), 6 deletions(-)
 create mode 100644 cpp/test/pack_unpack_c.cc
 create mode 100644 cpp/test/streaming_c.cc

diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am
index f9c5f22..1f52215 100644
--- a/cpp/test/Makefile.am
+++ b/cpp/test/Makefile.am
@@ -6,7 +6,9 @@ AM_LDFLAGS = ../src/libmsgpack.la -lgtest_main
 check_PROGRAMS = \
 		zone \
 		pack_unpack \
+		pack_unpack_c \
 		streaming \
+		streaming_c \
 		object \
 		convert \
 		buffer \
@@ -20,8 +22,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
diff --git a/cpp/test/pack_unpack_c.cc b/cpp/test/pack_unpack_c.cc
new file mode 100644
index 0000000..e9a0389
--- /dev/null
+++ b/cpp/test/pack_unpack_c.cc
@@ -0,0 +1,70 @@
+#include 
+#include 
+#include 
+
+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);
+}
+
diff --git a/cpp/test/streaming.cc b/cpp/test/streaming.cc
index c01b8be..e80c671 100644
--- a/cpp/test/streaming.cc
+++ b/cpp/test/streaming.cc
@@ -2,28 +2,33 @@
 #include 
 #include 
 
-
 TEST(streaming, basic)
 {
-	std::ostringstream stream;
-	msgpack::packer pk(&stream);
+	msgpack::sbuffer buffer;
 
+	msgpack::packer 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);
 	}
 }
 
diff --git a/cpp/test/streaming_c.cc b/cpp/test/streaming_c.cc
new file mode 100644
index 0000000..6c87ac6
--- /dev/null
+++ b/cpp/test/streaming_c.cc
@@ -0,0 +1,57 @@
+#include 
+#include 
+#include 
+
+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);
+	}
+}
+

From 3d3af3284e3a5fd03395b6694fd745491509aa64 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 08:43:30 +0900
Subject: [PATCH 049/152] cpp: adds Doxyfile

---
 cpp/Doxyfile                 | 1552 ++++++++++++++++++++++++++++++++++
 cpp/Makefile.am              |    6 +
 cpp/preprocess               |    1 +
 cpp/src/Makefile.am          |   17 +
 cpp/src/msgpack.h            |    5 +
 cpp/src/msgpack/object.h     |    8 +
 cpp/src/msgpack/pack.h       |   15 +
 cpp/src/msgpack/sbuffer.h    |   17 +-
 cpp/src/msgpack/unpack.h     |  117 ++-
 cpp/src/msgpack/vrefbuffer.h |   24 +-
 cpp/src/msgpack/zbuffer.h    |   23 +-
 cpp/src/msgpack/zone.h       |    7 +
 12 files changed, 1758 insertions(+), 34 deletions(-)
 create mode 100644 cpp/Doxyfile

diff --git a/cpp/Doxyfile b/cpp/Doxyfile
new file mode 100644
index 0000000..ca77230
--- /dev/null
+++ b/cpp/Doxyfile
@@ -0,0 +1,1552 @@
+# Doxyfile 1.6.2
+
+# This file describes the settings to be used by the documentation system
+# doxygen (www.doxygen.org) for a project
+#
+# All text after a hash (#) is considered a comment and will be ignored
+# The format is:
+#       TAG = value [value, ...]
+# For lists items can also be appended using:
+#       TAG += value [value, ...]
+# Values that contain spaces should be placed between quotes (" ")
+
+#---------------------------------------------------------------------------
+# Project related configuration options
+#---------------------------------------------------------------------------
+
+# This tag specifies the encoding used for all characters in the config file
+# that follow. The default is UTF-8 which is also the encoding used for all
+# text before the first occurrence of this tag. Doxygen uses libiconv (or the
+# iconv built into libc) for the transcoding. See
+# http://www.gnu.org/software/libiconv for the list of possible encodings.
+
+DOXYFILE_ENCODING      = UTF-8
+
+# The PROJECT_NAME tag is a single word (or a sequence of words surrounded
+# by quotes) that should identify the project.
+
+PROJECT_NAME           = "MessagePack"
+
+# The PROJECT_NUMBER tag can be used to enter a project or revision number.
+# This could be handy for archiving the generated documentation or
+# if some version control system is used.
+
+PROJECT_NUMBER         =
+
+# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
+# base path where the generated documentation will be put.
+# If a relative path is entered, it will be relative to the location
+# where doxygen was started. If left blank the current directory will be used.
+
+OUTPUT_DIRECTORY       = doc
+
+# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create
+# 4096 sub-directories (in 2 levels) under the output directory of each output
+# format and will distribute the generated files over these directories.
+# Enabling this option can be useful when feeding doxygen a huge amount of
+# source files, where putting all generated files in the same directory would
+# otherwise cause performance problems for the file system.
+
+CREATE_SUBDIRS         = NO
+
+# The OUTPUT_LANGUAGE tag is used to specify the language in which all
+# documentation generated by doxygen is written. Doxygen will use this
+# information to generate all constant output in the proper language.
+# The default language is English, other supported languages are:
+# Afrikaans, Arabic, Brazilian, Catalan, Chinese, Chinese-Traditional,
+# Croatian, Czech, Danish, Dutch, Esperanto, Farsi, Finnish, French, German,
+# Greek, Hungarian, Italian, Japanese, Japanese-en (Japanese with English
+# messages), Korean, Korean-en, Lithuanian, Norwegian, Macedonian, Persian,
+# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak,
+# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese.
+
+OUTPUT_LANGUAGE        = English
+
+# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will
+# include brief member descriptions after the members that are listed in
+# the file and class documentation (similar to JavaDoc).
+# Set to NO to disable this.
+
+BRIEF_MEMBER_DESC      = YES
+
+# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend
+# the brief description of a member or function before the detailed description.
+# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the
+# brief descriptions will be completely suppressed.
+
+REPEAT_BRIEF           = YES
+
+# This tag implements a quasi-intelligent brief description abbreviator
+# that is used to form the text in various listings. Each string
+# in this list, if found as the leading text of the brief description, will be
+# stripped from the text and the result after processing the whole list, is
+# used as the annotated text. Otherwise, the brief description is used as-is.
+# If left blank, the following values are used ("$name" is automatically
+# replaced with the name of the entity): "The $name class" "The $name widget"
+# "The $name file" "is" "provides" "specifies" "contains"
+# "represents" "a" "an" "the"
+
+ABBREVIATE_BRIEF       =
+
+# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then
+# Doxygen will generate a detailed section even if there is only a brief
+# description.
+
+ALWAYS_DETAILED_SEC    = NO
+
+# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all
+# inherited members of a class in the documentation of that class as if those
+# members were ordinary class members. Constructors, destructors and assignment
+# operators of the base classes will not be shown.
+
+INLINE_INHERITED_MEMB  = NO
+
+# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full
+# path before files name in the file list and in the header files. If set
+# to NO the shortest path that makes the file name unique will be used.
+
+FULL_PATH_NAMES        = YES
+
+# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag
+# can be used to strip a user-defined part of the path. Stripping is
+# only done if one of the specified strings matches the left-hand part of
+# the path. The tag can be used to show relative paths in the file list.
+# If left blank the directory from which doxygen is run is used as the
+# path to strip.
+
+STRIP_FROM_PATH        =
+
+# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of
+# the path mentioned in the documentation of a class, which tells
+# the reader which header file to include in order to use a class.
+# If left blank only the name of the header file containing the class
+# definition is used. Otherwise one should specify the include paths that
+# are normally passed to the compiler using the -I flag.
+
+STRIP_FROM_INC_PATH    =
+
+# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter
+# (but less readable) file names. This can be useful is your file systems
+# doesn't support long names like on DOS, Mac, or CD-ROM.
+
+SHORT_NAMES            = NO
+
+# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen
+# will interpret the first line (until the first dot) of a JavaDoc-style
+# comment as the brief description. If set to NO, the JavaDoc
+# comments will behave just like regular Qt-style comments
+# (thus requiring an explicit @brief command for a brief description.)
+
+JAVADOC_AUTOBRIEF      = YES
+
+# If the QT_AUTOBRIEF tag is set to YES then Doxygen will
+# interpret the first line (until the first dot) of a Qt-style
+# comment as the brief description. If set to NO, the comments
+# will behave just like regular Qt-style comments (thus requiring
+# an explicit \brief command for a brief description.)
+
+QT_AUTOBRIEF           = NO
+
+# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen
+# treat a multi-line C++ special comment block (i.e. a block of //! or ///
+# comments) as a brief description. This used to be the default behaviour.
+# The new default is to treat a multi-line C++ comment block as a detailed
+# description. Set this tag to YES if you prefer the old behaviour instead.
+
+MULTILINE_CPP_IS_BRIEF = NO
+
+# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented
+# member inherits the documentation from any documented member that it
+# re-implements.
+
+INHERIT_DOCS           = YES
+
+# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce
+# a new page for each member. If set to NO, the documentation of a member will
+# be part of the file/class/namespace that contains it.
+
+SEPARATE_MEMBER_PAGES  = NO
+
+# The TAB_SIZE tag can be used to set the number of spaces in a tab.
+# Doxygen uses this value to replace tabs by spaces in code fragments.
+
+TAB_SIZE               = 8
+
+# This tag can be used to specify a number of aliases that acts
+# as commands in the documentation. An alias has the form "name=value".
+# For example adding "sideeffect=\par Side Effects:\n" will allow you to
+# put the command \sideeffect (or @sideeffect) in the documentation, which
+# will result in a user-defined paragraph with heading "Side Effects:".
+# You can put \n's in the value part of an alias to insert newlines.
+
+ALIASES                =
+
+# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C
+# sources only. Doxygen will then generate output that is more tailored for C.
+# For instance, some of the names that are used will be different. The list
+# of all members will be omitted, etc.
+
+OPTIMIZE_OUTPUT_FOR_C  = NO
+
+# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java
+# sources only. Doxygen will then generate output that is more tailored for
+# Java. For instance, namespaces will be presented as packages, qualified
+# scopes will look different, etc.
+
+OPTIMIZE_OUTPUT_JAVA   = NO
+
+# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran
+# sources only. Doxygen will then generate output that is more tailored for
+# Fortran.
+
+OPTIMIZE_FOR_FORTRAN   = NO
+
+# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL
+# sources. Doxygen will then generate output that is tailored for
+# VHDL.
+
+OPTIMIZE_OUTPUT_VHDL   = NO
+
+# Doxygen selects the parser to use depending on the extension of the files it parses.
+# With this tag you can assign which parser to use for a given extension.
+# Doxygen has a built-in mapping, but you can override or extend it using this tag.
+# The format is ext=language, where ext is a file extension, and language is one of
+# the parsers supported by doxygen: IDL, Java, Javascript, C#, C, C++, D, PHP,
+# Objective-C, Python, Fortran, VHDL, C, C++. For instance to make doxygen treat
+# .inc files as Fortran files (default is PHP), and .f files as C (default is Fortran),
+# use: inc=Fortran f=C. Note that for custom extensions you also need to set FILE_PATTERNS otherwise the files are not read by doxygen.
+
+EXTENSION_MAPPING      =
+
+# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want
+# to include (a tag file for) the STL sources as input, then you should
+# set this tag to YES in order to let doxygen match functions declarations and
+# definitions whose arguments contain STL classes (e.g. func(std::string); v.s.
+# func(std::string) {}). This also make the inheritance and collaboration
+# diagrams that involve STL classes more complete and accurate.
+
+BUILTIN_STL_SUPPORT    = NO
+
+# If you use Microsoft's C++/CLI language, you should set this option to YES to
+# enable parsing support.
+
+CPP_CLI_SUPPORT        = NO
+
+# Set the SIP_SUPPORT tag to YES if your project consists of sip sources only.
+# Doxygen will parse them like normal C++ but will assume all classes use public
+# instead of private inheritance when no explicit protection keyword is present.
+
+SIP_SUPPORT            = NO
+
+# For Microsoft's IDL there are propget and propput attributes to indicate getter
+# and setter methods for a property. Setting this option to YES (the default)
+# will make doxygen to replace the get and set methods by a property in the
+# documentation. This will only work if the methods are indeed getting or
+# setting a simple type. If this is not the case, or you want to show the
+# methods anyway, you should set this option to NO.
+
+IDL_PROPERTY_SUPPORT   = YES
+
+# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC
+# tag is set to YES, then doxygen will reuse the documentation of the first
+# member in the group (if any) for the other members of the group. By default
+# all members of a group must be documented explicitly.
+
+DISTRIBUTE_GROUP_DOC   = NO
+
+# Set the SUBGROUPING tag to YES (the default) to allow class member groups of
+# the same type (for instance a group of public functions) to be put as a
+# subgroup of that type (e.g. under the Public Functions section). Set it to
+# NO to prevent subgrouping. Alternatively, this can be done per class using
+# the \nosubgrouping command.
+
+SUBGROUPING            = YES
+
+# When TYPEDEF_HIDES_STRUCT is enabled, a typedef of a struct, union, or enum
+# is documented as struct, union, or enum with the name of the typedef. So
+# typedef struct TypeS {} TypeT, will appear in the documentation as a struct
+# with name TypeT. When disabled the typedef will appear as a member of a file,
+# namespace, or class. And the struct will be named TypeS. This can typically
+# be useful for C code in case the coding convention dictates that all compound
+# types are typedef'ed and only the typedef is referenced, never the tag name.
+
+TYPEDEF_HIDES_STRUCT   = NO
+
+# The SYMBOL_CACHE_SIZE determines the size of the internal cache use to
+# determine which symbols to keep in memory and which to flush to disk.
+# When the cache is full, less often used symbols will be written to disk.
+# For small to medium size projects (<1000 input files) the default value is
+# probably good enough. For larger projects a too small cache size can cause
+# doxygen to be busy swapping symbols to and from disk most of the time
+# causing a significant performance penality.
+# If the system has enough physical memory increasing the cache will improve the
+# performance by keeping more symbols in memory. Note that the value works on
+# a logarithmic scale so increasing the size by one will rougly double the
+# memory usage. The cache size is given by this formula:
+# 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0,
+# corresponding to a cache size of 2^16 = 65536 symbols
+
+SYMBOL_CACHE_SIZE      = 0
+
+#---------------------------------------------------------------------------
+# Build related configuration options
+#---------------------------------------------------------------------------
+
+# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in
+# documentation are documented, even if no documentation was available.
+# Private class members and static file members will be hidden unless
+# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES
+
+EXTRACT_ALL            = NO
+
+# If the EXTRACT_PRIVATE tag is set to YES all private members of a class
+# will be included in the documentation.
+
+EXTRACT_PRIVATE        = NO
+
+# If the EXTRACT_STATIC tag is set to YES all static members of a file
+# will be included in the documentation.
+
+EXTRACT_STATIC         = NO
+
+# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs)
+# defined locally in source files will be included in the documentation.
+# If set to NO only classes defined in header files are included.
+
+EXTRACT_LOCAL_CLASSES  = YES
+
+# This flag is only useful for Objective-C code. When set to YES local
+# methods, which are defined in the implementation section but not in
+# the interface are included in the documentation.
+# If set to NO (the default) only methods in the interface are included.
+
+EXTRACT_LOCAL_METHODS  = NO
+
+# If this flag is set to YES, the members of anonymous namespaces will be
+# extracted and appear in the documentation as a namespace called
+# 'anonymous_namespace{file}', where file will be replaced with the base
+# name of the file that contains the anonymous namespace. By default
+# anonymous namespace are hidden.
+
+EXTRACT_ANON_NSPACES   = NO
+
+# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all
+# undocumented members of documented classes, files or namespaces.
+# If set to NO (the default) these members will be included in the
+# various overviews, but no documentation section is generated.
+# This option has no effect if EXTRACT_ALL is enabled.
+
+HIDE_UNDOC_MEMBERS     = NO
+
+# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all
+# undocumented classes that are normally visible in the class hierarchy.
+# If set to NO (the default) these classes will be included in the various
+# overviews. This option has no effect if EXTRACT_ALL is enabled.
+
+HIDE_UNDOC_CLASSES     = NO
+
+# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all
+# friend (class|struct|union) declarations.
+# If set to NO (the default) these declarations will be included in the
+# documentation.
+
+HIDE_FRIEND_COMPOUNDS  = NO
+
+# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any
+# documentation blocks found inside the body of a function.
+# If set to NO (the default) these blocks will be appended to the
+# function's detailed documentation block.
+
+HIDE_IN_BODY_DOCS      = NO
+
+# The INTERNAL_DOCS tag determines if documentation
+# that is typed after a \internal command is included. If the tag is set
+# to NO (the default) then the documentation will be excluded.
+# Set it to YES to include the internal documentation.
+
+INTERNAL_DOCS          = NO
+
+# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate
+# file names in lower-case letters. If set to YES upper-case letters are also
+# allowed. This is useful if you have classes or files whose names only differ
+# in case and if your file system supports case sensitive file names. Windows
+# and Mac users are advised to set this option to NO.
+
+CASE_SENSE_NAMES       = NO
+
+# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen
+# will show members with their full class and namespace scopes in the
+# documentation. If set to YES the scope will be hidden.
+
+HIDE_SCOPE_NAMES       = NO
+
+# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen
+# will put a list of the files that are included by a file in the documentation
+# of that file.
+
+SHOW_INCLUDE_FILES     = YES
+
+# If the FORCE_LOCAL_INCLUDES tag is set to YES then Doxygen
+# will list include files with double quotes in the documentation
+# rather than with sharp brackets.
+
+FORCE_LOCAL_INCLUDES   = NO
+
+# If the INLINE_INFO tag is set to YES (the default) then a tag [inline]
+# is inserted in the documentation for inline members.
+
+INLINE_INFO            = YES
+
+# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen
+# will sort the (detailed) documentation of file and class members
+# alphabetically by member name. If set to NO the members will appear in
+# declaration order.
+
+SORT_MEMBER_DOCS       = YES
+
+# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the
+# brief documentation of file, namespace and class members alphabetically
+# by member name. If set to NO (the default) the members will appear in
+# declaration order.
+
+SORT_BRIEF_DOCS        = NO
+
+# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the (brief and detailed) documentation of class members so that constructors and destructors are listed first. If set to NO (the default) the constructors will appear in the respective orders defined by SORT_MEMBER_DOCS and SORT_BRIEF_DOCS. This tag will be ignored for brief docs if SORT_BRIEF_DOCS is set to NO and ignored for detailed docs if SORT_MEMBER_DOCS is set to NO.
+
+SORT_MEMBERS_CTORS_1ST = NO
+
+# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the
+# hierarchy of group names into alphabetical order. If set to NO (the default)
+# the group names will appear in their defined order.
+
+SORT_GROUP_NAMES       = NO
+
+# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be
+# sorted by fully-qualified names, including namespaces. If set to
+# NO (the default), the class list will be sorted only by class name,
+# not including the namespace part.
+# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES.
+# Note: This option applies only to the class list, not to the
+# alphabetical list.
+
+SORT_BY_SCOPE_NAME     = NO
+
+# The GENERATE_TODOLIST tag can be used to enable (YES) or
+# disable (NO) the todo list. This list is created by putting \todo
+# commands in the documentation.
+
+GENERATE_TODOLIST      = YES
+
+# The GENERATE_TESTLIST tag can be used to enable (YES) or
+# disable (NO) the test list. This list is created by putting \test
+# commands in the documentation.
+
+GENERATE_TESTLIST      = YES
+
+# The GENERATE_BUGLIST tag can be used to enable (YES) or
+# disable (NO) the bug list. This list is created by putting \bug
+# commands in the documentation.
+
+GENERATE_BUGLIST       = YES
+
+# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or
+# disable (NO) the deprecated list. This list is created by putting
+# \deprecated commands in the documentation.
+
+GENERATE_DEPRECATEDLIST= YES
+
+# The ENABLED_SECTIONS tag can be used to enable conditional
+# documentation sections, marked by \if sectionname ... \endif.
+
+ENABLED_SECTIONS       =
+
+# The MAX_INITIALIZER_LINES tag determines the maximum number of lines
+# the initial value of a variable or define consists of for it to appear in
+# the documentation. If the initializer consists of more lines than specified
+# here it will be hidden. Use a value of 0 to hide initializers completely.
+# The appearance of the initializer of individual variables and defines in the
+# documentation can be controlled using \showinitializer or \hideinitializer
+# command in the documentation regardless of this setting.
+
+MAX_INITIALIZER_LINES  = 30
+
+# Set the SHOW_USED_FILES tag to NO to disable the list of files generated
+# at the bottom of the documentation of classes and structs. If set to YES the
+# list will mention the files that were used to generate the documentation.
+
+SHOW_USED_FILES        = YES
+
+# If the sources in your project are distributed over multiple directories
+# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy
+# in the documentation. The default is NO.
+
+SHOW_DIRECTORIES       = YES
+
+# Set the SHOW_FILES tag to NO to disable the generation of the Files page.
+# This will remove the Files entry from the Quick Index and from the
+# Folder Tree View (if specified). The default is YES.
+
+SHOW_FILES             = YES
+
+# Set the SHOW_NAMESPACES tag to NO to disable the generation of the
+# Namespaces page.
+# This will remove the Namespaces entry from the Quick Index
+# and from the Folder Tree View (if specified). The default is YES.
+
+SHOW_NAMESPACES        = YES
+
+# The FILE_VERSION_FILTER tag can be used to specify a program or script that
+# doxygen should invoke to get the current version for each file (typically from
+# the version control system). Doxygen will invoke the program by executing (via
+# popen()) the command  , where  is the value of
+# the FILE_VERSION_FILTER tag, and  is the name of an input file
+# provided by doxygen. Whatever the program writes to standard output
+# is used as the file version. See the manual for examples.
+
+FILE_VERSION_FILTER    =
+
+# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed by
+# doxygen. The layout file controls the global structure of the generated output files
+# in an output format independent way. The create the layout file that represents
+# doxygen's defaults, run doxygen with the -l option. You can optionally specify a
+# file name after the option, if omitted DoxygenLayout.xml will be used as the name
+# of the layout file.
+
+LAYOUT_FILE            =
+
+#---------------------------------------------------------------------------
+# configuration options related to warning and progress messages
+#---------------------------------------------------------------------------
+
+# The QUIET tag can be used to turn on/off the messages that are generated
+# by doxygen. Possible values are YES and NO. If left blank NO is used.
+
+QUIET                  = NO
+
+# The WARNINGS tag can be used to turn on/off the warning messages that are
+# generated by doxygen. Possible values are YES and NO. If left blank
+# NO is used.
+
+WARNINGS               = YES
+
+# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings
+# for undocumented members. If EXTRACT_ALL is set to YES then this flag will
+# automatically be disabled.
+
+WARN_IF_UNDOCUMENTED   = YES
+
+# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for
+# potential errors in the documentation, such as not documenting some
+# parameters in a documented function, or documenting parameters that
+# don't exist or using markup commands wrongly.
+
+WARN_IF_DOC_ERROR      = YES
+
+# This WARN_NO_PARAMDOC option can be abled to get warnings for
+# functions that are documented, but have no documentation for their parameters
+# or return value. If set to NO (the default) doxygen will only warn about
+# wrong or incomplete parameter documentation, but not about the absence of
+# documentation.
+
+WARN_NO_PARAMDOC       = NO
+
+# The WARN_FORMAT tag determines the format of the warning messages that
+# doxygen can produce. The string should contain the $file, $line, and $text
+# tags, which will be replaced by the file and line number from which the
+# warning originated and the warning text. Optionally the format may contain
+# $version, which will be replaced by the version of the file (if it could
+# be obtained via FILE_VERSION_FILTER)
+
+WARN_FORMAT            = "$file:$line: $text"
+
+# The WARN_LOGFILE tag can be used to specify a file to which warning
+# and error messages should be written. If left blank the output is written
+# to stderr.
+
+WARN_LOGFILE           =
+
+#---------------------------------------------------------------------------
+# configuration options related to the input files
+#---------------------------------------------------------------------------
+
+# The INPUT tag can be used to specify the files and/or directories that contain
+# documented source files. You may enter file names like "myfile.cpp" or
+# directories like "/usr/src/myproject". Separate the files or directories
+# with spaces.
+
+INPUT                  =
+
+# This tag can be used to specify the character encoding of the source files
+# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is
+# also the default input encoding. Doxygen uses libiconv (or the iconv built
+# into libc) for the transcoding. See http://www.gnu.org/software/libiconv for
+# the list of possible encodings.
+
+INPUT_ENCODING         = UTF-8
+
+# If the value of the INPUT tag contains directories, you can use the
+# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
+# and *.h) to filter out the source-files in the directories. If left
+# blank the following patterns are tested:
+# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx
+# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py *.f90
+
+FILE_PATTERNS          = *.hpp *.h
+
+# The RECURSIVE tag can be used to turn specify whether or not subdirectories
+# should be searched for input files as well. Possible values are YES and NO.
+# If left blank NO is used.
+
+#RECURSIVE              = NO
+RECURSIVE              = YES
+
+# The EXCLUDE tag can be used to specify files and/or directories that should
+# excluded from the INPUT source files. This way you can easily exclude a
+# subdirectory from a directory tree whose root is specified with the INPUT tag.
+
+EXCLUDE                =
+
+# The EXCLUDE_SYMLINKS tag can be used select whether or not files or
+# directories that are symbolic links (a Unix filesystem feature) are excluded
+# from the input.
+
+EXCLUDE_SYMLINKS       = NO
+
+# If the value of the INPUT tag contains directories, you can use the
+# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
+# certain files from those directories. Note that the wildcards are matched
+# against the file with absolute path, so to exclude all test directories
+# for example use the pattern */test/*
+
+EXCLUDE_PATTERNS       =
+
+# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
+# (namespaces, classes, functions, etc.) that should be excluded from the
+# output. The symbol name can be a fully qualified name, a word, or if the
+# wildcard * is used, a substring. Examples: ANamespace, AClass,
+# AClass::ANamespace, ANamespace::*Test
+
+EXCLUDE_SYMBOLS        =
+
+# The EXAMPLE_PATH tag can be used to specify one or more files or
+# directories that contain example code fragments that are included (see
+# the \include command).
+
+EXAMPLE_PATH           =
+
+# If the value of the EXAMPLE_PATH tag contains directories, you can use the
+# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
+# and *.h) to filter out the source-files in the directories. If left
+# blank all files are included.
+
+EXAMPLE_PATTERNS       =
+
+# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be
+# searched for input files to be used with the \include or \dontinclude
+# commands irrespective of the value of the RECURSIVE tag.
+# Possible values are YES and NO. If left blank NO is used.
+
+EXAMPLE_RECURSIVE      = NO
+
+# The IMAGE_PATH tag can be used to specify one or more files or
+# directories that contain image that are included in the documentation (see
+# the \image command).
+
+IMAGE_PATH             =
+
+# The INPUT_FILTER tag can be used to specify a program that doxygen should
+# invoke to filter for each input file. Doxygen will invoke the filter program
+# by executing (via popen()) the command  , where 
+# is the value of the INPUT_FILTER tag, and  is the name of an
+# input file. Doxygen will then use the output that the filter program writes
+# to standard output.
+# If FILTER_PATTERNS is specified, this tag will be
+# ignored.
+
+INPUT_FILTER           =
+
+# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern
+# basis.
+# Doxygen will compare the file name with each pattern and apply the
+# filter if there is a match.
+# The filters are a list of the form:
+# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further
+# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER
+# is applied to all files.
+
+FILTER_PATTERNS        =
+
+# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using
+# INPUT_FILTER) will be used to filter the input files when producing source
+# files to browse (i.e. when SOURCE_BROWSER is set to YES).
+
+FILTER_SOURCE_FILES    = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to source browsing
+#---------------------------------------------------------------------------
+
+# If the SOURCE_BROWSER tag is set to YES then a list of source files will
+# be generated. Documented entities will be cross-referenced with these sources.
+# Note: To get rid of all source code in the generated output, make sure also
+# VERBATIM_HEADERS is set to NO.
+
+SOURCE_BROWSER         = NO
+
+# Setting the INLINE_SOURCES tag to YES will include the body
+# of functions and classes directly in the documentation.
+
+INLINE_SOURCES         = NO
+
+# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct
+# doxygen to hide any special comment blocks from generated source code
+# fragments. Normal C and C++ comments will always remain visible.
+
+STRIP_CODE_COMMENTS    = YES
+
+# If the REFERENCED_BY_RELATION tag is set to YES
+# then for each documented function all documented
+# functions referencing it will be listed.
+
+REFERENCED_BY_RELATION = YES
+
+# If the REFERENCES_RELATION tag is set to YES
+# then for each documented function all documented entities
+# called/used by that function will be listed.
+
+REFERENCES_RELATION    = YES
+
+# If the REFERENCES_LINK_SOURCE tag is set to YES (the default)
+# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from
+# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will
+# link to the source code.
+# Otherwise they will link to the documentation.
+
+REFERENCES_LINK_SOURCE = YES
+
+# If the USE_HTAGS tag is set to YES then the references to source code
+# will point to the HTML generated by the htags(1) tool instead of doxygen
+# built-in source browser. The htags tool is part of GNU's global source
+# tagging system (see http://www.gnu.org/software/global/global.html). You
+# will need version 4.8.6 or higher.
+
+USE_HTAGS              = NO
+
+# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen
+# will generate a verbatim copy of the header file for each class for
+# which an include is specified. Set to NO to disable this.
+
+VERBATIM_HEADERS       = YES
+
+#---------------------------------------------------------------------------
+# configuration options related to the alphabetical class index
+#---------------------------------------------------------------------------
+
+# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index
+# of all compounds will be generated. Enable this if the project
+# contains a lot of classes, structs, unions or interfaces.
+
+ALPHABETICAL_INDEX     = NO
+
+# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then
+# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns
+# in which this list will be split (can be a number in the range [1..20])
+
+COLS_IN_ALPHA_INDEX    = 5
+
+# In case all classes in a project start with a common prefix, all
+# classes will be put under the same header in the alphabetical index.
+# The IGNORE_PREFIX tag can be used to specify one or more prefixes that
+# should be ignored while generating the index headers.
+
+IGNORE_PREFIX          =
+
+#---------------------------------------------------------------------------
+# configuration options related to the HTML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_HTML tag is set to YES (the default) Doxygen will
+# generate HTML output.
+
+GENERATE_HTML          = YES
+
+# The HTML_OUTPUT tag is used to specify where the HTML docs will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `html' will be used as the default path.
+
+HTML_OUTPUT            = html
+
+# The HTML_FILE_EXTENSION tag can be used to specify the file extension for
+# each generated HTML page (for example: .htm,.php,.asp). If it is left blank
+# doxygen will generate files with .html extension.
+
+HTML_FILE_EXTENSION    = .html
+
+# The HTML_HEADER tag can be used to specify a personal HTML header for
+# each generated HTML page. If it is left blank doxygen will generate a
+# standard header.
+
+HTML_HEADER            =
+
+# The HTML_FOOTER tag can be used to specify a personal HTML footer for
+# each generated HTML page. If it is left blank doxygen will generate a
+# standard footer.
+
+HTML_FOOTER            =
+
+# The HTML_STYLESHEET tag can be used to specify a user-defined cascading
+# style sheet that is used by each HTML page. It can be used to
+# fine-tune the look of the HTML output. If the tag is left blank doxygen
+# will generate a default style sheet. Note that doxygen will try to copy
+# the style sheet file to the HTML output directory, so don't put your own
+# stylesheet in the HTML output directory as well, or it will be erased!
+
+HTML_STYLESHEET        =
+
+# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML
+# page will contain the date and time when the page was generated. Setting
+# this to NO can help when comparing the output of multiple runs.
+
+HTML_TIMESTAMP         = NO
+
+# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes,
+# files or namespaces will be aligned in HTML using tables. If set to
+# NO a bullet list will be used.
+
+HTML_ALIGN_MEMBERS     = YES
+
+# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML
+# documentation will contain sections that can be hidden and shown after the
+# page has loaded. For this to work a browser that supports
+# JavaScript and DHTML is required (for instance Mozilla 1.0+, Firefox
+# Netscape 6.0+, Internet explorer 5.0+, Konqueror, or Safari).
+
+HTML_DYNAMIC_SECTIONS  = NO
+
+# If the GENERATE_DOCSET tag is set to YES, additional index files
+# will be generated that can be used as input for Apple's Xcode 3
+# integrated development environment, introduced with OSX 10.5 (Leopard).
+# To create a documentation set, doxygen will generate a Makefile in the
+# HTML output directory. Running make will produce the docset in that
+# directory and running "make install" will install the docset in
+# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find
+# it at startup.
+# See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html for more information.
+
+GENERATE_DOCSET        = NO
+
+# When GENERATE_DOCSET tag is set to YES, this tag determines the name of the
+# feed. A documentation feed provides an umbrella under which multiple
+# documentation sets from a single provider (such as a company or product suite)
+# can be grouped.
+
+DOCSET_FEEDNAME        = "Doxygen generated docs"
+
+# When GENERATE_DOCSET tag is set to YES, this tag specifies a string that
+# should uniquely identify the documentation set bundle. This should be a
+# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen
+# will append .docset to the name.
+
+DOCSET_BUNDLE_ID       = org.doxygen.Project
+
+# If the GENERATE_HTMLHELP tag is set to YES, additional index files
+# will be generated that can be used as input for tools like the
+# Microsoft HTML help workshop to generate a compiled HTML help file (.chm)
+# of the generated HTML documentation.
+
+GENERATE_HTMLHELP      = NO
+
+# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can
+# be used to specify the file name of the resulting .chm file. You
+# can add a path in front of the file if the result should not be
+# written to the html output directory.
+
+CHM_FILE               =
+
+# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can
+# be used to specify the location (absolute path including file name) of
+# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run
+# the HTML help compiler on the generated index.hhp.
+
+HHC_LOCATION           =
+
+# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag
+# controls if a separate .chi index file is generated (YES) or that
+# it should be included in the master .chm file (NO).
+
+GENERATE_CHI           = NO
+
+# If the GENERATE_HTMLHELP tag is set to YES, the CHM_INDEX_ENCODING
+# is used to encode HtmlHelp index (hhk), content (hhc) and project file
+# content.
+
+CHM_INDEX_ENCODING     =
+
+# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag
+# controls whether a binary table of contents is generated (YES) or a
+# normal table of contents (NO) in the .chm file.
+
+BINARY_TOC             = NO
+
+# The TOC_EXPAND flag can be set to YES to add extra items for group members
+# to the contents of the HTML help documentation and to the tree view.
+
+TOC_EXPAND             = NO
+
+# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and QHP_VIRTUAL_FOLDER
+# are set, an additional index file will be generated that can be used as input for
+# Qt's qhelpgenerator to generate a Qt Compressed Help (.qch) of the generated
+# HTML documentation.
+
+GENERATE_QHP           = NO
+
+# If the QHG_LOCATION tag is specified, the QCH_FILE tag can
+# be used to specify the file name of the resulting .qch file.
+# The path specified is relative to the HTML output folder.
+
+QCH_FILE               =
+
+# The QHP_NAMESPACE tag specifies the namespace to use when generating
+# Qt Help Project output. For more information please see
+# http://doc.trolltech.com/qthelpproject.html#namespace
+
+QHP_NAMESPACE          = org.doxygen.Project
+
+# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating
+# Qt Help Project output. For more information please see
+# http://doc.trolltech.com/qthelpproject.html#virtual-folders
+
+QHP_VIRTUAL_FOLDER     = doc
+
+# If QHP_CUST_FILTER_NAME is set, it specifies the name of a custom filter to add.
+# For more information please see
+# http://doc.trolltech.com/qthelpproject.html#custom-filters
+
+QHP_CUST_FILTER_NAME   =
+
+# The QHP_CUST_FILT_ATTRS tag specifies the list of the attributes of the custom filter to add.For more information please see
+# Qt Help Project / Custom Filters.
+
+QHP_CUST_FILTER_ATTRS  =
+
+# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this project's
+# filter section matches.
+# Qt Help Project / Filter Attributes.
+
+QHP_SECT_FILTER_ATTRS  =
+
+# If the GENERATE_QHP tag is set to YES, the QHG_LOCATION tag can
+# be used to specify the location of Qt's qhelpgenerator.
+# If non-empty doxygen will try to run qhelpgenerator on the generated
+# .qhp file.
+
+QHG_LOCATION           =
+
+# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files
+#  will be generated, which together with the HTML files, form an Eclipse help
+#  plugin. To install this plugin and make it available under the help contents
+# menu in Eclipse, the contents of the directory containing the HTML and XML
+# files needs to be copied into the plugins directory of eclipse. The name of
+# the directory within the plugins directory should be the same as
+# the ECLIPSE_DOC_ID value. After copying Eclipse needs to be restarted before the help appears.
+
+GENERATE_ECLIPSEHELP   = NO
+
+# A unique identifier for the eclipse help plugin. When installing the plugin
+# the directory name containing the HTML and XML files should also have
+# this name.
+
+ECLIPSE_DOC_ID         = org.doxygen.Project
+
+# The DISABLE_INDEX tag can be used to turn on/off the condensed index at
+# top of each HTML page. The value NO (the default) enables the index and
+# the value YES disables it.
+
+DISABLE_INDEX          = NO
+
+# This tag can be used to set the number of enum values (range [1..20])
+# that doxygen will group on one line in the generated HTML documentation.
+
+ENUM_VALUES_PER_LINE   = 4
+
+# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index
+# structure should be generated to display hierarchical information.
+# If the tag value is set to YES, a side panel will be generated
+# containing a tree-like index structure (just like the one that
+# is generated for HTML Help). For this to work a browser that supports
+# JavaScript, DHTML, CSS and frames is required (i.e. any modern browser).
+# Windows users are probably better off using the HTML help feature.
+
+GENERATE_TREEVIEW      = NO
+
+# By enabling USE_INLINE_TREES, doxygen will generate the Groups, Directories,
+# and Class Hierarchy pages using a tree view instead of an ordered list.
+
+USE_INLINE_TREES       = NO
+
+# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be
+# used to set the initial width (in pixels) of the frame in which the tree
+# is shown.
+
+TREEVIEW_WIDTH         = 250
+
+# Use this tag to change the font size of Latex formulas included
+# as images in the HTML documentation. The default is 10. Note that
+# when you change the font size after a successful doxygen run you need
+# to manually remove any form_*.png images from the HTML output directory
+# to force them to be regenerated.
+
+FORMULA_FONTSIZE       = 10
+
+# When the SEARCHENGINE tag is enabled doxygen will generate a search box for the HTML output. The underlying search engine uses javascript
+# and DHTML and should work on any modern browser. Note that when using HTML help (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET) there is already a search function so this one should
+# typically be disabled. For large projects the javascript based search engine
+# can be slow, then enabling SERVER_BASED_SEARCH may provide a better solution.
+
+SEARCHENGINE           = YES
+
+# When the SERVER_BASED_SEARCH tag is enabled the search engine will be implemented using a PHP enabled web server instead of at the web client using Javascript. Doxygen will generate the search PHP script and index
+# file to put on the web server. The advantage of the server based approach is that it scales better to large projects and allows full text search. The disadvances is that it is more difficult to setup
+# and does not have live searching capabilities.
+
+SERVER_BASED_SEARCH    = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the LaTeX output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will
+# generate Latex output.
+
+GENERATE_LATEX         = YES
+
+# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `latex' will be used as the default path.
+
+LATEX_OUTPUT           = latex
+
+# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be
+# invoked. If left blank `latex' will be used as the default command name.
+# Note that when enabling USE_PDFLATEX this option is only used for
+# generating bitmaps for formulas in the HTML output, but not in the
+# Makefile that is written to the output directory.
+
+LATEX_CMD_NAME         = latex
+
+# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to
+# generate index for LaTeX. If left blank `makeindex' will be used as the
+# default command name.
+
+MAKEINDEX_CMD_NAME     = makeindex
+
+# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact
+# LaTeX documents. This may be useful for small projects and may help to
+# save some trees in general.
+
+COMPACT_LATEX          = NO
+
+# The PAPER_TYPE tag can be used to set the paper type that is used
+# by the printer. Possible values are: a4, a4wide, letter, legal and
+# executive. If left blank a4wide will be used.
+
+PAPER_TYPE             = a4wide
+
+# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX
+# packages that should be included in the LaTeX output.
+
+EXTRA_PACKAGES         =
+
+# The LATEX_HEADER tag can be used to specify a personal LaTeX header for
+# the generated latex document. The header should contain everything until
+# the first chapter. If it is left blank doxygen will generate a
+# standard header. Notice: only use this tag if you know what you are doing!
+
+LATEX_HEADER           =
+
+# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated
+# is prepared for conversion to pdf (using ps2pdf). The pdf file will
+# contain links (just like the HTML output) instead of page references
+# This makes the output suitable for online browsing using a pdf viewer.
+
+PDF_HYPERLINKS         = YES
+
+# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of
+# plain latex in the generated Makefile. Set this option to YES to get a
+# higher quality PDF documentation.
+
+USE_PDFLATEX           = YES
+
+# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode.
+# command to the generated LaTeX files. This will instruct LaTeX to keep
+# running if errors occur, instead of asking the user for help.
+# This option is also used when generating formulas in HTML.
+
+LATEX_BATCHMODE        = NO
+
+# If LATEX_HIDE_INDICES is set to YES then doxygen will not
+# include the index chapters (such as File Index, Compound Index, etc.)
+# in the output.
+
+LATEX_HIDE_INDICES     = NO
+
+# If LATEX_SOURCE_CODE is set to YES then doxygen will include source code with syntax highlighting in the LaTeX output. Note that which sources are shown also depends on other settings such as SOURCE_BROWSER.
+
+LATEX_SOURCE_CODE      = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the RTF output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output
+# The RTF output is optimized for Word 97 and may not look very pretty with
+# other RTF readers or editors.
+
+GENERATE_RTF           = NO
+
+# The RTF_OUTPUT tag is used to specify where the RTF docs will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `rtf' will be used as the default path.
+
+RTF_OUTPUT             = rtf
+
+# If the COMPACT_RTF tag is set to YES Doxygen generates more compact
+# RTF documents. This may be useful for small projects and may help to
+# save some trees in general.
+
+COMPACT_RTF            = NO
+
+# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated
+# will contain hyperlink fields. The RTF file will
+# contain links (just like the HTML output) instead of page references.
+# This makes the output suitable for online browsing using WORD or other
+# programs which support those fields.
+# Note: wordpad (write) and others do not support links.
+
+RTF_HYPERLINKS         = NO
+
+# Load stylesheet definitions from file. Syntax is similar to doxygen's
+# config file, i.e. a series of assignments. You only have to provide
+# replacements, missing definitions are set to their default value.
+
+RTF_STYLESHEET_FILE    =
+
+# Set optional variables used in the generation of an rtf document.
+# Syntax is similar to doxygen's config file.
+
+RTF_EXTENSIONS_FILE    =
+
+#---------------------------------------------------------------------------
+# configuration options related to the man page output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_MAN tag is set to YES (the default) Doxygen will
+# generate man pages
+
+GENERATE_MAN           = NO
+
+# The MAN_OUTPUT tag is used to specify where the man pages will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `man' will be used as the default path.
+
+MAN_OUTPUT             = man
+
+# The MAN_EXTENSION tag determines the extension that is added to
+# the generated man pages (default is the subroutine's section .3)
+
+MAN_EXTENSION          = .3
+
+# If the MAN_LINKS tag is set to YES and Doxygen generates man output,
+# then it will generate one additional man file for each entity
+# documented in the real man page(s). These additional files
+# only source the real man page, but without them the man command
+# would be unable to find the correct page. The default is NO.
+
+MAN_LINKS              = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the XML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_XML tag is set to YES Doxygen will
+# generate an XML file that captures the structure of
+# the code including all documentation.
+
+GENERATE_XML           = NO
+
+# The XML_OUTPUT tag is used to specify where the XML pages will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `xml' will be used as the default path.
+
+XML_OUTPUT             = xml
+
+# The XML_SCHEMA tag can be used to specify an XML schema,
+# which can be used by a validating XML parser to check the
+# syntax of the XML files.
+
+XML_SCHEMA             =
+
+# The XML_DTD tag can be used to specify an XML DTD,
+# which can be used by a validating XML parser to check the
+# syntax of the XML files.
+
+XML_DTD                =
+
+# If the XML_PROGRAMLISTING tag is set to YES Doxygen will
+# dump the program listings (including syntax highlighting
+# and cross-referencing information) to the XML output. Note that
+# enabling this will significantly increase the size of the XML output.
+
+XML_PROGRAMLISTING     = YES
+
+#---------------------------------------------------------------------------
+# configuration options for the AutoGen Definitions output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will
+# generate an AutoGen Definitions (see autogen.sf.net) file
+# that captures the structure of the code including all
+# documentation. Note that this feature is still experimental
+# and incomplete at the moment.
+
+GENERATE_AUTOGEN_DEF   = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the Perl module output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_PERLMOD tag is set to YES Doxygen will
+# generate a Perl module file that captures the structure of
+# the code including all documentation. Note that this
+# feature is still experimental and incomplete at the
+# moment.
+
+GENERATE_PERLMOD       = NO
+
+# If the PERLMOD_LATEX tag is set to YES Doxygen will generate
+# the necessary Makefile rules, Perl scripts and LaTeX code to be able
+# to generate PDF and DVI output from the Perl module output.
+
+PERLMOD_LATEX          = NO
+
+# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be
+# nicely formatted so it can be parsed by a human reader.
+# This is useful
+# if you want to understand what is going on.
+# On the other hand, if this
+# tag is set to NO the size of the Perl module output will be much smaller
+# and Perl will parse it just the same.
+
+PERLMOD_PRETTY         = YES
+
+# The names of the make variables in the generated doxyrules.make file
+# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX.
+# This is useful so different doxyrules.make files included by the same
+# Makefile don't overwrite each other's variables.
+
+PERLMOD_MAKEVAR_PREFIX =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the preprocessor
+#---------------------------------------------------------------------------
+
+# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will
+# evaluate all C-preprocessor directives found in the sources and include
+# files.
+
+ENABLE_PREPROCESSING   = YES
+
+# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro
+# names in the source code. If set to NO (the default) only conditional
+# compilation will be performed. Macro expansion can be done in a controlled
+# way by setting EXPAND_ONLY_PREDEF to YES.
+
+MACRO_EXPANSION        = NO
+
+# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES
+# then the macro expansion is limited to the macros specified with the
+# PREDEFINED and EXPAND_AS_DEFINED tags.
+
+EXPAND_ONLY_PREDEF     = NO
+
+# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files
+# in the INCLUDE_PATH (see below) will be search if a #include is found.
+
+SEARCH_INCLUDES        = YES
+
+# The INCLUDE_PATH tag can be used to specify one or more directories that
+# contain include files that are not input files but should be processed by
+# the preprocessor.
+
+INCLUDE_PATH           =
+
+# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard
+# patterns (like *.h and *.hpp) to filter out the header-files in the
+# directories. If left blank, the patterns specified with FILE_PATTERNS will
+# be used.
+
+INCLUDE_FILE_PATTERNS  =
+
+# The PREDEFINED tag can be used to specify one or more macro names that
+# are defined before the preprocessor is started (similar to the -D option of
+# gcc). The argument of the tag is a list of macros of the form: name
+# or name=definition (no spaces). If the definition and the = are
+# omitted =1 is assumed. To prevent a macro definition from being
+# undefined via #undef or recursively expanded use the := operator
+# instead of the = operator.
+
+PREDEFINED             =
+
+# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then
+# this tag can be used to specify a list of macro names that should be expanded.
+# The macro definition that is found in the sources will be used.
+# Use the PREDEFINED tag if you want to use a different macro definition.
+
+EXPAND_AS_DEFINED      =
+
+# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then
+# doxygen's preprocessor will remove all function-like macros that are alone
+# on a line, have an all uppercase name, and do not end with a semicolon. Such
+# function macros are typically used for boiler-plate code, and will confuse
+# the parser if not removed.
+
+SKIP_FUNCTION_MACROS   = YES
+
+#---------------------------------------------------------------------------
+# Configuration::additions related to external references
+#---------------------------------------------------------------------------
+
+# The TAGFILES option can be used to specify one or more tagfiles.
+# Optionally an initial location of the external documentation
+# can be added for each tagfile. The format of a tag file without
+# this location is as follows:
+#
+# TAGFILES = file1 file2 ...
+# Adding location for the tag files is done as follows:
+#
+# TAGFILES = file1=loc1 "file2 = loc2" ...
+# where "loc1" and "loc2" can be relative or absolute paths or
+# URLs. If a location is present for each tag, the installdox tool
+# does not have to be run to correct the links.
+# Note that each tag file must have a unique name
+# (where the name does NOT include the path)
+# If a tag file is not located in the directory in which doxygen
+# is run, you must also specify the path to the tagfile here.
+
+TAGFILES               =
+
+# When a file name is specified after GENERATE_TAGFILE, doxygen will create
+# a tag file that is based on the input files it reads.
+
+GENERATE_TAGFILE       =
+
+# If the ALLEXTERNALS tag is set to YES all external classes will be listed
+# in the class index. If set to NO only the inherited external classes
+# will be listed.
+
+ALLEXTERNALS           = NO
+
+# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed
+# in the modules index. If set to NO, only the current project's groups will
+# be listed.
+
+EXTERNAL_GROUPS        = YES
+
+# The PERL_PATH should be the absolute path and name of the perl script
+# interpreter (i.e. the result of `which perl').
+
+PERL_PATH              = /usr/bin/perl
+
+#---------------------------------------------------------------------------
+# Configuration options related to the dot tool
+#---------------------------------------------------------------------------
+
+# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will
+# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base
+# or super classes. Setting the tag to NO turns the diagrams off. Note that
+# this option is superseded by the HAVE_DOT option below. This is only a
+# fallback. It is recommended to install and use dot, since it yields more
+# powerful graphs.
+
+CLASS_DIAGRAMS         = YES
+
+# You can define message sequence charts within doxygen comments using the \msc
+# command. Doxygen will then run the mscgen tool (see
+# http://www.mcternan.me.uk/mscgen/) to produce the chart and insert it in the
+# documentation. The MSCGEN_PATH tag allows you to specify the directory where
+# the mscgen tool resides. If left empty the tool is assumed to be found in the
+# default search path.
+
+MSCGEN_PATH            =
+
+# If set to YES, the inheritance and collaboration graphs will hide
+# inheritance and usage relations if the target is undocumented
+# or is not a class.
+
+HIDE_UNDOC_RELATIONS   = YES
+
+# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is
+# available from the path. This tool is part of Graphviz, a graph visualization
+# toolkit from AT&T and Lucent Bell Labs. The other options in this section
+# have no effect if this option is set to NO (the default)
+
+HAVE_DOT               = NO
+
+# By default doxygen will write a font called FreeSans.ttf to the output
+# directory and reference it in all dot files that doxygen generates. This
+# font does not include all possible unicode characters however, so when you need
+# these (or just want a differently looking font) you can specify the font name
+# using DOT_FONTNAME. You need need to make sure dot is able to find the font,
+# which can be done by putting it in a standard location or by setting the
+# DOTFONTPATH environment variable or by setting DOT_FONTPATH to the directory
+# containing the font.
+
+DOT_FONTNAME           = FreeSans
+
+# The DOT_FONTSIZE tag can be used to set the size of the font of dot graphs.
+# The default size is 10pt.
+
+DOT_FONTSIZE           = 10
+
+# By default doxygen will tell dot to use the output directory to look for the
+# FreeSans.ttf font (which doxygen will put there itself). If you specify a
+# different font using DOT_FONTNAME you can set the path where dot
+# can find it using this tag.
+
+DOT_FONTPATH           =
+
+# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen
+# will generate a graph for each documented class showing the direct and
+# indirect inheritance relations. Setting this tag to YES will force the
+# the CLASS_DIAGRAMS tag to NO.
+
+CLASS_GRAPH            = YES
+
+# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen
+# will generate a graph for each documented class showing the direct and
+# indirect implementation dependencies (inheritance, containment, and
+# class references variables) of the class with other documented classes.
+
+COLLABORATION_GRAPH    = YES
+
+# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen
+# will generate a graph for groups, showing the direct groups dependencies
+
+GROUP_GRAPHS           = YES
+
+# If the UML_LOOK tag is set to YES doxygen will generate inheritance and
+# collaboration diagrams in a style similar to the OMG's Unified Modeling
+# Language.
+
+UML_LOOK               = NO
+
+# If set to YES, the inheritance and collaboration graphs will show the
+# relations between templates and their instances.
+
+TEMPLATE_RELATIONS     = NO
+
+# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT
+# tags are set to YES then doxygen will generate a graph for each documented
+# file showing the direct and indirect include dependencies of the file with
+# other documented files.
+
+INCLUDE_GRAPH          = YES
+
+# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and
+# HAVE_DOT tags are set to YES then doxygen will generate a graph for each
+# documented header file showing the documented files that directly or
+# indirectly include this file.
+
+INCLUDED_BY_GRAPH      = YES
+
+# If the CALL_GRAPH and HAVE_DOT options are set to YES then
+# doxygen will generate a call dependency graph for every global function
+# or class method. Note that enabling this option will significantly increase
+# the time of a run. So in most cases it will be better to enable call graphs
+# for selected functions only using the \callgraph command.
+
+CALL_GRAPH             = NO
+
+# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then
+# doxygen will generate a caller dependency graph for every global function
+# or class method. Note that enabling this option will significantly increase
+# the time of a run. So in most cases it will be better to enable caller
+# graphs for selected functions only using the \callergraph command.
+
+CALLER_GRAPH           = NO
+
+# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen
+# will graphical hierarchy of all classes instead of a textual one.
+
+GRAPHICAL_HIERARCHY    = YES
+
+# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES
+# then doxygen will show the dependencies a directory has on other directories
+# in a graphical way. The dependency relations are determined by the #include
+# relations between the files in the directories.
+
+DIRECTORY_GRAPH        = YES
+
+# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images
+# generated by dot. Possible values are png, jpg, or gif
+# If left blank png will be used.
+
+DOT_IMAGE_FORMAT       = png
+
+# The tag DOT_PATH can be used to specify the path where the dot tool can be
+# found. If left blank, it is assumed the dot tool can be found in the path.
+
+DOT_PATH               =
+
+# The DOTFILE_DIRS tag can be used to specify one or more directories that
+# contain dot files that are included in the documentation (see the
+# \dotfile command).
+
+DOTFILE_DIRS           =
+
+# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of
+# nodes that will be shown in the graph. If the number of nodes in a graph
+# becomes larger than this value, doxygen will truncate the graph, which is
+# visualized by representing a node as a red box. Note that doxygen if the
+# number of direct children of the root node in a graph is already larger than
+# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note
+# that the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH.
+
+DOT_GRAPH_MAX_NODES    = 50
+
+# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the
+# graphs generated by dot. A depth value of 3 means that only nodes reachable
+# from the root by following a path via at most 3 edges will be shown. Nodes
+# that lay further from the root node will be omitted. Note that setting this
+# option to 1 or 2 may greatly reduce the computation time needed for large
+# code bases. Also note that the size of a graph can be further restricted by
+# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction.
+
+MAX_DOT_GRAPH_DEPTH    = 0
+
+# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent
+# background. This is disabled by default, because dot on Windows does not
+# seem to support this out of the box. Warning: Depending on the platform used,
+# enabling this option may lead to badly anti-aliased labels on the edges of
+# a graph (i.e. they become hard to read).
+
+DOT_TRANSPARENT        = NO
+
+# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output
+# files in one run (i.e. multiple -o and -T options on the command line). This
+# makes dot run faster, but since only newer versions of dot (>1.8.10)
+# support this, this feature is disabled by default.
+
+DOT_MULTI_TARGETS      = NO
+
+# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will
+# generate a legend page explaining the meaning of the various boxes and
+# arrows in the dot generated graphs.
+
+GENERATE_LEGEND        = YES
+
+# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will
+# remove the intermediate dot files that are used to generate
+# the various graphs.
+
+DOT_CLEANUP            = YES
diff --git a/cpp/Makefile.am b/cpp/Makefile.am
index 6b37803..871ff8c 100644
--- a/cpp/Makefile.am
+++ b/cpp/Makefile.am
@@ -11,3 +11,9 @@ DOC_FILES = \
 EXTRA_DIST = \
 		$(DOC_FILES)
 
+doxygen:
+	./preprocess
+	./preprocess clean
+	cd src && $(MAKE) doxygen
+	./preprocess
+
diff --git a/cpp/preprocess b/cpp/preprocess
index 1a5e6a3..1c169e9 100755
--- a/cpp/preprocess
+++ b/cpp/preprocess
@@ -6,6 +6,7 @@ preprocess() {
 		echo ""
 		echo "** preprocess failed **"
 		echo ""
+		exit 1
 	else
 		mv $1.tmp $1
 	fi
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index 4cfa87e..6b6457e 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -73,3 +73,20 @@ EXTRA_DIST = \
 		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
+
diff --git a/cpp/src/msgpack.h b/cpp/src/msgpack.h
index 21729f4..0cd8a19 100644
--- a/cpp/src/msgpack.h
+++ b/cpp/src/msgpack.h
@@ -15,6 +15,11 @@
  *    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"
diff --git a/cpp/src/msgpack/object.h b/cpp/src/msgpack/object.h
index 71a27bb..cf0b4c1 100644
--- a/cpp/src/msgpack/object.h
+++ b/cpp/src/msgpack/object.h
@@ -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
 }
diff --git a/cpp/src/msgpack/pack.h b/cpp/src/msgpack/pack.h
index 1525e0f..1252895 100644
--- a/cpp/src/msgpack/pack.h
+++ b/cpp/src/msgpack/pack.h
@@ -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 {
@@ -74,6 +87,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
diff --git a/cpp/src/msgpack/sbuffer.h b/cpp/src/msgpack/sbuffer.h
index caff2e8..778dea7 100644
--- a/cpp/src/msgpack/sbuffer.h
+++ b/cpp/src/msgpack/sbuffer.h
@@ -21,15 +21,17 @@
 #include 
 #include 
 
-#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;
@@ -58,6 +60,10 @@ static inline void msgpack_sbuffer_free(msgpack_sbuffer* 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;
@@ -94,6 +100,9 @@ static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf)
 	sbuf->size = 0;
 }
 
+/** @} */
+
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/cpp/src/msgpack/unpack.h b/cpp/src/msgpack/unpack.h
index 1f43ab7..82698fc 100644
--- a/cpp/src/msgpack/unpack.h
+++ b/cpp/src/msgpack/unpack.h
@@ -22,19 +22,34 @@
 #include "msgpack/object.h"
 #include 
 
-#ifndef MSGPACK_UNPACKER_INIT_BUFFER_SIZE
-#define MSGPACK_UNPACKER_INIT_BUFFER_SIZE (64*1024)
-#endif
-
-#ifndef MSGPACK_UNPACKER_RESERVE_SIZE
-#define MSGPACK_UNPACKER_RESERVE_SIZE (32*1024)
-#endif
-
 #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;
@@ -46,27 +61,100 @@ typedef struct msgpack_unpacker {
 	void* ctx;
 } msgpack_unpacker;
 
-typedef struct msgpack_unpacked {
-	msgpack_zone* zone;
-	msgpack_object data;
-} msgpack_unpacked;
 
+#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);
 
 
@@ -83,10 +171,10 @@ void msgpack_unpacker_reset(msgpack_unpacker* mpac);
 static inline size_t msgpack_unpacker_message_size(const msgpack_unpacker* mpac);
 
 
-bool msgpack_unpack_next(msgpack_unpacked* result,
-		const char* data, size_t len, size_t* off);
+/** @} */
 
 
+// obsolete
 typedef enum {
 	MSGPACK_UNPACK_SUCCESS				=  2,
 	MSGPACK_UNPACK_EXTRA_BYTES			=  1,
@@ -94,6 +182,7 @@ typedef enum {
 	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);
diff --git a/cpp/src/msgpack/vrefbuffer.h b/cpp/src/msgpack/vrefbuffer.h
index ffb2302..123499d 100644
--- a/cpp/src/msgpack/vrefbuffer.h
+++ b/cpp/src/msgpack/vrefbuffer.h
@@ -30,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;
 
@@ -64,6 +62,14 @@ 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);
@@ -86,6 +92,8 @@ 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)
 {
diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h
index 0ff9675..abb9c50 100644
--- a/cpp/src/msgpack/zbuffer.h
+++ b/cpp/src/msgpack/zbuffer.h
@@ -23,25 +23,26 @@
 #include 
 #include 
 
-#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);
@@ -60,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);
@@ -191,6 +196,8 @@ char* msgpack_zbuffer_release_buffer(msgpack_zbuffer* zbuf)
 	return tmp;
 }
 
+/** @} */
+
 
 #ifdef __cplusplus
 }
diff --git a/cpp/src/msgpack/zone.h b/cpp/src/msgpack/zone.h
index ce5be6d..0e811df 100644
--- a/cpp/src/msgpack/zone.h
+++ b/cpp/src/msgpack/zone.h
@@ -25,6 +25,12 @@ 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

From fb3e11408c4c94d3584cb68b8dcbc5a6c16db88f Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 15:56:29 +0900
Subject: [PATCH 050/152] add test/cases.json

---
 test/README.md    | 51 +++++++++++++++++++++++++++++++++++++++++++++++
 test/cases.json   |  1 +
 test/cases_gen.rb |  5 +++++
 3 files changed, 57 insertions(+)
 create mode 100644 test/README.md
 create mode 100644 test/cases.json

diff --git a/test/README.md b/test/README.md
new file mode 100644
index 0000000..9139e61
--- /dev/null
+++ b/test/README.md
@@ -0,0 +1,51 @@
+MessagePack cross-language test cases
+=====================================
+
+## cases
+
+Valid serialized data are stored in "cases.mpac" and "cases_compact.mpac".
+These files describe same objects. And "cases.json" describes an array of the described objects.
+
+Thus you can verify your implementations as comparing the objects.
+
+
+## crosslang
+
+The *crosslang* tool reads serialized data from stdin and writes re-serialize data to stdout.
+
+There are C++ and Ruby implementation of crosslang tool. You can verify your implementation
+as comparing that implementations.
+
+### C++ version
+
+    $ cd ../cpp && ./configure && make && make install
+    or
+    $ port install msgpack  # MacPorts
+    
+    $ g++ -Wall -lmsgpack crosslang.cc
+
+    Usage: ./crosslang [in-file] [out-file]
+    
+    This tool is for testing of MessagePack implementation.
+    This does following behavior:
+    
+      1. Reads objects serialized by MessagePack from  (default: stdin)
+      2. Re-serializes the objects using C++ implementation of MessagePack (Note that C++ implementation is considered valid)
+      3. Writes the re-serialized objects into  (default: stdout)
+
+### Ruby version
+
+    $ gem install msgpack
+    or
+    $ port install rb_msgpack   # MacPorts
+
+    $ ruby crosslang.rb
+    Usage: crosslang.rb [in-file] [out-file]
+    
+    This tool is for testing of MessagePack implementation.
+    This does following behavior:
+    
+      1. Reads objects serialized by MessagePack from  (default: stdin)
+      2. Re-serializes the objects using Ruby implementation of MessagePack (Note that Ruby implementation is considered valid)
+      3. Writes the re-serialized objects into  (default: stdout)
+
diff --git a/test/cases.json b/test/cases.json
new file mode 100644
index 0000000..fd390d4
--- /dev/null
+++ b/test/cases.json
@@ -0,0 +1 @@
+[false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]]
\ No newline at end of file
diff --git a/test/cases_gen.rb b/test/cases_gen.rb
index 7efbfe7..b349f4e 100644
--- a/test/cases_gen.rb
+++ b/test/cases_gen.rb
@@ -3,6 +3,7 @@
 #
 require 'rubygems' rescue nil
 require 'msgpack'
+require 'json'
 
 source = <
Date: Tue, 1 Jun 2010 15:58:44 +0900
Subject: [PATCH 051/152] update test/README.md

---
 test/README.md | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)

diff --git a/test/README.md b/test/README.md
index 9139e61..8027002 100644
--- a/test/README.md
+++ b/test/README.md
@@ -22,16 +22,10 @@ as comparing that implementations.
     or
     $ port install msgpack  # MacPorts
     
-    $ g++ -Wall -lmsgpack crosslang.cc
+    $ g++ -Wall -lmsgpack crosslang.cc -o crosslang
 
+    $ ./crosslang
     Usage: ./crosslang [in-file] [out-file]
-    
-    This tool is for testing of MessagePack implementation.
-    This does following behavior:
-    
-      1. Reads objects serialized by MessagePack from  (default: stdin)
-      2. Re-serializes the objects using C++ implementation of MessagePack (Note that C++ implementation is considered valid)
-      3. Writes the re-serialized objects into  (default: stdout)
 
 ### Ruby version
 
@@ -41,11 +35,4 @@ as comparing that implementations.
 
     $ ruby crosslang.rb
     Usage: crosslang.rb [in-file] [out-file]
-    
-    This tool is for testing of MessagePack implementation.
-    This does following behavior:
-    
-      1. Reads objects serialized by MessagePack from  (default: stdin)
-      2. Re-serializes the objects using Ruby implementation of MessagePack (Note that Ruby implementation is considered valid)
-      3. Writes the re-serialized objects into  (default: stdout)
 

From d4049fe593ae4465e7a258d138c2166571a0f1a7 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 1 Jun 2010 16:35:21 +0900
Subject: [PATCH 052/152] ruby: add test/test_cases.rb

---
 ruby/makegem.sh                               |  4 +-
 ruby/test/test_cases.rb                       | 46 +++++++++++++++++++
 ruby/test/test_helper.rb                      |  4 ++
 .../test_pack_unpack.rb}                      | 25 +++++-----
 test/cases_gen.rb                             |  5 +-
 5 files changed, 70 insertions(+), 14 deletions(-)
 create mode 100644 ruby/test/test_cases.rb
 rename ruby/{msgpack_test.rb => test/test_pack_unpack.rb} (86%)

diff --git a/ruby/makegem.sh b/ruby/makegem.sh
index 5ea66f1..827f452 100755
--- a/ruby/makegem.sh
+++ b/ruby/makegem.sh
@@ -13,7 +13,9 @@ cp ../msgpack/pack_template.h   msgpack/
 cp ../msgpack/unpack_define.h   msgpack/
 cp ../msgpack/unpack_template.h msgpack/
 cp ../msgpack/sysdep.h          msgpack/
-cat msgpack_test.rb | sed "s/require ['\"]msgpack['\"]/require File.dirname(__FILE__) + '\/test_helper.rb'/" > test/msgpack_test.rb
+cp ../test/cases.mpac           test/
+cp ../test/cases_compact.mpac   test/
+cp ../test/cases.json           test/
 
 gem build msgpack.gemspec
 
diff --git a/ruby/test/test_cases.rb b/ruby/test/test_cases.rb
new file mode 100644
index 0000000..bfb752e
--- /dev/null
+++ b/ruby/test/test_cases.rb
@@ -0,0 +1,46 @@
+#!/usr/bin/env ruby
+here = File.dirname(__FILE__)
+require "#{here}/test_helper"
+
+begin
+require 'json'
+rescue LoadError
+require 'rubygems'
+require 'json'
+end
+
+CASES_PATH         = "#{here}/cases.mpac"
+CASES_COMPACT_PATH = "#{here}/cases_compact.mpac"
+CASES_JSON_PATH    = "#{here}/cases.json"
+
+class MessagePackTestCases < Test::Unit::TestCase
+	def feed_file(path)
+		pac = MessagePack::Unpacker.new
+		pac.feed File.read(path)
+		pac
+	end
+
+	def test_compare_compact
+		pac  = feed_file(CASES_PATH)
+		cpac = feed_file(CASES_COMPACT_PATH)
+
+		 objs = [];  pac.each {| obj|  objs <<  obj }
+		cobjs = []; cpac.each {|cobj| cobjs << cobj }
+
+		objs.zip(cobjs).each {|obj, cobj|
+			assert_equal(obj, cobj)
+		}
+	end
+
+	def test_compare_json
+		pac  = feed_file(CASES_PATH)
+
+		objs = []; pac.each {|obj| objs <<  obj }
+		jobjs = JSON.load File.read(CASES_JSON_PATH)
+
+		objs.zip(jobjs) {|obj, jobj|
+			assert_equal(obj, jobj)
+		}
+	end
+end
+
diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb
index 6a63489..19226ef 100644
--- a/ruby/test/test_helper.rb
+++ b/ruby/test/test_helper.rb
@@ -1,3 +1,7 @@
 require 'test/unit'
+begin
+require File.dirname(__FILE__) + '/../msgpack'
+rescue LoadError
 require File.dirname(__FILE__) + '/../lib/msgpack'
+end
 
diff --git a/ruby/msgpack_test.rb b/ruby/test/test_pack_unpack.rb
similarity index 86%
rename from ruby/msgpack_test.rb
rename to ruby/test/test_pack_unpack.rb
index 8cbb586..e22bab3 100644
--- a/ruby/msgpack_test.rb
+++ b/ruby/test/test_pack_unpack.rb
@@ -1,8 +1,7 @@
 #!/usr/bin/env ruby
-require 'msgpack'
-require 'test/unit'
+require File.dirname(__FILE__)+'/test_helper'
 
-class MessagePackTestFormat < Test::Unit::TestCase
+class MessagePackTestPackUnpack < Test::Unit::TestCase
 	def self.it(name, &block)
 		define_method("test_#{name}", &block)
 	end
@@ -177,16 +176,18 @@ class MessagePackTestFormat < Test::Unit::TestCase
 		match ({}), "\x80"
 	end
 
-	it "{0=>0, 1=>1, ..., 14=>14}" do
-		a = (0..14).to_a;
-		match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a"
-	end
-
-	it "{0=>0, 1=>1, ..., 15=>15}" do
-		a = (0..15).to_a;
-		match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a"
-	end
+## FIXME
+#	it "{0=>0, 1=>1, ..., 14=>14}" do
+#		a = (0..14).to_a;
+#		match Hash[*a.zip(a).flatten], "\x8f\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x04\x04\x0a\x0a"
+#	end
+#
+#	it "{0=>0, 1=>1, ..., 15=>15}" do
+#		a = (0..15).to_a;
+#		match Hash[*a.zip(a).flatten], "\xde\x00\x10\x05\x05\x0b\x0b\x00\x00\x06\x06\x0c\x0c\x01\x01\x07\x07\x0d\x0d\x02\x02\x08\x08\x0e\x0e\x03\x03\x09\x09\x0f\x0f\x04\x04\x0a\x0a"
+#	end
 
+## FIXME
 #	it "fixmap" do
 #		check_map 1, 0
 #		check_map 1, (1<<4)-1
diff --git a/test/cases_gen.rb b/test/cases_gen.rb
index b349f4e..95662fb 100644
--- a/test/cases_gen.rb
+++ b/test/cases_gen.rb
@@ -1,7 +1,10 @@
 #
 # MessagePack format test case
 #
-require 'rubygems' rescue nil
+begin
+require 'rubygems'
+rescue LoadError
+end
 require 'msgpack'
 require 'json'
 

From 7cd41aeb7280e5752deb5b60a140b627bd50d61e Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 3 Jun 2010 00:17:17 +0900
Subject: [PATCH 053/152] erlang: tracing crosslang.rb moving to ../test

---
 erlang/msgpack.erl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index f23ec09..ca9769e 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -337,7 +337,7 @@ basic_test()->
 port_test()->
     Tests = test_data(),
     {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
-    Port = open_port({spawn, "ruby ../crosslang.rb"}, [binary]),
+    Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
     true = port_command(Port, msgpack:pack(Tests) ),
     %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner
     receive

From 9c3ed173b1c497cfe9bdfff2e5e695e5a3377e0a Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Thu, 3 Jun 2010 21:51:40 +0900
Subject: [PATCH 054/152] ruby: fixes buffering routine

---
 ruby/unpack.c | 98 ++++++++++++++++++++-------------------------------
 1 file changed, 39 insertions(+), 59 deletions(-)

diff --git a/ruby/unpack.c b/ruby/unpack.c
index 0f7b9f0..65ae476 100644
--- a/ruby/unpack.c
+++ b/ruby/unpack.c
@@ -386,90 +386,70 @@ static VALUE MessagePack_Unpacker_stream_set(VALUE self, VALUE val)
 }
 
 
-#ifdef RUBY_VM
-#  ifndef STR_SHARED
-#    define STR_SHARED  FL_USER2
-#  endif
-#  ifndef STR_NOEMBED
-#    define STR_NOEMBED FL_USER1
-#  endif
-#  ifndef STR_ASSOC
-#    define STR_ASSOC   FL_USER3
-#  endif
-#  ifndef STR_NOCAPA_P
-#    define STR_NOCAPA_P(s) (FL_TEST(s,STR_NOEMBED) && FL_ANY(s,STR_SHARED|STR_ASSOC))
-#  endif
-#  define NEED_MORE_CAPA(s,size) (!STR_NOCAPA_P(s) && RSTRING(s)->as.heap.aux.capa < size)
-#else
-#  ifndef STR_NOCAPA
-#    ifndef STR_ASSOC
-#      define STR_ASSOC   FL_USER3
-#    endif
-#    ifndef ELTS_SHARED
-#      define ELTS_SHARED FL_USER2
-#    endif
-#    define STR_NOCAPA  (ELTS_SHARED|STR_ASSOC)
-#  endif
-#  define NEED_MORE_CAPA(s,size) (!FL_TEST(s,STR_NOCAPA) && RSTRING(s)->aux.capa < size)
-#endif
-
-static void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len)
+static void reserve_buffer(msgpack_unpack_t* mp, size_t require)
 {
 	struct unpack_buffer* buffer = &mp->user.buffer;
 
 	if(buffer->size == 0) {
-		char* tmp = ALLOC_N(char, MSGPACK_UNPACKER_BUFFER_INIT_SIZE);
+		size_t nsize = MSGPACK_UNPACKER_BUFFER_INIT_SIZE;
+		while(nsize < require) {
+			nsize *= 2;
+		}
+		char* tmp = ALLOC_N(char, nsize);
 		buffer->ptr = tmp;
-		buffer->free = MSGPACK_UNPACKER_BUFFER_INIT_SIZE;
+		buffer->free = nsize;
 		buffer->size = 0;
+		return;
+	}
 
-	} else if(buffer->size <= mp->user.offset) {
+	if(buffer->size <= mp->user.offset) {
 		/* clear buffer and rewind offset */
 		buffer->free += buffer->size;
 		buffer->size = 0;
 		mp->user.offset = 0;
 	}
 
-	if(len <= buffer->free) {
-		/* enough free space: just copy */
-		memcpy(buffer->ptr+buffer->size, ptr, len);
-		buffer->size += len;
-		buffer->free -= len;
+	if(require <= buffer->free) {
+		/* enough free space */
 		return;
 	}
 
-	size_t csize = buffer->size + buffer->free;
+	size_t nsize = (buffer->size + buffer->free) * 2;
 
 	if(mp->user.offset <= buffer->size / 2) {
-		/* parsed less than half: realloc and copy */
-		csize *= 2;
-		while(csize < buffer->size + len) {
-			csize *= 2;
+		/* parsed less than half: realloc only */
+		while(nsize < buffer->size + require) {
+			nsize *= 2;
 		}
-		char* tmp = REALLOC_N(buffer->ptr, char, csize);
-		memcpy(tmp + buffer->size, ptr, len);
+		char* tmp = REALLOC_N(buffer->ptr, char, nsize);
+		buffer->free = nsize - buffer->size;
 		buffer->ptr = tmp;
-		buffer->free = csize - buffer->size;
-		return;
-	}
 
-	size_t not_parsed = buffer->size - mp->user.offset;
-
-	if(csize < not_parsed + len) {
-		/* more buffer size */
-		csize *= 2;
-		while(csize < not_parsed + len) {
-			csize *= 2;
+	} else {
+		/* parsed more than half: realloc and move */
+		size_t not_parsed = buffer->size - mp->user.offset;
+		while(nsize < not_parsed + require) {
+			nsize *= 2;
 		}
-		char* tmp = REALLOC_N(buffer->ptr, char, csize);
+		char* tmp = REALLOC_N(buffer->ptr, char, nsize);
+		memcpy(tmp, tmp + mp->user.offset, not_parsed);
+		buffer->free = nsize - buffer->size;
+		buffer->size = not_parsed;
 		buffer->ptr = tmp;
+		mp->user.offset = 0;
 	}
+}
 
-	memcpy(buffer->ptr+not_parsed, ptr, not_parsed);
-	buffer->size = not_parsed;
-	buffer->free = csize - buffer->size;
-	buffer->ptr = buffer->ptr;
-	mp->user.offset = 0;
+static inline void feed_buffer(msgpack_unpack_t* mp, const char* ptr, size_t len)
+{
+	struct unpack_buffer* buffer = &mp->user.buffer;
+
+	if(buffer->free < len) {
+		reserve_buffer(mp, len);
+	}
+	memcpy(buffer->ptr + buffer->size, ptr, len);
+	buffer->size += len;
+	buffer->free -= len;
 }
 
 /**

From 251090406a642d968e314db3aa1d5240db00598a Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Thu, 3 Jun 2010 21:52:01 +0900
Subject: [PATCH 055/152] ruby: adds a test case for buffering

---
 ruby/test/test_pack_unpack.rb | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb
index e22bab3..9dff44f 100644
--- a/ruby/test/test_pack_unpack.rb
+++ b/ruby/test/test_pack_unpack.rb
@@ -203,6 +203,37 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase
 #		#check_map 5, (1<<32)-1  # memory error
 #	end
 
+	it "buffer" do
+		str = "a"*32*1024*4
+		raw = str.to_msgpack
+		pac = MessagePack::Unpacker.new
+
+		len = 0
+		parsed = false
+
+		n = 655
+		time = raw.size / n
+		time += 1 unless raw.size % n == 0
+		off = 0
+
+		time.times do
+			assert(!parsed)
+
+			fe = raw[off, n]
+			assert(fe.length > 0)
+			off += fe.length
+
+			pac.feed fe
+			pac.each {|obj|
+				assert(!parsed)
+				assert_equal(obj, str)
+				parsed = true
+			}
+		end
+
+		assert(parsed)
+	end
+
 	it "gc mark" do
 		obj = [{["a","b"]=>["c","d"]}, ["e","f"], "d"]
 		num = 4

From b3e0ad13030cdaee442582a8dd16c46518e9d6c9 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Thu, 3 Jun 2010 22:00:15 +0900
Subject: [PATCH 056/152] ruby: 0.4.2

---
 ruby/msgpack.gemspec | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ruby/msgpack.gemspec b/ruby/msgpack.gemspec
index e10622f..fb6338a 100644
--- a/ruby/msgpack.gemspec
+++ b/ruby/msgpack.gemspec
@@ -1,7 +1,7 @@
 Gem::Specification.new do |s|
   s.platform = Gem::Platform::RUBY
   s.name = "msgpack"
-  s.version = "0.4.1"
+  s.version = "0.4.2"
   s.summary = "MessagePack, a binary-based efficient data interchange format."
   s.author = "FURUHASHI Sadayuki"
   s.email = "frsyuki@users.sourceforge.jp"

From 82a5dd6cf9a49f6ffa444d5fc0ba5d4e3f10eb3e Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Thu, 10 Jun 2010 14:02:58 -0700
Subject: [PATCH 057/152] java: update

---
 java/src/main/java/org/msgpack/Packer.java    |   4 +
 java/src/main/java/org/msgpack/Schema.java    |  79 ++----------
 java/src/main/java/org/msgpack/Unpacker.java  |   4 +-
 .../org/msgpack/schema/BooleanSchema.java     |  64 ++++++++++
 .../{RawSchema.java => ByteArraySchema.java}  |  58 ++++-----
 .../java/org/msgpack/schema/ByteSchema.java   |  55 +++++----
 .../org/msgpack/schema/ClassGenerator.java    |  23 ++--
 .../java/org/msgpack/schema/ClassSchema.java  |  58 ++++-----
 .../java/org/msgpack/schema/DoubleSchema.java |  50 +++-----
 .../java/org/msgpack/schema/FloatSchema.java  |  50 +++-----
 .../msgpack/schema/GenericClassSchema.java    |   4 -
 .../org/msgpack/schema/GenericSchema.java     |  73 +----------
 .../java/org/msgpack/schema/IntSchema.java    |  55 +++++----
 .../{ArraySchema.java => ListSchema.java}     |  80 +++++-------
 .../java/org/msgpack/schema/LongSchema.java   |  39 +++---
 .../java/org/msgpack/schema/MapSchema.java    |  43 ++++---
 .../org/msgpack/schema/SSchemaParser.java     |  22 +++-
 .../java/org/msgpack/schema/SetSchema.java    | 115 ++++++++++++++++++
 .../java/org/msgpack/schema/ShortSchema.java  |  52 ++++----
 .../java/org/msgpack/schema/StringSchema.java |  65 +++++-----
 20 files changed, 512 insertions(+), 481 deletions(-)
 create mode 100644 java/src/main/java/org/msgpack/schema/BooleanSchema.java
 rename java/src/main/java/org/msgpack/schema/{RawSchema.java => ByteArraySchema.java} (70%)
 rename java/src/main/java/org/msgpack/schema/{ArraySchema.java => ListSchema.java} (60%)
 create mode 100644 java/src/main/java/org/msgpack/schema/SetSchema.java

diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java
index a92d2b6..dd510f3 100644
--- a/java/src/main/java/org/msgpack/Packer.java
+++ b/java/src/main/java/org/msgpack/Packer.java
@@ -223,6 +223,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;
diff --git a/java/src/main/java/org/msgpack/Schema.java b/java/src/main/java/org/msgpack/Schema.java
index f191f7a..25e10f9 100644
--- a/java/src/main/java/org/msgpack/Schema.java
+++ b/java/src/main/java/org/msgpack/Schema.java
@@ -20,28 +20,13 @@ package org.msgpack;
 import java.io.Writer;
 import java.io.IOException;
 import org.msgpack.schema.SSchemaParser;
-import org.msgpack.schema.ClassGenerator;
+//import org.msgpack.schema.ClassGenerator;
 
 public abstract class Schema {
-	private String expression;
-	private String name;
+	public Schema() { }
 
-	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 abstract String getClassName();
+	public abstract String getExpression();
 
 	public static Schema parse(String source) {
 		return SSchemaParser.parse(source);
@@ -51,83 +36,43 @@ public abstract class Schema {
 		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");
+		throw new MessageTypeException("type error");
 	}
 
 	public Object createFromByte(byte v) {
-		throw new RuntimeException("type error");
+		throw new MessageTypeException("type error");
 	}
 
 	public Object createFromShort(short v) {
-		throw new RuntimeException("type error");
+		throw new MessageTypeException("type error");
 	}
 
 	public Object createFromInt(int v) {
-		throw new RuntimeException("type error");
+		throw new MessageTypeException("type error");
 	}
 
 	public Object createFromLong(long v) {
-		throw new RuntimeException("type error");
+		throw new MessageTypeException("type error");
 	}
 
 	public Object createFromFloat(float v) {
-		throw new RuntimeException("type error");
+		throw new MessageTypeException("type error");
 	}
 
 	public Object createFromDouble(double v) {
-		throw new RuntimeException("type error");
+		throw new MessageTypeException("type error");
 	}
 
 	public Object createFromRaw(byte[] b, int offset, int length) {
-		throw new RuntimeException("type error");
+		throw new MessageTypeException("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);
-	}
-	*/
 }
 
diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java
index f22c58b..32bab64 100644
--- a/java/src/main/java/org/msgpack/Unpacker.java
+++ b/java/src/main/java/org/msgpack/Unpacker.java
@@ -561,11 +561,11 @@ public class Unpacker implements Iterable {
 		return impl.unpackObject();
 	}
 
-	final void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
+	final public void unpack(MessageUnpackable obj) throws IOException, MessageTypeException {
 		obj.messageUnpack(this);
 	}
 
-	final boolean tryUnpackNull() throws IOException {
+	final public boolean tryUnpackNull() throws IOException {
 		return impl.tryUnpackNull();
 	}
 }
diff --git a/java/src/main/java/org/msgpack/schema/BooleanSchema.java b/java/src/main/java/org/msgpack/schema/BooleanSchema.java
new file mode 100644
index 0000000..2c325f1
--- /dev/null
+++ b/java/src/main/java/org/msgpack/schema/BooleanSchema.java
@@ -0,0 +1,64 @@
+//
+// 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.schema;
+
+import java.io.IOException;
+import org.msgpack.*;
+
+public class BooleanSchema extends Schema {
+	public BooleanSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Boolean";
+	}
+
+	@Override
+	public String getExpression() {
+		return "boolean";
+	}
+
+	@Override
+	public void pack(Packer pk, Object obj) throws IOException {
+		if(obj instanceof Boolean) {
+			pk.packBoolean((Boolean)obj);
+		} else if(obj == null) {
+			pk.packNil();
+		} else {
+			throw MessageTypeException.invalidConvert(obj, this);
+		}
+	}
+
+	public static final boolean convertBoolean(Object obj) throws MessageTypeException {
+		if(obj instanceof Boolean) {
+			return (Boolean)obj;
+		}
+		throw new MessageTypeException();
+	}
+
+	@Override
+	public Object convert(Object obj) throws MessageTypeException {
+		return convertBoolean(obj);
+	}
+
+	@Override
+	public Object createFromBoolean(boolean v) {
+		return v;
+	}
+}
+
diff --git a/java/src/main/java/org/msgpack/schema/RawSchema.java b/java/src/main/java/org/msgpack/schema/ByteArraySchema.java
similarity index 70%
rename from java/src/main/java/org/msgpack/schema/RawSchema.java
rename to java/src/main/java/org/msgpack/schema/ByteArraySchema.java
index f621e4c..af9c0ed 100644
--- a/java/src/main/java/org/msgpack/schema/RawSchema.java
+++ b/java/src/main/java/org/msgpack/schema/ByteArraySchema.java
@@ -22,59 +22,48 @@ import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import org.msgpack.*;
 
-public class RawSchema extends Schema {
-	public RawSchema() {
-		super("raw");
-	}
+public class ByteArraySchema extends Schema {
+	public ByteArraySchema() { }
 
-	public String getFullName() {
+	@Override
+	public String getClassName() {
 		return "byte[]";
 	}
 
+	@Override
+	public String getExpression() {
+		return "raw";
+	}
+
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
-		// FIXME instanceof GenericObject
 		if(obj instanceof byte[]) {
-			byte[] d = (byte[])obj;
-			pk.packRaw(d.length);
-			pk.packRawBody(d);
-
-		} else if(obj instanceof ByteBuffer) {
-			ByteBuffer d = (ByteBuffer)obj;
-			if(!d.hasArray()) {
-				throw MessageTypeException.invalidConvert(obj, this);
-			}
-			pk.packRaw(d.capacity());
-			pk.packRawBody(d.array(), d.position(), d.capacity());
-
+			byte[] b = (byte[])obj;
+			pk.packRaw(b.length);
+			pk.packRawBody(b);
 		} else if(obj instanceof String) {
 			try {
-				byte[] d = ((String)obj).getBytes("UTF-8");
-				pk.packRaw(d.length);
-				pk.packRawBody(d);
+				byte[] b = ((String)obj).getBytes("UTF-8");
+				pk.packRaw(b.length);
+				pk.packRawBody(b);
 			} catch (UnsupportedEncodingException e) {
-				throw MessageTypeException.invalidConvert(obj, this);
+				throw new MessageTypeException();
 			}
-
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
-	@Override
-	public Object convert(Object obj) throws MessageTypeException {
-		// FIXME instanceof GenericObject
+	public static final byte[] convertByteArray(Object obj) throws MessageTypeException {
 		if(obj instanceof byte[]) {
 			// FIXME copy?
 			//byte[] d = (byte[])obj;
 			//byte[] v = new byte[d.length];
 			//System.arraycopy(d, 0, v, 0, d.length);
 			//return v;
-			return obj;
-
+			return (byte[])obj;
 		} else if(obj instanceof ByteBuffer) {
 			ByteBuffer d = (ByteBuffer)obj;
 			byte[] v = new byte[d.capacity()];
@@ -82,19 +71,22 @@ public class RawSchema extends Schema {
 			d.get(v);
 			d.position(pos);
 			return v;
-
 		} else if(obj instanceof String) {
 			try {
 				return ((String)obj).getBytes("UTF-8");
 			} catch (UnsupportedEncodingException e) {
-				throw MessageTypeException.invalidConvert(obj, this);
+				throw new MessageTypeException();
 			}
-
 		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
+			throw new MessageTypeException();
 		}
 	}
 
+	@Override
+	public Object convert(Object obj) throws MessageTypeException {
+		return convertByteArray(obj);
+	}
+
 	@Override
 	public Object createFromRaw(byte[] b, int offset, int length) {
 		byte[] d = new byte[length];
diff --git a/java/src/main/java/org/msgpack/schema/ByteSchema.java b/java/src/main/java/org/msgpack/schema/ByteSchema.java
index 9ee6a82..6003a0f 100644
--- a/java/src/main/java/org/msgpack/schema/ByteSchema.java
+++ b/java/src/main/java/org/msgpack/schema/ByteSchema.java
@@ -21,8 +21,11 @@ import java.io.IOException;
 import org.msgpack.*;
 
 public class ByteSchema extends Schema {
-	public ByteSchema() {
-		super("Byte");
+	public ByteSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Byte";
 	}
 
 	@Override
@@ -33,27 +36,32 @@ public class ByteSchema extends Schema {
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
 		if(obj instanceof Number) {
-			pk.packByte( ((Number)obj).byteValue() );
-
+			short value = ((Number)obj).shortValue();
+			if(value > Byte.MAX_VALUE) {
+				throw new MessageTypeException();
+			}
+			pk.packByte((byte)value);
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
+	public static final byte convertByte(Object obj) throws MessageTypeException {
+		if(obj instanceof Number) {
+			short value = ((Number)obj).shortValue();
+			if(value > Byte.MAX_VALUE) {
+				throw new MessageTypeException();
+			}
+			return (byte)value;
+		}
+		throw new MessageTypeException();
+	}
+
 	@Override
 	public Object convert(Object obj) throws MessageTypeException {
-		if(obj instanceof Byte) {
-			return obj;
-
-		} else if(obj instanceof Number) {
-			return ((Number)obj).byteValue();
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
-		}
+		return convertByte(obj);
 	}
 
 	@Override
@@ -63,26 +71,25 @@ public class ByteSchema extends Schema {
 
 	@Override
 	public Object createFromShort(short v) {
+		if(v > Byte.MAX_VALUE) {
+			throw new MessageTypeException();
+		}
 		return (byte)v;
 	}
 
 	@Override
 	public Object createFromInt(int v) {
+		if(v > Byte.MAX_VALUE) {
+			throw new MessageTypeException();
+		}
 		return (byte)v;
 	}
 
 	@Override
 	public Object createFromLong(long v) {
-		return (byte)v;
-	}
-
-	@Override
-	public Object createFromFloat(float v) {
-		return (byte)v;
-	}
-
-	@Override
-	public Object createFromDouble(double v) {
+		if(v > Byte.MAX_VALUE) {
+			throw new MessageTypeException();
+		}
 		return (byte)v;
 	}
 }
diff --git a/java/src/main/java/org/msgpack/schema/ClassGenerator.java b/java/src/main/java/org/msgpack/schema/ClassGenerator.java
index f8a13fa..a515996 100644
--- a/java/src/main/java/org/msgpack/schema/ClassGenerator.java
+++ b/java/src/main/java/org/msgpack/schema/ClassGenerator.java
@@ -77,8 +77,11 @@ public class ClassGenerator {
 			for(FieldSchema f : cs.getFields()) {
 				findSubclassSchema(dst, f.getSchema());
 			}
-		} else if(s instanceof ArraySchema) {
-			ArraySchema as = (ArraySchema)s;
+		} else if(s instanceof ListSchema) {
+			ListSchema as = (ListSchema)s;
+			findSubclassSchema(dst, as.getElementSchema(0));
+		} else if(s instanceof SetSchema) {
+			SetSchema as = (SetSchema)s;
 			findSubclassSchema(dst, as.getElementSchema(0));
 		} else if(s instanceof MapSchema) {
 			MapSchema as = (MapSchema)s;
@@ -105,7 +108,7 @@ public class ClassGenerator {
 
 	private void writeClass() throws IOException {
 		line();
-		line("public final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
+		line("public final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
 		line("{");
 		pushIndent();
 			writeSchema();
@@ -117,7 +120,7 @@ public class ClassGenerator {
 
 	private void writeSubclass() throws IOException {
 		line();
-		line("final class "+schema.getName()+" implements MessagePackable, MessageConvertable");
+		line("final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable");
 		line("{");
 		pushIndent();
 			writeSchema();
@@ -135,7 +138,7 @@ public class ClassGenerator {
 	private void writeMemberVariables() throws IOException {
 		line();
 		for(FieldSchema f : schema.getFields()) {
-			line("public "+f.getSchema().getFullName()+" "+f.getName()+";");
+			line("public "+f.getSchema().getClassName()+" "+f.getName()+";");
 		}
 	}
 
@@ -156,7 +159,7 @@ public class ClassGenerator {
 
 	private void writeConstructors() throws IOException {
 		line();
-		line("public "+schema.getName()+"() { }");
+		line("public "+schema.getClassName()+"() { }");
 	}
 
 	private void writeAccessors() throws IOException {
@@ -195,7 +198,7 @@ public class ClassGenerator {
 			line("FieldSchema[] _fields = _SCHEMA.getFields();");
 			int i = 0;
 			for(FieldSchema f : schema.getFields()) {
-				line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getFullName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
+				line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getClassName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);");
 				++i;
 			}
 		popIndent();
@@ -205,13 +208,13 @@ public class ClassGenerator {
 	private void writeFactoryFunction() throws IOException {
 		line();
 		line("@SuppressWarnings(\"unchecked\")");
-		line("public static "+schema.getName()+" createFromMessage(Object[] _message)");
+		line("public static "+schema.getClassName()+" createFromMessage(Object[] _message)");
 		line("{");
 		pushIndent();
-			line(schema.getName()+" _self = new "+schema.getName()+"();");
+			line(schema.getClassName()+" _self = new "+schema.getClassName()+"();");
 			int i = 0;
 			for(FieldSchema f : schema.getFields()) {
-				line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getFullName()+")_message["+i+"];");
+				line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getClassName()+")_message["+i+"];");
 				++i;
 			}
 			line("return _self;");
diff --git a/java/src/main/java/org/msgpack/schema/ClassSchema.java b/java/src/main/java/org/msgpack/schema/ClassSchema.java
index cd5c008..cd59755 100644
--- a/java/src/main/java/org/msgpack/schema/ClassSchema.java
+++ b/java/src/main/java/org/msgpack/schema/ClassSchema.java
@@ -22,6 +22,7 @@ import java.util.List;
 import org.msgpack.*;
 
 public abstract class ClassSchema extends Schema implements IArraySchema {
+	protected String name;
 	protected FieldSchema[] fields;
 	protected List imports;
 	protected String namespace;
@@ -30,7 +31,7 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
 	public ClassSchema(
 			String name, String namespace,
 			List imports, List fields) {
-		super(name);
+		this.name = name;
 		this.namespace = namespace;
 		this.imports = imports;  // FIXME clone?
 		this.fields = new FieldSchema[fields.size()];
@@ -42,6 +43,31 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
 		}
 	}
 
+	@Override
+	public String getClassName() {
+		return name;
+	}
+
+	@Override
+	public String getExpression() {
+		StringBuffer b = new StringBuffer();
+		b.append("(class ");
+		b.append(name);
+		if(namespace != null) {
+			b.append(" (package "+namespace+")");
+		}
+		for(FieldSchema f : fields) {
+			b.append(" "+f.getExpression());
+		}
+		b.append(")");
+		return b.toString();
+	}
+
+	public boolean equals(ClassSchema o) {
+		return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
+			name.equals(o.name);
+	}
+
 	public final FieldSchema[] getFields() {
 		return fields;
 	}
@@ -61,35 +87,5 @@ public abstract class ClassSchema extends Schema implements IArraySchema {
 	void setImports(List imports) {
 		this.imports = imports;  // FIXME clone?
 	}
-
-	//@Override
-	//public String getFullName()
-	//{
-	//	if(namespace == null) {
-	//		return getName();
-	//	} else {
-	//		return namespace+"."+getName();
-	//	}
-	//}
-
-	@Override
-	public String getExpression() {
-		StringBuffer b = new StringBuffer();
-		b.append("(class ");
-		b.append(getName());
-		if(namespace != null) {
-			b.append(" (package "+namespace+")");
-		}
-		for(FieldSchema f : fields) {
-			b.append(" "+f.getExpression());
-		}
-		b.append(")");
-		return b.toString();
-	}
-
-	public boolean equals(SpecificClassSchema o) {
-		return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
-			getName().equals(o.getName());
-	}
 }
 
diff --git a/java/src/main/java/org/msgpack/schema/DoubleSchema.java b/java/src/main/java/org/msgpack/schema/DoubleSchema.java
index d53e47d..cb857c3 100644
--- a/java/src/main/java/org/msgpack/schema/DoubleSchema.java
+++ b/java/src/main/java/org/msgpack/schema/DoubleSchema.java
@@ -21,8 +21,11 @@ import java.io.IOException;
 import org.msgpack.*;
 
 public class DoubleSchema extends Schema {
-	public DoubleSchema() {
-		super("Double");
+	public DoubleSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Double";
 	}
 
 	@Override
@@ -32,43 +35,30 @@ public class DoubleSchema extends Schema {
 
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
-		if(obj instanceof Number) {
-			pk.packDouble( ((Number)obj).doubleValue() );
-
+		if(obj instanceof Double) {
+			pk.packDouble((Double)obj);
+		} else if(obj instanceof Float) {
+			pk.packFloat((Float)obj);
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
+	public static final double convertDouble(Object obj) throws MessageTypeException {
+		if(obj instanceof Double) {
+			return (Double)obj;
+		} else if(obj instanceof Float) {
+			return ((Float)obj).doubleValue();
+		} else {
+			throw new MessageTypeException();
+		}
+	}
+
 	@Override
 	public Object convert(Object obj) throws MessageTypeException {
-		if(obj instanceof Double) {
-			return obj;
-
-		} else if(obj instanceof Number) {
-			return ((Number)obj).doubleValue();
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
-		}
-	}
-
-	@Override
-	public Object createFromByte(byte v) {
-		return (double)v;
-	}
-
-	@Override
-	public Object createFromShort(short v) {
-		return (double)v;
-	}
-
-	@Override
-	public Object createFromInt(int v) {
-		return (double)v;
+		return convertDouble(obj);
 	}
 
 	@Override
diff --git a/java/src/main/java/org/msgpack/schema/FloatSchema.java b/java/src/main/java/org/msgpack/schema/FloatSchema.java
index 2777521..cd73201 100644
--- a/java/src/main/java/org/msgpack/schema/FloatSchema.java
+++ b/java/src/main/java/org/msgpack/schema/FloatSchema.java
@@ -21,8 +21,11 @@ import java.io.IOException;
 import org.msgpack.*;
 
 public class FloatSchema extends Schema {
-	public FloatSchema() {
-		super("Float");
+	public FloatSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Float";
 	}
 
 	@Override
@@ -32,43 +35,30 @@ public class FloatSchema extends Schema {
 
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
-		if(obj instanceof Number) {
-			pk.packFloat( ((Number)obj).floatValue() );
-
+		if(obj instanceof Double) {
+			pk.packDouble((Double)obj);
+		} else if(obj instanceof Float) {
+			pk.packFloat((Float)obj);
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
+	public static final float convertFloat(Object obj) throws MessageTypeException {
+		if(obj instanceof Double) {
+			return ((Double)obj).floatValue();
+		} else if(obj instanceof Float) {
+			return (Float)obj;
+		} else {
+			throw new MessageTypeException();
+		}
+	}
+
 	@Override
 	public Object convert(Object obj) throws MessageTypeException {
-		if(obj instanceof Float) {
-			return obj;
-
-		} else if(obj instanceof Number) {
-			return ((Number)obj).floatValue();
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
-		}
-	}
-
-	@Override
-	public Object createFromByte(byte v) {
-		return (float)v;
-	}
-
-	@Override
-	public Object createFromShort(short v) {
-		return (float)v;
-	}
-
-	@Override
-	public Object createFromInt(int v) {
-		return (float)v;
+		return convertFloat(obj);
 	}
 
 	@Override
diff --git a/java/src/main/java/org/msgpack/schema/GenericClassSchema.java b/java/src/main/java/org/msgpack/schema/GenericClassSchema.java
index ffdd4ab..1ab4c33 100644
--- a/java/src/main/java/org/msgpack/schema/GenericClassSchema.java
+++ b/java/src/main/java/org/msgpack/schema/GenericClassSchema.java
@@ -41,10 +41,8 @@ public class GenericClassSchema extends ClassSchema {
 				FieldSchema f = fields[i];
 				f.getSchema().pack(pk, d.get(f.getName()));
 			}
-
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
@@ -55,7 +53,6 @@ public class GenericClassSchema extends ClassSchema {
 		if(obj instanceof Collection) {
 			// FIXME optimize
 			return createFromArray( ((Collection)obj).toArray() );
-
 		} else if(obj instanceof Map) {
 			HashMap m = new HashMap(fields.length);
 			Map d = (Map)obj;
@@ -65,7 +62,6 @@ public class GenericClassSchema extends ClassSchema {
 				m.put(fieldName, f.getSchema().convert(d.get(fieldName)));
 			}
 			return m;
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
diff --git a/java/src/main/java/org/msgpack/schema/GenericSchema.java b/java/src/main/java/org/msgpack/schema/GenericSchema.java
index 0adf898..f9098ed 100644
--- a/java/src/main/java/org/msgpack/schema/GenericSchema.java
+++ b/java/src/main/java/org/msgpack/schema/GenericSchema.java
@@ -22,11 +22,13 @@ import java.util.List;
 import java.util.HashMap;
 import java.io.IOException;
 import org.msgpack.*;
-//import org.msgpack.generic.*;
 
 public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
-	public GenericSchema() {
-		super("Object");
+	public GenericSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Object";
 	}
 
 	@Override
@@ -123,70 +125,5 @@ public class GenericSchema extends Schema implements IArraySchema, IMapSchema {
 		}
 		return m;
 	}
-
-	/*
-   @Override
-	public Object createFromNil() {
-		return null;
-	}
-
-	@Override
-	public Object createFromBoolean(boolean v) {
-		return new GenericBoolean(v);
-	}
-
-	@Override
-	public Object createFromFromByte(byte v) {
-		return new GenericByte(v);
-	}
-
-	@Override
-	public Object createFromShort(short v) {
-		return new GenericShort(v);
-	}
-
-	@Override
-	public Object createFromInt(int v) {
-		return new GenericInt(v);
-	}
-
-	@Override
-	public Object createFromLong(long v) {
-		return new GenericLong(v);
-	}
-
-	@Override
-	public Object createFromFloat(float v) {
-		return new GenericFloat(v);
-	}
-
-	@Override
-	public Object createFromDouble(double v) {
-		return new GenericDouble(v);
-	}
-
-	@Override
-	public Object createFromRaw(byte[] b, int offset, int length) {
-		return new GenericRaw(b, offset, length);
-	}
-
-	@Override
-	public Object createFromArray(Object[] obj) {
-		// FIXME GenericArray
-		return Arrays.asList(obj);
-	}
-
-	@Override
-	public Object createFromMap(Object[] obj) {
-		GenericMap m = new GenericMap(obj.length / 2);
-		int i = 0;
-		while(i < obj.length) {
-			Object k = obj[i++];
-			Object v = obj[i++];
-			m.put(k, v);
-		}
-		return m;
-	}
-	*/
 }
 
diff --git a/java/src/main/java/org/msgpack/schema/IntSchema.java b/java/src/main/java/org/msgpack/schema/IntSchema.java
index 5a7e281..269f4fb 100644
--- a/java/src/main/java/org/msgpack/schema/IntSchema.java
+++ b/java/src/main/java/org/msgpack/schema/IntSchema.java
@@ -21,8 +21,11 @@ import java.io.IOException;
 import org.msgpack.*;
 
 public class IntSchema extends Schema {
-	public IntSchema() {
-		super("Integer");
+	public IntSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Integer";
 	}
 
 	@Override
@@ -33,27 +36,38 @@ public class IntSchema extends Schema {
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
 		if(obj instanceof Number) {
-			pk.packInt( ((Number)obj).intValue() );
-
+			int value = ((Number)obj).intValue();
+			if(value >= Short.MAX_VALUE) {
+				long lvalue = ((Number)obj).longValue();
+				if(lvalue > Integer.MAX_VALUE) {
+					throw new MessageTypeException();
+				}
+			}
+			pk.packInt(value);
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
+	public static final int convertInt(Object obj) throws MessageTypeException {
+		if(obj instanceof Number) {
+			int value = ((Number)obj).intValue();
+			if(value >= Integer.MAX_VALUE) {
+				long lvalue = ((Number)obj).longValue();
+				if(lvalue > Integer.MAX_VALUE) {
+					throw new MessageTypeException();
+				}
+			}
+			return value;
+		}
+		throw new MessageTypeException();
+	}
+
 	@Override
 	public Object convert(Object obj) throws MessageTypeException {
-		if(obj instanceof Integer) {
-			return obj;
-
-		} else if(obj instanceof Number) {
-			return ((Number)obj).intValue();
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
-		}
+		return convertInt(obj);
 	}
 
 	@Override
@@ -73,16 +87,9 @@ public class IntSchema extends Schema {
 
 	@Override
 	public Object createFromLong(long v) {
-		return (int)v;
-	}
-
-	@Override
-	public Object createFromFloat(float v) {
-		return (int)v;
-	}
-
-	@Override
-	public Object createFromDouble(double v) {
+		if(v > Integer.MAX_VALUE) {
+			throw new MessageTypeException();
+		}
 		return (int)v;
 	}
 }
diff --git a/java/src/main/java/org/msgpack/schema/ArraySchema.java b/java/src/main/java/org/msgpack/schema/ListSchema.java
similarity index 60%
rename from java/src/main/java/org/msgpack/schema/ArraySchema.java
rename to java/src/main/java/org/msgpack/schema/ListSchema.java
index fd47143..bef8cc4 100644
--- a/java/src/main/java/org/msgpack/schema/ArraySchema.java
+++ b/java/src/main/java/org/msgpack/schema/ListSchema.java
@@ -22,37 +22,32 @@ import java.util.Collection;
 import java.util.Set;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.RandomAccess;
 import java.io.IOException;
 import org.msgpack.*;
 
-public class ArraySchema extends Schema implements IArraySchema {
+public class ListSchema extends Schema implements IArraySchema {
 	private Schema elementSchema;
 
-	public ArraySchema(Schema elementSchema)
-	{
-		super("array");
+	public ListSchema(Schema elementSchema) {
 		this.elementSchema = elementSchema;
 	}
 
 	@Override
-	public String getFullName()
-	{
-		return "List<"+elementSchema.getFullName()+">";
+	public String getClassName() {
+		return "List<"+elementSchema.getClassName()+">";
 	}
 
 	@Override
-	public String getExpression()
-	{
+	public String getExpression() {
 		return "(array "+elementSchema.getExpression()+")";
 	}
 
 	@Override
-	@SuppressWarnings("unchecked")
-	public void pack(Packer pk, Object obj) throws IOException
-	{
+	public void pack(Packer pk, Object obj) throws IOException {
 		if(obj instanceof List) {
-			ArrayList d = (ArrayList)obj;
+			List d = (List)obj;
 			pk.packArray(d.size());
 			if(obj instanceof RandomAccess) {
 				for(int i=0; i < d.size(); ++i) {
@@ -63,62 +58,53 @@ public class ArraySchema extends Schema implements IArraySchema {
 					elementSchema.pack(pk, e);
 				}
 			}
-
 		} else if(obj instanceof Set) {
 			Set d = (Set)obj;
 			pk.packArray(d.size());
 			for(Object e : d) {
 				elementSchema.pack(pk, e);
 			}
-
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
-	@Override
 	@SuppressWarnings("unchecked")
-	public Object convert(Object obj) throws MessageTypeException
-	{
-		if(obj instanceof List) {
-			List d = (List)obj;
-			ArrayList ar = new ArrayList(d.size());
-			if(obj instanceof RandomAccess) {
-				for(int i=0; i < d.size(); ++i) {
-					ar.add( elementSchema.convert(d.get(i)) );
-				}
-			} else {
-				for(Object e : d) {
-					ar.add( elementSchema.convert(e) );
-				}
-			}
-			return ar;
-
-		} else if(obj instanceof Collection) {
-			Collection d = (Collection)obj;
-			ArrayList ar = new ArrayList(d.size());
-			for(Object e : (Collection)obj) {
-				ar.add( elementSchema.convert(e) );
-			}
-			return ar;
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
+	public static final  List convertList(Object obj,
+			Schema elementSchema, List dest) throws MessageTypeException {
+		if(!(obj instanceof List)) {
+			throw new MessageTypeException();
 		}
+		List d = (List)obj;
+		if(dest == null) {
+			dest = new ArrayList(d.size());
+		}
+		if(obj instanceof RandomAccess) {
+			for(int i=0; i < d.size(); ++i) {
+				dest.add( (T)elementSchema.convert(d.get(i)) );
+			}
+		} else {
+			for(Object e : d) {
+				dest.add( (T)elementSchema.convert(e) );
+			}
+		}
+		return dest;
 	}
 
 	@Override
-	public Schema getElementSchema(int index)
-	{
+	public Object convert(Object obj) throws MessageTypeException {
+		return convertList(obj, elementSchema, null);
+	}
+
+	@Override
+	public Schema getElementSchema(int index) {
 		return elementSchema;
 	}
 
 	@Override
-	public Object createFromArray(Object[] obj)
-	{
+	public Object createFromArray(Object[] obj) {
 		return Arrays.asList(obj);
 	}
 }
diff --git a/java/src/main/java/org/msgpack/schema/LongSchema.java b/java/src/main/java/org/msgpack/schema/LongSchema.java
index 83a30e3..728fa21 100644
--- a/java/src/main/java/org/msgpack/schema/LongSchema.java
+++ b/java/src/main/java/org/msgpack/schema/LongSchema.java
@@ -21,8 +21,11 @@ import java.io.IOException;
 import org.msgpack.*;
 
 public class LongSchema extends Schema {
-	public LongSchema() {
-		super("Long");
+	public LongSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Long";
 	}
 
 	@Override
@@ -33,27 +36,25 @@ public class LongSchema extends Schema {
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
 		if(obj instanceof Number) {
-			pk.packLong( ((Number)obj).longValue() );
-
+			long value = ((Number)obj).longValue();
+			pk.packLong(value);
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
+	public static final long convertLong(Object obj) throws MessageTypeException {
+		if(obj instanceof Number) {
+			return ((Number)obj).longValue();
+		}
+		throw new MessageTypeException();
+	}
+
 	@Override
 	public Object convert(Object obj) throws MessageTypeException {
-		if(obj instanceof Long) {
-			return obj;
-
-		} else if(obj instanceof Number) {
-			return ((Number)obj).longValue();
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
-		}
+		return convertLong(obj);
 	}
 
 	@Override
@@ -75,15 +76,5 @@ public class LongSchema extends Schema {
 	public Object createFromLong(long v) {
 		return (long)v;
 	}
-
-	@Override
-	public Object createFromFloat(float v) {
-		return (long)v;
-	}
-
-	@Override
-	public Object createFromDouble(double v) {
-		return (long)v;
-	}
 }
 
diff --git a/java/src/main/java/org/msgpack/schema/MapSchema.java b/java/src/main/java/org/msgpack/schema/MapSchema.java
index ba75993..339a5c2 100644
--- a/java/src/main/java/org/msgpack/schema/MapSchema.java
+++ b/java/src/main/java/org/msgpack/schema/MapSchema.java
@@ -27,14 +27,13 @@ public class MapSchema extends Schema implements IMapSchema {
 	private Schema valueSchema;
 
 	public MapSchema(Schema keySchema, Schema valueSchema) {
-		super("map");
 		this.keySchema = keySchema;
 		this.valueSchema = valueSchema;
 	}
 
 	@Override
-	public String getFullName() {
-		return "HashList<"+keySchema.getFullName()+", "+valueSchema.getFullName()+">";
+	public String getClassName() {
+		return "Map<"+keySchema.getClassName()+", "+valueSchema.getClassName()+">";
 	}
 
 	@Override
@@ -52,29 +51,33 @@ public class MapSchema extends Schema implements IMapSchema {
 				keySchema.pack(pk, e.getKey());
 				valueSchema.pack(pk, e.getValue());
 			}
-
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
-	@Override
 	@SuppressWarnings("unchecked")
-	public Object convert(Object obj) throws MessageTypeException {
-		if(obj instanceof Map) {
-			Map d = (Map)obj;
-			Map m = new HashMap();
-			for(Map.Entry e : d.entrySet()) {
-				m.put(keySchema.convert(e.getKey()), valueSchema.convert(e.getValue()));
-			}
-			return m;
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
+	public static final  Map convertMap(Object obj,
+			Schema keySchema, Schema valueSchema, Map dest) throws MessageTypeException {
+		if(!(obj instanceof Map)) {
+			throw new MessageTypeException();
 		}
+		Map d = (Map)obj;
+		if(dest == null) {
+			dest = new HashMap(d.size());
+		}
+		for(Map.Entry e : d.entrySet()) {
+			dest.put((K)keySchema.convert(e.getKey()),
+					(V)valueSchema.convert(e.getValue()));
+		}
+		return (Map)d;
+	}
+
+	@Override
+	public Object convert(Object obj) throws MessageTypeException {
+		return convertMap(obj, keySchema, valueSchema, null);
 	}
 
 	@Override
@@ -90,14 +93,14 @@ public class MapSchema extends Schema implements IMapSchema {
 	@Override
 	@SuppressWarnings("unchecked")
 	public Object createFromMap(Object[] obj) {
-		HashMap m = new HashMap(obj.length / 2);
+		HashMap dest = new HashMap(obj.length / 2);
 		int i = 0;
 		while(i < obj.length) {
 			Object k = obj[i++];
 			Object v = obj[i++];
-			m.put(k, v);
+			dest.put(k, v);
 		}
-		return m;
+		return dest;
 	}
 }
 
diff --git a/java/src/main/java/org/msgpack/schema/SSchemaParser.java b/java/src/main/java/org/msgpack/schema/SSchemaParser.java
index 4ae8a4b..4345e92 100644
--- a/java/src/main/java/org/msgpack/schema/SSchemaParser.java
+++ b/java/src/main/java/org/msgpack/schema/SSchemaParser.java
@@ -140,7 +140,7 @@ public class SSchemaParser {
 			if(type.equals("string")) {
 				return new StringSchema();
 			} else if(type.equals("raw")) {
-				return new RawSchema();
+				return new ByteArraySchema();
 			} else if(type.equals("byte")) {
 				return new ByteSchema();
 			} else if(type.equals("short")) {
@@ -163,11 +163,13 @@ public class SSchemaParser {
 			if(type.equals("class")) {
 				return parseClass(exp);
 			} else if(type.equals("array")) {
-				return parseArray(exp);
+				return parseList(exp);
+			} else if(type.equals("set")) {
+				return parseSet(exp);
 			} else if(type.equals("map")) {
 				return parseMap(exp);
 			} else {
-				throw new RuntimeException("class, array or map is expected but got '"+type+"': "+exp);
+				throw new RuntimeException("class, list, set or map is expected but got '"+type+"': "+exp);
 			}
 		}
 	}
@@ -209,12 +211,20 @@ public class SSchemaParser {
 		}
 	}
 
-	private ArraySchema parseArray(SExp exp) {
+	private ListSchema parseList(SExp exp) {
 		if(exp.size() != 2) {
-			throw new RuntimeException("array is (array ELEMENT_TYPE): "+exp);
+			throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp);
 		}
 		Schema elementType = readType(exp.getTuple(1));
-		return new ArraySchema(elementType);
+		return new ListSchema(elementType);
+	}
+
+	private SetSchema parseSet(SExp exp) {
+		if(exp.size() != 2) {
+			throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp);
+		}
+		Schema elementType = readType(exp.getTuple(1));
+		return new SetSchema(elementType);
 	}
 
 	private MapSchema parseMap(SExp exp) {
diff --git a/java/src/main/java/org/msgpack/schema/SetSchema.java b/java/src/main/java/org/msgpack/schema/SetSchema.java
new file mode 100644
index 0000000..a3e1974
--- /dev/null
+++ b/java/src/main/java/org/msgpack/schema/SetSchema.java
@@ -0,0 +1,115 @@
+//
+// 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.schema;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Set;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.RandomAccess;
+import java.io.IOException;
+import org.msgpack.*;
+
+public class SetSchema extends Schema implements IArraySchema {
+	private Schema elementSchema;
+
+	public SetSchema(Schema elementSchema) {
+		this.elementSchema = elementSchema;
+	}
+
+	@Override
+	public String getClassName() {
+		return "Set<"+elementSchema.getClassName()+">";
+	}
+
+	@Override
+	public String getExpression() {
+		return "(set "+elementSchema.getExpression()+")";
+	}
+
+	@Override
+	public void pack(Packer pk, Object obj) throws IOException {
+		if(obj instanceof List) {
+			List d = (List)obj;
+			pk.packArray(d.size());
+			if(obj instanceof RandomAccess) {
+				for(int i=0; i < d.size(); ++i) {
+					elementSchema.pack(pk, d.get(i));
+				}
+			} else {
+				for(Object e : d) {
+					elementSchema.pack(pk, e);
+				}
+			}
+		} else if(obj instanceof Set) {
+			Set d = (Set)obj;
+			pk.packArray(d.size());
+			for(Object e : d) {
+				elementSchema.pack(pk, e);
+			}
+		} else if(obj == null) {
+			pk.packNil();
+		} else {
+			throw MessageTypeException.invalidConvert(obj, this);
+		}
+	}
+
+	@SuppressWarnings("unchecked")
+	public static final  Set convertSet(Object obj,
+			Schema elementSchema, Set dest) throws MessageTypeException {
+		if(!(obj instanceof List)) {
+			throw new MessageTypeException();
+		}
+		List d = (List)obj;
+		if(dest == null) {
+			dest = new HashSet(d.size());
+		}
+		if(obj instanceof RandomAccess) {
+			for(int i=0; i < d.size(); ++i) {
+				dest.add( (T)elementSchema.convert(d.get(i)) );
+			}
+		} else {
+			for(Object e : d) {
+				dest.add( (T)elementSchema.convert(e) );
+			}
+		}
+		return dest;
+	}
+
+	@Override
+	public Object convert(Object obj) throws MessageTypeException {
+		return convertSet(obj, elementSchema, null);
+	}
+
+	@Override
+	public Schema getElementSchema(int index) {
+		return elementSchema;
+	}
+
+	@Override
+	public Object createFromArray(Object[] obj) {
+		Set m = new HashSet(obj.length);
+		for(int i=0; i < obj.length; i++) {
+			m.add(obj[i]);
+		}
+		return m;
+	}
+}
+
diff --git a/java/src/main/java/org/msgpack/schema/ShortSchema.java b/java/src/main/java/org/msgpack/schema/ShortSchema.java
index f32ab41..21b9327 100644
--- a/java/src/main/java/org/msgpack/schema/ShortSchema.java
+++ b/java/src/main/java/org/msgpack/schema/ShortSchema.java
@@ -21,8 +21,11 @@ import java.io.IOException;
 import org.msgpack.*;
 
 public class ShortSchema extends Schema {
-	public ShortSchema() {
-		super("Short");
+	public ShortSchema() { }
+
+	@Override
+	public String getClassName() {
+		return "Short";
 	}
 
 	@Override
@@ -33,27 +36,32 @@ public class ShortSchema extends Schema {
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
 		if(obj instanceof Number) {
-			pk.packShort( ((Number)obj).shortValue() );
-
+			int value = ((Number)obj).intValue();
+			if(value > Short.MAX_VALUE) {
+				throw new MessageTypeException();
+			}
+			pk.packShort((short)value);
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
+	public static final short convertShort(Object obj) throws MessageTypeException {
+		if(obj instanceof Number) {
+			int value = ((Number)obj).intValue();
+			if(value > Short.MAX_VALUE) {
+				throw new MessageTypeException();
+			}
+			return (short)value;
+		}
+		throw new MessageTypeException();
+	}
+
 	@Override
 	public Object convert(Object obj) throws MessageTypeException {
-		if(obj instanceof Short) {
-			return obj;
-
-		} else if(obj instanceof Number) {
-			return ((Number)obj).shortValue();
-
-		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
-		}
+		return convertShort(obj);
 	}
 
 	@Override
@@ -68,21 +76,17 @@ public class ShortSchema extends Schema {
 
 	@Override
 	public Object createFromInt(int v) {
+		if(v > Short.MAX_VALUE) {
+			throw new MessageTypeException();
+		}
 		return (short)v;
 	}
 
 	@Override
 	public Object createFromLong(long v) {
-		return (short)v;
-	}
-
-	@Override
-	public Object createFromFloat(float v) {
-		return (short)v;
-	}
-
-	@Override
-	public Object createFromDouble(double v) {
+		if(v > Short.MAX_VALUE) {
+			throw new MessageTypeException();
+		}
 		return (short)v;
 	}
 }
diff --git a/java/src/main/java/org/msgpack/schema/StringSchema.java b/java/src/main/java/org/msgpack/schema/StringSchema.java
index 46d515b..23e4e64 100644
--- a/java/src/main/java/org/msgpack/schema/StringSchema.java
+++ b/java/src/main/java/org/msgpack/schema/StringSchema.java
@@ -23,61 +23,48 @@ import java.io.UnsupportedEncodingException;
 import org.msgpack.*;
 
 public class StringSchema extends Schema {
-	public StringSchema() {
-		super("string");
-	}
+	public StringSchema() { }
 
 	@Override
-	public String getFullName() {
+	public String getClassName() {
 		return "String";
 	}
 
+	@Override
+	public String getExpression() {
+		return "string";
+	}
+
 	@Override
 	public void pack(Packer pk, Object obj) throws IOException {
-		// FIXME instanceof GenericObject
-		if(obj instanceof String) {
+		if(obj instanceof byte[]) {
+			byte[] b = (byte[])obj;
+			pk.packRaw(b.length);
+			pk.packRawBody(b);
+		} else if(obj instanceof String) {
 			try {
-				byte[] d = ((String)obj).getBytes("UTF-8");
-				pk.packRaw(d.length);
-				pk.packRawBody(d);
+				byte[] b = ((String)obj).getBytes("UTF-8");
+				pk.packRaw(b.length);
+				pk.packRawBody(b);
 			} catch (UnsupportedEncodingException e) {
-				throw MessageTypeException.invalidConvert(obj, this);
+				throw new MessageTypeException();
 			}
-
-		} else if(obj instanceof byte[]) {
-			byte[] d = (byte[])obj;
-			pk.packRaw(d.length);
-			pk.packRawBody(d);
-
-		} else if(obj instanceof ByteBuffer) {
-			ByteBuffer d = (ByteBuffer)obj;
-			if(!d.hasArray()) {
-				throw MessageTypeException.invalidConvert(obj, this);
-			}
-			pk.packRaw(d.capacity());
-			pk.packRawBody(d.array(), d.position(), d.capacity());
-
 		} else if(obj == null) {
 			pk.packNil();
-
 		} else {
 			throw MessageTypeException.invalidConvert(obj, this);
 		}
 	}
 
-	@Override
-	public Object convert(Object obj) throws MessageTypeException {
-		// FIXME instanceof GenericObject
-		if(obj instanceof String) {
-			return obj;
-
-		} else if(obj instanceof byte[]) {
+	public static final String convertString(Object obj) throws MessageTypeException {
+		if(obj instanceof byte[]) {
 			try {
 				return new String((byte[])obj, "UTF-8");
 			} catch (UnsupportedEncodingException e) {
-				throw MessageTypeException.invalidConvert(obj, this);
+				throw new MessageTypeException();
 			}
-
+		} else if(obj instanceof String) {
+			return (String)obj;
 		} else if(obj instanceof ByteBuffer) {
 			ByteBuffer d = (ByteBuffer)obj;
 			try {
@@ -91,14 +78,18 @@ public class StringSchema extends Schema {
 					return new String(v, "UTF-8");
 				}
 			} catch (UnsupportedEncodingException e) {
-				throw MessageTypeException.invalidConvert(obj, this);
+				throw new MessageTypeException();
 			}
-
 		} else {
-			throw MessageTypeException.invalidConvert(obj, this);
+			throw new MessageTypeException();
 		}
 	}
 
+	@Override
+	public Object convert(Object obj) throws MessageTypeException {
+		return convertString(obj);
+	}
+
 	@Override
 	public Object createFromRaw(byte[] b, int offset, int length) {
 		try {

From 59603b902adf2abe0d274f41520569fde387a841 Mon Sep 17 00:00:00 2001
From: INADA Naoki 
Date: Tue, 15 Jun 2010 17:51:24 +0900
Subject: [PATCH 058/152] Python: add "load(s)/dump(s)" alias for compatibility
 to simplejson/marshal/pickle.

---
 python/msgpack/__init__.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/python/msgpack/__init__.py b/python/msgpack/__init__.py
index 797b29c..26bd2dd 100644
--- a/python/msgpack/__init__.py
+++ b/python/msgpack/__init__.py
@@ -1,3 +1,10 @@
 # coding: utf-8
 from _msgpack import *
 
+# alias for compatibility to simplejson/marshal/pickle.
+load = unpack
+loads = unpackb
+
+dump = pack
+dumps = packb
+

From f222f5ed9b49e9b9f4d31693969c4260ad97cef0 Mon Sep 17 00:00:00 2001
From: Naoki INADA 
Date: Tue, 15 Jun 2010 18:06:58 +0900
Subject: [PATCH 059/152] Python: 0.1.4

---
 python/setup.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/python/setup.py b/python/setup.py
index 8d8a6f4..64e71ed 100755
--- a/python/setup.py
+++ b/python/setup.py
@@ -14,7 +14,7 @@ except ImportError:
     from distutils.command.build_ext import build_ext
     have_cython = False
 
-version = '0.1.3'
+version = '0.1.4'
 
 # take care of extension modules.
 if have_cython:

From fd8069342052142449d4703f8a12014cf283fedf Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Tue, 22 Jun 2010 11:15:18 +0900
Subject: [PATCH 060/152] erlang: tests improved and code refined.

---
 erlang/OMakefile             |   2 +-
 erlang/msgpack.erl           | 116 ++++++++++++++++++-----------------
 erlang/testcase_generator.rb |  10 +--
 3 files changed, 68 insertions(+), 60 deletions(-)

diff --git a/erlang/OMakefile b/erlang/OMakefile
index ee72f78..34c590f 100644
--- a/erlang/OMakefile
+++ b/erlang/OMakefile
@@ -36,7 +36,7 @@ msgpack.beam: msgpack.erl
 	erlc $<
 
 test: msgpack.beam
-	erl -s msgpack test -s init stop
+	erl -noshell -s msgpack test -s init stop
 
 clean:
 	-rm *.beam
diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index ca9769e..5a468a1 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -31,10 +31,52 @@
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
-
-
 -type reason() ::  enomem.
 
+% ===== external APIs ===== %
+pack(O) when is_integer(O) andalso O < 0 ->
+    pack_int_(O);
+pack(O) when is_integer(O) ->
+    pack_uint_(O);
+pack(O) when is_float(O)->
+    pack_double(O);
+pack(nil) ->
+    pack_nil();
+pack(Bool) when is_atom(Bool) ->
+    pack_bool(Bool);
+pack(Bin) when is_binary(Bin)->
+    pack_raw(Bin);
+pack(List)  when is_list(List)->
+    pack_array(List);
+pack({dict, Map})->
+    pack_map({dict, Map});
+pack(_) ->
+    undefined.
+
+% unpacking.
+% if failed in decoding and not end, get more data
+% and feed more Bin into this function.
+% TODO: error case for imcomplete format when short for any type formats.
+-spec unpack( binary() )-> {term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
+unpack(Bin) when not is_binary(Bin)->
+    {error, badard};
+unpack(Bin) when bit_size(Bin) >= 8 ->
+    << Flag:8/unsigned-integer, Payload/binary >> = Bin,
+    unpack_(Flag, Payload);
+unpack(_)-> % when bit_size(Bin) < 8 ->
+    {more, 8}.
+
+unpack_all(Data)->
+    case unpack(Data) of
+	{ Term, Binary } when bit_size(Binary) =:= 0 ->
+	    [Term];
+	{ Term, Binary } when is_binary(Binary) ->
+	    [Term|unpack_all(Binary)]
+    end.
+
+
+% ===== internal APIs ===== %
+
 % positive fixnum
 pack_uint_(N) when is_integer( N ) , N < 128 ->  
     << 2#0:1, N:7 >>;
@@ -113,7 +155,7 @@ pack_array(L) when is_list(L)->
     end.
 pack_array_([])-> <<>>;
 pack_array_([Head|Tail])->
-    << (pack_object(Head))/binary, (pack_array_(Tail))/binary >>.
+    << (pack(Head))/binary, (pack_array_(Tail))/binary >>.
 
 unpack_array_(<<>>, 0)-> [];
 unpack_array_(Remain, 0) when is_binary(Remain)-> [Remain];
@@ -134,7 +176,7 @@ pack_map({dict,M})->
 
 pack_map_([])-> <<>>;
 pack_map_([{Key,Value}|Tail]) ->
-    << (pack_object(Key)),(pack_object(Value)),(pack_map_(Tail)) >>.
+    << (pack(Key)),(pack(Value)),(pack_map_(Tail)) >>.
 
 unpack_map_(<<>>, 0)-> [];
 unpack_map_(Bin,  0) when is_binary(Bin)-> [Bin];
@@ -143,38 +185,7 @@ unpack_map_(Bin, Len) when is_binary(Bin) and is_integer(Len) ->
     { Value, Rest2 } = unpack(Rest),
     [{Key,Value}|unpack_map_(Rest2,Len-1)].
 
-pack_object(O) when is_integer(O) andalso O < 0 ->
-    pack_int_(O);
-pack_object(O) when is_integer(O) ->
-    pack_uint_(O);
-pack_object(O) when is_float(O)->
-    pack_double(O);
-pack_object(nil) ->
-    pack_nil();
-pack_object(Bool) when is_atom(Bool) ->
-    pack_bool(Bool);
-pack_object(Bin) when is_binary(Bin)->
-    pack_raw(Bin);
-pack_object(List)  when is_list(List)->
-    pack_array(List);
-pack_object({dict, Map})->
-    pack_map({dict, Map});
-pack_object(_) ->
-    undefined.
-
-pack(Obj)->
-    pack_object(Obj).
-
-
-% unpacking.
-% if failed in decoding and not end, get more data 
-% and feed more Bin into this function.
-% TODO: error case for imcomplete format when short for any type formats.
--spec unpack( binary() )-> {term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
-unpack(Bin) when not is_binary(Bin)->
-    {error, badard};
-unpack(Bin) when bit_size(Bin) >= 8 ->
-    << Flag:8/unsigned-integer, Payload/binary >> = Bin,
+unpack_(Flag, Payload)->
     case Flag of 
 	16#C0 ->
 	    {nil, Payload};
@@ -305,20 +316,17 @@ unpack(Bin) when bit_size(Bin) >= 8 ->
 	_Other ->
 	    erlang:display(_Other),
 	    {error, no_code_matches}
-    end;
-unpack(_)-> % when bit_size(Bin) < 8 ->
-    {more, 8}.
-    
-unpack_all(Data)->
-    case unpack(Data) of
-	{ Term, Binary } when bit_size(Binary) =:= 0 -> 
-	    [Term];
-	{ Term, Binary } when is_binary(Binary) ->
-	    [Term|unpack_all(Binary)]
     end.
 
 -ifdef(EUNIT).
 
+compare_all([], [])-> ok;
+compare_all([], R)-> {toomuchrhs, R};
+compare_all(L, [])-> {toomuchlhs, L};
+compare_all([LH|LTL], [RH|RTL]) ->
+    LH=RH,
+    compare_all(LTL, RTL).
+
 test_data()->
     [0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF,
      -1, -23, -512, -1230, -567898, -16#FFFFFFFFFF,
@@ -339,7 +347,6 @@ port_test()->
     {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
     Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
     true = port_command(Port, msgpack:pack(Tests) ),
-    %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner
     receive
 	{Port, {data, Data}}->  {Tests, <<>>}=msgpack:unpack(Data)
     after 1024-> ?assert(false)   end,
@@ -348,24 +355,23 @@ port_test()->
 unknown_test()->
     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]]
+	     <<"hogehoge">>, <<"243546rf7g68h798j">>,
+%	     123.123,  %FIXME
+%            -234.4355, 1.0e-34, 1.0e64, % FIXME
+	     [23, 234, 0.23],
+	     [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
+	     42
 	    ],
     Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
-    %Port ! {self, {command, msgpack:pack(Tests)}}, ... not owner
     receive
 	{Port, {data, Data}}->
-	    Tests=msgpack:unpack_all(Data)
-%	    io:format("~p~n", [Tests])
+	    compare_all(Tests, msgpack:unpack_all(Data))
     after 1024-> ?assert(false)   end,
     port_close(Port).
 
 test_([]) -> 0;
 test_([S|Rest])->
     Pack = msgpack:pack(S),
-%    io:format("testing: ~p => ~p~n", [S, Pack]),
     {S, <<>>} = msgpack:unpack( Pack ),
     1+test_(Rest).
 
diff --git a/erlang/testcase_generator.rb b/erlang/testcase_generator.rb
index a173790..a7c76c5 100644
--- a/erlang/testcase_generator.rb
+++ b/erlang/testcase_generator.rb
@@ -39,10 +39,12 @@ 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]]
+        "hogehoge", "243546rf7g68h798j",
+#        123.123 , #FIXME
+#       -234.4355, 1.0e-34, 1.0e64,
+        [23, 234, 0.23],
+        [0,42,"sum", [1,2]], [1,42, nil, [3]],
+        42
        ]
 begin
   objs.each do |obj|

From b1e66256ce35ea4a953d255a2a98e9c8ddbe6401 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Tue, 22 Jun 2010 11:28:36 +0900
Subject: [PATCH 061/152] erlang: external APIs' type/specs.

---
 erlang/msgpack.erl | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 5a468a1..f2380fc 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -18,37 +18,38 @@
 -module(msgpack).
 -author('kuenishi+msgpack@gmail.com').
 
-%% tuples, atoms are not supported.  lists, integers, double, and so on.
+%% tuples, atoms are not supported. lists, integers, double, and so on.
 %% see http://msgpack.sourceforge.jp/spec for
 %% supported formats. APIs are almost compatible
 %% for C API (http://msgpack.sourceforge.jp/c:doc)
 %% except buffering functions (both copying and zero-copying).
--export([pack/1, unpack/1, unpack_all/1, test/0]).
-
--include_lib("eunit/include/eunit.hrl").
+-export([pack/1, unpack/1, unpack_all/1]).
 
 % compile:
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
--type reason() ::  enomem.
+-type reason() ::  enomem | badarg.
+-type map() :: any(). % there's no 'dict' type...
+-type msgpack_term() :: [msgpack_term()] | integer() | float() | {dict, map()}.
 
 % ===== external APIs ===== %
+-spec pack(Term::msgpack_term()) -> binary().
 pack(O) when is_integer(O) andalso O < 0 ->
     pack_int_(O);
 pack(O) when is_integer(O) ->
     pack_uint_(O);
-pack(O) when is_float(O)->
+pack(O) when is_float(O) ->
     pack_double(O);
 pack(nil) ->
     pack_nil();
 pack(Bool) when is_atom(Bool) ->
     pack_bool(Bool);
-pack(Bin) when is_binary(Bin)->
+pack(Bin) when is_binary(Bin) ->
     pack_raw(Bin);
-pack(List)  when is_list(List)->
+pack(List)  when is_list(List) ->
     pack_array(List);
-pack({dict, Map})->
+pack({dict, Map}) ->
     pack_map({dict, Map});
 pack(_) ->
     undefined.
@@ -57,15 +58,16 @@ pack(_) ->
 % if failed in decoding and not end, get more data
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
--spec unpack( binary() )-> {term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
+-spec unpack( binary() )-> {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
 unpack(Bin) when not is_binary(Bin)->
-    {error, badard};
+    {error, badarg};
 unpack(Bin) when bit_size(Bin) >= 8 ->
     << Flag:8/unsigned-integer, Payload/binary >> = Bin,
     unpack_(Flag, Payload);
 unpack(_)-> % when bit_size(Bin) < 8 ->
     {more, 8}.
 
+-spec unpack_all( binary() ) -> [msgpack_term()].
 unpack_all(Data)->
     case unpack(Data) of
 	{ Term, Binary } when bit_size(Binary) =:= 0 ->
@@ -318,6 +320,8 @@ unpack_(Flag, Payload)->
 	    {error, no_code_matches}
     end.
 
+% ===== test codes ===== %
+-include_lib("eunit/include/eunit.hrl").
 -ifdef(EUNIT).
 
 compare_all([], [])-> ok;

From 230ee3a03b5148d11c614fc18c7c7603c9a8af14 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Wed, 23 Jun 2010 01:26:10 +0900
Subject: [PATCH 062/152] erlang: too short binary to decode causes error
 {more, Int}.

---
 erlang/msgpack.erl | 211 +++++++++++++++++++++++++++------------------
 1 file changed, 125 insertions(+), 86 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index f2380fc..d2f7068 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -29,7 +29,7 @@
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
--type reason() ::  enomem | badarg.
+-type reason() ::  enomem | badarg | no_code_matches.
 -type map() :: any(). % there's no 'dict' type...
 -type msgpack_term() :: [msgpack_term()] | integer() | float() | {dict, map()}.
 
@@ -58,14 +58,15 @@ pack(_) ->
 % if failed in decoding and not end, get more data
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
--spec unpack( binary() )-> {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
+-spec unpack( binary() )-> 
+    {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
 unpack(Bin) when not is_binary(Bin)->
     {error, badarg};
 unpack(Bin) when bit_size(Bin) >= 8 ->
     << Flag:8/unsigned-integer, Payload/binary >> = Bin,
     unpack_(Flag, Payload);
-unpack(_)-> % when bit_size(Bin) < 8 ->
-    {more, 8}.
+unpack(<<>>)-> % when bit_size(Bin) < 8 ->
+    {more, 1}.
 
 -spec unpack_all( binary() ) -> [msgpack_term()].
 unpack_all(Data)->
@@ -129,12 +130,9 @@ pack_bool(false)->   << 16#C2:8 >>.
 pack_double(F) when is_float(F)->
     << 16#CB:8, F:64/big-float-unit:1 >>.
 
-power(N,0) when is_integer(N) -> 1;
-power(N,D) when is_integer(N) and is_integer(D) -> N * power(N, D-1).
-
 % raw bytes
 pack_raw(Bin) when is_binary(Bin)->
-    MaxLen = power(2,16),
+    MaxLen = 16#10000, % 65536
     case byte_size(Bin) of
 	Len when Len < 6->
 	    << 2#101:3, Len:5, Bin/binary >>;
@@ -146,7 +144,7 @@ pack_raw(Bin) when is_binary(Bin)->
 
 % list / tuple
 pack_array(L) when is_list(L)->
-    MaxLen = power(2,16),
+    MaxLen = 16#10000, %65536
     case length(L) of
  	Len when Len < 16 ->
  	    << 2#1001:4, Len:4/integer-unit:1, (pack_array_(L))/binary >>;
@@ -159,14 +157,19 @@ pack_array_([])-> <<>>;
 pack_array_([Head|Tail])->
     << (pack(Head))/binary, (pack_array_(Tail))/binary >>.
 
-unpack_array_(<<>>, 0)-> [];
-unpack_array_(Remain, 0) when is_binary(Remain)-> [Remain];
-unpack_array_(Bin, RestLen) when is_binary(Bin)->
-    {Term, Rest} = unpack(Bin),
-    [Term|unpack_array_(Rest, RestLen-1)].
+% FIXME! this should be tail-recursive and without lists:reverse/1
+unpack_array_(<<>>, 0, RetList)   -> {lists:reverse(RetList), <<>>};
+unpack_array_(Remain, 0, RetList) when is_binary(Remain)-> {lists:reverse(RetList), Remain};
+unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, RestLen};
+unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
+    case unpack(Bin) of
+	{more, Len} -> {more, Len+RestLen-1};
+	{Term, Rest}->
+	    unpack_array_(Rest, RestLen-1, [Term|RetList])
+    end.
     
 pack_map({dict,M})->
-    MaxLen = power(2,16),
+    MaxLen = 16#10000, %65536
     case dict:size(M) of
 	Len when Len < 16 ->
  	    << 2#1001:4, Len:4/integer-unit:1, (pack_map_(dict:to_list(M))) >>;
@@ -180,14 +183,25 @@ pack_map_([])-> <<>>;
 pack_map_([{Key,Value}|Tail]) ->
     << (pack(Key)),(pack(Value)),(pack_map_(Tail)) >>.
 
-unpack_map_(<<>>, 0)-> [];
-unpack_map_(Bin,  0) when is_binary(Bin)-> [Bin];
-unpack_map_(Bin, Len) when is_binary(Bin) and is_integer(Len) ->
-    { Key, Rest } = unpack(Bin),
-    { Value, Rest2 } = unpack(Rest),
-    [{Key,Value}|unpack_map_(Rest2,Len-1)].
+-spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
+    {more, non_neg_integer()} | { any(), binary()}.
+unpack_map_(Bin,  0,  TmpMap) when is_binary(Bin) -> { dict:from_list(TmpMap), Bin};
+unpack_map_(Bin, Len, TmpMap) when is_binary(Bin) and is_integer(Len) ->
+    case unpack(Bin) of
+	{ more, MoreLen } -> { more, MoreLen+Len-1 };
+	{ Key, Rest } ->
+	    case unpack(Rest) of
+		{more, MoreLen} -> { more, MoreLen+Len-1 };
+		{ Value, Rest2 }->
+		    unpack_map_(Rest2,Len-1,[{Key,Value}|TmpMap])
+	    end
+    end.
 
+% {more, 
+-spec unpack_(Flag::integer(), Payload::binary())->
+    {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
 unpack_(Flag, Payload)->
+    PayloadLen = byte_size(Payload),
     case Flag of 
 	16#C0 ->
 	    {nil, Payload};
@@ -196,86 +210,101 @@ unpack_(Flag, Payload)->
 	16#C3 ->
 	    {true, Payload};
 
-	16#CA -> % 32bit float
+	16#CA when PayloadLen >= 4 -> % 32bit float
 	    << Return:32/float-unit:1, Rest/binary >> = Payload,
 	    {Return, Rest};
-	16#CB -> % 64bit float
+	16#CA ->
+	    {more, 4-PayloadLen}; % at least more
+
+	16#CB when PayloadLen >= 8 -> % 64bit float
 	    << Return:64/float-unit:1, Rest/binary >> = Payload,
 	    {Return, Rest};
+	16#CB ->
+	    {more, 8-PayloadLen};
 
-	16#CC -> % uint 8
+	16#CC when PayloadLen >= 1 -> % uint 8
 	    << Int:8/unsigned-integer, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#CD -> % uint 16
+	16#CC ->
+	    {more, 1};
+
+	16#CD when PayloadLen >= 2 -> % uint 16
 	    << Int:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#CE ->
+	16#CD ->
+	    {more, 2-PayloadLen};
+
+	16#CE when PayloadLen >= 4 ->
 	    << Int:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#CF ->
+	16#CE ->
+	    {more, 4-PayloadLen}; % at least more
+
+	16#CF when PayloadLen >= 8 ->
 	    << Int:64/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
+	16#CF ->
+	    {more, 8-PayloadLen};
 
-	16#D0 -> % int 8
+	16#D0 when PayloadLen >= 1 -> % int 8
 	    << Int:8/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#D1 -> % int 16
+	16#D0 ->
+	    {more, 1};
+
+	16#D1 when PayloadLen >= 2 -> % int 16
 	    << Int:16/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#D2 -> % int 32
+	16#D1 ->
+	    {more, 2-PayloadLen};
+
+	16#D2 when PayloadLen >= 4 -> % int 32
 	    << Int:32/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#D3 -> % int 64
+	16#D2 ->
+	    {more, 4-PayloadLen};
+
+	16#D3 when PayloadLen >= 8 -> % int 64
 	    << Int:64/big-signed-integer-unit:1, Rest/binary >> = Payload,
 	    {Int, Rest};
-	16#DA -> % raw 16
+	16#D3 ->
+	    {more, 8-PayloadLen};
+
+	16#DA when PayloadLen >= 2 -> % raw 16
 	    << Len:16/unsigned-integer-unit:1, Rest/binary >> = Payload,
 	    << Return:Len/binary, Remain/binary >> = Rest,
 	    {Return, Remain};
-	16#DB -> % raw 32
+	16#DA ->
+	    {more, 16-PayloadLen};
+
+	16#DB when PayloadLen >= 4 -> % raw 32
 	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
 	    << Return:Len/binary, Remain/binary >> = Rest,
 	    {Return, Remain};
-	16#DC -> % array 16
+	16#DB ->
+	    {more, 4-PayloadLen};
+
+	16#DC when PayloadLen >= 2 -> % array 16
 	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    Array=unpack_array_(Rest, Len),
-	    case length(Array) of
-		Len -> {Array, <<>>};
-		_ -> 
-		    {Return, RemainRest} = lists:split(Len, Array),
-		    [Remain] = RemainRest,
-		    {Return, Remain}
-	    end;
-	16#DD -> % array 32
+	    unpack_array_(Rest, Len, []);
+	16#DC ->
+	    {more, 2-PayloadLen};
+
+	16#DD when PayloadLen >= 4 -> % array 32
 	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    Array=unpack_array_(Rest, Len),
-	    case length(Array) of
-		Len -> {Array, <<>>};
-		_ -> 
-		    {Return, RemainRest} = lists:split(Len, Array),
-		    [Remain] = RemainRest,
-		    {Return, Remain}
-	    end;
-	16#DE -> % map 16
+	    unpack_array_(Rest, Len, []);
+	16#DD ->
+	    {more, 4-PayloadLen};
+
+	16#DE when PayloadLen >= 2 -> % map 16
 	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    Array=unpack_map_(Rest, Len),
-	    case length(Array) of
-		Len -> { dict:from_list(Array), <<>>};
-		_ -> 
-		    {Return, RemainRest} = lists:split(Len, Array),
-		    [Remain] = RemainRest,
-		    {dict:from_list(Return), Remain}
-	    end;
-	16#DF -> % map 32
+	    unpack_map_(Rest, Len, []);
+	16#DE ->
+	    {more, 2-PayloadLen};
+
+	16#DF when PayloadLen >= 4 -> % map 32
 	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    Array=unpack_map_(Rest, Len),
-	    case length(Array) of
-		Len -> { dict:from_list(Array), <<>>};
-		_ -> 
-		    {Return, RemainRest} = lists:split(Len, Array),
-		    [Remain] = RemainRest,
-		    {dict:from_list(Return), Remain}
-	    end;
+	    unpack_map_(Rest, Len, []);
 
 	% positive fixnum
 	Code when Code >= 2#00000000, Code < 2#10000000->
@@ -294,29 +323,15 @@ unpack_(Flag, Payload)->
 	Code when Code >= 2#10010000 , Code < 2#10100000 ->
 %        1001XXXX for FixArray
 	    Len = Code rem 2#10010000,
-	    Array=unpack_array_(Payload, Len),
-	    case length(Array) of
-		Len -> { Array, <<>>};
-		_ -> 
-		    {Return, RemainRest} = lists:split(Len, Array),
-		    [Remain] = RemainRest,
-		    {Return, Remain}
-	    end;
+	    unpack_array_(Payload, Len, []);
 
 	Code when Code >= 2#10000000 , Code < 2#10010000 ->
 %        1000XXXX for FixMap
 	    Len = Code rem 2#10000000,
-	    Array=unpack_map_(Payload, Len),
-	    case length(Array) of
-		Len -> { dict:from_list(Array), <<>>};
-		_ -> 
-		    {Return, RemainRest} = lists:split(Len, Array),
-		    [Remain] = RemainRest,
-		    {dict:from_list(Return), Remain}
-	    end;
+	    unpack_map_(Payload, Len, []);
 
 	_Other ->
-	    erlang:display(_Other),
+%	    erlang:display(_Other),
 	    {error, no_code_matches}
     end.
 
@@ -356,6 +371,26 @@ port_test()->
     after 1024-> ?assert(false)   end,
     port_close(Port).
 
+test_p(Len,Term,OrigBin,Len) ->
+    {Term, <<>>}=msgpack:unpack(OrigBin);
+test_p(I,_,OrigBin,Len) ->
+    <> = OrigBin,
+    {more, N}=msgpack:unpack(Bin),
+    ?assert(0 < N),
+    ?assert(N < Len).
+
+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), %55),
+%%     Longest = lists:seq(0,12345),
+    {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)).
+%%     {Longest, <<>>} = msgpack:unpack(msgpack:pack(Longest)).
+
 unknown_test()->
     Tests = [0, 1, 2, 123, 512, 1230, 678908,
 	     -1, -23, -512, -1230, -567898,
@@ -377,6 +412,10 @@ test_([]) -> 0;
 test_([S|Rest])->
     Pack = msgpack:pack(S),
     {S, <<>>} = msgpack:unpack( Pack ),
+%    ?debugVal( hoge ),
     1+test_(Rest).
 
+other_test()->
+    {more,1}=msgpack:unpack(<<>>).
+
 -endif.

From bc0c5f0cdca4d5244b3237b63305cfd01d1e9a9d Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Wed, 23 Jun 2010 09:02:53 +0900
Subject: [PATCH 063/152] erlang: (un)pack_map improved, incremental unpacking

---
 erlang/msgpack.erl | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index d2f7068..764de11 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -167,7 +167,8 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
 	{Term, Rest}->
 	    unpack_array_(Rest, RestLen-1, [Term|RetList])
     end.
-    
+
+% FIXME: write test for pack_map/1
 pack_map({dict,M})->
     MaxLen = 16#10000, %65536
     case dict:size(M) of
@@ -183,17 +184,18 @@ pack_map_([])-> <<>>;
 pack_map_([{Key,Value}|Tail]) ->
     << (pack(Key)),(pack(Value)),(pack_map_(Tail)) >>.
 
+% FIXME: write test for unpack_map/1
 -spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
     {more, non_neg_integer()} | { any(), binary()}.
-unpack_map_(Bin,  0,  TmpMap) when is_binary(Bin) -> { dict:from_list(TmpMap), Bin};
-unpack_map_(Bin, Len, TmpMap) when is_binary(Bin) and is_integer(Len) ->
+unpack_map_(Bin,  0,  Dict) when is_binary(Bin) -> { {dict, Dict}, Bin};
+unpack_map_(Bin, Len, Dict) when is_binary(Bin) and is_integer(Len) ->
     case unpack(Bin) of
 	{ more, MoreLen } -> { more, MoreLen+Len-1 };
 	{ Key, Rest } ->
 	    case unpack(Rest) of
 		{more, MoreLen} -> { more, MoreLen+Len-1 };
 		{ Value, Rest2 }->
-		    unpack_map_(Rest2,Len-1,[{Key,Value}|TmpMap])
+		    unpack_map_(Rest2,Len-1,dict:append(Key,Value,Dict))
 	    end
     end.
 
@@ -298,13 +300,13 @@ unpack_(Flag, Payload)->
 
 	16#DE when PayloadLen >= 2 -> % map 16
 	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, []);
+	    unpack_map_(Rest, Len, dict:new());
 	16#DE ->
 	    {more, 2-PayloadLen};
 
 	16#DF when PayloadLen >= 4 -> % map 32
 	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, []);
+	    unpack_map_(Rest, Len, dict:new());
 
 	% positive fixnum
 	Code when Code >= 2#00000000, Code < 2#10000000->
@@ -328,7 +330,7 @@ unpack_(Flag, Payload)->
 	Code when Code >= 2#10000000 , Code < 2#10010000 ->
 %        1000XXXX for FixMap
 	    Len = Code rem 2#10000000,
-	    unpack_map_(Payload, Len, []);
+	    unpack_map_(Payload, Len, dict:new());
 
 	_Other ->
 %	    erlang:display(_Other),
@@ -373,7 +375,7 @@ port_test()->
 
 test_p(Len,Term,OrigBin,Len) ->
     {Term, <<>>}=msgpack:unpack(OrigBin);
-test_p(I,_,OrigBin,Len) ->
+test_p(I,_,OrigBin,Len) when I < Len->
     <> = OrigBin,
     {more, N}=msgpack:unpack(Bin),
     ?assert(0 < N),

From 2cdfbd8970756742af7b378612756c59d184d86b Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 24 Jun 2010 07:26:34 +0900
Subject: [PATCH 064/152] erlang: testing pack_map/unpack_map with a silly bug

---
 erlang/msgpack.erl | 40 +++++++++++++++++++++++++++-------------
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 764de11..dca5888 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -49,10 +49,10 @@ pack(Bin) when is_binary(Bin) ->
     pack_raw(Bin);
 pack(List)  when is_list(List) ->
     pack_array(List);
-pack({dict, Map}) ->
-    pack_map({dict, Map});
-pack(_) ->
-    undefined.
+pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
+    pack_map(Map);
+pack(_O) ->
+    {error, undefined}.
 
 % unpacking.
 % if failed in decoding and not end, get more data
@@ -169,25 +169,25 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
     end.
 
 % FIXME: write test for pack_map/1
-pack_map({dict,M})->
+pack_map(M)->
     MaxLen = 16#10000, %65536
     case dict:size(M) of
 	Len when Len < 16 ->
  	    << 2#1001:4, Len:4/integer-unit:1, (pack_map_(dict:to_list(M))) >>;
 	Len when Len < MaxLen ->
-	    << 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M))) >>;
+	    << 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M)))/binary >>;
 	Len ->
-	    << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M))) >>
+	    << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M)))/binary >>
     end.
 
 pack_map_([])-> <<>>;
 pack_map_([{Key,Value}|Tail]) ->
-    << (pack(Key)),(pack(Value)),(pack_map_(Tail)) >>.
+    << (pack(Key))/binary,(pack(Value))/binary,(pack_map_(Tail))/binary >>.
 
 % FIXME: write test for unpack_map/1
 -spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
     {more, non_neg_integer()} | { any(), binary()}.
-unpack_map_(Bin,  0,  Dict) when is_binary(Bin) -> { {dict, Dict}, Bin};
+unpack_map_(Bin,  0,  Dict) when is_binary(Bin) -> {Dict, Bin};
 unpack_map_(Bin, Len, Dict) when is_binary(Bin) and is_integer(Len) ->
     case unpack(Bin) of
 	{ more, MoreLen } -> { more, MoreLen+Len-1 };
@@ -333,7 +333,6 @@ unpack_(Flag, Payload)->
 	    unpack_map_(Payload, Len, dict:new());
 
 	_Other ->
-%	    erlang:display(_Other),
 	    {error, no_code_matches}
     end.
 
@@ -388,10 +387,25 @@ partial_test()-> % error handling test.
     [test_p(X, Term, Bin, BinLen) || X <- lists:seq(0,BinLen)].
 
 long_test()->
-    Longer = lists:seq(0, 655), %55),
-%%     Longest = lists:seq(0,12345),
-    {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)).
+    Longer = lists:seq(0, 65), %55),
+%    Longest = lists:seq(0,12345),
+    {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)),
+%    {Longest, <<>>} = msgpack:unpack(msgpack:pack(Longest)).
+    ok.
+
+map_test()->
+    Ints = lists:seq(0, 65), %55),
+    Map = dict:from_list([ {X, X*2} || X <- Ints ]),
+    S=msgpack:pack(Map),
+%    ?debugVal(msgpack:unpack(S)),
+    {Map2, <<>>} = msgpack:unpack(S),
+    ?assertEqual(dict:size(Map), dict:size(Map2)),
+%    ?debugVal(dict:to_list(Map2)),
+    OrdMap = orddict:from_list( dict:to_list(Map) ),
+    OrdMap2 = orddict:from_list( dict:to_list(Map2) ),
+%    ?assertEqual(OrdMap, OrdMap2), % FIXME!! its a misery bug.
 %%     {Longest, <<>>} = msgpack:unpack(msgpack:pack(Longest)).
+    ok.
 
 unknown_test()->
     Tests = [0, 1, 2, 123, 512, 1230, 678908,

From 92d192277e14be4f32135a85cbca233e7dca3182 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Fri, 25 Jun 2010 00:22:53 +0900
Subject: [PATCH 065/152] erlang: unpack_map's silly bug fixed. use
 dict:store/3....

---
 erlang/msgpack.erl           | 22 +++++++++-------------
 erlang/testcase_generator.rb |  5 +++--
 2 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index dca5888..0d4151b 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -195,7 +195,7 @@ unpack_map_(Bin, Len, Dict) when is_binary(Bin) and is_integer(Len) ->
 	    case unpack(Rest) of
 		{more, MoreLen} -> { more, MoreLen+Len-1 };
 		{ Value, Rest2 }->
-		    unpack_map_(Rest2,Len-1,dict:append(Key,Value,Dict))
+		    unpack_map_(Rest2,Len-1,dict:store(Key,Value,Dict))
 	    end
     end.
 
@@ -352,7 +352,7 @@ test_data()->
      -1, -23, -512, -1230, -567898, -16#FFFFFFFFFF,
      123.123, -234.4355, 1.0e-34, 1.0e64,
      [23, 234, 0.23],
-     "hogehoge", "243546rf7g68h798j",
+     <<"hogehoge">>, <<"243546rf7g68h798j", 0, 23, 255>>,
      <<"hoasfdafdas][">>,
      [0,42,"sum", [1,2]], [1,42, nil, [3]]
     ].
@@ -387,34 +387,31 @@ partial_test()-> % error handling test.
     [test_p(X, Term, Bin, BinLen) || X <- lists:seq(0,BinLen)].
 
 long_test()->
-    Longer = lists:seq(0, 65), %55),
+    Longer = lists:seq(0, 655),
 %    Longest = lists:seq(0,12345),
     {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)),
 %    {Longest, <<>>} = msgpack:unpack(msgpack:pack(Longest)).
     ok.
 
 map_test()->
-    Ints = lists:seq(0, 65), %55),
+    Ints = lists:seq(0, 65),
     Map = dict:from_list([ {X, X*2} || X <- Ints ]),
-    S=msgpack:pack(Map),
-%    ?debugVal(msgpack:unpack(S)),
-    {Map2, <<>>} = msgpack:unpack(S),
+    {Map2, <<>>} = msgpack:unpack(msgpack:pack(Map)),
     ?assertEqual(dict:size(Map), dict:size(Map2)),
-%    ?debugVal(dict:to_list(Map2)),
     OrdMap = orddict:from_list( dict:to_list(Map) ),
     OrdMap2 = orddict:from_list( dict:to_list(Map2) ),
-%    ?assertEqual(OrdMap, OrdMap2), % FIXME!! its a misery bug.
-%%     {Longest, <<>>} = msgpack:unpack(msgpack:pack(Longest)).
+    ?assertEqual(OrdMap, OrdMap2),
     ok.
 
 unknown_test()->
     Tests = [0, 1, 2, 123, 512, 1230, 678908,
 	     -1, -23, -512, -1230, -567898,
 	     <<"hogehoge">>, <<"243546rf7g68h798j">>,
-%	     123.123,  %FIXME
-%            -234.4355, 1.0e-34, 1.0e64, % FIXME
+	     123.123,
+	     -234.4355, 1.0e-34, 1.0e64,
 	     [23, 234, 0.23],
 	     [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
+	     dict:from_list([{1,2},{<<"hoge">>,nil}]),
 	     42
 	    ],
     Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
@@ -428,7 +425,6 @@ test_([]) -> 0;
 test_([S|Rest])->
     Pack = msgpack:pack(S),
     {S, <<>>} = msgpack:unpack( Pack ),
-%    ?debugVal( hoge ),
     1+test_(Rest).
 
 other_test()->
diff --git a/erlang/testcase_generator.rb b/erlang/testcase_generator.rb
index a7c76c5..cfc36f9 100644
--- a/erlang/testcase_generator.rb
+++ b/erlang/testcase_generator.rb
@@ -40,10 +40,11 @@ end
 objs = [0, 1, 2, 123, 512, 1230, 678908,
         -1, -23, -512, -1230, -567898,
         "hogehoge", "243546rf7g68h798j",
-#        123.123 , #FIXME
-#       -234.4355, 1.0e-34, 1.0e64,
+        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 },
         42
        ]
 begin

From 57f0598373ce542f7499218792a15fa0eee161f7 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Fri, 25 Jun 2010 00:44:14 +0900
Subject: [PATCH 066/152] erlang: code refined and tests added

---
 erlang/msgpack.erl | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 0d4151b..789ed53 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -116,8 +116,7 @@ pack_int_( N ) when is_integer( N )->
     << 16#D3:8, N:64/big-signed-integer-unit:1 >>.
 
 % nil
-pack_nil()->
-    << 16#C0:8 >>.
+pack_nil()->    << 16#C0:8 >>.
 % pack_true / pack_false
 pack_bool(true)->    << 16#C3:8 >>;
 pack_bool(false)->   << 16#C2:8 >>.
@@ -132,11 +131,10 @@ pack_double(F) when is_float(F)->
 
 % raw bytes
 pack_raw(Bin) when is_binary(Bin)->
-    MaxLen = 16#10000, % 65536
     case byte_size(Bin) of
 	Len when Len < 6->
 	    << 2#101:3, Len:5, Bin/binary >>;
-	Len when Len < MaxLen ->
+	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 >>
@@ -144,11 +142,10 @@ pack_raw(Bin) when is_binary(Bin)->
 
 % list / tuple
 pack_array(L) when is_list(L)->
-    MaxLen = 16#10000, %65536
     case length(L) of
  	Len when Len < 16 ->
  	    << 2#1001:4, Len:4/integer-unit:1, (pack_array_(L))/binary >>;
-	Len when Len < MaxLen ->
+	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 >>
@@ -164,17 +161,15 @@ unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, RestLen};
 unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
     case unpack(Bin) of
 	{more, Len} -> {more, Len+RestLen-1};
-	{Term, Rest}->
-	    unpack_array_(Rest, RestLen-1, [Term|RetList])
+	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
     end.
 
 % FIXME: write test for pack_map/1
 pack_map(M)->
-    MaxLen = 16#10000, %65536
     case dict:size(M) of
 	Len when Len < 16 ->
  	    << 2#1001:4, Len:4/integer-unit:1, (pack_map_(dict:to_list(M))) >>;
-	Len when Len < MaxLen ->
+	Len when Len < 16#10000 -> % 65536
 	    << 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M)))/binary >>;
 	Len ->
 	    << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M)))/binary >>
@@ -348,13 +343,15 @@ compare_all([LH|LTL], [RH|RTL]) ->
     compare_all(LTL, RTL).
 
 test_data()->
-    [0, 1, 2, 123, 512, 1230, 678908, 16#FFFFFFFFFF,
+    [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]]
+     [0,42, <<"sum">>, [1,2]], [1,42, nil, [3]],
+     42
     ].
 
 basic_test()->
@@ -395,7 +392,7 @@ long_test()->
 
 map_test()->
     Ints = lists:seq(0, 65),
-    Map = dict:from_list([ {X, X*2} || X <- Ints ]),
+    Map = dict:from_list([ {X, X*2} || X <- Ints ] ++ [{<<"hage">>, 324}, {43542, [nil, true, false]}]),
     {Map2, <<>>} = msgpack:unpack(msgpack:pack(Map)),
     ?assertEqual(dict:size(Map), dict:size(Map2)),
     OrdMap = orddict:from_list( dict:to_list(Map) ),

From ad052cb510e3be659df0322cbf33e4840c010b9b Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Fri, 25 Jun 2010 01:26:57 +0900
Subject: [PATCH 067/152] updated readme

---
 erlang/README.md | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)

diff --git a/erlang/README.md b/erlang/README.md
index 50d446d..8616d5e 100644
--- a/erlang/README.md
+++ b/erlang/README.md
@@ -2,18 +2,8 @@ MessagePack for Erlang
 ======================
 Binary-based efficient object serialization library.
 
-## Status 
+see wiki ( http://redmine.msgpack.org/projects/msgpack/wiki/QuickStartErlang ) for details
 
-still in development.
+# Status
 
-TODOs:
- - decide string specification.
-
-## Installation
-
-## Example
-
-## License
-
-
- - 
+0.1.0 released.

From 0cca90c21deb2517de95c83a837bf90efc6b9fa0 Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 25 Jun 2010 17:32:11 +0200
Subject: [PATCH 068/152] Fix encoding of fixmap type. The tag value was wrong,
 and a missing /binary flag caused an error.

---
 erlang/msgpack.erl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 789ed53..bbf9e64 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -168,7 +168,7 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
 pack_map(M)->
     case dict:size(M) of
 	Len when Len < 16 ->
- 	    << 2#1001:4, Len:4/integer-unit:1, (pack_map_(dict:to_list(M))) >>;
+ 	    << 2#1000:4, Len:4/integer-unit:1, (pack_map_(dict:to_list(M)))/binary >>;
 	Len when Len < 16#10000 -> % 65536
 	    << 16#DE:8, Len:16/big-unsigned-integer-unit:1, (pack_map_(dict:to_list(M)))/binary >>;
 	Len ->

From a1b2b41cdcb4bbc98e21bcd334b729ec6e7b90d5 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Sat, 26 Jun 2010 08:40:36 +0900
Subject: [PATCH 069/152] erlang: bugfix(serialization of -234 goes <<208,22>>
 while it should go int16 <<0xD1, ...>>)

---
 erlang/msgpack.erl           | 20 ++++++++++----------
 erlang/testcase_generator.rb |  1 +
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 789ed53..63d648c 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -86,15 +86,12 @@ pack_uint_(N) when is_integer( N ) , N < 128 ->
 % uint 8
 pack_uint_( N ) when is_integer( N ) andalso N < 256 ->
     << 16#CC:8, N:8 >>;
-
 % uint 16
 pack_uint_( N ) when is_integer( N ) andalso N < 65536 ->
     << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>;
-
 % uint 32
 pack_uint_( N ) when is_integer( N ) andalso N < 16#FFFFFFFF->
     << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>;
-
 % uint 64
 pack_uint_( N ) when is_integer( N )->
     << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>.
@@ -103,13 +100,13 @@ pack_uint_( N ) when is_integer( N )->
 pack_int_( N ) when is_integer( N ) , N >= -32->
     << 2#111:3, N:5 >>;
 % int 8
-pack_int_( N ) when is_integer( N ) , N >= -256 ->
-    << 16#D0:8, N:8 >>;
+pack_int_( N ) when is_integer( N ) , N > -128 ->
+    << 16#D0:8, N:8/big-signed-integer-unit:1 >>;
 % int 16
-pack_int_( N ) when is_integer( N ), N >= -65536 ->
+pack_int_( N ) when is_integer( N ), N > -32768 ->
     << 16#D1:8, N:16/big-signed-integer-unit:1 >>;
 % int 32
-pack_int_( N ) when is_integer( N ), N >= -16#FFFFFFFF ->
+pack_int_( N ) when is_integer( N ), N > -16#FFFFFFFF ->
     << 16#D2:8, N:32/big-signed-integer-unit:1 >>;
 % int 64
 pack_int_( N ) when is_integer( N )->
@@ -351,6 +348,7 @@ test_data()->
      <<"hogehoge">>, <<"243546rf7g68h798j", 0, 23, 255>>,
      <<"hoasfdafdas][">>,
      [0,42, <<"sum">>, [1,2]], [1,42, nil, [3]],
+     -234, -40000, -16#10000000, -16#100000000,
      42
     ].
 
@@ -409,6 +407,7 @@ unknown_test()->
 	     [23, 234, 0.23],
 	     [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
 	     dict:from_list([{1,2},{<<"hoge">>,nil}]),
+	     -234, -50000,
 	     42
 	    ],
     Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
@@ -419,9 +418,10 @@ unknown_test()->
     port_close(Port).
 
 test_([]) -> 0;
-test_([S|Rest])->
-    Pack = msgpack:pack(S),
-    {S, <<>>} = msgpack:unpack( Pack ),
+test_([Before|Rest])->
+    Pack = msgpack:pack(Before),
+    {After, <<>>} = msgpack:unpack( Pack ),
+    ?assertEqual(Before, After),
     1+test_(Rest).
 
 other_test()->
diff --git a/erlang/testcase_generator.rb b/erlang/testcase_generator.rb
index cfc36f9..8445bdd 100644
--- a/erlang/testcase_generator.rb
+++ b/erlang/testcase_generator.rb
@@ -45,6 +45,7 @@ objs = [0, 1, 2, 123, 512, 1230, 678908,
         [23, 234, 0.23],
         [0,42,"sum", [1,2]], [1,42, nil, [3]],
         { 1 => 2, "hoge" => nil },
+        -234, -50000,
         42
        ]
 begin

From 279121f87f322f3c1a2430751eb3a1032030853d Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Mon, 28 Jun 2010 11:56:12 +0200
Subject: [PATCH 070/152] 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.

---
 erlang/msgpack.erl | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index bbf9e64..bc63677 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -30,8 +30,7 @@
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
 -type reason() ::  enomem | badarg | no_code_matches.
--type map() :: any(). % there's no 'dict' type...
--type msgpack_term() :: [msgpack_term()] | integer() | float() | {dict, map()}.
+-type msgpack_term() :: [msgpack_term()] | {[{msgpack_term(),msgpack_term()}]} | integer() | float().
 
 % ===== external APIs ===== %
 -spec pack(Term::msgpack_term()) -> binary().
@@ -49,8 +48,10 @@ pack(Bin) when is_binary(Bin) ->
     pack_raw(Bin);
 pack(List)  when is_list(List) ->
     pack_array(List);
-pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
+pack({Map}) when is_list(Map) ->
     pack_map(Map);
+pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
+    pack_map(dict:from_list(Map));
 pack(_O) ->
     {error, undefined}.
 
@@ -166,13 +167,13 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
 
 % FIXME: write test for pack_map/1
 pack_map(M)->
-    case dict:size(M) of
+    case length(M) of
 	Len when Len < 16 ->
- 	    << 2#1000:4, Len:4/integer-unit:1, (pack_map_(dict:to_list(M)))/binary >>;
+ 	    << 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_(dict:to_list(M)))/binary >>;
+	    << 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_(dict:to_list(M)))/binary >>
+	    << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(M))/binary >>
     end.
 
 pack_map_([])-> <<>>;
@@ -182,15 +183,15 @@ pack_map_([{Key,Value}|Tail]) ->
 % FIXME: write test for unpack_map/1
 -spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
     {more, non_neg_integer()} | { any(), binary()}.
-unpack_map_(Bin,  0,  Dict) when is_binary(Bin) -> {Dict, Bin};
-unpack_map_(Bin, Len, Dict) when is_binary(Bin) and is_integer(Len) ->
+unpack_map_(Bin,  0,  Acc) when is_binary(Bin) -> {{lists:reverse(Acc)}, Bin};
+unpack_map_(Bin, Len, Acc) when is_binary(Bin) and is_integer(Len) ->
     case unpack(Bin) of
 	{ more, MoreLen } -> { more, MoreLen+Len-1 };
 	{ Key, Rest } ->
 	    case unpack(Rest) of
 		{more, MoreLen} -> { more, MoreLen+Len-1 };
-		{ Value, Rest2 }->
-		    unpack_map_(Rest2,Len-1,dict:store(Key,Value,Dict))
+		{ Value, Rest2 } ->
+		    unpack_map_(Rest2,Len-1,[{Key,Value}|Acc])
 	    end
     end.
 
@@ -295,13 +296,13 @@ unpack_(Flag, Payload)->
 
 	16#DE when PayloadLen >= 2 -> % map 16
 	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, dict:new());
+	    unpack_map_(Rest, Len, []);
 	16#DE ->
 	    {more, 2-PayloadLen};
 
 	16#DF when PayloadLen >= 4 -> % map 32
 	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, dict:new());
+	    unpack_map_(Rest, Len, []);
 
 	% positive fixnum
 	Code when Code >= 2#00000000, Code < 2#10000000->
@@ -325,7 +326,7 @@ unpack_(Flag, Payload)->
 	Code when Code >= 2#10000000 , Code < 2#10010000 ->
 %        1000XXXX for FixMap
 	    Len = Code rem 2#10000000,
-	    unpack_map_(Payload, Len, dict:new());
+	    unpack_map_(Payload, Len, []);
 
 	_Other ->
 	    {error, no_code_matches}
@@ -392,12 +393,9 @@ long_test()->
 
 map_test()->
     Ints = lists:seq(0, 65),
-    Map = dict:from_list([ {X, X*2} || X <- Ints ] ++ [{<<"hage">>, 324}, {43542, [nil, true, false]}]),
+    Map = {[ {X, X*2} || X <- Ints ] ++ [{<<"hage">>, 324}, {43542, [nil, true, false]}]},
     {Map2, <<>>} = msgpack:unpack(msgpack:pack(Map)),
-    ?assertEqual(dict:size(Map), dict:size(Map2)),
-    OrdMap = orddict:from_list( dict:to_list(Map) ),
-    OrdMap2 = orddict:from_list( dict:to_list(Map2) ),
-    ?assertEqual(OrdMap, OrdMap2),
+    ?assertEqual(Map, Map2),
     ok.
 
 unknown_test()->

From 537322e3b59978d26fcf096433fc718c16cc8b84 Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Mon, 28 Jun 2010 14:17:44 +0200
Subject: [PATCH 071/152] Big speedup (around 40%) of maps and arrays encoding
 by using proper tail recursion.

---
 erlang/msgpack.erl | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index bc63677..255542b 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -59,7 +59,7 @@ pack(_O) ->
 % if failed in decoding and not end, get more data
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
--spec unpack( binary() )-> 
+-spec unpack( binary() )->
     {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
 unpack(Bin) when not is_binary(Bin)->
     {error, badarg};
@@ -82,7 +82,7 @@ unpack_all(Data)->
 % ===== internal APIs ===== %
 
 % positive fixnum
-pack_uint_(N) when is_integer( N ) , N < 128 ->  
+pack_uint_(N) when is_integer( N ) , N < 128 ->
     << 2#0:1, N:7 >>;
 % uint 8
 pack_uint_( N ) when is_integer( N ) andalso N < 256 ->
@@ -145,15 +145,15 @@ pack_raw(Bin) when is_binary(Bin)->
 pack_array(L) when is_list(L)->
     case length(L) of
  	Len when Len < 16 ->
- 	    << 2#1001:4, Len:4/integer-unit:1, (pack_array_(L))/binary >>;
+ 	    << 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 >>;
+	    << 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 >>
+	    << 16#DD:8, Len:32/big-unsigned-integer-unit:1,(pack_array_(L, <<>>))/binary >>
     end.
-pack_array_([])-> <<>>;
-pack_array_([Head|Tail])->
-    << (pack(Head))/binary, (pack_array_(Tail))/binary >>.
+pack_array_([], Acc) -> Acc;
+pack_array_([Head|Tail], Acc) ->
+    pack_array_(Tail, <>).
 
 % FIXME! this should be tail-recursive and without lists:reverse/1
 unpack_array_(<<>>, 0, RetList)   -> {lists:reverse(RetList), <<>>};
@@ -169,16 +169,16 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
 pack_map(M)->
     case length(M) of
 	Len when Len < 16 ->
- 	    << 2#1000:4, Len:4/integer-unit:1, (pack_map_(M))/binary >>;
+ 	    << 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 >>;
+	    << 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 >>
+	    << 16#DF:8, Len:32/big-unsigned-integer-unit:1, (pack_map_(M, <<>>))/binary >>
     end.
 
-pack_map_([])-> <<>>;
-pack_map_([{Key,Value}|Tail]) ->
-    << (pack(Key))/binary,(pack(Value))/binary,(pack_map_(Tail))/binary >>.
+pack_map_([], Acc) -> Acc;
+pack_map_([{Key,Value}|Tail], Acc) ->
+    pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
 
 % FIXME: write test for unpack_map/1
 -spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
@@ -200,7 +200,7 @@ unpack_map_(Bin, Len, Acc) when is_binary(Bin) and is_integer(Len) ->
     {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
 unpack_(Flag, Payload)->
     PayloadLen = byte_size(Payload),
-    case Flag of 
+    case Flag of
 	16#C0 ->
 	    {nil, Payload};
 	16#C2 ->
@@ -313,7 +313,7 @@ unpack_(Flag, Payload)->
 	    {(Code - 16#100), Payload};
 
 	Code when Code >= 2#10100000 , Code < 2#11000000 ->
-%	 101XXXXX for FixRaw 
+%	 101XXXXX for FixRaw
 	    Len = Code rem 2#10100000,
 	    << Return:Len/binary, Remain/binary >> = Payload,
 	    {Return, Remain};
@@ -344,7 +344,7 @@ compare_all([LH|LTL], [RH|RTL]) ->
     compare_all(LTL, RTL).
 
 test_data()->
-    [true, false, nil, 
+    [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,

From b471e52e281d189396a78cef3eb7ecacbab51d21 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Tue, 29 Jun 2010 00:21:47 +0900
Subject: [PATCH 072/152] erlang: explicit API for serializing proplists,      
   so as not to make wrong call of pack({proplists()}).

---
 erlang/msgpack.erl | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index c807065..29e1728 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -24,6 +24,7 @@
 %% for C API (http://msgpack.sourceforge.jp/c:doc)
 %% except buffering functions (both copying and zero-copying).
 -export([pack/1, unpack/1, unpack_all/1]).
+-export([pack_map/1])
 
 % compile:
 % erl> c(msgpack).
@@ -51,7 +52,7 @@ pack(List)  when is_list(List) ->
 pack({Map}) when is_list(Map) ->
     pack_map(Map);
 pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
-    pack_map(dict:from_list(Map));
+    pack_map(dict:to_list(Map));
 pack(_O) ->
     {error, undefined}.
 
@@ -78,6 +79,15 @@ unpack_all(Data)->
 	    [Term|unpack_all(Binary)]
     end.
 
+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.
 
 % ===== internal APIs ===== %
 
@@ -162,17 +172,6 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
 	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
     end.
 
-% FIXME: write test for pack_map/1
-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>>).
@@ -404,7 +403,7 @@ unknown_test()->
 	     -234.4355, 1.0e-34, 1.0e64,
 	     [23, 234, 0.23],
 	     [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
-	     dict:from_list([{1,2},{<<"hoge">>,nil}]),
+	     {[{1,2},{<<"hoge">>,nil}]},
 	     -234, -50000,
 	     42
 	    ],

From 90e305d789d231258e26a2dc546cfdb6f8c09ce8 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Tue, 29 Jun 2010 00:21:47 +0900
Subject: [PATCH 073/152] erlang: explicit API for serializing proplists,      
   so as not to make wrong call of pack({proplists()}).

---
 erlang/msgpack.erl | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index c807065..a168b55 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -24,6 +24,7 @@
 %% for C API (http://msgpack.sourceforge.jp/c:doc)
 %% except buffering functions (both copying and zero-copying).
 -export([pack/1, unpack/1, unpack_all/1]).
+-export([pack_map/1]).
 
 % compile:
 % erl> c(msgpack).
@@ -51,7 +52,7 @@ pack(List)  when is_list(List) ->
 pack({Map}) when is_list(Map) ->
     pack_map(Map);
 pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
-    pack_map(dict:from_list(Map));
+    pack_map(dict:to_list(Map));
 pack(_O) ->
     {error, undefined}.
 
@@ -78,6 +79,15 @@ unpack_all(Data)->
 	    [Term|unpack_all(Binary)]
     end.
 
+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.
 
 % ===== internal APIs ===== %
 
@@ -162,17 +172,6 @@ unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
 	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
     end.
 
-% FIXME: write test for pack_map/1
-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>>).
@@ -404,7 +403,7 @@ unknown_test()->
 	     -234.4355, 1.0e-34, 1.0e64,
 	     [23, 234, 0.23],
 	     [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
-	     dict:from_list([{1,2},{<<"hoge">>,nil}]),
+	     {[{1,2},{<<"hoge">>,nil}]},
 	     -234, -50000,
 	     42
 	    ],

From 8f7f23a0e5fcc595f6d6178e9e532b38b5cd1b46 Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Mon, 28 Jun 2010 18:11:52 +0200
Subject: [PATCH 074/152] 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.

---
 erlang/msgpack.erl | 290 +++++++++++++++++++--------------------------
 1 file changed, 123 insertions(+), 167 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 255542b..e94262d 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -42,8 +42,10 @@ pack(O) when is_float(O) ->
     pack_double(O);
 pack(nil) ->
     pack_nil();
-pack(Bool) when is_atom(Bool) ->
-    pack_bool(Bool);
+pack(true) ->
+    pack_true();
+pack(false) ->
+    pack_false();
 pack(Bin) when is_binary(Bin) ->
     pack_raw(Bin);
 pack(List)  when is_list(List) ->
@@ -61,13 +63,8 @@ pack(_O) ->
 % TODO: error case for imcomplete format when short for any type formats.
 -spec unpack( binary() )->
     {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
-unpack(Bin) when not is_binary(Bin)->
-    {error, badarg};
-unpack(Bin) when bit_size(Bin) >= 8 ->
-    << Flag:8/unsigned-integer, Payload/binary >> = Bin,
-    unpack_(Flag, Payload);
-unpack(<<>>)-> % when bit_size(Bin) < 8 ->
-    {more, 1}.
+unpack(Bin) ->
+    unpack_(Bin).
 
 -spec unpack_all( binary() ) -> [msgpack_term()].
 unpack_all(Data)->
@@ -82,56 +79,52 @@ unpack_all(Data)->
 % ===== internal APIs ===== %
 
 % positive fixnum
-pack_uint_(N) when is_integer( N ) , N < 128 ->
+pack_uint_(N) when N < 128 ->
     << 2#0:1, N:7 >>;
 % uint 8
-pack_uint_( N ) when is_integer( N ) andalso N < 256 ->
+pack_uint_(N) when N < 256 ->
     << 16#CC:8, N:8 >>;
-
 % uint 16
-pack_uint_( N ) when is_integer( N ) andalso N < 65536 ->
+pack_uint_(N) when N < 65536 ->
     << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>;
-
 % uint 32
-pack_uint_( N ) when is_integer( N ) andalso N < 16#FFFFFFFF->
+pack_uint_(N) when N < 16#FFFFFFFF->
     << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>;
-
 % uint 64
-pack_uint_( N ) when is_integer( N )->
+pack_uint_(N) ->
     << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>.
 
 % negative fixnum
-pack_int_( N ) when is_integer( N ) , N >= -32->
+pack_int_(N) when N >= -32->
     << 2#111:3, N:5 >>;
 % int 8
-pack_int_( N ) when is_integer( N ) , N >= -256 ->
+pack_int_(N) when N >= -256 ->
     << 16#D0:8, N:8 >>;
 % int 16
-pack_int_( N ) when is_integer( N ), N >= -65536 ->
+pack_int_(N) when N >= -65536 ->
     << 16#D1:8, N:16/big-signed-integer-unit:1 >>;
 % int 32
-pack_int_( N ) when is_integer( N ), N >= -16#FFFFFFFF ->
+pack_int_(N) when N >= -16#FFFFFFFF ->
     << 16#D2:8, N:32/big-signed-integer-unit:1 >>;
 % int 64
-pack_int_( N ) when is_integer( N )->
+pack_int_(N) ->
     << 16#D3:8, N:64/big-signed-integer-unit:1 >>.
 
-% nil
-pack_nil()->    << 16#C0:8 >>.
-% pack_true / pack_false
-pack_bool(true)->    << 16#C3:8 >>;
-pack_bool(false)->   << 16#C2:8 >>.
+% nil/true/false
+pack_nil() ->  << 16#C0:8 >>.
+pack_true()->  << 16#C3:8 >>.
+pack_false()-> << 16#C2:8 >>.
 
 % 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) when is_float(F)->
+pack_double(F) ->
     << 16#CB:8, F:64/big-float-unit:1 >>.
 
 % raw bytes
-pack_raw(Bin) when is_binary(Bin)->
+pack_raw(Bin) ->
     case byte_size(Bin) of
 	Len when Len < 6->
 	    << 2#101:3, Len:5, Bin/binary >>;
@@ -142,24 +135,22 @@ pack_raw(Bin) when is_binary(Bin)->
     end.
 
 % list / tuple
-pack_array(L) when is_list(L)->
+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 >>;
+	    << 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 >>
+	    << 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, <>).
 
 % FIXME! this should be tail-recursive and without lists:reverse/1
-unpack_array_(<<>>, 0, RetList)   -> {lists:reverse(RetList), <<>>};
-unpack_array_(Remain, 0, RetList) when is_binary(Remain)-> {lists:reverse(RetList), Remain};
-unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, RestLen};
-unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
+unpack_array_(Remain, 0, RetList) -> {lists:reverse(RetList), Remain};
+unpack_array_(Bin, RestLen, RetList) ->
     case unpack(Bin) of
 	{more, Len} -> {more, Len+RestLen-1};
 	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
@@ -181,10 +172,10 @@ pack_map_([{Key,Value}|Tail], Acc) ->
     pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
 
 % FIXME: write test for unpack_map/1
--spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
-    {more, non_neg_integer()} | { any(), binary()}.
-unpack_map_(Bin,  0,  Acc) when is_binary(Bin) -> {{lists:reverse(Acc)}, Bin};
-unpack_map_(Bin, Len, Acc) when is_binary(Bin) and is_integer(Len) ->
+-spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}]) ->
+    {more, non_neg_integer()} | {any(), binary()}.
+unpack_map_(Bin,  0,  Acc) -> {{lists:reverse(Acc)}, Bin};
+unpack_map_(Bin, Len, Acc) ->
     case unpack(Bin) of
 	{ more, MoreLen } -> { more, MoreLen+Len-1 };
 	{ Key, Rest } ->
@@ -195,142 +186,107 @@ unpack_map_(Bin, Len, Acc) when is_binary(Bin) and is_integer(Len) ->
 	    end
     end.
 
-% {more, 
--spec unpack_(Flag::integer(), Payload::binary())->
-    {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
-unpack_(Flag, Payload)->
-    PayloadLen = byte_size(Payload),
-    case Flag of
-	16#C0 ->
-	    {nil, Payload};
-	16#C2 ->
-	    {false, Payload};
-	16#C3 ->
-	    {true, Payload};
 
-	16#CA when PayloadLen >= 4 -> % 32bit float
-	    << Return:32/float-unit:1, Rest/binary >> = Payload,
-	    {Return, Rest};
-	16#CA ->
-	    {more, 4-PayloadLen}; % at least more
+-spec unpack_(Payload::binary()) -> {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
+unpack_(<<16#C0, Rest/binary>>) ->
+    {nil, Rest};
+unpack_(<<16#C2, Rest/binary>>) ->
+    {false, Rest};
+unpack_(<<16#C3, Rest/binary>>) ->
+    {true, Rest};
 
-	16#CB when PayloadLen >= 8 -> % 64bit float
-	    << Return:64/float-unit:1, Rest/binary >> = Payload,
-	    {Return, Rest};
-	16#CB ->
-	    {more, 8-PayloadLen};
+unpack_(<<16#CA, Return:32/float-unit:1, Rest/binary>>) -> % 32bit float
+    {Return, Rest};
+unpack_(<<16#CA, Rest/binary>>) ->
+    {more, 4-byte_size(Rest)};
+unpack_(<<16#CB, Return:64/float-unit:1, Rest/binary>>) -> % 64bit float
+    {Return, Rest};
+unpack_(<<16#CB, Rest/binary>>) ->
+    {more, 8-byte_size(Rest)};
 
-	16#CC when PayloadLen >= 1 -> % uint 8
-	    << Int:8/unsigned-integer, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CC ->
-	    {more, 1};
+unpack_(<<16#CC, Int:8/unsigned-integer, Rest/binary>>) -> % uint 8
+    {Int, Rest};
+unpack_(<<16#CC>>) ->
+    {more, 1};
+unpack_(<<16#CD, Int:16/big-unsigned-integer-unit:1, Rest/binary>>) -> % uint 16
+    {Int, Rest};
+unpack_(<<16#CD, Rest/binary>>) ->
+    {more, 2-byte_size(Rest)};
+unpack_(<<16#CE, Int:32/big-unsigned-integer-unit:1, Rest/binary>>) -> % uint 32
+    {Int, Rest};
+unpack_(<<16#CE, Rest/binary>>) ->
+    {more, 4-byte_size(Rest)};
+unpack_(<<16#CF, Int:64/big-unsigned-integer-unit:1, Rest/binary>>) -> % uint 64
+    {Int, Rest};
+unpack_(<<16#CF, Rest/binary>>) ->
+    {more, 8-byte_size(Rest)};
 
-	16#CD when PayloadLen >= 2 -> % uint 16
-	    << Int:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CD ->
-	    {more, 2-PayloadLen};
+unpack_(<<16#D0, Int:8/signed-integer, Rest/binary>>) -> % int 8
+    {Int, Rest};
+unpack_(<<16#D0>>) ->
+    {more, 1};
+unpack_(<<16#D1, Int:16/big-signed-integer-unit:1, Rest/binary>>) -> % int 16
+    {Int, Rest};
+unpack_(<<16#D1, Rest/binary>>) ->
+    {more, 2-byte_size(Rest)};
+unpack_(<<16#D2, Int:32/big-signed-integer-unit:1, Rest/binary>>) -> % int 32
+    {Int, Rest};
+unpack_(<<16#D2, Rest/binary>>) ->
+    {more, 4-byte_size(Rest)};
+unpack_(<<16#D3, Int:64/big-signed-integer-unit:1, Rest/binary>>) -> % int 64
+    {Int, Rest};
+unpack_(<<16#D3, Rest/binary>>) ->
+    {more, 8-byte_size(Rest)};
 
-	16#CE when PayloadLen >= 4 ->
-	    << Int:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CE ->
-	    {more, 4-PayloadLen}; % at least more
+unpack_(<<16#DA, Len:16/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> % raw 16
+    {Val, Rest};
+unpack_(<<16#DA, Rest/binary>>) ->
+    {more, 16-byte_size(Rest)};
+unpack_(<<16#DB, Len:32/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> % raw 32
+    {Val, Rest};
+unpack_(<<16#DB, Rest/binary>>) ->
+    {more, 32-byte_size(Rest)};
 
-	16#CF when PayloadLen >= 8 ->
-	    << Int:64/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CF ->
-	    {more, 8-PayloadLen};
+unpack_(<<16#DC, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> % array 16
+    unpack_array_(Rest, Len, []);
+unpack_(<<16#DC, Rest/binary>>) ->
+    {more, 2-byte_size(Rest)};
+unpack_(<<16#DD, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> % array 32
+    unpack_array_(Rest, Len, []);
+unpack_(<<16#DD, Rest/binary>>) ->
+    {more, 4-byte_size(Rest)};
 
-	16#D0 when PayloadLen >= 1 -> % int 8
-	    << Int:8/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D0 ->
-	    {more, 1};
+unpack_(<<16#DE, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> % map 16
+    unpack_map_(Rest, Len, []);
+unpack_(<<16#DE, Rest/binary>>) ->
+    {more, 2-byte_size(Rest)};
+unpack_(<<16#DF, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> % map 32
+    unpack_map_(Rest, Len, []);
+unpack_(<<16#DF, Rest/binary>>) ->
+    {more, 4-byte_size(Rest)};
 
-	16#D1 when PayloadLen >= 2 -> % int 16
-	    << Int:16/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D1 ->
-	    {more, 2-PayloadLen};
+unpack_(<<0:1, Value:7, Rest/binary>>) -> % positive fixnum
+    {Value, Rest};
+unpack_(<<2#111:3, Value:5, Rest/binary>>) -> % negative fixnum
+    {Value - 2#100000, Rest};
+unpack_(<<2#101:3, Len:5, Value:Len/binary, Rest/binary>>) -> % fixraw
+    {Value, Rest};
+unpack_(<<2#101:3, Len:5, Rest/binary>>) ->
+    {more, Len-byte_size(Rest)};
+unpack_(<<2#1001:4, Len:4, Rest/binary>>) -> % fixarray
+    unpack_array_(Rest, Len, []);
+unpack_(<<2#1000:4, Len:4, Rest/binary>>) -> % fixmap
+    unpack_map_(Rest, Len, []);
 
-	16#D2 when PayloadLen >= 4 -> % int 32
-	    << Int:32/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D2 ->
-	    {more, 4-PayloadLen};
+%unpack_(<>) when F==16#C1; F==16#C4; F==16#C5; F==16#C6; F==16#C7; F==16#C8; F==16#C9; F==16#D5; F==16#D6; F==16#D7; F==16#D8; F==16#D9->
+%    {error, {badarg, <>}}.
+%unpack_(Other) when is_binary(Bin) ->
+%    {more, 1}.
+unpack_(<<>>) ->
+    {more, 1}.
+unpack_(Other) ->
+    {error, {badarg, Other}}.
 
-	16#D3 when PayloadLen >= 8 -> % int 64
-	    << Int:64/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D3 ->
-	    {more, 8-PayloadLen};
-
-	16#DA when PayloadLen >= 2 -> % raw 16
-	    << Len:16/unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    << Return:Len/binary, Remain/binary >> = Rest,
-	    {Return, Remain};
-	16#DA ->
-	    {more, 16-PayloadLen};
-
-	16#DB when PayloadLen >= 4 -> % raw 32
-	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    << Return:Len/binary, Remain/binary >> = Rest,
-	    {Return, Remain};
-	16#DB ->
-	    {more, 4-PayloadLen};
-
-	16#DC when PayloadLen >= 2 -> % array 16
-	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_array_(Rest, Len, []);
-	16#DC ->
-	    {more, 2-PayloadLen};
-
-	16#DD when PayloadLen >= 4 -> % array 32
-	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_array_(Rest, Len, []);
-	16#DD ->
-	    {more, 4-PayloadLen};
-
-	16#DE when PayloadLen >= 2 -> % map 16
-	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, []);
-	16#DE ->
-	    {more, 2-PayloadLen};
-
-	16#DF when PayloadLen >= 4 -> % map 32
-	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, []);
-
-	% positive fixnum
-	Code when Code >= 2#00000000, Code < 2#10000000->
-	    {Code, Payload};
-
-	% negative fixnum
-	Code when Code >= 2#11100000 ->
-	    {(Code - 16#100), Payload};
-
-	Code when Code >= 2#10100000 , Code < 2#11000000 ->
-%	 101XXXXX for FixRaw
-	    Len = Code rem 2#10100000,
-	    << Return:Len/binary, Remain/binary >> = Payload,
-	    {Return, Remain};
-
-	Code when Code >= 2#10010000 , Code < 2#10100000 ->
-%        1001XXXX for FixArray
-	    Len = Code rem 2#10010000,
-	    unpack_array_(Payload, Len, []);
-
-	Code when Code >= 2#10000000 , Code < 2#10010000 ->
-%        1000XXXX for FixMap
-	    Len = Code rem 2#10000000,
-	    unpack_map_(Payload, Len, []);
-
-	_Other ->
-	    {error, no_code_matches}
-    end.
 
 % ===== test codes ===== %
 -include_lib("eunit/include/eunit.hrl").
@@ -419,7 +375,7 @@ unknown_test()->
 test_([]) -> 0;
 test_([S|Rest])->
     Pack = msgpack:pack(S),
-    {S, <<>>} = msgpack:unpack( Pack ),
+    ?assertEqual({S, <<>>}, msgpack:unpack(Pack)),
     1+test_(Rest).
 
 other_test()->

From 9fffa9800ae4b09180d2b892f1f70215534a874b Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 29 Jun 2010 14:54:09 +0900
Subject: [PATCH 075/152] ruby: fixes RDoc of Unpacker#execute and
 Unpacker#execute_impl

---
 ruby/makegem.sh | 2 ++
 ruby/unpack.c   | 4 ++--
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/ruby/makegem.sh b/ruby/makegem.sh
index 827f452..d21f06a 100755
--- a/ruby/makegem.sh
+++ b/ruby/makegem.sh
@@ -19,6 +19,8 @@ cp ../test/cases.json           test/
 
 gem build msgpack.gemspec
 
+rdoc rbinit.c pack.c unpack.c
+
 if [ $? -eq 0 ]; then
 	rm -rf ext msgpack test/msgpack_test.rb
 fi
diff --git a/ruby/unpack.c b/ruby/unpack.c
index 65ae476..151dbf4 100644
--- a/ruby/unpack.c
+++ b/ruby/unpack.c
@@ -638,7 +638,7 @@ static VALUE MessagePack_Unpacker_execute_impl(VALUE self, VALUE data,
  * Document-method: MessagePack::Unpacker#execute_limit
  *
  * call-seq:
- *   unpacker.unpack_limit(data, offset, limit) -> next offset
+ *   unpacker.execute_limit(data, offset, limit) -> next offset
  *
  * Deserializes one object over the specified buffer from _offset_ bytes upto _limit_ bytes.
  *
@@ -660,7 +660,7 @@ static VALUE MessagePack_Unpacker_execute_limit(VALUE self, VALUE data,
  * Document-method: MessagePack::Unpacker#execute
  *
  * call-seq:
- *   unpacker.unpack(data, offset) -> next offset
+ *   unpacker.execute(data, offset) -> next offset
  *
  * Deserializes one object over the specified buffer from _offset_ bytes.
  *

From 34a29cd0a50eea4a0e008fe3947c86179d536540 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 29 Jun 2010 14:54:40 +0900
Subject: [PATCH 076/152] ruby: fixes SEGV problem caused by GC bug at
 MessagePack_Unpacker_mark.

---
 ruby/test/test_helper.rb |  1 +
 ruby/unpack.c            | 18 +++++++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb
index 19226ef..bf9fee8 100644
--- a/ruby/test/test_helper.rb
+++ b/ruby/test/test_helper.rb
@@ -5,3 +5,4 @@ rescue LoadError
 require File.dirname(__FILE__) + '/../lib/msgpack'
 end
 
+GC.stress = true
diff --git a/ruby/unpack.c b/ruby/unpack.c
index 151dbf4..0948151 100644
--- a/ruby/unpack.c
+++ b/ruby/unpack.c
@@ -287,6 +287,7 @@ static void MessagePack_Unpacker_mark(msgpack_unpack_t *mp)
 	unsigned int i;
 	rb_gc_mark(mp->user.stream);
 	rb_gc_mark(mp->user.streambuf);
+	rb_gc_mark_maybe(template_data(mp));
 	for(i=0; i < mp->top; ++i) {
 		rb_gc_mark(mp->stack[i].obj);
 		rb_gc_mark_maybe(mp->stack[i].map_key);
@@ -297,6 +298,17 @@ static VALUE MessagePack_Unpacker_alloc(VALUE klass)
 {
 	VALUE obj;
 	msgpack_unpack_t* mp = ALLOC_N(msgpack_unpack_t, 1);
+
+	// rb_gc_mark (not _maybe) is used for following member objects.
+	mp->user.stream = Qnil;
+	mp->user.streambuf = Qnil;
+
+	mp->user.finished = 0;
+	mp->user.offset = 0;
+	mp->user.buffer.size = 0;
+	mp->user.buffer.free = 0;
+	mp->user.buffer.ptr = NULL;
+
 	obj = Data_Wrap_Struct(klass, MessagePack_Unpacker_mark,
 			MessagePack_Unpacker_free, mp);
 	return obj;
@@ -343,14 +355,10 @@ static VALUE MessagePack_Unpacker_initialize(int argc, VALUE *argv, VALUE self)
 
 	UNPACKER(self, mp);
 	template_init(mp);
-	mp->user.finished = 0;
-	mp->user.offset = 0;
-	mp->user.buffer.size = 0;
-	mp->user.buffer.free = 0;
-	mp->user.buffer.ptr = NULL;
 	mp->user.stream = stream;
 	mp->user.streambuf = rb_str_buf_new(MSGPACK_UNPACKER_BUFFER_RESERVE_SIZE);
 	mp->user.stream_append_method = append_method_of(stream);
+
 	return self;
 }
 

From 123ae024c6d5c217f18a9444c61b292145227278 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 29 Jun 2010 15:12:52 +0900
Subject: [PATCH 077/152] ruby: MessagePack::VERSION constant

---
 ruby/extconf.rb               | 3 ++-
 ruby/msgpack.gemspec          | 3 ++-
 ruby/rbinit.c                 | 4 +++-
 ruby/test/test_helper.rb      | 2 +-
 ruby/test/test_pack_unpack.rb | 4 ++++
 ruby/version.rb               | 3 +++
 6 files changed, 15 insertions(+), 4 deletions(-)
 create mode 100644 ruby/version.rb

diff --git a/ruby/extconf.rb b/ruby/extconf.rb
index e6d4bd6..eb6a389 100644
--- a/ruby/extconf.rb
+++ b/ruby/extconf.rb
@@ -1,4 +1,5 @@
 require 'mkmf'
-$CFLAGS << " -I.. -Wall -O4"
+require './version.rb'
+$CFLAGS << %[ -I.. -Wall -O4 -DMESSAGEPACK_VERSION=\\"#{MessagePack::VERSION}\\"]
 create_makefile('msgpack')
 
diff --git a/ruby/msgpack.gemspec b/ruby/msgpack.gemspec
index fb6338a..95a2bd0 100644
--- a/ruby/msgpack.gemspec
+++ b/ruby/msgpack.gemspec
@@ -1,7 +1,8 @@
+require './version.rb'
 Gem::Specification.new do |s|
   s.platform = Gem::Platform::RUBY
   s.name = "msgpack"
-  s.version = "0.4.2"
+  s.version = MessagePack::VERSION
   s.summary = "MessagePack, a binary-based efficient data interchange format."
   s.author = "FURUHASHI Sadayuki"
   s.email = "frsyuki@users.sourceforge.jp"
diff --git a/ruby/rbinit.c b/ruby/rbinit.c
index ad51f6b..28a8bfe 100644
--- a/ruby/rbinit.c
+++ b/ruby/rbinit.c
@@ -43,7 +43,9 @@ static VALUE mMessagePack;
 void Init_msgpack(void)
 {
 	mMessagePack = rb_define_module("MessagePack");
+
+	rb_define_const(mMessagePack, "VERSION", rb_str_new2(MESSAGEPACK_VERSION));
+
 	Init_msgpack_unpack(mMessagePack);
 	Init_msgpack_pack(mMessagePack);
 }
-
diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb
index bf9fee8..80d7806 100644
--- a/ruby/test/test_helper.rb
+++ b/ruby/test/test_helper.rb
@@ -5,4 +5,4 @@ rescue LoadError
 require File.dirname(__FILE__) + '/../lib/msgpack'
 end
 
-GC.stress = true
+#GC.stress = true
diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb
index 9dff44f..25bde81 100644
--- a/ruby/test/test_pack_unpack.rb
+++ b/ruby/test/test_pack_unpack.rb
@@ -276,6 +276,10 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase
 		assert_equal(parsed, num)
 	end
 
+	it "MessagePack::VERSION constant" do
+		p MessagePack::VERSION
+	end
+
 	private
 	def check(len, obj)
 		v = obj.to_msgpack
diff --git a/ruby/version.rb b/ruby/version.rb
new file mode 100644
index 0000000..b156620
--- /dev/null
+++ b/ruby/version.rb
@@ -0,0 +1,3 @@
+module MessagePack
+	VERSION = "0.4.3"
+end

From 20de730541475516aa7a6361af1d1b5e4ea574b8 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 29 Jun 2010 15:39:47 +0900
Subject: [PATCH 078/152] ruby: 0.4.3

---
 ruby/ChangeLog  | 6 ++++++
 ruby/makegem.sh | 1 +
 2 files changed, 7 insertions(+)

diff --git a/ruby/ChangeLog b/ruby/ChangeLog
index e69de29..d3a7282 100644
--- a/ruby/ChangeLog
+++ b/ruby/ChangeLog
@@ -0,0 +1,6 @@
+
+2010-06-29 version 0.4.3:
+
+  * Adds MessagePack::VERSION constant
+  * Fixes SEGV problem caused by GC bug at MessagePack_Unpacker_mark
+
diff --git a/ruby/makegem.sh b/ruby/makegem.sh
index d21f06a..bf30cd4 100755
--- a/ruby/makegem.sh
+++ b/ruby/makegem.sh
@@ -8,6 +8,7 @@ cp pack.h       ext/
 cp rbinit.c     ext/
 cp unpack.c     ext/
 cp unpack.h     ext/
+cp version.rb   ext/
 cp ../msgpack/pack_define.h     msgpack/
 cp ../msgpack/pack_template.h   msgpack/
 cp ../msgpack/unpack_define.h   msgpack/

From 33a7d56042539282e3b02d75d365dc3dfa57266c Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Tue, 29 Jun 2010 11:59:56 +0200
Subject: [PATCH 079/152] * 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.

---
 erlang/msgpack.erl | 178 ++++++++++++++++-----------------------------
 1 file changed, 62 insertions(+), 116 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index e94262d..dc4907d 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -29,11 +29,13 @@
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
--type reason() ::  enomem | badarg | no_code_matches.
--type msgpack_term() :: [msgpack_term()] | {[{msgpack_term(),msgpack_term()}]} | integer() | float().
+-type reason() ::  enomem | {badarg, term()}.
+-type msgpack_term() :: [msgpack_term()] | {[{msgpack_term(),msgpack_term()}]} | integer() | float() | binary().
 
 % ===== external APIs ===== %
--spec pack(Term::msgpack_term()) -> binary().
+
+% @doc Pack one erlang term into an msgpack message.
+-spec pack(Term::msgpack_term()) -> binary() | reason().
 pack(O) when is_integer(O) andalso O < 0 ->
     pack_int_(O);
 pack(O) when is_integer(O) ->
@@ -54,17 +56,17 @@ pack({Map}) when is_list(Map) ->
     pack_map(Map);
 pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
     pack_map(dict:from_list(Map));
-pack(_O) ->
-    {error, undefined}.
+pack(Other) ->
+    {error, {badarg, Other}}.
 
-% unpacking.
-% if failed in decoding and not end, get more data
-% and feed more Bin into this function.
-% TODO: error case for imcomplete format when short for any type formats.
--spec unpack( binary() )->
-    {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
-unpack(Bin) ->
-    unpack_(Bin).
+% @doc Unpack one (possibly deeply nested) msgpack message into an erlang term.
+%      If failed in decoding and not end, get more data
+%      and feed more Bin into this function.
+-spec unpack( binary() ) -> {Decoded::msgpack_term(), Rest::binary()} | {more, undefined} | {error, reason()}.
+unpack(Bin) when is_binary(Bin) ->
+    unpack_(Bin);
+unpack(Other) ->
+    {error, {badarg, Other}}.
 
 -spec unpack_all( binary() ) -> [msgpack_term()].
 unpack_all(Data)->
@@ -134,7 +136,7 @@ pack_raw(Bin) ->
 	    << 16#DB:8, Len:32/big-unsigned-integer-unit:1, Bin/binary >>
     end.
 
-% list / tuple
+% list
 pack_array(L) ->
     case length(L) of
  	Len when Len < 16 ->
@@ -152,7 +154,7 @@ pack_array_([Head|Tail], Acc) ->
 unpack_array_(Remain, 0, RetList) -> {lists:reverse(RetList), Remain};
 unpack_array_(Bin, RestLen, RetList) ->
     case unpack(Bin) of
-	{more, Len} -> {more, Len+RestLen-1};
+	{more, undefined} -> {more, undefined};
 	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
     end.
 
@@ -172,120 +174,66 @@ pack_map_([{Key,Value}|Tail], Acc) ->
     pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
 
 % FIXME: write test for unpack_map/1
--spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}]) ->
-    {more, non_neg_integer()} | {any(), binary()}.
+-spec unpack_map_(binary(), non_neg_integer(), [{msgpack_term(), msgpack_term()}]) -> {more, undefined} | {any(), binary()} | {error, reason()}.
 unpack_map_(Bin,  0,  Acc) -> {{lists:reverse(Acc)}, Bin};
 unpack_map_(Bin, Len, Acc) ->
     case unpack(Bin) of
-	{ more, MoreLen } -> { more, MoreLen+Len-1 };
-	{ Key, Rest } ->
+	{more, undefined} -> {more, undefined};
+	{Key, Rest} ->
 	    case unpack(Rest) of
-		{more, MoreLen} -> { more, MoreLen+Len-1 };
-		{ Value, Rest2 } ->
+		{more, undefined} -> {more, undefined};
+		{Value, Rest2} ->
 		    unpack_map_(Rest2,Len-1,[{Key,Value}|Acc])
 	    end
     end.
 
 
--spec unpack_(Payload::binary()) -> {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
-unpack_(<<16#C0, Rest/binary>>) ->
-    {nil, Rest};
-unpack_(<<16#C2, Rest/binary>>) ->
-    {false, Rest};
-unpack_(<<16#C3, Rest/binary>>) ->
-    {true, Rest};
+-spec unpack_(Payload::binary()) -> {more, undefined} | {msgpack_term(), binary()} | {error, reason()}.
+% Atoms
+unpack_(<<16#C0, Rest/binary>>) -> {nil, Rest};
+unpack_(<<16#C2, Rest/binary>>) -> {false, Rest};
+unpack_(<<16#C3, Rest/binary>>) -> {true, Rest};
 
-unpack_(<<16#CA, Return:32/float-unit:1, Rest/binary>>) -> % 32bit float
-    {Return, Rest};
-unpack_(<<16#CA, Rest/binary>>) ->
-    {more, 4-byte_size(Rest)};
-unpack_(<<16#CB, Return:64/float-unit:1, Rest/binary>>) -> % 64bit float
-    {Return, Rest};
-unpack_(<<16#CB, Rest/binary>>) ->
-    {more, 8-byte_size(Rest)};
+% Floats
+unpack_(<<16#CA, Val:32/float-unit:1, Rest/binary>>) -> {Val, Rest};
+unpack_(<<16#CB, Val:64/float-unit:1, Rest/binary>>) -> {Val, Rest};
 
-unpack_(<<16#CC, Int:8/unsigned-integer, Rest/binary>>) -> % uint 8
-    {Int, Rest};
-unpack_(<<16#CC>>) ->
-    {more, 1};
-unpack_(<<16#CD, Int:16/big-unsigned-integer-unit:1, Rest/binary>>) -> % uint 16
-    {Int, Rest};
-unpack_(<<16#CD, Rest/binary>>) ->
-    {more, 2-byte_size(Rest)};
-unpack_(<<16#CE, Int:32/big-unsigned-integer-unit:1, Rest/binary>>) -> % uint 32
-    {Int, Rest};
-unpack_(<<16#CE, Rest/binary>>) ->
-    {more, 4-byte_size(Rest)};
-unpack_(<<16#CF, Int:64/big-unsigned-integer-unit:1, Rest/binary>>) -> % uint 64
-    {Int, Rest};
-unpack_(<<16#CF, Rest/binary>>) ->
-    {more, 8-byte_size(Rest)};
+% Unsigned integers
+unpack_(<<16#CC, Val:8/unsigned-integer, Rest/binary>>) ->             {Val, Rest};
+unpack_(<<16#CD, Val:16/big-unsigned-integer-unit:1, Rest/binary>>) -> {Val, Rest};
+unpack_(<<16#CE, Val:32/big-unsigned-integer-unit:1, Rest/binary>>) -> {Val, Rest};
+unpack_(<<16#CF, Val:64/big-unsigned-integer-unit:1, Rest/binary>>) -> {Val, Rest};
 
-unpack_(<<16#D0, Int:8/signed-integer, Rest/binary>>) -> % int 8
-    {Int, Rest};
-unpack_(<<16#D0>>) ->
-    {more, 1};
-unpack_(<<16#D1, Int:16/big-signed-integer-unit:1, Rest/binary>>) -> % int 16
-    {Int, Rest};
-unpack_(<<16#D1, Rest/binary>>) ->
-    {more, 2-byte_size(Rest)};
-unpack_(<<16#D2, Int:32/big-signed-integer-unit:1, Rest/binary>>) -> % int 32
-    {Int, Rest};
-unpack_(<<16#D2, Rest/binary>>) ->
-    {more, 4-byte_size(Rest)};
-unpack_(<<16#D3, Int:64/big-signed-integer-unit:1, Rest/binary>>) -> % int 64
-    {Int, Rest};
-unpack_(<<16#D3, Rest/binary>>) ->
-    {more, 8-byte_size(Rest)};
+% Signed integers
+unpack_(<<16#D0, Val:8/signed-integer, Rest/binary>>) ->             {Val, Rest};
+unpack_(<<16#D1, Val:16/big-signed-integer-unit:1, Rest/binary>>) -> {Val, Rest};
+unpack_(<<16#D2, Val:32/big-signed-integer-unit:1, Rest/binary>>) -> {Val, Rest};
+unpack_(<<16#D3, Val:64/big-signed-integer-unit:1, Rest/binary>>) -> {Val, Rest};
 
-unpack_(<<16#DA, Len:16/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> % raw 16
-    {Val, Rest};
-unpack_(<<16#DA, Rest/binary>>) ->
-    {more, 16-byte_size(Rest)};
-unpack_(<<16#DB, Len:32/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> % raw 32
-    {Val, Rest};
-unpack_(<<16#DB, Rest/binary>>) ->
-    {more, 32-byte_size(Rest)};
+% Raw bytes
+unpack_(<<16#DA, Len:16/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> {Val, Rest};
+unpack_(<<16#DB, Len:32/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>>) -> {Val, Rest};
 
-unpack_(<<16#DC, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> % array 16
-    unpack_array_(Rest, Len, []);
-unpack_(<<16#DC, Rest/binary>>) ->
-    {more, 2-byte_size(Rest)};
-unpack_(<<16#DD, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> % array 32
-    unpack_array_(Rest, Len, []);
-unpack_(<<16#DD, Rest/binary>>) ->
-    {more, 4-byte_size(Rest)};
+% Arrays
+unpack_(<<16#DC, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_array_(Rest, Len, []);
+unpack_(<<16#DD, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_array_(Rest, Len, []);
 
-unpack_(<<16#DE, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> % map 16
-    unpack_map_(Rest, Len, []);
-unpack_(<<16#DE, Rest/binary>>) ->
-    {more, 2-byte_size(Rest)};
-unpack_(<<16#DF, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> % map 32
-    unpack_map_(Rest, Len, []);
-unpack_(<<16#DF, Rest/binary>>) ->
-    {more, 4-byte_size(Rest)};
+% Maps
+unpack_(<<16#DE, Len:16/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_map_(Rest, Len, []);
+unpack_(<<16#DF, Len:32/big-unsigned-integer-unit:1, Rest/binary>>) -> unpack_map_(Rest, Len, []);
 
-unpack_(<<0:1, Value:7, Rest/binary>>) -> % positive fixnum
-    {Value, Rest};
-unpack_(<<2#111:3, Value:5, Rest/binary>>) -> % negative fixnum
-    {Value - 2#100000, Rest};
-unpack_(<<2#101:3, Len:5, Value:Len/binary, Rest/binary>>) -> % fixraw
-    {Value, Rest};
-unpack_(<<2#101:3, Len:5, Rest/binary>>) ->
-    {more, Len-byte_size(Rest)};
-unpack_(<<2#1001:4, Len:4, Rest/binary>>) -> % fixarray
-    unpack_array_(Rest, Len, []);
-unpack_(<<2#1000:4, Len:4, Rest/binary>>) -> % fixmap
-    unpack_map_(Rest, Len, []);
+% Tag-encoded lengths (kept last, for speed)
+unpack_(<<0:1, Val:7, Rest/binary>>) ->                      {Val, Rest};                  % pos fixnum
+unpack_(<<2#111:3, Val:5, Rest/binary>>) ->                  {Val - 2#100000, Rest};       % neg fixnum
+unpack_(<<2#101:3, Len:5, Val:Len/binary, Rest/binary>>) ->  {Val, Rest};                  % fixraw
+unpack_(<<2#1001:4, Len:4, Rest/binary>>) ->                 unpack_array_(Rest, Len, []); % fixarray
+unpack_(<<2#1000:4, Len:4, Rest/binary>>) ->                 unpack_map_(Rest, Len, []);   % fixmap
 
-%unpack_(<>) when F==16#C1; F==16#C4; F==16#C5; F==16#C6; F==16#C7; F==16#C8; F==16#C9; F==16#D5; F==16#D6; F==16#D7; F==16#D8; F==16#D9->
-%    {error, {badarg, <>}}.
-%unpack_(Other) when is_binary(Bin) ->
-%    {more, 1}.
-unpack_(<<>>) ->
-    {more, 1}.
-unpack_(Other) ->
-    {error, {badarg, Other}}.
+% Incomplete / invalid data
+unpack_(<>) when F==16#C1; F==16#C4; F==16#C5; F==16#C6; F==16#C7; F==16#C8; F==16#C9; F==16#D5; F==16#D6; F==16#D7; F==16#D8; F==16#D9->
+    {error, {badarg, <>}};
+unpack_(_Bin) ->
+    {more, undefined}.
 
 
 % ===== test codes ===== %
@@ -330,9 +278,7 @@ test_p(Len,Term,OrigBin,Len) ->
     {Term, <<>>}=msgpack:unpack(OrigBin);
 test_p(I,_,OrigBin,Len) when I < Len->
     <> = OrigBin,
-    {more, N}=msgpack:unpack(Bin),
-    ?assert(0 < N),
-    ?assert(N < Len).
+    ?assertEqual({more, undefined}, msgpack:unpack(Bin)).
 
 partial_test()-> % error handling test.
     Term = lists:seq(0, 45),
@@ -379,6 +325,6 @@ test_([S|Rest])->
     1+test_(Rest).
 
 other_test()->
-    {more,1}=msgpack:unpack(<<>>).
+    ?assertEqual({more,undefined}, msgpack:unpack(<<>>)).
 
 -endif.

From 83b4b7d83d0d32b2c30d26e51439b4dfec3127f9 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 1 Jul 2010 00:58:48 +0900
Subject: [PATCH 080/152] erlang: more suitable variable name and removing
 unnecessary guards.

---
 erlang/msgpack.erl | 75 +++++++++++++++++++++++-----------------------
 1 file changed, 38 insertions(+), 37 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index a168b55..c99002c 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -31,20 +31,24 @@
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
 -type reason() ::  enomem | badarg | no_code_matches.
--type msgpack_term() :: [msgpack_term()] | {[{msgpack_term(),msgpack_term()}]} | integer() | float().
+-type msgpack_term() :: [msgpack_term()]
+		      | {[{msgpack_term(),msgpack_term()}]}
+		      | integer() | float() | binary().
 
 % ===== external APIs ===== %
 -spec pack(Term::msgpack_term()) -> binary().
-pack(O) when is_integer(O) andalso O < 0 ->
-    pack_int_(O);
-pack(O) when is_integer(O) ->
-    pack_uint_(O);
-pack(O) when is_float(O) ->
-    pack_double(O);
+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) ->
-    pack_nil();
-pack(Bool) when is_atom(Bool) ->
-    pack_bool(Bool);
+    << 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) ->
@@ -53,7 +57,7 @@ pack({Map}) when is_list(Map) ->
     pack_map(Map);
 pack(Map) when is_tuple(Map), element(1,Map)=:=dict ->
     pack_map(dict:to_list(Map));
-pack(_O) ->
+pack(_Other) ->
     {error, undefined}.
 
 % unpacking.
@@ -92,53 +96,47 @@ pack_map(M)->
 % ===== internal APIs ===== %
 
 % positive fixnum
-pack_uint_(N) when is_integer( N ) , N < 128 ->
+pack_uint_(N) when N < 128 ->
     << 2#0:1, N:7 >>;
 % uint 8
-pack_uint_( N ) when is_integer( N ) andalso N < 256 ->
+pack_uint_(N) when N < 256 ->
     << 16#CC:8, N:8 >>;
 % uint 16
-pack_uint_( N ) when is_integer( N ) andalso N < 65536 ->
+pack_uint_(N) when N < 65536 ->
     << 16#CD:8, N:16/big-unsigned-integer-unit:1 >>;
 % uint 32
-pack_uint_( N ) when is_integer( N ) andalso N < 16#FFFFFFFF->
+pack_uint_(N) when N < 16#FFFFFFFF->
     << 16#CE:8, N:32/big-unsigned-integer-unit:1 >>;
 % uint 64
-pack_uint_( N ) when is_integer( N )->
+pack_uint_(N) ->
     << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>.
 
 % negative fixnum
-pack_int_( N ) when is_integer( N ) , N >= -32->
+pack_int_(N) when is_integer(N) , N >= -32->
     << 2#111:3, N:5 >>;
 % int 8
-pack_int_( N ) when is_integer( N ) , N > -128 ->
+pack_int_(N) when N > -128 ->
     << 16#D0:8, N:8/big-signed-integer-unit:1 >>;
 % int 16
-pack_int_( N ) when is_integer( N ), N > -32768 ->
+pack_int_(N) when N > -32768 ->
     << 16#D1:8, N:16/big-signed-integer-unit:1 >>;
 % int 32
-pack_int_( N ) when is_integer( N ), N > -16#FFFFFFFF ->
+pack_int_(N) when N > -16#FFFFFFFF ->
     << 16#D2:8, N:32/big-signed-integer-unit:1 >>;
 % int 64
-pack_int_( N ) when is_integer( N )->
+pack_int_(N) ->
     << 16#D3:8, N:64/big-signed-integer-unit:1 >>.
 
-% nil
-pack_nil()->    << 16#C0:8 >>.
-% pack_true / pack_false
-pack_bool(true)->    << 16#C3:8 >>;
-pack_bool(false)->   << 16#C2:8 >>.
-
 % 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) when is_float(F)->
+pack_double(F) ->
     << 16#CB:8, F:64/big-float-unit:1 >>.
 
 % raw bytes
-pack_raw(Bin) when is_binary(Bin)->
+pack_raw(Bin) ->
     case byte_size(Bin) of
 	Len when Len < 6->
 	    << 2#101:3, Len:5, Bin/binary >>;
@@ -149,7 +147,7 @@ pack_raw(Bin) when is_binary(Bin)->
     end.
 
 % list / tuple
-pack_array(L) when is_list(L)->
+pack_array(L) ->
     case length(L) of
  	Len when Len < 16 ->
  	    << 2#1001:4, Len:4/integer-unit:1, (pack_array_(L, <<>>))/binary >>;
@@ -165,10 +163,10 @@ pack_array_([Head|Tail], Acc) ->
 % FIXME! this should be tail-recursive and without lists:reverse/1
 unpack_array_(<<>>, 0, RetList)   -> {lists:reverse(RetList), <<>>};
 unpack_array_(Remain, 0, RetList) when is_binary(Remain)-> {lists:reverse(RetList), Remain};
-unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, RestLen};
+unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, undefined};
 unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
     case unpack(Bin) of
-	{more, Len} -> {more, Len+RestLen-1};
+	{more, Len} -> {more, undefined};
 	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
     end.
 
@@ -179,8 +177,8 @@ pack_map_([{Key,Value}|Tail], Acc) ->
 % FIXME: write test for unpack_map/1
 -spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
     {more, non_neg_integer()} | { any(), binary()}.
-unpack_map_(Bin,  0,  Acc) when is_binary(Bin) -> {{lists:reverse(Acc)}, Bin};
-unpack_map_(Bin, Len, Acc) when is_binary(Bin) and is_integer(Len) ->
+unpack_map_(Bin,  0,  Acc) -> {{lists:reverse(Acc)}, Bin};
+unpack_map_(Bin, Len, Acc) ->
     case unpack(Bin) of
 	{ more, MoreLen } -> { more, MoreLen+Len-1 };
 	{ Key, Rest } ->
@@ -371,9 +369,12 @@ test_p(Len,Term,OrigBin,Len) ->
     {Term, <<>>}=msgpack:unpack(OrigBin);
 test_p(I,_,OrigBin,Len) when I < Len->
     <> = OrigBin,
-    {more, N}=msgpack:unpack(Bin),
-    ?assert(0 < N),
-    ?assert(N < Len).
+    case msgpack:unpack(Bin) of
+	{more, N} when not is_integer(N) ->
+	    ?assertEqual(undefined, N);
+	{more, N} ->
+	    ?assert( N < Len )
+    end.
 
 partial_test()-> % error handling test.
     Term = lists:seq(0, 45),

From acb8fa613e87081267ec9963e5770580208e3e1f Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 1 Jul 2010 01:02:19 +0900
Subject: [PATCH 081/152] erlang: adding shorthand fix for {more, undefined}
 problem

---
 erlang/msgpack.erl | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index c99002c..d24220b 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -166,7 +166,7 @@ unpack_array_(Remain, 0, RetList) when is_binary(Remain)-> {lists:reverse(RetLis
 unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, undefined};
 unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
     case unpack(Bin) of
-	{more, Len} -> {more, undefined};
+	{more, _} -> {more, undefined};
 	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
     end.
 
@@ -180,11 +180,11 @@ pack_map_([{Key,Value}|Tail], Acc) ->
 unpack_map_(Bin,  0,  Acc) -> {{lists:reverse(Acc)}, Bin};
 unpack_map_(Bin, Len, Acc) ->
     case unpack(Bin) of
-	{ more, MoreLen } -> { more, MoreLen+Len-1 };
-	{ Key, Rest } ->
+	{more, _} -> {more, undefined};
+	{Key, Rest} ->
 	    case unpack(Rest) of
-		{more, MoreLen} -> { more, MoreLen+Len-1 };
-		{ Value, Rest2 } ->
+		{more, _} -> {more, undefined};
+		{Value, Rest2} ->
 		    unpack_map_(Rest2,Len-1,[{Key,Value}|Acc])
 	    end
     end.

From 2469768a85a067e5ba425d09ebd717cd0104449a Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 1 Jul 2010 01:07:56 +0900
Subject: [PATCH 082/152] erlang: reducing unnecessary binary matching in
 unpack_/2    * more efficient unpack_/1 by Vincent de Phille's code. thanks.

---
 erlang/msgpack.erl | 207 +++++++++++++++------------------------------
 1 file changed, 70 insertions(+), 137 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index d24220b..703dce2 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -69,10 +69,11 @@ pack(_Other) ->
 unpack(Bin) when not is_binary(Bin)->
     {error, badarg};
 unpack(Bin) when bit_size(Bin) >= 8 ->
-    << Flag:8/unsigned-integer, Payload/binary >> = Bin,
-    unpack_(Flag, Payload);
-unpack(<<>>)-> % when bit_size(Bin) < 8 ->
-    {more, 1}.
+    unpack_(Bin);
+unpack(<<>>)->
+    {more, 1};
+unpack(_) ->
+    {more, undefined}.
 
 -spec unpack_all( binary() ) -> [msgpack_term()].
 unpack_all(Data)->
@@ -83,6 +84,7 @@ unpack_all(Data)->
 	    [Term|unpack_all(Binary)]
     end.
 
+-spec pack_map(M::[{msgpack_term(),msgpack_term()}])-> binary().
 pack_map(M)->
     case length(M) of
 	Len when Len < 16 ->
@@ -189,143 +191,74 @@ unpack_map_(Bin, Len, Acc) ->
 	    end
     end.
 
-% {more, 
--spec unpack_(Flag::integer(), Payload::binary())->
-    {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
-unpack_(Flag, Payload)->
-    PayloadLen = byte_size(Payload),
-    case Flag of
-	16#C0 ->
-	    {nil, Payload};
-	16#C2 ->
-	    {false, Payload};
-	16#C3 ->
-	    {true, Payload};
+-spec unpack_(Payload::binary()) -> 
+		     {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
+unpack_(Binary)->
+    case Binary of
+% ATOMS
+	<<16#C0, Rest/binary>> -> {nil, Rest};
+	<<16#C2, Rest/binary>> -> {false, Rest};
+	<<16#C3, Rest/binary>> -> {true, Rest};
+% Floats
+	<<16#CA, Val:32/float-unit:1, Rest/binary>> ->   {Val, Rest};
+	<<16#CB, Val:64/float-unit:1, Rest/binary>> ->   {Val, Rest};
+% Unsigned integers
+	<<16#CC, Val:8/unsigned-integer, Rest/binary>> ->             {Val, Rest};
+	<<16#CD, Val:16/big-unsigned-integer-unit:1, Rest/binary>> -> {Val, Rest};
+	<<16#CE, Val:32/big-unsigned-integer-unit:1, Rest/binary>> -> {Val, Rest};
+	<<16#CF, Val:64/big-unsigned-integer-unit:1, Rest/binary>> -> {Val, Rest};
+% Signed integers
+	<<16#D0, Val:8/signed-integer, Rest/binary>> ->             {Val, Rest};
+	<<16#D1, Val:16/big-signed-integer-unit:1, Rest/binary>> -> {Val, Rest};
+	<<16#D2, Val:32/big-signed-integer-unit:1, Rest/binary>> -> {Val, Rest};
+	<<16#D3, Val:64/big-signed-integer-unit:1, Rest/binary>> -> {Val, Rest};
+% Raw bytes
+	<<16#DA, Len:16/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>> -> {Val, Rest};
+	<<16#DB, Len:32/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>> -> {Val, Rest};
+% Arrays
+	<<16#DC, Len:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, Len, []);
+	<<16#DD, Len:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, Len, []);
+% Maps
+	<<16#DE, Len:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, Len, []);
+	<<16#DF, Len:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, Len, []);
 
-	16#CA when PayloadLen >= 4 -> % 32bit float
-	    << Return:32/float-unit:1, Rest/binary >> = Payload,
-	    {Return, Rest};
-	16#CA ->
-	    {more, 4-PayloadLen}; % at least more
+% Tag-encoded lengths (kept last, for speed)
+	<<0:1, Val:7, Rest/binary>> ->                      {Val, Rest};                  % positive int
+	<<2#111:3, Val:5, Rest/binary>> ->                  {Val - 2#100000, Rest};       % negative int
+	<<2#101:3, Len:5, Val:Len/binary, Rest/binary>> ->  {Val, Rest};                  % raw bytes
+	<<2#1001:4, Len:4, Rest/binary>> ->                 unpack_array_(Rest, Len, []); % array
+	<<2#1000:4, Len:4, Rest/binary>> ->                 unpack_map_(Rest, Len, []);   % map
+	
+% Incomplete / invalid data
+	<<16#CA, Rest/binary>> -> {more, 4-byte_size(Rest)};
+	<<16#CB, Rest/binary>> -> {more, 8-byte_size(Rest)};
+	<<16#CC>> ->              {more, 1};
+	<<16#CD, Rest/binary>> -> {more, 2-byte_size(Rest)};
+	<<16#CE, Rest/binary>> -> {more, 4-byte_size(Rest)};
+	<<16#CF, Rest/binary>> -> {more, 8-byte_size(Rest)};
+	<<16#D0>> ->              {more, 1};
+	<<16#D1, Rest/binary>> -> {more, 2-byte_size(Rest)};
+	<<16#D2, Rest/binary>> -> {more, 4-byte_size(Rest)};
+	<<16#D3, Rest/binary>> -> {more, 8-byte_size(Rest)};
+	<<16#DA, Rest/binary>> -> {more, 16-byte_size(Rest)};
+	<<16#DB, Rest/binary>> -> {more, 32-byte_size(Rest)};
+	<<16#DC, Rest/binary>> -> {more, 2-byte_size(Rest)};
+	<<16#DD, Rest/binary>> -> {more, 4-byte_size(Rest)};
+	<<16#DE, Rest/binary>> -> {more, 2-byte_size(Rest)};
+	<<16#DF, Rest/binary>> -> {more, 4-byte_size(Rest)};
+	<<2#101:3, Len:5, Rest/binary>> ->  {more, Len-byte_size(Rest)};
 
-	16#CB when PayloadLen >= 8 -> % 64bit float
-	    << Return:64/float-unit:1, Rest/binary >> = Payload,
-	    {Return, Rest};
-	16#CB ->
-	    {more, 8-PayloadLen};
-
-	16#CC when PayloadLen >= 1 -> % uint 8
-	    << Int:8/unsigned-integer, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CC ->
-	    {more, 1};
-
-	16#CD when PayloadLen >= 2 -> % uint 16
-	    << Int:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CD ->
-	    {more, 2-PayloadLen};
-
-	16#CE when PayloadLen >= 4 ->
-	    << Int:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CE ->
-	    {more, 4-PayloadLen}; % at least more
-
-	16#CF when PayloadLen >= 8 ->
-	    << Int:64/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#CF ->
-	    {more, 8-PayloadLen};
-
-	16#D0 when PayloadLen >= 1 -> % int 8
-	    << Int:8/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D0 ->
-	    {more, 1};
-
-	16#D1 when PayloadLen >= 2 -> % int 16
-	    << Int:16/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D1 ->
-	    {more, 2-PayloadLen};
-
-	16#D2 when PayloadLen >= 4 -> % int 32
-	    << Int:32/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D2 ->
-	    {more, 4-PayloadLen};
-
-	16#D3 when PayloadLen >= 8 -> % int 64
-	    << Int:64/big-signed-integer-unit:1, Rest/binary >> = Payload,
-	    {Int, Rest};
-	16#D3 ->
-	    {more, 8-PayloadLen};
-
-	16#DA when PayloadLen >= 2 -> % raw 16
-	    << Len:16/unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    << Return:Len/binary, Remain/binary >> = Rest,
-	    {Return, Remain};
-	16#DA ->
-	    {more, 16-PayloadLen};
-
-	16#DB when PayloadLen >= 4 -> % raw 32
-	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    << Return:Len/binary, Remain/binary >> = Rest,
-	    {Return, Remain};
-	16#DB ->
-	    {more, 4-PayloadLen};
-
-	16#DC when PayloadLen >= 2 -> % array 16
-	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_array_(Rest, Len, []);
-	16#DC ->
-	    {more, 2-PayloadLen};
-
-	16#DD when PayloadLen >= 4 -> % array 32
-	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_array_(Rest, Len, []);
-	16#DD ->
-	    {more, 4-PayloadLen};
-
-	16#DE when PayloadLen >= 2 -> % map 16
-	    << Len:16/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, []);
-	16#DE ->
-	    {more, 2-PayloadLen};
-
-	16#DF when PayloadLen >= 4 -> % map 32
-	    << Len:32/big-unsigned-integer-unit:1, Rest/binary >> = Payload,
-	    unpack_map_(Rest, Len, []);
-
-	% positive fixnum
-	Code when Code >= 2#00000000, Code < 2#10000000->
-	    {Code, Payload};
-
-	% negative fixnum
-	Code when Code >= 2#11100000 ->
-	    {(Code - 16#100), Payload};
-
-	Code when Code >= 2#10100000 , Code < 2#11000000 ->
-%	 101XXXXX for FixRaw
-	    Len = Code rem 2#10100000,
-	    << Return:Len/binary, Remain/binary >> = Payload,
-	    {Return, Remain};
-
-	Code when Code >= 2#10010000 , Code < 2#10100000 ->
-%        1001XXXX for FixArray
-	    Len = Code rem 2#10010000,
-	    unpack_array_(Payload, Len, []);
-
-	Code when Code >= 2#10000000 , Code < 2#10010000 ->
-%        1000XXXX for FixMap
-	    Len = Code rem 2#10000000,
-	    unpack_map_(Payload, Len, []);
-
-	_Other ->
-	    {error, no_code_matches}
+	<<>> ->                  {more, 1};
+	<<2#101:3, _/binary>> -> {more, undefined};
+	<> when F==16#C1;
+				  F==16#C7; F==16#C8; F==16#C9; F==16#D5;
+				  F==16#D6; F==16#D7; F==16#D8; F==16#D9->
+	    {error, {badarg, <>}};
+	Other ->
+	    {error, {badarg, Other}}
     end.
 
+
 % ===== test codes ===== %
 -include_lib("eunit/include/eunit.hrl").
 -ifdef(EUNIT).

From 370e92b1a6d79cd6d65840cae588ded11b167fce Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 1 Jul 2010 01:14:20 +0900
Subject: [PATCH 083/152] erlang: just a golf.

---
 erlang/msgpack.erl | 44 ++++++++++++++++++++++----------------------
 1 file changed, 22 insertions(+), 22 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 703dce2..6d94a23 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -200,34 +200,34 @@ unpack_(Binary)->
 	<<16#C2, Rest/binary>> -> {false, Rest};
 	<<16#C3, Rest/binary>> -> {true, Rest};
 % Floats
-	<<16#CA, Val:32/float-unit:1, Rest/binary>> ->   {Val, Rest};
-	<<16#CB, Val:64/float-unit:1, Rest/binary>> ->   {Val, Rest};
+	<<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, Val:8/unsigned-integer, Rest/binary>> ->             {Val, Rest};
-	<<16#CD, Val:16/big-unsigned-integer-unit:1, Rest/binary>> -> {Val, Rest};
-	<<16#CE, Val:32/big-unsigned-integer-unit:1, Rest/binary>> -> {Val, Rest};
-	<<16#CF, Val:64/big-unsigned-integer-unit:1, Rest/binary>> -> {Val, Rest};
+	<<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, Val:8/signed-integer, Rest/binary>> ->             {Val, Rest};
-	<<16#D1, Val:16/big-signed-integer-unit:1, Rest/binary>> -> {Val, Rest};
-	<<16#D2, Val:32/big-signed-integer-unit:1, Rest/binary>> -> {Val, Rest};
-	<<16#D3, Val:64/big-signed-integer-unit:1, Rest/binary>> -> {Val, Rest};
+	<<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, Len:16/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>> -> {Val, Rest};
-	<<16#DB, Len:32/unsigned-integer-unit:1, Val:Len/binary, Rest/binary>> -> {Val, Rest};
+	<<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, Len:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, Len, []);
-	<<16#DD, Len:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_array_(Rest, Len, []);
+	<<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, Len:16/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, Len, []);
-	<<16#DF, Len:32/big-unsigned-integer-unit:1, Rest/binary>> -> unpack_map_(Rest, Len, []);
+	<<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, Val:7, Rest/binary>> ->                      {Val, Rest};                  % positive int
-	<<2#111:3, Val:5, Rest/binary>> ->                  {Val - 2#100000, Rest};       % negative int
-	<<2#101:3, Len:5, Val:Len/binary, Rest/binary>> ->  {Val, Rest};                  % raw bytes
-	<<2#1001:4, Len:4, Rest/binary>> ->                 unpack_array_(Rest, Len, []); % array
-	<<2#1000:4, Len:4, Rest/binary>> ->                 unpack_map_(Rest, Len, []);   % map
+	<<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
 	
 % Incomplete / invalid data
 	<<16#CA, Rest/binary>> -> {more, 4-byte_size(Rest)};
@@ -246,7 +246,7 @@ unpack_(Binary)->
 	<<16#DD, Rest/binary>> -> {more, 4-byte_size(Rest)};
 	<<16#DE, Rest/binary>> -> {more, 2-byte_size(Rest)};
 	<<16#DF, Rest/binary>> -> {more, 4-byte_size(Rest)};
-	<<2#101:3, Len:5, Rest/binary>> ->  {more, Len-byte_size(Rest)};
+	<<2#101:3, L:5, Rest/binary>> ->  {more, L-byte_size(Rest)};
 
 	<<>> ->                  {more, 1};
 	<<2#101:3, _/binary>> -> {more, undefined};

From ff5d5d7cbcb7e136370f3450d67b06244d5c3bc7 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 1 Jul 2010 01:14:38 +0900
Subject: [PATCH 084/152] erlang: updated the comments

---
 erlang/msgpack.erl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 6d94a23..8a54271 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -162,7 +162,7 @@ pack_array_([], Acc) -> Acc;
 pack_array_([Head|Tail], Acc) ->
     pack_array_(Tail, <>).
 
-% FIXME! this should be tail-recursive and without lists:reverse/1
+% FIXME! this should be without lists:reverse/1
 unpack_array_(<<>>, 0, RetList)   -> {lists:reverse(RetList), <<>>};
 unpack_array_(Remain, 0, RetList) when is_binary(Remain)-> {lists:reverse(RetList), Remain};
 unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, undefined};
@@ -176,7 +176,7 @@ pack_map_([], Acc) -> Acc;
 pack_map_([{Key,Value}|Tail], Acc) ->
     pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
 
-% FIXME: write test for unpack_map/1
+% FIXME! this should be without lists:reverse/1
 -spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
     {more, non_neg_integer()} | { any(), binary()}.
 unpack_map_(Bin,  0,  Acc) -> {{lists:reverse(Acc)}, Bin};

From 584462f9b9efefa25e93600024c3d9680923b3a4 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 1 Jul 2010 01:16:25 +0900
Subject: [PATCH 085/152] erlang: improved spec.

---
 erlang/msgpack.erl | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 8a54271..aab07b7 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -64,8 +64,9 @@ pack(_Other) ->
 % if failed in decoding and not end, get more data
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
--spec unpack( binary() )->
-    {msgpack_term(), binary()} | {more, non_neg_integer()} | {error, reason()}.
+-spec unpack( Bin::binary() )-> {msgpack_term(), binary()} |
+				{more, non_neg_integer()} | {more, undefined} |
+				{error, reason()}.
 unpack(Bin) when not is_binary(Bin)->
     {error, badarg};
 unpack(Bin) when bit_size(Bin) >= 8 ->

From 71dd44f4308dbdda2d087130452182093262ee01 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 12:26:21 +0900
Subject: [PATCH 086/152] cpp: adds operator<<(std::ostream&, const
 tuple&) (experimental)

---
 cpp/src/msgpack/type/tuple.hpp.erb | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/cpp/src/msgpack/type/tuple.hpp.erb b/cpp/src/msgpack/type/tuple.hpp.erb
index 0d9ae91..ebef816 100644
--- a/cpp/src/msgpack/type/tuple.hpp.erb
+++ b/cpp/src/msgpack/type/tuple.hpp.erb
@@ -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 A<%=j%><%}%>>
+//inline std::ostream& operator<< (std::ostream& o,
+//		const msgpack::type::tuple, A<%=j%><%}%>>& v) {
+//	return o << "["
+//	<%0.upto(i) {|j|%>
+//	<<<%if j != 0 then%> ", " <<<%end%> v.template get<<%=j%>>()<%}%>
+//	<< "]";
+//}
+//<%}%>
+
 #endif /* msgpack/type/tuple.hpp */
 

From 3af10a1d000882f338529360bf35ae0e3a21ffc4 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 17:00:58 +0900
Subject: [PATCH 087/152] cpp: adds MSGPACK_VERSION{,_MAJOR,_MINOR} macros and
 msgpack{,_major,_minor} functions

---
 cpp/Makefile.am              |  1 -
 cpp/configure.in             |  5 +++++
 cpp/src/Makefile.am          | 15 ++++++++++----
 cpp/src/msgpack.h            |  2 ++
 cpp/src/msgpack/version.h.in | 40 ++++++++++++++++++++++++++++++++++++
 cpp/src/version.c            | 17 +++++++++++++++
 cpp/test/Makefile.am         |  3 +++
 7 files changed, 78 insertions(+), 5 deletions(-)
 create mode 100644 cpp/src/msgpack/version.h.in
 create mode 100644 cpp/src/version.c

diff --git a/cpp/Makefile.am b/cpp/Makefile.am
index 871ff8c..7dd4891 100644
--- a/cpp/Makefile.am
+++ b/cpp/Makefile.am
@@ -12,7 +12,6 @@ EXTRA_DIST = \
 		$(DOC_FILES)
 
 doxygen:
-	./preprocess
 	./preprocess clean
 	cd src && $(MAKE) doxygen
 	./preprocess
diff --git a/cpp/configure.in b/cpp/configure.in
index 0895be4..ffe3671 100644
--- a/cpp/configure.in
+++ b/cpp/configure.in
@@ -54,5 +54,10 @@ add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as f
 ])
 fi
 
+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 test/Makefile])
 
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index 6b6457e..78d14aa 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -4,6 +4,7 @@ lib_LTLIBRARIES = libmsgpack.la
 libmsgpack_la_SOURCES = \
 		unpack.c \
 		objectc.c \
+		version.c \
 		vrefbuffer.c \
 		zone.c \
 		object.cpp
@@ -18,15 +19,12 @@ 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
 
-# work around for duplicated file name
-kumo_manager_CFLAGS = $(AM_CFLAGS)
-kumo_manager_CXXFLAGS = $(AM_CXXFLAGS)
-
 
 nobase_include_HEADERS = \
 		msgpack/pack_define.h \
@@ -44,6 +42,7 @@ nobase_include_HEADERS = \
 		msgpack/zone.h \
 		msgpack.hpp \
 		msgpack/sbuffer.hpp \
+		msgpack/version.h \
 		msgpack/vrefbuffer.hpp \
 		msgpack/zbuffer.hpp \
 		msgpack/pack.hpp \
@@ -69,11 +68,19 @@ nobase_include_HEADERS = \
 		msgpack/type/tr1/unordered_set.hpp
 
 EXTRA_DIST = \
+		msgpack/version.h.in \
 		msgpack/zone.hpp.erb \
 		msgpack/type/define.hpp.erb \
 		msgpack/type/tuple.hpp.erb
 
 
+msgpack/version.h: msgpack/version.h.in Makefile.in
+	sed -e s/VERSION_UNDEFINED/$(VERSION)/ \
+		-e s/VERSION_MAJOR_UNDEFINED/$(VERSION_MAJOR)/ \
+		-e s/VERSION_MINOR_UNDEFINED/$(VERSION_MINOR)/ \
+		$< > $@
+
+
 doxygen_c:
 	cat ../Doxyfile > Doxyfile_c
 	echo "FILE_PATTERNS      = *.h" >> Doxyfile_c
diff --git a/cpp/src/msgpack.h b/cpp/src/msgpack.h
index 0cd8a19..08f2768 100644
--- a/cpp/src/msgpack.h
+++ b/cpp/src/msgpack.h
@@ -26,3 +26,5 @@
 #include "msgpack/unpack.h"
 #include "msgpack/sbuffer.h"
 #include "msgpack/vrefbuffer.h"
+#include "msgpack/version.h"
+
diff --git a/cpp/src/msgpack/version.h.in b/cpp/src/msgpack/version.h.in
new file mode 100644
index 0000000..af292d0
--- /dev/null
+++ b/cpp/src/msgpack/version.h.in
@@ -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_UNDEFINED"
+#define MSGPACK_VERSION_MAJOR VERSION_MAJOR_UNDEFINED
+#define MSGPACK_VERSION_MINOR VERSION_MINOR_UNDEFINED
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* msgpack/version.h */
+
diff --git a/cpp/src/version.c b/cpp/src/version.c
new file mode 100644
index 0000000..3d956f1
--- /dev/null
+++ b/cpp/src/version.c
@@ -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;
+}
+
diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am
index 1f52215..bb9439e 100644
--- a/cpp/test/Makefile.am
+++ b/cpp/test/Makefile.am
@@ -13,6 +13,7 @@ check_PROGRAMS = \
 		convert \
 		buffer \
 		cases \
+		version \
 		msgpackc_test \
 		msgpack_test
 
@@ -37,6 +38,8 @@ buffer_LDADD = -lz
 
 cases_SOURCES = cases.cc
 
+version_SOURCES = version.cc
+
 msgpackc_test_SOURCES = msgpackc_test.cpp
 
 msgpack_test_SOURCES = msgpack_test.cpp

From c57f6161415156328bd7039be44895b868ee6f76 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 17:10:25 +0900
Subject: [PATCH 088/152] cpp: adds MSGPACK_VERSION{,_MAJOR,_MINOR} macros and
 msgpack{,_major,_minor} functions

---
 cpp/src/Makefile.am |  2 ++
 cpp/test/version.cc | 13 +++++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 cpp/test/version.cc

diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index 78d14aa..79b692d 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -80,6 +80,8 @@ msgpack/version.h: msgpack/version.h.in Makefile.in
 		-e s/VERSION_MINOR_UNDEFINED/$(VERSION_MINOR)/ \
 		$< > $@
 
+#version.c: msgpack/version.h
+
 
 doxygen_c:
 	cat ../Doxyfile > Doxyfile_c
diff --git a/cpp/test/version.cc b/cpp/test/version.cc
new file mode 100644
index 0000000..9357271
--- /dev/null
+++ b/cpp/test/version.cc
@@ -0,0 +1,13 @@
+#include 
+#include 
+
+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());
+}
+

From a2bd5ae6386af95617635fec1503640cbadb627b Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 17:45:15 +0900
Subject: [PATCH 089/152] cpp: ./configure supports --disable-cxx option not to
 build/install C++ API

---
 cpp/configure.in    | 27 ++++++++++++++++++---------
 cpp/src/Makefile.am | 14 ++++++++++----
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/cpp/configure.in b/cpp/configure.in
index ffe3671..04fa8e3 100644
--- a/cpp/configure.in
+++ b/cpp/configure.in
@@ -9,23 +9,31 @@ CFLAGS="-O4 -Wall $CFLAGS"
 AC_SUBST(CXXFLAGS)
 CXXFLAGS="-O4 -Wall $CXXFLAGS"
 
+
 AC_PROG_CC
-AC_PROG_CXX
+
+
+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.]) )
+				   [disable assert macros and omit -g option]) )
+AC_MSG_RESULT([$enable_debug])
 if test "$enable_debug" != "no"; then
 	CXXFLAGS="$CXXFLAGS -g"
 	CFLAGS="$CFLAGS -g"
@@ -33,7 +41,6 @@ else
 	CXXFLAGS="$CXXFLAGS -DNDEBUG"
 	CFLAGS="$CFLAGS -DNDEBUG"
 fi
-AC_MSG_RESULT($enable_debug)
 
 
 AC_CACHE_CHECK([for __sync_* atomic operations], msgpack_cv_atomic_ops, [
@@ -54,10 +61,12 @@ add CFLAGS="--march=i686" and CXXFLAGS="-march=i686" options to ./configure as f
 ])
 fi
 
+
 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 test/Makefile])
 
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index 79b692d..fc67385 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -6,8 +6,12 @@ libmsgpack_la_SOURCES = \
 		objectc.c \
 		version.c \
 		vrefbuffer.c \
-		zone.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
@@ -39,7 +43,10 @@ nobase_include_HEADERS = \
 		msgpack/pack.h \
 		msgpack/unpack.h \
 		msgpack/object.h \
-		msgpack/zone.h \
+		msgpack/zone.h
+
+if ENABLE_CXX
+nobase_include_HEADERS += \
 		msgpack.hpp \
 		msgpack/sbuffer.hpp \
 		msgpack/version.h \
@@ -66,6 +73,7 @@ nobase_include_HEADERS = \
 		msgpack/type/define.hpp \
 		msgpack/type/tr1/unordered_map.hpp \
 		msgpack/type/tr1/unordered_set.hpp
+endif
 
 EXTRA_DIST = \
 		msgpack/version.h.in \
@@ -80,8 +88,6 @@ msgpack/version.h: msgpack/version.h.in Makefile.in
 		-e s/VERSION_MINOR_UNDEFINED/$(VERSION_MINOR)/ \
 		$< > $@
 
-#version.c: msgpack/version.h
-
 
 doxygen_c:
 	cat ../Doxyfile > Doxyfile_c

From 39facd5dc660b90304e4d3d593244a5b9f7cd6f0 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 17:59:07 +0900
Subject: [PATCH 090/152] cpp: version 0.5.1

---
 cpp/ChangeLog    | 10 ++++++++++
 cpp/configure.in |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/cpp/ChangeLog b/cpp/ChangeLog
index 53a9991..3277c13 100644
--- a/cpp/ChangeLog
+++ b/cpp/ChangeLog
@@ -1,4 +1,14 @@
 
+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.
diff --git a/cpp/configure.in b/cpp/configure.in
index 04fa8e3..dd04ed4 100644
--- a/cpp/configure.in
+++ b/cpp/configure.in
@@ -1,6 +1,6 @@
 AC_INIT(src/object.cpp)
 AC_CONFIG_AUX_DIR(ac)
-AM_INIT_AUTOMAKE(msgpack, 0.5.0)
+AM_INIT_AUTOMAKE(msgpack, 0.5.1)
 AC_CONFIG_HEADER(config.h)
 
 AC_SUBST(CFLAGS)

From 0c331d288715b156f262cdb7841fb0dba4dc5d83 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 18:18:28 +0900
Subject: [PATCH 091/152] cpp: updates vcproj

---
 cpp/msgpack_vc8.postbuild.bat | 77 ++++++++++++++++++-----------------
 cpp/msgpack_vc8.vcproj        | 40 +++++++++++++-----
 cpp/src/Makefile.am           |  2 +-
 3 files changed, 70 insertions(+), 49 deletions(-)

diff --git a/cpp/msgpack_vc8.postbuild.bat b/cpp/msgpack_vc8.postbuild.bat
index bae13f3..20fabbb 100644
--- a/cpp/msgpack_vc8.postbuild.bat
+++ b/cpp/msgpack_vc8.postbuild.bat
@@ -2,42 +2,43 @@ 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\
+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\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\deque.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\
 
diff --git a/cpp/msgpack_vc8.vcproj b/cpp/msgpack_vc8.vcproj
index 5804790..ed0daa4 100644
--- a/cpp/msgpack_vc8.vcproj
+++ b/cpp/msgpack_vc8.vcproj
@@ -157,7 +157,7 @@
 			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
 			>
 			
 				
 			
 			
 				
 			
 			
 				
 			
 			
 				
 			
 			
+				
+					
+				
+				
+					
+				
+			
+			
 			
 		
@@ -247,23 +267,23 @@
 			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
 			>
 			
 			
 			
 			
 			
 			
 			
 			
 			
 			
 		
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index fc67385..a6910b4 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -38,6 +38,7 @@ nobase_include_HEADERS = \
 		msgpack/sysdep.h \
 		msgpack.h \
 		msgpack/sbuffer.h \
+		msgpack/version.h \
 		msgpack/vrefbuffer.h \
 		msgpack/zbuffer.h \
 		msgpack/pack.h \
@@ -49,7 +50,6 @@ if ENABLE_CXX
 nobase_include_HEADERS += \
 		msgpack.hpp \
 		msgpack/sbuffer.hpp \
-		msgpack/version.h \
 		msgpack/vrefbuffer.hpp \
 		msgpack/zbuffer.hpp \
 		msgpack/pack.hpp \

From fe77251242f34e08f49f41fbbb2561e9278d8635 Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 19:16:49 +0900
Subject: [PATCH 092/152] cpp: fixes missing dependency to generate version.h

---
 cpp/src/Makefile.am | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index a6910b4..e12eb24 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -88,6 +88,8 @@ msgpack/version.h: msgpack/version.h.in Makefile.in
 		-e s/VERSION_MINOR_UNDEFINED/$(VERSION_MINOR)/ \
 		$< > $@
 
+version.c: msgpack/version.h
+
 
 doxygen_c:
 	cat ../Doxyfile > Doxyfile_c

From 167e2475d89cb867428b9e7b5aac7f269fd95ecb Mon Sep 17 00:00:00 2001
From: frsyuki 
Date: Tue, 6 Jul 2010 23:30:15 +0900
Subject: [PATCH 093/152] cpp: generate version.h using AC_OUTPUT macro in
 ./configure

---
 cpp/configure.in             | 5 ++++-
 cpp/src/Makefile.am          | 9 ---------
 cpp/src/msgpack/version.h.in | 6 +++---
 3 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/cpp/configure.in b/cpp/configure.in
index dd04ed4..ab29501 100644
--- a/cpp/configure.in
+++ b/cpp/configure.in
@@ -68,5 +68,8 @@ AC_SUBST(VERSION_MAJOR, $major)
 AC_SUBST(VERSION_MINOR, $minor)
 
 
-AC_OUTPUT([Makefile src/Makefile test/Makefile])
+AC_OUTPUT([Makefile
+		   src/Makefile
+		   src/msgpack/version.h
+		   test/Makefile])
 
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index e12eb24..31096f0 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -82,15 +82,6 @@ EXTRA_DIST = \
 		msgpack/type/tuple.hpp.erb
 
 
-msgpack/version.h: msgpack/version.h.in Makefile.in
-	sed -e s/VERSION_UNDEFINED/$(VERSION)/ \
-		-e s/VERSION_MAJOR_UNDEFINED/$(VERSION_MAJOR)/ \
-		-e s/VERSION_MINOR_UNDEFINED/$(VERSION_MINOR)/ \
-		$< > $@
-
-version.c: msgpack/version.h
-
-
 doxygen_c:
 	cat ../Doxyfile > Doxyfile_c
 	echo "FILE_PATTERNS      = *.h" >> Doxyfile_c
diff --git a/cpp/src/msgpack/version.h.in b/cpp/src/msgpack/version.h.in
index af292d0..f1feb33 100644
--- a/cpp/src/msgpack/version.h.in
+++ b/cpp/src/msgpack/version.h.in
@@ -27,9 +27,9 @@ const char* msgpack_version(void);
 int msgpack_version_major(void);
 int msgpack_version_minor(void);
 
-#define MSGPACK_VERSION "VERSION_UNDEFINED"
-#define MSGPACK_VERSION_MAJOR VERSION_MAJOR_UNDEFINED
-#define MSGPACK_VERSION_MINOR VERSION_MINOR_UNDEFINED
+#define MSGPACK_VERSION "@VERSION@"
+#define MSGPACK_VERSION_MAJOR @VERSION_MAJOR@
+#define MSGPACK_VERSION_MINOR @VERSION_MINOR@
 
 
 #ifdef __cplusplus

From 45fb482ab42c7f47bf65313ade6808d0cfe3bca5 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 8 Jul 2010 23:36:18 +0900
Subject: [PATCH 094/152] erlang: added simple performance test.

---
 erlang/msgpack.erl | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index aab07b7..ee99311 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -359,4 +359,9 @@ test_([Before|Rest])->
 other_test()->
     {more,1}=msgpack:unpack(<<>>).
 
+benchmark_test()->
+    Data=[test_data() || _ <- lists:seq(0, 10000)],
+    S=?debugTime("  serialize", msgpack:pack(Data)),
+    {Data,<<>>}=?debugTime("deserialize", msgpack:unpack(S)).
+
 -endif.

From 485915c27a3ddf12e4ba4c9c0e27769869bb945c Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Thu, 8 Jul 2010 23:39:47 +0900
Subject: [PATCH 095/152] erlang: added simple performance test description.

---
 erlang/msgpack.erl | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index ee99311..94fed86 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -362,6 +362,7 @@ other_test()->
 benchmark_test()->
     Data=[test_data() || _ <- lists:seq(0, 10000)],
     S=?debugTime("  serialize", msgpack:pack(Data)),
-    {Data,<<>>}=?debugTime("deserialize", msgpack:unpack(S)).
+    {Data,<<>>}=?debugTime("deserialize", msgpack:unpack(S)),
+    ?debugFmt("for ~p KB test data.", [byte_size(S) div 1024]).
 
 -endif.

From eab66a022e5b5fd9c4731ae8ba970b2146e27599 Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Fri, 9 Jul 2010 01:04:09 +0900
Subject: [PATCH 096/152] erlang: added try-catch clause for easy error
 handling

---
 erlang/msgpack.erl | 220 +++++++++++++++++++++++----------------------
 1 file changed, 112 insertions(+), 108 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 94fed86..d4fd0ba 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -19,9 +19,8 @@
 -author('kuenishi+msgpack@gmail.com').
 
 %% tuples, atoms are not supported. lists, integers, double, and so on.
-%% see http://msgpack.sourceforge.jp/spec for
-%% supported formats. APIs are almost compatible
-%% for C API (http://msgpack.sourceforge.jp/c:doc)
+%% see http://msgpack.sourceforge.jp/spec for supported formats.
+%% APIs are almost compatible with C API (http://msgpack.sourceforge.jp/c:doc)
 %% except buffering functions (both copying and zero-copying).
 -export([pack/1, unpack/1, unpack_all/1]).
 -export([pack_map/1]).
@@ -30,51 +29,38 @@
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
--type reason() ::  enomem | badarg | no_code_matches.
+-type reason() ::  enomem | badarg | no_code_matches | undefined.
 -type msgpack_term() :: [msgpack_term()]
 		      | {[{msgpack_term(),msgpack_term()}]}
 		      | integer() | float() | binary().
 
 % ===== external APIs ===== %
--spec pack(Term::msgpack_term()) -> binary().
-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(Map) when is_tuple(Map), element(1,Map)=:=dict ->
-    pack_map(dict:to_list(Map));
-pack(_Other) ->
-    {error, undefined}.
+-spec pack(Term::msgpack_term()) -> binary() | {error, reason()}.
+pack(Term)->
+    try
+	pack_(Term)
+    catch
+	error:Error when is_tuple(Error), element(1, Error) =:= error ->
+	    Error;
+	throw:Exception ->
+	    erlang:display(Exception),
+	    {error, Exception}
+    end.
 
 % unpacking.
 % if failed in decoding and not end, get more data
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
--spec unpack( Bin::binary() )-> {msgpack_term(), binary()} |
-				{more, non_neg_integer()} | {more, undefined} |
-				{error, reason()}.
-unpack(Bin) when not is_binary(Bin)->
-    {error, badarg};
-unpack(Bin) when bit_size(Bin) >= 8 ->
-    unpack_(Bin);
-unpack(<<>>)->
-    {more, 1};
-unpack(_) ->
-    {more, undefined}.
+-spec unpack( Bin::binary() )-> {msgpack_term(), binary()} | {error, reason()}.
+unpack(Bin)->
+    try
+	unpack_(Bin)
+    catch
+	error:Error when is_tuple(Error), element(1, Error) =:= error ->
+	    Error;
+	throw:Exception ->
+	    {error, Exception}
+    end.
 
 -spec unpack_all( binary() ) -> [msgpack_term()].
 unpack_all(Data)->
@@ -85,7 +71,7 @@ unpack_all(Data)->
 	    [Term|unpack_all(Binary)]
     end.
 
--spec pack_map(M::[{msgpack_term(),msgpack_term()}])-> binary().
+-spec pack_map(M::[{msgpack_term(),msgpack_term()}])-> binary() | {error, badarg}.
 pack_map(M)->
     case length(M) of
 	Len when Len < 16 ->
@@ -98,6 +84,31 @@ pack_map(M)->
 
 % ===== 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_(Map) when is_tuple(Map), element(1,Map)=:=dict ->
+    pack_map(dict:to_list(Map));
+pack_(_Other) ->
+    throw({error, undefined}).
+
 % positive fixnum
 pack_uint_(N) when N < 128 ->
     << 2#0:1, N:7 >>;
@@ -149,7 +160,7 @@ pack_raw(Bin) ->
 	    << 16#DB:8, Len:32/big-unsigned-integer-unit:1, Bin/binary >>
     end.
 
-% list / tuple
+% list
 pack_array(L) ->
     case length(L) of
  	Len when Len < 16 ->
@@ -159,43 +170,40 @@ pack_array(L) ->
 	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, <>).
+    pack_array_(Tail, <>).
 
-% FIXME! this should be without lists:reverse/1
-unpack_array_(<<>>, 0, RetList)   -> {lists:reverse(RetList), <<>>};
-unpack_array_(Remain, 0, RetList) when is_binary(Remain)-> {lists:reverse(RetList), Remain};
-unpack_array_(<<>>, RestLen, _RetList) when RestLen > 0 ->  {more, undefined};
-unpack_array_(Bin, RestLen, RetList) when is_binary(Bin)->
-    case unpack(Bin) of
-	{more, _} -> {more, undefined};
-	{Term, Rest}-> unpack_array_(Rest, RestLen-1, [Term|RetList])
-    end.
+% Users SHOULD NOT send too long list: this uses lists:reverse/1
+unpack_array_(Remain, 0,    Acc) when is_binary(Remain)-> {lists:reverse(Acc), Remain};
+unpack_array_(<<>>, RestLen,  _) when RestLen > 0 -> throw(short);
+unpack_array_(Bin, RestLen, Acc) when is_binary(Bin)->
+    {Term, Rest}=unpack_(Bin),
+    unpack_array_(Rest, RestLen-1, [Term|Acc]).
 
 pack_map_([], Acc) -> Acc;
 pack_map_([{Key,Value}|Tail], Acc) ->
-    pack_map_(Tail, << Acc/binary, (pack(Key))/binary, (pack(Value))/binary>>).
+    pack_map_(Tail, << Acc/binary, (pack_(Key))/binary, (pack_(Value))/binary>>).
 
-% FIXME! this should be without lists:reverse/1
--spec unpack_map_(binary(), non_neg_integer(), [{term(), msgpack_term()}])->
-    {more, non_neg_integer()} | { any(), 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_(<<>>, _,  _ )  -> throw(short);
 unpack_map_(Bin, Len, Acc) ->
-    case unpack(Bin) of
-	{more, _} -> {more, undefined};
-	{Key, Rest} ->
-	    case unpack(Rest) of
-		{more, _} -> {more, undefined};
-		{Value, Rest2} ->
-		    unpack_map_(Rest2,Len-1,[{Key,Value}|Acc])
-	    end
-    end.
+    {Key, Rest} = unpack_(Bin),
+    {Value, Rest2} = unpack_(Rest),
+    unpack_map_(Rest2,Len-1,[{Key,Value}|Acc]).
 
--spec unpack_(Payload::binary()) -> 
-		     {more, pos_integer()} | {msgpack_term(), binary()} | {error, reason()}.
-unpack_(Binary)->
-    case Binary of
+% unpack then all
+-spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | {error, reason()} | no_return().
+unpack_(Bin) when not is_binary(Bin)->
+    throw(badarg);
+unpack_(<<>>)->
+    throw(short);
+unpack_(Bin) when bit_size(Bin) >= 8 ->
+    case Bin of
 % ATOMS
 	<<16#C0, Rest/binary>> -> {nil, Rest};
 	<<16#C2, Rest/binary>> -> {false, Rest};
@@ -231,35 +239,36 @@ unpack_(Binary)->
 	<<2#1000:4, L:4, Rest/binary>> ->            unpack_map_(Rest, L, []);   % map
 	
 % Incomplete / invalid data
-	<<16#CA, Rest/binary>> -> {more, 4-byte_size(Rest)};
-	<<16#CB, Rest/binary>> -> {more, 8-byte_size(Rest)};
-	<<16#CC>> ->              {more, 1};
-	<<16#CD, Rest/binary>> -> {more, 2-byte_size(Rest)};
-	<<16#CE, Rest/binary>> -> {more, 4-byte_size(Rest)};
-	<<16#CF, Rest/binary>> -> {more, 8-byte_size(Rest)};
-	<<16#D0>> ->              {more, 1};
-	<<16#D1, Rest/binary>> -> {more, 2-byte_size(Rest)};
-	<<16#D2, Rest/binary>> -> {more, 4-byte_size(Rest)};
-	<<16#D3, Rest/binary>> -> {more, 8-byte_size(Rest)};
-	<<16#DA, Rest/binary>> -> {more, 16-byte_size(Rest)};
-	<<16#DB, Rest/binary>> -> {more, 32-byte_size(Rest)};
-	<<16#DC, Rest/binary>> -> {more, 2-byte_size(Rest)};
-	<<16#DD, Rest/binary>> -> {more, 4-byte_size(Rest)};
-	<<16#DE, Rest/binary>> -> {more, 2-byte_size(Rest)};
-	<<16#DF, Rest/binary>> -> {more, 4-byte_size(Rest)};
-	<<2#101:3, L:5, Rest/binary>> ->  {more, L-byte_size(Rest)};
+%	<<_:16/integer, _/binary>>
+	_ -> throw(short)
+%% 	<<16#CA, _/binary>> -> {more, 4-byte_size(Rest)};
+%% 	<<16#CB, Rest/binary>> -> {more, 8-byte_size(Rest)};
+%% 	<<16#CC>> ->              {more, 1};
+%% 	<<16#CD, Rest/binary>> -> {more, 2-byte_size(Rest)};
+%% 	<<16#CE, Rest/binary>> -> {more, 4-byte_size(Rest)};
+%% 	<<16#CF, Rest/binary>> -> {more, 8-byte_size(Rest)};
+%% 	<<16#D0>> ->              {more, 1};
+%% 	<<16#D1, Rest/binary>> -> {more, 2-byte_size(Rest)};
+%% 	<<16#D2, Rest/binary>> -> {more, 4-byte_size(Rest)};
+%% 	<<16#D3, Rest/binary>> -> {more, 8-byte_size(Rest)};
+%% 	<<16#DA, Rest/binary>> -> {more, 16-byte_size(Rest)};
+%% 	<<16#DB, Rest/binary>> -> {more, 32-byte_size(Rest)};
+%% 	<<16#DC, Rest/binary>> -> {more, 2-byte_size(Rest)};
+%% 	<<16#DD, Rest/binary>> -> {more, 4-byte_size(Rest)};
+%% 	<<16#DE, Rest/binary>> -> {more, 2-byte_size(Rest)};
+%% 	<<16#DF, Rest/binary>> -> {more, 4-byte_size(Rest)};
+%% 	<<2#101:3, L:5, Rest/binary>> ->  throw(short); % {more, L-byte_size(Rest)};
 
-	<<>> ->                  {more, 1};
-	<<2#101:3, _/binary>> -> {more, undefined};
-	<> when F==16#C1;
-				  F==16#C7; F==16#C8; F==16#C9; F==16#D5;
-				  F==16#D6; F==16#D7; F==16#D8; F==16#D9->
-	    {error, {badarg, <>}};
-	Other ->
-	    {error, {badarg, Other}}
+%% 	<<>> ->   throw(short); %               {more, 1};
+%% 	<<2#101:3, _/binary>> -> {more, undefined};
+%% 	<> when F==16#C1;
+%% 				  F==16#C7; F==16#C8; F==16#C9; F==16#D5;
+%% 				  F==16#D6; F==16#D7; F==16#D8; F==16#D9->
+%% 	    throw({badarg, <>});
+%	Other ->
+%	    throw({unknown, Other})
     end.
 
-
 % ===== test codes ===== %
 -include_lib("eunit/include/eunit.hrl").
 -ifdef(EUNIT).
@@ -268,9 +277,15 @@ compare_all([], [])-> ok;
 compare_all([], R)-> {toomuchrhs, R};
 compare_all(L, [])-> {toomuchlhs, L};
 compare_all([LH|LTL], [RH|RTL]) ->
-    LH=RH,
+    ?assertEqual(LH, RH),
     compare_all(LTL, RTL).
 
+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,
@@ -303,12 +318,7 @@ test_p(Len,Term,OrigBin,Len) ->
     {Term, <<>>}=msgpack:unpack(OrigBin);
 test_p(I,_,OrigBin,Len) when I < Len->
     <> = OrigBin,
-    case msgpack:unpack(Bin) of
-	{more, N} when not is_integer(N) ->
-	    ?assertEqual(undefined, N);
-	{more, N} ->
-	    ?assert( N < Len )
-    end.
+    ?assertEqual({error,short}, msgpack:unpack(Bin)).
 
 partial_test()-> % error handling test.
     Term = lists:seq(0, 45),
@@ -343,21 +353,15 @@ unknown_test()->
 	     42
 	    ],
     Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
+    timer:sleep(1),
     receive
 	{Port, {data, Data}}->
 	    compare_all(Tests, msgpack:unpack_all(Data))
     after 1024-> ?assert(false)   end,
     port_close(Port).
 
-test_([]) -> 0;
-test_([Before|Rest])->
-    Pack = msgpack:pack(Before),
-    {After, <<>>} = msgpack:unpack( Pack ),
-    ?assertEqual(Before, After),
-    1+test_(Rest).
-
 other_test()->
-    {more,1}=msgpack:unpack(<<>>).
+    ?assertEqual({error,short},msgpack:unpack(<<>>)).
 
 benchmark_test()->
     Data=[test_data() || _ <- lists:seq(0, 10000)],

From e799082e5c4d39094c666da0f1c52ab2d6eb088c Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Fri, 9 Jul 2010 01:21:35 +0900
Subject: [PATCH 097/152] erlang: better test cases, except 'Broken pipe'

---
 erlang/msgpack.erl | 74 +++++++++++++++++-----------------------------
 1 file changed, 27 insertions(+), 47 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index d4fd0ba..96ea407 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -142,7 +142,7 @@ pack_int_(N) ->
     << 16#D3:8, N:64/big-signed-integer-unit:1 >>.
 
 % float : erlang's float is always IEEE 754 64bit format.
-%pack_float(F) when is_float(F)->
+% pack_float(F) when is_float(F)->
 %    << 16#CA:8, F:32/big-float-unit:1 >>.
 %    pack_double(F).
 % double
@@ -198,10 +198,7 @@ unpack_map_(Bin, Len, Acc) ->
 
 % unpack then all
 -spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | {error, reason()} | no_return().
-unpack_(Bin) when not is_binary(Bin)->
-    throw(badarg);
-unpack_(<<>>)->
-    throw(short);
+unpack_(Bin) when not is_binary(Bin)->    throw(badarg);
 unpack_(Bin) when bit_size(Bin) >= 8 ->
     case Bin of
 % ATOMS
@@ -239,43 +236,31 @@ unpack_(Bin) when bit_size(Bin) >= 8 ->
 	<<2#1000:4, L:4, Rest/binary>> ->            unpack_map_(Rest, L, []);   % map
 	
 % Incomplete / invalid data
-%	<<_:16/integer, _/binary>>
-	_ -> throw(short)
-%% 	<<16#CA, _/binary>> -> {more, 4-byte_size(Rest)};
-%% 	<<16#CB, Rest/binary>> -> {more, 8-byte_size(Rest)};
-%% 	<<16#CC>> ->              {more, 1};
-%% 	<<16#CD, Rest/binary>> -> {more, 2-byte_size(Rest)};
-%% 	<<16#CE, Rest/binary>> -> {more, 4-byte_size(Rest)};
-%% 	<<16#CF, Rest/binary>> -> {more, 8-byte_size(Rest)};
-%% 	<<16#D0>> ->              {more, 1};
-%% 	<<16#D1, Rest/binary>> -> {more, 2-byte_size(Rest)};
-%% 	<<16#D2, Rest/binary>> -> {more, 4-byte_size(Rest)};
-%% 	<<16#D3, Rest/binary>> -> {more, 8-byte_size(Rest)};
-%% 	<<16#DA, Rest/binary>> -> {more, 16-byte_size(Rest)};
-%% 	<<16#DB, Rest/binary>> -> {more, 32-byte_size(Rest)};
-%% 	<<16#DC, Rest/binary>> -> {more, 2-byte_size(Rest)};
-%% 	<<16#DD, Rest/binary>> -> {more, 4-byte_size(Rest)};
-%% 	<<16#DE, Rest/binary>> -> {more, 2-byte_size(Rest)};
-%% 	<<16#DF, Rest/binary>> -> {more, 4-byte_size(Rest)};
-%% 	<<2#101:3, L:5, Rest/binary>> ->  throw(short); % {more, L-byte_size(Rest)};
-
-%% 	<<>> ->   throw(short); %               {more, 1};
-%% 	<<2#101:3, _/binary>> -> {more, undefined};
-%% 	<> when F==16#C1;
-%% 				  F==16#C7; F==16#C8; F==16#C9; F==16#D5;
-%% 				  F==16#D6; F==16#D7; F==16#D8; F==16#D9->
-%% 	    throw({badarg, <>});
-%	Other ->
-%	    throw({unknown, Other})
-    end.
+	<> when F==16#CA; F==16#CB; F==16#CC;
+				F==16#CD; F==16#CE; F==16#CF;
+				F==16#D0; F==16#D1; F==16#D2;
+				F==16#D3; F==16#DA; F==16#DB;
+				F==16#DC; F==16#DD; F==16#DE;
+				F==16#DF ->
+	    throw(short);
+	<> when F==16#C1;
+				F==16#C7; F==16#C8; F==16#C9;
+				F==16#D5; F==16#D6; F==16#D7;
+				F==16#D8; F==16#D9 ->
+	    throw(badarg);
+	_ ->
+	    throw(short) % or unknown/badarg?
+    end;
+unpack_(<<>>)->                    throw(short);
+unpack_(<<2#101:3, _/binary>>) ->  throw(short).
 
 % ===== test codes ===== %
 -include_lib("eunit/include/eunit.hrl").
 -ifdef(EUNIT).
 
 compare_all([], [])-> ok;
-compare_all([], R)-> {toomuchrhs, R};
-compare_all(L, [])-> {toomuchlhs, L};
+compare_all([],  R)-> {toomuchrhs, R};
+compare_all(L,  [])-> {toomuchlhs, L};
 compare_all([LH|LTL], [RH|RTL]) ->
     ?assertEqual(LH, RH),
     compare_all(LTL, RTL).
@@ -305,9 +290,9 @@ basic_test()->
     Passed = length(Tests).
 
 port_test()->
+    Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
     Tests = test_data(),
     {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
-    Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
     true = port_command(Port, msgpack:pack(Tests) ),
     receive
 	{Port, {data, Data}}->  {Tests, <<>>}=msgpack:unpack(Data)
@@ -328,10 +313,7 @@ partial_test()-> % error handling test.
 
 long_test()->
     Longer = lists:seq(0, 655),
-%    Longest = lists:seq(0,12345),
-    {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)),
-%    {Longest, <<>>} = msgpack:unpack(msgpack:pack(Longest)).
-    ok.
+    {Longer, <<>>} = msgpack:unpack(msgpack:pack(Longer)).
 
 map_test()->
     Ints = lists:seq(0, 65),
@@ -341,6 +323,7 @@ map_test()->
     ok.
 
 unknown_test()->
+    Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
     Tests = [0, 1, 2, 123, 512, 1230, 678908,
 	     -1, -23, -512, -1230, -567898,
 	     <<"hogehoge">>, <<"243546rf7g68h798j">>,
@@ -348,16 +331,13 @@ unknown_test()->
 	     -234.4355, 1.0e-34, 1.0e64,
 	     [23, 234, 0.23],
 	     [0,42,<<"sum">>, [1,2]], [1,42, nil, [3]],
-	     {[{1,2},{<<"hoge">>,nil}]},
+	     {[{1,2},{<<"hoge">>,nil}]}, % map
 	     -234, -50000,
 	     42
 	    ],
-    Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
-    timer:sleep(1),
     receive
-	{Port, {data, Data}}->
-	    compare_all(Tests, msgpack:unpack_all(Data))
-    after 1024-> ?assert(false)   end,
+	{Port, {data, Data}}-> compare_all(Tests, msgpack:unpack_all(Data))
+    after 1024-> ?assert(false)  end,
     port_close(Port).
 
 other_test()->

From 64c36b7a8faac55d8dc80342f27929b5538c4307 Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 13:06:57 +0200
Subject: [PATCH 098/152] 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.
---
 erlang/msgpack.erl | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 1291d54..d1ba9cd 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -52,7 +52,9 @@ pack(Term)->
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
 -spec unpack( Bin::binary() )-> {msgpack_term(), binary()} | {error, reason()}.
-unpack(Bin)->
+unpack(Bin) when not is_binary(Bin) ->
+    {error, badarg};
+unpack(Bin) ->
     try
 	unpack_(Bin)
     catch
@@ -126,7 +128,7 @@ pack_uint_(N) ->
     << 16#CF:8, N:64/big-unsigned-integer-unit:1 >>.
 
 % negative fixnum
-pack_int_(N) when is_integer(N) , N >= -32->
+pack_int_(N) when N >= -32->
     << 2#111:3, N:5 >>;
 % int 8
 pack_int_(N) when N > -128 ->
@@ -176,9 +178,9 @@ pack_array_([Head|Tail], Acc) ->
     pack_array_(Tail, <>).
 
 % Users SHOULD NOT send too long list: this uses lists:reverse/1
-unpack_array_(Remain, 0,    Acc) when is_binary(Remain)-> {lists:reverse(Acc), Remain};
-unpack_array_(<<>>, RestLen,  _) when RestLen > 0 -> throw(short);
-unpack_array_(Bin, RestLen, Acc) when is_binary(Bin)->
+unpack_array_(Remain, 0,    Acc) -> {lists:reverse(Acc), Remain};
+unpack_array_(<<>>, RestLen,  _) -> throw(short);
+unpack_array_(Bin, RestLen, Acc) ->
     {Term, Rest}=unpack_(Bin),
     unpack_array_(Rest, RestLen-1, [Term|Acc]).
 
@@ -198,8 +200,7 @@ unpack_map_(Bin, Len, Acc) ->
 
 % unpack then all
 -spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | {error, reason()} | no_return().
-unpack_(Bin) when not is_binary(Bin)->    throw(badarg);
-unpack_(Bin) when bit_size(Bin) >= 8 ->
+unpack_(Bin) ->
     case Bin of
 % ATOMS
 	<<16#C0, Rest/binary>> -> {nil, Rest};
@@ -234,7 +235,7 @@ unpack_(Bin) when bit_size(Bin) >= 8 ->
 	<<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
-	
+
 % Incomplete / invalid data
 	<> when F==16#CA; F==16#CB; F==16#CC;
 				F==16#CD; F==16#CE; F==16#CF;

From 6abc120279ced47d899cd8596e4d48fc2171e2cf Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 13:29:47 +0200
Subject: [PATCH 099/152] 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
---
 erlang/msgpack.erl | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index d1ba9cd..1c1eae9 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -236,24 +236,15 @@ unpack_(Bin) ->
 	<<2#1001:4, L:4, Rest/binary>> ->            unpack_array_(Rest, L, []); % array
 	<<2#1000:4, L:4, Rest/binary>> ->            unpack_map_(Rest, L, []);   % map
 
-% Incomplete / invalid data
-	<> when F==16#CA; F==16#CB; F==16#CC;
-				F==16#CD; F==16#CE; F==16#CF;
-				F==16#D0; F==16#D1; F==16#D2;
-				F==16#D3; F==16#DA; F==16#DB;
-				F==16#DC; F==16#DD; F==16#DE;
-				F==16#DF ->
-	    throw(short);
-	<> when F==16#C1;
-				F==16#C7; F==16#C8; F==16#C9;
-				F==16#D5; F==16#D6; F==16#D7;
-				F==16#D8; F==16#D9 ->
+% Invalid data
+	<> 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);
+% Incomplete data (we've covered every complete/invalid case; anything left is incomplete)
 	_ ->
-	    throw(short) % or unknown/badarg?
-    end;
-unpack_(<<>>)->                    throw(short);
-unpack_(<<2#101:3, _/binary>>) ->  throw(short).
+	    throw(short)
+    end.
 
 % ===== test codes ===== %
 -include_lib("eunit/include/eunit.hrl").

From ba4a971bfaabe7da2159a634cd07977e06c61e3a Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 13:44:02 +0200
Subject: [PATCH 100/152] 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).
---
 erlang/msgpack.erl | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 1c1eae9..ff3eac7 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -178,11 +178,10 @@ pack_array_([Head|Tail], Acc) ->
     pack_array_(Tail, <>).
 
 % Users SHOULD NOT send too long list: this uses lists:reverse/1
-unpack_array_(Remain, 0,    Acc) -> {lists:reverse(Acc), Remain};
-unpack_array_(<<>>, RestLen,  _) -> throw(short);
-unpack_array_(Bin, RestLen, Acc) ->
-    {Term, Rest}=unpack_(Bin),
-    unpack_array_(Rest, RestLen-1, [Term|Acc]).
+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]).
 
 pack_map_([], Acc) -> Acc;
 pack_map_([{Key,Value}|Tail], Acc) ->
@@ -191,14 +190,13 @@ pack_map_([{Key,Value}|Tail], Acc) ->
 % 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_(<<>>, _,  _ )  -> throw(short);
+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_map_(Rest2, Len-1, [{Key,Value}|Acc]).
 
-% unpack then all
+% unpack them all
 -spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | {error, reason()} | no_return().
 unpack_(Bin) ->
     case Bin of

From a4258505a99087df2397cf8d6cfbf194311a995c Mon Sep 17 00:00:00 2001
From: UENISHI Kota 
Date: Fri, 9 Jul 2010 23:23:00 +0900
Subject: [PATCH 101/152] erlang: modified wrong testcase.

---
 erlang/msgpack.erl | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index ff3eac7..e862cad 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -282,10 +282,11 @@ basic_test()->
 port_test()->
     Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
     Tests = test_data(),
-    {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
-    true = port_command(Port, msgpack:pack(Tests) ),
+    S=msgpack:pack([Tests]),
+    true = port_command(Port, S),
+    {[Tests],<<>>} = msgpack:unpack(S),
     receive
-	{Port, {data, Data}}->  {Tests, <<>>}=msgpack:unpack(Data)
+	{Port, {data, Data}}->  {[Tests], <<>>}=msgpack:unpack(Data)
     after 1024-> ?assert(false)   end,
     port_close(Port).
 

From 8a3f090684f0835958e60e6a5e3e81b4d8ff541a Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 17:36:36 +0200
Subject: [PATCH 102/152] 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.
---
 erlang/msgpack.erl | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index ff3eac7..fb9a3e1 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -29,7 +29,7 @@
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
--type reason() ::  enomem | badarg | no_code_matches | undefined.
+-type reason() ::  badarg | short.
 -type msgpack_term() :: [msgpack_term()]
 		      | {[{msgpack_term(),msgpack_term()}]}
 		      | integer() | float() | binary().
@@ -73,7 +73,7 @@ unpack_all(Data)->
 	    [Term|unpack_all(Binary)]
     end.
 
--spec pack_map(M::[{msgpack_term(),msgpack_term()}])-> binary() | {error, badarg}.
+-spec pack_map(M::[{msgpack_term(),msgpack_term()}]) -> binary() | no_return().
 pack_map(M)->
     case length(M) of
 	Len when Len < 16 ->
@@ -109,8 +109,9 @@ pack_({Map}) when is_list(Map) ->
 pack_(Map) when is_tuple(Map), element(1,Map)=:=dict ->
     pack_map(dict:to_list(Map));
 pack_(_Other) ->
-    throw({error, undefined}).
+    throw({error, badarg}).
 
+-spec pack_uint_(non_neg_integer()) -> binary().
 % positive fixnum
 pack_uint_(N) when N < 128 ->
     << 2#0:1, N:7 >>;
@@ -127,6 +128,7 @@ pack_uint_(N) when N < 16#FFFFFFFF->
 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 >>;
@@ -143,6 +145,7 @@ pack_int_(N) when N > -16#FFFFFFFF ->
 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 >>.
@@ -151,6 +154,7 @@ pack_int_(N) ->
 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
@@ -162,6 +166,7 @@ pack_raw(Bin) ->
 	    << 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
@@ -178,6 +183,7 @@ pack_array_([Head|Tail], Acc) ->
     pack_array_(Tail, <>).
 
 % 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),
@@ -188,8 +194,8 @@ 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().
+-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),
@@ -197,7 +203,7 @@ unpack_map_(Bin, Len, Acc) ->
     unpack_map_(Rest2, Len-1, [{Key,Value}|Acc]).
 
 % unpack them all
--spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | {error, reason()} | no_return().
+-spec unpack_(Bin::binary()) -> {msgpack_term(), binary()} | no_return().
 unpack_(Bin) ->
     case Bin of
 % ATOMS

From 21992f1b9e43f3e0d2c3da8f1f8d94d9ee70b6fb Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 18:53:24 +0200
Subject: [PATCH 103/152] 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.
---
 erlang/msgpack.erl | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index fb9a3e1..58ad414 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -261,6 +261,15 @@ 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, <>);
+        {Port, eof} -> Acc
+    after 1000 -> Acc
+    end.
+
 test_([]) -> 0;
 test_([Term|Rest])->
     Pack = msgpack:pack(Term),
@@ -286,13 +295,12 @@ basic_test()->
     Passed = length(Tests).
 
 port_test()->
-    Port = open_port({spawn, "ruby ../test/crosslang.rb"}, [binary]),
     Tests = test_data(),
-    {[Tests],<<>>} = msgpack:unpack(msgpack:pack([Tests])),
-    true = port_command(Port, msgpack:pack(Tests) ),
-    receive
-	{Port, {data, Data}}->  {Tests, <<>>}=msgpack:unpack(Data)
-    after 1024-> ?assert(false)   end,
+    ?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) ->
@@ -319,7 +327,7 @@ map_test()->
     ok.
 
 unknown_test()->
-    Port = open_port({spawn, "ruby testcase_generator.rb"}, [binary]),
+    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">>,
@@ -331,9 +339,7 @@ unknown_test()->
 	     -234, -50000,
 	     42
 	    ],
-    receive
-	{Port, {data, Data}}-> compare_all(Tests, msgpack:unpack_all(Data))
-    after 1024-> ?assert(false)  end,
+    ?assertEqual(ok, compare_all(Tests, msgpack:unpack_all(port_receive(Port)))),
     port_close(Port).
 
 other_test()->

From 2c29377abf1997d3a55178ef0562aa38d255f0fe Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 20:30:17 +0200
Subject: [PATCH 104/152] erlang: s/short/incomplete/ and
 s/badarg/{badarg,Term}/

Nicer error returns.
---
 erlang/msgpack.erl | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 58ad414..102efed 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -29,7 +29,7 @@
 % erl> c(msgpack).
 % erl> S = .
 % erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ).
--type reason() ::  badarg | short.
+-type reason() :: {badarg,term()} | incomplete.
 -type msgpack_term() :: [msgpack_term()]
 		      | {[{msgpack_term(),msgpack_term()}]}
 		      | integer() | float() | binary().
@@ -43,7 +43,6 @@ pack(Term)->
 	error:Error when is_tuple(Error), element(1, Error) =:= error ->
 	    Error;
 	throw:Exception ->
-	    erlang:display(Exception),
 	    {error, Exception}
     end.
 
@@ -52,9 +51,7 @@ pack(Term)->
 % and feed more Bin into this function.
 % TODO: error case for imcomplete format when short for any type formats.
 -spec unpack( Bin::binary() )-> {msgpack_term(), binary()} | {error, reason()}.
-unpack(Bin) when not is_binary(Bin) ->
-    {error, badarg};
-unpack(Bin) ->
+unpack(Bin) when is_binary(Bin) ->
     try
 	unpack_(Bin)
     catch
@@ -62,7 +59,9 @@ unpack(Bin) ->
 	    Error;
 	throw:Exception ->
 	    {error, Exception}
-    end.
+    end;
+unpack(Other) ->
+    {error, {badarg, Other}}.
 
 -spec unpack_all( binary() ) -> [msgpack_term()].
 unpack_all(Data)->
@@ -108,8 +107,8 @@ pack_({Map}) when is_list(Map) ->
     pack_map(Map);
 pack_(Map) when is_tuple(Map), element(1,Map)=:=dict ->
     pack_map(dict:to_list(Map));
-pack_(_Other) ->
-    throw({error, badarg}).
+pack_(Other) ->
+    throw({error, {badarg, Other}}).
 
 -spec pack_uint_(non_neg_integer()) -> binary().
 % positive fixnum
@@ -241,13 +240,13 @@ unpack_(Bin) ->
 	<<2#1000:4, L:4, Rest/binary>> ->            unpack_map_(Rest, L, []);   % map
 
 % Invalid data
-	<> when F==16#C1;
+	<> 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);
+	    throw({badarg, <>});
 % Incomplete data (we've covered every complete/invalid case; anything left is incomplete)
 	_ ->
-	    throw(short)
+	    throw(incomplete)
     end.
 
 % ===== test codes ===== %
@@ -307,7 +306,7 @@ test_p(Len,Term,OrigBin,Len) ->
     {Term, <<>>}=msgpack:unpack(OrigBin);
 test_p(I,_,OrigBin,Len) when I < Len->
     <> = OrigBin,
-    ?assertEqual({error,short}, msgpack:unpack(Bin)).
+    ?assertEqual({error,incomplete}, msgpack:unpack(Bin)).
 
 partial_test()-> % error handling test.
     Term = lists:seq(0, 45),
@@ -343,7 +342,7 @@ unknown_test()->
     port_close(Port).
 
 other_test()->
-    ?assertEqual({error,short},msgpack:unpack(<<>>)).
+    ?assertEqual({error,incomplete},msgpack:unpack(<<>>)).
 
 benchmark_test()->
     Data=[test_data() || _ <- lists:seq(0, 10000)],

From 02c882bda39f65bdbe7be1b149b74f0f9a8283c6 Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 20:34:38 +0200
Subject: [PATCH 105/152] erlang: Make pack_map/1 api private

---
 erlang/msgpack.erl | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 102efed..0e7fb5d 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -23,7 +23,6 @@
 %% APIs are almost compatible with C API (http://msgpack.sourceforge.jp/c:doc)
 %% except buffering functions (both copying and zero-copying).
 -export([pack/1, unpack/1, unpack_all/1]).
--export([pack_map/1]).
 
 % compile:
 % erl> c(msgpack).
@@ -72,16 +71,6 @@ unpack_all(Data)->
 	    [Term|unpack_all(Binary)]
     end.
 
--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.
 
 % ===== internal APIs ===== %
 
@@ -188,6 +177,17 @@ 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>>).

From e944c1ee93083a054f318e42a16eff699d9f066d Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Fri, 9 Jul 2010 20:37:06 +0200
Subject: [PATCH 106/152] 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.
---
 erlang/msgpack.erl | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index 0e7fb5d..f23ac87 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -39,8 +39,6 @@ pack(Term)->
     try
 	pack_(Term)
     catch
-	error:Error when is_tuple(Error), element(1, Error) =:= error ->
-	    Error;
 	throw:Exception ->
 	    {error, Exception}
     end.
@@ -54,8 +52,6 @@ unpack(Bin) when is_binary(Bin) ->
     try
 	unpack_(Bin)
     catch
-	error:Error when is_tuple(Error), element(1, Error) =:= error ->
-	    Error;
 	throw:Exception ->
 	    {error, Exception}
     end;

From e629e8784ff242f7c5e62066b0670f5d82afe4e2 Mon Sep 17 00:00:00 2001
From: Vincent de Phily 
Date: Mon, 12 Jul 2010 14:08:22 +0200
Subject: [PATCH 107/152] 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.
---
 erlang/OMakefile   |  7 +++--
 erlang/msgpack.erl | 70 +++++++++++++++++++++++++++++++++-------------
 2 files changed, 56 insertions(+), 21 deletions(-)

diff --git a/erlang/OMakefile b/erlang/OMakefile
index 34c590f..89b1c63 100644
--- a/erlang/OMakefile
+++ b/erlang/OMakefile
@@ -30,13 +30,16 @@
 # If so, define the subdirectory targets and uncomment this section.
 #
 
-.DEFAULT: msgpack.beam
+.DEFAULT: msgpack.beam msgpack.html
 
 msgpack.beam: msgpack.erl
 	erlc $<
 
+msgpack.html: msgpack.erl
+	erl -noshell -run edoc_run file $<
+
 test: msgpack.beam
 	erl -noshell -s msgpack test -s init stop
 
 clean:
-	-rm *.beam
+	-rm -f *.beam *.html
diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl
index f23ac87..aa9851d 100644
--- a/erlang/msgpack.erl
+++ b/erlang/msgpack.erl
@@ -15,26 +15,47 @@
 %%    See the License for the specific language governing permissions and
 %%    limitations under the License.
 
+
+%% @doc MessagePack codec for Erlang.
+%%
+%%      APIs are almost compatible with C API
+%%      except for buffering functions (both copying and zero-copying), which are unavailable.
+%%
+%%   
+%%     
+%%     
+%%     
+%%     
+%%     
+%%     
+%%     
+%%     
+%%     
+%%   
Equivalence between Erlang and Msgpack type :
erlang msgpack
integer() pos_fixnum/neg_fixnum/uint8/uint16/uint32/uint64/int8/int16/int32/int64
float() float/double
nil nil
boolean() boolean
binary() fix_raw/raw16/raw32
list() fix_array/array16/array32
{proplist()} fix_map/map16/map32
+%% @end + -module(msgpack). -author('kuenishi+msgpack@gmail.com'). -%% tuples, atoms are not supported. lists, integers, double, and so on. -%% see http://msgpack.sourceforge.jp/spec for supported formats. -%% APIs are almost compatible with C API (http://msgpack.sourceforge.jp/c:doc) -%% except buffering functions (both copying and zero-copying). -export([pack/1, unpack/1, unpack_all/1]). -% compile: -% erl> c(msgpack). -% erl> S = . -% erl> {S, <<>>} = msgpack:unpack( msgpack:pack(S) ). --type reason() :: {badarg,term()} | incomplete. +% @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 ===== % --spec pack(Term::msgpack_term()) -> binary() | {error, reason()}. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% 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) @@ -43,11 +64,12 @@ pack(Term)-> {error, Exception} end. -% unpacking. -% if failed in decoding and not end, get more data -% and feed more Bin into this function. -% TODO: error case for imcomplete format when short for any type formats. --spec unpack( Bin::binary() )-> {msgpack_term(), binary()} | {error, reason()}. +% @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) @@ -58,7 +80,7 @@ unpack(Bin) when is_binary(Bin) -> unpack(Other) -> {error, {badarg, Other}}. --spec unpack_all( binary() ) -> [msgpack_term()]. +-spec unpack_all(binary()) -> [msgpack_term()]. unpack_all(Data)-> case unpack(Data) of { Term, Binary } when bit_size(Binary) =:= 0 -> @@ -68,7 +90,9 @@ unpack_all(Data)-> end. -% ===== internal APIs ===== % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% internal APIs +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % pack them all -spec pack_(msgpack_term()) -> binary() | no_return(). @@ -95,6 +119,7 @@ pack_(Map) when is_tuple(Map), element(1,Map)=:=dict -> pack_(Other) -> throw({error, {badarg, Other}}). + -spec pack_uint_(non_neg_integer()) -> binary(). % positive fixnum pack_uint_(N) when N < 128 -> @@ -129,6 +154,7 @@ pack_int_(N) when N > -16#FFFFFFFF -> 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)-> @@ -138,6 +164,7 @@ pack_int_(N) -> pack_double(F) -> << 16#CB:8, F:64/big-float-unit:1 >>. + -spec pack_raw(binary()) -> binary(). % raw bytes pack_raw(Bin) -> @@ -150,6 +177,7 @@ pack_raw(Bin) -> << 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) -> @@ -173,6 +201,7 @@ 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 @@ -245,7 +274,10 @@ unpack_(Bin) -> throw(incomplete) end. -% ===== test codes ===== % + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% unit tests +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -include_lib("eunit/include/eunit.hrl"). -ifdef(EUNIT). From ca0c844f32038329b21f92ce42c62618057ecc02 Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Wed, 14 Jul 2010 09:58:05 +0900 Subject: [PATCH 108/152] clearly specified this distribution requires requires C99. --- perl/Makefile.PL | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/perl/Makefile.PL b/perl/Makefile.PL index 58ab7c7..e9f9618 100644 --- a/perl/Makefile.PL +++ b/perl/Makefile.PL @@ -1,4 +1,6 @@ use inc::Module::Install; +use Config; + name 'Data-MessagePack'; all_from 'lib/Data/MessagePack.pm'; readme_from('lib/Data/MessagePack.pm'); @@ -11,6 +13,8 @@ tests 't/*.t'; recursive_author_tests('xt'); use_ppport 3.19; +requires_c99(); # msgpack C library requires C99. + clean_files qw{ *.stackdump *.gcov *.gcda *.gcno From 9ac69337e89305a36ae3a3d3a9d89272a7453452 Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Wed, 14 Jul 2010 09:58:28 +0900 Subject: [PATCH 109/152] perl: bump up version to 0.13! --- perl/Changes | 4 ++++ perl/lib/Data/MessagePack.pm | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/perl/Changes b/perl/Changes index b03a9d0..f32d5f4 100644 --- a/perl/Changes +++ b/perl/Changes @@ -1,3 +1,7 @@ +0.13 + + - clearly specify requires_c99(), because msgpack C header requires C99. + 0.12 - PERL_NO_GET_CONTEXT makes horrible dTHXs. remove it. diff --git a/perl/lib/Data/MessagePack.pm b/perl/lib/Data/MessagePack.pm index dcc713d..ee07d35 100644 --- a/perl/lib/Data/MessagePack.pm +++ b/perl/lib/Data/MessagePack.pm @@ -4,7 +4,7 @@ use warnings; use XSLoader; use 5.008001; -our $VERSION = '0.12'; +our $VERSION = '0.13'; our $PreferInteger = 0; our $true = do { bless \(my $dummy = 1), "Data::MessagePack::Boolean" }; From 331bf0af21cecfec1025e0f69fb4b3ffad4129fe Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 14 Jul 2010 17:02:04 +0900 Subject: [PATCH 110/152] cpp: type::raw_ref::str(), operator==, operator!=, operator< and operator> are now const --- cpp/src/msgpack/type/raw.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cpp/src/msgpack/type/raw.hpp b/cpp/src/msgpack/type/raw.hpp index 21d9a0d..87d188f 100644 --- a/cpp/src/msgpack/type/raw.hpp +++ b/cpp/src/msgpack/type/raw.hpp @@ -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; } From f5453d38ec96b55e95ac745472d9b55087fc1d2f Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 14 Jul 2010 17:06:16 +0900 Subject: [PATCH 111/152] cpp: version 0.5.2 --- cpp/ChangeLog | 5 +++++ cpp/configure.in | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cpp/ChangeLog b/cpp/ChangeLog index 3277c13..504ac4b 100644 --- a/cpp/ChangeLog +++ b/cpp/ChangeLog @@ -1,4 +1,9 @@ +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 diff --git a/cpp/configure.in b/cpp/configure.in index ab29501..93174da 100644 --- a/cpp/configure.in +++ b/cpp/configure.in @@ -1,6 +1,6 @@ AC_INIT(src/object.cpp) AC_CONFIG_AUX_DIR(ac) -AM_INIT_AUTOMAKE(msgpack, 0.5.1) +AM_INIT_AUTOMAKE(msgpack, 0.5.2) AC_CONFIG_HEADER(config.h) AC_SUBST(CFLAGS) From 7b152640d962434192496e4d96788622d4e90886 Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Sun, 18 Jul 2010 23:40:25 +0900 Subject: [PATCH 112/152] erlang: 'edoc' document generation --- erlang/.gitignore | 1 + erlang/OMakefile | 7 +++++-- erlang/edoc/.gitignore | 4 ++++ erlang/msgpack.erl | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 erlang/edoc/.gitignore diff --git a/erlang/.gitignore b/erlang/.gitignore index 7d6d324..0f7faad 100644 --- a/erlang/.gitignore +++ b/erlang/.gitignore @@ -2,3 +2,4 @@ MANIFEST *.beam .omakedb* *.omc +*~ \ No newline at end of file diff --git a/erlang/OMakefile b/erlang/OMakefile index 89b1c63..d213444 100644 --- a/erlang/OMakefile +++ b/erlang/OMakefile @@ -30,10 +30,10 @@ # If so, define the subdirectory targets and uncomment this section. # -.DEFAULT: msgpack.beam msgpack.html +.DEFAULT: msgpack.beam msgpack.beam: msgpack.erl - erlc $< + erlc -Wall +debug_info $< msgpack.html: msgpack.erl erl -noshell -run edoc_run file $< @@ -41,5 +41,8 @@ msgpack.html: msgpack.erl 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 + clean: -rm -f *.beam *.html diff --git a/erlang/edoc/.gitignore b/erlang/edoc/.gitignore new file mode 100644 index 0000000..97f4246 --- /dev/null +++ b/erlang/edoc/.gitignore @@ -0,0 +1,4 @@ +*.html +*.css +*.png +edoc-info diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index aa9851d..a697483 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -18,7 +18,7 @@ %% @doc MessagePack codec for Erlang. %% -%% APIs are almost compatible with C API +%% APIs are almost compatible with C API %% except for buffering functions (both copying and zero-copying), which are unavailable. %% %% From dad7a03d1975c28574fb768e10a63355832a0474 Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Sun, 18 Jul 2010 23:42:23 +0900 Subject: [PATCH 113/152] erlang: stopped support for dict() type. --- erlang/msgpack.erl | 2 -- 1 file changed, 2 deletions(-) diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index a697483..13bb8e1 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -114,8 +114,6 @@ pack_(List) when is_list(List) -> pack_array(List); pack_({Map}) when is_list(Map) -> pack_map(Map); -pack_(Map) when is_tuple(Map), element(1,Map)=:=dict -> - pack_map(dict:to_list(Map)); pack_(Other) -> throw({error, {badarg, Other}}). From 6cabad19d5a576e3e44e069c0a2b107bd33d30ef Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Sun, 18 Jul 2010 23:48:20 +0900 Subject: [PATCH 114/152] erlang: unpack_all/1 improve, error handling added. --- erlang/msgpack.erl | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index 13bb8e1..d4a0bc8 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -80,16 +80,22 @@ unpack(Bin) when is_binary(Bin) -> unpack(Other) -> {error, {badarg, Other}}. --spec unpack_all(binary()) -> [msgpack_term()]. +-spec unpack_all(binary()) -> [msgpack_term()] | {error, incomplete} | {error, {badarg, term()}}. unpack_all(Data)-> - case unpack(Data) of - { Term, Binary } when bit_size(Binary) =:= 0 -> + 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)] + [Term|unpack_all_(Binary)] end. - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % internal APIs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% From 8a3ac6d9bd374acc6b134d330bf7aeb906fa1a80 Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Sun, 18 Jul 2010 23:50:29 +0900 Subject: [PATCH 115/152] erlang: omake menus added. --- erlang/OMakefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/erlang/OMakefile b/erlang/OMakefile index d213444..2107940 100644 --- a/erlang/OMakefile +++ b/erlang/OMakefile @@ -22,7 +22,7 @@ # Phony targets are scoped, so you probably want to declare them first. # -.PHONY: all clean test #install +.PHONY: all clean test edoc dialyzer #install ######################################################################## # Subdirectories. @@ -44,5 +44,8 @@ test: msgpack.beam 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 From dcbcf5842f58b7ead3f524ac027576ce0c55e9ab Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Sun, 18 Jul 2010 23:55:07 +0900 Subject: [PATCH 116/152] erlang: msgpack:unpack_all/1 doc. --- erlang/msgpack.erl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index d4a0bc8..b54874d 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -80,6 +80,11 @@ unpack(Bin) when is_binary(Bin) -> 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 From 227c168b65be72e5e3a843af38d9382b59fc858a Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 24 Jul 2010 18:07:22 +0900 Subject: [PATCH 117/152] java: fixes fatal offset calculation bugs on BufferedUnpackerIMPL.unpackInt() --- java/src/main/java/org/msgpack/BufferedUnpackerImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index cc6604d..f4ed35b 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -103,7 +103,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { case 0xcc: // unsigned int 8 more(2); advance(2); - return (int)((short)buffer[offset+1] & 0xff); + return (int)((short)(buffer[offset-1]) & 0xff); case 0xcd: // unsigned int 16 more(3); castBuffer.rewind(); @@ -137,7 +137,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { case 0xd0: // signed int 8 more(2); advance(2); - return (int)buffer[offset+1]; + return (int)buffer[offset-1]; case 0xd1: // signed int 16 more(3); castBuffer.rewind(); @@ -178,7 +178,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { case 0xcc: // unsigned int 8 more(2); advance(2); - return (long)((short)buffer[offset+1] & 0xff); + return (long)((short)(buffer[offset-1]) & 0xff); case 0xcd: // unsigned int 16 more(3); castBuffer.rewind(); @@ -207,7 +207,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { case 0xd0: // signed int 8 more(2); advance(2); - return (long)buffer[offset+1]; + return (long)buffer[offset-1]; case 0xd1: // signed int 16 more(3); castBuffer.rewind(); From 2aef495d62d19b2f1721989225700942ea71e582 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 24 Jul 2010 18:08:00 +0900 Subject: [PATCH 118/152] java: adds MessagePackObject class --- .../java/org/msgpack/MessagePackObject.java | 130 ++++++++++++++++++ .../java/org/msgpack/object/ArrayType.java | 48 +++++++ .../msgpack/object/BigIntegerTypeIMPL.java | 92 +++++++++++++ .../java/org/msgpack/object/BooleanType.java | 39 ++++++ .../org/msgpack/object/DoubleTypeIMPL.java | 71 ++++++++++ .../java/org/msgpack/object/FloatType.java | 28 ++++ .../org/msgpack/object/FloatTypeIMPL.java | 70 ++++++++++ .../java/org/msgpack/object/IntegerType.java | 49 +++++++ .../msgpack/object/LongIntegerTypeIMPL.java | 89 ++++++++++++ .../main/java/org/msgpack/object/MapType.java | 48 +++++++ .../main/java/org/msgpack/object/NilType.java | 28 ++++ .../msgpack/object/ShortIntegerTypeIMPL.java | 91 ++++++++++++ 12 files changed, 783 insertions(+) create mode 100644 java/src/main/java/org/msgpack/MessagePackObject.java create mode 100644 java/src/main/java/org/msgpack/object/ArrayType.java create mode 100644 java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java create mode 100644 java/src/main/java/org/msgpack/object/BooleanType.java create mode 100644 java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java create mode 100644 java/src/main/java/org/msgpack/object/FloatType.java create mode 100644 java/src/main/java/org/msgpack/object/FloatTypeIMPL.java create mode 100644 java/src/main/java/org/msgpack/object/IntegerType.java create mode 100644 java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java create mode 100644 java/src/main/java/org/msgpack/object/MapType.java create mode 100644 java/src/main/java/org/msgpack/object/NilType.java create mode 100644 java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java new file mode 100644 index 0000000..aba27e4 --- /dev/null +++ b/java/src/main/java/org/msgpack/MessagePackObject.java @@ -0,0 +1,130 @@ +// +// 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 { + public boolean isNull() { + 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 asList() { + throw new MessageTypeException("type error"); + } + + public Map 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 float floatValue() { + throw new MessageTypeException("type error"); + } + + public double doubleValue() { + throw new MessageTypeException("type error"); + } +} + diff --git a/java/src/main/java/org/msgpack/object/ArrayType.java b/java/src/main/java/org/msgpack/object/ArrayType.java new file mode 100644 index 0000000..06e9b16 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/ArrayType.java @@ -0,0 +1,48 @@ +// +// 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.Set; +import java.util.Map; +import java.util.Arrays; +import org.msgpack.*; + +public class ArrayType extends MessagePackObject { + private MessagePackObject[] array; + + public ArrayType(MessagePackObject[] array) { + this.array = array; + } + + @Override + public boolean isArrayType() { + return true; + } + + @Override + public MessagePackObject[] asArray() { + return array; + } + + @Override + public List asList() { + return Arrays.asList(array); + } +} + diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java new file mode 100644 index 0000000..1d638c9 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -0,0 +1,92 @@ +// +// 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 org.msgpack.*; + +class BigIntegerTypeIMPL extends IntegerType { + private BigInteger value; + + BigIntegerTypeIMPL(BigInteger vlaue) { + 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 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 float floatValue() { + return value.floatValue(); + } + + @Override + public double doubleValue() { + return value.doubleValue(); + } +} + diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java new file mode 100644 index 0000000..c9e84b6 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/BooleanType.java @@ -0,0 +1,39 @@ +// +// 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 org.msgpack.*; + +public class BooleanType extends MessagePackObject { + private boolean value; + + public BooleanType(boolean value) { + this.value = value; + } + + @Override + public boolean isBooleanType() { + return false; + } + + @Override + public boolean asBoolean() { + return value; + } +} + diff --git a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java new file mode 100644 index 0000000..0e33d5b --- /dev/null +++ b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java @@ -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.math.BigInteger; +import org.msgpack.*; + +class DoubleTypeIMPL extends FloatType { + private double value; + + public DoubleTypeIMPL(double vlaue) { + 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 float floatValue() { + return (float)value; + } + + @Override + public double doubleValue() { + return (double)value; + } +} + diff --git a/java/src/main/java/org/msgpack/object/FloatType.java b/java/src/main/java/org/msgpack/object/FloatType.java new file mode 100644 index 0000000..2782dda --- /dev/null +++ b/java/src/main/java/org/msgpack/object/FloatType.java @@ -0,0 +1,28 @@ +// +// 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 org.msgpack.*; + +public abstract class FloatType extends MessagePackObject { + @Override + public boolean isFloatType() { + return true; + } +} + diff --git a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java new file mode 100644 index 0000000..75a5070 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java @@ -0,0 +1,70 @@ +// +// 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 org.msgpack.*; + +class FloatTypeIMPL extends FloatType { + private float value; + + public FloatTypeIMPL(float vlaue) { + 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; + } +} + diff --git a/java/src/main/java/org/msgpack/object/IntegerType.java b/java/src/main/java/org/msgpack/object/IntegerType.java new file mode 100644 index 0000000..d6a9b54 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/IntegerType.java @@ -0,0 +1,49 @@ +// +// 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 org.msgpack.*; + +public abstract class IntegerType extends MessagePackObject { + public static IntegerType create(byte value) { + return new ShortIntegerTypeIMPL((int)value); + } + + public static IntegerType create(short value) { + return new ShortIntegerTypeIMPL((int)value); + } + + public static IntegerType create(int value) { + return new ShortIntegerTypeIMPL(value); + } + + public static IntegerType create(long value) { + return new LongIntegerTypeIMPL(value); + } + + public static IntegerType create(BigInteger value) { + return new BigIntegerTypeIMPL(value); + } + + @Override + public boolean isIntegerType() { + return true; + } +} + diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java new file mode 100644 index 0000000..c914e91 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java @@ -0,0 +1,89 @@ +// +// 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 org.msgpack.*; + +class LongIntegerTypeIMPL extends IntegerType { + private long value; + + LongIntegerTypeIMPL(long value) { + this.value = value; + } + + @Override + public byte asByte() { + if(value > (long)Byte.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (byte)value; + } + + @Override + public short asShort() { + if(value > (long)Short.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (short)value; + } + + @Override + public int asInt() { + if(value > (long)Integer.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (int)value; + } + + @Override + public long asLong() { + 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 float floatValue() { + return (float)value; + } + + @Override + public double doubleValue() { + return (double)value; + } +} + diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java new file mode 100644 index 0000000..dbd145b --- /dev/null +++ b/java/src/main/java/org/msgpack/object/MapType.java @@ -0,0 +1,48 @@ +// +// 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.HashMap; +import java.util.Map; +import org.msgpack.*; + +public class MapType extends MessagePackObject { + MessagePackObject[] map; + + public MapType(MessagePackObject[] map) { + this.map = map; + } + + @Override + public boolean isMapType() { + return false; + } + + @Override + public Map asMap() { + HashMap m = new HashMap(map.length / 2); + int i = 0; + while(i < map.length) { + MessagePackObject k = map[i++]; + MessagePackObject v = map[i++]; + m.put(k, v); + } + return m; + } +} + diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java new file mode 100644 index 0000000..c36ff05 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/NilType.java @@ -0,0 +1,28 @@ +// +// 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 org.msgpack.*; + +public class NilType extends MessagePackObject { + @Override + public boolean isNull() { + return true; + } +} + diff --git a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java new file mode 100644 index 0000000..a725950 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java @@ -0,0 +1,91 @@ +// +// 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 org.msgpack.*; + +class ShortIntegerTypeIMPL extends IntegerType { + private int value; + + ShortIntegerTypeIMPL(int value) { + this.value = value; + } + + @Override + public byte asByte() { + if(value > (int)Byte.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (byte)value; + } + + @Override + public short asShort() { + if(value > (int)Short.MAX_VALUE) { + throw new MessageTypeException("type error"); + } + return (short)value; + } + + @Override + public int asInt() { + return value; + } + + @Override + public long asLong() { + return value; + } + + @Override + public BigInteger asBigInteger() { + return BigInteger.valueOf((long)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; + } +} + From 02ae247536ec5570c3a150de8283ef399aaf82eb Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 24 Jul 2010 18:20:00 +0900 Subject: [PATCH 119/152] java: adds MessagePackObject class 2 --- .../msgpack/object/BigIntegerTypeIMPL.java | 2 +- .../java/org/msgpack/object/BooleanType.java | 2 +- .../org/msgpack/object/DoubleTypeIMPL.java | 2 +- .../java/org/msgpack/object/FloatType.java | 8 ++++ .../org/msgpack/object/FloatTypeIMPL.java | 2 +- .../java/org/msgpack/object/IntegerType.java | 10 ++-- .../main/java/org/msgpack/object/MapType.java | 4 +- .../main/java/org/msgpack/object/RawType.java | 48 +++++++++++++++++++ 8 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 java/src/main/java/org/msgpack/object/RawType.java diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java index 1d638c9..1ebb83d 100644 --- a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -23,7 +23,7 @@ import org.msgpack.*; class BigIntegerTypeIMPL extends IntegerType { private BigInteger value; - BigIntegerTypeIMPL(BigInteger vlaue) { + BigIntegerTypeIMPL(BigInteger value) { this.value = value; } diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java index c9e84b6..d272b6f 100644 --- a/java/src/main/java/org/msgpack/object/BooleanType.java +++ b/java/src/main/java/org/msgpack/object/BooleanType.java @@ -28,7 +28,7 @@ public class BooleanType extends MessagePackObject { @Override public boolean isBooleanType() { - return false; + return true; } @Override diff --git a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java index 0e33d5b..8bbc52a 100644 --- a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java @@ -23,7 +23,7 @@ import org.msgpack.*; class DoubleTypeIMPL extends FloatType { private double value; - public DoubleTypeIMPL(double vlaue) { + public DoubleTypeIMPL(double value) { this.value = value; } diff --git a/java/src/main/java/org/msgpack/object/FloatType.java b/java/src/main/java/org/msgpack/object/FloatType.java index 2782dda..514efd5 100644 --- a/java/src/main/java/org/msgpack/object/FloatType.java +++ b/java/src/main/java/org/msgpack/object/FloatType.java @@ -24,5 +24,13 @@ public abstract class FloatType extends MessagePackObject { 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); + } } diff --git a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java index 75a5070..8821640 100644 --- a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java @@ -23,7 +23,7 @@ import org.msgpack.*; class FloatTypeIMPL extends FloatType { private float value; - public FloatTypeIMPL(float vlaue) { + public FloatTypeIMPL(float value) { this.value = value; } diff --git a/java/src/main/java/org/msgpack/object/IntegerType.java b/java/src/main/java/org/msgpack/object/IntegerType.java index d6a9b54..43357e8 100644 --- a/java/src/main/java/org/msgpack/object/IntegerType.java +++ b/java/src/main/java/org/msgpack/object/IntegerType.java @@ -21,6 +21,11 @@ import java.math.BigInteger; import org.msgpack.*; public abstract class IntegerType extends MessagePackObject { + @Override + public boolean isIntegerType() { + return true; + } + public static IntegerType create(byte value) { return new ShortIntegerTypeIMPL((int)value); } @@ -40,10 +45,5 @@ public abstract class IntegerType extends MessagePackObject { public static IntegerType create(BigInteger value) { return new BigIntegerTypeIMPL(value); } - - @Override - public boolean isIntegerType() { - return true; - } } diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java index dbd145b..359ebe6 100644 --- a/java/src/main/java/org/msgpack/object/MapType.java +++ b/java/src/main/java/org/msgpack/object/MapType.java @@ -22,7 +22,7 @@ import java.util.Map; import org.msgpack.*; public class MapType extends MessagePackObject { - MessagePackObject[] map; + private MessagePackObject[] map; public MapType(MessagePackObject[] map) { this.map = map; @@ -30,7 +30,7 @@ public class MapType extends MessagePackObject { @Override public boolean isMapType() { - return false; + return true; } @Override diff --git a/java/src/main/java/org/msgpack/object/RawType.java b/java/src/main/java/org/msgpack/object/RawType.java new file mode 100644 index 0000000..107ba27 --- /dev/null +++ b/java/src/main/java/org/msgpack/object/RawType.java @@ -0,0 +1,48 @@ +// +// 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 org.msgpack.*; + +class RawType extends MessagePackObject { + private byte[] bytes; + + public RawType(byte[] bytes) { + this.bytes = bytes; + } + + @Override + public boolean isRawType() { + return true; + } + + @Override + public byte[] asByteArray() { + return bytes; + } + + @Override + public String asString() { + try { + return new String(bytes, "UTF-8"); + } catch (Exception e) { + throw new MessageTypeException("type error"); + } + } +} + From cd83388f8b035102f260f2ab45a551233dd593da Mon Sep 17 00:00:00 2001 From: Kazuki Ohta Date: Tue, 27 Jul 2010 08:59:09 +0900 Subject: [PATCH 120/152] java: fixed repository location. msgpack.sourceforge.net => msgpack.org --- java/pom.xml | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/java/pom.xml b/java/pom.xml index 9b74b4c..959d2df 100755 --- a/java/pom.xml +++ b/java/pom.xml @@ -7,7 +7,7 @@ MessagePack for Java MessagePack for Java - http://msgpack.sourceforge.net/ + http://msgpack.org/ @@ -97,29 +97,26 @@ - msgpack.sourceforge.net + msgpack.org MessagePack Maven2 Repository - http://msgpack.sourceforge.net/maven2 - - - msgpack.sourceforge.net - MessagePack Maven2 Snapshot Repository - http://msgpack.sourceforge.net/maven2-snapshot + http://msgpack.org/maven2 false - shell.sourceforge.net - Repository at sourceforge.net - scp://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2/ + msgpack.org + Repository at msgpack.org + + file:///Users/otakazuki/soft/website/maven2/ true - shell.sourceforge.net - Repository Name - scp://shell.sourceforge.net/home/groups/m/ms/msgpack/htdocs/maven2-snapshot/ + msgpack.org + Repository at msgpack.org + + file:///Users/otakazuki/soft/website/maven2/ From cba47b635a5a3bab2050fb4e0d099c23f8455867 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 27 Jul 2010 09:50:57 +0900 Subject: [PATCH 121/152] java: changed deploy path to ./target/website/maven2 directory. --- java/pom.xml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/java/pom.xml b/java/pom.xml index 959d2df..44806d5 100755 --- a/java/pom.xml +++ b/java/pom.xml @@ -108,15 +108,13 @@ false msgpack.org Repository at msgpack.org - - file:///Users/otakazuki/soft/website/maven2/ + file://${project.build.directory}/website/maven2/ true msgpack.org Repository at msgpack.org - - file:///Users/otakazuki/soft/website/maven2/ + file://${project.build.directory}/website/maven2/ From 6c91b862c9da56ce0914d430fea2cb388e32d5d5 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 28 Jul 2010 01:17:20 +0900 Subject: [PATCH 122/152] java: MessagePackObject implements Cloneable and MessagePackable interfaces --- .../java/org/msgpack/MessagePackObject.java | 4 ++- java/src/main/java/org/msgpack/Packer.java | 22 +++++++++++++++ .../java/org/msgpack/object/ArrayType.java | 28 +++++++++++++++++-- .../msgpack/object/BigIntegerTypeIMPL.java | 19 +++++++++++++ .../java/org/msgpack/object/BooleanType.java | 19 +++++++++++++ .../org/msgpack/object/DoubleTypeIMPL.java | 19 +++++++++++++ .../org/msgpack/object/FloatTypeIMPL.java | 19 +++++++++++++ .../msgpack/object/LongIntegerTypeIMPL.java | 19 +++++++++++++ .../main/java/org/msgpack/object/MapType.java | 27 ++++++++++++++++++ .../main/java/org/msgpack/object/NilType.java | 19 +++++++++++++ .../main/java/org/msgpack/object/RawType.java | 23 ++++++++++++++- .../msgpack/object/ShortIntegerTypeIMPL.java | 19 +++++++++++++ 12 files changed, 233 insertions(+), 4 deletions(-) diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java index aba27e4..b1a6fab 100644 --- a/java/src/main/java/org/msgpack/MessagePackObject.java +++ b/java/src/main/java/org/msgpack/MessagePackObject.java @@ -22,7 +22,7 @@ import java.util.Set; import java.util.Map; import java.math.BigInteger; -public abstract class MessagePackObject { +public abstract class MessagePackObject implements Cloneable, MessagePackable { public boolean isNull() { return false; } @@ -126,5 +126,7 @@ public abstract class MessagePackObject { public double doubleValue() { throw new MessageTypeException("type error"); } + + abstract public Object clone(); } diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index dd510f3..fbf7e35 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -22,6 +22,7 @@ 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. @@ -194,6 +195,27 @@ public class Packer { return this; } + public Packer packBigInteger(BigInteger d) throws IOException { + if(d.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) { + return packLong(d.longValue()); + } else if(d.bitLength() <= 64) { + castBytes[0] = (byte)0xcf; + byte[] barray = d.toByteArray(); + castBytes[1] = barray[0]; + castBytes[2] = barray[1]; + castBytes[3] = barray[2]; + castBytes[4] = barray[3]; + castBytes[5] = barray[4]; + castBytes[6] = barray[5]; + castBytes[7] = barray[6]; + castBytes[8] = barray[7]; + out.write(castBytes); + return this; + } else { + throw new MessageTypeException("can't BigInteger larger than 0xffffffffffffffff"); + } + } + public Packer packFloat(float d) throws IOException { castBytes[0] = (byte)0xca; castBuffer.putFloat(1, d); diff --git a/java/src/main/java/org/msgpack/object/ArrayType.java b/java/src/main/java/org/msgpack/object/ArrayType.java index 06e9b16..694f53f 100644 --- a/java/src/main/java/org/msgpack/object/ArrayType.java +++ b/java/src/main/java/org/msgpack/object/ArrayType.java @@ -18,9 +18,8 @@ package org.msgpack.object; import java.util.List; -import java.util.Set; -import java.util.Map; import java.util.Arrays; +import java.io.IOException; import org.msgpack.*; public class ArrayType extends MessagePackObject { @@ -44,5 +43,30 @@ public class ArrayType extends MessagePackObject { public List 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 Object clone() { + MessagePackObject[] copy = new MessagePackObject[array.length]; + for(int i=0; i < array.length; i++) { + copy[i] = (MessagePackObject)array[i].clone(); + } + return copy; + } } diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java index 1ebb83d..f7c73ae 100644 --- a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -18,6 +18,7 @@ package org.msgpack.object; import java.math.BigInteger; +import java.io.IOException; import org.msgpack.*; class BigIntegerTypeIMPL extends IntegerType { @@ -88,5 +89,23 @@ class BigIntegerTypeIMPL extends IntegerType { 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()) { + return false; + } + return ((BigIntegerTypeIMPL)obj).value.equals(value); + } + + @Override + public Object clone() { + return new BigIntegerTypeIMPL(value); + } } diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java index d272b6f..c6e4f30 100644 --- a/java/src/main/java/org/msgpack/object/BooleanType.java +++ b/java/src/main/java/org/msgpack/object/BooleanType.java @@ -17,6 +17,7 @@ // package org.msgpack.object; +import java.io.IOException; import org.msgpack.*; public class BooleanType extends MessagePackObject { @@ -35,5 +36,23 @@ public class BooleanType extends MessagePackObject { 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 Object clone() { + return new BooleanType(value); + } } diff --git a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java index 8bbc52a..dafe540 100644 --- a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java @@ -18,6 +18,7 @@ package org.msgpack.object; import java.math.BigInteger; +import java.io.IOException; import org.msgpack.*; class DoubleTypeIMPL extends FloatType { @@ -67,5 +68,23 @@ class DoubleTypeIMPL extends FloatType { 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 Object clone() { + return new DoubleTypeIMPL(value); + } } diff --git a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java index 8821640..234b2ad 100644 --- a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java @@ -18,6 +18,7 @@ package org.msgpack.object; import java.math.BigInteger; +import java.io.IOException; import org.msgpack.*; class FloatTypeIMPL extends FloatType { @@ -66,5 +67,23 @@ class FloatTypeIMPL extends FloatType { 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 Object clone() { + return new FloatTypeIMPL(value); + } } diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java index c914e91..0ce22a2 100644 --- a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java @@ -18,6 +18,7 @@ package org.msgpack.object; import java.math.BigInteger; +import java.io.IOException; import org.msgpack.*; class LongIntegerTypeIMPL extends IntegerType { @@ -85,5 +86,23 @@ class LongIntegerTypeIMPL extends IntegerType { public double doubleValue() { return (double)value; } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packLong(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return ((LongIntegerTypeIMPL)obj).value == value; + } + + @Override + public Object clone() { + return new LongIntegerTypeIMPL(value); + } } diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java index 359ebe6..d456e78 100644 --- a/java/src/main/java/org/msgpack/object/MapType.java +++ b/java/src/main/java/org/msgpack/object/MapType.java @@ -19,6 +19,8 @@ package org.msgpack.object; import java.util.HashMap; import java.util.Map; +import java.util.Arrays; +import java.io.IOException; import org.msgpack.*; public class MapType extends MessagePackObject { @@ -44,5 +46,30 @@ public class MapType extends MessagePackObject { } return m; } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packMap(map.length / 2); + for(int i=0; i < map.length; i++) { + map[i].messagePack(pk); + } + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return Arrays.equals(((MapType)obj).map, map); + } + + @Override + public Object clone() { + MessagePackObject[] copy = new MessagePackObject[map.length]; + for(int i=0; i < map.length; i++) { + copy[i] = (MessagePackObject)map[i].clone(); + } + return copy; + } } diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java index c36ff05..7a46376 100644 --- a/java/src/main/java/org/msgpack/object/NilType.java +++ b/java/src/main/java/org/msgpack/object/NilType.java @@ -17,6 +17,7 @@ // package org.msgpack.object; +import java.io.IOException; import org.msgpack.*; public class NilType extends MessagePackObject { @@ -24,5 +25,23 @@ public class NilType extends MessagePackObject { public boolean isNull() { return true; } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packNil(); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return true; + } + + @Override + public Object clone() { + return new NilType(); + } } diff --git a/java/src/main/java/org/msgpack/object/RawType.java b/java/src/main/java/org/msgpack/object/RawType.java index 107ba27..18a419d 100644 --- a/java/src/main/java/org/msgpack/object/RawType.java +++ b/java/src/main/java/org/msgpack/object/RawType.java @@ -17,9 +17,11 @@ // package org.msgpack.object; +import java.util.Arrays; +import java.io.IOException; import org.msgpack.*; -class RawType extends MessagePackObject { +public class RawType extends MessagePackObject { private byte[] bytes; public RawType(byte[] bytes) { @@ -44,5 +46,24 @@ class RawType extends MessagePackObject { throw new MessageTypeException("type error"); } } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packRaw(bytes.length); + pk.packRawBody(bytes); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return ((RawType)obj).bytes.equals(bytes); + } + + @Override + public Object clone() { + return new RawType((byte[])bytes.clone()); + } } diff --git a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java index a725950..83a4daf 100644 --- a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java @@ -18,6 +18,7 @@ package org.msgpack.object; import java.math.BigInteger; +import java.io.IOException; import org.msgpack.*; class ShortIntegerTypeIMPL extends IntegerType { @@ -87,5 +88,23 @@ class ShortIntegerTypeIMPL extends IntegerType { public double doubleValue() { return (double)value; } + + @Override + public void messagePack(Packer pk) throws IOException { + pk.packInt(value); + } + + @Override + public boolean equals(Object obj) { + if(obj.getClass() != getClass()) { + return false; + } + return ((ShortIntegerTypeIMPL)obj).value == value; + } + + @Override + public Object clone() { + return new ShortIntegerTypeIMPL(value); + } } From d3bb37d113575e8b36f44c06ac9eeb6b406aa298 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 10 Aug 2010 14:11:25 +0900 Subject: [PATCH 123/152] java: fixes MapSchema --- java/src/main/java/org/msgpack/schema/MapSchema.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/main/java/org/msgpack/schema/MapSchema.java b/java/src/main/java/org/msgpack/schema/MapSchema.java index 339a5c2..2e09af3 100644 --- a/java/src/main/java/org/msgpack/schema/MapSchema.java +++ b/java/src/main/java/org/msgpack/schema/MapSchema.java @@ -72,7 +72,7 @@ public class MapSchema extends Schema implements IMapSchema { dest.put((K)keySchema.convert(e.getKey()), (V)valueSchema.convert(e.getValue())); } - return (Map)d; + return dest; } @Override From 057f73a73e0c3ddabe92f1cd2c394fe4afa13514 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 10 Aug 2010 14:11:44 +0900 Subject: [PATCH 124/152] java: implements MessagePackObject::hashCode() --- java/src/main/java/org/msgpack/object/ArrayType.java | 5 +++++ .../main/java/org/msgpack/object/BigIntegerTypeIMPL.java | 5 +++++ java/src/main/java/org/msgpack/object/BooleanType.java | 9 +++++++++ .../src/main/java/org/msgpack/object/DoubleTypeIMPL.java | 6 ++++++ java/src/main/java/org/msgpack/object/FloatTypeIMPL.java | 5 +++++ .../java/org/msgpack/object/LongIntegerTypeIMPL.java | 5 +++++ java/src/main/java/org/msgpack/object/MapType.java | 5 +++++ java/src/main/java/org/msgpack/object/NilType.java | 5 +++++ java/src/main/java/org/msgpack/object/RawType.java | 5 +++++ .../java/org/msgpack/object/ShortIntegerTypeIMPL.java | 5 +++++ 10 files changed, 55 insertions(+) diff --git a/java/src/main/java/org/msgpack/object/ArrayType.java b/java/src/main/java/org/msgpack/object/ArrayType.java index 694f53f..350ce32 100644 --- a/java/src/main/java/org/msgpack/object/ArrayType.java +++ b/java/src/main/java/org/msgpack/object/ArrayType.java @@ -60,6 +60,11 @@ public class ArrayType extends MessagePackObject { return Arrays.equals(((ArrayType)obj).array, array); } + @Override + public int hashCode() { + return array.hashCode(); + } + @Override public Object clone() { MessagePackObject[] copy = new MessagePackObject[array.length]; diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java index f7c73ae..fd517e7 100644 --- a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -103,6 +103,11 @@ class BigIntegerTypeIMPL extends IntegerType { return ((BigIntegerTypeIMPL)obj).value.equals(value); } + @Override + public int hashCode() { + return value.hashCode(); + } + @Override public Object clone() { return new BigIntegerTypeIMPL(value); diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java index c6e4f30..1d12c1c 100644 --- a/java/src/main/java/org/msgpack/object/BooleanType.java +++ b/java/src/main/java/org/msgpack/object/BooleanType.java @@ -50,6 +50,15 @@ public class BooleanType extends MessagePackObject { return ((BooleanType)obj).value == value; } + @Override + public int hashCode() { + if(value) { + return 1231; + } else { + return 1237; + } + } + @Override public Object clone() { return new BooleanType(value); diff --git a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java index dafe540..b47a709 100644 --- a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java @@ -82,6 +82,12 @@ class DoubleTypeIMPL extends FloatType { 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); diff --git a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java index 234b2ad..1d79961 100644 --- a/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/FloatTypeIMPL.java @@ -81,6 +81,11 @@ class FloatTypeIMPL extends FloatType { return ((FloatTypeIMPL)obj).value == value; } + @Override + public int hashCode() { + return Float.floatToIntBits(value); + } + @Override public Object clone() { return new FloatTypeIMPL(value); diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java index 0ce22a2..940ab6f 100644 --- a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java @@ -100,6 +100,11 @@ class LongIntegerTypeIMPL extends IntegerType { return ((LongIntegerTypeIMPL)obj).value == value; } + @Override + public int hashCode() { + return (int)(value^(value>>>32)); + } + @Override public Object clone() { return new LongIntegerTypeIMPL(value); diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java index d456e78..619d388 100644 --- a/java/src/main/java/org/msgpack/object/MapType.java +++ b/java/src/main/java/org/msgpack/object/MapType.java @@ -63,6 +63,11 @@ public class MapType extends MessagePackObject { return Arrays.equals(((MapType)obj).map, map); } + @Override + public int hashCode() { + return map.hashCode(); + } + @Override public Object clone() { MessagePackObject[] copy = new MessagePackObject[map.length]; diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java index 7a46376..ece62f0 100644 --- a/java/src/main/java/org/msgpack/object/NilType.java +++ b/java/src/main/java/org/msgpack/object/NilType.java @@ -39,6 +39,11 @@ public class NilType extends MessagePackObject { return true; } + @Override + public int hashCode() { + return 0; + } + @Override public Object clone() { return new NilType(); diff --git a/java/src/main/java/org/msgpack/object/RawType.java b/java/src/main/java/org/msgpack/object/RawType.java index 18a419d..3a39486 100644 --- a/java/src/main/java/org/msgpack/object/RawType.java +++ b/java/src/main/java/org/msgpack/object/RawType.java @@ -61,6 +61,11 @@ public class RawType extends MessagePackObject { return ((RawType)obj).bytes.equals(bytes); } + @Override + public int hashCode() { + return bytes.hashCode(); + } + @Override public Object clone() { return new RawType((byte[])bytes.clone()); diff --git a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java index 83a4daf..60e92b8 100644 --- a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java @@ -102,6 +102,11 @@ class ShortIntegerTypeIMPL extends IntegerType { return ((ShortIntegerTypeIMPL)obj).value == value; } + @Override + public int hashCode() { + return value; + } + @Override public Object clone() { return new ShortIntegerTypeIMPL(value); From 8c67087a154da0e7cdc32c0b676d2956ce1d0f47 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 16:32:42 +0900 Subject: [PATCH 125/152] java: adds MessagePackObject.bigIntegerValue(), asBigInteger() and equals() --- .../main/java/org/msgpack/MessagePackObject.java | 4 ++++ .../org/msgpack/object/BigIntegerTypeIMPL.java | 15 +++++++++++++++ .../java/org/msgpack/object/DoubleTypeIMPL.java | 5 +++++ .../org/msgpack/object/LongIntegerTypeIMPL.java | 15 +++++++++++++++ .../org/msgpack/object/ShortIntegerTypeIMPL.java | 10 ++++++++++ 5 files changed, 49 insertions(+) diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java index b1a6fab..6181f7a 100644 --- a/java/src/main/java/org/msgpack/MessagePackObject.java +++ b/java/src/main/java/org/msgpack/MessagePackObject.java @@ -119,6 +119,10 @@ public abstract class MessagePackObject implements Cloneable, MessagePackable { throw new MessageTypeException("type error"); } + public BigInteger bigIntegerValue() { + throw new MessageTypeException("type error"); + } + public float floatValue() { throw new MessageTypeException("type error"); } diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java index fd517e7..7b060ee 100644 --- a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -60,6 +60,11 @@ class BigIntegerTypeIMPL extends IntegerType { return value.longValue(); } + @Override + public BigInteger asBigInteger() { + return value; + } + @Override public byte byteValue() { return value.byteValue(); @@ -80,6 +85,11 @@ class BigIntegerTypeIMPL extends IntegerType { return value.longValue(); } + @Override + public BigInteger bigIntegerValue() { + return value; + } + @Override public float floatValue() { return value.floatValue(); @@ -98,6 +108,11 @@ class BigIntegerTypeIMPL extends IntegerType { @Override public boolean equals(Object obj) { if(obj.getClass() != getClass()) { + if(obj.getClass() == ShortIntegerTypeIMPL.class) { + return BigInteger.valueOf((long)((ShortIntegerTypeIMPL)obj).shortValue()).equals(value); + } else if(obj.getClass() == LongIntegerTypeIMPL.class) { + return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value); + } return false; } return ((BigIntegerTypeIMPL)obj).value.equals(value); diff --git a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java index b47a709..fd38089 100644 --- a/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/DoubleTypeIMPL.java @@ -59,6 +59,11 @@ class DoubleTypeIMPL extends FloatType { return (long)value; } + @Override + public BigInteger bigIntegerValue() { + return BigInteger.valueOf((long)value); + } + @Override public float floatValue() { return (float)value; diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java index 940ab6f..3928a29 100644 --- a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java @@ -57,6 +57,11 @@ class LongIntegerTypeIMPL extends IntegerType { return value; } + @Override + public BigInteger asBigInteger() { + return BigInteger.valueOf(value); + } + @Override public byte byteValue() { return (byte)value; @@ -77,6 +82,11 @@ class LongIntegerTypeIMPL extends IntegerType { return (long)value; } + @Override + public BigInteger bigIntegerValue() { + return BigInteger.valueOf(value); + } + @Override public float floatValue() { return (float)value; @@ -95,6 +105,11 @@ class LongIntegerTypeIMPL extends IntegerType { @Override public boolean equals(Object obj) { if(obj.getClass() != getClass()) { + if(obj.getClass() == ShortIntegerTypeIMPL.class) { + return value == ((ShortIntegerTypeIMPL)obj).longValue(); + } else if(obj.getClass() == BigIntegerTypeIMPL.class) { + return (long)value == ((BigIntegerTypeIMPL)obj).longValue(); + } return false; } return ((LongIntegerTypeIMPL)obj).value == value; diff --git a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java index 60e92b8..dbed426 100644 --- a/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/ShortIntegerTypeIMPL.java @@ -79,6 +79,11 @@ class ShortIntegerTypeIMPL extends IntegerType { return (long)value; } + @Override + public BigInteger bigIntegerValue() { + return BigInteger.valueOf((long)value); + } + @Override public float floatValue() { return (float)value; @@ -97,6 +102,11 @@ class ShortIntegerTypeIMPL extends IntegerType { @Override public boolean equals(Object obj) { if(obj.getClass() != getClass()) { + if(obj.getClass() == LongIntegerTypeIMPL.class) { + return (long)value == ((LongIntegerTypeIMPL)obj).longValue(); + } else if(obj.getClass() == BigIntegerTypeIMPL.class) { + return ((BigIntegerTypeIMPL)obj).bigIntegerValue().equals(BigInteger.valueOf((long)value)); + } return false; } return ((ShortIntegerTypeIMPL)obj).value == value; From 8b79e6d3c72a02f4dc039799e3cd370c06e966b0 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 18:10:30 +0900 Subject: [PATCH 126/152] java: uses MessagePackObject instead of Object for type of deserialized objects --- .../org/msgpack/BufferedUnpackerImpl.java | 8 +- .../org/msgpack/MessageTypeException.java | 10 - java/src/main/java/org/msgpack/Packer.java | 6 - java/src/main/java/org/msgpack/Schema.java | 78 ---- .../main/java/org/msgpack/UnpackIterator.java | 4 +- .../main/java/org/msgpack/UnpackResult.java | 6 +- java/src/main/java/org/msgpack/Unpacker.java | 30 +- .../main/java/org/msgpack/UnpackerImpl.java | 132 ++---- .../org/msgpack/schema/BooleanSchema.java | 64 --- .../org/msgpack/schema/ByteArraySchema.java | 97 ---- .../java/org/msgpack/schema/ByteSchema.java | 96 ---- .../org/msgpack/schema/ClassGenerator.java | 244 ---------- .../java/org/msgpack/schema/ClassSchema.java | 91 ---- .../java/org/msgpack/schema/DoubleSchema.java | 74 --- .../java/org/msgpack/schema/FieldSchema.java | 43 -- .../java/org/msgpack/schema/FloatSchema.java | 74 --- .../msgpack/schema/GenericClassSchema.java | 87 ---- .../org/msgpack/schema/GenericSchema.java | 129 ------ .../java/org/msgpack/schema/IArraySchema.java | 26 -- .../java/org/msgpack/schema/IMapSchema.java | 27 -- .../java/org/msgpack/schema/IntSchema.java | 96 ---- .../java/org/msgpack/schema/ListSchema.java | 111 ----- .../java/org/msgpack/schema/LongSchema.java | 80 ---- .../java/org/msgpack/schema/MapSchema.java | 106 ----- .../msgpack/schema/ReflectionClassSchema.java | 64 --- .../org/msgpack/schema/SSchemaParser.java | 264 ----------- .../java/org/msgpack/schema/SetSchema.java | 115 ----- .../java/org/msgpack/schema/ShortSchema.java | 93 ---- .../msgpack/schema/SpecificClassSchema.java | 122 ----- .../java/org/msgpack/schema/StringSchema.java | 102 ---- .../test/java/org/msgpack/TestPackUnpack.java | 435 +++++++++--------- 31 files changed, 270 insertions(+), 2644 deletions(-) delete mode 100644 java/src/main/java/org/msgpack/Schema.java delete mode 100644 java/src/main/java/org/msgpack/schema/BooleanSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/ByteArraySchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/ByteSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/ClassGenerator.java delete mode 100644 java/src/main/java/org/msgpack/schema/ClassSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/DoubleSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/FieldSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/FloatSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/GenericClassSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/GenericSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/IArraySchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/IMapSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/IntSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/ListSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/LongSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/MapSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/ReflectionClassSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/SSchemaParser.java delete mode 100644 java/src/main/java/org/msgpack/schema/SetSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/ShortSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/SpecificClassSchema.java delete mode 100644 java/src/main/java/org/msgpack/schema/StringSchema.java diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index f4ed35b..9496238 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -47,7 +47,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { offset = noffset; } while(!super.isFinished()); - Object obj = super.getData(); + MessagePackObject obj = super.getData(); super.reset(); result.done(obj); @@ -198,7 +198,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { { long o = castBuffer.getLong(0); if(o < 0) { - // FIXME + // FIXME unpackBigInteger throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported"); } advance(9); @@ -231,6 +231,8 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } + // FIXME unpackBigInteger + final float unpackFloat() throws IOException, MessageTypeException { more(1); int b = buffer[offset]; @@ -414,7 +416,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { return s; } - final Object unpackObject() throws IOException { + final MessagePackObject unpackObject() throws IOException { UnpackResult result = new UnpackResult(); if(!next(result)) { super.reset(); diff --git a/java/src/main/java/org/msgpack/MessageTypeException.java b/java/src/main/java/org/msgpack/MessageTypeException.java index 0fa37b7..698ef6d 100644 --- a/java/src/main/java/org/msgpack/MessageTypeException.java +++ b/java/src/main/java/org/msgpack/MessageTypeException.java @@ -23,15 +23,5 @@ public class MessageTypeException extends RuntimeException { 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()); - } - */ } diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index fbf7e35..98af3d6 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -308,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); diff --git a/java/src/main/java/org/msgpack/Schema.java b/java/src/main/java/org/msgpack/Schema.java deleted file mode 100644 index 25e10f9..0000000 --- a/java/src/main/java/org/msgpack/Schema.java +++ /dev/null @@ -1,78 +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 { - public Schema() { } - - public abstract String getClassName(); - public abstract String getExpression(); - - public static Schema parse(String source) { - return SSchemaParser.parse(source); - } - - public static Schema load(String source) { - return SSchemaParser.load(source); - } - - 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 MessageTypeException("type error"); - } - - public Object createFromByte(byte v) { - throw new MessageTypeException("type error"); - } - - public Object createFromShort(short v) { - throw new MessageTypeException("type error"); - } - - public Object createFromInt(int v) { - throw new MessageTypeException("type error"); - } - - public Object createFromLong(long v) { - throw new MessageTypeException("type error"); - } - - public Object createFromFloat(float v) { - throw new MessageTypeException("type error"); - } - - public Object createFromDouble(double v) { - throw new MessageTypeException("type error"); - } - - public Object createFromRaw(byte[] b, int offset, int length) { - throw new MessageTypeException("type error"); - } -} - diff --git a/java/src/main/java/org/msgpack/UnpackIterator.java b/java/src/main/java/org/msgpack/UnpackIterator.java index f17e229..8c778b6 100644 --- a/java/src/main/java/org/msgpack/UnpackIterator.java +++ b/java/src/main/java/org/msgpack/UnpackIterator.java @@ -21,7 +21,7 @@ import java.io.IOException; import java.util.Iterator; import java.util.NoSuchElementException; -public class UnpackIterator extends UnpackResult implements Iterator { +public class UnpackIterator extends UnpackResult implements Iterator { private Unpacker pac; UnpackIterator(Unpacker pac) { @@ -38,7 +38,7 @@ public class UnpackIterator extends UnpackResult implements Iterator { } } - public Object next() { + public MessagePackObject next() { if(!finished && !hasNext()) { throw new NoSuchElementException(); } diff --git a/java/src/main/java/org/msgpack/UnpackResult.java b/java/src/main/java/org/msgpack/UnpackResult.java index cec18a1..bb981c1 100644 --- a/java/src/main/java/org/msgpack/UnpackResult.java +++ b/java/src/main/java/org/msgpack/UnpackResult.java @@ -19,13 +19,13 @@ package org.msgpack; public class UnpackResult { protected boolean finished = false; - protected Object data = null; + protected MessagePackObject data = null; public boolean isFinished() { return finished; } - public Object getData() { + public MessagePackObject getData() { return data; } @@ -34,7 +34,7 @@ public class UnpackResult { data = null; } - void done(Object obj) { + void done(MessagePackObject obj) { finished = true; data = obj; } diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 32bab64..3a95243 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -26,8 +26,8 @@ import java.nio.ByteBuffer; /** * Unpacker enables you to deserialize objects from stream. * - * Unpacker provides Buffered API, Unbuffered API, Schema API - * and Direct Conversion API. + * 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: @@ -39,7 +39,7 @@ import java.nio.ByteBuffer; * UnpackResult result = pac.next(); * * // use an iterator. - * for(Object obj : pac) { + * for(MessagePackObject obj : pac) { * // use MessageConvertable interface to convert the * // the generic object to the specific type. * } @@ -56,7 +56,7 @@ import java.nio.ByteBuffer; * pac.feed(input_bytes); * * // use next() method or iterators. - * for(Object obj : pac) { + * for(MessagePackObject obj : pac) { * // ... * } * @@ -75,7 +75,7 @@ import java.nio.ByteBuffer; * System.in.read(pac.getBuffer(), pac.getBufferOffset(), pac.getBufferCapacity()); * * // use next() method or iterators. - * for(Object obj : pac) { + * for(MessagePackObject obj : pac) { * // ... * } * @@ -96,12 +96,12 @@ import java.nio.ByteBuffer; * * // take out object if deserialized object is ready. * if(pac.isFinished()) { - * Object obj = pac.getData(); + * MessagePackObject obj = pac.getData(); * // ... * } * */ -public class Unpacker implements Iterable { +public class Unpacker implements Iterable { // buffer: // +---------------------------------------------+ @@ -170,16 +170,6 @@ public class Unpacker implements Iterable { this.stream = stream; } - /** - * Sets schema to convert deserialized object into specific type. - * Default schema is {@link GenericSchema} that leaves objects for generic type. Use {@link MessageConvertable#messageConvert(Object)} method to convert the generic object. - * @param s schem to use - */ - public Unpacker useSchema(Schema s) { - impl.setSchema(s); - return this; - } - /** * Gets the input stream. @@ -255,7 +245,7 @@ public class Unpacker implements Iterable { /** * Returns the iterator that calls {@link next()} method repeatedly. */ - public Iterator iterator() { + public Iterator iterator() { return new UnpackIterator(this); } @@ -387,7 +377,7 @@ public class Unpacker implements Iterable { /** * Gets the object deserialized by {@link execute(byte[])} method. */ - public Object getData() { + public MessagePackObject getData() { return impl.getData(); } @@ -557,7 +547,7 @@ public class Unpacker implements Iterable { * Gets one {@code Object} value from the buffer. * This method calls {@link fill()} method if needed. */ - final public Object unpackObject() throws IOException { + final public MessagePackObject unpackObject() throws IOException { return impl.unpackObject(); } diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java index 10cf5f0..f004e6c 100644 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -18,11 +18,7 @@ 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 org.msgpack.object.*; public class UnpackerImpl { static final int CS_HEADER = 0x00; @@ -55,30 +51,19 @@ 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 static final Schema GENERIC_SCHEMA = new GenericSchema(); - private Schema rootSchema; + private MessagePackObject data = null; public UnpackerImpl() { - setSchema(GENERIC_SCHEMA); - } - - public void setSchema(Schema schema) - { - this.rootSchema = schema; reset(); } - public final Object getData() + public final MessagePackObject getData() { return data; } @@ -94,7 +79,6 @@ public class UnpackerImpl { top_ct = 0; top_count = 0; top_obj = null; - top_schema = rootSchema; } public final void reset() @@ -127,20 +111,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 = new RawType(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -151,25 +135,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]; + obj = new MessagePackObject[count]; if(count == 0) { - obj = ((IArraySchema)top_schema).createFromArray((Object[])obj); + obj = new ArrayType((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; } @@ -177,13 +156,10 @@ 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]; + obj = new MessagePackObject[count*2]; if(count == 0) { - obj = ((IMapSchema)top_schema).createFromMap((Object[])obj); + obj = new MapType((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -191,23 +167,21 @@ public class UnpackerImpl { 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 = new NilType(); break _push; case 0xc2: // false - obj = top_schema.createFromBoolean(false); + obj = new BooleanType(false); break _push; case 0xc3: // true - obj = top_schema.createFromBoolean(true); + obj = new BooleanType(true); break _push; case 0xca: // float case 0xcb: // double @@ -251,13 +225,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: @@ -265,7 +239,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: @@ -273,13 +247,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: @@ -292,34 +266,34 @@ public class UnpackerImpl { //obj = GenericBigInteger.valueOf(o & 0x7fffffffL).setBit(31); throw new UnpackException("uint 64 bigger than 0x7fffffff is not supported"); } 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 = new RawType(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -330,77 +304,67 @@ public class UnpackerImpl { // FIXME overflow check trail = castBuffer.getInt(0) & 0x7fffffff; if(trail == 0) { - obj = top_schema.createFromRaw(new byte[0], 0, 0); + obj = new RawType(new byte[0]); break _push; } cs = ACS_RAW_VALUE; - case ACS_RAW_VALUE: - obj = top_schema.createFromRaw(src, n, trail); + case ACS_RAW_VALUE: { + byte[] raw = new byte[trail]; + System.arraycopy(src, n, raw, 0, trail); + obj = new RawType(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]; + obj = new MessagePackObject[count]; if(count == 0) { - obj = ((IArraySchema)top_schema).createFromArray((Object[])obj); + obj = new ArrayType((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]; + obj = new MessagePackObject[count]; if(count == 0) { - obj = ((IArraySchema)top_schema).createFromArray((Object[])obj); + obj = new ArrayType((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]; + obj = new MessagePackObject[count*2]; if(count == 0) { - obj = ((IMapSchema)top_schema).createFromMap((Object[])obj); + obj = new MapType((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -408,26 +372,21 @@ public class UnpackerImpl { 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]; + obj = new MessagePackObject[count*2]; if(count == 0) { - obj = ((IMapSchema)top_schema).createFromMap((Object[])obj); + obj = new MapType((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -435,11 +394,9 @@ public class UnpackerImpl { 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"); @@ -454,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; } @@ -468,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 = new ArrayType((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; } @@ -484,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: { @@ -495,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 = new MapType((MessagePackObject[])mp); stack_obj[top] = null; - stack_schema[top] = null; --top; break _push; } diff --git a/java/src/main/java/org/msgpack/schema/BooleanSchema.java b/java/src/main/java/org/msgpack/schema/BooleanSchema.java deleted file mode 100644 index 2c325f1..0000000 --- a/java/src/main/java/org/msgpack/schema/BooleanSchema.java +++ /dev/null @@ -1,64 +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.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class BooleanSchema extends Schema { - public BooleanSchema() { } - - @Override - public String getClassName() { - return "Boolean"; - } - - @Override - public String getExpression() { - return "boolean"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Boolean) { - pk.packBoolean((Boolean)obj); - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final boolean convertBoolean(Object obj) throws MessageTypeException { - if(obj instanceof Boolean) { - return (Boolean)obj; - } - throw new MessageTypeException(); - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertBoolean(obj); - } - - @Override - public Object createFromBoolean(boolean v) { - return v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ByteArraySchema.java b/java/src/main/java/org/msgpack/schema/ByteArraySchema.java deleted file mode 100644 index af9c0ed..0000000 --- a/java/src/main/java/org/msgpack/schema/ByteArraySchema.java +++ /dev/null @@ -1,97 +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.schema; - -import java.nio.ByteBuffer; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import org.msgpack.*; - -public class ByteArraySchema extends Schema { - public ByteArraySchema() { } - - @Override - public String getClassName() { - return "byte[]"; - } - - @Override - public String getExpression() { - return "raw"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof byte[]) { - byte[] b = (byte[])obj; - pk.packRaw(b.length); - pk.packRawBody(b); - } else if(obj instanceof String) { - try { - byte[] b = ((String)obj).getBytes("UTF-8"); - pk.packRaw(b.length); - pk.packRawBody(b); - } catch (UnsupportedEncodingException e) { - throw new MessageTypeException(); - } - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final byte[] convertByteArray(Object obj) throws MessageTypeException { - if(obj instanceof byte[]) { - // FIXME copy? - //byte[] d = (byte[])obj; - //byte[] v = new byte[d.length]; - //System.arraycopy(d, 0, v, 0, d.length); - //return v; - return (byte[])obj; - } else if(obj instanceof ByteBuffer) { - ByteBuffer d = (ByteBuffer)obj; - byte[] v = new byte[d.capacity()]; - int pos = d.position(); - d.get(v); - d.position(pos); - return v; - } else if(obj instanceof String) { - try { - return ((String)obj).getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new MessageTypeException(); - } - } else { - throw new MessageTypeException(); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertByteArray(obj); - } - - @Override - public Object createFromRaw(byte[] b, int offset, int length) { - byte[] d = new byte[length]; - System.arraycopy(b, offset, d, 0, length); - return d; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ByteSchema.java b/java/src/main/java/org/msgpack/schema/ByteSchema.java deleted file mode 100644 index 6003a0f..0000000 --- a/java/src/main/java/org/msgpack/schema/ByteSchema.java +++ /dev/null @@ -1,96 +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.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class ByteSchema extends Schema { - public ByteSchema() { } - - @Override - public String getClassName() { - return "Byte"; - } - - @Override - public String getExpression() { - return "byte"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - short value = ((Number)obj).shortValue(); - if(value > Byte.MAX_VALUE) { - throw new MessageTypeException(); - } - pk.packByte((byte)value); - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final byte convertByte(Object obj) throws MessageTypeException { - if(obj instanceof Number) { - short value = ((Number)obj).shortValue(); - if(value > Byte.MAX_VALUE) { - throw new MessageTypeException(); - } - return (byte)value; - } - throw new MessageTypeException(); - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertByte(obj); - } - - @Override - public Object createFromByte(byte v) { - return (byte)v; - } - - @Override - public Object createFromShort(short v) { - if(v > Byte.MAX_VALUE) { - throw new MessageTypeException(); - } - return (byte)v; - } - - @Override - public Object createFromInt(int v) { - if(v > Byte.MAX_VALUE) { - throw new MessageTypeException(); - } - return (byte)v; - } - - @Override - public Object createFromLong(long v) { - if(v > Byte.MAX_VALUE) { - throw new MessageTypeException(); - } - return (byte)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ClassGenerator.java b/java/src/main/java/org/msgpack/schema/ClassGenerator.java deleted file mode 100644 index a515996..0000000 --- a/java/src/main/java/org/msgpack/schema/ClassGenerator.java +++ /dev/null @@ -1,244 +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.schema; - -import java.util.ArrayList; -import java.util.List; -import java.io.IOException; -import java.io.File; -import java.io.Writer; -import org.msgpack.*; - -public class ClassGenerator { - private ClassSchema schema; - private Writer writer; - private int indent; - - private ClassGenerator(Writer writer) { - this.writer = writer; - this.indent = 0; - } - - public static void write(Schema schema, Writer dest) throws IOException { - if(!(schema instanceof ClassSchema)) { - throw new RuntimeException("schema is not class schema"); - } - ClassSchema cs = (ClassSchema)schema; - new ClassGenerator(dest).run(cs); - } - - private void run(ClassSchema cs) throws IOException { - List subclasses = new ArrayList(); - for(FieldSchema f : cs.getFields()) { - findSubclassSchema(subclasses, f.getSchema()); - } - - for(ClassSchema sub : subclasses) { - sub.setNamespace(cs.getNamespace()); - sub.setImports(cs.getImports()); - } - - this.schema = cs; - - writeHeader(); - - writeClass(); - - for(ClassSchema sub : subclasses) { - this.schema = sub; - writeSubclass(); - } - - writeFooter(); - - this.schema = null; - writer.flush(); - } - - private void findSubclassSchema(List dst, Schema s) { - if(s instanceof ClassSchema) { - ClassSchema cs = (ClassSchema)s; - if(!dst.contains(cs)) { dst.add(cs); } - for(FieldSchema f : cs.getFields()) { - findSubclassSchema(dst, f.getSchema()); - } - } else if(s instanceof ListSchema) { - ListSchema as = (ListSchema)s; - findSubclassSchema(dst, as.getElementSchema(0)); - } else if(s instanceof SetSchema) { - SetSchema as = (SetSchema)s; - findSubclassSchema(dst, as.getElementSchema(0)); - } else if(s instanceof MapSchema) { - MapSchema as = (MapSchema)s; - findSubclassSchema(dst, as.getKeySchema()); - findSubclassSchema(dst, as.getValueSchema()); - } - } - - private void writeHeader() throws IOException { - if(schema.getNamespace() != null) { - line("package "+schema.getNamespace()+";"); - line(); - } - line("import java.util.*;"); - line("import java.io.*;"); - line("import org.msgpack.*;"); - line("import org.msgpack.schema.ClassSchema;"); - line("import org.msgpack.schema.FieldSchema;"); - } - - private void writeFooter() throws IOException { - line(); - } - - private void writeClass() throws IOException { - line(); - line("public final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable"); - line("{"); - pushIndent(); - writeSchema(); - writeMemberVariables(); - writeMemberFunctions(); - popIndent(); - line("}"); - } - - private void writeSubclass() throws IOException { - line(); - line("final class "+schema.getClassName()+" implements MessagePackable, MessageConvertable"); - line("{"); - pushIndent(); - writeSchema(); - writeMemberVariables(); - writeMemberFunctions(); - popIndent(); - line("}"); - } - - private void writeSchema() throws IOException { - line("private static final ClassSchema _SCHEMA = (ClassSchema)Schema.load(\""+schema.getExpression()+"\");"); - line("public static ClassSchema getSchema() { return _SCHEMA; }"); - } - - private void writeMemberVariables() throws IOException { - line(); - for(FieldSchema f : schema.getFields()) { - line("public "+f.getSchema().getClassName()+" "+f.getName()+";"); - } - } - - private void writeMemberFunctions() throws IOException { - // void messagePack(Packer pk) - // boolean equals(Object obj) - // int hashCode() - // void set(int _index, Object _value) - // Object get(int _index); - // getXxx() - // setXxx(Xxx xxx) - writeConstructors(); - writeAccessors(); - writePackFunction(); - writeConvertFunction(); - writeFactoryFunction(); - } - - private void writeConstructors() throws IOException { - line(); - line("public "+schema.getClassName()+"() { }"); - } - - private void writeAccessors() throws IOException { - // FIXME - //line(); - //for(FieldSchema f : schema.getFields()) { - // line(""); - //} - } - - private void writePackFunction() throws IOException { - line(); - line("@Override"); - line("public void messagePack(Packer _pk) throws IOException"); - line("{"); - pushIndent(); - line("_pk.packArray("+schema.getFields().length+");"); - line("FieldSchema[] _fields = _SCHEMA.getFields();"); - int i = 0; - for(FieldSchema f : schema.getFields()) { - line("_fields["+i+"].getSchema().pack(_pk, "+f.getName()+");"); - ++i; - } - popIndent(); - line("}"); - } - - private void writeConvertFunction() throws IOException { - line(); - line("@Override"); - line("@SuppressWarnings(\"unchecked\")"); - line("public void messageConvert(Object obj) throws MessageTypeException"); - line("{"); - pushIndent(); - line("Object[] _source = ((List)obj).toArray();"); - line("FieldSchema[] _fields = _SCHEMA.getFields();"); - int i = 0; - for(FieldSchema f : schema.getFields()) { - line("if(_source.length <= "+i+") { return; } this."+f.getName()+" = ("+f.getSchema().getClassName()+")_fields["+i+"].getSchema().convert(_source["+i+"]);"); - ++i; - } - popIndent(); - line("}"); - } - - private void writeFactoryFunction() throws IOException { - line(); - line("@SuppressWarnings(\"unchecked\")"); - line("public static "+schema.getClassName()+" createFromMessage(Object[] _message)"); - line("{"); - pushIndent(); - line(schema.getClassName()+" _self = new "+schema.getClassName()+"();"); - int i = 0; - for(FieldSchema f : schema.getFields()) { - line("if(_message.length <= "+i+") { return _self; } _self."+f.getName()+" = ("+f.getSchema().getClassName()+")_message["+i+"];"); - ++i; - } - line("return _self;"); - popIndent(); - line("}"); - } - - private void line(String str) throws IOException { - for(int i=0; i < indent; ++i) { - writer.write("\t"); - } - writer.write(str+"\n"); - } - - private void line() throws IOException { - writer.write("\n"); - } - - private void pushIndent() { - indent += 1; - } - - private void popIndent() { - indent -= 1; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ClassSchema.java b/java/src/main/java/org/msgpack/schema/ClassSchema.java deleted file mode 100644 index cd59755..0000000 --- a/java/src/main/java/org/msgpack/schema/ClassSchema.java +++ /dev/null @@ -1,91 +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.schema; - -import java.util.Arrays; -import java.util.List; -import org.msgpack.*; - -public abstract class ClassSchema extends Schema implements IArraySchema { - protected String name; - protected FieldSchema[] fields; - protected List imports; - protected String namespace; - protected String fqdn; - - public ClassSchema( - String name, String namespace, - List imports, List fields) { - this.name = name; - this.namespace = namespace; - this.imports = imports; // FIXME clone? - this.fields = new FieldSchema[fields.size()]; - System.arraycopy(fields.toArray(), 0, this.fields, 0, fields.size()); - if(namespace == null) { - this.fqdn = name; - } else { - this.fqdn = namespace+"."+name; - } - } - - @Override - public String getClassName() { - return name; - } - - @Override - public String getExpression() { - StringBuffer b = new StringBuffer(); - b.append("(class "); - b.append(name); - if(namespace != null) { - b.append(" (package "+namespace+")"); - } - for(FieldSchema f : fields) { - b.append(" "+f.getExpression()); - } - b.append(")"); - return b.toString(); - } - - public boolean equals(ClassSchema o) { - return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) && - name.equals(o.name); - } - - public final FieldSchema[] getFields() { - return fields; - } - - String getNamespace() { - return namespace; - } - - List getImports() { - return imports; - } - - void setNamespace(String namespace) { - this.namespace = namespace; - } - - void setImports(List imports) { - this.imports = imports; // FIXME clone? - } -} - diff --git a/java/src/main/java/org/msgpack/schema/DoubleSchema.java b/java/src/main/java/org/msgpack/schema/DoubleSchema.java deleted file mode 100644 index cb857c3..0000000 --- a/java/src/main/java/org/msgpack/schema/DoubleSchema.java +++ /dev/null @@ -1,74 +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.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class DoubleSchema extends Schema { - public DoubleSchema() { } - - @Override - public String getClassName() { - return "Double"; - } - - @Override - public String getExpression() { - return "double"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Double) { - pk.packDouble((Double)obj); - } else if(obj instanceof Float) { - pk.packFloat((Float)obj); - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final double convertDouble(Object obj) throws MessageTypeException { - if(obj instanceof Double) { - return (Double)obj; - } else if(obj instanceof Float) { - return ((Float)obj).doubleValue(); - } else { - throw new MessageTypeException(); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertDouble(obj); - } - - @Override - public Object createFromFloat(float v) { - return (double)v; - } - - @Override - public Object createFromDouble(double v) { - return (double)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/FieldSchema.java b/java/src/main/java/org/msgpack/schema/FieldSchema.java deleted file mode 100644 index 66c2ff2..0000000 --- a/java/src/main/java/org/msgpack/schema/FieldSchema.java +++ /dev/null @@ -1,43 +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.schema; - -import org.msgpack.Schema; - -public class FieldSchema { - private String name; - private Schema schema; - - public FieldSchema(String name, Schema schema) { - this.name = name; - this.schema = schema; - } - - public final String getName() { - return name; - } - - public final Schema getSchema() { - return schema; - } - - public String getExpression() { - return "(field "+name+" "+schema.getExpression()+")"; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/FloatSchema.java b/java/src/main/java/org/msgpack/schema/FloatSchema.java deleted file mode 100644 index cd73201..0000000 --- a/java/src/main/java/org/msgpack/schema/FloatSchema.java +++ /dev/null @@ -1,74 +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.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class FloatSchema extends Schema { - public FloatSchema() { } - - @Override - public String getClassName() { - return "Float"; - } - - @Override - public String getExpression() { - return "float"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Double) { - pk.packDouble((Double)obj); - } else if(obj instanceof Float) { - pk.packFloat((Float)obj); - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final float convertFloat(Object obj) throws MessageTypeException { - if(obj instanceof Double) { - return ((Double)obj).floatValue(); - } else if(obj instanceof Float) { - return (Float)obj; - } else { - throw new MessageTypeException(); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertFloat(obj); - } - - @Override - public Object createFromFloat(float v) { - return (float)v; - } - - @Override - public Object createFromDouble(double v) { - return (float)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/GenericClassSchema.java b/java/src/main/java/org/msgpack/schema/GenericClassSchema.java deleted file mode 100644 index 1ab4c33..0000000 --- a/java/src/main/java/org/msgpack/schema/GenericClassSchema.java +++ /dev/null @@ -1,87 +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.schema; - -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.HashMap; -import java.io.IOException; -import org.msgpack.*; - -public class GenericClassSchema extends ClassSchema { - public GenericClassSchema( - String name, String namespace, - List imports, List fields) { - super(name, namespace, imports, fields); - } - - @Override - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Map) { - Map d = (Map)obj; - pk.packArray(fields.length); - for(int i=0; i < fields.length; ++i) { - FieldSchema f = fields[i]; - f.getSchema().pack(pk, d.get(f.getName())); - } - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Collection) { - // FIXME optimize - return createFromArray( ((Collection)obj).toArray() ); - } else if(obj instanceof Map) { - HashMap m = new HashMap(fields.length); - Map d = (Map)obj; - for(int i=0; i < fields.length; ++i) { - FieldSchema f = fields[i]; - String fieldName = f.getName(); - m.put(fieldName, f.getSchema().convert(d.get(fieldName))); - } - return m; - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public Schema getElementSchema(int index) { - // FIXME check index < fields.length - return fields[index].getSchema(); - } - - public Object createFromArray(Object[] obj) { - HashMap m = new HashMap(fields.length); - int i=0; - for(; i < obj.length; ++i) { - m.put(fields[i].getName(), obj[i]); - } - for(; i < fields.length; ++i) { - m.put(fields[i].getName(), null); - } - return m; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/GenericSchema.java b/java/src/main/java/org/msgpack/schema/GenericSchema.java deleted file mode 100644 index f9098ed..0000000 --- a/java/src/main/java/org/msgpack/schema/GenericSchema.java +++ /dev/null @@ -1,129 +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.schema; - -import java.util.Arrays; -import java.util.List; -import java.util.HashMap; -import java.io.IOException; -import org.msgpack.*; - -public class GenericSchema extends Schema implements IArraySchema, IMapSchema { - public GenericSchema() { } - - @Override - public String getClassName() { - return "Object"; - } - - @Override - public String getExpression() { - return "object"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - pk.pack(obj); - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return obj; - } - - @Override - public Schema getElementSchema(int index) { - return this; - } - - @Override - public Schema getKeySchema() { - return this; - } - - @Override - public Schema getValueSchema() { - return this; - } - - @Override - public Object createFromNil() { - return null; - } - - @Override - public Object createFromBoolean(boolean v) { - return v; - } - - @Override - public Object createFromByte(byte v) { - return v; - } - - @Override - public Object createFromShort(short v) { - return v; - } - - @Override - public Object createFromInt(int v) { - return v; - } - - @Override - public Object createFromLong(long v) { - return v; - } - - @Override - public Object createFromFloat(float v) { - return v; - } - - @Override - public Object createFromDouble(double v) { - return v; - } - - @Override - public Object createFromRaw(byte[] b, int offset, int length) { - byte[] bytes = new byte[length]; - System.arraycopy(b, offset, bytes, 0, length); - return bytes; - } - - @Override - public Object createFromArray(Object[] obj) { - return Arrays.asList(obj); - } - - @Override - @SuppressWarnings("unchecked") - public Object createFromMap(Object[] obj) { - HashMap m = new HashMap(obj.length / 2); - int i = 0; - while(i < obj.length) { - Object k = obj[i++]; - Object v = obj[i++]; - m.put(k, v); - } - return m; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/IArraySchema.java b/java/src/main/java/org/msgpack/schema/IArraySchema.java deleted file mode 100644 index 67e9f55..0000000 --- a/java/src/main/java/org/msgpack/schema/IArraySchema.java +++ /dev/null @@ -1,26 +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.schema; - -import org.msgpack.Schema; - -public interface IArraySchema { - public Schema getElementSchema(int index); - public Object createFromArray(Object[] obj); -} - diff --git a/java/src/main/java/org/msgpack/schema/IMapSchema.java b/java/src/main/java/org/msgpack/schema/IMapSchema.java deleted file mode 100644 index 3a2f556..0000000 --- a/java/src/main/java/org/msgpack/schema/IMapSchema.java +++ /dev/null @@ -1,27 +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.schema; - -import org.msgpack.Schema; - -public interface IMapSchema { - public Schema getKeySchema(); - public Schema getValueSchema(); - public Object createFromMap(Object[] obj); -} - diff --git a/java/src/main/java/org/msgpack/schema/IntSchema.java b/java/src/main/java/org/msgpack/schema/IntSchema.java deleted file mode 100644 index 269f4fb..0000000 --- a/java/src/main/java/org/msgpack/schema/IntSchema.java +++ /dev/null @@ -1,96 +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.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class IntSchema extends Schema { - public IntSchema() { } - - @Override - public String getClassName() { - return "Integer"; - } - - @Override - public String getExpression() { - return "int"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - int value = ((Number)obj).intValue(); - if(value >= Short.MAX_VALUE) { - long lvalue = ((Number)obj).longValue(); - if(lvalue > Integer.MAX_VALUE) { - throw new MessageTypeException(); - } - } - pk.packInt(value); - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final int convertInt(Object obj) throws MessageTypeException { - if(obj instanceof Number) { - int value = ((Number)obj).intValue(); - if(value >= Integer.MAX_VALUE) { - long lvalue = ((Number)obj).longValue(); - if(lvalue > Integer.MAX_VALUE) { - throw new MessageTypeException(); - } - } - return value; - } - throw new MessageTypeException(); - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertInt(obj); - } - - @Override - public Object createFromByte(byte v) { - return (int)v; - } - - @Override - public Object createFromShort(short v) { - return (int)v; - } - - @Override - public Object createFromInt(int v) { - return (int)v; - } - - @Override - public Object createFromLong(long v) { - if(v > Integer.MAX_VALUE) { - throw new MessageTypeException(); - } - return (int)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ListSchema.java b/java/src/main/java/org/msgpack/schema/ListSchema.java deleted file mode 100644 index bef8cc4..0000000 --- a/java/src/main/java/org/msgpack/schema/ListSchema.java +++ /dev/null @@ -1,111 +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.schema; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Set; -import java.util.List; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.RandomAccess; -import java.io.IOException; -import org.msgpack.*; - -public class ListSchema extends Schema implements IArraySchema { - private Schema elementSchema; - - public ListSchema(Schema elementSchema) { - this.elementSchema = elementSchema; - } - - @Override - public String getClassName() { - return "List<"+elementSchema.getClassName()+">"; - } - - @Override - public String getExpression() { - return "(array "+elementSchema.getExpression()+")"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof List) { - List d = (List)obj; - pk.packArray(d.size()); - if(obj instanceof RandomAccess) { - for(int i=0; i < d.size(); ++i) { - elementSchema.pack(pk, d.get(i)); - } - } else { - for(Object e : d) { - elementSchema.pack(pk, e); - } - } - } else if(obj instanceof Set) { - Set d = (Set)obj; - pk.packArray(d.size()); - for(Object e : d) { - elementSchema.pack(pk, e); - } - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @SuppressWarnings("unchecked") - public static final List convertList(Object obj, - Schema elementSchema, List dest) throws MessageTypeException { - if(!(obj instanceof List)) { - throw new MessageTypeException(); - } - List d = (List)obj; - if(dest == null) { - dest = new ArrayList(d.size()); - } - if(obj instanceof RandomAccess) { - for(int i=0; i < d.size(); ++i) { - dest.add( (T)elementSchema.convert(d.get(i)) ); - } - } else { - for(Object e : d) { - dest.add( (T)elementSchema.convert(e) ); - } - } - return dest; - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertList(obj, elementSchema, null); - } - - @Override - public Schema getElementSchema(int index) { - return elementSchema; - } - - @Override - public Object createFromArray(Object[] obj) { - return Arrays.asList(obj); - } -} - diff --git a/java/src/main/java/org/msgpack/schema/LongSchema.java b/java/src/main/java/org/msgpack/schema/LongSchema.java deleted file mode 100644 index 728fa21..0000000 --- a/java/src/main/java/org/msgpack/schema/LongSchema.java +++ /dev/null @@ -1,80 +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.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class LongSchema extends Schema { - public LongSchema() { } - - @Override - public String getClassName() { - return "Long"; - } - - @Override - public String getExpression() { - return "long"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - long value = ((Number)obj).longValue(); - pk.packLong(value); - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final long convertLong(Object obj) throws MessageTypeException { - if(obj instanceof Number) { - return ((Number)obj).longValue(); - } - throw new MessageTypeException(); - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertLong(obj); - } - - @Override - public Object createFromByte(byte v) { - return (long)v; - } - - @Override - public Object createFromShort(short v) { - return (long)v; - } - - @Override - public Object createFromInt(int v) { - return (long)v; - } - - @Override - public Object createFromLong(long v) { - return (long)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/MapSchema.java b/java/src/main/java/org/msgpack/schema/MapSchema.java deleted file mode 100644 index 2e09af3..0000000 --- a/java/src/main/java/org/msgpack/schema/MapSchema.java +++ /dev/null @@ -1,106 +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.schema; - -import java.util.Map; -import java.util.HashMap; -import java.io.IOException; -import org.msgpack.*; - -public class MapSchema extends Schema implements IMapSchema { - private Schema keySchema; - private Schema valueSchema; - - public MapSchema(Schema keySchema, Schema valueSchema) { - this.keySchema = keySchema; - this.valueSchema = valueSchema; - } - - @Override - public String getClassName() { - return "Map<"+keySchema.getClassName()+", "+valueSchema.getClassName()+">"; - } - - @Override - public String getExpression() { - return "(map "+keySchema.getExpression()+" "+valueSchema.getExpression()+")"; - } - - @Override - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Map) { - Map d = (Map)obj; - pk.packMap(d.size()); - for(Map.Entry e : d.entrySet()) { - keySchema.pack(pk, e.getKey()); - valueSchema.pack(pk, e.getValue()); - } - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @SuppressWarnings("unchecked") - public static final Map convertMap(Object obj, - Schema keySchema, Schema valueSchema, Map dest) throws MessageTypeException { - if(!(obj instanceof Map)) { - throw new MessageTypeException(); - } - Map d = (Map)obj; - if(dest == null) { - dest = new HashMap(d.size()); - } - for(Map.Entry e : d.entrySet()) { - dest.put((K)keySchema.convert(e.getKey()), - (V)valueSchema.convert(e.getValue())); - } - return dest; - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertMap(obj, keySchema, valueSchema, null); - } - - @Override - public Schema getKeySchema() { - return keySchema; - } - - @Override - public Schema getValueSchema() { - return valueSchema; - } - - @Override - @SuppressWarnings("unchecked") - public Object createFromMap(Object[] obj) { - HashMap dest = new HashMap(obj.length / 2); - int i = 0; - while(i < obj.length) { - Object k = obj[i++]; - Object v = obj[i++]; - dest.put(k, v); - } - return dest; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ReflectionClassSchema.java b/java/src/main/java/org/msgpack/schema/ReflectionClassSchema.java deleted file mode 100644 index fb94adf..0000000 --- a/java/src/main/java/org/msgpack/schema/ReflectionClassSchema.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.msgpack.schema; - -import java.util.Arrays; -import java.util.List; -import java.lang.reflect.*; -import org.msgpack.*; - -// FIXME -public abstract class ReflectionClassSchema extends ClassSchema { - private Constructor constructorCache; - - public ReflectionClassSchema(String name, List fields, String namespace, List imports) { - super(name, namespace, imports, fields); - } - - /* - Schema getElementSchema(int index) - { - // FIXME check index < fields.length - fields[index].getSchema(); - } - - Object createFromArray(Object[] obj) - { - Object o = newInstance(); - ((MessageConvertable)o).messageConvert(obj); - return o; - } - - Object newInstance() - { - if(constructorCache == null) { - cacheConstructor(); - } - try { - return constructorCache.newInstance((Object[])null); - } catch (InvocationTargetException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (InstantiationException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (IllegalAccessException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } - } - - private void cacheConstructor() - { - try { - Class c = Class.forName(fqdn); - int index = 0; - for(SpecificFieldSchema f : fields) { - f.cacheField(c, index++); - } - constructorCache = c.getDeclaredConstructor((Class[])null); - constructorCache.setAccessible(true); - } catch(ClassNotFoundException e) { - throw new RuntimeException("class not found: "+fqdn); - } catch (NoSuchMethodException e) { - throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage()); - } - } - */ -} - diff --git a/java/src/main/java/org/msgpack/schema/SSchemaParser.java b/java/src/main/java/org/msgpack/schema/SSchemaParser.java deleted file mode 100644 index 4345e92..0000000 --- a/java/src/main/java/org/msgpack/schema/SSchemaParser.java +++ /dev/null @@ -1,264 +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.schema; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Stack; -import java.util.regex.Pattern; -import java.util.regex.Matcher; -import org.msgpack.*; - -// FIXME exception class - -public class SSchemaParser { - public static Schema parse(String source) { - return new SSchemaParser(false).run(source); - } - - public static Schema load(String source) { - return new SSchemaParser(true).run(source); - } - - private static abstract class SExp { - boolean isAtom() { return false; } - public String getAtom() { return null; } - - boolean isTuple() { return false; } - public SExp getTuple(int i) { return null; } - public int size() { return 0; } - public boolean empty() { return size() == 0; } - Iterator iterator(int offset) { return null; } - } - - private static class SAtom extends SExp { - private String atom; - - SAtom(String atom) { this.atom = atom; } - - boolean isAtom() { return true; } - public String getAtom() { return atom; } - - public String toString() { return atom; } - } - - private static class STuple extends SExp { - private List tuple; - - STuple() { this.tuple = new ArrayList(); } - - public void add(SExp e) { tuple.add(e); } - - boolean isTuple() { return true; } - public SExp getTuple(int i) { return tuple.get(i); } - public int size() { return tuple.size(); } - - Iterator iterator(int skip) { - Iterator i = tuple.iterator(); - for(int s=0; s < skip; ++s) { i.next(); } - return i; - } - - public String toString() { - if(tuple.isEmpty()) { return "()"; } - Iterator i = tuple.iterator(); - StringBuffer o = new StringBuffer(); - o.append("(").append(i.next()); - while(i.hasNext()) { o.append(" ").append(i.next()); } - o.append(")"); - return o.toString(); - } - } - - boolean specificClass; - - private SSchemaParser(boolean specificClass) { - this.specificClass = specificClass; - } - - private static Pattern pattern = Pattern.compile( - "(?:\\s+)|([\\(\\)]|[\\d\\w\\.]+)"); - - private Schema run(String source) { - Matcher m = pattern.matcher(source); - - Stack stack = new Stack(); - String token; - - while(true) { - while(true) { - if(!m.find()) { throw new RuntimeException("unexpected end of file"); } - token = m.group(1); - if(token != null) { break; } - } - - if(token.equals("(")) { - stack.push(new STuple()); - } else if(token.equals(")")) { - STuple top = stack.pop(); - if(stack.empty()) { - stack.push(top); - break; - } - stack.peek().add(top); - } else { - if(stack.empty()) { - throw new RuntimeException("unexpected token '"+token+"'"); - } - stack.peek().add(new SAtom(token)); - } - } - - while(true) { - if(!m.find()) { break; } - token = m.group(1); - if(token != null) { throw new RuntimeException("unexpected token '"+token+"'"); } - } - - return readType( stack.pop() ); - } - - private Schema readType(SExp exp) { - if(exp.isAtom()) { - String type = exp.getAtom(); - if(type.equals("string")) { - return new StringSchema(); - } else if(type.equals("raw")) { - return new ByteArraySchema(); - } else if(type.equals("byte")) { - return new ByteSchema(); - } else if(type.equals("short")) { - return new ShortSchema(); - } else if(type.equals("int")) { - return new IntSchema(); - } else if(type.equals("long")) { - return new LongSchema(); - } else if(type.equals("float")) { - return new FloatSchema(); - } else if(type.equals("double")) { - return new DoubleSchema(); - } else if(type.equals("object")) { - return new GenericSchema(); - } else { - throw new RuntimeException("byte, short, int, long, float, double, raw, string or object is expected but got '"+type+"': "+exp); - } - } else { - String type = exp.getTuple(0).getAtom(); - if(type.equals("class")) { - return parseClass(exp); - } else if(type.equals("array")) { - return parseList(exp); - } else if(type.equals("set")) { - return parseSet(exp); - } else if(type.equals("map")) { - return parseMap(exp); - } else { - throw new RuntimeException("class, list, set or map is expected but got '"+type+"': "+exp); - } - } - } - - private ClassSchema parseClass(SExp exp) { - if(exp.size() < 3 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("class is (class NAME CLASS_BODY): "+exp); - } - - String namespace = null; - List imports = new ArrayList(); - String name = exp.getTuple(1).getAtom(); - List fields = new ArrayList(); - - for(Iterator i=exp.iterator(2); i.hasNext();) { - SExp subexp = i.next(); - if(!subexp.isTuple() || subexp.empty() || !subexp.getTuple(0).isAtom()) { - throw new RuntimeException("field, package or import is expected: "+subexp); - } - String type = subexp.getTuple(0).getAtom(); - if(type.equals("field")) { - fields.add( parseField(subexp) ); - } else if(type.equals("package")) { - if(namespace != null) { - throw new RuntimeException("duplicated package definition: "+subexp); - } - namespace = parseNamespace(subexp); - } else if(type.equals("import")) { - imports.add( parseImport(subexp) ); - } else { - throw new RuntimeException("field, package or import is expected but got '"+type+"': "+subexp); - } - } - - if(specificClass) { - return new SpecificClassSchema(name, namespace, imports, fields); - } else { - return new GenericClassSchema(name, namespace, imports, fields); - } - } - - private ListSchema parseList(SExp exp) { - if(exp.size() != 2) { - throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp); - } - Schema elementType = readType(exp.getTuple(1)); - return new ListSchema(elementType); - } - - private SetSchema parseSet(SExp exp) { - if(exp.size() != 2) { - throw new RuntimeException("list is (list ELEMENT_TYPE): "+exp); - } - Schema elementType = readType(exp.getTuple(1)); - return new SetSchema(elementType); - } - - private MapSchema parseMap(SExp exp) { - if(exp.size() != 3 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("map is (map KEY_TYPE VALUE_TYPE): "+exp); - } - Schema keyType = readType(exp.getTuple(1)); - Schema valueType = readType(exp.getTuple(2)); - return new MapSchema(keyType, valueType); - } - - private String parseNamespace(SExp exp) { - if(exp.size() != 2 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("package is (package NAME): "+exp); - } - String name = exp.getTuple(1).getAtom(); - return name; - } - - private String parseImport(SExp exp) { - if(exp.size() != 2 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("import is (import NAME): "+exp); - } - String name = exp.getTuple(1).getAtom(); - return name; - } - - private FieldSchema parseField(SExp exp) { - if(exp.size() != 3 || !exp.getTuple(1).isAtom()) { - throw new RuntimeException("field is (field NAME TYPE): "+exp); - } - String name = exp.getTuple(1).getAtom(); - Schema type = readType(exp.getTuple(2)); - return new FieldSchema(name, type); - } -} - diff --git a/java/src/main/java/org/msgpack/schema/SetSchema.java b/java/src/main/java/org/msgpack/schema/SetSchema.java deleted file mode 100644 index a3e1974..0000000 --- a/java/src/main/java/org/msgpack/schema/SetSchema.java +++ /dev/null @@ -1,115 +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.schema; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Set; -import java.util.List; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.RandomAccess; -import java.io.IOException; -import org.msgpack.*; - -public class SetSchema extends Schema implements IArraySchema { - private Schema elementSchema; - - public SetSchema(Schema elementSchema) { - this.elementSchema = elementSchema; - } - - @Override - public String getClassName() { - return "Set<"+elementSchema.getClassName()+">"; - } - - @Override - public String getExpression() { - return "(set "+elementSchema.getExpression()+")"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof List) { - List d = (List)obj; - pk.packArray(d.size()); - if(obj instanceof RandomAccess) { - for(int i=0; i < d.size(); ++i) { - elementSchema.pack(pk, d.get(i)); - } - } else { - for(Object e : d) { - elementSchema.pack(pk, e); - } - } - } else if(obj instanceof Set) { - Set d = (Set)obj; - pk.packArray(d.size()); - for(Object e : d) { - elementSchema.pack(pk, e); - } - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @SuppressWarnings("unchecked") - public static final Set convertSet(Object obj, - Schema elementSchema, Set dest) throws MessageTypeException { - if(!(obj instanceof List)) { - throw new MessageTypeException(); - } - List d = (List)obj; - if(dest == null) { - dest = new HashSet(d.size()); - } - if(obj instanceof RandomAccess) { - for(int i=0; i < d.size(); ++i) { - dest.add( (T)elementSchema.convert(d.get(i)) ); - } - } else { - for(Object e : d) { - dest.add( (T)elementSchema.convert(e) ); - } - } - return dest; - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertSet(obj, elementSchema, null); - } - - @Override - public Schema getElementSchema(int index) { - return elementSchema; - } - - @Override - public Object createFromArray(Object[] obj) { - Set m = new HashSet(obj.length); - for(int i=0; i < obj.length; i++) { - m.add(obj[i]); - } - return m; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/ShortSchema.java b/java/src/main/java/org/msgpack/schema/ShortSchema.java deleted file mode 100644 index 21b9327..0000000 --- a/java/src/main/java/org/msgpack/schema/ShortSchema.java +++ /dev/null @@ -1,93 +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.schema; - -import java.io.IOException; -import org.msgpack.*; - -public class ShortSchema extends Schema { - public ShortSchema() { } - - @Override - public String getClassName() { - return "Short"; - } - - @Override - public String getExpression() { - return "short"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof Number) { - int value = ((Number)obj).intValue(); - if(value > Short.MAX_VALUE) { - throw new MessageTypeException(); - } - pk.packShort((short)value); - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final short convertShort(Object obj) throws MessageTypeException { - if(obj instanceof Number) { - int value = ((Number)obj).intValue(); - if(value > Short.MAX_VALUE) { - throw new MessageTypeException(); - } - return (short)value; - } - throw new MessageTypeException(); - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertShort(obj); - } - - @Override - public Object createFromByte(byte v) { - return (short)v; - } - - @Override - public Object createFromShort(short v) { - return (short)v; - } - - @Override - public Object createFromInt(int v) { - if(v > Short.MAX_VALUE) { - throw new MessageTypeException(); - } - return (short)v; - } - - @Override - public Object createFromLong(long v) { - if(v > Short.MAX_VALUE) { - throw new MessageTypeException(); - } - return (short)v; - } -} - diff --git a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java b/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java deleted file mode 100644 index 850f621..0000000 --- a/java/src/main/java/org/msgpack/schema/SpecificClassSchema.java +++ /dev/null @@ -1,122 +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.schema; - -import java.util.Collection; -import java.util.List; -import java.lang.reflect.*; -import java.io.IOException; -import org.msgpack.*; - -public class SpecificClassSchema extends ClassSchema { - private Class classCache; - private Method factoryCache; - private Constructor constructorCache; - - public SpecificClassSchema( - String name, String namespace, - List imports, List fields) { - super(name, namespace, imports, fields); - } - - @Override - @SuppressWarnings("unchecked") - public void pack(Packer pk, Object obj) throws IOException { - if(obj == null) { - pk.packNil(); - return; - } - if(classCache == null) { - cacheFactory(); - } - if(classCache.isInstance(obj)) { - ((MessagePackable)obj).messagePack(pk); - } else { - // FIXME Map - throw MessageTypeException.invalidConvert(obj, this); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - if(obj instanceof Collection) { - if(constructorCache == null) { - cacheConstructor(); - } - try { - MessageConvertable o = (MessageConvertable)constructorCache.newInstance((Object[])null); - o.messageConvert(obj); - return o; - } catch (InvocationTargetException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (InstantiationException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } catch (IllegalAccessException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } - - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public Schema getElementSchema(int index) { - // FIXME check index < fields.length - return fields[index].getSchema(); - } - - public Object createFromArray(Object[] obj) { - if(factoryCache == null) { - cacheFactory(); - } - try { - return factoryCache.invoke(null, new Object[]{obj}); - } catch (InvocationTargetException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getCause()); - } catch (IllegalAccessException e) { - throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - private void cacheFactory() { - try { - classCache = Class.forName(fqdn); - factoryCache = classCache.getDeclaredMethod("createFromMessage", new Class[]{Object[].class}); - factoryCache.setAccessible(true); - } catch(ClassNotFoundException e) { - throw new RuntimeException("class not found: "+fqdn); - } catch (NoSuchMethodException e) { - throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage()); - } - } - - @SuppressWarnings("unchecked") - private void cacheConstructor() { - try { - classCache = Class.forName(fqdn); - constructorCache = classCache.getDeclaredConstructor((Class[])null); - constructorCache.setAccessible(true); - } catch(ClassNotFoundException e) { - throw new RuntimeException("class not found: "+fqdn); - } catch (NoSuchMethodException e) { - throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage()); - } - } -} - diff --git a/java/src/main/java/org/msgpack/schema/StringSchema.java b/java/src/main/java/org/msgpack/schema/StringSchema.java deleted file mode 100644 index 23e4e64..0000000 --- a/java/src/main/java/org/msgpack/schema/StringSchema.java +++ /dev/null @@ -1,102 +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.schema; - -import java.nio.ByteBuffer; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import org.msgpack.*; - -public class StringSchema extends Schema { - public StringSchema() { } - - @Override - public String getClassName() { - return "String"; - } - - @Override - public String getExpression() { - return "string"; - } - - @Override - public void pack(Packer pk, Object obj) throws IOException { - if(obj instanceof byte[]) { - byte[] b = (byte[])obj; - pk.packRaw(b.length); - pk.packRawBody(b); - } else if(obj instanceof String) { - try { - byte[] b = ((String)obj).getBytes("UTF-8"); - pk.packRaw(b.length); - pk.packRawBody(b); - } catch (UnsupportedEncodingException e) { - throw new MessageTypeException(); - } - } else if(obj == null) { - pk.packNil(); - } else { - throw MessageTypeException.invalidConvert(obj, this); - } - } - - public static final String convertString(Object obj) throws MessageTypeException { - if(obj instanceof byte[]) { - try { - return new String((byte[])obj, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new MessageTypeException(); - } - } else if(obj instanceof String) { - return (String)obj; - } else if(obj instanceof ByteBuffer) { - ByteBuffer d = (ByteBuffer)obj; - try { - if(d.hasArray()) { - return new String(d.array(), d.position(), d.capacity(), "UTF-8"); - } else { - byte[] v = new byte[d.capacity()]; - int pos = d.position(); - d.get(v); - d.position(pos); - return new String(v, "UTF-8"); - } - } catch (UnsupportedEncodingException e) { - throw new MessageTypeException(); - } - } else { - throw new MessageTypeException(); - } - } - - @Override - public Object convert(Object obj) throws MessageTypeException { - return convertString(obj); - } - - @Override - public Object createFromRaw(byte[] b, int offset, int length) { - try { - return new String(b, offset, length, "UTF-8"); - } catch (Exception e) { - throw new RuntimeException(e.getMessage()); - } - } -} - diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index b02bbb4..ca3d235 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -8,240 +8,223 @@ import org.junit.Test; import static org.junit.Assert.*; public class TestPackUnpack { - protected Object unpackOne(ByteArrayOutputStream out) { - return unpackOne(out, null); - } - protected Object unpackOne(ByteArrayOutputStream out, Schema schema) { - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - if (schema != null) - upk = upk.useSchema(schema); - Iterator it = upk.iterator(); - assertEquals(true, it.hasNext()); - Object obj = it.next(); - assertEquals(false, it.hasNext()); - return obj; - } + public MessagePackObject unpackOne(ByteArrayOutputStream out) { + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + Iterator it = upk.iterator(); + assertEquals(true, it.hasNext()); + MessagePackObject obj = it.next(); + assertEquals(false, it.hasNext()); + return obj; + } - @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testInt(rand.nextInt()); - } - public void testInt(int val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Byte) - assertEquals(val, ((Byte)obj).intValue()); - else if (obj instanceof Integer) - assertEquals(val, ((Integer)obj).intValue()); - else if (obj instanceof Short) - assertEquals(val, ((Short)obj).intValue()); - else if (obj instanceof Long) - assertEquals(val, ((Long)obj).intValue()); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + public void testInt(int val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asInt()); + } + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } - @Test - public void testFloat() throws Exception { - testFloat((float)0.0); - testFloat((float)-0.0); - testFloat((float)1.0); - testFloat((float)-1.0); - testFloat((float)Float.MAX_VALUE); - testFloat((float)Float.MIN_VALUE); - testFloat((float)Float.NaN); - testFloat((float)Float.NEGATIVE_INFINITY); - testFloat((float)Float.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testFloat(rand.nextFloat()); - } - public void testFloat(float val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Float) - assertEquals(val, ((Float)obj).floatValue(), 10e-10); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + public void testFloat(float val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asFloat(), 10e-10); + } + @Test + public void testFloat() throws Exception { + testFloat((float)0.0); + testFloat((float)-0.0); + testFloat((float)1.0); + testFloat((float)-1.0); + testFloat((float)Float.MAX_VALUE); + testFloat((float)Float.MIN_VALUE); + testFloat((float)Float.NaN); + testFloat((float)Float.NEGATIVE_INFINITY); + testFloat((float)Float.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testFloat(rand.nextFloat()); + } - @Test - public void testDouble() throws Exception { - testDouble((double)0.0); - testDouble((double)-0.0); - testDouble((double)1.0); - testDouble((double)-1.0); - testDouble((double)Double.MAX_VALUE); - testDouble((double)Double.MIN_VALUE); - testDouble((double)Double.NaN); - testDouble((double)Double.NEGATIVE_INFINITY); - testDouble((double)Double.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testDouble(rand.nextDouble()); - } - public void testDouble(double val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Double) - assertEquals(val, ((Double)obj).doubleValue(), 10e-10); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + public void testDouble(double val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asDouble(), 10e-10); + } + @Test + public void testDouble() throws Exception { + testDouble((double)0.0); + testDouble((double)-0.0); + testDouble((double)1.0); + testDouble((double)-1.0); + testDouble((double)Double.MAX_VALUE); + testDouble((double)Double.MIN_VALUE); + testDouble((double)Double.NaN); + testDouble((double)Double.NEGATIVE_INFINITY); + testDouble((double)Double.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testDouble(rand.nextDouble()); + } - @Test - public void testNil() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).packNil(); - Object obj = unpackOne(out); - assertEquals(null, obj); - } + @Test + public void testNil() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).packNil(); + MessagePackObject obj = unpackOne(out); + assertTrue(obj.isNull()); + } - @Test - public void testBoolean() throws Exception { - testBoolean(false); - testBoolean(true); - } - public void testBoolean(boolean val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof Boolean) - assertEquals(val, ((Boolean)obj).booleanValue()); - else { - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testBoolean() throws Exception { + testBoolean(false); + testBoolean(true); + } + public void testBoolean(boolean val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asBoolean()); + } - @Test - public void testString() throws Exception { - testString(""); - testString("a"); - testString("ab"); - testString("abc"); - // small size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 31 + 1; - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - // medium size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 15); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - // large size string - for (int i = 0; i < 10; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 31); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - } - public void testString(String val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out); - if (obj instanceof byte[]) - assertEquals(val, new String((byte[])obj)); - else { - System.out.println("obj=" + obj); - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + public void testString(String val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asString()); + } + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); - @Test - public void testArray() throws Exception { - List emptyList = new ArrayList(); - testArray(emptyList, Schema.parse("(array int)")); + // small size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 31 + 1; + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(array int)"); - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(j); - testArray(l, schema); - } - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(array string)"); - List l = new ArrayList(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - l.add(Integer.toString(j)); - testArray(l, schema); - } - } - public void testArray(List val, Schema schema) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out, schema); - if (obj instanceof List) - assertTrue(val.equals(obj)); - else { - System.out.println("obj=" + obj); - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + // medium size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 15); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } - @Test - public void testMap() throws Exception { - Map emptyMap = new HashMap(); - testMap(emptyMap, Schema.parse("(map int int)")); + // large size string + for (int i = 0; i < 10; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 31); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + } - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(map int int)"); - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(j, j); - testMap(m, schema); - } - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(map string int)"); - Map m = new HashMap(); - int len = (int)Math.random() % 1000 + 1; - for (int j = 0; j < len; j++) - m.put(Integer.toString(j), j); - testMap(m, schema); - } - } - public void testMap(Map val, Schema schema) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - Object obj = unpackOne(out, schema); - if (obj instanceof Map) - assertTrue(val.equals(obj)); - else { - System.out.println("obj=" + obj); - System.out.println("Got unexpected class: " + obj.getClass()); - assertTrue(false); - } - } + @Test + public void testArray() throws Exception { + List emptyList = new ArrayList(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyList); + MessagePackObject obj = unpackOne(out); + assertEquals(emptyList, obj.asList()); + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + MessagePackObject obj = unpackOne(out); + List list = obj.asList(); + assertEquals(l.size(), list.size()); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j).intValue(), list.get(j).asInt()); + } + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(Integer.toString(j)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + MessagePackObject obj = unpackOne(out); + List list = obj.asList(); + assertEquals(l.size(), list.size()); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j), list.get(j).asString()); + } + } + } + + @Test + public void testMap() throws Exception { + Map emptyMap = new HashMap(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyMap); + MessagePackObject obj = unpackOne(out); + assertEquals(emptyMap, obj.asMap()); + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(j, j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + MessagePackObject obj = unpackOne(out); + Map map = obj.asMap(); + assertEquals(m.size(), map.size()); + for (Map.Entry pair : map.entrySet()) { + Integer val = m.get(pair.getKey().asInt()); + assertNotNull(val); + assertEquals(val.intValue(), pair.getValue().asInt()); + } + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(Integer.toString(j), j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + MessagePackObject obj = unpackOne(out); + Map map = obj.asMap(); + assertEquals(m.size(), map.size()); + for (Map.Entry pair : map.entrySet()) { + Integer val = m.get(pair.getKey().asString()); + assertNotNull(val); + assertEquals(val.intValue(), pair.getValue().asInt()); + } + } + } }; + From 5658ca5b903b294dcccad0dfa6fa6a7bcd9acc12 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 22:24:51 +0900 Subject: [PATCH 127/152] java: adds ObjectEquals test --- .../main/java/org/msgpack/UnpackerImpl.java | 30 +++--- .../java/org/msgpack/object/ArrayType.java | 6 +- .../msgpack/object/BigIntegerTypeIMPL.java | 2 +- .../java/org/msgpack/object/BooleanType.java | 6 +- .../msgpack/object/LongIntegerTypeIMPL.java | 2 +- .../main/java/org/msgpack/object/MapType.java | 6 +- .../main/java/org/msgpack/object/NilType.java | 6 ++ .../main/java/org/msgpack/object/RawType.java | 20 +++- .../java/org/msgpack/TestObjectEquals.java | 96 +++++++++++++++++++ 9 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 java/src/test/java/org/msgpack/TestObjectEquals.java diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java index f004e6c..cfd3d22 100644 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -124,7 +124,7 @@ public class UnpackerImpl { if((b & 0xe0) == 0xa0) { // FixRaw trail = b & 0x1f; if(trail == 0) { - obj = new RawType(new byte[0]); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -139,7 +139,7 @@ public class UnpackerImpl { //System.out.println("fixarray count:"+count); obj = new MessagePackObject[count]; if(count == 0) { - obj = new ArrayType((MessagePackObject[])obj); + obj = ArrayType.create((MessagePackObject[])obj); break _push; } ++top; @@ -159,7 +159,7 @@ public class UnpackerImpl { count = b & 0x0f; obj = new MessagePackObject[count*2]; if(count == 0) { - obj = new MapType((MessagePackObject[])obj); + obj = MapType.create((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -175,13 +175,13 @@ public class UnpackerImpl { switch(b & 0xff) { // FIXME case 0xc0: // nil - obj = new NilType(); + obj = NilType.create(); break _push; case 0xc2: // false - obj = new BooleanType(false); + obj = BooleanType.create(false); break _push; case 0xc3: // true - obj = new BooleanType(true); + obj = BooleanType.create(true); break _push; case 0xca: // float case 0xcb: // double @@ -293,7 +293,7 @@ public class UnpackerImpl { castBuffer.put(src, n, 2); trail = ((int)castBuffer.getShort(0)) & 0xffff; if(trail == 0) { - obj = new RawType(new byte[0]); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; @@ -304,14 +304,14 @@ public class UnpackerImpl { // FIXME overflow check trail = castBuffer.getInt(0) & 0x7fffffff; if(trail == 0) { - obj = new RawType(new byte[0]); + obj = RawType.create(new byte[0]); break _push; } cs = ACS_RAW_VALUE; case ACS_RAW_VALUE: { byte[] raw = new byte[trail]; System.arraycopy(src, n, raw, 0, trail); - obj = new RawType(raw); + obj = RawType.create(raw); } break _push; case CS_ARRAY_16: @@ -323,7 +323,7 @@ public class UnpackerImpl { count = ((int)castBuffer.getShort(0)) & 0xffff; obj = new MessagePackObject[count]; if(count == 0) { - obj = new ArrayType((MessagePackObject[])obj); + obj = ArrayType.create((MessagePackObject[])obj); break _push; } ++top; @@ -344,7 +344,7 @@ public class UnpackerImpl { count = castBuffer.getInt(0) & 0x7fffffff; obj = new MessagePackObject[count]; if(count == 0) { - obj = new ArrayType((MessagePackObject[])obj); + obj = ArrayType.create((MessagePackObject[])obj); break _push; } ++top; @@ -364,7 +364,7 @@ public class UnpackerImpl { count = ((int)castBuffer.getShort(0)) & 0xffff; obj = new MessagePackObject[count*2]; if(count == 0) { - obj = new MapType((MessagePackObject[])obj); + obj = MapType.create((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -386,7 +386,7 @@ public class UnpackerImpl { count = castBuffer.getInt(0) & 0x7fffffff; obj = new MessagePackObject[count*2]; if(count == 0) { - obj = new MapType((MessagePackObject[])obj); + obj = MapType.create((MessagePackObject[])obj); break _push; } //System.out.println("fixmap count:"+count); @@ -425,7 +425,7 @@ public class UnpackerImpl { top_obj = stack_obj[top]; top_ct = stack_ct[top]; top_count = stack_count[top]; - obj = new ArrayType((MessagePackObject[])ar); + obj = ArrayType.create((MessagePackObject[])ar); stack_obj[top] = null; --top; break _push; @@ -447,7 +447,7 @@ public class UnpackerImpl { top_obj = stack_obj[top]; top_ct = stack_ct[top]; top_count = stack_count[top]; - obj = new MapType((MessagePackObject[])mp); + obj = MapType.create((MessagePackObject[])mp); stack_obj[top] = null; --top; break _push; diff --git a/java/src/main/java/org/msgpack/object/ArrayType.java b/java/src/main/java/org/msgpack/object/ArrayType.java index 350ce32..36134dc 100644 --- a/java/src/main/java/org/msgpack/object/ArrayType.java +++ b/java/src/main/java/org/msgpack/object/ArrayType.java @@ -25,10 +25,14 @@ import org.msgpack.*; public class ArrayType extends MessagePackObject { private MessagePackObject[] array; - public ArrayType(MessagePackObject[] array) { + ArrayType(MessagePackObject[] array) { this.array = array; } + public static ArrayType create(MessagePackObject[] array) { + return new ArrayType(array); + } + @Override public boolean isArrayType() { return true; diff --git a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java index 7b060ee..b29879f 100644 --- a/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/BigIntegerTypeIMPL.java @@ -109,7 +109,7 @@ class BigIntegerTypeIMPL extends IntegerType { public boolean equals(Object obj) { if(obj.getClass() != getClass()) { if(obj.getClass() == ShortIntegerTypeIMPL.class) { - return BigInteger.valueOf((long)((ShortIntegerTypeIMPL)obj).shortValue()).equals(value); + return BigInteger.valueOf(((ShortIntegerTypeIMPL)obj).longValue()).equals(value); } else if(obj.getClass() == LongIntegerTypeIMPL.class) { return BigInteger.valueOf(((LongIntegerTypeIMPL)obj).longValue()).equals(value); } diff --git a/java/src/main/java/org/msgpack/object/BooleanType.java b/java/src/main/java/org/msgpack/object/BooleanType.java index 1d12c1c..65bd42b 100644 --- a/java/src/main/java/org/msgpack/object/BooleanType.java +++ b/java/src/main/java/org/msgpack/object/BooleanType.java @@ -23,10 +23,14 @@ import org.msgpack.*; public class BooleanType extends MessagePackObject { private boolean value; - public BooleanType(boolean value) { + BooleanType(boolean value) { this.value = value; } + public static BooleanType create(boolean value) { + return new BooleanType(value); + } + @Override public boolean isBooleanType() { return true; diff --git a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java index 3928a29..d052e77 100644 --- a/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java +++ b/java/src/main/java/org/msgpack/object/LongIntegerTypeIMPL.java @@ -108,7 +108,7 @@ class LongIntegerTypeIMPL extends IntegerType { if(obj.getClass() == ShortIntegerTypeIMPL.class) { return value == ((ShortIntegerTypeIMPL)obj).longValue(); } else if(obj.getClass() == BigIntegerTypeIMPL.class) { - return (long)value == ((BigIntegerTypeIMPL)obj).longValue(); + return BigInteger.valueOf(value).equals(((BigIntegerTypeIMPL)obj).bigIntegerValue()); } return false; } diff --git a/java/src/main/java/org/msgpack/object/MapType.java b/java/src/main/java/org/msgpack/object/MapType.java index 619d388..148c0bf 100644 --- a/java/src/main/java/org/msgpack/object/MapType.java +++ b/java/src/main/java/org/msgpack/object/MapType.java @@ -26,10 +26,14 @@ import org.msgpack.*; public class MapType extends MessagePackObject { private MessagePackObject[] map; - public MapType(MessagePackObject[] map) { + MapType(MessagePackObject[] map) { this.map = map; } + public static MapType create(MessagePackObject[] map) { + return new MapType(map); + } + @Override public boolean isMapType() { return true; diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java index ece62f0..d0572f1 100644 --- a/java/src/main/java/org/msgpack/object/NilType.java +++ b/java/src/main/java/org/msgpack/object/NilType.java @@ -21,6 +21,12 @@ import java.io.IOException; import org.msgpack.*; public class NilType extends MessagePackObject { + private static NilType instance = new NilType(); + + public static NilType create() { + return instance; + } + @Override public boolean isNull() { return true; diff --git a/java/src/main/java/org/msgpack/object/RawType.java b/java/src/main/java/org/msgpack/object/RawType.java index 3a39486..26f6e0d 100644 --- a/java/src/main/java/org/msgpack/object/RawType.java +++ b/java/src/main/java/org/msgpack/object/RawType.java @@ -24,10 +24,26 @@ import org.msgpack.*; public class RawType extends MessagePackObject { private byte[] bytes; - public RawType(byte[] bytes) { + RawType(byte[] bytes) { this.bytes = bytes; } + RawType(String str) { + try { + this.bytes = str.getBytes("UTF-8"); + } catch (Exception e) { + throw new MessageTypeException("type error"); + } + } + + public static RawType create(byte[] bytes) { + return new RawType(bytes); + } + + public static RawType create(String str) { + return new RawType(str); + } + @Override public boolean isRawType() { return true; @@ -58,7 +74,7 @@ public class RawType extends MessagePackObject { if(obj.getClass() != getClass()) { return false; } - return ((RawType)obj).bytes.equals(bytes); + return Arrays.equals(((RawType)obj).bytes, bytes); } @Override diff --git a/java/src/test/java/org/msgpack/TestObjectEquals.java b/java/src/test/java/org/msgpack/TestObjectEquals.java new file mode 100644 index 0000000..b2b018d --- /dev/null +++ b/java/src/test/java/org/msgpack/TestObjectEquals.java @@ -0,0 +1,96 @@ +package org.msgpack; + +import org.msgpack.*; +import org.msgpack.object.*; +import java.math.BigInteger; +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestObjectEquals { + public void testInt(int val) throws Exception { + MessagePackObject objInt = IntegerType.create(val); + MessagePackObject objLong = IntegerType.create((long)val); + MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf((long)val)); + assertTrue(objInt.equals(objInt)); + assertTrue(objInt.equals(objLong)); + assertTrue(objInt.equals(objBigInt)); + assertTrue(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertTrue(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } + + public void testLong(long val) throws Exception { + MessagePackObject objInt = IntegerType.create((int)val); + MessagePackObject objLong = IntegerType.create(val); + MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf(val)); + if(val > (long)Integer.MAX_VALUE || val < (long)Integer.MIN_VALUE) { + assertTrue(objInt.equals(objInt)); + assertFalse(objInt.equals(objLong)); + assertFalse(objInt.equals(objBigInt)); + assertFalse(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertFalse(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } else { + assertTrue(objInt.equals(objInt)); + assertTrue(objInt.equals(objLong)); + assertTrue(objInt.equals(objBigInt)); + assertTrue(objLong.equals(objInt)); + assertTrue(objLong.equals(objLong)); + assertTrue(objLong.equals(objBigInt)); + assertTrue(objBigInt.equals(objInt)); + assertTrue(objBigInt.equals(objLong)); + assertTrue(objBigInt.equals(objBigInt)); + } + } + @Test + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testLong(rand.nextLong()); + } + + @Test + public void testNil() throws Exception { + assertTrue(NilType.create().equals(NilType.create())); + assertFalse(NilType.create().equals(IntegerType.create(0))); + assertFalse(NilType.create().equals(BooleanType.create(false))); + } + + public void testString(String str) throws Exception { + assertTrue(RawType.create(str).equals(RawType.create(str))); + } + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + } +} + From fdfabc9f8862da5982830017e21b0628a00356eb Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 22:51:23 +0900 Subject: [PATCH 128/152] java: adds cross-language test case --- .../main/java/org/msgpack/UnpackerImpl.java | 1 + java/src/test/java/org/msgpack/TestCases.java | 46 ++++++++++++++++++ java/src/test/resources/cases.json | 1 + java/src/test/resources/cases.mpac | Bin 0 -> 213 bytes java/src/test/resources/cases_compact.mpac | Bin 0 -> 116 bytes 5 files changed, 48 insertions(+) create mode 100644 java/src/test/java/org/msgpack/TestCases.java create mode 100644 java/src/test/resources/cases.json create mode 100644 java/src/test/resources/cases.mpac create mode 100644 java/src/test/resources/cases_compact.mpac diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java index cfd3d22..d4f99e3 100644 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -308,6 +308,7 @@ public class UnpackerImpl { break _push; } cs = ACS_RAW_VALUE; + break _fixed_trail_again; case ACS_RAW_VALUE: { byte[] raw = new byte[trail]; System.arraycopy(src, n, raw, 0, trail); diff --git a/java/src/test/java/org/msgpack/TestCases.java b/java/src/test/java/org/msgpack/TestCases.java new file mode 100644 index 0000000..632645f --- /dev/null +++ b/java/src/test/java/org/msgpack/TestCases.java @@ -0,0 +1,46 @@ +package org.msgpack; + +import java.io.*; +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestCases { + public void feedFile(Unpacker pac, String path) throws Exception { + FileInputStream input = new FileInputStream(path); + byte[] buffer = new byte[32*1024]; + while(true) { + int count = input.read(buffer); + if(count < 0) { + break; + } + pac.feed(buffer, 0, count); + } + } + + @Test + public void testCases() throws Exception { + System.out.println( new File(".").getAbsoluteFile().getParent() ); + + + Unpacker pac = new Unpacker(); + Unpacker pac_compact = new Unpacker(); + + feedFile(pac, "src/test/resources/cases.mpac"); + feedFile(pac_compact, "src/test/resources/cases_compact.mpac"); + + UnpackResult result = new UnpackResult(); + while(pac.next(result)) { + UnpackResult result_compact = new UnpackResult(); + assertTrue( pac_compact.next(result_compact) ); + System.out.println("obj: "+result_compact.getData()); + if(!result.getData().equals(result_compact.getData())) { + System.out.println("compact: "+result_compact.getData().asString()); + System.out.println("data : "+result.getData().asString()); + } + assertTrue( result.getData().equals(result_compact.getData()) ); + } + } +}; + diff --git a/java/src/test/resources/cases.json b/java/src/test/resources/cases.json new file mode 100644 index 0000000..fd390d4 --- /dev/null +++ b/java/src/test/resources/cases.json @@ -0,0 +1 @@ +[false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]] \ No newline at end of file diff --git a/java/src/test/resources/cases.mpac b/java/src/test/resources/cases.mpac new file mode 100644 index 0000000000000000000000000000000000000000..5ec08c6a9af42d9568eb429a209a639616e94711 GIT binary patch literal 213 zcmXYp!4<+V3`3R4n8lOSY|v~#CV>aXw2-v7Qc6c)17ihrkbe}**V_dHM&J(DgGLop zU?R;l%8FI9$y_sy>V|HFdDW~{neAn-roN|Wd+OcH160;F91fo! Date: Wed, 18 Aug 2010 22:55:50 +0900 Subject: [PATCH 129/152] java: fixes cross-language test case --- java/src/test/java/org/msgpack/TestCases.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/java/src/test/java/org/msgpack/TestCases.java b/java/src/test/java/org/msgpack/TestCases.java index 632645f..c368972 100644 --- a/java/src/test/java/org/msgpack/TestCases.java +++ b/java/src/test/java/org/msgpack/TestCases.java @@ -21,9 +21,6 @@ public class TestCases { @Test public void testCases() throws Exception { - System.out.println( new File(".").getAbsoluteFile().getParent() ); - - Unpacker pac = new Unpacker(); Unpacker pac_compact = new Unpacker(); @@ -34,13 +31,10 @@ public class TestCases { while(pac.next(result)) { UnpackResult result_compact = new UnpackResult(); assertTrue( pac_compact.next(result_compact) ); - System.out.println("obj: "+result_compact.getData()); - if(!result.getData().equals(result_compact.getData())) { - System.out.println("compact: "+result_compact.getData().asString()); - System.out.println("data : "+result.getData().asString()); - } assertTrue( result.getData().equals(result_compact.getData()) ); } + + assertFalse( pac_compact.next(result) ); } }; From 40dc9de6c9bc90a95e55d5d2999f7e45d34cabc8 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 23:37:47 +0900 Subject: [PATCH 130/152] java: supports packing/unpacking of BigInteger less than 0xffffffffffffffff --- .../java/org/msgpack/MessageConvertable.java | 2 +- java/src/main/java/org/msgpack/Packer.java | 22 ++++++++------- .../main/java/org/msgpack/UnpackerImpl.java | 5 ++-- .../test/java/org/msgpack/TestPackUnpack.java | 27 +++++++++++++++++++ 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/java/src/main/java/org/msgpack/MessageConvertable.java b/java/src/main/java/org/msgpack/MessageConvertable.java index da251dc..8acf1f2 100644 --- a/java/src/main/java/org/msgpack/MessageConvertable.java +++ b/java/src/main/java/org/msgpack/MessageConvertable.java @@ -18,6 +18,6 @@ package org.msgpack; public interface MessageConvertable { - public void messageConvert(Object obj) throws MessageTypeException; + public void messageConvert(MessagePackObject obj) throws MessageTypeException; } diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 98af3d6..60e04bf 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -196,19 +196,19 @@ public class Packer { } public Packer packBigInteger(BigInteger d) throws IOException { - if(d.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) { + if(d.bitLength() <= 63) { return packLong(d.longValue()); - } else if(d.bitLength() <= 64) { + } else if(d.bitLength() <= 64 && d.signum() >= 0) { castBytes[0] = (byte)0xcf; byte[] barray = d.toByteArray(); - castBytes[1] = barray[0]; - castBytes[2] = barray[1]; - castBytes[3] = barray[2]; - castBytes[4] = barray[3]; - castBytes[5] = barray[4]; - castBytes[6] = barray[5]; - castBytes[7] = barray[6]; - castBytes[8] = barray[7]; + 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 { @@ -436,6 +436,8 @@ 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 MessageTypeException("unknown object "+o+" ("+o.getClass()+")"); } diff --git a/java/src/main/java/org/msgpack/UnpackerImpl.java b/java/src/main/java/org/msgpack/UnpackerImpl.java index d4f99e3..6a7085f 100644 --- a/java/src/main/java/org/msgpack/UnpackerImpl.java +++ b/java/src/main/java/org/msgpack/UnpackerImpl.java @@ -18,6 +18,7 @@ package org.msgpack; import java.nio.ByteBuffer; +import java.math.BigInteger; import org.msgpack.object.*; public class UnpackerImpl { @@ -262,9 +263,7 @@ 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 = IntegerType.create(o); } diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index ca3d235..75c5fe7 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -3,6 +3,7 @@ package org.msgpack; import org.msgpack.*; import java.io.*; import java.util.*; +import java.math.BigInteger; import org.junit.Test; import static org.junit.Assert.*; @@ -36,6 +37,32 @@ public class TestPackUnpack { testInt(rand.nextInt()); } + public void testBigInteger(BigInteger val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + if(!val.equals(obj.asBigInteger())) { + System.out.println("expect: "+val); + System.out.println("but : "+obj.asBigInteger()); + } + assertEquals(val, obj.asBigInteger()); + } + @Test + public void testBigInteger() throws Exception { + testBigInteger(BigInteger.valueOf(0)); + testBigInteger(BigInteger.valueOf(-1)); + testBigInteger(BigInteger.valueOf(1)); + testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); + BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); + testBigInteger(max); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); + } + public void testFloat(float val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); From 48da2b8353a640de6bdc4e59c7fe91b640321b27 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 23:47:31 +0900 Subject: [PATCH 131/152] java: adds Packer.pack() --- java/src/main/java/org/msgpack/Packer.java | 103 ++++++++++++------ .../test/java/org/msgpack/TestPackUnpack.java | 24 +++- 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 60e04bf..0ac8d89 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -315,39 +315,36 @@ public class Packer { } - 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; - } - - @SuppressWarnings("unchecked") - public Packer pack(Map o) throws IOException { - if(o == null) { return packNil(); } - packMap(o.size()); - for(Map.Entry e : ((Map)o).entrySet()) { - pack(e.getKey()); - pack(e.getValue()); + public Packer pack(boolean o) throws IOException { + if(o) { + return packTrue(); + } else { + return packFalse(); } - return this; + } + + public Packer pack(byte o) throws IOException { + return packByte(o); + } + + public Packer pack(short o) throws IOException { + return packShort(o); + } + + public Packer pack(int o) throws IOException { + return packInt(o); + } + + public Packer pack(long o) throws IOException { + return packLong(o); + } + + 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 { @@ -379,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); @@ -389,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 e : ((Map)o).entrySet()) { + pack(e.getKey()); + pack(e.getValue()); + } + return this; + } + - @SuppressWarnings("unchecked") public Packer pack(Object o) throws IOException { if(o == null) { return packNil(); @@ -413,7 +448,7 @@ public class Packer { } else if(o instanceof Map) { Map m = (Map)o; packMap(m.size()); - for(Map.Entry e : m.entrySet()) { + for(Map.Entry e : m.entrySet()) { pack(e.getKey()); pack(e.getValue()); } diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 75c5fe7..8163678 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -37,14 +37,30 @@ public class TestPackUnpack { testInt(rand.nextInt()); } + public void testLong(long val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asLong()); + } + @Test + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testLong(rand.nextLong()); + } + public void testBigInteger(BigInteger val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - if(!val.equals(obj.asBigInteger())) { - System.out.println("expect: "+val); - System.out.println("but : "+obj.asBigInteger()); - } assertEquals(val, obj.asBigInteger()); } @Test From 193a739749687e45a71a6821e625815587c4c4bf Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 19 Aug 2010 00:05:48 +0900 Subject: [PATCH 132/152] java: updates TestDirectConversion --- cpp/test/cases.cc | 2 + .../org/msgpack/TestDirectConversion.java | 369 +++++++++++------- .../java/org/msgpack/TestObjectEquals.java | 48 +-- .../test/java/org/msgpack/TestPackUnpack.java | 54 +-- 4 files changed, 292 insertions(+), 181 deletions(-) diff --git a/cpp/test/cases.cc b/cpp/test/cases.cc index b408876..eb1286c 100644 --- a/cpp/test/cases.cc +++ b/cpp/test/cases.cc @@ -32,5 +32,7 @@ TEST(cases, format) EXPECT_TRUE( pac_compact.next(&result_compact) ); EXPECT_EQ(result_compact.get(), result.get()); } + + EXPECT_FALSE( pac_compact.next(&result) ); } diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java index 77bbc58..dbacf01 100644 --- a/java/src/test/java/org/msgpack/TestDirectConversion.java +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -8,141 +8,248 @@ import org.junit.Test; import static org.junit.Assert.*; public class TestDirectConversion { - @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testInt(rand.nextInt()); - } - public void testInt(int val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - assertEquals(val, upk.unpackInt()); - } + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } + public void testInt(int val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackInt()); + } - @Test - public void testFloat() throws Exception { - testFloat((float)0.0); - testFloat((float)-0.0); - testFloat((float)1.0); - testFloat((float)-1.0); - testFloat((float)Float.MAX_VALUE); - testFloat((float)Float.MIN_VALUE); - testFloat((float)Float.NaN); - testFloat((float)Float.NEGATIVE_INFINITY); - testFloat((float)Float.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testFloat(rand.nextFloat()); - } - public void testFloat(float val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - float f = upk.unpackFloat(); - if(Float.isNaN(val)) { - assertTrue(Float.isNaN(f)); - } else { - assertEquals(val, f, 10e-10); - } - } + @Test + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testLong(rand.nextLong()); + } + public void testLong(long val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackLong()); + } - @Test - public void testDouble() throws Exception { - testDouble((double)0.0); - testDouble((double)-0.0); - testDouble((double)1.0); - testDouble((double)-1.0); - testDouble((double)Double.MAX_VALUE); - testDouble((double)Double.MIN_VALUE); - testDouble((double)Double.NaN); - testDouble((double)Double.NEGATIVE_INFINITY); - testDouble((double)Double.POSITIVE_INFINITY); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testDouble(rand.nextDouble()); - } - public void testDouble(double val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - double f = upk.unpackDouble(); - if(Double.isNaN(val)) { - assertTrue(Double.isNaN(f)); - } else { - assertEquals(val, f, 10e-10); - } - } + @Test + public void testFloat() throws Exception { + testFloat((float)0.0); + testFloat((float)-0.0); + testFloat((float)1.0); + testFloat((float)-1.0); + testFloat((float)Float.MAX_VALUE); + testFloat((float)Float.MIN_VALUE); + testFloat((float)Float.NaN); + testFloat((float)Float.NEGATIVE_INFINITY); + testFloat((float)Float.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testFloat(rand.nextFloat()); + } + public void testFloat(float val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackFloat(), 10e-10); + } - @Test - public void testNil() throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).packNil(); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - assertEquals(null, upk.unpackNull()); - } + @Test + public void testDouble() throws Exception { + testDouble((double)0.0); + testDouble((double)-0.0); + testDouble((double)1.0); + testDouble((double)-1.0); + testDouble((double)Double.MAX_VALUE); + testDouble((double)Double.MIN_VALUE); + testDouble((double)Double.NaN); + testDouble((double)Double.NEGATIVE_INFINITY); + testDouble((double)Double.POSITIVE_INFINITY); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testDouble(rand.nextDouble()); + } + public void testDouble(double val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackDouble(), 10e-10); + } - @Test - public void testBoolean() throws Exception { - testBoolean(false); - testBoolean(true); - } - public void testBoolean(boolean val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - assertEquals(val, upk.unpackBoolean()); - } + @Test + public void testNil() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).packNil(); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(null, pac.unpackNull()); + } - @Test - public void testString() throws Exception { - testString(""); - testString("a"); - testString("ab"); - testString("abc"); - // small size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 31 + 1; - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - // medium size string - for (int i = 0; i < 100; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 15); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - // large size string - for (int i = 0; i < 10; i++) { - StringBuilder sb = new StringBuilder(); - int len = (int)Math.random() % 100 + (1 << 31); - for (int j = 0; j < len; j++) - sb.append('a' + ((int)Math.random()) & 26); - testString(sb.toString()); - } - } - public void testString(String val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - assertEquals(val, upk.unpackString()); - } + @Test + public void testBoolean() throws Exception { + testBoolean(false); + testBoolean(true); + } + public void testBoolean(boolean val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackBoolean()); + } - // FIXME container types + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + + // small size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 31 + 1; + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + + // medium size string + for (int i = 0; i < 100; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 15); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + + // large size string + for (int i = 0; i < 10; i++) { + StringBuilder sb = new StringBuilder(); + int len = (int)Math.random() % 100 + (1 << 31); + for (int j = 0; j < len; j++) + sb.append('a' + ((int)Math.random()) & 26); + testString(sb.toString()); + } + } + public void testString(String val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackString()); + } + + @Test + public void testArray() throws Exception { + List emptyList = new ArrayList(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyList); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackArray(); + assertEquals(0, ulen); + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackArray(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j).intValue(), pac.unpackInt()); + } + } + + for (int i = 0; i < 1000; i++) { + List l = new ArrayList(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + l.add(Integer.toString(j)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(l); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackArray(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + assertEquals(l.get(j), pac.unpackString()); + } + } + } + + @Test + public void testMap() throws Exception { + Map emptyMap = new HashMap(); + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(emptyMap); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackMap(); + assertEquals(0, ulen); + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(j, j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackMap(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + Integer val = m.get(pac.unpackInt()); + assertNotNull(val); + assertEquals(val.intValue(), pac.unpackInt()); + } + } + + for (int i = 0; i < 1000; i++) { + Map m = new HashMap(); + int len = (int)Math.random() % 1000 + 1; + for (int j = 0; j < len; j++) + m.put(Integer.toString(j), j); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(m); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + int ulen = pac.unpackMap(); + assertEquals(len, ulen); + for (int j = 0; j < len; j++) { + Integer val = m.get(pac.unpackString()); + assertNotNull(val); + assertEquals(val.intValue(), pac.unpackInt()); + } + } + } }; + diff --git a/java/src/test/java/org/msgpack/TestObjectEquals.java b/java/src/test/java/org/msgpack/TestObjectEquals.java index b2b018d..25fe927 100644 --- a/java/src/test/java/org/msgpack/TestObjectEquals.java +++ b/java/src/test/java/org/msgpack/TestObjectEquals.java @@ -9,6 +9,17 @@ import org.junit.Test; import static org.junit.Assert.*; public class TestObjectEquals { + @Test + public void testInt() throws Exception { + testInt(0); + testInt(-1); + testInt(1); + testInt(Integer.MIN_VALUE); + testInt(Integer.MAX_VALUE); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testInt(rand.nextInt()); + } public void testInt(int val) throws Exception { MessagePackObject objInt = IntegerType.create(val); MessagePackObject objLong = IntegerType.create((long)val); @@ -23,18 +34,20 @@ public class TestObjectEquals { assertTrue(objBigInt.equals(objLong)); assertTrue(objBigInt.equals(objBigInt)); } + @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); + public void testLong() throws Exception { + testLong(0); + testLong(-1); + testLong(1); + testLong(Integer.MIN_VALUE); + testLong(Integer.MAX_VALUE); + testLong(Long.MIN_VALUE); + testLong(Long.MAX_VALUE); Random rand = new Random(); for (int i = 0; i < 1000; i++) - testInt(rand.nextInt()); + testLong(rand.nextLong()); } - public void testLong(long val) throws Exception { MessagePackObject objInt = IntegerType.create((int)val); MessagePackObject objLong = IntegerType.create(val); @@ -61,19 +74,6 @@ public class TestObjectEquals { assertTrue(objBigInt.equals(objBigInt)); } } - @Test - public void testLong() throws Exception { - testLong(0); - testLong(-1); - testLong(1); - testLong(Integer.MIN_VALUE); - testLong(Integer.MAX_VALUE); - testLong(Long.MIN_VALUE); - testLong(Long.MAX_VALUE); - Random rand = new Random(); - for (int i = 0; i < 1000; i++) - testLong(rand.nextLong()); - } @Test public void testNil() throws Exception { @@ -82,9 +82,6 @@ public class TestObjectEquals { assertFalse(NilType.create().equals(BooleanType.create(false))); } - public void testString(String str) throws Exception { - assertTrue(RawType.create(str).equals(RawType.create(str))); - } @Test public void testString() throws Exception { testString(""); @@ -92,5 +89,8 @@ public class TestObjectEquals { testString("ab"); testString("abc"); } + public void testString(String str) throws Exception { + assertTrue(RawType.create(str).equals(RawType.create(str))); + } } diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 8163678..7edd411 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -11,20 +11,14 @@ import static org.junit.Assert.*; public class TestPackUnpack { public MessagePackObject unpackOne(ByteArrayOutputStream out) { ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - Iterator it = upk.iterator(); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); assertEquals(true, it.hasNext()); MessagePackObject obj = it.next(); assertEquals(false, it.hasNext()); return obj; } - public void testInt(int val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asInt()); - } @Test public void testInt() throws Exception { testInt(0); @@ -36,13 +30,13 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testInt(rand.nextInt()); } - - public void testLong(long val) throws Exception { + public void testInt(int val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asLong()); + assertEquals(val, obj.asInt()); } + @Test public void testLong() throws Exception { testLong(0); @@ -56,13 +50,13 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testLong(rand.nextLong()); } - - public void testBigInteger(BigInteger val) throws Exception { + public void testLong(long val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asBigInteger()); + assertEquals(val, obj.asLong()); } + @Test public void testBigInteger() throws Exception { testBigInteger(BigInteger.valueOf(0)); @@ -78,13 +72,13 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); } - - public void testFloat(float val) throws Exception { + public void testBigInteger(BigInteger val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asFloat(), 10e-10); + assertEquals(val, obj.asBigInteger()); } + @Test public void testFloat() throws Exception { testFloat((float)0.0); @@ -100,13 +94,14 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testFloat(rand.nextFloat()); } - - public void testDouble(double val) throws Exception { + public void testFloat(float val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asDouble(), 10e-10); + float f = obj.asFloat(); + assertEquals(val, f, 10e-10); } + @Test public void testDouble() throws Exception { testDouble((double)0.0); @@ -122,6 +117,13 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testDouble(rand.nextDouble()); } + public void testDouble(double val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + double f = obj.asDouble(); + assertEquals(val, f, 10e-10); + } @Test public void testNil() throws Exception { @@ -143,12 +145,6 @@ public class TestPackUnpack { assertEquals(val, obj.asBoolean()); } - public void testString(String val) throws Exception { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - new Packer(out).pack(val); - MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asString()); - } @Test public void testString() throws Exception { testString(""); @@ -183,6 +179,12 @@ public class TestPackUnpack { testString(sb.toString()); } } + public void testString(String val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asString()); + } @Test public void testArray() throws Exception { From 1d17836b7d2eb4e5e0f25d2466df70e137642061 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 19 Aug 2010 00:54:23 +0900 Subject: [PATCH 133/152] java: NilType::create() returns NilType's one and only instance --- java/src/main/java/org/msgpack/MessagePackObject.java | 2 +- java/src/main/java/org/msgpack/object/NilType.java | 10 ++++++---- java/src/test/java/org/msgpack/TestPackUnpack.java | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/java/src/main/java/org/msgpack/MessagePackObject.java b/java/src/main/java/org/msgpack/MessagePackObject.java index 6181f7a..2424446 100644 --- a/java/src/main/java/org/msgpack/MessagePackObject.java +++ b/java/src/main/java/org/msgpack/MessagePackObject.java @@ -23,7 +23,7 @@ import java.util.Map; import java.math.BigInteger; public abstract class MessagePackObject implements Cloneable, MessagePackable { - public boolean isNull() { + public boolean isNil() { return false; } diff --git a/java/src/main/java/org/msgpack/object/NilType.java b/java/src/main/java/org/msgpack/object/NilType.java index d0572f1..c443db1 100644 --- a/java/src/main/java/org/msgpack/object/NilType.java +++ b/java/src/main/java/org/msgpack/object/NilType.java @@ -21,14 +21,16 @@ import java.io.IOException; import org.msgpack.*; public class NilType extends MessagePackObject { - private static NilType instance = new NilType(); + private final static NilType INSTANCE = new NilType(); public static NilType create() { - return instance; + return INSTANCE; } + private NilType() { } + @Override - public boolean isNull() { + public boolean isNil() { return true; } @@ -52,7 +54,7 @@ public class NilType extends MessagePackObject { @Override public Object clone() { - return new NilType(); + return INSTANCE; } } diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 7edd411..494c8a8 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -130,7 +130,7 @@ public class TestPackUnpack { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).packNil(); MessagePackObject obj = unpackOne(out); - assertTrue(obj.isNull()); + assertTrue(obj.isNil()); } @Test From b4c98584db002cbb4d3960b6ed025212248dcce8 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 19 Aug 2010 01:19:34 +0900 Subject: [PATCH 134/152] java: adds Unpacker.unpackBigInteger() --- .../org/msgpack/BufferedUnpackerImpl.java | 25 ++++++++++++++++--- java/src/main/java/org/msgpack/Unpacker.java | 10 ++++++++ .../org/msgpack/TestDirectConversion.java | 24 ++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java index 9496238..5b449c7 100644 --- a/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java +++ b/java/src/main/java/org/msgpack/BufferedUnpackerImpl.java @@ -19,7 +19,7 @@ package org.msgpack; import java.io.IOException; import java.nio.ByteBuffer; -//import java.math.BigInteger; +import java.math.BigInteger; abstract class BufferedUnpackerImpl extends UnpackerImpl { int offset = 0; @@ -198,8 +198,7 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { { long o = castBuffer.getLong(0); if(o < 0) { - // FIXME unpackBigInteger - throw new MessageTypeException("uint 64 bigger than 0x7fffffff is not supported"); + throw new MessageTypeException(); } advance(9); return o; @@ -231,7 +230,25 @@ abstract class BufferedUnpackerImpl extends UnpackerImpl { } } - // FIXME unpackBigInteger + 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); diff --git a/java/src/main/java/org/msgpack/Unpacker.java b/java/src/main/java/org/msgpack/Unpacker.java index 3a95243..3cae502 100644 --- a/java/src/main/java/org/msgpack/Unpacker.java +++ b/java/src/main/java/org/msgpack/Unpacker.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.io.IOException; import java.util.Iterator; import java.nio.ByteBuffer; +import java.math.BigInteger; /** * Unpacker enables you to deserialize objects from stream. @@ -452,6 +453,15 @@ public class Unpacker implements Iterable { 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. diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java index dbacf01..1822ecb 100644 --- a/java/src/test/java/org/msgpack/TestDirectConversion.java +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -3,6 +3,7 @@ package org.msgpack; import org.msgpack.*; import java.io.*; import java.util.*; +import java.math.BigInteger; import org.junit.Test; import static org.junit.Assert.*; @@ -48,6 +49,29 @@ public class TestDirectConversion { assertEquals(val, pac.unpackLong()); } + @Test + public void testBigInteger() throws Exception { + testBigInteger(BigInteger.valueOf(0)); + testBigInteger(BigInteger.valueOf(-1)); + testBigInteger(BigInteger.valueOf(1)); + testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MIN_VALUE)); + testBigInteger(BigInteger.valueOf(Long.MAX_VALUE)); + BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63); + testBigInteger(max); + Random rand = new Random(); + for (int i = 0; i < 1000; i++) + testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); + } + public void testBigInteger(BigInteger val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + assertEquals(val, pac.unpackBigInteger()); + } + @Test public void testFloat() throws Exception { testFloat((float)0.0); From c8e351b31e28042c10f7406238636c92a64a696c Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 21 Aug 2010 03:36:44 +0900 Subject: [PATCH 135/152] java: adds TestMessageUnpackable test --- java/src/main/java/org/msgpack/Packer.java | 2 +- java/src/test/java/org/msgpack/Image.java | 50 +++++++++++++++++++ .../org/msgpack/TestMessageUnpackable.java | 34 +++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 java/src/test/java/org/msgpack/Image.java create mode 100644 java/src/test/java/org/msgpack/TestMessageUnpackable.java diff --git a/java/src/main/java/org/msgpack/Packer.java b/java/src/main/java/org/msgpack/Packer.java index 0ac8d89..139b3b1 100644 --- a/java/src/main/java/org/msgpack/Packer.java +++ b/java/src/main/java/org/msgpack/Packer.java @@ -212,7 +212,7 @@ public class Packer { out.write(castBytes); return this; } else { - throw new MessageTypeException("can't BigInteger larger than 0xffffffffffffffff"); + throw new MessageTypeException("can't pack BigInteger larger than 0xffffffffffffffff"); } } diff --git a/java/src/test/java/org/msgpack/Image.java b/java/src/test/java/org/msgpack/Image.java new file mode 100644 index 0000000..3fcfe38 --- /dev/null +++ b/java/src/test/java/org/msgpack/Image.java @@ -0,0 +1,50 @@ +package org.msgpack; + +import org.msgpack.*; +import java.io.*; +import java.util.*; + +public class Image implements MessagePackable, MessageUnpackable { + public String uri = ""; + public String title = ""; + public int width = 0; + public int height = 0; + public int size = 0; + + public void messagePack(Packer pk) throws IOException { + pk.packArray(5); + pk.pack(uri); + pk.pack(title); + pk.pack(width); + pk.pack(height); + pk.pack(size); + } + + public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException { + int length = pac.unpackArray(); + if(length != 5) { + throw new MessageTypeException(); + } + uri = pac.unpackString(); + title = pac.unpackString(); + width = pac.unpackInt(); + height = pac.unpackInt(); + size = pac.unpackInt(); + } + + public boolean equals(Image obj) { + return uri.equals(obj.uri) && + title.equals(obj.title) && + width == obj.width && + height == obj.height && + size == obj.size; + } + + public boolean equals(Object obj) { + if(obj.getClass() != Image.class) { + return false; + } + return equals((Image)obj); + } +} + diff --git a/java/src/test/java/org/msgpack/TestMessageUnpackable.java b/java/src/test/java/org/msgpack/TestMessageUnpackable.java new file mode 100644 index 0000000..9099f91 --- /dev/null +++ b/java/src/test/java/org/msgpack/TestMessageUnpackable.java @@ -0,0 +1,34 @@ +package org.msgpack; + +import org.msgpack.*; +import java.io.*; +import java.util.*; +import java.math.BigInteger; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestMessageUnpackable { + @Test + public void testImage() throws Exception { + Image src = new Image(); + src.title = "msgpack"; + src.uri = "http://msgpack.org/"; + src.width = 2560; + src.height = 1600; + src.size = 4096000; + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + src.messagePack(new Packer(out)); + + Image dst = new Image(); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker pac = new Unpacker(in); + + dst.messageUnpack(pac); + + assertEquals(src, dst); + } +} + From a91c1ec6d9b86a3f8f71504e0889191da13a210e Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Sat, 21 Aug 2010 16:02:23 +0900 Subject: [PATCH 136/152] fixed segv on cyclic reference(patch by dankogai) --- perl/pack.c | 28 +++++++++++++++++----------- perl/t/08_cycle.t | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 perl/t/08_cycle.t diff --git a/perl/pack.c b/perl/pack.c index 589cee8..af6669c 100644 --- a/perl/pack.c +++ b/perl/pack.c @@ -49,6 +49,8 @@ static void need(enc_t *enc, STRLEN len); # error "msgpack only supports IVSIZE = 8,4,2 environment." #endif +#define ERR_NESTING_EXCEEDED "perl structure exceeds maximum nesting level (max_depth set too low?)" + static void need(enc_t *enc, STRLEN len) { @@ -146,9 +148,10 @@ static int try_int(enc_t* enc, const char *p, size_t len) { } -static void _msgpack_pack_rv(enc_t *enc, SV* sv); +static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth); -static void _msgpack_pack_sv(enc_t *enc, SV* sv) { +static void _msgpack_pack_sv(enc_t *enc, SV* sv, int depth) { + if (!depth) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); if (sv==NULL) { @@ -171,7 +174,7 @@ static void _msgpack_pack_sv(enc_t *enc, SV* sv) { } else if (SvIOKp(sv)) { PACK_IV(enc, SvIV(sv)); } else if (SvROK(sv)) { - _msgpack_pack_rv(enc, SvRV(sv)); + _msgpack_pack_rv(enc, SvRV(sv), depth-1); } else if (!SvOK(sv)) { msgpack_pack_nil(enc); } else if (isGV(sv)) { @@ -182,8 +185,9 @@ static void _msgpack_pack_sv(enc_t *enc, SV* sv) { } } -static void _msgpack_pack_rv(enc_t *enc, SV* sv) { +static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth) { svtype svt; + if (!depth) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); svt = SvTYPE(sv); @@ -207,8 +211,8 @@ static void _msgpack_pack_rv(enc_t *enc, SV* sv) { msgpack_pack_map(enc, count); while (he = hv_iternext(hval)) { - _msgpack_pack_sv(enc, hv_iterkeysv(he)); - _msgpack_pack_sv(enc, HeVAL(he)); + _msgpack_pack_sv(enc, hv_iterkeysv(he), depth); + _msgpack_pack_sv(enc, HeVAL(he), depth); } } else if (svt == SVt_PVAV) { AV* ary = (AV*)sv; @@ -218,7 +222,7 @@ static void _msgpack_pack_rv(enc_t *enc, SV* sv) { for (i=0; ipack($dat)"); + if (items < 2) { + Perl_croak(aTHX_ "Usage: Data::MessagePack->pack($dat [,$max_depth])"); } - SV* val = ST(1); + SV* val = ST(1); + int depth = 512; + if (items >= 3) depth = SvIV(ST(2)); enc_t enc; enc.sv = sv_2mortal(NEWSV(0, INIT_SIZE)); @@ -256,7 +262,7 @@ XS(xs_pack) { enc.end = SvEND(enc.sv); SvPOK_only(enc.sv); - _msgpack_pack_sv(&enc, val); + _msgpack_pack_sv(&enc, val, depth); SvCUR_set(enc.sv, enc.cur - SvPVX (enc.sv)); *SvEND (enc.sv) = 0; /* many xs functions expect a trailing 0 for text strings */ diff --git a/perl/t/08_cycle.t b/perl/t/08_cycle.t new file mode 100644 index 0000000..55d8427 --- /dev/null +++ b/perl/t/08_cycle.t @@ -0,0 +1,25 @@ +use t::Util; +use Test::More; +use Data::MessagePack; + +plan tests => 5; + +my $aref = [0]; +$aref->[1] = $aref; +eval { Data::MessagePack->pack($aref) }; +ok $@, $@; + +my $href = {}; +$href->{cycle} = $href; +eval { Data::MessagePack->pack($aref) }; +ok $@, $@; + +$aref = [0,[1,2]]; +eval { Data::MessagePack->pack($aref) }; +ok !$@; + +eval { Data::MessagePack->pack($aref, 3) }; +ok !$@; + +eval { Data::MessagePack->pack($aref, 2) }; +ok $@, $@; From 8de1f764fdcab4e9ec3f5739b2fb0a8ae2a02df9 Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Sat, 21 Aug 2010 16:09:30 +0900 Subject: [PATCH 137/152] Perl: bump up version to 0.14 --- perl/Changes | 5 +++++ perl/lib/Data/MessagePack.pm | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/perl/Changes b/perl/Changes index f32d5f4..13fc98b 100644 --- a/perl/Changes +++ b/perl/Changes @@ -1,3 +1,8 @@ +0.14 + + - fixed segv on serializing cyclic reference + (Dan Kogai) + 0.13 - clearly specify requires_c99(), because msgpack C header requires C99. diff --git a/perl/lib/Data/MessagePack.pm b/perl/lib/Data/MessagePack.pm index ee07d35..3c38a79 100644 --- a/perl/lib/Data/MessagePack.pm +++ b/perl/lib/Data/MessagePack.pm @@ -4,7 +4,7 @@ use warnings; use XSLoader; use 5.008001; -our $VERSION = '0.13'; +our $VERSION = '0.14'; our $PreferInteger = 0; our $true = do { bless \(my $dummy = 1), "Data::MessagePack::Boolean" }; From 59ba8dec4ee082e8777047e6ae72e8b6998cdc79 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Fri, 27 Aug 2010 16:45:48 +0900 Subject: [PATCH 138/152] cpp: fixes include paths --- cpp/src/msgpack/object.h | 2 +- cpp/src/msgpack/object.hpp | 6 +++--- cpp/src/msgpack/pack.h | 9 ++++++--- cpp/src/msgpack/pack.hpp | 4 ++-- cpp/src/msgpack/sbuffer.hpp | 2 +- cpp/src/msgpack/type.hpp | 28 ++++++++++++++-------------- cpp/src/msgpack/unpack.h | 4 ++-- cpp/src/msgpack/unpack.hpp | 6 +++--- cpp/src/msgpack/vrefbuffer.h | 2 +- cpp/src/msgpack/vrefbuffer.hpp | 2 +- cpp/src/msgpack/zbuffer.h | 2 +- cpp/src/msgpack/zbuffer.hpp | 2 +- cpp/src/msgpack/zone.h | 2 +- cpp/src/msgpack/zone.hpp.erb | 2 +- 14 files changed, 38 insertions(+), 35 deletions(-) diff --git a/cpp/src/msgpack/object.h b/cpp/src/msgpack/object.h index cf0b4c1..baeeb9b 100644 --- a/cpp/src/msgpack/object.h +++ b/cpp/src/msgpack/object.h @@ -18,7 +18,7 @@ #ifndef MSGPACK_OBJECT_H__ #define MSGPACK_OBJECT_H__ -#include "msgpack/zone.h" +#include "zone.h" #include #ifdef __cplusplus diff --git a/cpp/src/msgpack/object.hpp b/cpp/src/msgpack/object.hpp index f80a390..96c026e 100644 --- a/cpp/src/msgpack/object.hpp +++ b/cpp/src/msgpack/object.hpp @@ -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 #include #include diff --git a/cpp/src/msgpack/pack.h b/cpp/src/msgpack/pack.h index 1252895..9c4ce59 100644 --- a/cpp/src/msgpack/pack.h +++ b/cpp/src/msgpack/pack.h @@ -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 #ifdef __cplusplus @@ -96,12 +96,15 @@ 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_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) { diff --git a/cpp/src/msgpack/pack.hpp b/cpp/src/msgpack/pack.hpp index ee90690..2f7dfa0 100644 --- a/cpp/src/msgpack/pack.hpp +++ b/cpp/src/msgpack/pack.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_PACK_HPP__ #define MSGPACK_PACK_HPP__ -#include "msgpack/pack_define.h" +#include "pack_define.h" #include #include @@ -137,7 +137,7 @@ inline void pack(Stream& s, const T& v) #define msgpack_pack_append_buffer append_buffer -#include "msgpack/pack_template.h" +#include "pack_template.h" template diff --git a/cpp/src/msgpack/sbuffer.hpp b/cpp/src/msgpack/sbuffer.hpp index e4a3f96..a9efc6d 100644 --- a/cpp/src/msgpack/sbuffer.hpp +++ b/cpp/src/msgpack/sbuffer.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_SBUFFER_HPP__ #define MSGPACK_SBUFFER_HPP__ -#include "msgpack/sbuffer.h" +#include "sbuffer.h" #include namespace msgpack { diff --git a/cpp/src/msgpack/type.hpp b/cpp/src/msgpack/type.hpp index fafa674..a55c68e 100644 --- a/cpp/src/msgpack/type.hpp +++ b/cpp/src/msgpack/type.hpp @@ -1,15 +1,15 @@ -#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" +#include "type/bool.hpp" +#include "type/float.hpp" +#include "type/int.hpp" +#include "type/list.hpp" +#include "type/deque.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" diff --git a/cpp/src/msgpack/unpack.h b/cpp/src/msgpack/unpack.h index 82698fc..bea7d92 100644 --- a/cpp/src/msgpack/unpack.h +++ b/cpp/src/msgpack/unpack.h @@ -18,8 +18,8 @@ #ifndef MSGPACK_UNPACKER_H__ #define MSGPACK_UNPACKER_H__ -#include "msgpack/zone.h" -#include "msgpack/object.h" +#include "zone.h" +#include "object.h" #include #ifdef __cplusplus diff --git a/cpp/src/msgpack/unpack.hpp b/cpp/src/msgpack/unpack.hpp index 7b45017..51580b6 100644 --- a/cpp/src/msgpack/unpack.hpp +++ b/cpp/src/msgpack/unpack.hpp @@ -18,9 +18,9 @@ #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 #include diff --git a/cpp/src/msgpack/vrefbuffer.h b/cpp/src/msgpack/vrefbuffer.h index 123499d..0643927 100644 --- a/cpp/src/msgpack/vrefbuffer.h +++ b/cpp/src/msgpack/vrefbuffer.h @@ -18,7 +18,7 @@ #ifndef MSGPACK_VREFBUFFER_H__ #define MSGPACK_VREFBUFFER_H__ -#include "msgpack/zone.h" +#include "zone.h" #include #ifndef _WIN32 diff --git a/cpp/src/msgpack/vrefbuffer.hpp b/cpp/src/msgpack/vrefbuffer.hpp index 7e0ffb2..8233527 100644 --- a/cpp/src/msgpack/vrefbuffer.hpp +++ b/cpp/src/msgpack/vrefbuffer.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_VREFBUFFER_HPP__ #define MSGPACK_VREFBUFFER_HPP__ -#include "msgpack/vrefbuffer.h" +#include "vrefbuffer.h" #include namespace msgpack { diff --git a/cpp/src/msgpack/zbuffer.h b/cpp/src/msgpack/zbuffer.h index abb9c50..efdd304 100644 --- a/cpp/src/msgpack/zbuffer.h +++ b/cpp/src/msgpack/zbuffer.h @@ -18,7 +18,7 @@ #ifndef MSGPACK_ZBUFFER_H__ #define MSGPACK_ZBUFFER_H__ -#include "msgpack/sysdep.h" +#include "sysdep.h" #include #include #include diff --git a/cpp/src/msgpack/zbuffer.hpp b/cpp/src/msgpack/zbuffer.hpp index 278f076..08e6fd0 100644 --- a/cpp/src/msgpack/zbuffer.hpp +++ b/cpp/src/msgpack/zbuffer.hpp @@ -18,7 +18,7 @@ #ifndef MSGPACK_ZBUFFER_HPP__ #define MSGPACK_ZBUFFER_HPP__ -#include "msgpack/zbuffer.h" +#include "zbuffer.h" #include namespace msgpack { diff --git a/cpp/src/msgpack/zone.h b/cpp/src/msgpack/zone.h index 0e811df..d8c60b6 100644 --- a/cpp/src/msgpack/zone.h +++ b/cpp/src/msgpack/zone.h @@ -18,7 +18,7 @@ #ifndef MSGPACK_ZONE_H__ #define MSGPACK_ZONE_H__ -#include "msgpack/sysdep.h" +#include "sysdep.h" #ifdef __cplusplus extern "C" { diff --git a/cpp/src/msgpack/zone.hpp.erb b/cpp/src/msgpack/zone.hpp.erb index 8e69aa4..1cef05e 100644 --- a/cpp/src/msgpack/zone.hpp.erb +++ b/cpp/src/msgpack/zone.hpp.erb @@ -18,7 +18,7 @@ #ifndef MSGPACK_ZONE_HPP__ #define MSGPACK_ZONE_HPP__ -#include "msgpack/zone.h" +#include "zone.h" #include #include #include From fe2a0f5089ebfc5c03db783a1f85b1c7c217128a Mon Sep 17 00:00:00 2001 From: frsyuki Date: Fri, 27 Aug 2010 17:42:05 +0900 Subject: [PATCH 139/152] cpp: adds fixed length serialization for integers --- cpp/src/Makefile.am | 3 +- cpp/src/msgpack/pack.h | 12 +++++ cpp/src/msgpack/pack.hpp | 56 +++++++++++++++++++++ cpp/src/msgpack/type.hpp | 3 +- cpp/src/msgpack/type/fixint.hpp | 89 +++++++++++++++++++++++++++++++++ cpp/test/Makefile.am | 6 +++ cpp/test/fixint.cc | 24 +++++++++ cpp/test/fixint_c.cc | 32 ++++++++++++ 8 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 cpp/src/msgpack/type/fixint.hpp create mode 100644 cpp/test/fixint.cc create mode 100644 cpp/test/fixint_c.cc diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am index 31096f0..0979d23 100644 --- a/cpp/src/Makefile.am +++ b/cpp/src/Makefile.am @@ -58,10 +58,11 @@ nobase_include_HEADERS += \ 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/deque.hpp \ msgpack/type/map.hpp \ msgpack/type/nil.hpp \ msgpack/type/pair.hpp \ diff --git a/cpp/src/msgpack/pack.h b/cpp/src/msgpack/pack.h index 9c4ce59..c156496 100644 --- a/cpp/src/msgpack/pack.h +++ b/cpp/src/msgpack/pack.h @@ -70,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); @@ -99,6 +108,9 @@ 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_fixint(name) \ + inline int msgpack_pack_fix ## name + #define msgpack_pack_user msgpack_packer* #define msgpack_pack_append_buffer(user, buf, len) \ diff --git a/cpp/src/msgpack/pack.hpp b/cpp/src/msgpack/pack.hpp index 2f7dfa0..0090b96 100644 --- a/cpp/src/msgpack/pack.hpp +++ b/cpp/src/msgpack/pack.hpp @@ -45,6 +45,15 @@ public: packer& pack_int32(int32_t d); packer& pack_int64(int64_t d); + packer& pack_fix_uint8(uint8_t d); + packer& pack_fix_uint16(uint16_t d); + packer& pack_fix_uint32(uint32_t d); + packer& pack_fix_uint64(uint64_t d); + packer& pack_fix_int8(int8_t d); + packer& pack_fix_int16(int16_t d); + packer& pack_fix_int32(int32_t d); + packer& pack_fix_int64(int64_t d); + packer& pack_short(short d); packer& pack_int(int d); packer& 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,6 +151,10 @@ inline void pack(Stream& s, const T& v) template \ inline void packer::_pack ## name +#define msgpack_pack_inline_func_fixint(name) \ + template \ + inline void packer::_pack_fix ## name + #define msgpack_pack_user Stream& #define msgpack_pack_append_buffer append_buffer @@ -149,6 +171,7 @@ packer::packer(Stream& s) : m_stream(s) { } template packer::~packer() { } + template inline packer& packer::pack_uint8(uint8_t d) { _pack_uint8(m_stream, d); return *this; } @@ -182,6 +205,39 @@ inline packer& packer::pack_int64(int64_t d) { _pack_int64(m_stream, d); return *this;} +template +inline packer& packer::pack_fix_uint8(uint8_t d) +{ _pack_fix_uint8(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint16(uint16_t d) +{ _pack_fix_uint16(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint32(uint32_t d) +{ _pack_fix_uint32(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_uint64(uint64_t d) +{ _pack_fix_uint64(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int8(int8_t d) +{ _pack_fix_int8(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int16(int16_t d) +{ _pack_fix_int16(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int32(int32_t d) +{ _pack_fix_int32(m_stream, d); return *this; } + +template +inline packer& packer::pack_fix_int64(int64_t d) +{ _pack_fix_int64(m_stream, d); return *this;} + + template inline packer& packer::pack_short(short d) { _pack_short(m_stream, d); return *this; } diff --git a/cpp/src/msgpack/type.hpp b/cpp/src/msgpack/type.hpp index a55c68e..bca69bf 100644 --- a/cpp/src/msgpack/type.hpp +++ b/cpp/src/msgpack/type.hpp @@ -1,8 +1,9 @@ #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/deque.hpp" #include "type/map.hpp" #include "type/nil.hpp" #include "type/pair.hpp" diff --git a/cpp/src/msgpack/type/fixint.hpp b/cpp/src/msgpack/type/fixint.hpp new file mode 100644 index 0000000..da58415 --- /dev/null +++ b/cpp/src/msgpack/type/fixint.hpp @@ -0,0 +1,89 @@ +// +// 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" + +namespace msgpack { + +namespace type { + + +template +struct fix_int { + fix_int() : value(0) { } + fix_int(T value) : value(value) { } + operator T() const { return value; } + T get() const { return value; } +private: + const T value; +}; + + +typedef fix_int fix_uint8; +typedef fix_int fix_uint16; +typedef fix_int fix_uint32; +typedef fix_int fix_uint64; + +typedef fix_int fix_int8; +typedef fix_int fix_int16; +typedef fix_int fix_int32; +typedef fix_int fix_int64; + + +} // namespace type + + +template +inline packer& operator<< (packer& o, const type::fix_int8& v) + { o.pack_fix_int8(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int16& v) + { o.pack_fix_int16(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int32& v) + { o.pack_fix_int32(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_int64& v) + { o.pack_fix_int64(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint8& v) + { o.pack_fix_uint8(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint16& v) + { o.pack_fix_uint16(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint32& v) + { o.pack_fix_uint32(v); return o; } + +template +inline packer& operator<< (packer& o, const type::fix_uint64& v) + { o.pack_fix_uint64(v); return o; } + + +} // namespace msgpack + +#endif /* msgpack/type/fixint.hpp */ + diff --git a/cpp/test/Makefile.am b/cpp/test/Makefile.am index bb9439e..5225f28 100644 --- a/cpp/test/Makefile.am +++ b/cpp/test/Makefile.am @@ -13,6 +13,8 @@ check_PROGRAMS = \ convert \ buffer \ cases \ + fixint \ + fixint_c \ version \ msgpackc_test \ msgpack_test @@ -38,6 +40,10 @@ 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 diff --git a/cpp/test/fixint.cc b/cpp/test/fixint.cc new file mode 100644 index 0000000..64a39ac --- /dev/null +++ b/cpp/test/fixint.cc @@ -0,0 +1,24 @@ +#include +#include + +template +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(2); + check_size(3); + check_size(5); + check_size(9); + + check_size(2); + check_size(3); + check_size(5); + check_size(9); +} + diff --git a/cpp/test/fixint_c.cc b/cpp/test/fixint_c.cc new file mode 100644 index 0000000..caa4d26 --- /dev/null +++ b/cpp/test/fixint_c.cc @@ -0,0 +1,32 @@ +#include +#include + +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); +} + From 2c7573a032b7aa3b0588bace376d3c24b53fbc2d Mon Sep 17 00:00:00 2001 From: frsyuki Date: Fri, 27 Aug 2010 17:53:02 +0900 Subject: [PATCH 140/152] cpp: updates msgpack_vc8.postbuild.bat --- cpp/msgpack_vc8.postbuild.bat | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/msgpack_vc8.postbuild.bat b/cpp/msgpack_vc8.postbuild.bat index 20fabbb..33ff232 100644 --- a/cpp/msgpack_vc8.postbuild.bat +++ b/cpp/msgpack_vc8.postbuild.bat @@ -26,10 +26,11 @@ 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\deque.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\ From 421bee38719ee66d79cd8c376c871678dbb55a1d Mon Sep 17 00:00:00 2001 From: frsyuki Date: Fri, 27 Aug 2010 17:53:19 +0900 Subject: [PATCH 141/152] cpp: version 0.5.3 --- cpp/ChangeLog | 7 +++++++ cpp/configure.in | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cpp/ChangeLog b/cpp/ChangeLog index 504ac4b..2756236 100644 --- a/cpp/ChangeLog +++ b/cpp/ChangeLog @@ -1,4 +1,11 @@ +2010-07-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::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 diff --git a/cpp/configure.in b/cpp/configure.in index 93174da..0104ef9 100644 --- a/cpp/configure.in +++ b/cpp/configure.in @@ -1,6 +1,6 @@ AC_INIT(src/object.cpp) AC_CONFIG_AUX_DIR(ac) -AM_INIT_AUTOMAKE(msgpack, 0.5.2) +AM_INIT_AUTOMAKE(msgpack, 0.5.3) AC_CONFIG_HEADER(config.h) AC_SUBST(CFLAGS) From c87f7cb9ac87525c9531ddf6b2fd16499535967c Mon Sep 17 00:00:00 2001 From: frsyuki Date: Fri, 27 Aug 2010 20:52:40 +0900 Subject: [PATCH 142/152] cpp: fixes fix_int; updates test/fixint.cc --- cpp/src/msgpack/type/fixint.hpp | 85 ++++++++++++++++++++++++++++++++- cpp/test/fixint.cc | 31 ++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/cpp/src/msgpack/type/fixint.hpp b/cpp/src/msgpack/type/fixint.hpp index da58415..ebe2696 100644 --- a/cpp/src/msgpack/type/fixint.hpp +++ b/cpp/src/msgpack/type/fixint.hpp @@ -19,6 +19,7 @@ #define MSGPACK_TYPE_FIXINT_HPP__ #include "msgpack/object.hpp" +#include "msgpack/type/int.hpp" namespace msgpack { @@ -29,10 +30,13 @@ template struct fix_int { fix_int() : value(0) { } fix_int(T value) : value(value) { } + operator T() const { return value; } + T get() const { return value; } + private: - const T value; + T value; }; @@ -50,6 +54,32 @@ typedef fix_int fix_int64; } // namespace type +inline type::fix_int8& operator>> (object o, type::fix_int8& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_int16& operator>> (object o, type::fix_int16& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_int32& operator>> (object o, type::fix_int32& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_int64& operator>> (object o, type::fix_int64& v) + { v = type::detail::convert_integer(o); return v; } + + +inline type::fix_uint8& operator>> (object o, type::fix_uint8& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_uint16& operator>> (object o, type::fix_uint16& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_uint32& operator>> (object o, type::fix_uint32& v) + { v = type::detail::convert_integer(o); return v; } + +inline type::fix_uint64& operator>> (object o, type::fix_uint64& v) + { v = type::detail::convert_integer(o); return v; } + + template inline packer& operator<< (packer& o, const type::fix_int8& v) { o.pack_fix_int8(v); return o; } @@ -66,6 +96,7 @@ template inline packer& operator<< (packer& o, const type::fix_int64& v) { o.pack_fix_int64(v); return o; } + template inline packer& operator<< (packer& o, const type::fix_uint8& v) { o.pack_fix_uint8(v); return o; } @@ -83,6 +114,58 @@ inline packer& operator<< (packer& 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(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_int16 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_int32 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_int64 v) + { static_cast(o) << v; } + + +inline void operator<< (object::with_zone& o, type::fix_uint8 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_uint16 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_uint32 v) + { static_cast(o) << v; } + +inline void operator<< (object::with_zone& o, type::fix_uint64 v) + { static_cast(o) << v; } + + } // namespace msgpack #endif /* msgpack/type/fixint.hpp */ diff --git a/cpp/test/fixint.cc b/cpp/test/fixint.cc index 64a39ac..63288a1 100644 --- a/cpp/test/fixint.cc +++ b/cpp/test/fixint.cc @@ -22,3 +22,34 @@ TEST(fixint, size) check_size(9); } + +template +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(); + check_convert(); + check_convert(); + check_convert(); + + check_convert(); + check_convert(); + check_convert(); + check_convert(); +} + From c42cba1d5453f718a85f49f7441387ed4d0a7210 Mon Sep 17 00:00:00 2001 From: UENISHI Kota Date: Fri, 27 Aug 2010 23:02:16 +0900 Subject: [PATCH 143/152] erlang: fixed bug around error case when serializing atom. --- erlang/msgpack.erl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/erlang/msgpack.erl b/erlang/msgpack.erl index b54874d..8c85458 100644 --- a/erlang/msgpack.erl +++ b/erlang/msgpack.erl @@ -126,7 +126,7 @@ pack_(List) when is_list(List) -> pack_({Map}) when is_list(Map) -> pack_map(Map); pack_(Other) -> - throw({error, {badarg, Other}}). + throw({badarg, Other}). -spec pack_uint_(non_neg_integer()) -> binary(). @@ -387,4 +387,9 @@ benchmark_test()-> {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. From c44c9ab74da304c70f78fbfda7fcad206435ff0e Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 29 Aug 2010 18:23:16 +0900 Subject: [PATCH 144/152] cpp: adds msgpack_vc2008.vcproj file in source package --- cpp/Makefile.am | 4 +++- cpp/{msgpack_vc8.postbuild.bat => msgpack_vc.postbuild.bat} | 0 cpp/msgpack_vc8.vcproj | 4 ++-- cpp/preprocess | 3 +++ 4 files changed, 8 insertions(+), 3 deletions(-) rename cpp/{msgpack_vc8.postbuild.bat => msgpack_vc.postbuild.bat} (100%) diff --git a/cpp/Makefile.am b/cpp/Makefile.am index 7dd4891..ecec1b5 100644 --- a/cpp/Makefile.am +++ b/cpp/Makefile.am @@ -6,7 +6,9 @@ DOC_FILES = \ NOTICE \ msgpack_vc8.vcproj \ msgpack_vc8.sln \ - msgpack_vc8.postbuild.bat + msgpack_vc2008.vcproj \ + msgpack_vc2008.sln \ + msgpack_vc.postbuild.bat EXTRA_DIST = \ $(DOC_FILES) diff --git a/cpp/msgpack_vc8.postbuild.bat b/cpp/msgpack_vc.postbuild.bat similarity index 100% rename from cpp/msgpack_vc8.postbuild.bat rename to cpp/msgpack_vc.postbuild.bat diff --git a/cpp/msgpack_vc8.vcproj b/cpp/msgpack_vc8.vcproj index ed0daa4..72d47b6 100644 --- a/cpp/msgpack_vc8.vcproj +++ b/cpp/msgpack_vc8.vcproj @@ -28,7 +28,7 @@ msgpack_vc2008.vcproj +sed -e 's/9\.00/10.00/' -e 's/msgpack_vc8/msgpack_vc2008/' < msgpack_vc8.sln > msgpack_vc2008.sln + From 3c75361e5a195eba9622927cac7aebe0944f9232 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 29 Aug 2010 18:24:32 +0900 Subject: [PATCH 145/152] cpp: updates README.md --- cpp/README.md | 45 +++++++++---------- .../org/msgpack/TestMessageUnpackable.java | 4 +- msgpack/pack_template.h | 20 ++++----- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/cpp/README.md b/cpp/README.md index 454ce1a..eac7793 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -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,24 +50,24 @@ To use the library in your program, include msgpack.hpp header and link msgpack obj.as(); // 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 -Copyright (C) 2008-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. + Copyright (C) 2008-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. See also NOTICE file. diff --git a/java/src/test/java/org/msgpack/TestMessageUnpackable.java b/java/src/test/java/org/msgpack/TestMessageUnpackable.java index 9099f91..32917c7 100644 --- a/java/src/test/java/org/msgpack/TestMessageUnpackable.java +++ b/java/src/test/java/org/msgpack/TestMessageUnpackable.java @@ -24,9 +24,7 @@ public class TestMessageUnpackable { Image dst = new Image(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker pac = new Unpacker(in); - - dst.messageUnpack(pac); + dst.messageUnpack(new Unpacker(in)); assertEquals(src, dst); } diff --git a/msgpack/pack_template.h b/msgpack/pack_template.h index daa8f81..b636967 100644 --- a/msgpack/pack_template.h +++ b/msgpack/pack_template.h @@ -272,63 +272,63 @@ do { \ } while(0) -#ifdef msgpack_pack_inline_func_fastint +#ifdef msgpack_pack_inline_func_fixint -msgpack_pack_inline_func_fastint(_uint8)(msgpack_pack_user x, uint8_t d) +msgpack_pack_inline_func_fixint(_uint8)(msgpack_pack_user x, uint8_t d) { unsigned char buf[2] = {0xcc, TAKE8_8(d)}; msgpack_pack_append_buffer(x, buf, 2); } -msgpack_pack_inline_func_fastint(_uint16)(msgpack_pack_user x, uint16_t d) +msgpack_pack_inline_func_fixint(_uint16)(msgpack_pack_user x, uint16_t d) { unsigned char buf[3]; buf[0] = 0xcd; _msgpack_store16(&buf[1], d); msgpack_pack_append_buffer(x, buf, 3); } -msgpack_pack_inline_func_fastint(_uint32)(msgpack_pack_user x, uint32_t d) +msgpack_pack_inline_func_fixint(_uint32)(msgpack_pack_user x, uint32_t d) { unsigned char buf[5]; buf[0] = 0xce; _msgpack_store32(&buf[1], d); msgpack_pack_append_buffer(x, buf, 5); } -msgpack_pack_inline_func_fastint(_uint64)(msgpack_pack_user x, uint64_t d) +msgpack_pack_inline_func_fixint(_uint64)(msgpack_pack_user x, uint64_t d) { unsigned char buf[9]; buf[0] = 0xcf; _msgpack_store64(&buf[1], d); msgpack_pack_append_buffer(x, buf, 9); } -msgpack_pack_inline_func_fastint(_int8)(msgpack_pack_user x, int8_t d) +msgpack_pack_inline_func_fixint(_int8)(msgpack_pack_user x, int8_t d) { unsigned char buf[2] = {0xd0, TAKE8_8(d)}; msgpack_pack_append_buffer(x, buf, 2); } -msgpack_pack_inline_func_fastint(_int16)(msgpack_pack_user x, int16_t d) +msgpack_pack_inline_func_fixint(_int16)(msgpack_pack_user x, int16_t d) { unsigned char buf[3]; buf[0] = 0xd1; _msgpack_store16(&buf[1], d); msgpack_pack_append_buffer(x, buf, 3); } -msgpack_pack_inline_func_fastint(_int32)(msgpack_pack_user x, int32_t d) +msgpack_pack_inline_func_fixint(_int32)(msgpack_pack_user x, int32_t d) { unsigned char buf[5]; buf[0] = 0xd2; _msgpack_store32(&buf[1], d); msgpack_pack_append_buffer(x, buf, 5); } -msgpack_pack_inline_func_fastint(_int64)(msgpack_pack_user x, int64_t d) +msgpack_pack_inline_func_fixint(_int64)(msgpack_pack_user x, int64_t d) { unsigned char buf[9]; buf[0] = 0xd3; _msgpack_store64(&buf[1], d); msgpack_pack_append_buffer(x, buf, 9); } -#undef msgpack_pack_inline_func_fastint +#undef msgpack_pack_inline_func_fixint #endif From 9684c8664fe7643274fe546ccbe0009c741ebc72 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sun, 29 Aug 2010 18:27:10 +0900 Subject: [PATCH 146/152] cpp: version 0.5.4 --- cpp/ChangeLog | 7 ++++++- cpp/configure.in | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/cpp/ChangeLog b/cpp/ChangeLog index 2756236..71c7d5b 100644 --- a/cpp/ChangeLog +++ b/cpp/ChangeLog @@ -1,5 +1,10 @@ -2010-07-27 version 0.5.3: +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 diff --git a/cpp/configure.in b/cpp/configure.in index 0104ef9..2dd92d1 100644 --- a/cpp/configure.in +++ b/cpp/configure.in @@ -1,6 +1,6 @@ AC_INIT(src/object.cpp) AC_CONFIG_AUX_DIR(ac) -AM_INIT_AUTOMAKE(msgpack, 0.5.3) +AM_INIT_AUTOMAKE(msgpack, 0.5.4) AC_CONFIG_HEADER(config.h) AC_SUBST(CFLAGS) From a1bd14e516a0baef6f96b441da70e29e5be7d175 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 31 Aug 2010 06:27:15 +0900 Subject: [PATCH 147/152] template: casts integer types explicitly --- msgpack/pack_template.h | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/msgpack/pack_template.h b/msgpack/pack_template.h index b636967..da54c36 100644 --- a/msgpack/pack_template.h +++ b/msgpack/pack_template.h @@ -69,7 +69,7 @@ do { \ } else { \ /* unsigned 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xcd; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } \ } while(0) @@ -89,12 +89,12 @@ do { \ if(d < (1<<16)) { \ /* unsigned 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xcd; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } else { \ /* unsigned 32 */ \ unsigned char buf[5]; \ - buf[0] = 0xce; _msgpack_store32(&buf[1], d); \ + buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ msgpack_pack_append_buffer(x, buf, 5); \ } \ } \ @@ -103,7 +103,7 @@ do { \ #define msgpack_pack_real_uint64(x, d) \ do { \ if(d < (1ULL<<8)) { \ - if(d < (1<<7)) { \ + if(d < (1ULL<<7)) { \ /* fixnum */ \ msgpack_pack_append_buffer(x, &TAKE8_64(d), 1); \ } else { \ @@ -115,12 +115,12 @@ do { \ if(d < (1ULL<<16)) { \ /* unsigned 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xcd; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } else if(d < (1ULL<<32)) { \ /* unsigned 32 */ \ unsigned char buf[5]; \ - buf[0] = 0xce; _msgpack_store32(&buf[1], d); \ + buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ msgpack_pack_append_buffer(x, buf, 5); \ } else { \ /* unsigned 64 */ \ @@ -149,7 +149,7 @@ do { \ if(d < -(1<<7)) { \ /* signed 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xd1; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } else { \ /* signed 8 */ \ @@ -167,7 +167,7 @@ do { \ } else { \ /* unsigned 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xcd; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } \ } \ @@ -179,12 +179,12 @@ do { \ if(d < -(1<<15)) { \ /* signed 32 */ \ unsigned char buf[5]; \ - buf[0] = 0xd2; _msgpack_store32(&buf[1], d); \ + buf[0] = 0xd2; _msgpack_store32(&buf[1], (int32_t)d); \ msgpack_pack_append_buffer(x, buf, 5); \ } else if(d < -(1<<7)) { \ /* signed 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xd1; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } else { \ /* signed 8 */ \ @@ -202,12 +202,12 @@ do { \ } else if(d < (1<<16)) { \ /* unsigned 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xcd; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } else { \ /* unsigned 32 */ \ unsigned char buf[5]; \ - buf[0] = 0xce; _msgpack_store32(&buf[1], d); \ + buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ msgpack_pack_append_buffer(x, buf, 5); \ } \ } \ @@ -225,14 +225,14 @@ do { \ } else { \ /* signed 32 */ \ unsigned char buf[5]; \ - buf[0] = 0xd2; _msgpack_store32(&buf[1], d); \ + buf[0] = 0xd2; _msgpack_store32(&buf[1], (int32_t)d); \ msgpack_pack_append_buffer(x, buf, 5); \ } \ } else { \ if(d < -(1<<7)) { \ /* signed 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xd1; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xd1; _msgpack_store16(&buf[1], (int16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } else { \ /* signed 8 */ \ @@ -252,14 +252,14 @@ do { \ } else { \ /* unsigned 16 */ \ unsigned char buf[3]; \ - buf[0] = 0xcd; _msgpack_store16(&buf[1], d); \ + buf[0] = 0xcd; _msgpack_store16(&buf[1], (uint16_t)d); \ msgpack_pack_append_buffer(x, buf, 3); \ } \ } else { \ if(d < (1LL<<32)) { \ /* unsigned 32 */ \ unsigned char buf[5]; \ - buf[0] = 0xce; _msgpack_store32(&buf[1], d); \ + buf[0] = 0xce; _msgpack_store32(&buf[1], (uint32_t)d); \ msgpack_pack_append_buffer(x, buf, 5); \ } else { \ /* unsigned 64 */ \ @@ -690,11 +690,11 @@ msgpack_pack_inline_func(_array)(msgpack_pack_user x, unsigned int n) msgpack_pack_append_buffer(x, &d, 1); } else if(n < 65536) { unsigned char buf[3]; - buf[0] = 0xdc; _msgpack_store16(&buf[1], n); + buf[0] = 0xdc; _msgpack_store16(&buf[1], (uint16_t)n); msgpack_pack_append_buffer(x, buf, 3); } else { unsigned char buf[5]; - buf[0] = 0xdd; _msgpack_store32(&buf[1], n); + buf[0] = 0xdd; _msgpack_store32(&buf[1], (uint32_t)n); msgpack_pack_append_buffer(x, buf, 5); } } @@ -711,11 +711,11 @@ msgpack_pack_inline_func(_map)(msgpack_pack_user x, unsigned int n) msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); } else if(n < 65536) { unsigned char buf[3]; - buf[0] = 0xde; _msgpack_store16(&buf[1], n); + buf[0] = 0xde; _msgpack_store16(&buf[1], (uint16_t)n); msgpack_pack_append_buffer(x, buf, 3); } else { unsigned char buf[5]; - buf[0] = 0xdf; _msgpack_store32(&buf[1], n); + buf[0] = 0xdf; _msgpack_store32(&buf[1], (uint32_t)n); msgpack_pack_append_buffer(x, buf, 5); } } @@ -732,11 +732,11 @@ msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l) msgpack_pack_append_buffer(x, &TAKE8_8(d), 1); } else if(l < 65536) { unsigned char buf[3]; - buf[0] = 0xda; _msgpack_store16(&buf[1], l); + buf[0] = 0xda; _msgpack_store16(&buf[1], (uint16_t)l); msgpack_pack_append_buffer(x, buf, 3); } else { unsigned char buf[5]; - buf[0] = 0xdb; _msgpack_store32(&buf[1], l); + buf[0] = 0xdb; _msgpack_store32(&buf[1], (uint32_t)l); msgpack_pack_append_buffer(x, buf, 5); } } From b5c78de2ddf82783a6f80a199b68927d1a1747ca Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 31 Aug 2010 06:30:16 +0900 Subject: [PATCH 148/152] ruby: converts encodings into UTF-8 on Ruby 1.9 --- ruby/encoding.h | 33 ++++++++++++++++++ ruby/pack.c | 21 ++++++++++-- ruby/rbinit.c | 15 +++++++++ ruby/test/test_encoding.rb | 68 ++++++++++++++++++++++++++++++++++++++ ruby/test/test_helper.rb | 4 ++- ruby/unpack.c | 38 +++------------------ 6 files changed, 141 insertions(+), 38 deletions(-) create mode 100644 ruby/encoding.h create mode 100644 ruby/test/test_encoding.rb diff --git a/ruby/encoding.h b/ruby/encoding.h new file mode 100644 index 0000000..2ad3fd7 --- /dev/null +++ b/ruby/encoding.h @@ -0,0 +1,33 @@ +/* + * MessagePack for Ruby + * + * Copyright (C) 2008-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. + */ +#ifndef ENCODING_H__ +#define ENCODING_H__ + + +#ifdef HAVE_RUBY_ENCODING_H +#include "ruby/encoding.h" +#define MSGPACK_RUBY_ENCODING +extern int s_enc_utf8; +extern int s_enc_ascii8bit; +extern int s_enc_usascii; +extern VALUE s_enc_utf8_value; +#endif + + +#endif /* encoding.h */ + diff --git a/ruby/pack.c b/ruby/pack.c index bbeac4a..35878c7 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -16,6 +16,8 @@ * limitations under the License. */ #include "ruby.h" +#include "encoding.h" + #include "msgpack/pack_define.h" static ID s_to_msgpack; @@ -131,7 +133,6 @@ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - // FIXME bignum if(RBIGNUM_SIGN(self)) { // positive msgpack_pack_uint64(out, rb_big2ull(self)); } else { // negative @@ -168,6 +169,14 @@ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); +#ifdef MSGPACK_RUBY_ENCODING + int enc = ENCODING_GET(self); + if(enc != s_enc_utf8 && enc != s_enc_ascii8bit && enc != s_enc_usascii) { + if(!ENC_CODERANGE_ASCIIONLY(self)) { + self = rb_str_encode(self, s_enc_utf8_value, 0, Qnil); + } + } +#endif msgpack_pack_raw(out, RSTRING_LEN(self)); msgpack_pack_raw_body(out, RSTRING_PTR(self), RSTRING_LEN(self)); return out; @@ -184,12 +193,16 @@ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { +#ifdef MSGPACK_RUBY_ENCODING + return MessagePack_String_to_msgpack(argc, argv, rb_id2str(SYM2ID(self))); +#else ARG_BUFFER(out, argc, argv); const char* name = rb_id2name(SYM2ID(self)); size_t len = strlen(name); msgpack_pack_raw(out, len); msgpack_pack_raw_body(out, name, len); return out; +#endif } @@ -205,7 +218,8 @@ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) static VALUE MessagePack_Array_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - msgpack_pack_array(out, RARRAY_LEN(self)); + // FIXME check sizeof(long) > sizeof(unsigned int) && RARRAY_LEN(self) > UINT_MAX + msgpack_pack_array(out, (unsigned int)RARRAY_LEN(self)); VALUE* p = RARRAY_PTR(self); VALUE* const pend = p + RARRAY_LEN(self); for(;p != pend; ++p) { @@ -239,7 +253,8 @@ static int MessagePack_Hash_to_msgpack_foreach(VALUE key, VALUE value, VALUE out static VALUE MessagePack_Hash_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - msgpack_pack_map(out, RHASH_SIZE(self)); + // FIXME check sizeof(st_index_t) > sizeof(unsigned int) && RARRAY_LEN(self) > UINT_MAX + msgpack_pack_map(out, (unsigned int)RHASH_SIZE(self)); rb_hash_foreach(self, MessagePack_Hash_to_msgpack_foreach, out); return out; } diff --git a/ruby/rbinit.c b/ruby/rbinit.c index 28a8bfe..4678159 100644 --- a/ruby/rbinit.c +++ b/ruby/rbinit.c @@ -17,9 +17,17 @@ */ #include "pack.h" #include "unpack.h" +#include "encoding.h" static VALUE mMessagePack; +#ifdef MSGPACK_RUBY_ENCODING +int s_enc_utf8; +int s_enc_ascii8bit; +int s_enc_usascii; +VALUE s_enc_utf8_value; +#endif + /** * Document-module: MessagePack * @@ -46,6 +54,13 @@ void Init_msgpack(void) rb_define_const(mMessagePack, "VERSION", rb_str_new2(MESSAGEPACK_VERSION)); +#ifdef MSGPACK_RUBY_ENCODING + s_enc_ascii8bit = rb_ascii8bit_encindex(); + s_enc_utf8 = rb_utf8_encindex(); + s_enc_usascii = rb_usascii_encindex(); + s_enc_utf8_value = rb_enc_from_encoding(rb_utf8_encoding()); +#endif + Init_msgpack_unpack(mMessagePack); Init_msgpack_pack(mMessagePack); } diff --git a/ruby/test/test_encoding.rb b/ruby/test/test_encoding.rb new file mode 100644 index 0000000..2cf0767 --- /dev/null +++ b/ruby/test/test_encoding.rb @@ -0,0 +1,68 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__)+'/test_helper' + +if RUBY_VERSION < "1.9" + exit +end + +class MessagePackTestEncoding < Test::Unit::TestCase + def self.it(name, &block) + define_method("test_#{name}", &block) + end + + it "US-ASCII" do + check_unpack "abc".force_encoding("US-ASCII") + end + + it "UTF-8 ascii" do + check_unpack "abc".force_encoding("UTF-8") + end + + it "UTF-8 mbstr" do + check_unpack "\xE3\x81\x82".force_encoding("UTF-8") + end + + it "UTF-8 invalid" do + check_unpack "\xD0".force_encoding("UTF-8") + end + + it "ASCII-8BIT" do + check_unpack "\xD0".force_encoding("ASCII-8BIT") + end + + it "EUC-JP" do + x = "\xA4\xA2".force_encoding("EUC-JP") + check_unpack(x) + end + + it "EUC-JP invalid" do + begin + "\xD0".force_encoding("EUC-JP").to_msgpack + assert(false) + rescue Encoding::InvalidByteSequenceError + assert(true) + end + end + + private + def check_unpack(str) + if str.encoding.to_s == "ASCII-8BIT" + should_str = str.dup.force_encoding("UTF-8") + else + should_str = str.encode("UTF-8") + end + + raw = str.to_msgpack + r = MessagePack.unpack(str.to_msgpack) + assert_equal(r.encoding.to_s, "UTF-8") + assert_equal(r, should_str.force_encoding("UTF-8")) + + if str.valid_encoding? + sym = str.to_sym + r = MessagePack.unpack(sym.to_msgpack) + assert_equal(r.encoding.to_s, "UTF-8") + assert_equal(r, should_str.force_encoding("UTF-8")) + end + end +end + diff --git a/ruby/test/test_helper.rb b/ruby/test/test_helper.rb index 80d7806..4def861 100644 --- a/ruby/test/test_helper.rb +++ b/ruby/test/test_helper.rb @@ -5,4 +5,6 @@ rescue LoadError require File.dirname(__FILE__) + '/../lib/msgpack' end -#GC.stress = true +if ENV["GC_STRESS"] + GC.stress = true +end diff --git a/ruby/unpack.c b/ruby/unpack.c index 0948151..3c5e350 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -16,17 +16,13 @@ * limitations under the License. */ #include "ruby.h" +#include "encoding.h" #include "msgpack/unpack_define.h" static ID s_sysread; static ID s_readpartial; -#ifdef HAVE_RUBY_ENCODING_H -#include "ruby/encoding.h" -int s_ascii_8bit; -#endif - struct unpack_buffer { size_t size; size_t free; @@ -136,6 +132,9 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha } else { *o = rb_str_substr(u->source, p - b, l); } +#ifdef MSGPACK_RUBY_ENCODING + ENCODING_SET(*o, s_enc_utf8); +#endif return 0; } @@ -163,16 +162,6 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha #endif -#ifdef HAVE_RUBY_ENCODING_H -static VALUE template_execute_rescue_enc(VALUE data) -{ - rb_gc_enable(); - VALUE* resc = (VALUE*)data; - rb_enc_set_index(resc[0], (int)resc[1]); - RERAISE; -} -#endif - static VALUE template_execute_rescue(VALUE nouse) { rb_gc_enable(); @@ -203,31 +192,16 @@ static int template_execute_wrap(msgpack_unpack_t* mp, (VALUE)from, }; -#ifdef HAVE_RUBY_ENCODING_H - int enc_orig = rb_enc_get_index(str); - rb_enc_set_index(str, s_ascii_8bit); -#endif - // FIXME execute実行中はmp->topが更新されないのでGC markが機能しない rb_gc_disable(); mp->user.source = str; -#ifdef HAVE_RUBY_ENCODING_H - VALUE resc[2] = {str, enc_orig}; - int ret = (int)rb_rescue(template_execute_do, (VALUE)args, - template_execute_rescue_enc, (VALUE)resc); -#else int ret = (int)rb_rescue(template_execute_do, (VALUE)args, template_execute_rescue, Qnil); -#endif rb_gc_enable(); -#ifdef HAVE_RUBY_ENCODING_H - rb_enc_set_index(str, enc_orig); -#endif - return ret; } @@ -746,10 +720,6 @@ void Init_msgpack_unpack(VALUE mMessagePack) s_sysread = rb_intern("sysread"); s_readpartial = rb_intern("readpartial"); -#ifdef HAVE_RUBY_ENCODING_H - s_ascii_8bit = rb_enc_find_index("ASCII-8BIT"); -#endif - eUnpackError = rb_define_class_under(mMessagePack, "UnpackError", rb_eStandardError); cUnpacker = rb_define_class_under(mMessagePack, "Unpacker", rb_cObject); rb_define_alloc_func(cUnpacker, MessagePack_Unpacker_alloc); From 09b47cc536ebd951c231fd5e09b4382a25b98020 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 31 Aug 2010 07:00:19 +0900 Subject: [PATCH 149/152] ruby: fixes compatibility with ruby-1.8.5 --- ruby/{encoding.h => compat.h} | 35 +++++++++++++++++++++++++++++++---- ruby/pack.c | 6 +++--- ruby/rbinit.c | 6 +++--- ruby/test/test_pack_unpack.rb | 12 ++++++++---- ruby/unpack.c | 12 +++--------- 5 files changed, 48 insertions(+), 23 deletions(-) rename ruby/{encoding.h => compat.h} (60%) diff --git a/ruby/encoding.h b/ruby/compat.h similarity index 60% rename from ruby/encoding.h rename to ruby/compat.h index 2ad3fd7..98c8881 100644 --- a/ruby/encoding.h +++ b/ruby/compat.h @@ -15,19 +15,46 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef ENCODING_H__ -#define ENCODING_H__ +#ifndef COMPAT_H__ +#define COMPAT_H__ #ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" -#define MSGPACK_RUBY_ENCODING +#define COMPAT_HAVE_ENCODING extern int s_enc_utf8; extern int s_enc_ascii8bit; extern int s_enc_usascii; extern VALUE s_enc_utf8_value; #endif +#ifdef RUBY_VM +#define COMPAT_RERAISE rb_exc_raise(rb_errinfo()) +#else +#define COMPAT_RERAISE rb_exc_raise(ruby_errinfo) +#endif -#endif /* encoding.h */ + +/* ruby 1.8.5 */ +#ifndef RSTRING_PTR +#define RSTRING_PTR(s) (RSTRING(s)->ptr) +#endif + +/* ruby 1.8.5 */ +#ifndef RSTRING_LEN +#define RSTRING_LEN(s) (RSTRING(s)->len) +#endif + +/* ruby 1.8.5 */ +#ifndef RARRAY_PTR +#define RARRAY_PTR(s) (RARRAY(s)->ptr) +#endif + +/* ruby 1.8.5 */ +#ifndef RARRAY_LEN +#define RARRAY_LEN(s) (RARRAY(s)->len) +#endif + + +#endif /* compat.h */ diff --git a/ruby/pack.c b/ruby/pack.c index 35878c7..49b69fc 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -16,7 +16,7 @@ * limitations under the License. */ #include "ruby.h" -#include "encoding.h" +#include "compat.h" #include "msgpack/pack_define.h" @@ -169,7 +169,7 @@ static VALUE MessagePack_Float_to_msgpack(int argc, VALUE *argv, VALUE self) static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING int enc = ENCODING_GET(self); if(enc != s_enc_utf8 && enc != s_enc_ascii8bit && enc != s_enc_usascii) { if(!ENC_CODERANGE_ASCIIONLY(self)) { @@ -193,7 +193,7 @@ static VALUE MessagePack_String_to_msgpack(int argc, VALUE *argv, VALUE self) */ static VALUE MessagePack_Symbol_to_msgpack(int argc, VALUE *argv, VALUE self) { -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING return MessagePack_String_to_msgpack(argc, argv, rb_id2str(SYM2ID(self))); #else ARG_BUFFER(out, argc, argv); diff --git a/ruby/rbinit.c b/ruby/rbinit.c index 4678159..1d1cbc6 100644 --- a/ruby/rbinit.c +++ b/ruby/rbinit.c @@ -17,11 +17,11 @@ */ #include "pack.h" #include "unpack.h" -#include "encoding.h" +#include "compat.h" static VALUE mMessagePack; -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING int s_enc_utf8; int s_enc_ascii8bit; int s_enc_usascii; @@ -54,7 +54,7 @@ void Init_msgpack(void) rb_define_const(mMessagePack, "VERSION", rb_str_new2(MESSAGEPACK_VERSION)); -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING s_enc_ascii8bit = rb_ascii8bit_encindex(); s_enc_utf8 = rb_utf8_encindex(); s_enc_usascii = rb_usascii_encindex(); diff --git a/ruby/test/test_pack_unpack.rb b/ruby/test/test_pack_unpack.rb index 25bde81..545e593 100644 --- a/ruby/test/test_pack_unpack.rb +++ b/ruby/test/test_pack_unpack.rb @@ -153,7 +153,8 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase end it "{1=>1}" do - match ({1=>1}), "\x81\x01\x01" + obj = {1=>1} + match obj, "\x81\x01\x01" end it "1.0" do @@ -165,15 +166,18 @@ class MessagePackTestPackUnpack < Test::Unit::TestCase end it "[0, 1, ..., 14]" do - match (0..14).to_a, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" + obj = (0..14).to_a + match obj, "\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" end it "[0, 1, ..., 15]" do - match (0..15).to_a, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + obj = (0..15).to_a + match obj, "\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" end it "{}" do - match ({}), "\x80" + obj = {} + match obj, "\x80" end ## FIXME diff --git a/ruby/unpack.c b/ruby/unpack.c index 3c5e350..2d10e75 100644 --- a/ruby/unpack.c +++ b/ruby/unpack.c @@ -16,7 +16,7 @@ * limitations under the License. */ #include "ruby.h" -#include "encoding.h" +#include "compat.h" #include "msgpack/unpack_define.h" @@ -132,7 +132,7 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha } else { *o = rb_str_substr(u->source, p - b, l); } -#ifdef MSGPACK_RUBY_ENCODING +#ifdef COMPAT_HAVE_ENCODING ENCODING_SET(*o, s_enc_utf8); #endif return 0; @@ -155,17 +155,11 @@ static inline int template_callback_raw(unpack_user* u, const char* b, const cha rb_raise(rb_eTypeError, "instance of String needed"); \ } -#ifdef RUBY_VM -#define RERAISE rb_exc_raise(rb_errinfo()) -#else -#define RERAISE rb_exc_raise(ruby_errinfo) -#endif - static VALUE template_execute_rescue(VALUE nouse) { rb_gc_enable(); - RERAISE; + COMPAT_RERAISE; } static VALUE template_execute_do(VALUE argv) From 71a1cb01842787e2fa897f023addd88337542915 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Tue, 31 Aug 2010 09:29:01 +0900 Subject: [PATCH 150/152] fixes compatibility with Rubinius --- ruby/compat.h | 17 +++++++++++++++++ ruby/extconf.rb | 2 +- ruby/pack.c | 8 ++------ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ruby/compat.h b/ruby/compat.h index 98c8881..d7a2ca7 100644 --- a/ruby/compat.h +++ b/ruby/compat.h @@ -35,6 +35,23 @@ extern VALUE s_enc_utf8_value; #endif +/* ruby 1.8 and Rubinius */ +#ifndef RBIGNUM_POSITIVE_P +# ifdef RUBINIUS +# define RBIGNUM_POSITIVE_P(b) (rb_funcall(b, rb_intern(">="), 1, INT2FIX(0)) == Qtrue) +# else +# define RBIGNUM_POSITIVE_P(b) (RBIGNUM(b)->sign) +# endif +#endif + + +/* Rubinius */ +#ifdef RUBINIUS +static inline void rb_gc_enable() { return; } +static inline void rb_gc_disable() { return; } +#endif + + /* ruby 1.8.5 */ #ifndef RSTRING_PTR #define RSTRING_PTR(s) (RSTRING(s)->ptr) diff --git a/ruby/extconf.rb b/ruby/extconf.rb index eb6a389..f1d44ec 100644 --- a/ruby/extconf.rb +++ b/ruby/extconf.rb @@ -1,5 +1,5 @@ require 'mkmf' require './version.rb' -$CFLAGS << %[ -I.. -Wall -O4 -DMESSAGEPACK_VERSION=\\"#{MessagePack::VERSION}\\"] +$CFLAGS << %[ -I.. -Wall -O4 -DMESSAGEPACK_VERSION=\\"#{MessagePack::VERSION}\\" -g] create_makefile('msgpack') diff --git a/ruby/pack.c b/ruby/pack.c index 49b69fc..8ce46aa 100644 --- a/ruby/pack.c +++ b/ruby/pack.c @@ -118,10 +118,6 @@ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) } -#ifndef RBIGNUM_SIGN // Ruby 1.8 -#define RBIGNUM_SIGN(b) (RBIGNUM(b)->sign) -#endif - /* * Document-method: Bignum#to_msgpack * @@ -133,9 +129,9 @@ static VALUE MessagePack_Fixnum_to_msgpack(int argc, VALUE *argv, VALUE self) static VALUE MessagePack_Bignum_to_msgpack(int argc, VALUE *argv, VALUE self) { ARG_BUFFER(out, argc, argv); - if(RBIGNUM_SIGN(self)) { // positive + if(RBIGNUM_POSITIVE_P(self)) { msgpack_pack_uint64(out, rb_big2ull(self)); - } else { // negative + } else { msgpack_pack_int64(out, rb_big2ll(self)); } return out; From 23a7137e6a6d7f2910fef2dde305f45f83416194 Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Tue, 31 Aug 2010 23:42:32 +0900 Subject: [PATCH 151/152] Perl: better argument validation(patch from dankogai) --- perl/pack.c | 4 ++-- perl/t/08_cycle.t | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/perl/pack.c b/perl/pack.c index af6669c..93b2e2f 100644 --- a/perl/pack.c +++ b/perl/pack.c @@ -151,7 +151,7 @@ static int try_int(enc_t* enc, const char *p, size_t len) { static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth); static void _msgpack_pack_sv(enc_t *enc, SV* sv, int depth) { - if (!depth) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); + if (depth <= 0) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); if (sv==NULL) { @@ -187,7 +187,7 @@ static void _msgpack_pack_sv(enc_t *enc, SV* sv, int depth) { static void _msgpack_pack_rv(enc_t *enc, SV* sv, int depth) { svtype svt; - if (!depth) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); + if (depth <= 0) Perl_croak(aTHX_ ERR_NESTING_EXCEEDED); SvGETMAGIC(sv); svt = SvTYPE(sv); diff --git a/perl/t/08_cycle.t b/perl/t/08_cycle.t index 55d8427..2bd66c1 100644 --- a/perl/t/08_cycle.t +++ b/perl/t/08_cycle.t @@ -2,7 +2,7 @@ use t::Util; use Test::More; use Data::MessagePack; -plan tests => 5; +plan tests => 6; my $aref = [0]; $aref->[1] = $aref; @@ -23,3 +23,6 @@ ok !$@; eval { Data::MessagePack->pack($aref, 2) }; ok $@, $@; + +eval { Data::MessagePack->pack($aref, -1) }; +ok $@, $@; From 558e9c21edf3cee5813aaa0e7797509eec5d43fb Mon Sep 17 00:00:00 2001 From: tokuhirom Date: Wed, 1 Sep 2010 08:19:05 +0900 Subject: [PATCH 152/152] Perl: 0.15 --- perl/Changes | 5 +++++ perl/lib/Data/MessagePack.pm | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/perl/Changes b/perl/Changes index 13fc98b..189990a 100644 --- a/perl/Changes +++ b/perl/Changes @@ -1,3 +1,8 @@ +0.15 + + - better argument validation. + (Dan Kogai) + 0.14 - fixed segv on serializing cyclic reference diff --git a/perl/lib/Data/MessagePack.pm b/perl/lib/Data/MessagePack.pm index 3c38a79..276353a 100644 --- a/perl/lib/Data/MessagePack.pm +++ b/perl/lib/Data/MessagePack.pm @@ -4,7 +4,7 @@ use warnings; use XSLoader; use 5.008001; -our $VERSION = '0.14'; +our $VERSION = '0.15'; our $PreferInteger = 0; our $true = do { bless \(my $dummy = 1), "Data::MessagePack::Boolean" };