public void ThrowsException_IfCalledWithNegativeBufferSize() { Should.Throw <ArgumentException>(() => { StreamUtils.CopyStream(inputStream, outputStream, -1); }); }
public static int ExtractSE3(List <string> args) { if (args.Count < 1) { Console.WriteLine("Usage: in.se3 [outdir]"); return(-1); } string infile = args[0]; string outpath = args.Count > 1 ? args[1] : args[0] + ".ext"; var se3 = new SE3(infile, null, TextUtils.GameTextEncoding.ASCII); var nub = new NUB.NUB(se3.ExtractNubStream(), se3.Endian); if (se3.FileCount != nub.EntryCount) { Console.WriteLine("WARNING: SE3 header and NUB header disagree on number of files. ({0} != {1})", se3.FileCount, nub.EntryCount); } System.IO.Directory.CreateDirectory(outpath); for (long i = 0; i < nub.EntryCount; ++i) { var file = nub.GetChildByIndex(i) as NUB.NubFile; if (file != null) { string fname = i < se3.Filenames.Count ? se3.Filenames[(int)i] : i.ToString("D8"); using (var ds = file.DataStream.Duplicate()) using (var fs = new FileStream(Path.Combine(outpath, fname + "." + file.Type), FileMode.Create)) { StreamUtils.CopyStream(ds, fs); } } } return(0); }
public void ThrowsException_IfCalledWithNullSource() { Should.Throw <ArgumentNullException>(() => { StreamUtils.CopyStream(null, outputStream); }); }
public void ThrowsException_IfCalledWithNullDestination() { Should.Throw <ArgumentNullException>(() => { StreamUtils.CopyStream(inputStream, null); }); }
private static void DoExtract(CpkContainer cpk, string outdir, bool recursive) { for (long i = 0; i < cpk.toc_entries; ++i) { CpkContainer.Entry entry = cpk.GetEntryByIndex(i); if (entry == null) { continue; } string name = entry.name; if (recursive && name.ToLowerInvariant().EndsWith(".cpk")) { CpkContainer subcpk; if (entry.compressed) { subcpk = new CpkContainer(cpk.GetChildByIndex(i).AsFile.DataStream); } else { subcpk = new CpkContainer(cpk.DuplicateStream(), entry.file_offset); } DoExtract(subcpk, Path.Combine(outdir, name + ".ext"), recursive); continue; } string outpath = Path.Combine(outdir, name); Console.WriteLine("Extracting {0}...", outpath); Directory.CreateDirectory(Path.GetDirectoryName(outpath)); using (var ds = cpk.GetChildByIndex(i).AsFile.DataStream) using (FileStream fs = new FileStream(outpath, FileMode.Create, FileAccess.Write)) { StreamUtils.CopyStream(ds, fs, ds.Length); } } }
public static int Execute(List <string> args) { if (args.Count != 1) { Console.WriteLine("Usage: SPKDunpack file"); return(-1); } var spkd = new SPKD(new DuplicatableFileStream(args[0])); DirectoryInfo d = System.IO.Directory.CreateDirectory(args[0] + ".ext"); for (int i = 0; i < spkd.MaxFileCount; ++i) { var f = spkd.GetChildByIndex(i); if (f != null) { string path = Path.Combine(d.FullName, spkd.GetFileName(i)); using (var ds = f.AsFile.DataStream.Duplicate()) using (var fs = new FileStream(path, FileMode.Create)) { StreamUtils.CopyStream(ds, fs); } } } return(0); }
public static int Extract(List <string> args) { if (args.Count < 1) { Console.WriteLine("Usage: archive.hac folder-to-extract-to"); return(-1); } string infile = args[0]; string outdir = args.Count == 1 ? infile + ".ext" : args[1]; using (var archive = new HyoutaUtils.HyoutaArchive.HyoutaArchiveContainer(new DuplicatableFileStream(infile))) { Directory.CreateDirectory(outdir); for (long i = 0; i < archive.Filecount; ++i) { var f = archive.GetFile(i); using (var ds = f.DataStream.Duplicate()) using (var fs = new FileStream(Path.Combine(outdir, f.Filename ?? i.ToString("D8")), FileMode.Create)) { StreamUtils.CopyStream(ds, fs); } } } return(0); }
public byte[] Decompress(byte[] buffer) { bool assume_zlib = true; if (assume_zlib) { Console.WriteLine("assuming zlib compression, trying to decompress..."); ulong insize = BitConverter.ToUInt32(buffer, 8) - 0x18; ulong outsize = BitConverter.ToUInt32(buffer, 12); byte[] output = new byte[(long)outsize]; int result = zlib.uncompress(output, 0, ref outsize, buffer, 0x18, insize); if (result != zlib.Z_OK) { throw new Exception(string.Format("zlib decompression error ({0})", result)); } return(output); } else { MemoryStream result = new MemoryStream(); int inSize = BitConverter.ToInt32(buffer, 8); int outSize = BitConverter.ToInt32(buffer, 12); int offset = 0x18; using (DeflateStream decompressionStream = new DeflateStream(new MemoryStream(buffer, offset, inSize - offset), CompressionMode.Decompress)) { StreamUtils.CopyStream(decompressionStream, result, outSize); } return(result.ToArray()); } }
// TODO: We could parse/extract the nub directly to keep filenames, but eh, effort... public void ExtractToNub(string targetName) { Data.Position = DataBegin; using (Stream fs = new FileStream(targetName, FileMode.Create)) { StreamUtils.CopyStream(Data, fs, Data.Length - DataBegin); } }
public static void Download(string uri, Stream destinationStream, Action <bool> resultCallback, Action <double> progressCallback, Cancellation c) { if (string.IsNullOrWhiteSpace(uri)) { resultCallback(false); } else { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.AllowReadStreamBuffering = false; request.BeginGetResponse((AsyncCallback)(asyncRes => { bool flag = true; try { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncRes); using (Stream responseStream = ((WebResponse)response).GetResponseStream()) StreamUtils.CopyStream(responseStream, destinationStream, progressCallback, c, response.ContentLength); } catch (Exception) { flag = false; } resultCallback(flag); }), null); } }
public FileInjector(CpkContainer cpk, string outpath, long injectionOffset) { if (DisableInjection) { return; } Cpk = cpk; if (outpath != null) { Directory.CreateDirectory(Path.GetDirectoryName(outpath)); OutputStream = new FileStream(outpath, FileMode.Create); } else { OutputStream = new HyoutaUtils.Streams.MemoryStream64(); } CurrentInjectionOffset = injectionOffset; using (Stream instream = cpk.DuplicateStream()) { instream.Position = 0; StreamUtils.CopyStream(instream, OutputStream, instream.Length); } EnsureAligned(); RemappedSubcpks = new Dictionary <string, SubcpkData>(); }
public void CopyStreamNew() { MemoryStream from = new MemoryStream(buffer); MemoryStream to = new MemoryStream(); StreamUtils.CopyStream(from, to); }
public static void WriteHallOfFameDataToSave(HallOfFameStructure hofs, System.IO.Stream file, int offset = 0x1C000, int pages = 2) { using (System.IO.Stream buffer = new System.IO.MemoryStream(0xF80 * pages)) { // Serialize buffer.Position = 0; hofs.Serialize(buffer); buffer.WriteAlign(0xF80 * pages, 0x00); // Calculate checksums buffer.Position = 0; ushort[] checksums = new ushort[pages]; for (int i = 0; i < pages; ++i) { checksums[i] = Checksum.CalculateSaveChecksum(buffer, 0xF80); } // Write back into save buffer.Position = 0; for (int i = 0; i < pages; ++i) { int secpos = offset + i * 0x1000; file.WriteAlign(secpos + 0x1000, 0x00); file.Position = secpos; StreamUtils.CopyStream(buffer, file, 0xF80); file.Position = secpos + 0xFF4; file.WriteUInt16(checksums[i]); file.Position = secpos + 0xFF8; file.WriteUInt32(0x08012025); file.Position = secpos + 0x1000; } } }
public static void Extract(FileStream file, String destination) { System.IO.Directory.CreateDirectory(destination); byte[] buffer = new byte[4]; file.Read(buffer, 0, 4); int count = BitConverter.ToInt32(buffer, 0); int[] FileOffsets = new int[count + 1]; for (int i = 0; i < count; ++i) { file.Read(buffer, 0, 4); FileOffsets[i] = BitConverter.ToInt32(buffer, 0); } FileOffsets[count] = (int)file.Length; for (int i = 0; i < count; ++i) { FileStream newFile = new FileStream(System.IO.Path.Combine(destination, i.ToString("D4")), FileMode.Create); file.Position = FileOffsets[i]; StreamUtils.CopyStream(file, newFile, FileOffsets[i + 1] - FileOffsets[i]); newFile.Close(); } return; }
private static PackageProject ExtractPackageProject(string fileName) { PackageProject packageProject = null; using (var packageReader = ZipFile.Open(fileName, ZipArchiveMode.Read)) { var packageEntry = packageReader.GetEntry("package.zip"); if (packageEntry == null) { return(null); } using (var memoryStream = new MemoryStream()) { StreamUtils.CopyStream(packageEntry.Open(), memoryStream, 0x4000); memoryStream.Seek(0, SeekOrigin.Begin); using (var reader = new ZipArchive(memoryStream)) { foreach (var entry in reader.Entries) { if (!entry.FullName.Is(Constants.ProjectKey)) { continue; } packageProject = IOUtils.LoadSolution(StreamUtil.LoadString(entry.Open())); break; } } } } return(packageProject); }
private static PackageProject ExtractPackageProject(string fileName) { PackageProject packageProject = null; using (var packageReader = new ZipReader(fileName)) { var packageEntry = packageReader.GetEntry("package.zip"); using (var memoryStream = new MemoryStream()) { StreamUtils.CopyStream(packageEntry.GetStream(), memoryStream, 0x4000); memoryStream.Seek(0, SeekOrigin.Begin); using (var reader = new ZipReader(memoryStream)) { foreach (var entry in reader.Entries) { if (!entry.Name.Is("installer/project")) { continue; } packageProject = IOUtils.LoadSolution(StreamUtil.LoadString(entry.GetStream())); break; } } } } return(packageProject); }
static void Extract(FileStream file, String destination, Dictionary <uint, string> filenameHashDict = null) { System.IO.Directory.CreateDirectory(destination); uint magic = file.ReadUInt32(); uint filecount = file.ReadUInt32(); var FileInfos = new S2ARHeaderFileInfo[filecount]; for (int i = 0; i < filecount; ++i) { FileInfos[i] = new S2ARHeaderFileInfo(); FileInfos[i].Hash = file.ReadUInt32(); FileInfos[i].Filesize = file.ReadUInt32(); } for (int i = 0; i < filecount; ++i) { string filename; if (filenameHashDict != null && filenameHashDict.ContainsKey(FileInfos[i].Hash)) { filename = System.IO.Path.Combine(destination, filenameHashDict[FileInfos[i].Hash]); } else { filename = System.IO.Path.Combine(destination, i.ToString("D4") + Util.GuessFileExtension(file)); } FileStream newFile = new FileStream(filename, FileMode.Create); StreamUtils.CopyStream(file, newFile, (int)FileInfos[i].Filesize); newFile.Close(); } return; }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> private void ReplaceDatabase(string databaseName, InputStream databaseStream, IEnumerator <KeyValuePair <string, InputStream> > attachmentStreams) { try { Database database = GetDatabase(databaseName); string dstAttachmentsPath = database.GetAttachmentStorePath(); OutputStream destStream = new FileOutputStream(new FilePath(database.GetPath())); StreamUtils.CopyStream(databaseStream, destStream); FilePath attachmentsFile = new FilePath(dstAttachmentsPath); FileDirUtils.DeleteRecursive(attachmentsFile); attachmentsFile.Mkdirs(); if (attachmentStreams != null) { StreamUtils.CopyStreamsToFolder(attachmentStreams, attachmentsFile); } database.Open(); database.ReplaceUUIDs(); } catch (FileNotFoundException e) { Log.E(Database.Tag, string.Empty, e); throw new CouchbaseLiteException(Status.InternalServerError); } catch (IOException e) { Log.E(Database.Tag, string.Empty, e); throw new CouchbaseLiteException(Status.InternalServerError); } }
protected override bool delete(string type, Stream stream, Func <string, bool> filter) { OutArchiveFormat archiveFormat = SevenZipWrapper.getArchiveFormat("abc" + type); Dictionary <int, string> fileDictionary = new Dictionary <int, string>(); foreach (var foundItem in lookup(getExtractor(stream, null), filter)) { fileDictionary.Add(foundItem.Value1, null); } if (fileDictionary.Count > 0) { string tempFile; using (var tempStream = TempStreamUtils.NewTempStream(out tempFile, "tmp")) StreamUtils.CopyStream(stream, tempStream, true, true, false); Delete(archiveFormat, tempFile, filter); using (var tempStream = new FileStream(tempFile, FileMode.Open)) StreamUtils.CopyStream(tempStream, stream, false, true, true); //SevenZipSharp crash when compressor.ModifyArchive() is used, //if Compression mode is Append (Create is fine). //lock (_lockObject) //{ // SevenZipCompressor compressor = getCompressor(archiveFormat); // compressor.ModifyArchive(archivePath, fileDictionary); //} } return(true); }
static void Main(string[] args) { List <string> infiles = new List <string>(); Prefilter filter = Prefilter.None; Exhaustion exhaustion = Exhaustion.Standard; for (int i = 0; i < args.Length; ++i) { if (args[i] == "--filter") { ++i; filter = (Prefilter)Enum.Parse(typeof(Prefilter), args[i], true); } else if (args[i] == "--exhaustion") { ++i; exhaustion = (Exhaustion)Enum.Parse(typeof(Exhaustion), args[i], true); } else { infiles.Add(args[i]); } } if (infiles.Count == 0) { Console.WriteLine("Usage: FileCompressor [options] files"); Console.WriteLine("Options:"); Console.WriteLine(" --filter"); foreach (object e in Enum.GetValues(typeof(Prefilter))) { Console.WriteLine(" " + e.ToString()); } Console.WriteLine(" --exhaustion"); foreach (object e in Enum.GetValues(typeof(Exhaustion))) { Console.WriteLine(" " + e.ToString()); } } foreach (string infile in infiles) { string outfile = infile + "_" + filter.ToString() + "_" + exhaustion.ToString() + ".bin"; Console.WriteLine("Generating: " + outfile); Stream ms; using (var fs = new FileStream(infile, FileMode.Open, FileAccess.Read, FileShare.Read)) { ms = fs.CopyToMemory(); fs.Close(); } using (var compressed = Compress(ms, filter, exhaustion)) { using (FileStream os = new FileStream(outfile, FileMode.Create)) { compressed.Position = 0; StreamUtils.CopyStream(compressed, os); } } } }
private void DoTargetRead(ulong length) { if (((ulong)Patch.Position) + length > (ulong)Patch.Length) { throw new Exception("Invalid length in TargetRead."); } StreamUtils.CopyStream(Patch, Target, length); }
public DuplicatableStream Decompress(Stream data) { using (MemoryStream ms = new MemoryStream()) using (DeflateStream decompressed = new DeflateStream(data, CompressionMode.Decompress)) { StreamUtils.CopyStream(decompressed, ms, (long)UncompressedFilesize); return(ms.CopyToByteArrayStreamAndDispose()); } }
private static void WriteStream(string outfile, Stream stream) { Console.WriteLine(string.Format("writing {0}", outfile)); using (var fs = new FileStream(outfile, FileMode.Create)) { stream.Position = 0; StreamUtils.CopyStream(stream, fs); } }
internal static StreamReader GetInternalStreamImpl(bool source, Stream streamValidator = null) { if (streamValidator != null) { return(new StreamReader(StreamUtils.CopyStream(streamValidator))); } return(new StreamReader(GetEntitiesInternalPath(source))); }
private bool RewriteZonesBuffered( BinaryWriter w, WriteDelegate write, ICollection <Zone> zones, TagData dataToWrite, bool tagExists) { bool result = true; // Load the 'interesting' part of the file in memory // TODO - detect and fine-tune cases when block at the extreme ends of the file are considered (e.g. SPC) long chunkBeginOffset = getFirstRecordedOffset(zones); long chunkEndOffset = getLastRecordedOffset(zones); if (embedder != null && implementedTagType == MetaDataIOFactory.TAG_ID3V2) { chunkBeginOffset = Math.Min(chunkBeginOffset, embedder.Id3v2Zone.Offset); chunkEndOffset = Math.Max(chunkEndOffset, embedder.Id3v2Zone.Offset + embedder.Id3v2Zone.Size); } long initialChunkLength = chunkEndOffset - chunkBeginOffset; w.BaseStream.Seek(chunkBeginOffset, SeekOrigin.Begin); using (MemoryStream chunk = new MemoryStream((int)initialChunkLength)) { StreamUtils.CopyStream(w.BaseStream, chunk, (int)initialChunkLength); using (BinaryWriter msw = new BinaryWriter(chunk, Settings.DefaultTextEncoding)) { result = RewriteZonesDirect(msw, write, zones, dataToWrite, tagExists, chunkBeginOffset, true); // -- Adjust file slot to new size of chunk -- long tagBeginOffset = chunkBeginOffset; long tagEndOffset = tagBeginOffset + initialChunkLength; // Need to build a larger file if (chunk.Length > initialChunkLength) { Logging.LogDelegator.GetLogDelegate()(Logging.Log.LV_DEBUG, "Disk stream operation : Lengthening (delta=" + (chunk.Length - initialChunkLength) + ")"); StreamUtils.LengthenStream(w.BaseStream, tagEndOffset, (uint)(chunk.Length - initialChunkLength)); } else if (chunk.Length < initialChunkLength) // Need to reduce file size { Logging.LogDelegator.GetLogDelegate()(Logging.Log.LV_DEBUG, "Disk stream operation : Shortening (delta=" + (chunk.Length - initialChunkLength) + ")"); StreamUtils.ShortenStream(w.BaseStream, tagEndOffset, (uint)(initialChunkLength - chunk.Length)); } // Copy tag contents to the new slot w.BaseStream.Seek(tagBeginOffset, SeekOrigin.Begin); chunk.Seek(0, SeekOrigin.Begin); StreamUtils.CopyStream(chunk, w.BaseStream); } } return(result); }
public override Stream GetSinglePlane(Stream inputStream, uint depthlevel) { if (Mipmaps > 1) { throw new Exception("Don't know how to deal with volume textures with mipmaps."); } if (!(Format == TextureFormat.DXT1a || Format == TextureFormat.DXT1b || Format == TextureFormat.DXT5a || Format == TextureFormat.DXT5b)) { throw new Exception("Volume textures only implemented for DXT formats."); } // Format reference: https://www.khronos.org/registry/OpenGL/extensions/NV/NV_texture_compression_vtc.txt uint bytecount = GetByteCountSingleDepthPlane(0); uint group = depthlevel / 4u; uint texInGroupBefore = depthlevel % 4u; uint texInGroupAfter = 3u - texInGroupBefore; // special handling for last group if depth not divisible by 4 uint nonDivisiblePlanesAtEnd = Depth % 4; uint groupCount = NumberUtils.Align(Depth, 4u) / 4; if (nonDivisiblePlanesAtEnd != 0 && group == (groupCount - 1u)) { texInGroupAfter = (nonDivisiblePlanesAtEnd - texInGroupBefore) - 1u; } uint bytesPerTexLine; switch (Format) { case TextureFormat.DXT1a: case TextureFormat.DXT1b: bytesPerTexLine = 8; break; case TextureFormat.DXT5a: case TextureFormat.DXT5b: bytesPerTexLine = 16; break; default: throw new Exception("Unexpected format during volume texture plane fetching: " + Format + "."); } // skip to correct group inputStream.DiscardBytes(group * bytecount * 4u); // copy texture MemoryStream s = new MemoryStream((int)bytecount); for (uint i = 0; i < bytecount; i += bytesPerTexLine) { inputStream.DiscardBytes(texInGroupBefore * bytesPerTexLine); StreamUtils.CopyStream(inputStream, s, bytesPerTexLine); inputStream.DiscardBytes(texInGroupAfter * bytesPerTexLine); } return(s); }
public static void FromStream(Stream source, MetaDataIO meta, ReadTagParams readTagParams, uint chunkSize) { IList <string> position = new List <string>(); bool inList = false; int listDepth = 0; int listCounter = 1; position.Add("ixml"); using (MemoryStream mem = new MemoryStream((int)chunkSize)) { StreamUtils.CopyStream(source, mem, (int)chunkSize); // Isolate XML structure in a clean memory chunk mem.Seek(0, SeekOrigin.Begin); using (XmlReader reader = XmlReader.Create(mem)) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // Element start string key = reader.Name; if (inList && reader.Depth == listDepth + 1 && !key.EndsWith("COUNT", StringComparison.OrdinalIgnoreCase)) { key = key + "[" + listCounter + "]"; listCounter++; } if (!key.Equals("BWFXML", StringComparison.OrdinalIgnoreCase)) { position.Add(key); } if (!inList && reader.Name.EndsWith("LIST", StringComparison.OrdinalIgnoreCase)) { inList = true; listDepth = reader.Depth; listCounter = 1; } break; case XmlNodeType.Text: if (reader.Value != null && reader.Value.Length > 0) { meta.SetMetaField(getPosition(position), reader.Value, readTagParams.ReadAllMetaFrames); } break; case XmlNodeType.EndElement: // Element end position.RemoveAt(position.Count - 1); if (inList && reader.Name.EndsWith("LIST", StringComparison.OrdinalIgnoreCase)) { inList = false; } break; } } } } }
public static int Execute(List <string> args) { if (args.Count == 0) { Console.WriteLine("This is intended to help extracting skit audio from the Xbox 360 game files."); Console.WriteLine("Do the following in order:"); Console.WriteLine("-- unpack chat.svo (FPS4 archive, with HyoutaTools -> ToVfps4e)"); Console.WriteLine("-- decompress individual skit with xbdecompress"); Console.WriteLine("-- unpack skit (FPS4 archive, with HyoutaTools -> ToVfps4e)"); Console.WriteLine("-- convert SE3 to NUB (with HyoutaTools -> SE3toNUB)"); Console.WriteLine(" (first four bytes of nub file should be 0x00020100)"); Console.WriteLine("-- extract nub archive with NUBExt r12beta"); Console.WriteLine("-- this gives you an \"xma\" file that isn't actually an xma, run this tool on it"); Console.WriteLine("-- resulting file is a valid enough xma file that can be converted to WAV with \"toWav\""); return(-1); } string filename = args[0]; using (var source = new FileStream(filename, FileMode.Open)) { using (var dest = new FileStream(filename + "-real.xma", FileMode.Create)) { source.Position = 0x100; int dataLength = (int)(source.Length - source.Position); dest.WriteAscii("RIFF"); dest.WriteUInt32((uint)dataLength + 0x34); dest.WriteAscii("WAVE"); dest.WriteAscii("fmt "); dest.WriteUInt32(0x20); source.Position = 0xBC; dest.WriteUInt16(source.ReadUInt16().SwapEndian()); dest.WriteUInt16(source.ReadUInt16().SwapEndian()); dest.WriteUInt16(source.ReadUInt16().SwapEndian()); dest.WriteUInt16(source.ReadUInt16().SwapEndian()); dest.WriteUInt16(source.ReadUInt16().SwapEndian()); dest.WriteByte((byte)source.ReadByte()); dest.WriteByte((byte)source.ReadByte()); dest.WriteUInt32(source.ReadUInt32().SwapEndian()); dest.WriteUInt32(source.ReadUInt32().SwapEndian()); dest.WriteUInt32(source.ReadUInt32().SwapEndian()); dest.WriteUInt32(source.ReadUInt32().SwapEndian()); dest.WriteByte((byte)source.ReadByte()); dest.WriteByte((byte)source.ReadByte()); dest.WriteUInt16(source.ReadUInt16().SwapEndian()); dest.WriteAscii("data"); dest.WriteUInt32((uint)dataLength); source.Position = 0x100; StreamUtils.CopyStream(source, dest, dataLength); } } return(0); }
public void DoesNotCopyAnything_IfCalledWithEmptySource() { using (var emptyStream = new MemoryStream()) { StreamUtils.CopyStream(emptyStream, outputStream); outputStream.ToArray().ShouldBeEmpty(); } }
public DDS(Stream ddsFile) { if (ddsFile.Length < 0x80) { throw new Exception("Invalid size for DDS file."); } Header = DDSHeader.FromStream(ddsFile); Data = new MemoryStream((int)ddsFile.Length - 0x80); StreamUtils.CopyStream(ddsFile, Data, ddsFile.Length - 0x80); }