public override byte[] Encode(ISOComponent component) { try { byte[] data = component.GetBytes(); // Check length int encodedLength = _prefix.EncodedLength; if (encodedLength == 0) { if (data.Length != Length) { throw new ISOException(string.Format("Binary data length is not the same as encoder length ({0}/{1}", data.Length, Length)); } } byte[] retValue = new byte[_translator.EncodedLength(data.Length) + encodedLength]; _prefix.EncodeLength(data.Length, retValue); _translator.Translate(data, retValue, encodedLength); return(retValue); } catch (Exception e) { throw new ISOException(string.Format("Exception encoding field {0}", component.Key), e); } }
public override byte[] Encode(ISOComponent component) { try { // if data is represented as byte[], we need to convert it to ASCII, otherwise take it as is String data; if (component.Value is byte[]) { data = System.Text.ASCIIEncoding.ASCII.GetString((byte[])component.Value); } else { data = component.Value.ToString(); } // Check the length if (data.Length > Length) { throw new ISOException(string.Format("{0}: Field length {1} is too long. Max {2}", component.Key, data.Length, Length)); } // Pad, Prefix and Translate string paddedData = _padder.Pad(data, Length); byte[] rawData = new byte[_prefix.EncodedLength + _translator.EncodedLength(Length)]; _prefix.EncodeLength(paddedData.Length, rawData); _translator.Translate(paddedData, rawData, _prefix.EncodedLength); return(rawData); } catch (Exception e) { throw new ISOException(string.Format("Exception encoding field {0}", component.Key), e); } }
public string GetFieldName(ISOComponent component, int fieldNumber) { if (_fields != null) { if (_fields[fieldNumber] is ISOFieldEncoder) { return(((ISOFieldEncoder)_fields[fieldNumber]).Name); } } return(string.Empty); }
public string Dump(bool includeBitmap = false) { StringBuilder sb = new StringBuilder(); // Header if (Header != null) { sb.AppendFormat("<iso header=\"{0}\">\n", Header.ToString()); } else { sb.AppendLine("<iso>"); } // Bitmap if (_fields.ContainsKey(-1)) { sb.AppendFormat("\t<bitmap>{0}</bitmap>\n", _fields[-1].ToString()); } // MTI if (_fields.ContainsKey(0)) { sb.AppendFormat("\t<mti>{0}</mti>\n", ((ISOComponent)_fields[0]).Value); } // Fields IList <int> keys = _fields.Keys; for (int i = 0; i < keys.Count; i++) { ISOComponent c = (ISOComponent)_fields[keys[i]]; if ((int)c.Key > 0 && c is ISOField) { sb.AppendFormat("\t<field id=\"{0}\" value=\"{1}\" />\n", c.Key, c.Value); } else if ((int)c.Key > 0 && c is ISOBinaryField) { sb.AppendFormat("\t<field id=\"{0}\" value=\"{1}\" />\n", c.Key, Utils.HexString((byte[])c.Value)); } } sb.AppendLine("</iso>"); return(sb.ToString()); }
public override void Add(ISOComponent component) { if (component != null) { int key = (int)component.Key; if (_fields.ContainsKey(key)) // if exists, remove the old field { _fields.Remove(key); } _fields.Add(key, component); if (_biggestField < key) { _biggestField = key; } _changed = true; } }
public override int Decode(ISOComponent component, byte[] data, int offset) { try { int length = this._prefix.DecodeLength(data, offset); if (length == -1) { length = Length; } else if (length > 0 && length > Length) { throw new ISOException(string.Format("{0}: Field length {1} is too long. Max {2}", component.Key, length, Length)); } int lenLen = _prefix.EncodedLength; string decodedStr = _translator.TranslateBack(data, offset + lenLen, length); component.Value = decodedStr; return(lenLen + _translator.EncodedLength(length)); } catch (Exception e) { throw new ISOException(string.Format("Exception decoding field {0}", component.Key), e); } }
public override int Decode(ISOComponent component, byte[] data, int offset) { try { int len = _prefix.DecodeLength(data, offset); if (len == -1) { // if we don't know the length, use the max defined for the field len = Length; } else if (Length > 0 && len > Length) { throw new ISOException(string.Format("{0}: Field length {1} is too long. Max {2}", component.Key, len, Length)); } int lenLen = _prefix.EncodedLength; byte[] decoded = _translator.TranslateBack(data, offset + lenLen, len); component.Value = decoded; return(lenLen + _translator.EncodedLength(len)); } catch (Exception e) { throw new ISOException(string.Format("Exception decoding field {0}", component.Key), e); } }
public byte[] Encode(ISOComponent component) { // Loop in each field, encode it a put into a list, where the maximum number may be 128 List<byte[]> list = new List<byte[]>(128); SortedList<int, object> children = component.GetChildren(); if (children != null) { int len = 0; byte[] b; byte[] header = null; ISOComponent c; // if it's message get header length if (component is ISOMessage) { if (HeaderLength > 0 && ((ISOMessage)component).Header != null) { header = ((ISOMessage)component).Header.Value; if (header != null) len += header.Length; } } // MTI c = (ISOComponent)children[0]; if (FirstField > 0 && c != null) { b = ((ISOFieldEncoder)_fields[0]).Encode(c); len += b.Length; list.Add(b); } // Bitmap if (HasBitmap) { c = (ISOComponent)children[-1]; if (c != null) { b = GetBitmapEncoder().Encode(c); len += b.Length; list.Add(b); } } // Fields foreach (int k in children.Keys) { if (k >= FirstField) { if ((c = (ISOComponent)children[k]) != null) { ISOFieldEncoder fe = (ISOFieldEncoder)_fields[k]; if (fe == null) throw new ISOException(string.Format("No encoder defined for field {0}", k)); b = fe.Encode(c); len += b.Length; list.Add(b); } } } // TODO: ++ Process Extended Bitmap ++ // Build final message int count = 0; byte[] msg = new byte[len]; if (header != null) { Array.Copy(header, 0, msg, 0, header.Length); count += header.Length; } for (int i = 0; i < list.Count; i++) { Array.Copy(list[i], 0, msg, count, list[i].Length); count += list[i].Length; } return msg; } return null; }
public override byte[] Encode(ISOComponent component) { try { // if data is represented as byte[], we need to convert it to ASCII, otherwise take it as is String data; if (component.Value is byte[]) { data = System.Text.ASCIIEncoding.ASCII.GetString((byte[])component.Value); } else { data = component.Value.ToString(); } // Check the length if (data.Length > Length) throw new ISOException(string.Format("{0}: Field length {1} is too long. Max {2}", component.Key, data.Length, Length)); // Pad, Prefix and Translate string paddedData = _padder.Pad(data, Length); byte[] rawData = new byte[_prefix.EncodedLength + _translator.EncodedLength(paddedData.Length)]; _prefix.EncodeLength(paddedData.Length, rawData); _translator.Translate(paddedData, rawData, _prefix.EncodedLength); return rawData; } catch (Exception e) { throw new ISOException(string.Format("Exception encoding field {0}", component.Key), e); } }
public override object GetValue(int fieldNumber) { ISOComponent c = GetComponent(fieldNumber); return(c != null ? c.Value : null); }
public override byte[] Encode(ISOComponent component) { try { byte[] data = component.GetBytes(); // Check length int encodedLength = _prefix.EncodedLength; if (encodedLength == 0) { if (data.Length != Length) { throw new ISOException(string.Format("Binary data length is not the same as encoder length ({0}/{1}", data.Length, Length)); } } byte[] retValue = new byte[_translator.EncodedLength(data.Length) + encodedLength]; _prefix.EncodeLength(data.Length, retValue); _translator.Translate(data, retValue, encodedLength); return retValue; } catch (Exception e) { throw new ISOException(string.Format("Exception encoding field {0}", component.Key), e); } }
public override int Decode(ISOComponent component, byte[] data, int offset) { try { int len = _prefix.DecodeLength(data, offset); if (len == -1) { // if we don't know the length, use the max defined for the field len = Length; } else if (Length > 0 && len > Length) { throw new ISOException(string.Format("{0}: Field length {1} is too long. Max {2}", component.Key, len, Length)); } int lenLen = _prefix.EncodedLength; string decodedStr = _translator.TranslateBack(data, offset + lenLen, len); component.Value = decodedStr; return lenLen + _translator.EncodedLength(len); } catch (Exception e) { throw new ISOException(string.Format("Exception decoding field {0}", component.Key), e); } }
public int Decode(ISOComponent component, byte[] data) { int offset = 0; #region - HEADER - if (component is ISOMessage && HeaderLength > 0) { byte[] header = new byte[HeaderLength]; Array.Copy(data, 0, header, 0, HeaderLength); ((ISOMessage)component).Header = new ISOHeader(header); ((ISOMessage)component).Header.ASCIIEncoding = this.HeaderASCII; offset += HeaderLength; } #endregion #region - MTI - if (!(_fields[0] is ISOBitmapEncoder)) { ISOComponent mti = ((ISOFieldEncoder)_fields[0]).CreateComponent(0); offset += ((ISOFieldEncoder)_fields[0]).Decode(mti, data, offset); component.Add(mti); } #endregion #region - BITMAP - BitArray bitArray = null; int totalFields = _fields.Count; if (HasBitmap) { ISOBitmap bitmap = new ISOBitmap(-1); offset += GetBitmapEncoder().Decode(bitmap, data, offset); bitArray = (BitArray)bitmap.Value; component.Add(bitmap); totalFields = Math.Min(totalFields, bitArray.Length); } #endregion #region - FIELDS - for (int i = FirstField; i < totalFields; i++) { if (bitArray[i]) // bit array starts at 0 { if (_fields[i] == null) { throw new ISOException(string.Format("Field encoder not defined for {0}", i)); } try { ISOComponent c = ((ISOFieldEncoder)_fields[i]).CreateComponent(i); offset += ((ISOFieldEncoder)_fields[i]).Decode(c, data, offset); component.Add(c); } catch (ISOException e) { e = new ISOException(string.Format("Error decoding field {0}: {1} ({2}). Bytes consumed: {3} ", i, e.Message, e.InnerException.Message, offset)); throw; } } } #endregion // TODO: Do something if still there're bytes to process...? return(offset); }
public int Decode(ISOComponent component, byte[] data) { int offset = 0; #region - HEADER - if (component is ISOMessage && HeaderLength > 0) { byte[] header = new byte[HeaderLength]; Array.Copy(data, 0, header, 0, HeaderLength); ((ISOMessage)component).Header = new ISOHeader(header); ((ISOMessage)component).Header.ASCIIEncoding = this.HeaderASCII; offset += HeaderLength; } #endregion #region - MTI - if (!(_fields[0] is ISOBitmapEncoder)) { ISOComponent mti = ((ISOFieldEncoder)_fields[0]).CreateComponent(0); offset += ((ISOFieldEncoder)_fields[0]).Decode(mti, data, offset); component.Add(mti); } #endregion #region - BITMAP - BitArray bitArray = null; int totalFields = _fields.Count; if (HasBitmap) { ISOBitmap bitmap = new ISOBitmap(-1); offset += GetBitmapEncoder().Decode(bitmap, data, offset); bitArray = (BitArray)bitmap.Value; component.Add(bitmap); totalFields = Math.Min(totalFields, bitArray.Length); } #endregion #region - FIELDS - for (int i = FirstField; i < totalFields; i++) { if (bitArray[i]) { // bit array starts at 0 if (_fields[i] == null) throw new ISOException(string.Format("Field encoder not defined for {0}", i)); try { ISOComponent c = ((ISOFieldEncoder)_fields[i]).CreateComponent(i); offset += ((ISOFieldEncoder)_fields[i]).Decode(c, data, offset); component.Add(c); } catch (ISOException e) { e = new ISOException(string.Format("Error decoding field {0}: {1} ({2}). Bytes consumed: {3} ", i, e.Message, e.InnerException.Message, offset)); throw; } } } #endregion // TODO: Do something if still there're bytes to process...? return offset; }
public byte[] Encode(ISOComponent component) { // Loop in each field, encode it a put into a list, where the maximum number may be 128 List <byte[]> list = new List <byte[]>(128); SortedList <int, object> children = component.GetChildren(); if (children != null) { int len = 0; byte[] b; byte[] header = null; ISOComponent c; // if it's message get header length if (component is ISOMessage) { if (HeaderLength > 0 && ((ISOMessage)component).Header != null) { header = ((ISOMessage)component).Header.Value; if (header != null) { len += header.Length; } } } // MTI c = (ISOComponent)children[0]; if (FirstField > 0 && c != null) { b = ((ISOFieldEncoder)_fields[0]).Encode(c); len += b.Length; list.Add(b); } // Bitmap if (HasBitmap) { c = (ISOComponent)children[-1]; if (c != null) { b = GetBitmapEncoder().Encode(c); len += b.Length; list.Add(b); } } // Fields foreach (int k in children.Keys) { if (k >= FirstField) { if ((c = (ISOComponent)children[k]) != null) { ISOFieldEncoder fe = (ISOFieldEncoder)_fields[k]; if (fe == null) { throw new ISOException(string.Format("No encoder defined for field {0}", k)); } b = fe.Encode(c); len += b.Length; list.Add(b); } } } // TODO: ++ Process Extended Bitmap ++ // Build final message int count = 0; byte[] msg = new byte[len]; if (header != null) { Array.Copy(header, 0, msg, 0, header.Length); count += header.Length; } for (int i = 0; i < list.Count; i++) { Array.Copy(list[i], 0, msg, count, list[i].Length); count += list[i].Length; } return(msg); } return(null); }
public override void Add(ISOComponent component) { if (component != null) { int key = (int)component.Key; if (_fields.ContainsKey(key)) // if exists, remove the old field _fields.Remove(key); _fields.Add(key, component); if (_biggestField < key) _biggestField = key; _changed = true; } }
public virtual void Add(ISOComponent component) { throw new NotImplementedException("N/A at this point."); }
public string GetFieldName(ISOComponent component, int fieldNumber) { if (_fields != null) { if (_fields[fieldNumber] is ISOFieldEncoder) { return ((ISOFieldEncoder)_fields[fieldNumber]).Name; } } return string.Empty; }