/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
@Override public String toString() { return name;
}
privatestatic Operation fromId(int id) {
Optional<Operation> optional = Stream.of(Operation.values())
.filter(o -> o.id == id).findFirst(); if (optional.isPresent()) { return optional.get();
} else { thrownew RuntimeException( "Unknown id " + id + " for enum Operation.");
}
}
}
public LdapMessage(byte[] message) { this.message = message;
parse();
}
public LdapMessage(String hexString) { this(parseHexBinary(hexString));
}
// Extracts the message ID and operation ID from an LDAP protocol encoding privatevoid parse() { if (message == null || message.length < 2) { thrownew RuntimeException( "Invalid ldap message: " + Arrays.toString(message));
}
if (message[0] != 0x30) { thrownew RuntimeException("Bad LDAP encoding in message, "
+ "expected ASN.1 SEQUENCE tag (0x30), encountered "
+ message[0]);
}
int index = 2; if ((message[1] & 0x80) == 0x80) {
index += (message[1] & 0x0F);
}
if (message[index] != 0x02) { thrownew RuntimeException("Bad LDAP encoding in message, "
+ "expected ASN.1 INTEGER tag (0x02), encountered "
+ message[index]);
} int length = message[index + 1];
index += 2;
messageID = new BigInteger(1,
Arrays.copyOfRange(message, index, index + length)).intValue();
index += length; int operationID = message[index];
operation = Operation.fromId(operationID);
}
/** * Return original ldap message in byte array. * * @return original ldap message
*/ publicbyte[] getMessage() { return Arrays.copyOf(message, message.length);
}
// "111" is not a valid hex encoding. if (len % 2 != 0) { thrownew IllegalArgumentException( "hexBinary needs to be even-length: " + s);
}
byte[] out = newbyte[len / 2];
for (int i = 0; i < len; i += 2) { int h = Character.digit(s.charAt(i), 16); int l = Character.digit(s.charAt(i + 1), 16); if (h == -1 || l == -1) { thrownew IllegalArgumentException( "contains illegal character for hexBinary: " + s);
}
out[i / 2] = (byte) (h * 16 + l);
}
return out;
}
publicstaticint getMessageLength(byte[] encoding) { if (encoding.length < 2) { // not enough data to extract msg len, just return -1 return -1;
}
if (encoding[0] != 0x30) { thrownew RuntimeException("Error: bad LDAP encoding message: "
+ "expected ASN.1 SEQUENCE tag (0x30), encountered "
+ encoding[0]);
}
int len; int index = 1; int payloadLen = 0;
if ((encoding[1] & 0x80) == 0x80) {
len = (encoding[1] & 0x0F);
index++;
} else {
len = 1;
}
if (len > 4) { thrownew RuntimeException( "Error: LDAP encoding message payload too large");
}
if (encoding.length < index + len) { // additional data required to extract payload len, return -1 return -1;
}
for (byte b : Arrays.copyOfRange(encoding, index, index + len)) {
payloadLen = payloadLen << 8 | (b & 0xFF);
}
if (payloadLen <= 0) { thrownew RuntimeException( "Error: invalid LDAP encoding message length or payload too large");
}
return index + len + payloadLen;
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.0 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.