|
|
@ -467,7 +467,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
* Encodes the given message object to its serialized form in protocol buffers |
|
|
|
* Encodes the given message object to its serialized form in protocol buffers |
|
|
|
* wire format. |
|
|
|
* wire format. |
|
|
|
* @param options [Hash] options for the encoder |
|
|
|
* @param options [Hash] options for the encoder |
|
|
|
* max_recursion_depth: set to maximum encoding depth for message (default is 64) |
|
|
|
* recursion_limit: set to maximum encoding depth for message (default is 64) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@JRubyMethod(required = 1, optional = 1, meta = true) |
|
|
|
@JRubyMethod(required = 1, optional = 1, meta = true) |
|
|
|
public static IRubyObject encode(ThreadContext context, IRubyObject recv, IRubyObject[] args) { |
|
|
|
public static IRubyObject encode(ThreadContext context, IRubyObject recv, IRubyObject[] args) { |
|
|
@ -475,17 +475,17 @@ public class RubyMessage extends RubyObject { |
|
|
|
throw context.runtime.newArgumentError("Tried to encode a " + args[0].getMetaClass() + " message with " + recv); |
|
|
|
throw context.runtime.newArgumentError("Tried to encode a " + args[0].getMetaClass() + " message with " + recv); |
|
|
|
} |
|
|
|
} |
|
|
|
RubyMessage message = (RubyMessage) args[0]; |
|
|
|
RubyMessage message = (RubyMessage) args[0]; |
|
|
|
int maxRecursionDepthInt = SINK_MAXIMUM_NESTING; |
|
|
|
int recursionLimitInt = SINK_MAXIMUM_NESTING; |
|
|
|
|
|
|
|
|
|
|
|
if (args.length > 1) { |
|
|
|
if (args.length > 1) { |
|
|
|
RubyHash options = (RubyHash) args[1]; |
|
|
|
RubyHash options = (RubyHash) args[1]; |
|
|
|
IRubyObject maxRecursionDepth = options.fastARef(context.runtime.newSymbol("max_recursion_depth")); |
|
|
|
IRubyObject recursionLimit = options.fastARef(context.runtime.newSymbol("recursion_limit")); |
|
|
|
|
|
|
|
|
|
|
|
if (maxRecursionDepth != null) { |
|
|
|
if (recursionLimit != null) { |
|
|
|
maxRecursionDepthInt = ((RubyNumeric) maxRecursionDepth).getIntValue(); |
|
|
|
recursionLimitInt = ((RubyNumeric) recursionLimit).getIntValue(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return context.runtime.newString(new ByteList(message.build(context, 0, maxRecursionDepthInt).toByteArray())); |
|
|
|
return context.runtime.newString(new ByteList(message.build(context, 0, recursionLimitInt).toByteArray())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
@ -496,7 +496,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
* format) under the interpretation given by this message class's definition |
|
|
|
* format) under the interpretation given by this message class's definition |
|
|
|
* and returns a message object with the corresponding field values. |
|
|
|
* and returns a message object with the corresponding field values. |
|
|
|
* @param options [Hash] options for the decoder |
|
|
|
* @param options [Hash] options for the decoder |
|
|
|
* max_recursion_depth: set to maximum decoding depth for message (default is 100) |
|
|
|
* recursion_limit: set to maximum decoding depth for message (default is 100) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@JRubyMethod(required = 1, optional = 1, meta = true) |
|
|
|
@JRubyMethod(required = 1, optional = 1, meta = true) |
|
|
|
public static IRubyObject decode(ThreadContext context, IRubyObject recv, IRubyObject[] args) { |
|
|
|
public static IRubyObject decode(ThreadContext context, IRubyObject recv, IRubyObject[] args) { |
|
|
@ -510,9 +510,9 @@ public class RubyMessage extends RubyObject { |
|
|
|
throw context.runtime.newArgumentError("Expected hash arguments."); |
|
|
|
throw context.runtime.newArgumentError("Expected hash arguments."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
IRubyObject maxRecursionDepth = ((RubyHash) args[1]).fastARef(context.runtime.newSymbol("max_recursion_depth")); |
|
|
|
IRubyObject recursionLimit = ((RubyHash) args[1]).fastARef(context.runtime.newSymbol("recursion_limit")); |
|
|
|
if (maxRecursionDepth != null) { |
|
|
|
if (recursionLimit != null) { |
|
|
|
input.setRecursionLimit(((RubyNumeric) maxRecursionDepth).getIntValue()); |
|
|
|
input.setRecursionLimit(((RubyNumeric) recursionLimit).getIntValue()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -664,9 +664,9 @@ public class RubyMessage extends RubyObject { |
|
|
|
return ret; |
|
|
|
return ret; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected DynamicMessage build(ThreadContext context, int depth, int maxRecursionDepth) { |
|
|
|
protected DynamicMessage build(ThreadContext context, int depth, int recursionLimit) { |
|
|
|
if (depth >= maxRecursionDepth) { |
|
|
|
if (depth >= recursionLimit) { |
|
|
|
throw context.runtime.newRuntimeError("Maximum recursion depth exceeded during encoding."); |
|
|
|
throw context.runtime.newRuntimeError("Recursion limit exceeded during encoding."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Handle the typical case where the fields.keySet contain the fieldDescriptors
|
|
|
|
// Handle the typical case where the fields.keySet contain the fieldDescriptors
|
|
|
@ -676,7 +676,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
if (value instanceof RubyMap) { |
|
|
|
if (value instanceof RubyMap) { |
|
|
|
builder.clearField(fieldDescriptor); |
|
|
|
builder.clearField(fieldDescriptor); |
|
|
|
RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); |
|
|
|
RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); |
|
|
|
for (DynamicMessage kv : ((RubyMap) value).build(context, mapDescriptor, depth, maxRecursionDepth)) { |
|
|
|
for (DynamicMessage kv : ((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) { |
|
|
|
builder.addRepeatedField(fieldDescriptor, kv); |
|
|
|
builder.addRepeatedField(fieldDescriptor, kv); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -685,7 +685,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
|
|
|
|
|
|
|
|
builder.clearField(fieldDescriptor); |
|
|
|
builder.clearField(fieldDescriptor); |
|
|
|
for (int i = 0; i < repeatedField.size(); i++) { |
|
|
|
for (int i = 0; i < repeatedField.size(); i++) { |
|
|
|
Object item = convert(context, fieldDescriptor, repeatedField.get(i), depth, maxRecursionDepth, |
|
|
|
Object item = convert(context, fieldDescriptor, repeatedField.get(i), depth, recursionLimit, |
|
|
|
/*isDefaultValueForBytes*/ false); |
|
|
|
/*isDefaultValueForBytes*/ false); |
|
|
|
builder.addRepeatedField(fieldDescriptor, item); |
|
|
|
builder.addRepeatedField(fieldDescriptor, item); |
|
|
|
} |
|
|
|
} |
|
|
@ -707,7 +707,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
fieldDescriptor.getFullName().equals("google.protobuf.FieldDescriptorProto.default_value")) { |
|
|
|
fieldDescriptor.getFullName().equals("google.protobuf.FieldDescriptorProto.default_value")) { |
|
|
|
isDefaultStringForBytes = true; |
|
|
|
isDefaultStringForBytes = true; |
|
|
|
} |
|
|
|
} |
|
|
|
builder.setField(fieldDescriptor, convert(context, fieldDescriptor, value, depth, maxRecursionDepth, isDefaultStringForBytes)); |
|
|
|
builder.setField(fieldDescriptor, convert(context, fieldDescriptor, value, depth, recursionLimit, isDefaultStringForBytes)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -727,7 +727,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
builder.clearField(fieldDescriptor); |
|
|
|
builder.clearField(fieldDescriptor); |
|
|
|
RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, |
|
|
|
RubyDescriptor mapDescriptor = (RubyDescriptor) getDescriptorForField(context, |
|
|
|
fieldDescriptor); |
|
|
|
fieldDescriptor); |
|
|
|
for (DynamicMessage kv : ((RubyMap) value).build(context, mapDescriptor, depth, maxRecursionDepth)) { |
|
|
|
for (DynamicMessage kv : ((RubyMap) value).build(context, mapDescriptor, depth, recursionLimit)) { |
|
|
|
builder.addRepeatedField(fieldDescriptor, kv); |
|
|
|
builder.addRepeatedField(fieldDescriptor, kv); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -839,7 +839,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
// convert a ruby object to protobuf type, skip type check since it is checked on the way in
|
|
|
|
// convert a ruby object to protobuf type, skip type check since it is checked on the way in
|
|
|
|
private Object convert(ThreadContext context, |
|
|
|
private Object convert(ThreadContext context, |
|
|
|
FieldDescriptor fieldDescriptor, |
|
|
|
FieldDescriptor fieldDescriptor, |
|
|
|
IRubyObject value, int depth, int maxRecursionDepth, |
|
|
|
IRubyObject value, int depth, int recursionLimit, |
|
|
|
boolean isDefaultStringForBytes) { |
|
|
|
boolean isDefaultStringForBytes) { |
|
|
|
Object val = null; |
|
|
|
Object val = null; |
|
|
|
switch (fieldDescriptor.getType()) { |
|
|
|
switch (fieldDescriptor.getType()) { |
|
|
@ -881,7 +881,7 @@ public class RubyMessage extends RubyObject { |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
case MESSAGE: |
|
|
|
case MESSAGE: |
|
|
|
val = ((RubyMessage) value).build(context, depth + 1, maxRecursionDepth); |
|
|
|
val = ((RubyMessage) value).build(context, depth + 1, recursionLimit); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case ENUM: |
|
|
|
case ENUM: |
|
|
|
EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType(); |
|
|
|
EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType(); |
|
|
|