コード例 #1
0
        public override int encodeReal(object obj, System.IO.Stream stream, ElementInfo elementInfo)
        {
            int result = 0;
            BitArrayOutputStream bitStream = (BitArrayOutputStream)stream;
            Double value = (Double)obj;
            //CoderUtils.checkConstraints(value,elementInfo);

            long asLong = System.BitConverter.DoubleToInt64Bits(value);

            if (value == Double.PositiveInfinity)
            { // positive infinity
                result += encodeLengthDeterminant(1, bitStream);
                doAlign(stream);
                stream.WriteByte(0x40); // 01000000 Value is PLUS-INFINITY
                result += 1;
            }
            else if (value == Double.NegativeInfinity)
            { // negative infinity
                result += encodeLengthDeterminant(1, bitStream);
                doAlign(stream);
                stream.WriteByte(0x41); // 01000001 Value is MINUS-INFINITY
                result += 1;
            }
            else if (asLong != 0)
            {
                long exponent = ((0x7ff0000000000000L & asLong) >> 52) - 1023 - 52;
                long mantissa = 0x000fffffffffffffL & asLong;
                mantissa |= 0x10000000000000L; // set virtual delimeter

                // pack mantissa for base 2
                while ((mantissa & 0xFFL) == 0)
                {
                    mantissa >>= 8;
                    exponent  += 8; //increment exponent to 8 (base 2)
                }
                while ((mantissa & 0x01L) == 0)
                {
                    mantissa >>= 1;
                    exponent  += 1; //increment exponent to 1
                }

                int szOfExp = CoderUtils.getIntegerLength(exponent);
                encodeLengthDeterminant(CoderUtils.getIntegerLength(mantissa) + szOfExp + 1, bitStream);
                doAlign(stream);

                byte realPreamble = 0x80;
                realPreamble |= (byte)(szOfExp - 1);
                if ((((ulong)asLong) & 0x8000000000000000L) == 0x8000000000000000L)
                {
                    realPreamble |= 0x40; // Sign
                }
                stream.WriteByte(realPreamble);
                result += 1;

                result += encodeIntegerValueAsBytes(exponent, stream);
                result += encodeIntegerValueAsBytes(mantissa, stream);
            }
            return(result);
        }
コード例 #2
0
ファイル: BEREncoder.cs プロジェクト: xj0229/gsf
        public override int encodeReal(object obj, Stream stream, ElementInfo elementInfo)
        {
            int    resultSize = 0;
            Double value      = (Double)obj;
            //CoderUtils.checkConstraints(value,elementInfo);
            int szOfInt = 0;

#if PocketPC
            byte[] dblValAsBytes = System.BitConverter.GetBytes(value);
            long   asLong        = System.BitConverter.ToInt64(dblValAsBytes, 0);
#else
            long asLong = BitConverter.DoubleToInt64Bits(value);
#endif
            if (value == Double.PositiveInfinity)
            {
                // positive infinity
                stream.WriteByte(0x40); // 01000000 Value is PLUS-INFINITY
            }
            else if (value == Double.NegativeInfinity)
            {
                // negative infinity
                stream.WriteByte(0x41); // 01000001 Value is MINUS-INFINITY
            }
            else if (asLong != 0x0)
            {
                long exponent = ((0x7ff0000000000000L & asLong) >> 52) - 1023 - 52;
                long mantissa = 0x000fffffffffffffL & asLong;
                mantissa |= 0x10000000000000L; // set virtual delimiter

                // pack mantissa for base 2
                while ((mantissa & 0xFFL) == 0x0)
                {
                    mantissa >>= 8;
                    exponent  += 8; //increment exponent to 8 (base 2)
                }
                while ((mantissa & 0x01L) == 0x0)
                {
                    mantissa >>= 1;
                    exponent  += 1; //increment exponent to 1
                }

                szOfInt += encodeIntegerValue(mantissa, stream);
                int szOfExp = CoderUtils.getIntegerLength(exponent);
                szOfInt += encodeIntegerValue(exponent, stream);

                byte realPreamble = 0x80;
                realPreamble |= (byte)(szOfExp - 1);
                if (((ulong)asLong & 0x8000000000000000L) == 1)
                {
                    realPreamble |= 0x40; // Sign
                }
                stream.WriteByte(realPreamble);
                szOfInt += 1;
            }
            resultSize += szOfInt;
            resultSize += encodeLength(szOfInt, stream);
            resultSize += encodeTag(BERCoderUtils.getTagValueForElement(elementInfo, TagClasses.Universal, ElementType.Primitive, UniversalTags.Real), stream);
            return(resultSize);
        }
コード例 #3
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);
        }
コード例 #4
0
        /// <summary>
        ///     Encoding of a unconstrained whole number
        ///     ITU-T X.691. 10.8.
        ///     NOTE – (Tutorial) This case only arises in the encoding of the
        ///     value of an integer type with no lower bound. The procedure
        ///     encodes the value as a 2's-complement-binary-integer into
        ///     the minimum number of octets required to accommodate the encoding,
        ///     and requires an explicit length encoding (typically a single octet)
        ///     as specified in later procedures.
        /// </summary>
        protected virtual int encodeUnconstraintNumber(long val, BitArrayOutputStream stream)
        {
            int result = 0;
            int intLen = CoderUtils.getIntegerLength(val);

            result += encodeLengthDeterminant(intLen, stream);
            doAlign(stream);
            result += encodeIntegerValueAsBytes(val, stream);
            return(result);
        }
コード例 #5
0
ファイル: BEREncoder.cs プロジェクト: xj0229/gsf
        protected internal int encodeIntegerValue(long val, Stream stream)
        {
            int resultSize = CoderUtils.getIntegerLength(val);

            for (int i = 0; i < resultSize; i++)
            {
                stream.WriteByte((byte)val);
                val = val >> 8;
            }
            return(resultSize);
        }
コード例 #6
0
        protected virtual int encodeIntegerValueAsBytes(long val, Stream stream)
        {
            int integerSize = CoderUtils.getIntegerLength(val);

            for (int i = integerSize - 1; i >= 0; i--)
            {
                long valueTmp = val >> (8 * i);
                stream.WriteByte((byte)valueTmp);
            }
            return(integerSize);
        }
コード例 #7
0
        /// <summary>
        ///     Encoding of a semi-constrained whole number
        ///     ITU-T X.691. 10.7.
        ///     NOTE – (Tutorial) This procedure is used when a lower bound can be
        ///     identified but not an upper bound. The encoding procedure places
        ///     the offset from the lower bound into the minimum number of octets
        ///     as a non-negative-binary-integer, and requires an explicit length
        ///     encoding (typically a single octet) as specified in later procedures.
        /// </summary>
        protected virtual int encodeSemiConstraintNumber(int val, int min, BitArrayOutputStream stream)
        {
            int result      = 0;
            int narrowedVal = val - min;
            int intLen      = CoderUtils.getIntegerLength(narrowedVal);

            result += encodeLengthDeterminant(intLen, stream);
            doAlign(stream);
            result += encodeIntegerValueAsBytes(narrowedVal, stream);
            return(result);
        }