ed-precompiled_msgpack 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ChangeLog +368 -0
- data/LICENSE +177 -0
- data/README.md +302 -0
- data/ext/java/org/msgpack/jruby/Buffer.java +233 -0
- data/ext/java/org/msgpack/jruby/Decoder.java +307 -0
- data/ext/java/org/msgpack/jruby/Encoder.java +456 -0
- data/ext/java/org/msgpack/jruby/ExtensionRegistry.java +167 -0
- data/ext/java/org/msgpack/jruby/ExtensionValue.java +128 -0
- data/ext/java/org/msgpack/jruby/Factory.java +130 -0
- data/ext/java/org/msgpack/jruby/MessagePackLibrary.java +45 -0
- data/ext/java/org/msgpack/jruby/Packer.java +266 -0
- data/ext/java/org/msgpack/jruby/Types.java +37 -0
- data/ext/java/org/msgpack/jruby/Unpacker.java +336 -0
- data/ext/msgpack/buffer.c +669 -0
- data/ext/msgpack/buffer.h +604 -0
- data/ext/msgpack/buffer_class.c +616 -0
- data/ext/msgpack/buffer_class.h +33 -0
- data/ext/msgpack/compat.h +26 -0
- data/ext/msgpack/extconf.rb +53 -0
- data/ext/msgpack/extension_value_class.c +34 -0
- data/ext/msgpack/extension_value_class.h +31 -0
- data/ext/msgpack/factory_class.c +276 -0
- data/ext/msgpack/factory_class.h +33 -0
- data/ext/msgpack/packer.c +199 -0
- data/ext/msgpack/packer.h +513 -0
- data/ext/msgpack/packer_class.c +442 -0
- data/ext/msgpack/packer_class.h +43 -0
- data/ext/msgpack/packer_ext_registry.c +74 -0
- data/ext/msgpack/packer_ext_registry.h +140 -0
- data/ext/msgpack/rbinit.c +35 -0
- data/ext/msgpack/rmem.c +93 -0
- data/ext/msgpack/rmem.h +109 -0
- data/ext/msgpack/sysdep.h +118 -0
- data/ext/msgpack/sysdep_endian.h +50 -0
- data/ext/msgpack/sysdep_types.h +46 -0
- data/ext/msgpack/unpacker.c +986 -0
- data/ext/msgpack/unpacker.h +152 -0
- data/ext/msgpack/unpacker_class.c +447 -0
- data/ext/msgpack/unpacker_class.h +43 -0
- data/ext/msgpack/unpacker_ext_registry.c +74 -0
- data/ext/msgpack/unpacker_ext_registry.h +62 -0
- data/lib/msgpack/bigint.rb +69 -0
- data/lib/msgpack/buffer.rb +9 -0
- data/lib/msgpack/core_ext.rb +139 -0
- data/lib/msgpack/factory.rb +211 -0
- data/lib/msgpack/packer.rb +37 -0
- data/lib/msgpack/symbol.rb +26 -0
- data/lib/msgpack/time.rb +29 -0
- data/lib/msgpack/timestamp.rb +76 -0
- data/lib/msgpack/unpacker.rb +41 -0
- data/lib/msgpack/version.rb +6 -0
- data/lib/msgpack.rb +53 -0
- data/msgpack.gemspec +41 -0
- metadata +216 -0
@@ -0,0 +1,307 @@
|
|
1
|
+
package org.msgpack.jruby;
|
2
|
+
|
3
|
+
|
4
|
+
import java.math.BigInteger;
|
5
|
+
import java.nio.ByteBuffer;
|
6
|
+
import java.nio.BufferUnderflowException;
|
7
|
+
import java.util.Iterator;
|
8
|
+
import java.util.Arrays;
|
9
|
+
|
10
|
+
import org.jruby.Ruby;
|
11
|
+
import org.jruby.RubyObject;
|
12
|
+
import org.jruby.RubyClass;
|
13
|
+
import org.jruby.RubyBignum;
|
14
|
+
import org.jruby.RubyString;
|
15
|
+
import org.jruby.RubyArray;
|
16
|
+
import org.jruby.RubyHash;
|
17
|
+
import org.jruby.RubyInteger;
|
18
|
+
import org.jruby.exceptions.RaiseException;
|
19
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
20
|
+
import org.jruby.util.ByteList;
|
21
|
+
|
22
|
+
import org.jcodings.Encoding;
|
23
|
+
import org.jcodings.specific.UTF8Encoding;
|
24
|
+
|
25
|
+
import static org.msgpack.jruby.Types.*;
|
26
|
+
|
27
|
+
|
28
|
+
public class Decoder implements Iterator<IRubyObject> {
|
29
|
+
private final Ruby runtime;
|
30
|
+
private final Encoding binaryEncoding;
|
31
|
+
private final Encoding utf8Encoding;
|
32
|
+
private final RubyClass unpackErrorClass;
|
33
|
+
private final RubyClass underflowErrorClass;
|
34
|
+
private final RubyClass malformedFormatErrorClass;
|
35
|
+
private final RubyClass stackErrorClass;
|
36
|
+
private final RubyClass unexpectedTypeErrorClass;
|
37
|
+
private final RubyClass unknownExtTypeErrorClass;
|
38
|
+
|
39
|
+
private Unpacker unpacker;
|
40
|
+
private ByteBuffer buffer;
|
41
|
+
private boolean symbolizeKeys;
|
42
|
+
private boolean freeze;
|
43
|
+
private boolean allowUnknownExt;
|
44
|
+
|
45
|
+
public Decoder(Ruby runtime) {
|
46
|
+
this(runtime, null, new byte[] {}, 0, 0, false, false, false);
|
47
|
+
}
|
48
|
+
|
49
|
+
public Decoder(Ruby runtime, Unpacker unpacker) {
|
50
|
+
this(runtime, unpacker, new byte[] {}, 0, 0, false, false, false);
|
51
|
+
}
|
52
|
+
|
53
|
+
public Decoder(Ruby runtime, byte[] bytes) {
|
54
|
+
this(runtime, null, bytes, 0, bytes.length, false, false, false);
|
55
|
+
}
|
56
|
+
|
57
|
+
public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes) {
|
58
|
+
this(runtime, unpacker, bytes, 0, bytes.length, false, false, false);
|
59
|
+
}
|
60
|
+
|
61
|
+
public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
|
62
|
+
this(runtime, unpacker, bytes, 0, bytes.length, symbolizeKeys, freeze, allowUnknownExt);
|
63
|
+
}
|
64
|
+
|
65
|
+
public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, int offset, int length) {
|
66
|
+
this(runtime, unpacker, bytes, offset, length, false, false, false);
|
67
|
+
}
|
68
|
+
|
69
|
+
public Decoder(Ruby runtime, Unpacker unpacker, byte[] bytes, int offset, int length, boolean symbolizeKeys, boolean freeze, boolean allowUnknownExt) {
|
70
|
+
this.runtime = runtime;
|
71
|
+
this.unpacker = unpacker;
|
72
|
+
this.symbolizeKeys = symbolizeKeys;
|
73
|
+
this.freeze = freeze;
|
74
|
+
this.allowUnknownExt = allowUnknownExt;
|
75
|
+
this.binaryEncoding = runtime.getEncodingService().getAscii8bitEncoding();
|
76
|
+
this.utf8Encoding = UTF8Encoding.INSTANCE;
|
77
|
+
this.unpackErrorClass = runtime.getModule("MessagePack").getClass("UnpackError");
|
78
|
+
this.underflowErrorClass = runtime.getModule("MessagePack").getClass("UnderflowError");
|
79
|
+
this.malformedFormatErrorClass = runtime.getModule("MessagePack").getClass("MalformedFormatError");
|
80
|
+
this.stackErrorClass = runtime.getModule("MessagePack").getClass("StackError");
|
81
|
+
this.unexpectedTypeErrorClass = runtime.getModule("MessagePack").getClass("UnexpectedTypeError");
|
82
|
+
this.unknownExtTypeErrorClass = runtime.getModule("MessagePack").getClass("UnknownExtTypeError");
|
83
|
+
this.symbolizeKeys = symbolizeKeys;
|
84
|
+
this.allowUnknownExt = allowUnknownExt;
|
85
|
+
feed(bytes, offset, length);
|
86
|
+
}
|
87
|
+
|
88
|
+
public void feed(byte[] bytes) {
|
89
|
+
feed(bytes, 0, bytes.length);
|
90
|
+
}
|
91
|
+
|
92
|
+
public void feed(byte[] bytes, int offset, int length) {
|
93
|
+
if (buffer == null) {
|
94
|
+
buffer = ByteBuffer.wrap(bytes, offset, length);
|
95
|
+
} else {
|
96
|
+
ByteBuffer newBuffer = ByteBuffer.allocate(buffer.remaining() + length);
|
97
|
+
newBuffer.put(buffer);
|
98
|
+
newBuffer.put(bytes, offset, length);
|
99
|
+
newBuffer.flip();
|
100
|
+
buffer = newBuffer;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
public void reset() {
|
105
|
+
buffer = null;
|
106
|
+
}
|
107
|
+
|
108
|
+
public int offset() {
|
109
|
+
return buffer.position();
|
110
|
+
}
|
111
|
+
|
112
|
+
private IRubyObject consumeUnsignedLong() {
|
113
|
+
long value = buffer.getLong();
|
114
|
+
if (value < 0) {
|
115
|
+
return RubyBignum.newBignum(runtime, BigInteger.valueOf(value & ((1L<<63)-1)).setBit(63));
|
116
|
+
} else {
|
117
|
+
return runtime.newFixnum(value);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
private IRubyObject consumeString(int size, Encoding encoding) {
|
122
|
+
byte[] bytes = readBytes(size);
|
123
|
+
ByteList byteList = new ByteList(bytes, encoding);
|
124
|
+
RubyString string = runtime.newString(byteList);
|
125
|
+
if (this.freeze) {
|
126
|
+
string = runtime.freezeAndDedupString(string);
|
127
|
+
}
|
128
|
+
return string;
|
129
|
+
}
|
130
|
+
|
131
|
+
private IRubyObject consumeArray(int size) {
|
132
|
+
IRubyObject[] elements = new IRubyObject[size];
|
133
|
+
for (int i = 0; i < size; i++) {
|
134
|
+
elements[i] = next();
|
135
|
+
}
|
136
|
+
return runtime.newArray(elements);
|
137
|
+
}
|
138
|
+
|
139
|
+
private IRubyObject consumeHash(int size) {
|
140
|
+
RubyHash hash = RubyHash.newHash(runtime);
|
141
|
+
for (int i = 0; i < size; i++) {
|
142
|
+
IRubyObject key = next();
|
143
|
+
if (key instanceof RubyString) {
|
144
|
+
if (this.symbolizeKeys) {
|
145
|
+
key = ((RubyString) key).intern();
|
146
|
+
} else {
|
147
|
+
key = runtime.freezeAndDedupString((RubyString) key);
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
hash.fastASet(key, next());
|
152
|
+
}
|
153
|
+
return hash;
|
154
|
+
}
|
155
|
+
|
156
|
+
private IRubyObject consumeExtension(int size) {
|
157
|
+
int type = buffer.get();
|
158
|
+
if (unpacker != null) {
|
159
|
+
ExtensionRegistry.ExtensionEntry entry = unpacker.lookupExtensionByTypeId(type);
|
160
|
+
if (entry != null) {
|
161
|
+
IRubyObject proc = entry.getUnpackerProc();
|
162
|
+
if (entry.isRecursive()) {
|
163
|
+
return proc.callMethod(runtime.getCurrentContext(), "call", unpacker);
|
164
|
+
} else {
|
165
|
+
ByteList byteList = new ByteList(readBytes(size), runtime.getEncodingService().getAscii8bitEncoding());
|
166
|
+
return proc.callMethod(runtime.getCurrentContext(), "call", runtime.newString(byteList));
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
if (this.allowUnknownExt) {
|
172
|
+
return ExtensionValue.newExtensionValue(runtime, type, readBytes(size));
|
173
|
+
}
|
174
|
+
|
175
|
+
throw runtime.newRaiseException(unknownExtTypeErrorClass, "unexpected extension type");
|
176
|
+
}
|
177
|
+
|
178
|
+
private byte[] readBytes(int size) {
|
179
|
+
byte[] payload = new byte[size];
|
180
|
+
buffer.get(payload);
|
181
|
+
return payload;
|
182
|
+
}
|
183
|
+
|
184
|
+
@Override
|
185
|
+
public void remove() {
|
186
|
+
throw new UnsupportedOperationException();
|
187
|
+
}
|
188
|
+
|
189
|
+
@Override
|
190
|
+
public boolean hasNext() {
|
191
|
+
return buffer.remaining() > 0;
|
192
|
+
}
|
193
|
+
|
194
|
+
public IRubyObject read_array_header() {
|
195
|
+
int position = buffer.position();
|
196
|
+
try {
|
197
|
+
byte b = buffer.get();
|
198
|
+
if ((b & 0xf0) == 0x90) {
|
199
|
+
return runtime.newFixnum(b & 0x0f);
|
200
|
+
} else if (b == ARY16) {
|
201
|
+
return runtime.newFixnum(buffer.getShort() & 0xffff);
|
202
|
+
} else if (b == ARY32) {
|
203
|
+
return runtime.newFixnum(buffer.getInt());
|
204
|
+
}
|
205
|
+
throw runtime.newRaiseException(unexpectedTypeErrorClass, "unexpected type");
|
206
|
+
} catch (RaiseException re) {
|
207
|
+
buffer.position(position);
|
208
|
+
throw re;
|
209
|
+
} catch (BufferUnderflowException bue) {
|
210
|
+
buffer.position(position);
|
211
|
+
throw runtime.newRaiseException(underflowErrorClass, "Not enough bytes available");
|
212
|
+
}
|
213
|
+
}
|
214
|
+
|
215
|
+
public IRubyObject read_map_header() {
|
216
|
+
int position = buffer.position();
|
217
|
+
try {
|
218
|
+
byte b = buffer.get();
|
219
|
+
if ((b & 0xf0) == 0x80) {
|
220
|
+
return runtime.newFixnum(b & 0x0f);
|
221
|
+
} else if (b == MAP16) {
|
222
|
+
return runtime.newFixnum(buffer.getShort() & 0xffff);
|
223
|
+
} else if (b == MAP32) {
|
224
|
+
return runtime.newFixnum(buffer.getInt());
|
225
|
+
}
|
226
|
+
throw runtime.newRaiseException(unexpectedTypeErrorClass, "unexpected type");
|
227
|
+
} catch (RaiseException re) {
|
228
|
+
buffer.position(position);
|
229
|
+
throw re;
|
230
|
+
} catch (BufferUnderflowException bue) {
|
231
|
+
buffer.position(position);
|
232
|
+
throw runtime.newRaiseException(underflowErrorClass, "Not enough bytes available");
|
233
|
+
}
|
234
|
+
}
|
235
|
+
|
236
|
+
@Override
|
237
|
+
public IRubyObject next() {
|
238
|
+
IRubyObject next = consumeNext();
|
239
|
+
if (freeze) {
|
240
|
+
next.setFrozen(true);
|
241
|
+
}
|
242
|
+
return next;
|
243
|
+
}
|
244
|
+
|
245
|
+
private IRubyObject consumeNext() {
|
246
|
+
int position = buffer.position();
|
247
|
+
try {
|
248
|
+
byte b = buffer.get();
|
249
|
+
outer: switch ((b >> 4) & 0xf) {
|
250
|
+
case 0x8: return consumeHash(b & 0x0f);
|
251
|
+
case 0x9: return consumeArray(b & 0x0f);
|
252
|
+
case 0xa:
|
253
|
+
case 0xb: return consumeString(b & 0x1f, utf8Encoding);
|
254
|
+
case 0xc:
|
255
|
+
switch (b) {
|
256
|
+
case NIL: return runtime.getNil();
|
257
|
+
case FALSE: return runtime.getFalse();
|
258
|
+
case TRUE: return runtime.getTrue();
|
259
|
+
case BIN8: return consumeString(buffer.get() & 0xff, binaryEncoding);
|
260
|
+
case BIN16: return consumeString(buffer.getShort() & 0xffff, binaryEncoding);
|
261
|
+
case BIN32: return consumeString(buffer.getInt(), binaryEncoding);
|
262
|
+
case VAREXT8: return consumeExtension(buffer.get() & 0xff);
|
263
|
+
case VAREXT16: return consumeExtension(buffer.getShort() & 0xffff);
|
264
|
+
case VAREXT32: return consumeExtension(buffer.getInt());
|
265
|
+
case FLOAT32: return runtime.newFloat(buffer.getFloat());
|
266
|
+
case FLOAT64: return runtime.newFloat(buffer.getDouble());
|
267
|
+
case UINT8: return runtime.newFixnum(buffer.get() & 0xffL);
|
268
|
+
case UINT16: return runtime.newFixnum(buffer.getShort() & 0xffffL);
|
269
|
+
case UINT32: return runtime.newFixnum(buffer.getInt() & 0xffffffffL);
|
270
|
+
case UINT64: return consumeUnsignedLong();
|
271
|
+
default: break outer;
|
272
|
+
}
|
273
|
+
case 0xd:
|
274
|
+
switch (b) {
|
275
|
+
case INT8: return runtime.newFixnum(buffer.get());
|
276
|
+
case INT16: return runtime.newFixnum(buffer.getShort());
|
277
|
+
case INT32: return runtime.newFixnum(buffer.getInt());
|
278
|
+
case INT64: return runtime.newFixnum(buffer.getLong());
|
279
|
+
case FIXEXT1: return consumeExtension(1);
|
280
|
+
case FIXEXT2: return consumeExtension(2);
|
281
|
+
case FIXEXT4: return consumeExtension(4);
|
282
|
+
case FIXEXT8: return consumeExtension(8);
|
283
|
+
case FIXEXT16: return consumeExtension(16);
|
284
|
+
case STR8: return consumeString(buffer.get() & 0xff, utf8Encoding);
|
285
|
+
case STR16: return consumeString(buffer.getShort() & 0xffff, utf8Encoding);
|
286
|
+
case STR32: return consumeString(buffer.getInt(), utf8Encoding);
|
287
|
+
case ARY16: return consumeArray(buffer.getShort() & 0xffff);
|
288
|
+
case ARY32: return consumeArray(buffer.getInt());
|
289
|
+
case MAP16: return consumeHash(buffer.getShort() & 0xffff);
|
290
|
+
case MAP32: return consumeHash(buffer.getInt());
|
291
|
+
default: break outer;
|
292
|
+
}
|
293
|
+
case 0xe:
|
294
|
+
case 0xf: return runtime.newFixnum((0x1f & b) - 0x20);
|
295
|
+
default: return runtime.newFixnum(b);
|
296
|
+
}
|
297
|
+
buffer.position(position);
|
298
|
+
throw runtime.newRaiseException(malformedFormatErrorClass, "Illegal byte sequence");
|
299
|
+
} catch (RaiseException re) {
|
300
|
+
buffer.position(position);
|
301
|
+
throw re;
|
302
|
+
} catch (BufferUnderflowException bue) {
|
303
|
+
buffer.position(position);
|
304
|
+
throw runtime.newRaiseException(underflowErrorClass, "Not enough bytes available");
|
305
|
+
}
|
306
|
+
}
|
307
|
+
}
|