public byte[] GetBytes() { byte[] RecordBytes = new byte[41]; byte[] RemoteNodeEBCDIC; byte[] LocalNodeEBCDIC; byte[] BufferSizeBytes; RemoteNodeEBCDIC = EbcdicEncoding.Convert(Encoding.ASCII, EbcdicEncoding.GetEncoding("EBCDIC-US"), Encoding.ASCII.GetBytes(RemoteNode.PadRight(8, ' '))); LocalNodeEBCDIC = EbcdicEncoding.Convert(Encoding.ASCII, EbcdicEncoding.GetEncoding("EBCDIC-US"), Encoding.ASCII.GetBytes(LocalNode.PadRight(8, ' '))); BufferSizeBytes = BitConverter.GetBytes(BufferSize); if (BitConverter.IsLittleEndian) { Array.Reverse(BufferSizeBytes); } RecordBytes[0] = 0x29; Array.Copy(LocalNodeEBCDIC, 0, RecordBytes, 1, 8); Array.Copy(RemoteNodeEBCDIC, 0, RecordBytes, 9, 8); RecordBytes[18] = 0x64; Array.Copy(BufferSizeBytes, 0, RecordBytes, 19, 2); RecordBytes[38] = 0x15; return(RecordBytes); }
private string ConvertDataResponse(byte[] allData, int startIndex, int endIndex, DataType type) { int len = (endIndex - startIndex) + 1; byte[] data = new byte[len]; for (int i = 0; i < data.Length; i++) { data[i] = allData[startIndex + i]; } if ((type == DataType.A) || (type == DataType.S)) { if (type == DataType.A) { bool isValid = false; foreach (byte b in data) { if (b != 0) { isValid = true; break; } } if (!isValid) { return(string.Empty); } } Encoding eC = EbcdicEncoding.GetEncoding(20838);//"EBCDIC-US" return(eC.GetString(data).Replace("\u0000", string.Empty)); } else if (type == DataType.B) { Array.Reverse(data); return(BitConverter.ToString(data)); } else { StringBuilder sb = new StringBuilder(); foreach (byte b in data) { sb.Append(b.ToString("X2")); } string hexString = sb.ToString(); string signString = hexString.Substring(hexString.Length - 1); hexString = hexString.Substring(0, hexString.Length - 1); if (signString.ToUpper() == "D") { hexString = "-" + hexString; } sb = null; return(hexString); } }
public byte[] GetBytes() { byte[] controlRecordBytes = new byte[33]; // encode control record bytes byte[] requestType = Encoding.ASCII.GetBytes(this.RequestType); byte[] senderName = Encoding.ASCII.GetBytes(this.SenderName); byte[] receiverName = Encoding.ASCII.GetBytes(this.ReceiverName); byte[] senderIP = BitConverter.GetBytes(Utils.stringToNjeIP(this.SenderIP)); byte[] receiverIP = BitConverter.GetBytes(Utils.stringToNjeIP(this.ReceiverIP)); requestType = EbcdicEncoding.Convert(Encoding.ASCII, EbcdicEncoding.GetEncoding("EBCDIC-US"), requestType); senderName = EbcdicEncoding.Convert(Encoding.ASCII, EbcdicEncoding.GetEncoding("EBCDIC-US"), senderName); receiverName = EbcdicEncoding.Convert(Encoding.ASCII, EbcdicEncoding.GetEncoding("EBCDIC-US"), receiverName); if (BitConverter.IsLittleEndian) { Array.Reverse(senderIP); Array.Reverse(receiverIP); } // create padded versions where necessary byte[] requestTypePad = { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 }; byte[] senderNamePad = { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 }; byte[] receiverNamePad = { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 }; Array.Copy(requestType, requestTypePad, requestType.Length <= 8 ? requestType.Length : 8); Array.Copy(senderName, senderNamePad, requestType.Length <= 8 ? senderName.Length : 8); Array.Copy(receiverName, receiverNamePad, requestType.Length <= 8 ? receiverName.Length : 8); // assemble control record Array.Copy(requestTypePad, 0, controlRecordBytes, 0, 8); Array.Copy(senderNamePad, 0, controlRecordBytes, 8, 8); Array.Copy(senderIP, 0, controlRecordBytes, 16, 4); Array.Copy(receiverNamePad, 0, controlRecordBytes, 20, 8); Array.Copy(receiverIP, 0, controlRecordBytes, 28, 4); controlRecordBytes[32] = ReasonCode; return(controlRecordBytes); }
public void ParseControlRecord(byte[] ControlRecordBytes) { if (ControlRecordBytes.Length != 33) { throw new FormatException("Invalid ControlRecord structure."); } // allocate arrays to ingest the record byte[] requestType = new byte[8]; byte[] senderName = new byte[8]; byte[] receiverName = new byte[8]; byte[] senderIP = new byte[4]; byte[] receiverIP = new byte[4]; Array.Copy(ControlRecordBytes, 0, requestType, 0, 8); Array.Copy(ControlRecordBytes, 8, senderName, 0, 8); Array.Copy(ControlRecordBytes, 20, receiverName, 0, 8); Array.Copy(ControlRecordBytes, 16, senderIP, 0, 4); Array.Copy(ControlRecordBytes, 28, senderIP, 0, 4); //de-EBCDIC-ize requestType = EbcdicEncoding.Convert(EbcdicEncoding.GetEncoding("EBCDIC-US"), Encoding.ASCII, requestType); senderName = EbcdicEncoding.Convert(EbcdicEncoding.GetEncoding("EBCDIC-US"), Encoding.ASCII, senderName); receiverName = EbcdicEncoding.Convert(EbcdicEncoding.GetEncoding("EBCDIC-US"), Encoding.ASCII, receiverName); this.RequestType = ASCIIEncoding.ASCII.GetString(requestType).Trim(); this.SenderName = ASCIIEncoding.ASCII.GetString(senderName).Trim(); this.ReceiverName = ASCIIEncoding.ASCII.GetString(receiverName).Trim(); // get IPs IPAddress senderIPaddr = new IPAddress(senderIP); IPAddress receiverIPaddr = new IPAddress(receiverIP); this.ReceiverIP = receiverIPaddr.ToString(); this.SenderIP = senderIPaddr.ToString(); // get Reason Code this.ReasonCode = ControlRecordBytes[32]; }
public void ParseBytes(byte[] SignInRecordBytes) { byte[] RemoteNodeEBCDIC = new byte[8]; byte[] LocalNodeEBCDIC = new byte[8]; byte[] BufferSizeBytes = new byte[8]; Array.Copy(SignInRecordBytes, 1, LocalNodeEBCDIC, 0, 8); Array.Copy(SignInRecordBytes, 9, RemoteNodeEBCDIC, 0, 8); Array.Copy(SignInRecordBytes, 19, BufferSizeBytes, 0, 2); if (BitConverter.IsLittleEndian) { Array.Reverse(BufferSizeBytes); } this.BufferSize = BitConverter.ToUInt16(BufferSizeBytes, 0); RemoteNodeEBCDIC = EbcdicEncoding.Convert(EbcdicEncoding.GetEncoding("EBCDIC-US"), Encoding.ASCII, RemoteNodeEBCDIC); LocalNodeEBCDIC = EbcdicEncoding.Convert(EbcdicEncoding.GetEncoding("EBCDIC-US"), Encoding.ASCII, LocalNodeEBCDIC); this.RemoteNode = ASCIIEncoding.ASCII.GetString(RemoteNodeEBCDIC).Trim(); this.LocalNode = ASCIIEncoding.ASCII.GetString(LocalNodeEBCDIC).Trim(); }
private byte[] ConvertData(string data, int startIndex, int endIndex, DataType type) { int len = (endIndex - startIndex) + 1; if ((type == DataType.A) || (type == DataType.S)) { //if convert to EBCDIC or Zoned Decimal Encoding eC = EbcdicEncoding.GetEncoding(20838); //"EBCDIC-US" if (data.Length < len) { data = data.PadRight(len, data == "0" ? '0' : ' '); } return(eC.GetBytes(data)); } else if (type == DataType.B) {//if convert to Binary int i = Convert.ToInt32(data); byte[] pB = System.BitConverter.GetBytes(i); Array.Reverse(pB); return(pB); } else {//if convert to packed decimal or other else Stack <byte> comp3 = new Stack <byte>(); long value = Convert.ToInt64(data); byte currentByte; if (value < 0) { currentByte = 0x0d; value = -value; } else { currentByte = 0x0f; } bool byteComplete = false; while (value != 0) { if (byteComplete) { currentByte = (byte)(value % 10); } else { currentByte |= (byte)((value % 10) << 4); } value /= 10; byteComplete = !byteComplete; if (byteComplete) { comp3.Push(currentByte); } } if (!byteComplete) { comp3.Push(currentByte); } byte[] returnValue = new byte[len]; if (comp3.ToArray().Length < len) { int index = 0; for (int i = 0; i < len; i++) { if (i < len - comp3.ToArray().Length) { returnValue[i] = 0000; } else { returnValue[i] = comp3.ToArray()[index]; index++; } } } else { returnValue = comp3.ToArray(); } return(returnValue); } }
/// <summary> /// Creates a new Sgy file /// </summary> /// <param name="fileInfo">The file info</param> /// <param name="textHeader">The ebcdic or ascii encoded (worthless to have different encodings) text header </param> /// <param name="header">The file binary header</param> /// <param name="endianBitconverter">A converter to accomodate little endian vs big endian bit conversion</param> /// <param name="overwrite">Should overwrite file on creation?</param> /// <returns>An instance of Sgy file</returns> public static SgyFile Create(FileInfo fileInfo, string textHeader, FileHeader header, EndianBitConverter endianBitconverter, bool overwrite = false) { return(Create(fileInfo, textHeader, EbcdicEncoding.GetEncoding("EBCDIC-US"), header, endianBitconverter, overwrite)); }
/// <summary> /// Creates a new Sgy file /// </summary> /// <param name="fileInfo">The file info</param> /// <param name="header">The header information of the file</param> /// <param name="overwrite"></param> /// <returns></returns> public static SgyFile Create(FileInfo fileInfo, FileHeader header, bool overwrite = false) { return(Create(fileInfo, new string(new char[TextHeaderBytesCount]), EbcdicEncoding.GetEncoding("EBCDIC-US"), header, new BigEndianBitConverter(), overwrite)); }
/// <summary> /// Read all metadata (headers, encodings, formats etc...) /// </summary> /// <param name="fileInfo">The file info of the sgy file</param> /// <returns></returns> public static SgyFile Open(FileInfo fileInfo) { if (fileInfo == null) { throw new ArgumentNullException("File info cannot be null"); } if (fileInfo.Exists == false) { throw new FileNotFoundException($"File {fileInfo.FullName} does not exist"); } if (fileInfo.Length < TextHeaderBytesCount + BinaryHeaderBytesCount) { throw new ArgumentException($"File {fileInfo.FullName} does not have enough bytes in its content to infer file metadata. This implies the file is corrupt, or not a real sgy file. Try creating a new sgy file isntead."); } // Local variables byte[] buffer = new byte[65536]; FileStream fileStream; Encoding textHeaderEncoding; List <string> textHeaders = new List <string>(); EndianBitConverter endianBitConverter; FileHeader binaryHeader; EndianBitConverter bigEBitConverter = new BigEndianBitConverter(); EndianBitConverter lilEBitConverter = new LittleEndianBitConverter(); // Get text header bytes and binary header bytes fileStream = new FileStream(fileInfo.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); fileStream.Read(buffer, 0, TextHeaderBytesCount + BinaryHeaderBytesCount); // Evaluate text encoding (ascii and ebcdic supported by sgy....for no modern day reason) textHeaderEncoding = buffer[0] == 'C' ? Encoding.ASCII : EbcdicEncoding.GetEncoding("EBCDIC-US"); textHeaders.Add(textHeaderEncoding.GetString(buffer, 0, TextHeaderBytesCount)); // Endianness and format code short bigEFormatCode = bigEBitConverter.ToInt16(buffer, 3224); short lilEFormatCode = lilEBitConverter.ToInt16(buffer, 3224); if (lilEFormatCode >= 0 || lilEFormatCode <= 8) { endianBitConverter = new LittleEndianBitConverter(); } if (bigEFormatCode >= 0 || bigEFormatCode <= 8) { endianBitConverter = new BigEndianBitConverter(); } else { throw new Exception("Cannot infer endianess from the format code"); } // File Binary Header binaryHeader = FileHeader.From(buffer, TextHeaderBytesCount, endianBitConverter); // Extended text headers fileStream.Seek(3600, SeekOrigin.Begin); fileStream.Read(buffer, 0, binaryHeader.ExtendedTextHeadersCount * TextHeaderBytesCount); for (int i = 0; i < binaryHeader.ExtendedTextHeadersCount; i++) { textHeaders.Add(textHeaderEncoding.GetString(buffer, i * TextHeaderBytesCount, TextHeaderBytesCount)); } return(new SgyFile(fileStream, textHeaders, textHeaderEncoding, binaryHeader, endianBitConverter)); }