/// <summary> /// Encode a sequence of integers into an OID object and set the payload. /// </summary> /// /// <param name="value">The array of integers.</param> public void prepareEncoding(int[] value_ren) { int firstNumber = 0; if (value_ren.Length == 0) throw new DerEncodingException("No integer in OID"); else { if (value_ren[0] >= 0 && value_ren[0] <= 2) firstNumber = value_ren[0] * 40; else throw new DerEncodingException( "First integer in OID is out of range"); } if (value_ren.Length >= 2) { if (value_ren[1] >= 0 && value_ren[1] <= 39) firstNumber += value_ren[1]; else throw new DerEncodingException( "Second integer in OID is out of range"); } DynamicByteBuffer encodedBuffer = new DynamicByteBuffer(10); encodedBuffer.ensuredPut(encode128(firstNumber)); if (value_ren.Length > 2) { for (int i = 2; i < value_ren.Length; ++i) encodedBuffer.ensuredPut(encode128(value_ren[i])); } encodeHeader(encodedBuffer.position()); payload_.ensuredPut(encodedBuffer.flippedBuffer()); }
/// <summary> /// Compute the encoding for one part of an OID, where values greater than 128 must be encoded as multiple bytes. /// </summary> /// /// <param name="value">A component of an OID.</param> /// <returns>The encoded buffer.</returns> public static ByteBuffer encode128(int value_ren) { int mask = (1 << 7) - 1; DynamicByteBuffer outBytes = new DynamicByteBuffer(10); // We encode backwards from the back. outBytes.position(outBytes.limit()); if (value_ren < 128) outBytes.ensuredPutFromBack((byte) (value_ren & mask)); else { outBytes.ensuredPutFromBack((byte) (value_ren & mask)); value_ren >>= 7; while (value_ren != 0) { outBytes.ensuredPutFromBack((byte) ((value_ren & mask) | (1 << 7))); value_ren >>= 7; } } return outBytes.buffer().slice(); }
/// <summary> /// Create a new DerInteger for the value. /// </summary> /// /// <param name="integer">The value to encode.</param> public DerInteger(int integer) : base(net.named_data.jndn.encoding.der.DerNodeType.Integer) { if (integer < 0) throw new DerEncodingException( "DerInteger: Negative integers are not currently supported"); // Convert the integer to bytes the easy/slow way. DynamicByteBuffer temp = new DynamicByteBuffer(10); // We encode backwards from the back. temp.position(temp.limit()); while (true) { temp.ensuredPutFromBack((byte) (integer & 0xff)); integer >>= 8; if (integer <= 0) // We check for 0 at the end so we encode one byte if it is 0. break; } if ((((int) temp.buffer().get(temp.position())) & 0xff) >= 0x80) // Make it a non-negative integer. temp.ensuredPutFromBack((byte) 0); payload_.ensuredPut(temp.buffer().slice()); encodeHeader(payload_.position()); }
/// <summary> /// Encode the given size and update the header. /// </summary> /// /// <param name="size">The payload size to encode.</param> protected internal void encodeHeader(int size) { DynamicByteBuffer buffer = new DynamicByteBuffer(10); buffer.ensuredPut((byte) nodeType_); if (size < 0) // We don't expect this to happen since this is a protected method and // always called with the non-negative size() of some buffer. throw new Exception("encodeHeader: DER object has negative length"); else if (size <= 127) buffer.ensuredPut((byte) (size & 0xff)); else { DynamicByteBuffer tempBuf = new DynamicByteBuffer(10); // We encode backwards from the back. tempBuf.position(tempBuf.limit()); int val = size; int n = 0; while (val != 0) { tempBuf.ensuredPutFromBack((byte) (val & 0xff)); val >>= 8; n += 1; } tempBuf.ensuredPutFromBack((byte) (((1 << 7) | n) & 0xff)); buffer.ensuredPut(tempBuf.buffer()); } header_ = buffer.flippedBuffer(); }
/// <summary> /// Create a new TlvEncoder with a default DynamicByteBuffer. /// When done, you should call getOutput(). /// </summary> /// public TlvEncoder() { output_ = new DynamicByteBuffer(16); // We will start encoding from the back. output_.position(output_.limit()); }
/// <summary> /// Create a new TlvEncoder to use a DynamicByteBuffer with the initialCapacity. /// When done, you should call getOutput(). /// </summary> /// /// <param name="initialCapacity">The initial capacity of buffer().</param> public TlvEncoder(int initialCapacity) { output_ = new DynamicByteBuffer(initialCapacity); // We will start encoding from the back. output_.position(output_.limit()); }