//该方法获取http请求头部信息 public HeaderStruct GetHttpHeader(HttpRequestBase Request) { string Url; if (Request.Url.ToString().Length > 2048) { Url = "http://localhost/"; } Url = Request.Url.ToString(); string Method = Request.HttpMethod.ToString(); string HostIp = Request.UserHostAddress; string ContentType = Request.ContentType; string Port = Request.Url.Port.ToString(); int TotalBytes = Request.TotalBytes; Dictionary <string, string> Cookies = new Dictionary <string, string>(); foreach (string CookiesKey in Request.Cookies.Keys) { Cookies.Add(CookiesKey, Request.Cookies.Get(CookiesKey).Value); } HeaderStruct Header = new HeaderStruct(); Header.URL = Url; Header.Method = Method; Header.HostIp = HostIp; Header.ContentType = ContentType; Header.Port = Port; Header.TotalBytes = TotalBytes; Header.Cookies = Cookies; return(Header); }
public void Load(Stream data, bool fast = false) { uint headersize = Helpers.ReadLEUInt(data); long startoffset = data.Position; Header = new HeaderStruct(); Header.magic = Helpers.ReadLEUInt(data); Header.totalCount = Helpers.ReadLEUInt(data); Header.ebxCount = Helpers.ReadLEUInt(data); Header.resCount = Helpers.ReadLEUInt(data); Header.chunkCount = Helpers.ReadLEUInt(data); Header.stringOffset = Helpers.ReadLEUInt(data); Header.chunkMetaOffset = Helpers.ReadLEUInt(data); Header.chunkMetaSize = Helpers.ReadLEUInt(data); ReadSha1List(data); ReadEbxList(data); ReadResList(data); ReadChunkList(data); if (Header.chunkCount != 0) ChunkMeta = BJSON.ReadField(data); else ChunkMeta = null; data.Seek(startoffset + Header.stringOffset, 0); ReadEbxListNames(data); data.Seek(startoffset + Header.stringOffset, 0); ReadResListNames(data); data.Seek(startoffset + headersize, 0); ReadEbxListData(data, fast); ReadResListData(data, fast); ReadChunkListData(data, fast); ApplySHA1s(); }
public void Serialize(SerializingFile con) { if (con.isLoading) { Header = new HeaderStruct(); } Header.Serialize(con); con.Seek((int)Header.EntryOffset, SeekOrigin.Begin); for (int i = 0; i < Header.FileCount; i++) { //Debug.WriteLine($"Serialize sfar file {i} at 0x{con.Memory.Position:X8}"); var feStruct = new FileEntryStruct(); feStruct.Serialize(con, Header); Files.Add(feStruct); //Debug.WriteLine($"Data offset for {i}: 0x{Files[i].DataOffset:X8} (0x{Files[i].RealDataOffset:X8}), header at 0x{pos:X8}"); } //var ordered = Files.OrderBy(x => x.DataOffset).ToList(); //foreach (var f in ordered) //{ // Debug.WriteLine($"0x{f.DataOffset:X8} (0x{f.RealDataOffset:X8}), header at {f.MyOffset:X8}"); //} //var firstfile = Files.MinBy(x => x.RealDataOffset); if (con.isLoading) { ReadFileNames(); } }
public static void ParsePacket(byte[] packet, out byte command, out byte[] payload) { var headerBytes = new ArraySegment <byte>(packet, 0, HEADER_SIZE).ToArray(); HeaderStruct header = StructInterop.ByteArrayToStruct <HeaderStruct>(headerBytes); // Payload if (header.length > 0) { payload = new ArraySegment <byte>(packet, HEADER_SIZE, header.length).ToArray(); } else { payload = null; } var footerBytes = new ArraySegment <byte>(packet, HEADER_SIZE + header.length, FOOTER_SIZE).ToArray(); FooterStruct footer = StructInterop.ByteArrayToStruct <FooterStruct>(footerBytes); // Detect protocol errors if (header.command == CMD_ERROR) { if (payload != null && payload.Length > 0) { throw HLDCProtocolException.FromCode(payload[0]); } else { throw new HLDCException("Protocol malformed error message"); } } command = header.command; }
public void TestWriteableVisualArea() { HeaderStruct header = new HeaderStruct() { MinX = 50, MaxX = 80, MinY = 50, MaxY = 80 }; Assert.AreEqual(false, _imageMaker .IsWritablePixel(header, 0, 100, 100)); Assert.AreEqual(true, _imageMaker .IsWritablePixel(header, 50 + 50 * 100, 100, 100)); Assert.AreEqual(true, _imageMaker .IsWritablePixel(header, 78 + 78 * 100, 100, 100)); Assert.AreEqual(true, _imageMaker .IsWritablePixel(header, 51 + 78 * 100, 100, 100)); Assert.AreEqual(false, _imageMaker .IsWritablePixel(header, 81 + 51 * 100, 100, 100)); Assert.AreEqual(false, _imageMaker .IsWritablePixel(header, 51 + 81 * 100, 100, 100)); Assert.AreEqual(false, _imageMaker .IsWritablePixel(header, 90 + 90 * 100, 100, 100)); }
public Psf Load(Stream stream) { EntryDictionary = new Dictionary <string, object>(); Header = stream.ReadStruct <HeaderStruct>(); Entries = stream.ReadStructVector <EntryStruct>(Header.NumberOfPairs); KeysStream = stream.SliceWithLength(Header.KeyTable); ValuesStream = stream.SliceWithLength(Header.ValueTable); foreach (var entry in Entries) { var key = KeysStream.ReadStringzAt(entry.KeyOffset); var valueStream = ValuesStream.SliceWithLength(entry.ValueOffset, entry.ValueSize); switch (entry.DataType) { case DataType.Binary: EntryDictionary[key] = valueStream.ReadAll(); break; case DataType.Int: EntryDictionary[key] = valueStream.ReadStruct <int>(); break; case DataType.Text: EntryDictionary[key] = valueStream.ReadStringz(-1, Encoding.UTF8); break; default: throw new NotImplementedException(); } } return(this); }
private void Read(BinaryReader reader) { Header = reader.Read <HeaderStruct>(); if (Header.ChunkCount > 0) { Chunks = reader.ReadArray <Chunk>(Header.ChunkCount); } }
private AnalyzeResult AnalyzeHttpHeader(HeaderStruct Header) { //实例化http的GET请求的分析结果 AnalyzeResult GetMethodResult = new AnalyzeResult(); //判断该请求是否有为黑名单,是否设置了访问流量限制和访问频率限制 LimitList LimitJudge = new LimitList(); FrequenceLimit FrequenceJudge = new FrequenceLimit(); FlowLimit FlowJudge = new FlowLimit(); GetMethodResult.RequestHeader = Header; GetMethodResult.RequestContents = string.Empty; if (!LimitJudge.IsLimitVisitor(Header.HostIp)) { GetMethodResult.ResultReport = "该主机已被设置为黑名单,无法访问"; return(GetMethodResult); } if (LimitJudge.GetLimitVisitorTotalRate(Header.HostIp) > 0) { if (!FrequenceJudge.IsOverTimesNum(LimitJudge.GetLimitVisitorTotalRate(Header.HostIp), Header.HostIp)) { GetMethodResult.ResultReport = "请求过于频繁,系统自动判定为异常请求!请稍后再试!"; return(GetMethodResult); } } else if (LimitJudge.GetLimitVisitorTotalRate(Header.HostIp) == 0) { GetMethodResult.ResultReport = "数据库出错"; return(GetMethodResult); } IPAddress CheckIPAddress = null; if (!IPAddress.TryParse(Header.HostIp, out CheckIPAddress)) { GetMethodResult.ResultReport = "请求报文的IP地址不合法!"; return(GetMethodResult); } HttpRequestFilter Filter = new HttpRequestFilter(); Header.URL = Filter.URLFilter(Header.URL); //过滤URL特殊字符 if (Header.URL == "URL字符过滤异常") { GetMethodResult.ResultReport = Header.URL; return(GetMethodResult); } GetMethodResult.ResultReport = "请求过滤成功"; return(GetMethodResult); }
/// <summary> /// /// </summary> /// <param name="Stream"></param> public void SetStream(Stream Stream) { this.Stream = Stream; // Read the header. this.Header = this.Stream.ReadStruct <HeaderStruct>(); if (this.Header.Magic != 0x4F534943) { throw (new InvalidDataException("Not a CISO File")); } // Read the block list this.Blocks = this.Stream.ReadStructVector <BlockInfo>((uint)(NumberOfBlocks + 1)); }
/// <summary> /// /// </summary> /// <param name="stream"></param> public void SetStream(Stream stream) { Stream = stream; // Read the header. Header = Stream.ReadStruct <HeaderStruct>(); if (Header.Magic != DaxfileSignature) { throw new InvalidDataException("Not a DAX File"); } var totalBlocks = Header.TotalBlocks; //Header.TotalBlocks var offsets = Stream.ReadStructVector <uint>(totalBlocks); var sizes = Stream.ReadStructVector <ushort>(totalBlocks); NcArea[] ncAreas = null; if (Header.Version >= Version.DaxformatVersion1) { ncAreas = Stream.ReadStructVector <NcArea>(Header.NcAreas); } Blocks = new BlockInfo[totalBlocks]; for (var n = 0; n < totalBlocks; n++) { Blocks[n].Position = offsets[n]; Blocks[n].Length = sizes[n]; Blocks[n].IsCompressed = true; } if (Header.Version < Version.DaxformatVersion1) { return; } if (ncAreas == null) { return; } foreach (var ncArea in ncAreas) { //Console.WriteLine("{0}-{1}", NCArea.frame, NCArea.size); for (var n = 0; n < ncArea.Size; n++) { Blocks[ncArea.Frame + n].IsCompressed = false; } } }
/// <summary> /// /// </summary> /// <param name="stream"></param> public void SetStream(Stream stream) { //this.Stream = new BufferedStream(Stream, 0x20000); Stream = stream; // Read the header. Header = Stream.ReadStruct <HeaderStruct>(); if (Header.Magic != 0x4F534943) { throw new InvalidDataException("Not a CISO File"); } // Read the block list Blocks = Stream.ReadStructVector <BlockInfo>((uint)(NumberOfBlocks + 1)); }
public Pmf Load(Stream Stream) { Header = Stream.ReadStruct<HeaderStruct>(); var Chunk = ReadChunk(Stream.SliceWithLength(0x50)); InfoHeader = Chunk.Stream.ReadStruct<InfoHeaderStruct>(); /* Console.WriteLine("0x{0:X}", (ulong)InfoHeader.FirstTimestamp); Console.WriteLine("0x{0:X}", (ulong)InfoHeader.LastTimestamp); Console.WriteLine("{0}", (ulong)InfoHeader.Width); Console.WriteLine("{0}", (ulong)InfoHeader.Height); */ return this; }
public void Serialize(SerializingContainer con, HeaderStruct header) { Header = header; MyOffset = (uint)con.GetPos(); if (con.isLoading) { Hash = new byte[16]; } for (int i = 0; i < 16; i++) { Hash[i] = con + Hash[i]; } BlockSizeIndex = con + BlockSizeIndex; UncompressedSize = con + UncompressedSize; UncompressedSizeAdder = con + UncompressedSizeAdder; RealUncompressedSize = UncompressedSize + UncompressedSizeAdder << 32; DataOffset = con + DataOffset; DataOffsetAdder = con + DataOffsetAdder; RealDataOffset = DataOffset + DataOffsetAdder << 32; if (BlockSizeIndex == 0xFFFFFFFF) { BlockOffsets = new long[1]; BlockOffsets[0] = RealDataOffset; BlockSizes = new ushort[1]; BlockSizes[0] = (ushort)UncompressedSize; BlockTableOffset = 0; } else { int numBlocks = (int)Math.Ceiling((double)UncompressedSize / (double)header.MaxBlockSize); if (con.isLoading) { BlockOffsets = new long[numBlocks]; BlockSizes = new ushort[numBlocks]; } BlockOffsets[0] = RealDataOffset; long pos = con.Memory.Position; con.Seek((int)getBlockOffset((int)BlockSizeIndex, header.EntryOffset, header.FileCount), SeekOrigin.Begin); BlockTableOffset = con.Memory.Position; BlockSizes[0] = con + BlockSizes[0]; for (int i = 1; i < numBlocks; i++) { BlockSizes[i] = con + BlockSizes[i]; BlockOffsets[i] = BlockOffsets[i - 1] + BlockSizes[i]; } con.Seek((int)pos, 0); } }
public static byte[] CreatePacket(byte command, byte[] payload = null) { List <byte> packetBuilder = new List <byte>(); if (payload != null && payload.Length > MAX_PAYLOAD_LEN) { throw new Exception("HLDC payload too large"); } packetBuilder.Add(HLDC_FRAME_DELIMITER); // Build the packet header HeaderStruct header = new HeaderStruct() { command = command, length = (byte)((payload != null) ? payload.Length : 0) }; packetBuilder.AddRange(EscapeBytes(StructInterop.StructToByteArray <HeaderStruct>(header))); // Build the packet payload UInt16 crc = 0; if (payload != null) { byte[] escapedPayload = EscapeBytes(payload); packetBuilder.AddRange(escapedPayload); crc = CalculateCrc(escapedPayload); } // BUild the packet footer FooterStruct footer = new FooterStruct() { crc = crc, }; packetBuilder.AddRange(EscapeBytes(StructInterop.StructToByteArray <FooterStruct>(footer))); packetBuilder.Add(HLDC_FRAME_DELIMITER); if (packetBuilder.Count > MAX_PACKET_LEN) { throw new Exception("HLDC packet too large"); } return(packetBuilder.ToArray()); }
public Pmf Load(Stream Stream) { this.Stream = Stream; Header = Stream.ReadStruct <HeaderStruct>(); var Chunk = ReadChunk(Stream.SliceWithLength(0x50)); InfoHeader = Chunk.Stream.ReadStruct <InfoHeaderStruct>(); /* * Console.WriteLine("0x{0:X}", (ulong)InfoHeader.FirstTimestamp); * Console.WriteLine("0x{0:X}", (ulong)InfoHeader.LastTimestamp); * Console.WriteLine("{0}", (ulong)InfoHeader.Width); * Console.WriteLine("{0}", (ulong)InfoHeader.Height); */ return(this); }
public void Serialize(SerializingContainer con, HeaderStruct header) { Header = header; MyOffset = (uint)con.GetPos(); if (con.isLoading) Hash = new byte[16]; for (int i = 0; i < 16; i++) Hash[i] = con + Hash[i]; BlockSizeIndex = con + BlockSizeIndex; UncompressedSize = con + UncompressedSize; UncompressedSizeAdder = con + UncompressedSizeAdder; RealUncompressedSize = UncompressedSize + UncompressedSizeAdder << 32; DataOffset = con + DataOffset; DataOffsetAdder = con + DataOffsetAdder; RealDataOffset = DataOffset + DataOffsetAdder << 32; if (BlockSizeIndex == 0xFFFFFFFF) { BlockOffsets = new long[1]; BlockOffsets[0] = RealDataOffset; BlockSizes = new ushort[1]; BlockSizes[0] = (ushort)UncompressedSize; BlockTableOffset = 0; } else { int numBlocks = (int)Math.Ceiling((double)UncompressedSize / (double)header.MaxBlockSize); if (con.isLoading) { BlockOffsets = new long[numBlocks]; BlockSizes = new ushort[numBlocks]; } BlockOffsets[0] = RealDataOffset; long pos = con.Memory.Position; con.Seek((int)getBlockOffset((int)BlockSizeIndex, header.EntryOffset, header.FileCount), SeekOrigin.Begin); BlockTableOffset = con.Memory.Position; BlockSizes[0] = con + BlockSizes[0]; for (int i = 1; i < numBlocks; i++) { BlockSizes[i] = con + BlockSizes[i]; BlockOffsets[i] = BlockOffsets[i - 1] + BlockSizes[i]; } con.Seek((int)pos, 0); } }
public void ReadHeader(Stream s) { header = new HeaderStruct(); header.Magic = ReadU32(s); header.Version = ReadU32(s); header.Header08 = ReadU64(s); header.Header10 = ReadU32(s); header.FileCount = ReadU32(s); header.Header18 = ReadU32(s); header.Header1C = ReadU32(s); header.Header20 = ReadU32(s); header.Header24 = ReadU32(s); header.Header28 = ReadU32(s); header.Header2C = ReadU32(s); header.Header30 = ReadU32(s); header.Header34 = ReadU32(s); header.Header38 = ReadU32(s); ReadU32(s); }
public void Read(Stream s, HeaderStruct header) { Header = header; MyOffset = (uint)s.Position; Hash = s.ReadToBuffer(16); BlockSizeIndex = s.ReadUInt32(); UncompressedSize = s.ReadUInt32(); UncompressedSizeAdder = (byte)s.ReadByte(); RealUncompressedSize = UncompressedSize + UncompressedSizeAdder << 32; //... This does nothing? DataOffset = s.ReadUInt32(); DataOffsetAdder = (byte)s.ReadByte(); RealDataOffset = DataOffset + DataOffsetAdder << 32; //... This does nothing? if ((int)BlockSizeIndex == -1) { BlockOffsets = new long[1]; BlockOffsets[0] = RealDataOffset; BlockSizes = new ushort[1]; BlockSizes[0] = (ushort)UncompressedSize; BlockTableOffset = 0; } else { //Todo: Finish this someday to make DLCPackage work with Testpatch added files //int numBlocks = (int)Math.Ceiling(UncompressedSize / (double)header.MaxBlockSize); ////if (con.isLoading) ////{ //BlockOffsets = new long[numBlocks]; //BlockSizes = new ushort[numBlocks]; ////} //BlockOffsets[0] = RealDataOffset; //long pos = con.Memory.Position; //con.Seek((int)getBlockOffset((int)BlockSizeIndex, header.EntryOffset, header.FileCount), SeekOrigin.Begin); //BlockTableOffset = con.Memory.Position; //BlockSizes[0] = con + BlockSizes[0]; //for (int i = 1; i < numBlocks; i++) //{ // BlockSizes[i] = con + BlockSizes[i]; // BlockOffsets[i] = BlockOffsets[i - 1] + BlockSizes[i]; //} //con.Seek((int)pos, SeekOrigin.Begin); } }
public Pbp Load(Stream Stream) { this.Stream = Stream; this.Header = Stream.ReadStruct<HeaderStruct>(); this.Files = new Dictionary<string, Stream>(); if (Header.Magic != HeaderStruct.MagicEnum.ExpectedValue) { throw(new Exception("Not a PBP file")); } var Offsets = Header.Offsets.Concat(new[] { (uint)Stream.Length }).ToArray(); for (int n = 0; n < 8; n++) { Files[Names[n]] = Stream.SliceWithBounds(Offsets[n + 0], Offsets[n + 1]); } return this; }
public Pbp Load(Stream stream) { Stream = stream; Header = stream.ReadStruct <HeaderStruct>(); Files = new Dictionary <string, Stream>(); if (Header.Magic != HeaderStruct.MagicEnum.ExpectedValue) { throw new Exception("Not a PBP file"); } var offsets = Header.Offsets.Concat(new[] { (uint)stream.Length }).ToArray(); for (int n = 0; n < 8; n++) { Files[Names[n]] = stream.SliceWithBounds(offsets[n + 0], offsets[n + 1]); } return(this); }
public void RoundTripHeader() { var pixels = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 }; var pixelData = new BitArray(pixels); HeaderStruct src = new HeaderStruct() { DataLength = 1024 + 255, BitsPerPixel = 255 }; int pixelPtr; pixelData = _imageMaker.WriteHeader(pixelData, src, out pixelPtr); HeaderStruct result = _dataMaker.ReadHeader(pixelData, out pixelPtr); Assert.AreEqual((HeaderStruct.BitSize * 4) / (3 * 32) + 2, pixelPtr); Assert.AreEqual(src.DataLength, result.DataLength); Assert.AreEqual(src.BitsPerPixel, result.BitsPerPixel); }
private static void setColumnHeader(Sheet sheet, int rowStartIndex, int colStartIndex, List <HeaderStruct> listTitle) { int colIndex = 0; int rowCount = 1; Row headerRow = null; int beginRow = rowStartIndex; for (int i = 0; i < listTitle.Count; i++) { if (listTitle[i].rowSpan > rowCount) { rowCount = listTitle[i].rowSpan; colStartIndex = i; } } for (int i = 0; i < rowCount; i++) { if (sheet.GetRow(i + beginRow) == null) { headerRow = sheet.CreateRow(i + beginRow); } } colIndex = colStartIndex; for (int k = 0; k < listTitle.Count; k++) { HeaderStruct title = listTitle[k]; for (int i = 0; i < title.rowSpan; i++) { headerRow = sheet.GetRow(i + beginRow); for (int j = 0; j < title.colSpan; j++) { headerRow.CreateCell(colIndex + j).SetCellValue(title.headerText); } } sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(beginRow, beginRow + title.rowSpan - 1, colIndex, colIndex + title.colSpan - 1)); colIndex += title.colSpan; } }
public void Serialize(SerializingFile con) { if (con.isLoading) { Header = new HeaderStruct(); } Header.Serialize(con); con.Seek((int)Header.EntryOffset, SeekOrigin.Begin); if (con.isLoading) { Files = new FileEntryStruct[Header.FileCount]; } for (int i = 0; i < Header.FileCount; i++) { Files[i].Serialize(con, Header); } if (con.isLoading) { ReadFileNames(con); } }
public Psf Load(Stream Stream) { EntryDictionary = new Dictionary<string, object>(); Header = Stream.ReadStruct<HeaderStruct>(); Entries = Stream.ReadStructVector<EntryStruct>(Header.NumberOfPairs); KeysStream = Stream.SliceWithLength(Header.KeyTable); ValuesStream = Stream.SliceWithLength(Header.ValueTable); foreach (var Entry in Entries) { var Key = KeysStream.ReadStringzAt(Entry.KeyOffset); var ValueStream = ValuesStream.SliceWithLength(Entry.ValueOffset, Entry.ValueSize);; switch (Entry.DataType) { case DataType.Binary: EntryDictionary[Key] = ValueStream.ReadAll(); break; case DataType.Int: EntryDictionary[Key] = ValueStream.ReadStruct<int>(); break; case DataType.Text: EntryDictionary[Key] = ValueStream.ReadStringz(-1, Encoding.UTF8); break; default: throw(new NotImplementedException()); } } return this; }
public TSS Load(Stream Stream) { this.Stream = Stream; var BinaryReader = new BinaryReader(Stream); Header = Stream.ReadStruct <HeaderStruct>(); if (Header.Magic != "TSS") { throw(new Exception("Not a TSS file!")); } this.CodeStream = new MemoryStream(SliceStream.CreateWithBounds(Stream, Header.CodeStart, Header.TextStart).ReadAll()); var TextData = SliceStream.CreateWithLength(Stream, Header.TextStart, Header.TextLen).ReadAll(); this.TextStream = new MemoryStream(); this.TextStream.WriteBytes(TextData); this.TextStream.Position = 0; return(this); }
public void Serialize(SerializingContainer con) { if (con.isLoading) { Header = new HeaderStruct(); } DebugLog.PrintLn("Header..."); Header.Serialize(con); con.Seek((int)Header.EntryOffset, SeekOrigin.Begin); if (con.isLoading) { Files = new FileEntryStruct[Header.FileCount]; } DebugLog.PrintLn("Files..."); for (int i = 0; i < Header.FileCount; i++) { Files[i].Serialize(con, Header); } if (con.isLoading) { ReadFileNames(con); } }
override public void Load(Stream Stream) { this.Stream = Stream; this.Header = this.Stream.ReadStruct <HeaderStruct>(); if (this.Header.Magic != "TO8SCEL") { throw (new Exception("Invalid Magic")); } if (this.Header.ListCount > 10000) { throw (new Exception("List too big")); } this.Stream.Position = Header.ListStart; for (int n = 0; n < Header.ListCount; n++) { var Entry = new Entry(this, this.Stream.ReadStruct <EntryStruct>(), n); //Console.WriteLine(Entry.EntryStruct); Entries.Add(Entry); } }
public static void ParsePacket(byte[] packet, out byte command, out byte[] payload) { // NOTE: Assumes packet has a complete HLDC frame (excluding the SOF/EOF bytes) // Use the HLDCFramer helper class to receive a complete HLDC frame var headerBytes = new ArraySegment <byte>(packet, 0, HEADER_SIZE).ToArray(); HeaderStruct header = StructInterop.ByteArrayToStruct <HeaderStruct>(headerBytes); // Payload if (header.length > 0) { payload = new ArraySegment <byte>(packet, HEADER_SIZE, header.length).ToArray(); } else { payload = null; } var footerBytes = new ArraySegment <byte>(packet, HEADER_SIZE + header.length, FOOTER_SIZE).ToArray(); FooterStruct footer = StructInterop.ByteArrayToStruct <FooterStruct>(footerBytes); // Detect protocol errors if (header.command == CMD_ERROR) { if (payload != null && payload.Length > 0) { throw HLDCProtocolException.FromCode(payload[0]); } else { throw new HLDCException("HLDC Malformed Error Message"); } } command = header.command; }
public void Load(Stream data, bool fast = false) { uint headersize = Helpers.ReadLEUInt(data); long startoffset = data.Position; Header = new HeaderStruct(); Header.magic = Helpers.ReadLEUInt(data); Header.totalCount = Helpers.ReadLEUInt(data); Header.ebxCount = Helpers.ReadLEUInt(data); Header.resCount = Helpers.ReadLEUInt(data); Header.chunkCount = Helpers.ReadLEUInt(data); Header.stringOffset = Helpers.ReadLEUInt(data); Header.chunkMetaOffset = Helpers.ReadLEUInt(data); Header.chunkMetaSize = Helpers.ReadLEUInt(data); ReadSha1List(data); ReadEbxList(data); ReadResList(data); ReadChunkList(data); if (Header.chunkCount != 0) { ChunkMeta = BJSON.ReadField(data); } else { ChunkMeta = null; } data.Seek(startoffset + Header.stringOffset, 0); ReadEbxListNames(data); data.Seek(startoffset + Header.stringOffset, 0); ReadResListNames(data); data.Seek(startoffset + headersize, 0); ReadEbxListData(data, fast); ReadResListData(data, fast); ReadChunkListData(data, fast); ApplySHA1s(); }
public void Serialize(SerializingFile con) { if (con.isLoading) Header = new HeaderStruct(); Header.Serialize(con); con.Seek((int)Header.EntryOffset, SeekOrigin.Begin); if (con.isLoading) Files = new FileEntryStruct[Header.FileCount]; for (int i = 0; i < Header.FileCount; i++) Files[i].Serialize(con, Header); if (con.isLoading) ReadFileNames(con); }
public PGF Load(Stream FileStream) { this.Header = FileStream.ReadStruct<HeaderStruct>(); if (this.Header.Revision >= 3) { this.HeaderExtraRevision3 = FileStream.ReadStruct<HeaderRevision3Struct>(); } FileStream.ReadStructVector(ref DimensionTable, Header.TableDimLength); FileStream.ReadStructVector(ref XAdjustTable, Header.TableXAdjustLength); FileStream.ReadStructVector(ref YAdjustTable, Header.TableYAdjustLength); FileStream.ReadStructVector(ref AdvanceTable, Header.TableAdvanceLength); PackedShadowCharMap = FileStream.ReadBytes(BitsToBytesHighAligned(Header.TableShadowMapLength * Header.TableShadowMapBpe)); if (Header.Revision == 3) { FileStream.ReadStructVector(ref CharmapCompressionTable1, HeaderExtraRevision3.TableCompCharMapLength1); FileStream.ReadStructVector(ref CharmapCompressionTable2, HeaderExtraRevision3.TableCompCharMapLength2); } PackedCharMap = FileStream.ReadBytes(BitsToBytesHighAligned(Header.TableCharMapLength * Header.TableCharMapBpe)); PackedCharPointerTable = FileStream.ReadBytes(BitsToBytesHighAligned(Header.TableCharPointerLength * Header.TableCharPointerBpe)); /* int BytesLeft = (int)(FileStream.Length - FileStream.Position); charData = new byte[BytesLeft]; FileStream.Read(charData, 0, BytesLeft); */ CharData = FileStream.ReadBytes((int)(FileStream.Length - FileStream.Position)); var NumberOfCharacters = Header.TableCharPointerLength; CharMap = new int[Header.FirstGlyph + Header.LastGlyph + 1]; CharPointer = new int[NumberOfCharacters]; Glyphs = new Glyph[NumberOfCharacters]; ReverseCharMap = new Dictionary<int, int>(); ShadowCharMap = new Dictionary<int, int>(); ReverseShadowCharMap = new Dictionary<int, int>(); foreach (var Pair in BitReader.FixedBitReader(PackedShadowCharMap, Header.TableShadowMapBpe)) { var UnicodeIndex = (int)Pair.Key + Header.FirstGlyph; var GlyphIndex = (int)Pair.Value; ShadowCharMap[UnicodeIndex] = GlyphIndex; ReverseShadowCharMap[GlyphIndex] = UnicodeIndex; } foreach (var Pair in BitReader.FixedBitReader(PackedCharMap, Header.TableCharMapBpe)) { var UnicodeIndex = (int)Pair.Key + Header.FirstGlyph; var GlyphIndex = (int)Pair.Value; CharMap[UnicodeIndex] = GlyphIndex; ReverseCharMap[GlyphIndex] = UnicodeIndex; } foreach (var Pair in BitReader.FixedBitReader(PackedCharPointerTable, Header.TableCharPointerBpe)) { CharPointer[Pair.Key] = (int)Pair.Value; } /* for (int n = 0; n < NumberOfCharacters; n++) { Glyphs[n] = new Glyph().Read(this, n); } */ Console.WriteLine(this.Header.FontName); /* Console.WriteLine(this.header.fontName); for (int n = 0; n < 300; n++) { Console.WriteLine(GetGlyphId((char)n)); } */ return this; }
public DosExe LoadHeader(Stream ExeStream) { this.Header = ExeStream.ReadStruct<DosExe.HeaderStruct>(); return this; }
protected byte[] DecryptPRX2(byte[] _pbIn, bool ShowInfo = false) { int size = (int)_pbIn.Length; var _pbOut = new byte[size]; _pbIn.CopyTo(_pbOut, 0); var _tmp1 = new byte[0x150]; var _tmp2 = new byte[0x90 + 0x14]; var _tmp3 = new byte[0x60 + 0x14]; fixed (byte* inbuf = _pbIn) fixed (byte* outbuf = _pbOut) fixed (byte* tmp1 = _tmp1) fixed (byte* tmp2 = _tmp2) fixed (byte* tmp3 = _tmp3) { var HeaderPointer = (HeaderStruct*)inbuf; this.Header = *(HeaderStruct*)inbuf; var pti = GetTagInfo2(this.Header.Tag); Console.WriteLine("{0}", pti); int retsize = *(int *)&inbuf[0xB0]; PointerUtils.Memset(_tmp1, 0, 0x150); PointerUtils.Memset(_tmp2, 0, 0x90 + 0x14); PointerUtils.Memset(_tmp3, 0, 0x60 + 0x14); PointerUtils.Memcpy(outbuf, inbuf, size); if (size < 0x160) { throw (new InvalidDataException("buffer not big enough, ")); } if ((size - 0x150) < retsize) { throw (new InvalidDataException("not enough data, ")); } PointerUtils.Memcpy(tmp1, outbuf, 0x150); int i, j; //byte *p = tmp2+0x14; for (i = 0; i < 9; i++) { for (j = 0; j < 0x10; j++) { _tmp2[0x14 + (i << 4) + j] = pti.key[j]; } _tmp2[0x14 + (i << 4)] = (byte)i; } if (Scramble((uint *)tmp2, 0x90, pti.code) < 0) { throw (new InvalidDataException("error in Scramble#1, ")); } PointerUtils.Memcpy(outbuf, tmp1 + 0xD0, 0x5C); PointerUtils.Memcpy(outbuf + 0x5C, tmp1 + 0x140, 0x10); PointerUtils.Memcpy(outbuf + 0x6C, tmp1 + 0x12C, 0x14); PointerUtils.Memcpy(outbuf + 0x80, tmp1 + 0x080, 0x30); PointerUtils.Memcpy(outbuf + 0xB0, tmp1 + 0x0C0, 0x10); PointerUtils.Memcpy(outbuf + 0xC0, tmp1 + 0x0B0, 0x10); PointerUtils.Memcpy(outbuf + 0xD0, tmp1 + 0x000, 0x80); PointerUtils.Memcpy(tmp3 + 0x14, outbuf + 0x5C, 0x60); if (Scramble((uint *)tmp3, 0x60, pti.code) < 0) { throw (new InvalidDataException("error in Scramble#2, ")); } PointerUtils.Memcpy(outbuf + 0x5C, tmp3, 0x60); PointerUtils.Memcpy(tmp3, outbuf + 0x6C, 0x14); PointerUtils.Memcpy(outbuf + 0x70, outbuf + 0x5C, 0x10); PointerUtils.Memset(outbuf + 0x18, 0, 0x58); PointerUtils.Memcpy(outbuf + 0x04, outbuf, 0x04); *((uint *)outbuf) = 0x014C; PointerUtils.Memcpy(outbuf + 0x08, tmp2, 0x10); /* sha-1 */ if (Kirk.hleUtilsBufferCopyWithRange(outbuf, 3000000, outbuf, 3000000, Core.Crypto.Kirk.CommandEnum.PSP_KIRK_CMD_SHA1_HASH) != Core.Crypto.Kirk.ResultEnum.OK) { throw (new InvalidDataException("error in sceUtilsBufferCopyWithRange 0xB, ")); } if (PointerUtils.Memcmp(outbuf, tmp3, 0x14) != 0) { throw (new InvalidDataException("WARNING (SHA-1 incorrect), ")); } int iXOR; for (iXOR = 0; iXOR < 0x40; iXOR++) { tmp3[iXOR+0x14] = (byte)(outbuf[iXOR+0x80] ^ _tmp2[iXOR+0x10]); } if (Scramble((uint *)tmp3, 0x40, pti.code) != 0) { throw (new InvalidDataException("error in Scramble#3, ")); } for (iXOR = 0x3F; iXOR >= 0; iXOR--) { outbuf[iXOR+0x40] = (byte)(_tmp3[iXOR] ^ _tmp2[iXOR+0x50]); // uns 8 } PointerUtils.Memset(outbuf + 0x80, 0, 0x30); *(uint *)&outbuf[0xA0] = 1; PointerUtils.Memcpy(outbuf + 0xB0, outbuf + 0xC0, 0x10); PointerUtils.Memset(outbuf + 0xC0, 0, 0x10); // the real decryption var ret = Kirk.hleUtilsBufferCopyWithRange(outbuf, size, outbuf + 0x40, size - 0x40, Core.Crypto.Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT_PRIVATE); if (ret != 0) { throw (new InvalidDataException(String.Format("error in sceUtilsBufferCopyWithRange 0x1 (0x{0:X}), ", ret))); } if (retsize < 0x150) { // Fill with 0 PointerUtils.Memset(outbuf + retsize, 0, 0x150 - retsize); } return _pbOut.Slice(0, retsize).ToArray(); } }
/// <summary> /// /// </summary> /// <param name="Stream"></param> public void SetStream(Stream Stream) { this.Stream = Stream; // Read the header. this.Header = this.Stream.ReadStruct<HeaderStruct>(); if (this.Header.Magic != DAXFILE_SIGNATURE) { throw (new InvalidDataException("Not a DAX File")); } var TotalBlocks = Header.TotalBlocks; //Header.TotalBlocks var Offsets = this.Stream.ReadStructVector<uint>(TotalBlocks); var Sizes = this.Stream.ReadStructVector<ushort>(TotalBlocks); NCArea[] NCAreas = null; if (Header.Version >= Version.DAXFORMAT_VERSION_1) { NCAreas = this.Stream.ReadStructVector<NCArea>(Header.NCAreas); } Blocks = new BlockInfo[TotalBlocks]; for (int n = 0; n < TotalBlocks; n++) { Blocks[n].Position = Offsets[n]; Blocks[n].Length = Sizes[n]; Blocks[n].IsCompressed = true; } if (Header.Version >= Version.DAXFORMAT_VERSION_1) { foreach (var NCArea in NCAreas) { //Console.WriteLine("{0}-{1}", NCArea.frame, NCArea.size); for (int n = 0; n < NCArea.Size; n++) { Blocks[NCArea.Frame + n].IsCompressed = false; } } } }
public void SerializeHeader(SerializingContainer Container) { if (Container.isLoading) Header = new HeaderStruct(); Header.Magic = Container + Header.Magic; Header.unk1 = Container + Header.unk1; Header.unk2 = Container + Header.unk2; int count = 0; if (!Container.isLoading) count = Header.Licensee.Length; else Header.Licensee = ""; Header.Licensee = SerializeString(Container, Header.Licensee); count = 0; if (!Container.isLoading) count = Header.Project.Length; else Header.Project = ""; Header.Project = SerializeString(Container, Header.Project); Header.unk3 = Container + Header.unk3; Header.unk4 = Container + Header.unk4; count = 0; if (!Container.isLoading) count = Header.Nodes.Length; count = Container + count; if (Container.isLoading) Header.Nodes = new HNodeStruct[count]; for (int i = 0; i < count; i++) { if (Container.isLoading) Header.Nodes[i] = new HNodeStruct(); HNodeStruct t = Header.Nodes[i]; t.unk1 = Container + t.unk1; t.unk2 = Container + t.unk2; t.Name = SerializeString(Container, t.Name); t.unk3 = Container + t.unk3; Header.Nodes[i] = t; } count = 0; if (!Container.isLoading) count = Header.Names.Length; count = Container + count; if (Container.isLoading) Header.Names = new string[count]; for (int i = 0; i < count; i++) Header.Names[i] = SerializeString(Container, Header.Names[i]); }
public static HeaderStruct ReadHeader(Stream s, int rev) { Header = new HeaderStruct(s, rev); return(Header); }
public Xex LoadHeader(Stream XexStream) { Header = XexStream.ReadStruct<HeaderStruct>(); var OptionalHeaders = XexStream.ReadStructVector<OptionalHeader>(Header.OptionalHeaderCount); Console.WriteLine("{0:X}", XexStream.Position); InfoList = new Dictionary<OptionalHeader.Ids, ulong>(); foreach (var OptionalHeader in OptionalHeaders) { //Console.WriteLine("{0}: 0x{1:X}", OptionalHeader.Id, (uint)OptionalHeader.Data); //InfoList[OptionalHeader.Id] = OptionalHeader.Data; InfoList.Add(OptionalHeader.Id, OptionalHeader.Data); switch (OptionalHeader.Id) { case Xex.OptionalHeader.Ids.OriginalPEName: this.OriginalPeName = LoadChunk(XexStream, OptionalHeader.Data).ReadStringz(Encoding: Encoding.UTF8); break; case Xex.OptionalHeader.Ids.LANKey: this.LanKey = XexStream.SliceWithLength(OptionalHeader.Data, 0x10).ReadAll(); break; case Xex.OptionalHeader.Ids.DefaultStackSize: this.DefaultStackSize = OptionalHeader.Data; break; case Xex.OptionalHeader.Ids.ChecksumTimestamp: this.ChecksumTimestamp = XexStream.SliceWithLength(OptionalHeader.Data).ReadStruct<ChecksumTimestampStruct>(); break; case Xex.OptionalHeader.Ids.ImageBaseAddress: this.ImageBaseAddress = OptionalHeader.Data; break; case Xex.OptionalHeader.Ids.EntryPoint: this.EntryPoint = OptionalHeader.Data; break; case Xex.OptionalHeader.Ids.StaticLibraries: this.StaticLibs = LoadChunk(XexStream, OptionalHeader.Data).ReadStructVectorUntilTheEndOfStream<StaticLib>(); foreach (var StaticLib in StaticLibs) { Console.WriteLine("StaticLib: {0}", StaticLib.ToStringDefault()); } break; case Xex.OptionalHeader.Ids.ImportLibraries: { var ImportLibrariesStream = LoadChunk(XexStream, OptionalHeader.Data); var TextLength = (uint)ImportLibrariesStream.ReadStruct<uint_be>(); var LibraryCount = (uint)ImportLibrariesStream.ReadStruct<uint_be>(); var TextStream = ImportLibrariesStream.ReadStream(TextLength); var LibraryNames = new String[LibraryCount]; for (int n = 0; n < LibraryCount; n++) { LibraryNames[n] = TextStream.ReadStringz(AllowEndOfStream: false); Console.WriteLine("ImportLib: {0}", LibraryNames[n]); } var ChunkUnk1 = ReadChunkIncludingTheSize(ImportLibrariesStream); var ImportAddressList = ReadChunkIncludingTheSize(ImportLibrariesStream); Console.Error.WriteLine("@TODO: Xex.OptionalHeader.Ids.ImportLibraries"); } break; case Xex.OptionalHeader.Ids.TLSInfo: { this.TLSInfo = XexStream.SliceWithLength(OptionalHeader.Data).ReadStruct<TLSInfoStruct>(); } break; default: Console.WriteLine("{0}: 0x{1:X}", OptionalHeader.Id, (uint)OptionalHeader.Data); break; } } PeStream = XexStream.SliceWithLength(Header.PeDataOffset); Pe = new Pe().LoadHeader(PeStream); Console.WriteLine("SecurityInfoOffset: {0:X}", (uint)Header.SecurityInfoOffset); var SecurityStream = LoadChunk(XexStream, Header.SecurityInfoOffset); var NumberOfSections = (ushort)SecurityStream.ReadStruct<ushort_be>(); Console.WriteLine("NumberOfSections: {0:X}", NumberOfSections); return this; }
public void SerializeHeader(SerializingContainer Container) { if (Container.isLoading) { Header = new HeaderStruct(); } Header.Magic = Container + Header.Magic; Header.unk1 = Container + Header.unk1; Header.unk2 = Container + Header.unk2; int count = 0; if (!Container.isLoading) { count = Header.Licensee.Length; } else { Header.Licensee = ""; } Header.Licensee = SerializeString(Container, Header.Licensee); count = 0; if (!Container.isLoading) { count = Header.Project.Length; } else { Header.Project = ""; } Header.Project = SerializeString(Container, Header.Project); Header.unk3 = Container + Header.unk3; Header.unk4 = Container + Header.unk4; count = 0; if (!Container.isLoading) { count = Header.Nodes.Length; } count = Container + count; if (Container.isLoading) { Header.Nodes = new HNodeStruct[count]; } for (int i = 0; i < count; i++) { if (Container.isLoading) { Header.Nodes[i] = new HNodeStruct(); } HNodeStruct t = Header.Nodes[i]; t.unk1 = Container + t.unk1; t.unk2 = Container + t.unk2; t.Name = SerializeString(Container, t.Name); t.unk3 = Container + t.unk3; Header.Nodes[i] = t; } count = 0; if (!Container.isLoading) { count = Header.Names.Length; } count = Container + count; if (Container.isLoading) { Header.Names = new string[count]; } for (int i = 0; i < count; i++) { Header.Names[i] = SerializeString(Container, Header.Names[i]); } }
/// <summary> /// /// </summary> /// <param name="Stream"></param> public void Load(byte[] inbuf) { var Kirk = new Kirk(); Kirk.kirk_init(); var outbuf = new byte[(int)inbuf.Length]; inbuf.CopyTo(outbuf, 0); fixed(byte *_inbuf = inbuf) fixed(byte *_outbuf = outbuf) { var HeaderPointer = (HeaderStruct *)_inbuf; this.Header = *(HeaderStruct *)_inbuf; var pti = g_oldTagInfo.Where(Tag => Tag.tag == this.Header.tag).Single(); for (int i = 0; i < 0x150; i++) { outbuf[i] = 0; } for (int i = 0; i < 0x40; i++) { outbuf[i] = 0x55; } //HeaderPointer->psp_size = 5; //HeaderPointer->boot_entry = 0; var h7_header = (Kirk.KIRK_AES128CBC_HEADER *) & _outbuf[0x2C]; h7_header->Mode = 5; h7_header->Unknown4 = 0; h7_header->Unknown8 = 0; h7_header->KeySeed = pti.code; h7_header->Datasize = 0x70; byte[] header = new byte[0x150]; Array.Copy(inbuf, 0xD0, header, 0x00, 0x80); Array.Copy(inbuf, 0x80, header, 0x80, 0x50); Array.Copy(inbuf, 0x00, header, 0xD0, 0x80); if (pti.codeExtra != 0) { //ScramblePRXV2(header, (byte)pti.codeExtra); throw (new NotImplementedException()); } Array.Copy(header, 0x40, outbuf, 0x40, 0x40); for (int iXOR = 0; iXOR < 0x70; iXOR++) { outbuf[0x40 + iXOR] = (byte)(outbuf[0x40 + iXOR] ^ (byte)pti.key[0x14 + iXOR]); } for (int k = 0; k < 0x30; k++) { outbuf[k + 0x80] = 0; } // Scramble the data by calling CMD7. var bScrambleOut = new byte[outbuf.Length]; var bScrambleIn = outbuf; fixed(byte *_bScrambleOut = bScrambleOut) { Kirk.hleUtilsBufferCopyWithRange(_bScrambleOut, 0x70, &_outbuf[0x2C], 0x70, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT); } for (int iXOR = 0x6F; iXOR >= 0; iXOR--) { outbuf[0x40 + iXOR] = (byte)(outbuf[0x2C + iXOR] ^ (byte)pti.key[0x20 + iXOR]); } for (int k = 0; k < 0x30; k++) { outbuf[k + 0x80] = 0; } var KirkHeader = (Kirk.AES128CMACHeader *) & _outbuf[0x40]; KirkHeader->Mode = (uint)Kirk.KIRK_MODE_CMD1; Array.Copy(inbuf, 0x40 + 0x70, outbuf, 0x40 + 0x70, 0x20); Array.Copy(inbuf, 0, outbuf, 0x40 + 0x90, 0x80); // Call KIRK CMD1 for final decryption. /* * ByteBuffer bDataOut = ByteBuffer.wrap(outbuf); * ByteBuffer bHeaderIn = bDataOut.duplicate(); * bHeaderIn.position(0x40); * hleUtilsBufferCopyWithRange(bDataOut, size, bHeaderIn, size, 0x1); */ //#if true { int size = outbuf.Length; var Result = Kirk.hleUtilsBufferCopyWithRange(&_outbuf[0x00], size, (byte *)KirkHeader, size, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT_PRIVATE, true); Console.WriteLine(Result); } //#endif File.WriteAllBytes("../../../TestInput/temp.bin", outbuf); //File.write //outbuf //Kirk.kirk_CMD1(); //Console.WriteLine("{0:X}", this.Header.tag); } }
public void Load(Stream DatStream, Stream DavStream) { if (DavStream == null) { DavStream = DatStream; } this.DatStream = DatStream; this.DavStream = DavStream; Header = DatStream.ReadStruct <HeaderStruct>(); var Magic = Encoding.ASCII.GetString(Header.Magic); if (Magic != "FPS4") { throw (new Exception(String.Format("Invalid Magic '{0}'", Magic))); } if (Header.FilePos != 0) { DatStream.Position = Header.FilePos; OriginalFilePath = DatStream.ReadStringz(0x100, Encoding.GetEncoding("Shift-JIS")); } else { OriginalFilePath = ""; } //Console.WriteLine("{0:X8}", Stream.Position); if (Header.ListCount > 10000) { throw (new Exception("List too big (" + Header.ListCount + ")")); } bool UseIndices = false; foreach (var Pass in new[] { false, true }) { DatStream.Position = Header.ListStart; for (uint n = 0; n < Header.ListCount; n++) { var EntryOffset = DatStream.Position; var EntryStream = DatStream.ReadStream(Header.EntrySizeof); var EntryStruct = default(EntryStruct); var ExtraEntrySizeof = Header.EntrySizeof - 0x0C; var IndexName = String.Format("{0}", n); var Name = ""; uint MappedFileIndex = 0; uint StringOffset = 0; // @TODO: EntryFormat probably is a bitfield // or a composed enum + bit field // I don't know the bit mapping. switch ((int)Header.EntryFormat) { case 0x8D: EntryStruct.Offset = EntryStream.ReadStruct <uint_be>(); EntryStruct.LengthReal = EntryStruct.LengthSectorAligned = EntryStream.ReadStruct <uint_be>(); Name = EntryStream.ReadStringz(0x20); MappedFileIndex = EntryStream.ReadStruct <uint_be>(); break; case 0x47: { EntryStruct = EntryStream.ReadStruct <EntryStruct>(); StringOffset = EntryStream.ReadStruct <uint_be>(); if (StringOffset != 0) { var NameStream = DatStream.SliceWithLength(StringOffset, 0x1000); Name = NameStream.ReadStringz(); //File.WriteAllBytes("c:/temp/name" + StringOffset + ".bin", NameStream.Slice().ReadAll()); //Console.WriteLine("{0:X8}: '{1}'", StringOffset, Name); } else { if (EntryStruct.LengthReal == 0) { continue; } } } break; case 0x4F: { EntryStruct = EntryStream.ReadStruct <EntryStruct>(); Name = EntryStream.ReadStringz(0x20); MappedFileIndex = EntryStream.ReadStruct <uint_be>(); } break; default: { EntryStruct = EntryStream.ReadStruct <EntryStruct>(); Name = EntryStream.ReadStringz(ExtraEntrySizeof); switch (ExtraEntrySizeof) { case 0: { } break; case 4: { StringOffset = EntryStream.ReadStruct <uint>(); // Pointer to string name. if (StringOffset != 0) { throw (new NotImplementedException()); } } break; default: { EntryStream.Position = EntryStream.Length - ExtraEntrySizeof; // (0xC para common.svo y btl.svo, en los otros no sé) --- Apaño temporal Name = EntryStream.ReadStringz(ExtraEntrySizeof); } break; } //Console.WriteLine("OFF:{0:X8}", EntryOffset); //Console.WriteLine("STR:{0}", EntryStruct.ToStringDefault()); //Console.WriteLine("NAM'{0}'({1})", Name, Name.Length); } break; } //Console.WriteLine("Name: {0}", Name); if (n == Header.ListCount - 1) { // Ignore last element with an empty name. if (Name.Length == 0 && EntryStruct.LengthReal == 0) { continue; } } if (IndexName.Length == 0) { IndexName = String.Format("{0}", n); } if (Name.Length == 0) { UseIndices = true; } if (UseIndices) { Name = IndexName; } //Console.WriteLine("Name: '{0}'", Encoding.UTF8.GetBytes(Name).ToStringArray()); if (Entries.ContainsKey(Name)) { //Console.Error.WriteLine("Warning: Name '{0}' already contained", Name); Name = n + "." + Name; } if (Pass) { var Entry = new Entry(this, EntryStruct, Name); Entry.MappedFileIndex = MappedFileIndex; Entry.Index = n; Entries[Name] = Entry; } } } }
public Pgf Load(Stream fileStream) { Header = fileStream.ReadStruct <HeaderStruct>(); if (Header.Revision >= 3) { HeaderExtraRevision3 = fileStream.ReadStruct <HeaderRevision3Struct>(); } fileStream.ReadStructVector(out DimensionTable, Header.TableDimLength); fileStream.ReadStructVector(out XAdjustTable, Header.TableXAdjustLength); fileStream.ReadStructVector(out YAdjustTable, Header.TableYAdjustLength); fileStream.ReadStructVector(out AdvanceTable, Header.TableAdvanceLength); PackedShadowCharMap = fileStream.ReadBytes(BitsToBytesHighAligned(Header.TableShadowMapLength * Header.TableShadowMapBpe)); if (Header.Revision == 3) { fileStream.ReadStructVector(out CharmapCompressionTable1, HeaderExtraRevision3.TableCompCharMapLength1); fileStream.ReadStructVector(out CharmapCompressionTable2, HeaderExtraRevision3.TableCompCharMapLength2); } PackedCharMap = fileStream.ReadBytes(BitsToBytesHighAligned(Header.TableCharMapLength * Header.TableCharMapBpe)); PackedCharPointerTable = fileStream.ReadBytes( BitsToBytesHighAligned(Header.TableCharPointerLength * Header.TableCharPointerBpe)); /* * int BytesLeft = (int)(FileStream.Length - FileStream.Position); * charData = new byte[BytesLeft]; * FileStream.Read(charData, 0, BytesLeft); */ CharData = fileStream.ReadBytes((int)(fileStream.Length - fileStream.Position)); var numberOfCharacters = Header.TableCharPointerLength; CharMap = new int[Header.FirstGlyph + Header.LastGlyph + 1]; CharPointer = new int[numberOfCharacters]; Glyphs = new IGlyph[numberOfCharacters]; ReverseCharMap = new Dictionary <int, int>(); ShadowCharMap = new Dictionary <int, int>(); ReverseShadowCharMap = new Dictionary <int, int>(); foreach (var pair in BitReader.FixedBitReader(PackedShadowCharMap, Header.TableShadowMapBpe)) { var unicodeIndex = (int)pair.Key + Header.FirstGlyph; var glyphIndex = (int)pair.Value; ShadowCharMap[unicodeIndex] = glyphIndex; ReverseShadowCharMap[glyphIndex] = unicodeIndex; } foreach (var pair in BitReader.FixedBitReader(PackedCharMap, Header.TableCharMapBpe)) { var unicodeIndex = (int)pair.Key + Header.FirstGlyph; var glyphIndex = (int)pair.Value; CharMap[unicodeIndex] = glyphIndex; ReverseCharMap[glyphIndex] = unicodeIndex; } foreach (var pair in BitReader.FixedBitReader(PackedCharPointerTable, Header.TableCharPointerBpe)) { CharPointer[pair.Key] = (int)pair.Value; } /* * for (int n = 0; n < NumberOfCharacters; n++) * { * Glyphs[n] = new Glyph().Read(this, n); * } */ Console.WriteLine(Header.FontName); /* * Console.WriteLine(this.header.fontName); * for (int n = 0; n < 300; n++) * { * Console.WriteLine(GetGlyphId((char)n)); * } */ return(this); }
public void Serialize(SerializingContainer con) { if (con.isLoading) Header = new HeaderStruct(); DebugLog.PrintLn("Header..."); Header.Serialize(con); con.Seek((int)Header.EntryOffset, SeekOrigin.Begin); if (con.isLoading) Files = new FileEntryStruct[Header.FileCount]; DebugLog.PrintLn("Files..."); for (int i = 0; i < Header.FileCount; i++) Files[i].Serialize(con, Header); if (con.isLoading) ReadFileNames(con); }
public void Serialize(SerializingFile con, HeaderStruct header) { Header = header; MyOffset = (uint)con.GetPos(); isActualFile = true; //default to true if (con.isLoading) { Hash = new byte[16]; } for (int i = 0; i < 16; i++) { Hash[i] = con + Hash[i]; } BlockSizeTableIndex = con + BlockSizeTableIndex; if (con.Memory.Endian == Endian.Big) { UncompressedSizeAdder = con + UncompressedSizeAdder; UncompressedSize = con + UncompressedSize; DataOffsetAdder = con + DataOffsetAdder; DataOffset = con + DataOffset; } else { UncompressedSize = con + UncompressedSize; UncompressedSizeAdder = con + UncompressedSizeAdder; DataOffset = con + DataOffset; DataOffsetAdder = con + DataOffsetAdder; } RealUncompressedSize = UncompressedSize + UncompressedSizeAdder; //<< 32 RealDataOffset = DataOffset + DataOffsetAdder; // << 32 if (BlockSizeTableIndex == 0xFFFFFFFF) //Decompressed { BlockOffsets = new long[1]; BlockOffsets[0] = RealDataOffset; BlockSizes = new ushort[1]; BlockSizes[0] = (ushort)UncompressedSize; BlockTableOffset = 0; } else //Compressed { int numBlocks = (int)Math.Ceiling(UncompressedSize / (double)header.MaxBlockSize); if (con.isLoading) { BlockOffsets = new long[numBlocks]; BlockSizes = new ushort[numBlocks]; } if (numBlocks > 0) { BlockOffsets[0] = RealDataOffset; long pos = con.Memory.Position; con.Seek((int)getBlockOffset((int)BlockSizeTableIndex, header.EntryOffset, header.FileCount), SeekOrigin.Begin); BlockTableOffset = con.Memory.Position; BlockSizes[0] = con + BlockSizes[0]; for (int i = 1; i < numBlocks; i++) //read any further blocks { BlockSizes[i] = con + BlockSizes[i]; BlockOffsets[i] = BlockOffsets[i - 1] + BlockSizes[i]; } con.Seek((int)pos, SeekOrigin.Begin); } } }
/// <summary> /// /// </summary> /// <param name="Stream"></param> public void SetStream(Stream Stream) { this.Stream = Stream; // Read the header. this.Header = this.Stream.ReadStruct<HeaderStruct>(); if (this.Header.Magic != 0x4F534943) { throw (new InvalidDataException("Not a CISO File")); } // Read the block list this.Blocks = this.Stream.ReadStructVector<BlockInfo>((uint)(NumberOfBlocks + 1)); }
protected byte[] DecryptPRX1(byte[] _pbIn, bool ShowInfo = false) { int cbTotal = (int)_pbIn.Length; var _pbOut = new byte[cbTotal]; _pbIn.CopyTo(_pbOut, 0); fixed (byte* pbIn = _pbIn) fixed (byte* pbOut = _pbOut) { var HeaderPointer = (HeaderStruct*)pbIn; this.Header = *(HeaderStruct*)pbIn; var pti = GetTagInfo(this.Header.Tag); if (ShowInfo) { Console.WriteLine("TAG_INFO: {0}", pti); } // build conversion into pbOut PointerUtils.Memcpy(pbOut, pbIn, _pbIn.Length); PointerUtils.Memset(pbOut, 0, 0x150); PointerUtils.Memset(pbOut, 0x55, 0x40); // step3 demangle in place var h7_header = (Kirk.KIRK_AES128CBC_HEADER*)&pbOut[0x2C]; h7_header->Mode = Core.Crypto.Kirk.KirkMode.DecryptCbc; h7_header->Unknown4 = 0; h7_header->Unknown8 = 0; h7_header->KeySeed = pti.code; // initial seed for PRX h7_header->Datasize = 0x70; // size // redo part of the SIG check (step2) var _buffer1 = new byte[0x150]; fixed (byte* buffer1 = _buffer1) { PointerUtils.Memcpy(buffer1 + 0x00, pbIn + 0xD0, 0x80); PointerUtils.Memcpy(buffer1 + 0x80, pbIn + 0x80, 0x50); PointerUtils.Memcpy(buffer1 + 0xD0, pbIn + 0x00, 0x80); if (pti.codeExtra != 0) { ExtraV2Mangle(buffer1 + 0x10, pti.codeExtra); } PointerUtils.Memcpy(pbOut + 0x40 /* 0x2C+20 */, buffer1 + 0x40, 0x40); } for (int iXOR = 0; iXOR < 0x70; iXOR++) { pbOut[0x40 + iXOR] = (byte)(pbOut[0x40 + iXOR] ^ pti.key[0x14 + iXOR]); } var ret = Kirk.hleUtilsBufferCopyWithRange( pbOut + 0x2C, 20 + 0x70, pbOut + 0x2C, 20 + 0x70, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT ); if (ret != 0) { throw (new Exception(CStringFormater.Sprintf("mangle#7 returned 0x%08X, ", ret))); } for (int iXOR = 0x6F; iXOR >= 0; iXOR--) { pbOut[0x40 + iXOR] = (byte)(pbOut[0x2C + iXOR] ^ pti.key[0x20 + iXOR]); } PointerUtils.Memset(pbOut + 0x80, 0, 0x30); // $40 bytes kept, clean up pbOut[0xA0] = 1; // copy unscrambled parts from header PointerUtils.Memcpy(pbOut + 0xB0, pbIn + 0xB0, 0x20); // file size + lots of zeros PointerUtils.Memcpy(pbOut + 0xD0, pbIn + 0x00, 0x80); // ~PSP header // step4: do the actual decryption of code block // point 0x40 bytes into the buffer to key info ret = Kirk.hleUtilsBufferCopyWithRange( pbOut, cbTotal, pbOut + 0x40, cbTotal - 0x40, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT_PRIVATE ); if (ret != 0) { throw (new Exception(CStringFormater.Sprintf("mangle#1 returned 0x%08X", ret))); } //File.WriteAllBytes("../../../TestInput/temp.bin", _pbOut); var OutputSize = *(int*)&pbIn[0xB0]; return _pbOut.Slice(0, OutputSize).ToArray(); } }
/// <summary> /// /// </summary> /// <param name="Stream"></param> public void Load(byte[] inbuf) { var Kirk = new Kirk(); Kirk.kirk_init(); var outbuf = new byte[(int)inbuf.Length]; inbuf.CopyTo(outbuf, 0); fixed (byte* _inbuf = inbuf) fixed (byte* _outbuf = outbuf) { var HeaderPointer = (HeaderStruct*)_inbuf; this.Header = *(HeaderStruct*)_inbuf; var pti = g_oldTagInfo.Where(Tag => Tag.tag == this.Header.tag).Single(); for (int i = 0; i < 0x150; i++) { outbuf[i] = 0; } for (int i = 0; i < 0x40; i++) { outbuf[i] = 0x55; } //HeaderPointer->psp_size = 5; //HeaderPointer->boot_entry = 0; var h7_header = (Kirk.KIRK_AES128CBC_HEADER*)&_outbuf[0x2C]; h7_header->Mode = 5; h7_header->Unknown4 = 0; h7_header->Unknown8 = 0; h7_header->KeySeed = pti.code; h7_header->Datasize = 0x70; byte[] header = new byte[0x150]; Array.Copy(inbuf, 0xD0, header, 0x00, 0x80); Array.Copy(inbuf, 0x80, header, 0x80, 0x50); Array.Copy(inbuf, 0x00, header, 0xD0, 0x80); if (pti.codeExtra != 0) { //ScramblePRXV2(header, (byte)pti.codeExtra); throw (new NotImplementedException()); } Array.Copy(header, 0x40, outbuf, 0x40, 0x40); for (int iXOR = 0; iXOR < 0x70; iXOR++) outbuf[0x40 + iXOR] = (byte)(outbuf[0x40 + iXOR] ^ (byte)pti.key[0x14 + iXOR]); for (int k = 0; k < 0x30; k++) outbuf[k + 0x80] = 0; // Scramble the data by calling CMD7. var bScrambleOut = new byte[outbuf.Length]; var bScrambleIn = outbuf; fixed (byte* _bScrambleOut = bScrambleOut) { Kirk.hleUtilsBufferCopyWithRange(_bScrambleOut, 0x70, &_outbuf[0x2C], 0x70, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT); } for (int iXOR = 0x6F; iXOR >= 0; iXOR--) { outbuf[0x40 + iXOR] = (byte)(outbuf[0x2C + iXOR] ^ (byte)pti.key[0x20 + iXOR]); } for (int k = 0; k < 0x30; k++) { outbuf[k + 0x80] = 0; } var KirkHeader = (Kirk.AES128CMACHeader *)&_outbuf[0x40]; KirkHeader->Mode = (uint)Kirk.KIRK_MODE_CMD1; Array.Copy(inbuf, 0x40 + 0x70, outbuf, 0x40 + 0x70, 0x20); Array.Copy(inbuf, 0, outbuf, 0x40 + 0x90, 0x80); // Call KIRK CMD1 for final decryption. /* ByteBuffer bDataOut = ByteBuffer.wrap(outbuf); ByteBuffer bHeaderIn = bDataOut.duplicate(); bHeaderIn.position(0x40); hleUtilsBufferCopyWithRange(bDataOut, size, bHeaderIn, size, 0x1); */ //#if true { int size = outbuf.Length; var Result = Kirk.hleUtilsBufferCopyWithRange(&_outbuf[0x00], size, (byte *)KirkHeader, size, Kirk.CommandEnum.PSP_KIRK_CMD_DECRYPT_PRIVATE, true); Console.WriteLine(Result); } //#endif File.WriteAllBytes("../../../TestInput/temp.bin", outbuf); //File.write //outbuf //Kirk.kirk_CMD1(); //Console.WriteLine("{0:X}", this.Header.tag); } }