Example #1
0
        public override IsoValue ParseBinary(int field,
                                             sbyte[] buf,
                                             int pos,
                                             ICustomField custom)
        {
            var sbytes = buf;

            if (pos < 0)
            {
                throw new ParseException($"Invalid bin LLLVAR field {field} pos {pos}");
            }

            if (pos + 2 > buf.Length)
            {
                throw new ParseException($"Insufficient data for bin LLLVAR header, field {field} pos {pos}");
            }

            var len = (sbytes[pos] & 0x0f) * 100 + ((sbytes[pos + 1] & 0xf0) >> 4) * 10 + (sbytes[pos + 1] & 0x0f);

            if (len < 0)
            {
                throw new ParseException($"Invalid bin LLLVAR length {len}, field {field} pos {pos}");
            }

            if (len + pos + 2 > buf.Length)
            {
                throw new ParseException($"Insufficient data for bin LLLVAR field {field}, pos {pos}");
            }

            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    buf.ToString(pos + 2,
                                                 len,
                                                 Encoding)));
            }

            var v = new IsoValue(IsoType,
                                 custom.DecodeField(buf.ToString(pos + 2,
                                                                 len,
                                                                 Encoding)),
                                 custom);

            if (v.Value == null)
            {
                return(new IsoValue(IsoType,
                                    buf.ToString(pos + 2,
                                                 len,
                                                 Encoding.Default)));
            }
            return(v);
        }
        public override IsoValue Parse(int field,
                                       sbyte[] buf,
                                       int pos,
                                       ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid ALPHA/NUM field {field} position {pos}");
            }
            if (pos + Length > buf.Length)
            {
                throw new ParseException(
                          $"Insufficient data for {IsoType} field {field} of length {Length}, pos {pos}");
            }
            try
            {
                var v = buf.BytesToString(pos,
                                          Length,
                                          Encoding);

                if (v.Length != Length)
                {
                    v = buf.BytesToString(pos,
                                          buf.Length - pos,
                                          Encoding).Substring(0,
                                                              Length);
                }

                if (custom == null)
                {
                    return(new IsoValue(IsoType,
                                        v,
                                        Length));
                }

                var decoded = custom.DecodeField(v);
                return(decoded == null
                    ? new IsoValue(IsoType,
                                   v,
                                   Length)
                    : new IsoValue(IsoType,
                                   decoded,
                                   Length,
                                   custom));
            }
            catch (Exception)
            {
                throw new ParseException(
                          $"Insufficient data for {IsoType} field {field} of length {Length}, pos {pos}");
            }
        }
Example #3
0
        public override IsoValue ParseBinary(int field,
                                             sbyte[] buf,
                                             int pos,
                                             ICustomField custom)
        {
            var sbytes = buf;

            if (pos < 0)
            {
                throw new ParseException($"Invalid bin LLVAR field {field} pos {pos}");
            }

            if (pos + 1 > buf.Length)
            {
                throw new ParseException($"Insufficient data for bin LLVAR header, field {field} pos {pos}");
            }

            var len = ((sbytes[pos] & 0xf0) >> 4) * 10 + (sbytes[pos] & 0x0f);

            if (len < 0)
            {
                throw new ParseException($"Invalid bin LLVAR length {len}, field {field} pos {pos}");
            }

            if (len + pos + 1 > buf.Length)
            {
                throw new ParseException($"Insufficient data for bin LLVAR field {field}, pos {pos}");
            }

            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    buf.SignedBytesToString(pos + 1,
                                                            len,
                                                            Encoding)));
            }

            var dec = custom.DecodeField(buf.SignedBytesToString(pos + 1,
                                                                 len,
                                                                 Encoding));

            return(dec == null
                ? new IsoValue(IsoType,
                               buf.SignedBytesToString(pos + 1,
                                                       len,
                                                       Encoding))
                : new IsoValue(IsoType,
                               dec,
                               custom));
        }
Example #4
0
        public override IsoValue Parse(int field,
                                       sbyte[] buf,
                                       int pos,
                                       ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid BINARY field {field} position {pos}");
            }

            if (pos + Length * 2 > buf.Length)
            {
                throw new ParseException($"Insufficient data for BINARY field {field} of length {Length}, pos {pos}");
            }

            var s = buf.ToString(pos,
                                 Length * 2,
                                 Encoding.Default);
            //var binval = HexCodec.HexDecode(Encoding.ASCII.GetString(buf,
            //    pos,
            //    Length * 2));

            var binval = HexCodec.HexDecode(s);

            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    binval,
                                    binval.Length));
            }

            s = buf.ToString(pos,
                             Length * 2,
                             Encoding);
            //var dec = custom.DecodeField(Encoding.GetString(buf,
            //    pos,
            //    Length * 2));
            var dec = custom.DecodeField(s);

            return(dec == null
                ? new IsoValue(IsoType,
                               binval,
                               binval.Length)
                : new IsoValue(IsoType,
                               dec,
                               Length,
                               custom));
        }
Example #5
0
        public override IsoValue ParseBinary(int field,
                                             sbyte[] buf,
                                             int pos,
                                             ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid bin ALPHA field {field} position {pos}");
            }
            if (pos + Length > buf.Length)
            {
                throw new ParseException(
                          $"Insufficient data for bin {IsoType} field {field} of length {Length}, pos {pos}");
            }
            try
            {
                string v;
                if (custom == null)
                {
                    v = buf.SbyteString(pos,
                                        Length,
                                        Encoding);
                    return(new IsoValue(IsoType,
                                        v,
                                        Length));
                }

                v = buf.SbyteString(pos,
                                    Length,
                                    Encoding);

                var decoded = custom.DecodeField(v);
                return(decoded == null
                    ? new IsoValue(IsoType,
                                   v,
                                   Length)
                    : new IsoValue(IsoType,
                                   decoded,
                                   Length,
                                   custom));
            }
            catch (Exception)
            {
                throw new ParseException(
                          $"Insufficient data for {IsoType} field {field} of length {Length}, pos {pos}");
            }
        }
Example #6
0
        public override IsoValue ParseBinary(int field,
                                             sbyte[] buf,
                                             int pos,
                                             ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid BINARY field {field} position {pos}");
            }
            if (pos + Length > buf.Length)
            {
                throw new ParseException($"Insufficient data for BINARY field {field} of length {Length}, pos {pos}");
            }
            var v      = new sbyte[Length];
            var sbytes = buf;

            Array.Copy(sbytes,
                       pos,
                       v,
                       0,
                       Length);
            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    v,
                                    Length));
            }
            var dec = custom.DecodeField(HexCodec.HexEncode(v,
                                                            0,
                                                            v.Length));

            return(dec == null
                ? new IsoValue(IsoType,
                               v,
                               Length)
                : new IsoValue(IsoType,
                               dec,
                               Length,
                               custom));
        }
Example #7
0
        public override IsoValue Parse(int field,
                                       sbyte[] buf,
                                       int pos,
                                       ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid LLBIN field {field} position {pos}");
            }
            if (pos + 2 > buf.Length)
            {
                throw new ParseException($"Invalid LLBIN field {field} position {pos}");
            }
            var len = DecodeLength(buf,
                                   pos,
                                   2);

            if (len < 0)
            {
                throw new ParseException($"Invalid LLBIN field {field} length {len} pos {pos}");
            }

            if (len + pos + 2 > buf.Length)
            {
                throw new ParseException(
                          $"Insufficient data for LLBIN field {field}, pos {pos} (LEN states '{buf.SignedBytesToString(pos, 2, Encoding.Default)}')");
            }

            var binval = len == 0
                ? new sbyte[0]
                : HexCodec.HexDecode(buf.SignedBytesToString(pos + 2,
                                                             len,
                                                             Encoding.Default));

            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    binval,
                                    binval.Length));
            }
            var binaryField = custom as ICustomBinaryField;

            if (binaryField != null)
            {
                try
                {
                    var dec = binaryField.DecodeBinaryField(buf,
                                                            pos + 2,
                                                            len);
                    if (dec == null)
                    {
                        return(new IsoValue(IsoType,
                                            binval,
                                            binval.Length));
                    }
                    return(new IsoValue(IsoType,
                                        dec,
                                        0,
                                        custom));
                }
                catch (Exception)
                {
                    throw new ParseException(
                              $"Insufficient data for LLBIN field {field}, pos {pos} (LEN states '{buf.SignedBytesToString(pos, 2, Encoding.Default)}')");
                }
            }
            try
            {
                var dec = custom.DecodeField(buf.SignedBytesToString(pos + 2,
                                                                     len,
                                                                     Encoding.Default));
                return(dec == null
                    ? new IsoValue(IsoType,
                                   binval,
                                   binval.Length)
                    : new IsoValue(IsoType,
                                   dec,
                                   binval.Length,
                                   custom));
            }
            catch (Exception)
            {
                throw new ParseException(
                          $"Insufficient data for LLBIN field {field}, pos {pos} (LEN states '{buf.SignedBytesToString(pos, 2, Encoding.Default)}')");
            }
        }
Example #8
0
        public override IsoValue ParseBinary(int field,
                                             sbyte[] buf,
                                             int pos,
                                             ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid bin LLBIN field {field} position {pos}");
            }
            if (pos + 1 > buf.Length)
            {
                throw new ParseException($"Insufficient bin LLBIN header field {field}");
            }

            var sbytes = buf;

            var l = ((sbytes[pos] & 0xf0) >> 4) * 10 + (sbytes[pos] & 0x0f);

            if (l < 0)
            {
                throw new ParseException($"Invalid bin LLBIN length {l} pos {pos}");
            }
            if (l + pos + 1 > buf.Length)
            {
                throw new ParseException(
                          $"Insufficient data for bin LLBIN field {field}, pos {pos}: need {l}, only {buf.Length} available");
            }
            var v = new sbyte[l];

            Array.Copy(sbytes,
                       pos + 1,
                       v,
                       0,
                       l);
            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    v));
            }
            if (custom is ICustomBinaryField)
            {
                try
                {
                    var dec = ((ICustomBinaryField)custom).DecodeBinaryField(sbytes,
                                                                             pos + 1,
                                                                             l);
                    return(dec == null
                        ? new IsoValue(IsoType,
                                       v,
                                       v.Length)
                        : new IsoValue(IsoType,
                                       dec,
                                       l,
                                       custom));
                }
                catch (Exception)
                {
                    throw new ParseException($"Insufficient data for LLBIN field {field}, pos {pos} length {l}");
                }
            }
            {
                var dec = custom.DecodeField(HexCodec.HexEncode(v,
                                                                0,
                                                                v.Length));
                return(dec == null
                    ? new IsoValue(IsoType,
                                   v)
                    : new IsoValue(IsoType,
                                   dec,
                                   custom));
            }
        }
Example #9
0
        public override IsoValue Parse(int field,
                                       sbyte[] buf,
                                       int pos,
                                       ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid LLVAR field {field} {pos}");
            }
            if (pos + 2 > buf.Length)
            {
                throw new ParseException($"Insufficient data for LLVAR header, pos {pos}");
            }
            var len = DecodeLength(buf,
                                   pos,
                                   2);

            if (len < 0)
            {
                throw new ParseException($"Invalid LLVAR length {len}, field {field} pos {pos}");
            }
            if (len + pos + 2 > buf.Length)
            {
                throw new ParseException($"Insufficient data for LLVAR field {field}, pos {pos}");
            }

            string v;

            try
            {
                v = len == 0
                    ? ""
                    : buf.SignedBytesToString(pos + 2,
                                              len,
                                              Encoding);
            }
            catch (Exception)
            {
                throw new ParseException($"Insufficient data for LLVAR header, field {field} pos {pos}");
            }

            //This is new: if the String's length is different from the specified length in the
            //buffer, there are probably some extended characters. So we create a String from
            //the rest of the buffer, and then cut it to the specified length.
            if (v.Length != len)
            {
                v = buf.SignedBytesToString(pos + 2,
                                            buf.Length - pos - 2,
                                            Encoding).Substring(0,
                                                                len);
            }

            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    v,
                                    len));
            }

            var decoded = custom.DecodeField(v);

            //If decode fails, return string; otherwise use the decoded object and its codec
            return(decoded == null
                ? new IsoValue(IsoType,
                               v,
                               len)
                : new IsoValue(IsoType,
                               decoded,
                               len,
                               custom));
        }
Example #10
0
        public override IsoValue Parse(int field,
                                       sbyte[] buf,
                                       int pos,
                                       ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid LLLBIN field {field} pos {pos}");
            }
            if (pos + 3 > buf.Length)
            {
                throw new ParseException($"Insufficient LLLBIN header field {field}");
            }

            var l = DecodeLength(buf,
                                 pos,
                                 3);

            if (l < 0)
            {
                throw new ParseException($"Invalid LLLBIN length {l} field {field} pos {pos}");
            }
            if (l + pos + 3 > buf.Length)
            {
                throw new ParseException($"Insufficient data for LLLBIN field {field}, pos {pos}");
            }

            var binval = l == 0
                ? new sbyte[0]
                : HexCodec.HexDecode(buf.SignedBytesToString(pos + 3,
                                                             l,
                                                             Encoding.Default));

            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    binval,
                                    binval.Length));
            }
            var customBinaryField = custom as ICustomBinaryField;

            if (customBinaryField != null)
            {
                try
                {
                    var dec = customBinaryField.DecodeBinaryField(buf,
                                                                  pos + 3,
                                                                  l);
                    return(dec == null
                        ? new IsoValue(IsoType,
                                       binval,
                                       binval.Length)
                        : new IsoValue(IsoType,
                                       dec,
                                       0,
                                       custom));
                }
                catch (Exception)
                {
                    throw new ParseException($"Insufficient data for LLLBIN field {field}, pos {pos}");
                }
            }
            try
            {
                var dec = custom.DecodeField(l == 0
                    ? ""
                    : buf.SignedBytesToString(pos + 3,
                                              l,
                                              Encoding.Default));
                return(dec == null
                    ? new IsoValue(IsoType,
                                   binval,
                                   binval.Length)
                    : new IsoValue(IsoType,
                                   dec,
                                   l,
                                   custom));
            }
            catch (Exception)
            {
                throw new Exception($"Insufficient data for LLLBIN field {field}, pos {pos}");
            }
        }
Example #11
0
        public override IsoValue ParseBinary(int field,
                                             sbyte[] buf,
                                             int pos,
                                             ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid bin LLLLBIN field {field} pos {pos}");
            }
            if (pos + 2 > buf.Length)
            {
                throw new ParseException($"Insufficient LLLLBIN header field {field}");
            }

            int l = ((buf[pos] & 0xf0) * 1000) + ((buf[pos] & 0x0f) * 100)
                    + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f);

            if (l < 0)
            {
                throw new ParseException($"Invalid LLLLBIN length {l} field {field} pos {pos}");
            }
            if (l + pos + 2 > buf.Length)
            {
                throw new ParseException(
                          $"Insufficient data for bin LLLLBIN field {field}, pos {pos} requires {l}, only {buf.Length - pos + 1} available");
            }

            var v = new sbyte[l];

            Array.Copy(buf,
                       pos + 2,
                       v,
                       0,
                       l);
            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    v));
            }
            var customBinaryField = custom as ICustomBinaryField;

            if (customBinaryField != null)
            {
                try
                {
                    var dec = customBinaryField.DecodeBinaryField(buf,
                                                                  pos + 2,
                                                                  l);
                    return(dec == null
                        ? new IsoValue(IsoType,
                                       v,
                                       v.Length)
                        : new IsoValue(IsoType,
                                       dec,
                                       l,
                                       custom));
                }
                catch (Exception)
                {
                    throw new ParseException($"Insufficient data for LLLLBIN field {field}, pos {pos}");
                }
            }
            {
                var dec = custom.DecodeField(HexCodec.HexEncode(v,
                                                                0,
                                                                v.Length));
                return(dec == null
                    ? new IsoValue(IsoType,
                                   v)
                    : new IsoValue(IsoType,
                                   dec,
                                   custom));
            }
        }
Example #12
0
        public override IsoValue Parse(int field,
                                       sbyte[] buf,
                                       int pos,
                                       ICustomField custom)
        {
            if (pos < 0)
            {
                throw new ParseException($"Invalid LLLLVAR field {field} {pos}");
            }
            if (pos + 4 > buf.Length)
            {
                throw new ParseException($"Insufficient data for LLLLVAR header, pos {pos}");
            }
            var len = DecodeLength(buf,
                                   pos,
                                   4);

            if (len < 0)
            {
                throw new ParseException($"Invalid LLLLVAR length {len}, field {field} pos {pos}");
            }
            if (len + pos + 4 > buf.Length)
            {
                throw new ParseException($"Insufficient data for LLLLVAR field {field}, pos {pos}");
            }

            string v;

            try
            {
                v = len == 0
                    ? ""
                    : buf.SbyteString(pos + 4,
                                      len,
                                      Encoding);
            }
            catch (Exception)
            {
                throw new ParseException($"Insufficient data for LLLLVAR header, field {field} pos {pos}");
            }

            //This is new: if the String's length is different from the specified
            // length in the buffer, there are probably some extended characters.
            // So we create a String from the rest of the buffer, and then cut it to
            // the specified length.
            if (v.Length != len)
            {
                v = buf.SbyteString(pos + 4,
                                    buf.Length - pos - 4,
                                    Encoding).Substring(0,
                                                        len);
            }
            if (custom == null)
            {
                return(new IsoValue(IsoType,
                                    v,
                                    len));
            }
            var dec = custom.DecodeField(v);

            return(dec == null
                ? new IsoValue(IsoType,
                               v,
                               len)
                : new IsoValue(IsoType,
                               dec,
                               len,
                               custom));
        }