Esempio n. 1
0
        /// <summary>
        ///     Encoding length determinant procedure.
        ///     ITU-T X.691. 10.9. General rules for encoding a length determinant
        /// </summary>
        protected virtual int encodeLengthDeterminant(int length, BitArrayOutputStream stream)
        {
            int result = 0;

            doAlign(stream);
            if (length >= 0 && length < 0x80)
            {
                // NOTE 2. a) ("n" less than 128)
                stream.WriteByte(length); // a single octet containing "n" with bit 8 set to zero;
                result = 1;
            }
            else if (length < 0x4000)
            {
                // NOTE 2. b) ("n" less than 16K) two octets
                // containing "n" with bit 8 of the first octet
                // set to 1 and bit 7 set to zero;
                stream.WriteByte((length >> 8) & 0x3f | 0x80);
                stream.WriteByte(length);
                result = 2;
            }
            else
            {
                // NOTE 2. b) (large "n") a single octet containing a count "m"
                // with bit 8 set to 1 and bit 7 set to 1.
                // The count "m" is one to four, and the length indicates that
                // a fragment of the material follows (a multiple "m" of 16K items).
                // For all values of "m", the fragment is then followed
                // by another length encoding for the remainder of the material.
                throw new ApplicationException("Not supported for this version. Length too big!");
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        ///     Encoding of a constrained whole number
        ///     ITU-T X.691. 10.5.
        ///     NOTE – (Tutorial) This subclause is referenced by other clauses,
        ///     and itself references earlier clauses for the production of
        ///     a nonnegative-binary-integer or a 2's-complement-binary-integer encoding.
        /// </summary>
        protected virtual int encodeConstraintNumber(long val, long min, long max, BitArrayOutputStream stream)
        {
            int  result      = 0;
            long valueRange  = max - min;
            long narrowedVal = val - min;
            int  maxBitLen   = PERCoderUtils.getMaxBitLength(valueRange);

            if (valueRange == 0)
            {
                return(result);
            }

            // The rest of this Note addresses the ALIGNED variant.
            if (valueRange > 0 && valueRange < 256)
            {
                /*
                 * 1. Where the range is less than or equal to 255, the value encodes
                 * into a bit-field of the minimum size for the range.
                 * 2. Where the range is exactly 256, the value encodes
                 * into a single octet octet-aligned bit-field.
                 */
                doAlign(stream);
                for (int i = maxBitLen - 1; i >= 0; i--)
                {
                    int bitValue = (int)((narrowedVal >> i) & 0x1);
                    stream.writeBit(bitValue);
                }
                result = 1;
            }
            else if (valueRange > 0 && valueRange < 65536)
            {
                /*
                 * 3. Where the range is 257 to 64K, the value encodes into
                 * a two octet octet-aligned bit-field.
                 */
                doAlign(stream);
                stream.WriteByte((byte)(narrowedVal >> 8));
                stream.WriteByte((byte)(narrowedVal & 0xFF));
                result = 2;
            }
            else
            {
                /*
                 * 4. Where the range is greater than 64K, the range is ignored
                 * and the value encodes into an  octet-aligned bit-field
                 * which is the minimum number of octets for the value.
                 * In this latter case, later procedures (see 10.9)
                 * also encode a length field (usually a single octet) to indicate
                 * the length of the encoding. For the other cases, the length
                 * of the encoding is independent of the value being encoded,
                 * and is not explicitly encoded.
                 */
                result = encodeConstraintLengthDeterminant(CoderUtils.getIntegerLength(narrowedVal), 1, CoderUtils.getPositiveIntegerLength(valueRange), stream);
                doAlign(stream);
                result += encodeIntegerValueAsBytes(narrowedVal, stream);
            }
            return(result);
        }
Esempio n. 3
0
        protected override int encodeConstraintNumber(long val, long min, long max, BitArrayOutputStream stream)
        {
            int  result      = 0;
            long valueRange  = max - min;
            long narrowedVal = val - min;
            int  maxBitLen   = PERCoderUtils.getMaxBitLength(valueRange);

            if (valueRange == 0)
            {
                return(result);
            }

            //For the UNALIGNED variant the value is always encoded in the minimum
            // number of bits necessary to represent the range (defined in 10.5.3).
            int currentBit = maxBitLen;

            while (currentBit > 8)
            {
                currentBit -= 8;
                result++;
                stream.WriteByte((byte)(narrowedVal >> currentBit));
            }
            if (currentBit > 0)
            {
                for (int i = currentBit - 1; i >= 0; i--)
                {
                    int bitValue = (int)((narrowedVal >> i) & 0x1);
                    stream.writeBit(bitValue);
                }
                result += 1;
            }
            return(result);
        }
        /// <seealso cref="BitArrayOutputStream.write(int)">
        /// </seealso>
        public virtual void  testWrite()
        {
            BitArrayOutputStream stream = new BitArrayOutputStream();

            stream.WriteByte(0xFF);
            stream.writeBit(true);
            stream.writeBit(false);
            stream.writeBit(true);
            stream.writeBit(false);
            stream.WriteByte(0xFF);
            stream.WriteByte(0x0F);
            stream.writeBit(true);
            stream.writeBit(true);
            stream.writeBit(true);
            stream.writeBit(true);
            System.Console.Out.WriteLine("Write " + ByteTools.byteArrayToHexString(stream.ToArray()));
            ByteTools.checkBuffers(stream.ToArray(), new byte[] { 0xFF, 0xAF, 0xF0, 0xFF });

            stream.writeBit(true);
            stream.writeBit(false);
            stream.writeBit(true);
            stream.writeBit(false);
            byte[] temp_byteArray;
            temp_byteArray = new byte[] { (0xCC), (0xFF), (0xFF), (0xBB) };
            stream.Write(temp_byteArray, 0, temp_byteArray.Length);
            System.Console.Out.WriteLine("After buf write " + ByteTools.byteArrayToHexString(stream.ToArray()));
            ByteTools.checkBuffers(stream.ToArray(), new byte[] { (0xFF), (0xAF), (0xF0), (0xFF), (0xAC), (0xCF), (0xFF), (0xFB), (0xB0) });
            stream.align();
            stream.writeBit(true);
            stream.writeBit(true);
            stream.align();
            stream.WriteByte(0xFF);

            System.Console.Out.WriteLine("After align " + ByteTools.byteArrayToHexString(stream.ToArray()));
            ByteTools.checkBuffers(stream.ToArray(), new byte[] { (0xFF), (0xAF), (0xF0), (0xFF), (0xAC), (0xCF), (0xFF), (0xFB), (0xB0), (0xC0), (0xFF) });
        }