public static PsnPacketChunk FromByteArray(byte[] data) { try { using (var ms = new MemoryStream(data)) using (var reader = new PsnBinaryReader(ms)) { var chunkHeader = reader.ReadChunkHeader(); switch ((PsnPacketChunkId)chunkHeader.ChunkId) { case PsnPacketChunkId.PsnDataPacket: return(PsnDataPacketChunk.Deserialize(chunkHeader, reader)); case PsnPacketChunkId.PsnInfoPacket: return(PsnInfoPacketChunk.Deserialize(chunkHeader, reader)); default: return(PsnUnknownPacketChunk.Deserialize(chunkHeader, reader)); } } } catch (EndOfStreamException) { // Received a bad packet return(null); } }
/// <summary> /// Searches the binary stream for the positions of sub-chunk headers /// </summary> /// <exception cref="IOException">An I/O error occurs. </exception> /// <exception cref="NotSupportedException">The stream does not support seeking. </exception> internal static IEnumerable <Tuple <PsnChunkHeader, long> > FindSubChunkHeaders(PsnBinaryReader reader, int chunkDataLength) { var chunkHeaders = new List <Tuple <PsnChunkHeader, long> >(); long startPos = reader.BaseStream.Position; while (reader.BaseStream.Position - startPos < chunkDataLength) { var chunkHeader = reader.ReadChunkHeader(); chunkHeaders.Add(Tuple.Create(chunkHeader, reader.BaseStream.Position)); reader.Seek(chunkHeader.DataLength, SeekOrigin.Current); Debug.Assert(reader.BaseStream.Position - startPos <= chunkDataLength); } reader.Seek(startPos, SeekOrigin.Begin); return(chunkHeaders); }