示例#1
0
        private void ParseFile(BinaryReader br)
        {
            string magic = Encoding.ASCII.GetString(br.ReadBytes(4));

            if (magic != HEADER_MAGIC)
            {
                throw new NsoException("Invalid magic");
            }

            br.ReadUInt64();//padding
            Flag = br.ReadUInt32();

            for (int i = 0; i < SEGMENT_NB; i++)
            {
                segments[i] = new NsoSegment(br, i, Flag);
            }

            BuildId = br.ReadBytes(0x20);

            for (int i = 0; i < SEGMENT_NB; i++)
            {
                segments[i].CompressedSize = br.ReadUInt32();
            }

            br.BaseStream.Seek(0x24, SeekOrigin.Current); //padding

            DynamicStringTable = new DataExtent(br);
            DynamicSymbolTable = new DataExtent(br);

            for (int i = 0; i < SEGMENT_NB; i++)
            {
                segments[i].Sha256 = br.ReadBytes(0x20);
            }

            for (int i = 0; i < SEGMENT_NB; i++)
            {
                segments[i].RawData = new byte[segments[i].DecompressedSize];

                br.BaseStream.Position = segments[i].FileOffset;

                if (segments[i].Compressed)
                {
                    byte[] comp = br.ReadBytes((int)segments[i].CompressedSize);
                    segments[i].RawData = LZ4Codec.Decode(comp, 0, comp.Length, (int)segments[i].DecompressedSize);
                }
                else
                {
                    segments[i].RawData = br.ReadBytes((int)segments[i].CompressedSize);
                }
            }

            try
            {
                ModSection = new ModHeader(this);
            }
            catch (NsoException)
            {
                ModSection = null;
            }
        }
示例#2
0
        public byte[] ReadNso(uint addr, int size)
        {
            NsoSegment seg = (NsoSegment)GetAddressSegment(addr);

            if (seg == null)
            {
                throw new NsoException("Invalid Address");
            }

            if (addr + size > seg.Address + seg.DecompressedSize)
            {
                throw new NsoException("Invalid Size");
            }

            using (MemoryStream ms = new MemoryStream(seg.RawData))
            {
                BinaryReader br = new BinaryReader(ms);
                br.BaseStream.Position = addr - seg.Address;
                return(br.ReadBytes(size));
            }
        }
示例#3
0
        public void WriteNso(uint addr, byte[] data)
        {
            NsoSegment seg = (NsoSegment)GetAddressSegment(addr);

            if (seg == null)
            {
                throw new NsoException("Invalid Address");
            }

            if (addr + data.Length > seg.Address + seg.DecompressedSize)
            {
                throw new NsoException("Invalid Size");
            }

            using (MemoryStream ms = new MemoryStream(seg.RawData))
            {
                BinaryWriter bw = new BinaryWriter(ms);
                bw.BaseStream.Position = addr - seg.Address;
                bw.Write(data);
                SHA256 sha = SHA256.Create();
                seg.Sha256 = sha.ComputeHash(seg.RawData);
            }
        }