Example #1
0
 public void TestAllTypesHaveParseInfo()
 {
     foreach (var isoType in (IsoType[])Enum.GetValues(typeof(IsoType)))
     {
         FieldParseInfo fpi = FieldParseInfo.GetInstance(isoType, isoType.Length(), Encoding.UTF8);
         Assert.NotNull(fpi);
     }
 }
 public CompositeField AddParser(FieldParseInfo fpi)
 {
     if (parsers == null)
     {
         parsers = new List <FieldParseInfo>(4);
     }
     parsers.Add(fpi);
     return(this);
 }
        private void TestFieldType(IsoType type,
                                   FieldParseInfo fieldParser,
                                   int offset1,
                                   int offset2)
        {
            var bigintCodec = new BigIntBcdCodec();
            var longCodec   = new LongBcdCodec();
            var mfact       = new MessageFactory <IsoMessage>();
            var tmpl        = new IsoMessage
            {
                Binary = true,
                Type   = 0x200
            };

            tmpl.SetValue(2,
                          1234567890L,
                          longCodec,
                          type,
                          0);
            tmpl.SetValue(3,
                          b29,
                          bigintCodec,
                          type,
                          0);
            mfact.AddMessageTemplate(tmpl);
            mfact.SetCustomField(2,
                                 longCodec);
            mfact.SetCustomField(3,
                                 bigintCodec);
            var parser = new Dictionary <int, FieldParseInfo>
            {
                { 2, fieldParser },
                { 3, fieldParser }
            };

            mfact.SetParseMap(0x200,
                              parser);
            mfact.UseBinaryMessages = true;

            //Test encoding
            tmpl = mfact.NewMessage(0x200);
            var buf     = tmpl.WriteData();
            var message = HexCodec.HexEncode(buf,
                                             0,
                                             buf.Length);

            Console.WriteLine("MESSAGE: " + message);
            for (var i = 0; i < 5; i++)
            {
                var b = longData2[i];
                Assert.Equal(b,
                             buf[i + offset1]);
            }

            for (var i = 0; i < 15; i++)
            {
                Assert.Equal(bigintData1[i],
                             buf[i + offset2]);
            }

            //Test parsing
            tmpl = mfact.ParseMessage(buf,
                                      0);
            Assert.Equal(1234567890L,
                         tmpl.GetObjectValue(2));
            Assert.Equal(b29,
                         tmpl.GetObjectValue(3));
        }
Example #4
0
        /// <summary>
        /// Parses a byte buffer containing an ISO8583 message. The buffer must
        /// not include the length header. If it includes the ISO message header,
        /// then its length must be specified so the message type can be found.
        /// </summary>
        /// <param name="mti_bitmap">The byte buffer containing the message, starting
        /// at the ISO header or the message type.</param>
        /// <param name="isoHeaderLength">Specifies the position at which the message
        /// type is located, which is algo the length of the ISO header.</param>
        /// <param name="encoder">The encoder to use for reading string values.</param>
        /// <returns>The parsed message.</returns>
        private IsoMessage ParseMessage(CaseBusiness.CC.Global.MensagemInterfaceDados interfaceDados, Byte[] mti_bitmap, Byte[] messageData, Int32 isoHeaderLength, Encoding encoder, ParseMode parseMode)
        {
            Int32 pos = 0;

            IsoMessage m = (isoHeaderLength > 0) ? new IsoMessage(interfaceDados.MensagemInterfaceHeader.TipoCodificacao, encoder.GetString(mti_bitmap, 0, isoHeaderLength)) : new IsoMessage(EncodingType.NotDefined, null);

            m.isValid     = true;
            m.MapFields   = interfaceDados.MensagemInterfaceHeader.MapaCamposVAR;
            m.IdInterface = interfaceDados.MensagemInterfaceHeader.IdMensagemInterfaceHeader;

            Int32 type = ((mti_bitmap[isoHeaderLength] - 48) << 12) | ((mti_bitmap[isoHeaderLength + 1] - 48) << 8) | ((mti_bitmap[isoHeaderLength + 2] - 48) << 4) | (mti_bitmap[isoHeaderLength + 3] - 48);

            m.Type = type;

            //Parse the bitmap
            BitArray bs = ((HexByteValue(mti_bitmap[isoHeaderLength + ConfigParser.ISOLengthMTI]) & 8) > 0) ? new BitArray(128) : new BitArray(64);

            Int32 tamanhoMensagemAtePrimeiroMapaDeBits = ConfigParser.ISOLengthMTI + ConfigParser.ISOLengthBitMap;

            for (Int32 i = isoHeaderLength + ConfigParser.ISOLengthMTI; i < isoHeaderLength + tamanhoMensagemAtePrimeiroMapaDeBits; i++)
            {
                Int32 hex = HexByteValue(mti_bitmap[i]);
                bs.Set(pos++, (hex & 8) > 0);
                bs.Set(pos++, (hex & 4) > 0);
                bs.Set(pos++, (hex & 2) > 0);
                bs.Set(pos++, (hex & 1) > 0);
            }

            //Extended bitmap
            if (bs.Get(0))
            {
                Int32 tamanhoMensagemAteSegundoMapaDeBits = tamanhoMensagemAtePrimeiroMapaDeBits + ConfigParser.ISOLengthBitMap;

                for (Int32 i = isoHeaderLength + tamanhoMensagemAtePrimeiroMapaDeBits; i < isoHeaderLength + tamanhoMensagemAteSegundoMapaDeBits; i++)
                {
                    Int32 hex = HexByteValue(mti_bitmap[i]);
                    bs.Set(pos++, (hex & 8) > 0);
                    bs.Set(pos++, (hex & 4) > 0);
                    bs.Set(pos++, (hex & 2) > 0);
                    bs.Set(pos++, (hex & 1) > 0);
                }
            }

            pos        = 0;
            m.BitArray = bs;

            foreach (KeyValuePair <Int32, FieldParseInfo> i in interfaceDados.MensagemInterfaceHeader.MapaCamposVAR)
            {
                try
                {
                    if (!bs.Get(i.Key - 1)) //Sу efetua checagem se o field do mapa existe na mensagem, caso nгo tenha, pula pro proximo field ou gera exception e pula pro proximo. Class BitArray nгo tem opзгo para checar se um bit estб ligado/desligado
                    {
                        continue;
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    continue;
                }

                FieldParseInfo fpi = i.Value;
                IsoValue       val = fpi.Parse(interfaceDados.MensagemInterfaceHeader.TipoCodificacao, messageData, pos, encoder, false);

                if (val.OriginalValue != null)
                {
                    if (parseMode == ParseMode.Complete)
                    {
                        if (fpi.SubDataElements_Fields != null)
                        {
                            if (fpi.SubDataElements_Fields.Count > 0)
                            {
                                val = parseDataElement_Field(interfaceDados.MensagemInterfaceHeader.TipoCodificacao, fpi.Base, fpi.SubDataElements_Fields, val, encoder, ref pos);
                            }

                            if (val == null)
                            {
                                m.isValid = false;
                                return(m);
                            }
                        }
                    }

                    if (interfaceDados.MensagemInterfaceHeader.TipoCodificacao == EncodingType.EBCDIC && fpi.Base == ConfigParser.Base.Hexadecimal)
                    {
                        pos += val.Length / 2;
                    }
                    else
                    {
                        pos += val.Length;
                    }

                    if (val.Type == IsoType.LLVAR)
                    {
                        pos += 2;
                    }
                    else if (val.Type == IsoType.LLLVAR)
                    {
                        pos += 3;
                    }

                    m.SetField(i.Key, val);

                    if (fpi.Required && !bs.Get(i.Key - 1))
                    {
                        m.isValid = false;
                    }
                }
            }

            return(m);
        }