public override int Read(byte[] buffer, int offset, int count) { using (new NtfsTransaction()) { return(_baseStream.Read(buffer, offset, count)); } }
public void ViewIO() { SparseMemoryStream memStream = new SparseMemoryStream(); memStream.SetLength(1024); ThreadSafeStream tss = new ThreadSafeStream(memStream); SparseStream altView = tss.OpenView(); // Check positions are independant tss.Position = 100; Assert.Equal(0, altView.Position); Assert.Equal(100, tss.Position); // Check I/O is synchronous byte[] buffer = new byte[200]; tss.WriteByte(99); altView.Read(buffer, 0, 200); Assert.Equal(99, buffer[100]); // Check positions are updated correctly Assert.Equal(200, altView.Position); Assert.Equal(101, tss.Position); }
public void ExtractFile(String FileSource, String FileTarget) { FileStream FileISO = File.Open(FileName, FileMode.Open, FileAccess.Read); try { CDReader ReaderISO = new CDReader(FileISO, true); SparseStream _SourceStream = ReaderISO.OpenFile(FileSource, FileMode.Open, FileAccess.Read); Byte[] _ReadAllByte = new Byte[_SourceStream.Length]; _SourceStream.Read(_ReadAllByte, 0, _ReadAllByte.Length); _SourceStream.Close(); FileStream _FileCreated = new FileStream(FileTarget, FileMode.CreateNew); _FileCreated.Position = 0; _FileCreated.Write(_ReadAllByte, 0, _ReadAllByte.Length); _FileCreated.Close(); } catch (IOException ex) { if (ex.Message != "No such file") { Console.WriteLine("-----------------------------------------------------"); Console.WriteLine("ExtractFile(" + FileSource + ", " + FileTarget + ")"); Console.WriteLine(ex.Message); Console.WriteLine("-----------------------------------------------------"); } else { Console.WriteLine("ExtractFile(" + FileSource + ") --> " + Path.GetFileName(FileName)); } } FileISO.Close(); }
internal override int Read(long diskOffset, byte[] block, int offset, int count) { long start = diskOffset - Start; long grainSizeBytes = _grainSize * Sizes.Sector; long outputGrain = start / grainSizeBytes; long outputGrainOffset = start % grainSizeBytes; long grainStart = (_grainMapRanges[outputGrain].Offset + _grainMapOffsets[outputGrain]) * grainSizeBytes; long maxRead = (_grainMapRanges[outputGrain].Count - _grainMapOffsets[outputGrain]) * grainSizeBytes; long readStart = grainStart + outputGrainOffset; int toRead = (int)Math.Min(count, maxRead - outputGrainOffset); if (readStart > _content.Length) { Array.Clear(block, offset, toRead); return(toRead); } else { _content.Position = readStart; return(_content.Read(block, offset, toRead)); } }
public void Sparse() { int fileSize = 1 * 1024 * 1024; NtfsFileSystem ntfs = FileSystemSource.NtfsFileSystem(); byte[] data = new byte[fileSize]; for (int i = 0; i < fileSize; i++) { data[i] = (byte)i; } using (SparseStream s = ntfs.OpenFile("file.bin", FileMode.CreateNew)) { s.Write(data, 0, fileSize); ntfs.SetAttributes("file.bin", ntfs.GetAttributes("file.bin") | FileAttributes.SparseFile); s.Position = 64 * 1024; s.Clear(128 * 1024); s.Position = fileSize - 64 * 1024; s.Clear(128 * 1024); } using (SparseStream s = ntfs.OpenFile("file.bin", FileMode.Open)) { Assert.Equal(fileSize + 64 * 1024, s.Length); List <StreamExtent> extents = new List <StreamExtent>(s.Extents); Assert.Equal(2, extents.Count); Assert.Equal(0, extents[0].Start); Assert.Equal(64 * 1024, extents[0].Length); Assert.Equal((64 + 128) * 1024, extents[1].Start); Assert.Equal(fileSize - (64 * 1024) - ((64 + 128) * 1024), extents[1].Length); s.Position = 72 * 1024; s.WriteByte(99); byte[] readBuffer = new byte[fileSize]; s.Position = 0; s.Read(readBuffer, 0, fileSize); for (int i = 64 * 1024; i < (128 + 64) * 1024; ++i) { data[i] = 0; } for (int i = fileSize - (64 * 1024); i < fileSize; ++i) { data[i] = 0; } data[72 * 1024] = 99; Assert.Equal(data, readBuffer); } }
public override int Read(byte[] buffer, int offset, int count) { AssertOpen(); Utilities.AssertBufferParameters(buffer, offset, count); using (new NtfsTransaction()) { return(_baseStream.Read(buffer, offset, count)); } }
public override int Read(long diskOffset, byte[] block, int offset, int count) { long position = diskOffset - Start; if (position < _bitmapStream.Length) { _bitmapStream.Position = position; return(_bitmapStream.Read(block, offset, count)); } _content.Position = position - _bitmapStream.Length; return(_content.Read(block, offset, count)); }
public override int Read(byte[] buffer, int offset, int count) { CheckDisposed(); if (_atEof || _position > _length) { _atEof = true; throw new IOException("Attempt to read beyond end of file"); } if (_position == _length) { _atEof = true; return 0; } SectorDisposition disposition = _bat.GetDisposition(_position); if (disposition == SectorDisposition.Stored) { int bytesPresent = (int)_bat.ContiguousBytes(_position, count); _fileStream.Position = _bat.GetFilePosition(_position); int read = (int)_fileStream.Read(buffer, offset, bytesPresent); _position += read; return read; } else if (disposition == SectorDisposition.Parent) { int bytesFromParent = (int)_bat.ContiguousBytes(_position, count); _parentStream.Position = _position; int read = _parentStream.Read(buffer, offset, bytesFromParent); _position += read; return read; } else { int bytesZero = (int)_bat.ContiguousBytes(_position, count); Array.Clear(buffer, offset, bytesZero); _position += bytesZero; return bytesZero; } }
public static void ReadIsoFolder(DiscFileSystem cdReader, string sIsoPath, string sDestinationRootPath) { try { string[] saFiles = cdReader.GetFiles(sIsoPath); foreach (string sFile in saFiles) { DiscFileInfo dfiIso = cdReader.GetFileInfo(sFile); string sDestinationPath = Path.Combine(sDestinationRootPath, dfiIso.DirectoryName.Substring(0, dfiIso.DirectoryName.Length - 1)); if (!Directory.Exists(sDestinationPath)) { Directory.CreateDirectory(sDestinationPath); } string sDestinationFile = Path.Combine(sDestinationPath, dfiIso.Name); SparseStream streamIsoFile = cdReader.OpenFile(sFile, FileMode.Open); FileStream fsDest = new FileStream(sDestinationFile, FileMode.Create); byte[] baData = new byte[0x4000]; while (true) { int nReadCount = streamIsoFile.Read(baData, 0, baData.Length); if (nReadCount < 1) { break; } else { fsDest.Write(baData, 0, nReadCount); } } streamIsoFile.Close(); fsDest.Close(); } string[] saDirectories = cdReader.GetDirectories(sIsoPath); foreach (string sDirectory in saDirectories) { ReadIsoFolder(cdReader, sDirectory, sDestinationRootPath); } return; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
public override int Read(long diskOffset, byte[] block, int offset, int count) { long relOffset = diskOffset - Start; if (relOffset < _grainTableStream.Length) { _grainTableStream.Position = relOffset; return(_grainTableStream.Read(block, offset, count)); } long grainSize = _header.GrainSize * Sizes.Sector; int grainIdx = (int)((relOffset - _grainTableStream.Length) / grainSize); long grainOffset = relOffset - _grainTableStream.Length - grainIdx * grainSize; int maxToRead = (int)Math.Min(count, grainSize * _grainContiguousRangeMapping[grainIdx] - grainOffset); _content.Position = _grainMapping[grainIdx] * grainSize + grainOffset; return(_content.Read(block, offset, maxToRead)); }
private void UpdateListbox() { Console.WriteLine("Updating listbox"); if (fileLoaded) { lfiles.Clear(); foreach (string i in fs.GetFiles("lua")) { Console.WriteLine("Adding " + i); SparseStream strm = fs.OpenFile(i, FileMode.Open); byte[] data = new byte[strm.Length]; strm.Read(data, 0, data.Length); strm.Close(); string sdata = Encoding.ASCII.GetString(data); LuaFile newfile = new LuaFile(i, sdata); lfiles.Add(newfile); } m_luaFiles.Items.Clear(); foreach (LuaFile file in lfiles) { m_luaFiles.Items.Add(file); } } }
public Byte[] ReadAllByte(String FileSource) { Byte[] result = { 0x0 }; FileStream FileISO = File.Open(FileName, FileMode.Open, FileAccess.Read); try { CDReader ReaderISO = new CDReader(FileISO, true); SparseStream _SourceStream = ReaderISO.OpenFile(FileSource, FileMode.Open, FileAccess.Read); Byte[] _ReadAllByte = new Byte[_SourceStream.Length]; _SourceStream.Read(_ReadAllByte, 0, _ReadAllByte.Length); result = _ReadAllByte; _SourceStream.Close(); } catch (Exception ex) { Console.WriteLine("-----------------------------------------------------"); Console.WriteLine("ReadAllByte(" + FileSource + ")"); Console.WriteLine(ex.Message); Console.WriteLine("-----------------------------------------------------"); } FileISO.Close(); return(result); }
public override int Read(byte[] buffer, int offset, int count) { CheckDisposed(); if (_position > Length) { _atEof = true; throw new IOException("Attempt to read beyond end of stream"); } if (_position == Length) { if (_atEof) { throw new IOException("Attempt to read beyond end of stream"); } else { _atEof = true; return(0); } } int maxToRead = (int)Math.Min(count, Length - _position); int totalRead = 0; int numRead; do { int grainTable = (int)(_position / _gtCoverage); int grainTableOffset = (int)(_position - (((long)grainTable) * _gtCoverage)); numRead = 0; if (!LoadGrainTable(grainTable)) { // Read from parent stream, to at most the end of grain table's coverage _parentDiskStream.Position = _position + _diskOffset; numRead = _parentDiskStream.Read(buffer, offset + totalRead, (int)Math.Min(maxToRead - totalRead, _gtCoverage - grainTableOffset)); } else { int grainSize = (int)(_header.GrainSize * Sizes.Sector); int grain = grainTableOffset / grainSize; int grainOffset = grainTableOffset - (grain * grainSize); int numToRead = Math.Min(maxToRead - totalRead, grainSize - grainOffset); if (GetGrainTableEntry(grain) == 0) { _parentDiskStream.Position = _position + _diskOffset; numRead = _parentDiskStream.Read(buffer, offset + totalRead, numToRead); } else { int bufferOffset = offset + totalRead; long grainStart = ((long)GetGrainTableEntry(grain)) * Sizes.Sector; numRead = ReadGrain(buffer, bufferOffset, grainStart, grainOffset, numToRead); } } _position += numRead; totalRead += numRead; }while (numRead != 0 && totalRead < maxToRead); return(totalRead); }
/// <summary> /// Creates a hex dump from a stream. /// </summary> /// <param name="stream">The stream to generate the hex dump from.</param> /// <param name="output">The destination for the hex dump.</param> public static void Generate(SparseStream stream, TextWriter output) { stream.Position = 0; byte[] buffer = new byte[1024 * 1024]; foreach(var block in StreamExtent.Blocks(stream.Extents, buffer.Length)) { long startPos = block.Offset * (long)buffer.Length; long endPos = Math.Min((block.Offset + block.Count) * (long)buffer.Length, stream.Length); stream.Position = startPos; while (stream.Position < endPos) { int numLoaded = 0; long readStart = stream.Position; while (numLoaded < buffer.Length) { int bytesRead = stream.Read(buffer, numLoaded, buffer.Length - numLoaded); if (bytesRead == 0) { break; } numLoaded += bytesRead; } for (int i = 0; i < numLoaded; i += 16) { bool foundVal = false; if (i > 0) { for (int j = 0; j < 16; j++) { if (buffer[i + j] != buffer[i + j - 16]) { foundVal = true; break; } } } else { foundVal = true; } if (foundVal) { output.Write("{0:x8}", i + readStart); for (int j = 0; j < 16; j++) { if (j % 8 == 0) { output.Write(" "); } output.Write(" {0:x2}", buffer[i + j]); } output.Write(" |"); for (int j = 0; j < 16; j++) { if (j % 8 == 0 && j != 0) { output.Write(" "); } output.Write("{0}", (buffer[i + j] >= 32 && buffer[i + j] < 127) ? (char)buffer[i + j] : '.'); } output.Write("|"); output.WriteLine(); } } } } }
void MainC(string[] args) { Disk disk = new Disk("server.bin"); PartitionTable partitions = disk.Partitions; PartitionInfo fs_pi = partitions[0]; FatFileSystem fs = new FatFileSystem(fs_pi.Open()); PartitionInfo mfs_pi = partitions[1]; FatFileSystem mfs = new FatFileSystem(mfs_pi.Open()); svtime = DateTime.Now.Ticks; Console.WriteLine("Starting..."); netListener = new EventBasedNetListener(); client = new NetManager(netListener, new XorEncryptLayer("dreamscape")); inetListener = new EventBasedNetListener(); iclient = new NetManager(inetListener); lua = new Lua(); gameInstance = new Instance("DataModel"); Factory factoryContainer = new Factory("Factories"); factoryContainer.Parent = gameInstance; CharacterFactory characterFactory = new CharacterFactory("CharacterFactory"); characterFactory.Parent = factoryContainer; GameLibrary gameLibrary = new GameLibrary("GameLibrary"); gameLibrary.Parent = factoryContainer; lua.LoadCLRPackage(); lua.DebugHook += Lua_DebugHook; SparseStream stream = fs.OpenFile("lua\\main.lua", FileMode.Open, FileAccess.Read); byte[] data = new byte[stream.Length]; stream.Read(data, 0, data.Length); string code = Encoding.ASCII.GetString(data); stream.Close(); lua["netwriter"] = new NetDataWriter(); lua["netserver"] = client; lua["DataModel"] = gameInstance; lua.DoString("import('dreamhost','dreamscape','System','NLua','LiteNetLib','LiteNetLib.Utils','dreamhost.lua')\n" + code, svtime.ToString()); this.characterFactory = characterFactory; client.Start(6698); Console.WriteLine("Server ready! Hosting on UDP port 6698."); client.AutoRecycle = true; netListener.ConnectionRequestEvent += (request) => { Console.WriteLine("Connection"); LuaFunction func = lua["peerhandle"] as LuaFunction; try { func.Call(request); } catch (Exception e) { request.Accept(); Console.WriteLine(e); } }; netListener.PeerDisconnectedEvent += (peer, info) => { Player plr = null; foreach (Player i in characterFactory.Children) { if (i.peer == peer) { plr = i; } } Console.WriteLine("Disconnect"); LuaFunction func = lua["peerdisconnect"] as LuaFunction; try { func.Call(peer, info, plr); } catch (Exception e) { Console.WriteLine(e); } }; netListener.PeerConnectedEvent += (peer) => { Player player = new Player(gameLibrary.FindEntropy().ToString(), peer); player.Parent = characterFactory; Console.WriteLine("Successful Connect"); LuaFunction func = lua["peerconnect"] as LuaFunction; try { func.Call(peer, player); } catch (Exception e) { Console.WriteLine(e); } }; netListener.NetworkReceiveEvent += NetworkReceive; Console.WriteLine("Starting info server..."); inetListener.PeerConnectedEvent += (peer) => { Console.WriteLine("Sending peer info"); NetDataWriter writer = new NetDataWriter(); LuaFunction func = lua["getinfo"] as LuaFunction; try { writer.Put(func.Call(peer).First() as string); } catch (Exception e) { Console.WriteLine(e); writer.Put("Please contact the <b>SYSOP</b> for this server.\n" + e); } peer.Send(writer, DeliveryMethod.ReliableSequenced); }; inetListener.ConnectionRequestEvent += (request) => { Console.WriteLine("Connected to info"); request.Accept(); }; iclient.Start(6699); iclient.AutoRecycle = true; Console.WriteLine("Info server ready! Hosting on UDP port 6699."); while (true) { client.PollEvents(); Thread.Sleep(15); } }
/// <summary> /// Creates a hex dump from a stream. /// </summary> /// <param name="stream">The stream to generate the hex dump from.</param> /// <param name="output">The destination for the hex dump.</param> public static void Generate(SparseStream stream, TextWriter output) { stream.Position = 0; byte[] buffer = new byte[1024 * 1024]; foreach (var block in StreamExtent.Blocks(stream.Extents, buffer.Length)) { long startPos = block.Offset * (long)buffer.Length; long endPos = Math.Min((block.Offset + block.Count) * (long)buffer.Length, stream.Length); stream.Position = startPos; while (stream.Position < endPos) { int numLoaded = 0; long readStart = stream.Position; while (numLoaded < buffer.Length) { int bytesRead = stream.Read(buffer, numLoaded, buffer.Length - numLoaded); if (bytesRead == 0) { break; } numLoaded += bytesRead; } for (int i = 0; i < numLoaded; i += 16) { bool foundVal = false; if (i > 0) { for (int j = 0; j < 16; j++) { if (buffer[i + j] != buffer[i + j - 16]) { foundVal = true; break; } } } else { foundVal = true; } if (foundVal) { output.Write("{0:x8}", i + readStart); for (int j = 0; j < 16; j++) { if (j % 8 == 0) { output.Write(" "); } output.Write(" {0:x2}", buffer[i + j]); } output.Write(" |"); for (int j = 0; j < 16; j++) { if (j % 8 == 0 && j != 0) { output.Write(" "); } output.Write("{0}", (buffer[i + j] >= 32 && buffer[i + j] < 127) ? (char)buffer[i + j] : '.'); } output.Write("|"); output.WriteLine(); } } } } }
public override int Read(byte[] buffer, int offset, int count) { return(_wrapped.Read(buffer, offset, count)); }
internal override int Read(long diskOffset, byte[] block, int offset, int count) { _stream.Position = diskOffset - Start; return(_stream.Read(block, offset, count)); }