| | |
| | | * |
| | | * |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | * Portions copyright 2012 ForgeRock AS. |
| | | * Portions copyright 2012-2013 ForgeRock AS. |
| | | */ |
| | | |
| | | package com.forgerock.opendj.ldap; |
| | |
| | | import static com.forgerock.opendj.ldap.LDAPConstants.ELEMENT_READ_STATE_NEED_TYPE; |
| | | import static com.forgerock.opendj.ldap.LDAPConstants.ELEMENT_READ_STATE_NEED_VALUE_BYTES; |
| | | import static com.forgerock.opendj.ldap.CoreMessages.*; |
| | | import static com.forgerock.opendj.util.StaticUtils.IO_LOG; |
| | | import static com.forgerock.opendj.util.StaticUtils.byteToHex; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.logging.Level; |
| | | |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.opendj.asn1.ASN1Reader; |
| | | import org.forgerock.opendj.asn1.AbstractASN1Reader; |
| | |
| | | import org.glassfish.grizzly.memory.CompositeBuffer; |
| | | import org.glassfish.grizzly.memory.MemoryManager; |
| | | |
| | | import com.forgerock.opendj.util.StaticUtils; |
| | | |
| | | /** |
| | | * Grizzly ASN1 reader implementation. |
| | | */ |
| | |
| | | public SequenceLimiter endSequence() throws IOException { |
| | | parent.checkLimit(remaining()); |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINE) && remaining() > 0) { |
| | | StaticUtils.DEBUG_LOG.fine(String.format( |
| | | "Ignoring %d unused trailing bytes in ASN.1 SEQUENCE", remaining())); |
| | | if (remaining() > 0) { |
| | | IO_LOG.debug("Ignoring {} unused trailing bytes in ASN.1 SEQUENCE", remaining()); |
| | | } |
| | | |
| | | for (int i = 0; i < remaining(); i++) { |
| | |
| | | readLimiter.checkLimit(peekLength); |
| | | final byte readByte = buffer.get(); |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format( |
| | | "READ ASN.1 BOOLEAN(type=0x%x, length=%d, value=%s)", peekType, peekLength, |
| | | String.valueOf(readByte != 0x00))); |
| | | } |
| | | IO_LOG.trace("READ ASN.1 BOOLEAN(type=0x{}, length={}, value={})", |
| | | byteToHex(peekType), peekLength, String.valueOf(readByte != 0x00)); |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | return readByte != 0x00; |
| | |
| | | public void readEndSequence() throws IOException { |
| | | readLimiter = readLimiter.endSequence(); |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format("READ ASN.1 END SEQUENCE")); |
| | | } |
| | | IO_LOG.debug("READ ASN.1 END SEQUENCE"); |
| | | |
| | | // Reset the state |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | |
| | | intValue = (intValue << 8) | (readByte & 0xFF); |
| | | } |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format( |
| | | "READ ASN.1 INTEGER(type=0x%x, length=%d, value=%d)", peekType, peekLength, |
| | | intValue)); |
| | | } |
| | | IO_LOG.trace("READ ASN.1 INTEGER(type=0x{}, length={}, value={})", |
| | | byteToHex(peekType), peekLength, intValue); |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | return intValue; |
| | |
| | | throw DecodeException.fatalError(message); |
| | | } |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format("READ ASN.1 NULL(type=0x%x, length=%d)", |
| | | peekType, peekLength)); |
| | | } |
| | | IO_LOG.trace("READ ASN.1 NULL(type=0x{}, length={})", |
| | | byteToHex(peekType), peekLength); |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | } |
| | |
| | | final byte[] value = new byte[peekLength]; |
| | | buffer.get(value); |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format( |
| | | "READ ASN.1 OCTETSTRING(type=0x%x, length=%d)", peekType, peekLength)); |
| | | } |
| | | IO_LOG.trace("READ ASN.1 OCTETSTRING(type=0x{}, length={})", byteToHex(peekType), peekLength); |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | return ByteString.wrap(value); |
| | |
| | | builder.append(buffer.get()); |
| | | } |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format( |
| | | "READ ASN.1 OCTETSTRING(type=0x%x, length=%d)", peekType, peekLength)); |
| | | } |
| | | IO_LOG.trace("READ ASN.1 OCTETSTRING(type=0x{}, length={})", byteToHex(peekType), peekLength); |
| | | |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |
| | | return builder; |
| | |
| | | try { |
| | | str = new String(readBuffer, 0, peekLength, "UTF-8"); |
| | | } catch (final Exception e) { |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.WARNING)) { |
| | | StaticUtils.DEBUG_LOG |
| | | .warning("Unable to decode ASN.1 OCTETSTRING bytes as UTF-8 string: " |
| | | + e.toString()); |
| | | } |
| | | // TODO: I18N |
| | | IO_LOG.warn("Unable to decode ASN.1 OCTETSTRING bytes as UTF-8 string", e); |
| | | |
| | | str = new String(stringBuffer, 0, peekLength); |
| | | } |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format( |
| | | "READ ASN.1 OCTETSTRING(type=0x%x, length=%d, value=%s)", peekType, peekLength, |
| | | str)); |
| | | } |
| | | IO_LOG.trace("READ ASN.1 OCTETSTRING(type=0x{}, length={}, value={})", |
| | | byteToHex(peekType), peekLength, str); |
| | | |
| | | return str; |
| | | } |
| | |
| | | |
| | | readLimiter = readLimiter.startSequence(peekLength); |
| | | |
| | | if (StaticUtils.DEBUG_LOG.isLoggable(Level.FINEST)) { |
| | | StaticUtils.DEBUG_LOG.finest(String.format( |
| | | "READ ASN.1 START SEQUENCE(type=0x%x, length=%d)", peekType, peekLength)); |
| | | } |
| | | IO_LOG.trace("READ ASN.1 START SEQUENCE(type=0x{}, length={})", |
| | | byteToHex(peekType), peekLength); |
| | | |
| | | // Reset the state |
| | | state = ELEMENT_READ_STATE_NEED_TYPE; |