@Test public void testEncode8() { ByteBuffer buf = ByteBuffer.wrap(new byte[13]); buf.position(buf.position() + 3); int len = DerUtils.encodeIdAndLength(DerId.TagClass.PRIVATE, DerId.EncodingType.CONSTRUCTED, 32, 10, buf); assertEquals(3, len); assertEquals(0, buf.position()); assertEquals((byte) 0xff, buf.get(0)); assertEquals((byte) 0x20, buf.get(1)); assertEquals((byte) 0xa, buf.get(2)); }
public static int encodeIdAndLength(DerId.TagClass tagClass, DerId.EncodingType encodingType, int tagNumber, int contentLength, ByteBuffer buf) { int origPos = buf.position(); int pos = buf.position(); if (contentLength < 0) { throw new IllegalArgumentException("Invalid content length " + contentLength); } else if (contentLength <= 0x7f) { pos--; buf.put(pos, (byte) contentLength); } else { int lenOctetCount = 0; while (contentLength != 0) { pos--; buf.put(pos, (byte) (contentLength & 0xff)); contentLength >>>= 8; lenOctetCount++; } pos--; buf.put(pos, (byte) (0x80 | lenOctetCount)); } if (tagNumber < 0) { throw new IllegalArgumentException("Invalid tag number " + tagNumber); } else { byte firstOctet = 0; switch (tagClass) { case UNIVERSAL: break; case APPLICATION: firstOctet = 0x40; break; case CONTEXT_SPECIFIC: firstOctet = (byte) 0x80; break; case PRIVATE: firstOctet = (byte) 0xc0; } switch (encodingType) { case PRIMITIVE: break; case CONSTRUCTED: firstOctet |= 0x20; } if (tagNumber <= 30) { firstOctet |= tagNumber; pos--; buf.put(pos, firstOctet); } else { boolean last = true; while (tagNumber != 0) { byte octet = (byte) (tagNumber & 0x7f); pos--; if (last) { buf.put(pos, octet); last = false; } else { buf.put(pos, (byte) (octet | 0x80)); } tagNumber >>>= 8; } firstOctet |= 0x1f; pos--; buf.put(pos, firstOctet); } } buf.position(pos); return origPos - pos; }
DerUtils { public static int encodeIdAndLength(DerId.TagClass tagClass, DerId.EncodingType encodingType, int tagNumber, int contentLength, ByteBuffer buf) { int origPos = buf.position(); int pos = buf.position(); if (contentLength < 0) { throw new IllegalArgumentException("Invalid content length " + contentLength); } else if (contentLength <= 0x7f) { pos--; buf.put(pos, (byte) contentLength); } else { int lenOctetCount = 0; while (contentLength != 0) { pos--; buf.put(pos, (byte) (contentLength & 0xff)); contentLength >>>= 8; lenOctetCount++; } pos--; buf.put(pos, (byte) (0x80 | lenOctetCount)); } if (tagNumber < 0) { throw new IllegalArgumentException("Invalid tag number " + tagNumber); } else { byte firstOctet = 0; switch (tagClass) { case UNIVERSAL: break; case APPLICATION: firstOctet = 0x40; break; case CONTEXT_SPECIFIC: firstOctet = (byte) 0x80; break; case PRIVATE: firstOctet = (byte) 0xc0; } switch (encodingType) { case PRIMITIVE: break; case CONSTRUCTED: firstOctet |= 0x20; } if (tagNumber <= 30) { firstOctet |= tagNumber; pos--; buf.put(pos, firstOctet); } else { boolean last = true; while (tagNumber != 0) { byte octet = (byte) (tagNumber & 0x7f); pos--; if (last) { buf.put(pos, octet); last = false; } else { buf.put(pos, (byte) (octet | 0x80)); } tagNumber >>>= 8; } firstOctet |= 0x1f; pos--; buf.put(pos, firstOctet); } } buf.position(pos); return origPos - pos; } }
DerUtils { public static int encodeIdAndLength(DerId.TagClass tagClass, DerId.EncodingType encodingType, int tagNumber, int contentLength, ByteBuffer buf) { int origPos = buf.position(); int pos = buf.position(); if (contentLength < 0) { throw new IllegalArgumentException("Invalid content length " + contentLength); } else if (contentLength <= 0x7f) { pos--; buf.put(pos, (byte) contentLength); } else { int lenOctetCount = 0; while (contentLength != 0) { pos--; buf.put(pos, (byte) (contentLength & 0xff)); contentLength >>>= 8; lenOctetCount++; } pos--; buf.put(pos, (byte) (0x80 | lenOctetCount)); } if (tagNumber < 0) { throw new IllegalArgumentException("Invalid tag number " + tagNumber); } else { byte firstOctet = 0; switch (tagClass) { case UNIVERSAL: break; case APPLICATION: firstOctet = 0x40; break; case CONTEXT_SPECIFIC: firstOctet = (byte) 0x80; break; case PRIVATE: firstOctet = (byte) 0xc0; } switch (encodingType) { case PRIMITIVE: break; case CONSTRUCTED: firstOctet |= 0x20; } if (tagNumber <= 30) { firstOctet |= tagNumber; pos--; buf.put(pos, firstOctet); } else { boolean last = true; while (tagNumber != 0) { byte octet = (byte) (tagNumber & 0x7f); pos--; if (last) { buf.put(pos, octet); last = false; } else { buf.put(pos, (byte) (octet | 0x80)); } tagNumber >>>= 8; } firstOctet |= 0x1f; pos--; buf.put(pos, firstOctet); } } buf.position(pos); return origPos - pos; } private  DerUtils(); }
DerUtils { public static int encodeIdAndLength(DerId.TagClass tagClass, DerId.EncodingType encodingType, int tagNumber, int contentLength, ByteBuffer buf) { int origPos = buf.position(); int pos = buf.position(); if (contentLength < 0) { throw new IllegalArgumentException("Invalid content length " + contentLength); } else if (contentLength <= 0x7f) { pos--; buf.put(pos, (byte) contentLength); } else { int lenOctetCount = 0; while (contentLength != 0) { pos--; buf.put(pos, (byte) (contentLength & 0xff)); contentLength >>>= 8; lenOctetCount++; } pos--; buf.put(pos, (byte) (0x80 | lenOctetCount)); } if (tagNumber < 0) { throw new IllegalArgumentException("Invalid tag number " + tagNumber); } else { byte firstOctet = 0; switch (tagClass) { case UNIVERSAL: break; case APPLICATION: firstOctet = 0x40; break; case CONTEXT_SPECIFIC: firstOctet = (byte) 0x80; break; case PRIVATE: firstOctet = (byte) 0xc0; } switch (encodingType) { case PRIMITIVE: break; case CONSTRUCTED: firstOctet |= 0x20; } if (tagNumber <= 30) { firstOctet |= tagNumber; pos--; buf.put(pos, firstOctet); } else { boolean last = true; while (tagNumber != 0) { byte octet = (byte) (tagNumber & 0x7f); pos--; if (last) { buf.put(pos, octet); last = false; } else { buf.put(pos, (byte) (octet | 0x80)); } tagNumber >>>= 8; } firstOctet |= 0x1f; pos--; buf.put(pos, firstOctet); } } buf.position(pos); return origPos - pos; } private  DerUtils(); static int decodeLength(ByteBuffer buf); static DerId decodeIdAndLength(ByteBuffer buf); static int encodeIdAndLength(DerId.TagClass tagClass, DerId.EncodingType encodingType, int tagNumber,
            int contentLength, ByteBuffer buf); static int sizeOf(int tagNumber, int contentLength); }
DerUtils { public static int encodeIdAndLength(DerId.TagClass tagClass, DerId.EncodingType encodingType, int tagNumber, int contentLength, ByteBuffer buf) { int origPos = buf.position(); int pos = buf.position(); if (contentLength < 0) { throw new IllegalArgumentException("Invalid content length " + contentLength); } else if (contentLength <= 0x7f) { pos--; buf.put(pos, (byte) contentLength); } else { int lenOctetCount = 0; while (contentLength != 0) { pos--; buf.put(pos, (byte) (contentLength & 0xff)); contentLength >>>= 8; lenOctetCount++; } pos--; buf.put(pos, (byte) (0x80 | lenOctetCount)); } if (tagNumber < 0) { throw new IllegalArgumentException("Invalid tag number " + tagNumber); } else { byte firstOctet = 0; switch (tagClass) { case UNIVERSAL: break; case APPLICATION: firstOctet = 0x40; break; case CONTEXT_SPECIFIC: firstOctet = (byte) 0x80; break; case PRIVATE: firstOctet = (byte) 0xc0; } switch (encodingType) { case PRIMITIVE: break; case CONSTRUCTED: firstOctet |= 0x20; } if (tagNumber <= 30) { firstOctet |= tagNumber; pos--; buf.put(pos, firstOctet); } else { boolean last = true; while (tagNumber != 0) { byte octet = (byte) (tagNumber & 0x7f); pos--; if (last) { buf.put(pos, octet); last = false; } else { buf.put(pos, (byte) (octet | 0x80)); } tagNumber >>>= 8; } firstOctet |= 0x1f; pos--; buf.put(pos, firstOctet); } } buf.position(pos); return origPos - pos; } private  DerUtils(); static int decodeLength(ByteBuffer buf); static DerId decodeIdAndLength(ByteBuffer buf); static int encodeIdAndLength(DerId.TagClass tagClass, DerId.EncodingType encodingType, int tagNumber,
            int contentLength, ByteBuffer buf); static int sizeOf(int tagNumber, int contentLength);  }