Пример #1
0
            /// <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();
            }
Пример #2
0
            /// <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());
            }
Пример #3
0
 /// <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());
 }
Пример #4
0
        /// <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();
        }
Пример #5
0
 /// <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());
 }