private byte[] ReceiveChunk(int totalBytesToRead, MyBinaryReader dataSenderStream) { int bytesRead = 0; int totalBytesReceived = 0; int chunkFragmentCounter = 0; MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[totalBytesToRead]; int maxInputLen = totalBytesToRead; while (totalBytesReceived < totalBytesToRead && (bytesRead = dataSenderStream.Read(buffer, 0, maxInputLen)) > 0) { if (totalBytesReceived + bytesRead >= totalBytesToRead) { int newPacketSize = bytesRead - ((totalBytesReceived + bytesRead) - totalBytesToRead); totalBytesReceived += newPacketSize; memStream.Write(buffer, 0, newPacketSize); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "Chunked.ReceiveChunk(0:MaxLen:{0}): Receiving fragment {1} from: Client -> Server ({2} buffer, totalBytesReceived:{3}, totalBytesToRead:{4})", buffer.Length, chunkFragmentCounter, bytesRead, totalBytesReceived, totalBytesToRead); break; } else { totalBytesReceived += bytesRead; memStream.Write(buffer, 0, bytesRead); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "Chunked.ReceiveChunk(1:MaxLen:{0}): Receiving fragment {1} from: Client -> Server ({2} buffer, totalBytesReceived:{3}, totalBytesToRead:{4})", buffer.Length, chunkFragmentCounter, bytesRead, totalBytesReceived, totalBytesToRead); } maxInputLen -= bytesRead; chunkFragmentCounter++; } byte[] fullDataChunk = memStream.ToArray(); return(fullDataChunk); }
public TcpClientSsl(RequestObj requestObj, MyBinaryReader clientStreamReader, BinaryWriter clientStreamWriter) : base(requestObj, TcpPortHttps) { base.clientStreamReader = clientStreamReader; base.clientStreamWriter = clientStreamWriter; }
public TcpBinaryFrameManager (int mode, Stream s, bool isServiceSide) { this.mode = mode; this.s = s; this.is_service_side = isServiceSide; reader = new MyBinaryReader (s); ResetWriteBuffer (); EncodingRecord = Soap12EncodingBinaryWithDictionary; // FIXME: it should depend on mode. }
public BicqSection(MyBinaryReader saveStream) : base(saveStream) { this.verNum = new SectionWithSubrecords(saveStream); this.game = new SectionWithSubrecords(saveStream); }
// Constructor. public GenericSection(MyBinaryReader saveStream) { Console.WriteLine("GenericSection constrcutor"); this.Name = saveStream.ReadCharString(4); Console.WriteLine(this.Name); }
// Constructor. public civ3Game(MyBinaryReader saveStream) { // Initialize my list of sections I don't know what to do with yet. this.pileOfSectionsToSortLater = new List <SectionWithLength>(); // Initialize empty byte[] pad list. this.bytePads = new List <byte[]>(); // Initialize empty tile list. this.tileSection = new List <SectionWithLength>(); // Read save file stream assuming fixed order and general structure. // Trying to use class constructors but reading some padding in manually. // FIX: hard-coding CIV3 length of 0x1a this.civ3Section = new GenericSection(saveStream, 0x1a); this.bicSection = new SectionWithLength(saveStream); this.bicqSection = new BicqSection(saveStream); this.gameSection = new GenericSection(saveStream, 0x0ecf); // reading in five sections (DATE, PLGI, PLGI, DATE and DATE) this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); // FIX: hard-coded byte padding here. Is it algined? Other way to realign? this.bytePads.Add(saveStream.ReadBytes(8)); // reading CNSL section this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); // reading three WRLD sections this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); // FIX: Ugly hack to grab presumed map width and height. int mapWidth = BitConverter.ToInt32(pileOfSectionsToSortLater[pileOfSectionsToSortLater.Count - 1].buffer, 0x04); int mapHeight = BitConverter.ToInt32(pileOfSectionsToSortLater[pileOfSectionsToSortLater.Count - 1].buffer, 0x18); Console.WriteLine(mapWidth); Console.WriteLine(mapHeight); this.pileOfSectionsToSortLater.Add(new SectionWithLength(saveStream)); // FIX: hard-seeking to first TILE section of my test file. // saveStream.BaseStream.Seek(0x34a4, SeekOrigin.Begin); // Read all TILE sections. There are mapWidth/2 * mapHeight * 4 of them // FIX: I really want one entity per game tile, but there are four TILE sections per game tile for (int i = 0; i < (mapWidth / 2) * mapHeight * 4; i++) { this.tileSection.Add(new SectionWithLength(saveStream)); } Console.WriteLine(tileSection.Count); /* * // FIX: Need to load all tiles. Load a single tile into the list. * this.tileSection.Add(new SectionWithLength(saveStream)); * this.tileSection.Add(new SectionWithLength(saveStream)); * this.tileSection.Add(new SectionWithLength(saveStream)); * this.tileSection.Add(new SectionWithLength(saveStream)); * this.tileSection.Add(new SectionWithLength(saveStream)); * this.tileSection.Add(new SectionWithLength(saveStream)); */ // Iterating and printing out section names of my junk pile. foreach (SectionWithLength mySection in this.pileOfSectionsToSortLater) { Console.WriteLine(mySection.Name); } }
public XeXHeader(MyBinaryReader reader) { long position = reader.BaseStream.Position; try { byte[] b = reader.ReadBytes(4); if (Encoding.ASCII.GetString(b) == "XEX2") { reader.BaseStream.Seek(position + 20L, SeekOrigin.Begin); uint num = reader.ReadUInt32(); UInt32 einfo = BitConverter.ToUInt32(executioninfo, 0); UInt32 tstamp = BitConverter.ToUInt32(basefiletimestamp, 0); for (int i = 0; i < num; i++) { UInt32 val = BitConverter.ToUInt32(reader.ReadBytes(4), 0); uint pos = reader.ReadUInt32(); long savepos = reader.BaseStream.Position; if (val == einfo) { reader.BaseStream.Seek((long)position + pos, SeekOrigin.Begin); this.short_mediaId = reader.ReadBytes(4); this.version = reader.ReadUInt32(); this.baseVersion = reader.ReadUInt32(); this.titleId = reader.ReadBytes(4); this.platform = reader.ReadByte(); this.executableType = reader.ReadByte(); this.discNumber = reader.ReadByte(); this.discCount = reader.ReadByte(); reader.BaseStream.Seek(savepos, SeekOrigin.Begin); } else if (val == tstamp) { reader.BaseStream.Seek((long)position + pos + 4, SeekOrigin.Begin); this.date = new DateTime(1970, 1, 1, 0, 0, 0).Add( TimeSpan.FromTicks((long)reader.ReadUInt32() * TimeSpan.TicksPerSecond)); reader.BaseStream.Seek(savepos, SeekOrigin.Begin); } } //Read cert reader.BaseStream.Seek((long)position + 0x10, SeekOrigin.Begin); uint offset = reader.ReadUInt32(); reader.BaseStream.Seek((long)position + (long)offset + 0x140, SeekOrigin.Begin); mediaid = reader.ReadBytes(16); reader.BaseStream.Seek((long)position + (long)offset + 0x178, SeekOrigin.Begin); regioncode = reader.ReadUInt32(); } else if (Encoding.ASCII.GetString(b) == "XBEH") { reader.EndianType = EndianType.LittleEndian; reader.BaseStream.Seek(position + 0x110, SeekOrigin.Begin); uint pos = reader.ReadUInt32(); this.date = new DateTime(1970, 1, 1, 0, 0, 0).Add(TimeSpan.FromTicks((long)reader.ReadUInt32() * TimeSpan.TicksPerSecond)); reader.BaseStream.Seek(position + pos + 0x8, SeekOrigin.Begin); this.titleId = new byte[4]; this.titleId[3] = reader.ReadByte(); this.titleId[2] = reader.ReadByte(); this.titleId[1] = reader.ReadByte(); this.titleId[0] = reader.ReadByte(); byte[] b2 = reader.ReadBytes(0x40); this.title = Encoding.Unicode.GetString(b2).Replace("\0", string.Empty).Trim(); reader.BaseStream.Seek(position + pos + 0xA8, SeekOrigin.Begin); this.discNumber = this.discCount = (byte)(reader.ReadUInt32() + 1); this.version = reader.ReadUInt32(); } else { throw new Exception("Not XEX/XEB file"); } } catch (Exception exception) { if (reader != null) { reader.BaseStream.Seek(position, SeekOrigin.Begin); } throw exception; } reader.BaseStream.Seek(position, SeekOrigin.Begin); }
public ResourcesFile(Stream stream, bool leaveOpen = true) { fileStartPosition = stream.Position; reader = new MyBinaryReader(stream, leaveOpen); const string ResourcesHeaderCorrupted = "Resources header corrupted."; // Read ResourceManager header // Check for magic number int magicNum = reader.ReadInt32(); if (magicNum != MagicNumber) { throw new BadImageFormatException("Not a .resources file - invalid magic number"); } // Assuming this is ResourceManager header V1 or greater, hopefully // after the version number there is a number of bytes to skip // to bypass the rest of the ResMgr header. For V2 or greater, we // use this to skip to the end of the header int resMgrHeaderVersion = reader.ReadInt32(); int numBytesToSkip = reader.ReadInt32(); if (numBytesToSkip < 0 || resMgrHeaderVersion < 0) { throw new BadImageFormatException(ResourcesHeaderCorrupted); } if (resMgrHeaderVersion > 1) { reader.BaseStream.Seek(numBytesToSkip, SeekOrigin.Current); } else { // We don't care about numBytesToSkip; read the rest of the header // readerType: reader.ReadString(); // resourceSetType: reader.ReadString(); } // Read RuntimeResourceSet header // Do file version check version = reader.ReadInt32(); if (version != ResourceSetVersion && version != 1) { throw new BadImageFormatException($"Unsupported resource set version: {version}"); } numResources = reader.ReadInt32(); if (numResources < 0) { throw new BadImageFormatException(ResourcesHeaderCorrupted); } // Read type positions into type positions array. // But delay initialize the type table. int numTypes = reader.ReadInt32(); if (numTypes < 0) { throw new BadImageFormatException(ResourcesHeaderCorrupted); } typeTable = new string[numTypes]; for (int i = 0; i < numTypes; i++) { typeTable[i] = reader.ReadString(); } // Prepare to read in the array of name hashes // Note that the name hashes array is aligned to 8 bytes so // we can use pointers into it on 64 bit machines. (4 bytes // may be sufficient, but let's plan for the future) // Skip over alignment stuff. All public .resources files // should be aligned No need to verify the byte values. long pos = reader.BaseStream.Position - fileStartPosition; int alignBytes = unchecked ((int)pos) & 7; if (alignBytes != 0) { for (int i = 0; i < 8 - alignBytes; i++) { reader.ReadByte(); } } // Skip over the array of name hashes try { reader.Seek(checked (4 * numResources), SeekOrigin.Current); } catch (OverflowException) { throw new BadImageFormatException(ResourcesHeaderCorrupted); } // Read in the array of relative positions for all the names. namePositions = new int[numResources]; for (int i = 0; i < numResources; i++) { int namePosition = reader.ReadInt32(); if (namePosition < 0) { throw new BadImageFormatException(ResourcesHeaderCorrupted); } namePositions[i] = namePosition; } // Read location of data section. int dataSectionOffset = reader.ReadInt32(); if (dataSectionOffset < 0) { throw new BadImageFormatException(ResourcesHeaderCorrupted); } // Store current location as start of name section nameSectionPosition = reader.BaseStream.Position; dataSectionPosition = fileStartPosition + dataSectionOffset; // _nameSectionOffset should be <= _dataSectionOffset; if not, it's corrupt if (dataSectionPosition < nameSectionPosition) { throw new BadImageFormatException(ResourcesHeaderCorrupted); } }
public int ForwardNonchunkedDataToPeerChunked( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, Encoding contentCharsetEncoding, int transferredContentLength, byte[] serverNewlineBytes, SniffedDataChunk sniffedDataChunk, bool mustBeProcessed) { int noBytesTransferred = 0; byte[] buffer = new byte[MaxBufferSize]; int totalTransferredBytes = 0; int bytesRead = 0; while (totalTransferredBytes < transferredContentLength) { // Read data from peer 1 int maxDataToTransfer = transferredContentLength - totalTransferredBytes; bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(2:DATA), No data to transfer"); break; } // if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } // Encode received bytes to the announced format DataChunk serverDataChunk = new DataChunk(buffer, bytesRead, this.requestObj.ServerResponseObj.ContentTypeEncoding.ContentCharsetEncoding); if (mustBeProcessed == true) { Lib.PluginCalls.ServerDataTransfer(this.requestObj, serverDataChunk); } // Send chunk size to recepient string chunkSizeHexStringTmp = serverDataChunk.ContentDataLength.ToString("x"); byte[] chunkSizeDeclaration = contentCharsetEncoding.GetBytes(chunkSizeHexStringTmp); outputStreamWriter.Write(chunkSizeDeclaration, 0, chunkSizeDeclaration.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); // Send data packet to recipient outputStreamWriter.Write(serverDataChunk.ContentData, 0, serverDataChunk.ContentDataLength); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); outputStreamWriter.Flush(); noBytesTransferred += serverDataChunk.ContentDataLength; Logging.Instance.LogMessage( this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(): Data successfully relayed to client. ContentDataLength:{0} chunkSizeHexStringTmp={1} noBytesTransferred={2}", serverDataChunk.ContentDataLength, chunkSizeHexStringTmp, noBytesTransferred); totalTransferredBytes += bytesRead; } // Send trailing "0 length" chunk string chunkSizeZeroHexString = 0.ToString("x"); byte[] chunkSizeZeroDeclaration = contentCharsetEncoding.GetBytes(chunkSizeZeroHexString); outputStreamWriter.Write(chunkSizeZeroDeclaration, 0, chunkSizeZeroDeclaration.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); outputStreamWriter.Write(serverNewlineBytes, 0, serverNewlineBytes.Length); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer3(2:DATA): Total amount of transferred data={0}", totalTransferredBytes); return(noBytesTransferred); }
private Record <long> ParseLongConstant(MyBinaryReader reader) { // var highBytes = reader.ReadInt32(); // var lowBytes = reader.ReadInt32(); return(new Record <long>(reader.ReadInt64())); }
public int ForwardNonchunkedDataToPeerNonChunked( MyBinaryReader inputStreamReader, BinaryWriter outputStreamWriter, Encoding contentCharsetEncoding, int transferredContentLength, SniffedDataChunk sniffedDataChunk, bool mustBeProcessed) { int noBytesTransferred = 0; MemoryStream memStream = new MemoryStream(); byte[] buffer = new byte[MaxBufferSize]; int totalTransferredBytes = 0; int bytesRead = 0; while (totalTransferredBytes < transferredContentLength) { // Read data from peer 1 int maxDataToTransfer = transferredContentLength - totalTransferredBytes; bytesRead = inputStreamReader.Read(buffer, 0, buffer.Length); if (bytesRead <= 0) { Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA), No data to transfer"); break; } // Write data to memory stream memStream.Write(buffer, 0, bytesRead); if (sniffedDataChunk != null) { sniffedDataChunk.AppendData(buffer, bytesRead); } totalTransferredBytes += bytesRead; Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA, TotalDataToTransfer:{0}): Relaying data from: Client -> Server (bytesRead:{1} totalTransferredBytes:{2})", buffer.Length, bytesRead, totalTransferredBytes); } byte[] dataPacket = memStream.ToArray(); //if (dataPacket.Length != MaxBufferSize) //{ // throw new Exception("The announced content length and the amount of received data are not the same"); //} // Encode received bytes to the announced format DataChunk serverDataChunk = new DataChunk(dataPacket, dataPacket.Length, this.requestObj.ServerResponseObj.ContentTypeEncoding.ContentCharsetEncoding); if (mustBeProcessed == true) { Lib.PluginCalls.ServerDataTransfer(this.requestObj, serverDataChunk); } // Send data packet to recipient outputStreamWriter.Write(serverDataChunk.ContentData, 0, serverDataChunk.ContentDataLength); outputStreamWriter.Flush(); noBytesTransferred += serverDataChunk.ContentDataLength; Logging.Instance.LogMessage( this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(): Data successfully relayed to client. ContentDataSize:{0})", serverDataChunk.ContentDataLength); Logging.Instance.LogMessage(this.requestObj.Id, this.requestObj.ProxyProtocol, Loglevel.Debug, "TcpClientRaw.ForwardNonchunkedDataToPeer2(2:DATA): Total amount of transferred data={0}", totalTransferredBytes); return(noBytesTransferred); }
private Tuple <Record <int>, Record <int> > ParseLongOrDoubleConstant(MyBinaryReader reader) => Tuple.Create(ParseIntegerConstant(reader), ParseIntegerConstant(reader));
private Record <float> ParseFloatConstant(MyBinaryReader reader) => new Record <float>(reader.ReadSingle());
private Record <int> ParseIntegerConstant(MyBinaryReader reader) => new Record <int>(reader.ReadInt32());
public List <IConstant> Parse(MyBinaryReader reader) { // заполняем первый индекс заглушкой, т.к. здесь нумерация с единицы var pool = new List <IConstant>(_poolCount) { null }; while (_poolCount - pool.Count > 0) { switch ((Tags)reader.ReadByte()) { // Здесь всем методам будем передавать index + 1 case Tags.CONSTANT_Class: // parse 2byte name_index pool.Add(new ClassInfo(reader.ReadUInt16())); break; case Tags.CONSTANT_Fieldref: case Tags.CONSTANT_Methodref: case Tags.CONSTANT_InterfaceMethodref: // u2 class_index // u2 name_and_type_index pool.Add(new FiMeInRef(reader.ReadUInt16(), reader.ReadUInt16())); break; case Tags.CONSTANT_String: // u2 string_index pool.Add(new StringRef(reader.ReadUInt16())); break; case Tags.CONSTANT_Integer: // u4 bytes pool.Add(ParseIntegerConstant(reader)); //index += 4; break; case Tags.CONSTANT_Float: // Не знаю, что будет с float, попробую не пользоваться формулой s * m * 2**(e-150) // u4 bytes pool.Add(ParseFloatConstant(reader)); //index += 4; break; case Tags.CONSTANT_Long: case Tags.CONSTANT_Double: var pair = ParseLongOrDoubleConstant(reader); pool.Add(pair.Item1); pool.Add(pair.Item2); //index += 8; break; case Tags.CONSTANT_NameAndType: // u2 name_index // u2 descriptor_index pool.Add(new NameAndTypeInfo(reader.ReadUInt16(), reader.ReadUInt16())); break; case Tags.CONSTANT_Utf8: var length = reader.ReadUInt16(); var fragment = reader.ReadBytes(length); pool.Add(ParseUtfStrings(fragment, length)); break; case Tags.CONSTANT_MethodHandle: // u1 reference_kind in range(1 to 9) // u2 reference_index pool.Add(new MethodHandle(reader.ReadByte(), reader.ReadUInt16())); // Надо бы валидацию break; case Tags.CONSTANT_MethodType: // u2 descriptor_index pool.Add(new MethodType(reader.ReadUInt16())); break; case Tags.CONSTANT_InvokeDynamic: // u2 bootstrap_method_attr_index // u2 name_and_type_index pool.Add(new InvokeDynamic(reader.ReadUInt16(), reader.ReadUInt16())); break; } } return(pool); }
public ClassFileParser(string fileName) { _bReader = ReadClassFile(fileName); constantPoolByteCount = 0; }