BraintreeEncryption.Library.BouncyCastle.Asn1.DerApplicationSpecific.ReplaceTagNumber C# (CSharp) Method

ReplaceTagNumber() private method

private ReplaceTagNumber ( int newTag, byte input ) : byte[]
newTag int
input byte
return byte[]
        private byte[] ReplaceTagNumber(
			int		newTag,
			byte[]	input)
        {
            int tagNo = input[0] & 0x1f;
            int index = 1;
            //
            // with tagged object tag number is bottom 5 bits, or stored at the start of the content
            //
            if (tagNo == 0x1f)
            {
                tagNo = 0;

                int b = input[index++] & 0xff;

                // X.690-0207 8.1.2.4.2
                // "c) bits 7 to 1 of the first subsequent octet shall not all be zero."
                if ((b & 0x7f) == 0) // Note: -1 will pass
                {
                    throw new InvalidOperationException("corrupted stream - invalid high tag number found");
                }

                while ((b >= 0) && ((b & 0x80) != 0))
                {
                    tagNo |= (b & 0x7f);
                    tagNo <<= 7;
                    b = input[index++] & 0xff;
                }

                tagNo |= (b & 0x7f);
            }

            byte[] tmp = new byte[input.Length - index + 1];

            Array.Copy(input, index, tmp, 1, tmp.Length - 1);

            tmp[0] = (byte)newTag;

            return tmp;
        }