/// <summary> /// Writes a string with the given encoding to the given offset of the file /// </summary> /// <param name="index">Index of the file to write</param> /// <param name="e">The encoding to use</param> /// <param name="value">The string to write. The entire string will be written with an ending null character.</param> public static async Task WriteNullTerminatedStringAsync(this IWriteOnlyBinaryDataAccessor accessor, long index, Encoding e, string value) { var nullChar = e.GetBytes(new[] { Convert.ToChar(0) }); var data = e.GetBytes(value); await accessor.WriteAsync(index, data.Length, data); await accessor.WriteAsync(index + data.Length, nullChar.Length, nullChar); }
/// <summary> /// Writes the current state of the NCCH partition to the given binary data accessor /// </summary> /// <param name="data">Data accessor to receive the binary data</param> /// <returns>A long representing the total length of data written</returns> public async Task <long> WriteBinary(IWriteOnlyBinaryDataAccessor data) { // Get the data var exheader = ExHeader?.ToByteArray(); var plainRegion = !string.IsNullOrEmpty(PlainRegion) ? Encoding.ASCII.GetBytes(PlainRegion) : null; var plainRegionOffset = 0; var logoRegionOffset = 0; var exeFs = ExeFs?.ToByteArray(); var exeFsOffset = 0; var romFs = RomFs?.Data; var romFsOffset = 0; // Write the data var offset = 0x200; // Skip the header, write it last if (exheader != null) { await data.WriteAsync(offset, exheader); offset += exheader.Length; } if (plainRegion != null) { plainRegionOffset = offset; await data.WriteAsync(offset, plainRegion); offset += plainRegion.Length; var padding = new byte[0x200 - plainRegion.Length % 0x200]; await data.WriteAsync(offset, padding); offset += padding.Length; } if (Logo != null) { logoRegionOffset = offset; await data.WriteAsync(offset, Logo); offset += Logo.Length; var padding = new byte[0x200 - Logo.Length % 0x200]; await data.WriteAsync(offset, padding); offset += padding.Length; } if (exeFs != null) { exeFsOffset = offset; await data.WriteAsync(offset, exeFs); offset += exeFs.Length; var padding = new byte[0x200 - exeFs.Length % 0x200]; await data.WriteAsync(offset, padding); offset += padding.Length; } if (romFs != null) { romFsOffset = offset; const int bufferSize = 1024 * 1024; for (int i = 0; i < romFs.Length; i += bufferSize) { int length = (int)Math.Min(bufferSize, romFs.Length - i); var block = await romFs.ReadArrayAsync(i, length); await data.WriteAsync(offset, block); offset += length; } var padding = new byte[0x200 - romFs.Length % 0x200]; await data.WriteAsync(offset, padding); offset += padding.Length; } // Create a new header using var sha = SHA256.Create(); var header = NcchHeader.Copy(this.Header); header.Signature = new byte[0x100]; // We lack the 3DS's private key, so leave out the signature header.ContentSize = (offset + MediaUnitSize - 1) / MediaUnitSize; // offset/MediaUnitSize, but rounding up header.ContentLockSeedHash = 0; // Unknown, left blank by SciresM's 3DS Builder if (Logo != null) { header.LogoRegionHash = sha.ComputeHash(Logo); } else { header.LogoRegionHash = new byte[0x20]; } if (exheader != null) { header.ExHeaderHash = NcchExtendedHeader.GetSuperblockHash(sha, exheader); header.ExHeaderSize = NcchExtendedHeader.ExHeaderDataSize; } else { header.ExHeaderHash = new byte[0x20]; header.ExHeaderSize = 0; } header.PlainRegionOffset = (plainRegionOffset + MediaUnitSize - 1) / MediaUnitSize; header.PlainRegionSize = ((plainRegion?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize; header.LogoRegionOffset = (logoRegionOffset + MediaUnitSize - 1) / MediaUnitSize; header.LogoRegionSize = ((Logo?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize; header.ExeFsOffset = (exeFsOffset + MediaUnitSize - 1) / MediaUnitSize; header.ExeFsSize = ((exeFs?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize; header.ExeFsHashRegionSize = 1; // Static 0x200 for exefs superblock header.RomFsOffset = (romFsOffset + MediaUnitSize - 1) / MediaUnitSize; header.RomFsSize = ((int)(romFs?.Length ?? 0) + MediaUnitSize - 1) / MediaUnitSize; header.RomFsHashRegionSize = ((RomFs?.Header?.MasterHashSize ?? 0) + MediaUnitSize - 1) / MediaUnitSize; header.ExeFsSuperblockHash = ExeFs?.GetSuperblockHash() ?? new byte[0x20]; header.RomFsSuperblockHash = RomFs != null ? await RomFs.GetSuperblockHash(sha, romFs, RomFs.Header) : new byte[0x20]; var headerData = await header.ToBinary().ReadArrayAsync(); await data.WriteAsync(0, headerData); return(offset); }
/// <summary> /// Writes a signed 16 bit little endian integer /// </summary> /// <param name="offset">Offset of the integer to write.</param> /// <param name="value">The integer to write</param> public static async Task WriteInt16Async(this IWriteOnlyBinaryDataAccessor accessor, long offset, Int16 value) { await accessor.WriteAsync(offset, 2, BitConverter.GetBytes(value)); }
/// <summary> /// Writes all of the given data to the desired index /// </summary> /// <param name="index">Index of the data to write</param> /// <param name="value">Data to write</param> public static async Task WriteAsync(this IWriteOnlyBinaryDataAccessor accessor, long index, byte[] value) { await accessor.WriteAsync(index, value.Length, value); }
/// <summary> /// Writes a string with the given encoding to the given offset of the file /// </summary> /// <param name="index">Index of the file to write</param> /// <param name="e">The encoding to use</param> /// <param name="value">The string to write. The entire string will be written without an ending null character.</param> public static async Task WriteStringAsync(this IWriteOnlyBinaryDataAccessor accessor, long index, Encoding e, string value) { await accessor.WriteAsync(index, e.GetBytes(value)); }