diff options
Diffstat (limited to 'src/org/gnunet/construct/parsers/StringParser.java')
-rw-r--r-- | src/org/gnunet/construct/parsers/StringParser.java | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/org/gnunet/construct/parsers/StringParser.java b/src/org/gnunet/construct/parsers/StringParser.java index a96d396..8df171e 100644 --- a/src/org/gnunet/construct/parsers/StringParser.java +++ b/src/org/gnunet/construct/parsers/StringParser.java | |||
@@ -21,7 +21,7 @@ | |||
21 | package org.gnunet.construct.parsers; | 21 | package org.gnunet.construct.parsers; |
22 | 22 | ||
23 | import org.gnunet.construct.Message; | 23 | import org.gnunet.construct.Message; |
24 | import org.gnunet.construct.ProtocolViolation; | 24 | import org.gnunet.construct.ProtocolViolationException; |
25 | import org.gnunet.construct.ReflectUtil; | 25 | import org.gnunet.construct.ReflectUtil; |
26 | 26 | ||
27 | import java.io.UnsupportedEncodingException; | 27 | import java.io.UnsupportedEncodingException; |
@@ -63,12 +63,20 @@ public class StringParser implements Parser { | |||
63 | public int parse(final ByteBuffer srcBuf, int frameOffset, Message frameObj, final Message dstObj, List<Field> | 63 | public int parse(final ByteBuffer srcBuf, int frameOffset, Message frameObj, final Message dstObj, List<Field> |
64 | frameSizePath) { | 64 | frameSizePath) { |
65 | 65 | ||
66 | if (srcBuf.get(srcBuf.position()) == 0) { | 66 | if (optional) { |
67 | if (!optional) { | 67 | if (frameSizePath == null) { |
68 | throw new ProtocolViolation("no data received for non-optional string"); | 68 | throw new AssertionError("optional string with no length field in the message!"); |
69 | } | ||
70 | final int frameSize = ReflectUtil.justGetInt(dstObj, frameSizePath); | ||
71 | int remaining = frameOffset + frameSize - srcBuf.position(); | ||
72 | |||
73 | if (remaining == 0) { | ||
74 | if (!optional) { | ||
75 | throw new ProtocolViolationException("no data received for non-optional string"); | ||
76 | } | ||
77 | ReflectUtil.justSet(dstObj, targetField, null); | ||
78 | return 0; | ||
69 | } | 79 | } |
70 | ReflectUtil.justSet(dstObj, targetField, null); | ||
71 | return 1; | ||
72 | } | 80 | } |
73 | 81 | ||
74 | int length = 0; | 82 | int length = 0; |
@@ -125,7 +133,9 @@ public class StringParser implements Parser { | |||
125 | 133 | ||
126 | @Override | 134 | @Override |
127 | public void patch(Message m, int frameSize, List<Field> frameSizePath, Message frameObj) { | 135 | public void patch(Message m, int frameSize, List<Field> frameSizePath, Message frameObj) { |
128 | // nothing to patch | 136 | if (frameSizePath != null) { |
137 | ReflectUtil.justSetInt(frameObj, frameSizePath, frameSize); | ||
138 | } | ||
129 | } | 139 | } |
130 | 140 | ||
131 | @Override | 141 | @Override |