use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.
the class Asn1Decoder method treatTagStartState.
/**
* Treat the start of a TLV. It reads the tag and get its value.
*
* @param stream The ByteBuffer containing the PDU to decode
* @param container The container that stores the current state,
* the result and other informations.
* @return <code>true</code> if there are more bytes to read, <code>false
* </code> otherwise
*/
private boolean treatTagStartState(ByteBuffer stream, Asn1Container container) {
if (stream.hasRemaining()) {
byte octet = stream.get();
TLV tlv = new TLV(container.getNewTlvId());
tlv.setTag(octet);
// Store the current TLV in the container.
container.setCurrentTLV(tlv);
// Create a link between the current TLV with its parent
tlv.setParent(container.getParentTLV());
// Switch to the next state, which is the Length decoding
container.setState(TLVStateEnum.LENGTH_STATE_START);
if (IS_DEBUG) {
byte tag = container.getCurrentTLV().getTag();
LOG.debug(I18n.msg(I18n.MSG_01000_TAG_DECODED, Asn1StringUtils.dumpByte(tag)));
}
return MORE;
} else {
// The stream has been exhausted
return END;
}
}
use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.
the class Asn1Decoder method treatLengthStartState.
/**
* Treat the Length start. The tag has been decoded, so we have to deal with
* the LENGTH, which can be multi-bytes.
*
* @param stream The ByteBuffer containing the PDU to decode
* @param container The container that stores the current state,
* the result and other informations.
* @return <code>true</code> if there are more bytes to read, <code>false
* </code> otherwise
* @throws DecoderException Thrown if anything went wrong
*/
private boolean treatLengthStartState(ByteBuffer stream, Asn1Container container) throws DecoderException {
if (stream.hasRemaining()) {
byte octet = stream.get();
TLV tlv = container.getCurrentTLV();
if ((octet & TLV.LENGTH_LONG_FORM) == 0) {
// We don't have a long form. The Length of the Value part is
// given by this byte.
tlv.setLength(octet);
tlv.setLengthNbBytes(1);
container.setState(TLVStateEnum.LENGTH_STATE_END);
} else if ((octet & TLV.LENGTH_EXTENSION_RESERVED) != TLV.LENGTH_EXTENSION_RESERVED) {
int expectedLength = octet & TLV.LENGTH_SHORT_MASK;
if (expectedLength > 4) {
String msg = I18n.err(I18n.ERR_01000_LENGTH_OVERFLOW);
LOG.error(msg);
throw new DecoderException(msg);
}
tlv.setLength(0);
tlv.setLengthNbBytes(1 + expectedLength);
tlv.setLengthBytesRead(1);
container.setState(TLVStateEnum.LENGTH_STATE_PENDING);
} else {
String msg = I18n.err(I18n.ERR_01001_LENGTH_EXTENSION_RESERVED);
LOG.error(msg);
throw new DecoderException(msg);
}
return MORE;
} else {
return END;
}
}
use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.
the class AbstractReadOctetString method action.
/**
* {@inheritDoc}
*/
@Override
public final void action(C container) throws DecoderException {
TLV tlv = container.getCurrentTLV();
// The Length should not be null
if ((tlv.getLength() == 0) && (!canBeNull)) {
String msg = I18n.err(I18n.ERR_01101_NULL_LENGTH);
LOG.error(msg);
// This will generate a PROTOCOL_ERROR
throw new DecoderException(msg);
}
BerValue value = tlv.getValue();
// The data should not be null
if ((value.getData() == null) && (!canBeNull)) {
String msg = I18n.err(I18n.ERR_01101_NULL_LENGTH);
LOG.error(msg);
// This will generate a PROTOCOL_ERROR
throw new DecoderException(msg);
}
setOctetString(value.getData(), container);
}
use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.
the class AbstractContainer method updateParent.
/**
* {@inheritDoc}
*/
@Override
public void updateParent() {
TLV parentTlv = tlv.getParent();
while ((parentTlv != null) && (parentTlv.getExpectedLength() == 0)) {
parentTlv = parentTlv.getParent();
}
this.parentTLV = parentTlv;
}
use of org.apache.directory.api.asn1.ber.tlv.TLV in project directory-ldap-api by apache.
the class LdapDecoderTest method testDecodeBadLengthTooSmall.
/**
* Test the decoding of a PDU with a bad Length. The first TLV has a length
* of 0x32 when the PDU is 0x33 bytes long.
*/
@Test
public void testDecodeBadLengthTooSmall() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x35);
stream.put(new byte[] { // Length should be 0x33...
0x30, // LDAPMessage ::=SEQUENCE {
0x32, 0x02, 0x01, // messageID MessageID
0x01, 0x60, // CHOICE { ..., bindRequest BindRequest, ...
0x2E, // BindRequest ::= APPLICATION[0] SEQUENCE {
0x02, 0x01, // version INTEGER (1..127),
0x03, 0x04, // name LDAPDN,
0x1F, 'u', 'i', 'd', '=', 'a', 'k', 'a', 'r', 'a', 's', 'u', 'l', 'u', ',', 'd', 'c', '=', 'e', 'x', 'a', 'm', 'p', 'l', 'e', ',', 'd', 'c', '=', 'c', 'o', 'm', (byte) 0x80, // authentication
0x08, // ...
'p', 'a', 's', 's', 'w', 'o', 'r', 'd' });
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<MessageDecorator<? extends Message>> ldapMessageContainer = new LdapMessageContainer<MessageDecorator<? extends Message>>(codec);
// Decode a BindRequest PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
assertEquals("ERR_01003_VALUE_LENGTH_ABOVE_EXPECTED_LENGTH The current Value length 48 is above the expected length 47", de.getMessage());
return;
}
fail("Should never reach this point..");
}
Aggregations