ChannelData: fix decode()

I haven't found any user of this method.

This method was only working if offset == 0
and binMessage isn't a padded message.

arrayLen is useless as we have msgLen, so remove it,
and introduce a deprecated decode() to keep same api

Signed-off-by: Etienne CHAMPETIER <champetier.etienne@gmail.com>
This commit is contained in:
Etienne CHAMPETIER 2016-04-25 12:27:51 +02:00
parent 84bd07c665
commit b76f9abfc9

View File

@ -188,15 +188,29 @@ public class ChannelData
* @return a Message object constructed from the binMessage array * @return a Message object constructed from the binMessage array
* @throws StunException ILLEGAL_ARGUMENT if one or more of the arguments * @throws StunException ILLEGAL_ARGUMENT if one or more of the arguments
* have invalid values. * have invalid values.
* @deprecated
*/ */
public static ChannelData decode(byte binMessage[], char offset, char arrayLen) throws StunException public static ChannelData decode(byte binMessage[], char offset, char arrayLen) throws StunException
{
return decode(binMessage, offset);
}
/**
* Constructs a message from its binary representation.
* @param binMessage the binary array that contains the encoded message
* @param offset the index where the message starts.
* @return a Message object constructed from the binMessage array
* @throws StunException ILLEGAL_ARGUMENT if one or more of the arguments
* have invalid values.
*/
public static ChannelData decode(byte binMessage[], char offset) throws StunException
{ {
char msgLen = 0; char msgLen = 0;
char channelNumber = 0; char channelNumber = 0;
ChannelData channelData = null; ChannelData channelData = null;
byte data[] = null; byte data[] = null;
if(arrayLen < 4) if((binMessage.length - offset) < HEADER_LENGTH)
{ {
throw new StunException(StunException.ILLEGAL_ARGUMENT, "Size too short"); throw new StunException(StunException.ILLEGAL_ARGUMENT, "Size too short");
} }
@ -209,7 +223,7 @@ public class ChannelData
} }
msgLen = (char)((binMessage[offset++]<<8) | (binMessage[offset++]&0xFF)); msgLen = (char)((binMessage[offset++]<<8) | (binMessage[offset++]&0xFF));
if(msgLen != (binMessage.length - 4)) if (msgLen > (binMessage.length - offset))
{ {
throw new StunException(StunException.ILLEGAL_ARGUMENT, "Size mismatch"); throw new StunException(StunException.ILLEGAL_ARGUMENT, "Size mismatch");
} }