Beispiel #1
0
        /// <summary>
        /// It decodes the data.
        /// </summary>
        /// <param name="parserContext">
        /// It's the parser context holding the data to be parsed.
        /// </param>
        /// <param name="length">
        /// It's the length of the data to get from the parser context.
        /// </param>
        /// <returns>
        /// A byte array with the decoded data.
        /// </returns>
        public byte[] Decode(ref ParserContext parserContext, int length)
        {
            if (parserContext.DataLength < (length << 1))
            {
                throw new ArgumentException(SR.InsufficientData, "length");
            }

            byte[] result = new byte[length];
            byte[] buffer = parserContext.GetBuffer();
            int    offset = parserContext.LowerDataBound;

            for (int i = offset + (length << 1) - 1; i >= offset; i--)
            {
                int right = buffer[i] > 0x40 ? 10 + (buffer[i] > 0x60 ?
                                                     buffer[i] - 0x61 : buffer[i] - 0x41) : buffer[i] - 0x30;
                int left = buffer[--i] > 0x40 ? 10 + (buffer[i] > 0x60 ?
                                                      buffer[i] - 0x61 : buffer[i] - 0x41) : buffer[i] - 0x30;

                result[(i - offset) >> 1] = ( byte)((left << 4) | right);
            }

            parserContext.Consumed(length << 1);

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// It decodes the data.
        /// </summary>
        /// <param name="parserContext">
        /// It's the parser context holding the data to be parsed.
        /// </param>
        /// <param name="length">
        /// It's the length of the data to get from the parser context.
        /// </param>
        /// <returns>
        /// A byte array with the decoded data.
        /// </returns>
        public byte[] Decode(ref ParserContext parserContext, int length)
        {
            if (parserContext.DataLength < ((length + 1) >> 1))
            {
                throw new ArgumentException(SR.InsufficientData, "length");
            }

            byte[] result = new byte[length];
            byte[] buffer = parserContext.GetBuffer();
            int    offset = parserContext.LowerDataBound;
            int    start  = (((length & 1) == 1) && _leftPadded) ? 1 : 0;

            for (int i = start; i < length + start; i++)
            {
                int shift = ((i & 1) == 1 ? 0 : 4);

                byte c = ( byte )((buffer[offset + (i >> 1)] >> shift) & 0x0F);

                if (c < 10)
                {
                    c += 0x30;
                }
                else
                {
                    c += 0x37;
                }

                result[i - start] = c;
            }

            parserContext.Consumed((length + 1) >> 1);

            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Convierte los datos formateados del indicador de largo
        /// de los datos del componente de mensajería.
        /// </summary>
        /// <param name="parserContext">
        /// Es el contexto de análisis y construcción de mensajes donde
        /// reside la información a decodificar.
        /// </param>
        /// <returns>
        /// Es el largo de los datos del componente de mensajería.
        /// </returns>
        public int Decode(ref ParserContext parserContext)
        {
            // Check available data.
            if (parserContext.DataLength < (_lengthsIndex + 1))
            {
                throw new ArgumentException(SR.InsufficientData, "parserContext");
            }

            int length = 0;

            byte[] buffer = parserContext.GetBuffer();
            int    offset = parserContext.LowerDataBound;

            // Decode length.
            for (int i = offset; i < (offset + _lengthsIndex + 1); i++)
            {
                if ((buffer[i] < 0x30) || (buffer[i] > 0x39))
                {
                    throw new MessagingException(SR.InvalidByteLengthDetected(buffer[i]));
                }

                length = length * 10 + buffer[i] - 0x30;
            }

            // Consume parser context data.
            parserContext.Consumed(_lengthsIndex + 1);

            return(length);
        }
        /// <summary>
        /// Gets the encoded length from the parser context.
        /// </summary>
        /// <param name="parserContext">
        /// It's the parser context holding the data to be parsed.
        /// </param>
        /// <returns>
        /// The length parsed from the parser context.
        /// </returns>
        public int Decode(ref ParserContext parserContext)
        {
            // Check available data.
            if (parserContext.DataLength < (_lengthsIndex + 1))
            {
                throw new ArgumentException(SR.InsufficientData, "parserContext");
            }

            int length = 0;
            int value;

            byte[] buffer = parserContext.GetBuffer();
            int    offset = parserContext.LowerDataBound;

            // Decode length.
            for (int i = offset; i <= (offset + _lengthsIndex); i++)
            {
                value = (buffer[i] & 0xF0) >> 4;
                if (value > 9)
                {
                    throw new MessagingException(SR.InvalidLengthDetected(value));
                }
                length = length * 10 + value;

                value = buffer[i] & 0x0F;
                if (value > 9)
                {
                    throw new MessagingException(SR.InvalidLengthType(value));
                }
                length = length * 10 + value;
            }

            // Consume parser context data.
            parserContext.Consumed(_lengthsIndex + 1);

            return(length);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the encoded length from the parser context.
        /// </summary>
        /// <param name="parserContext">
        /// It's the parser context holding the data to be parsed.
        /// </param>
        /// <returns>
        /// The length parsed from the parser context.
        /// </returns>
        public int Decode(ref ParserContext parserContext)
        {
            // Check available data.
            if (parserContext.DataLength < (_lengthsIndex + 1))
            {
                throw new ArgumentException(SR.InsufficientData, "parserContext");
            }

            int length = 0;

            byte[] buffer = parserContext.GetBuffer();
            int    offset = parserContext.LowerDataBound;

            // Decode length.
            for (int i = offset; i <= (offset + _lengthsIndex); i++)
            {
                length = (length * 256) + (( int )buffer[i]);
            }

            // Consume parser context data.
            parserContext.Consumed(_lengthsIndex + 1);

            return(length);
        }