From f369da34cf9aca151df0150d90e651e6a88ee700 Mon Sep 17 00:00:00 2001 From: Yuuta Liang Date: Thu, 12 Oct 2023 12:19:49 +0800 Subject: Fix lint Signed-off-by: Yuuta Liang --- src/main/model/asn1/ASN1Object.java | 8 +--- src/main/model/asn1/ObjectIdentifier.java | 65 ++++++++++++++++++------------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/main/model/asn1/ASN1Object.java b/src/main/model/asn1/ASN1Object.java index 1af26ce..d1bce06 100644 --- a/src/main/model/asn1/ASN1Object.java +++ b/src/main/model/asn1/ASN1Object.java @@ -80,12 +80,8 @@ public class ASN1Object implements Encodable { public ASN1Object(BytesReader encoded, boolean hasParentTag) throws ParseException { if (hasParentTag) { this.parentTag = new Tag(encoded); - if (parentTag.getCls() != TagClass.CONTEXT_SPECIFIC) { - throw new ParseException("Parent tag must be CONTEXT_SPECIFIC, but found " - + parentTag.getCls() + "[" + parentTag.getNumber() + "]."); - } - if (!parentTag.isConstructive()) { - throw new ParseException("Parent tag must be constructive."); + if (parentTag.getCls() != TagClass.CONTEXT_SPECIFIC || !parentTag.isConstructive()) { + throw new ParseException("Parent tag must be CONTEXT_SPECIFIC and constructive."); } int parentLen = new ASN1Length(encoded).getLength(); // Validate length diff --git a/src/main/model/asn1/ObjectIdentifier.java b/src/main/model/asn1/ObjectIdentifier.java index e2b9dfe..c7278db 100644 --- a/src/main/model/asn1/ObjectIdentifier.java +++ b/src/main/model/asn1/ObjectIdentifier.java @@ -102,6 +102,16 @@ public class ObjectIdentifier extends ASN1Object { nums.add(0); nums.add((int) first); } + nums.addAll(parse(raw)); + this.ints = nums.toArray(new Integer[0]); + } + + /** + * EFFECTS: Parse input OID bytes. + * REQUIRES: raw.length >= 1 + */ + private static List parse(Byte[] raw) throws ParseException { + List nums = new ArrayList<>(); List num = new ArrayList<>(); for (int i = 1; i < raw.length; i++) { Byte b = raw[i]; @@ -123,13 +133,9 @@ public class ObjectIdentifier extends ASN1Object { } } if (!num.isEmpty()) { - throw new ParseException("Unterminated byte. Currently " - + num.stream().map(BitSet::toByteArray).map(Utils::byteToByte) - .flatMap(Arrays::stream) - .map(b -> String.format("0x%02X", b)) - .collect(Collectors.toList())); + throw new ParseException("Unterminated byte."); } - this.ints = nums.toArray(new Integer[0]); + return nums; } /** @@ -171,33 +177,38 @@ public class ObjectIdentifier extends ASN1Object { Arrays.asList(Utils.valToByte(ints[0] * 40 + ints[1])), Stream.of(ints) .skip(2) - .map(i -> { - BigInteger bi = BigInteger.valueOf(i); - List bs = Arrays.asList(Utils.byteToByte(bi.toByteArray())); - Collections.reverse(bs); - final BitSet bitSetOriginal = BitSet.valueOf(Utils.byteToByte(bs.toArray(new Byte[0]))); - BitSet bitSet = new BitSet(bs.size() * 16); - int k = 0; - for (int j = 0; j < bs.size() * 8; j++) { - if (j == 0 || j % 7 != 0) { - bitSet.set(k++, bitSetOriginal.get(j)); - } else { - bitSet.set(k++, j != 7); - bitSet.set(k++, bitSetOriginal.get(j)); - } - } - byte[] bs1 = bitSet.toByteArray(); - List res = - Arrays.asList(Utils.byteToByte(bs1)); - Collections.reverse(res); - return res; - }) + .map(ObjectIdentifier::encodeSingleInt) .flatMap(Collection::stream) .collect(Collectors.toList()) ).flatMap(Collection::stream) .toArray(Byte[]::new); } + /** + * EFFECTS: Encode a single int component into OID-format bytes. + */ + private static List encodeSingleInt(int i) { + BigInteger bi = BigInteger.valueOf(i); + List bs = Arrays.asList(Utils.byteToByte(bi.toByteArray())); + Collections.reverse(bs); + final BitSet bitSetOriginal = BitSet.valueOf(Utils.byteToByte(bs.toArray(new Byte[0]))); + BitSet bitSet = new BitSet(bs.size() * 16); + int k = 0; + for (int j = 0; j < bs.size() * 8; j++) { + if (j == 0 || j % 7 != 0) { + bitSet.set(k++, bitSetOriginal.get(j)); + } else { + bitSet.set(k++, j != 7); + bitSet.set(k++, bitSetOriginal.get(j)); + } + } + byte[] bs1 = bitSet.toByteArray(); + List res = + Arrays.asList(Utils.byteToByte(bs1)); + Collections.reverse(res); + return res; + } + public Integer[] getInts() { return ints; } -- cgit v1.2.3