示例#1
0
        private static IChunk NextChunk(BinaryReader binaryReader, Encoding stringEncoding)
        {
            ChunkDescriptor chunkDescriptor = new ChunkDescriptor(binaryReader);

            if (chunkDescriptor.ChunkId == null)
            {
                return(null);
            }

            switch (chunkDescriptor.ChunkId)
            {
            case "RIFF":
                return(new RiffChunk(chunkDescriptor, binaryReader, stringEncoding));

            case "LIST":
                return(new ListChunk(chunkDescriptor, binaryReader, stringEncoding));

            case "fmt ":
                return(new FmtChunk(chunkDescriptor, binaryReader));

            case "data":
                return(new DataChunk(chunkDescriptor, binaryReader));

            default:
                return(new UnknowChunk(chunkDescriptor, binaryReader));
            }
        }
示例#2
0
        public MemoryMappedFile(ChunkDescriptor ChunkDescriptor, MemoryMappedFileRepository Repo, string Path, Stream Stream)
            : base(ChunkDescriptor)
        {
            if (ChunkDescriptor == null)
            {
                throw new ArgumentNullException("ChunkDescriptor");
            }
            if (Path == null)
            {
                throw new ArgumentNullException("Path");
            }
            if (Repo == null)
            {
                throw new ArgumentNullException("Repo");
            }
            repo = Repo;
            path = Path;
            Stream OStream = System.IO.File.OpenWrite(path);
            long   Count   = ((Stream.Length - 1) >> 20) + 1;

            for (long n = 0; n != Count; n++)
            {
                byte[] Bytes = new byte[1 << 20];
                Stream.Read(Bytes, 0, 1 << 20);
                OStream.Write(Bytes, 0, 1 << 20);
            }
            Init();
        }
示例#3
0
            public RiffChunk(ChunkDescriptor chunkDescriptor, BinaryReader binaryReader, Encoding stringEncoding)
            {
                Descriptor = chunkDescriptor;
                Format     = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
                Chunks     = new Dictionary <string, IChunk>();
                if (Descriptor.ChunkSize > 0)
                {
                    int position = 4;
                    while (position < Descriptor.ChunkSize)
                    {
                        IChunk chunk = NextChunk(binaryReader, stringEncoding);
                        if (chunk == null)
                        {
                            throw new Exception("Unexpected stream end");
                        }

                        Chunks.Add(chunk.Descriptor.ChunkId, chunk);
                        position += chunk.Descriptor.ChunkSize + 8;
                    }
                }
                else
                {
                    while (true)
                    {
                        IChunk chunk = NextChunk(binaryReader, stringEncoding);
                        if (chunk == null)
                        {
                            break;
                        }

                        Chunks.Add(chunk.Descriptor.ChunkId, chunk);
                    }
                }
            }
        public void CompressDecompress()
        {
            string path  = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"Resources\\gpl-3.0.txt");
            string text  = File.ReadAllText(path);
            var    chunk = new ChunkDescriptor {
                Position = 0, Size = text.Length
            };

            byte[] compressed;
            using (var r = new CompressedChunkStreamReader(File.OpenRead(path)))
            {
                compressed = r.Read(chunk);
            }
            var compressedStream = new MemoryStream(compressed);

            Stream decompressedStream;

            using (var r = new DecompressedChunkStreamReader(compressedStream))
            {
                decompressedStream = new MemoryStream(r.Read(new ChunkDescriptor {
                    Position = 0, Size = compressed.Length
                }));
            }

            var decompressedText = new StreamReader(decompressedStream).ReadToEnd();

            Assert.AreEqual(text, decompressedText);
        }
示例#5
0
            public DataChunk(ChunkDescriptor chunkDescriptor, BinaryReader binaryReader)
            {
                Descriptor = chunkDescriptor;
                if (Descriptor.ChunkSize > 0)
                {
                    Data = binaryReader.ReadBytes(Descriptor.ChunkSize);
                }
                else
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        byte[] buffer = new byte[1024];
                        while (true)
                        {
                            int length = binaryReader.Read(buffer, 0, buffer.Length);
                            if (length <= 0)
                            {
                                break;
                            }

                            memoryStream.Write(buffer, 0, length);
                        }
                        Data = memoryStream.ToArray();
                    }
                }
            }
 public override ChunkData Pull(ChunkDescriptor Chunk)
 {
     using (Mono.Data.Sqlite.SqliteCommand Comm = new Mono.Data.Sqlite.SqliteCommand("SELECT PATH FROM CHUNKS WHERE ID = @p0", Base)) {
         Comm.Parameters.AddWithValue("p0", Chunk.Hash);
         Mono.Data.Sqlite.SqliteDataReader DR = Comm.ExecuteReader();
         if (DR.Read())
         {
             return(new File(Chunk, this, DR.GetString(0)));
         }
         return(null);
     }
 }
示例#7
0
 public MemoryMappedFile(ChunkDescriptor ChunkDescriptor, MemoryMappedFileRepository Repo, string Path)
     : base(ChunkDescriptor)
 {
     if (ChunkDescriptor == null)
         throw new ArgumentNullException ("ChunkDescriptor");
     if (Repo == null)
         throw new ArgumentNullException ("Repo");
     if (Path == null)
         throw new ArgumentNullException ("Path");
     repo = Repo;
     path = Path;
 }
示例#8
0
 public MemoryMappedFile(ChunkDescriptor ChunkDescriptor, MemoryMappedFileRepository Repo, string Path, byte[] Data)
     : base(ChunkDescriptor)
 {
     if (ChunkDescriptor == null)
         throw new ArgumentNullException ("ChunkDescriptor");
     if (Data == null)
         throw new ArgumentNullException ("Data");
     if (Path == null)
         throw new ArgumentNullException ("Path");
     if (Repo == null)
         throw new ArgumentNullException ("Repo");
     repo = Repo;
     path = Path;
     System.IO.File.WriteAllBytes (Path, Data);
     Init ();
 }
示例#9
0
            public FmtChunk(ChunkDescriptor chunkDescriptor, BinaryReader binaryReader)
            {
                Descriptor = chunkDescriptor;

                // General data
                AudioFormat   = binaryReader.ReadInt16();
                NumChennals   = binaryReader.ReadInt16();
                SampleRate    = binaryReader.ReadInt32();
                ByteRate      = binaryReader.ReadInt32();
                BlockAlign    = binaryReader.ReadInt16();
                BitsPerSample = binaryReader.ReadInt16();

                int position = 16;

                // ExtraParams
                if (Descriptor.ChunkSize > 16)
                {
                    ExtraParamSize = binaryReader.ReadInt16();
                    position      += 2;
                    if (ExtraParamSize != 0)
                    {
                        if (ExtraParamSize == 22)
                        {
                            ExtraParams = new Extra(binaryReader);
                            position   += ExtraParamSize;
                        }
                        else
                        {
                            throw new FormatNotSupportedException();
                        }
                    }
                    else
                    {
                        ExtraParamSize = 0;
                    }
                }
                else
                {
                    ExtraParamSize = 0;
                }

                // Consume unused data (if exits)
                if (position < Descriptor.ChunkSize)
                {
                    binaryReader.ReadBytes(Descriptor.ChunkSize - position);
                }
            }
        public void ReadChunk()
        {
            string path   = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"Resources\\gpl-3.0.txt");
            string text   = File.ReadAllText(path);
            var    source = File.OpenRead(path);
            var    chunk  = new ChunkDescriptor {
                Position = 0, Size = 100
            };
            var reader = new ChunkStreamReader(source);
            var target = new MemoryStream(reader.Read(chunk));

            target.Seek(0, SeekOrigin.Begin);
            Assert.AreEqual(100, target.Length);
            var targetReader = new StreamReader(target);

            Assert.AreEqual(text.Substring(0, 100), targetReader.ReadToEnd());
        }
示例#11
0
 public MemoryMappedFile(ChunkDescriptor ChunkDescriptor, MemoryMappedFileRepository Repo, string Path)
     : base(ChunkDescriptor)
 {
     if (ChunkDescriptor == null)
     {
         throw new ArgumentNullException("ChunkDescriptor");
     }
     if (Repo == null)
     {
         throw new ArgumentNullException("Repo");
     }
     if (Path == null)
     {
         throw new ArgumentNullException("Path");
     }
     repo = Repo;
     path = Path;
 }
        public override void Push(ChunkDescriptor Chunk)
        {
            if (Chunk == null)
            {
                throw new ArgumentNullException("Chunk");
            }
            string NName      = Chunk.Hash.ToHexadecimal();
            string FPath      = path + System.IO.Path.DirectorySeparatorChar + NName;
            Stream ChunkBytes = Chunk.GetRawData();

            new File(Chunk, this, FPath, ChunkBytes);
            using (Mono.Data.Sqlite.SqliteCommand Comm = new Mono.Data.Sqlite.SqliteCommand("INSERT INTO Files (ID, Name, Hash, Attributes) VALUES (@p0,@p1,@p2,@p3)", Base)) {
                Comm.Parameters.AddWithValue("p0", Chunk.Hash);
                Comm.Parameters.AddWithValue("p1", NName);
                Comm.Parameters.AddWithValue("p2", null);
                Comm.ExecuteNonQuery();
            }
        }
示例#13
0
 public MemoryMappedFile(ChunkDescriptor ChunkDescriptor, MemoryMappedFileRepository Repo, string Path, Stream Stream)
     : base(ChunkDescriptor)
 {
     if (ChunkDescriptor == null)
         throw new ArgumentNullException ("ChunkDescriptor");
     if (Path == null)
         throw new ArgumentNullException ("Path");
     if (Repo == null)
         throw new ArgumentNullException ("Repo");
     repo = Repo;
     path = Path;
     Stream OStream = System.IO.File.OpenWrite (path);
     long Count = ((Stream.Length - 1) >> 20) + 1;
     for (long n = 0; n != Count; n++) {
         byte[] Bytes = new byte[1 << 20];
         Stream.Read (Bytes, 0, 1 << 20);
         OStream.Write (Bytes, 0, 1 << 20);
     }
     Init ();
 }
示例#14
0
        List <Chunk> GetCachedChunks(ISyntaxMode mode, TextDocument doc, Mono.TextEditor.Highlighting.ColorScheme style, DocumentLine line, int offset, int length)
        {
            ChunkDescriptor descriptor;

            if (chunkDict.TryGetValue(line, out descriptor))
            {
                bool isInvalid;
                if (descriptor.Equals(line, offset, length, out isInvalid))
                {
                    return(descriptor.Chunk);
                }
                chunkDict.Remove(line);
            }
            ;

            var chunks = mode.GetChunks(style, line, offset, length).ToList();

            descriptor       = new ChunkDescriptor(line, offset, length, chunks);
            chunkDict [line] = descriptor;
            return(chunks);
        }
示例#15
0
            public ListChunk(ChunkDescriptor chunkDescriptor, BinaryReader binaryReader, Encoding stringEncoding)
            {
                Descriptor = chunkDescriptor;

                ListType = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
                int position = 4;

                Data = new SortedDictionary <string, string>();
                while (position < Descriptor.ChunkSize)
                {
                    string subChunkId = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
                    position += 4;
                    int subChunkSize = binaryReader.ReadInt32();
                    position += 4;
                    string data = stringEncoding.GetString(binaryReader.ReadBytes(subChunkSize)).TrimEnd('\0');
                    position += subChunkSize;
                    if (subChunkSize % 2 != 0)
                    {
                        binaryReader.ReadByte();
                        position++;
                    }
                    Data.Add(subChunkId, data);
                }
            }
示例#16
0
 public MemoryMappedFile(ChunkDescriptor ChunkDescriptor, MemoryMappedFileRepository Repo, string Path, byte[] Data)
     : base(ChunkDescriptor)
 {
     if (ChunkDescriptor == null)
     {
         throw new ArgumentNullException("ChunkDescriptor");
     }
     if (Data == null)
     {
         throw new ArgumentNullException("Data");
     }
     if (Path == null)
     {
         throw new ArgumentNullException("Path");
     }
     if (Repo == null)
     {
         throw new ArgumentNullException("Repo");
     }
     repo = Repo;
     path = Path;
     System.IO.File.WriteAllBytes(Path, Data);
     Init();
 }
示例#17
0
 public override ChunkData Pull(ChunkDescriptor Chunk)
 {
     using (Mono.Data.Sqlite.SqliteCommand Comm  = new Mono.Data.Sqlite.SqliteCommand ("SELECT PATH FROM CHUNKS WHERE ID = @p0", Base)) {
         Comm.Parameters.AddWithValue("p0", Chunk.Hash);
         Mono.Data.Sqlite.SqliteDataReader DR = Comm.ExecuteReader ();
         if(DR.Read())
         {
             return new File(Chunk, this, DR.GetString(0));
         }
         return null;
     }
 }
示例#18
0
 public UnknowChunk(ChunkDescriptor chunkDescriptor, BinaryReader binaryReader)
 {
     Descriptor = chunkDescriptor;
     Data       = binaryReader.ReadBytes(Descriptor.ChunkSize);
 }
示例#19
0
        public BlorbFile(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            this.memory = stream.ReadFully();

            var reader = new MemoryReader(this.memory, 0);

            var dwords = reader.NextDWords(3);

            // First, ensure that this is a valid format
            if (dwords[0] != id_FORM)
            {
                throw new InvalidOperationException();
            }

            if (dwords[2] != id_IFRS)
            {
                throw new InvalidOperationException();
            }

            int totalLength = (int)dwords[1] + 8;

            // Collect all chunks
            this.chunks = new List <ChunkDescriptor>();

            while (reader.Address < totalLength)
            {
                var chunk = new ChunkDescriptor();

                chunk.Address = (uint)reader.Address;

                var type = reader.NextDWord();
                var len  = reader.NextDWord();

                chunk.Type = type;
                if (type == id_FORM)
                {
                    chunk.DataAddress = chunk.Address;
                    chunk.Length      = len + 8;
                }
                else
                {
                    chunk.DataAddress = (uint)reader.Address;
                    chunk.Length      = len;
                }

                chunks.Add(chunk);

                reader.Skip((int)len);
                if ((reader.Address & 1) != 0)
                {
                    reader.Skip(1);
                }

                if (reader.Address > totalLength)
                {
                    throw new InvalidOperationException();
                }
            }

            // Loop through chunks and collect resources
            this.resources = new List <ResourceDecriptor>();

            foreach (var chunk in chunks)
            {
                if (chunk.Type == id_RIdx)
                {
                    reader.Address = (int)chunk.DataAddress;
                    var numResources = (int)reader.NextDWord();

                    if (chunk.Length < (numResources * 12) + 4)
                    {
                        throw new InvalidOperationException();
                    }

                    for (int i = 0; i < numResources; i++)
                    {
                        var resource = new ResourceDecriptor();
                        resource.Usage  = reader.NextDWord();
                        resource.Number = reader.NextDWord();

                        var resourcePos = reader.NextDWord();

                        var chunkIndex = chunks.FindIndex(c => c.Address == resourcePos);
                        if (chunkIndex < 0)
                        {
                            throw new InvalidOperationException();
                        }

                        resource.ChunkNumber = chunkIndex;

                        resources.Add(resource);
                    }
                }
                else if (chunk.Type == id_RelN)
                {
                    reader.Address = (int)chunk.DataAddress;
                    if (chunk.Length < 2)
                    {
                        throw new InvalidOperationException();
                    }

                    releaseNumber = reader.NextWord();
                }
                else if (chunk.Type == id_IFhd)
                {
                    reader.Address = (int)chunk.DataAddress;
                    if (chunk.Length < 3)
                    {
                        throw new InvalidOperationException();
                    }

                    var header = new ZHeader();
                    header.ReleaseNumber = reader.NextWord();
                    header.SerialNumber  = new char[6];
                    for (int i = 0; i < 6; i++)
                    {
                        header.SerialNumber[i] = (char)reader.NextByte();
                    }
                    header.Checksum = reader.NextWord();
                }
                else if (chunk.Type == id_Reso)
                {
                }
                else if (chunk.Type == id_Loop)
                {
                }
                else if (chunk.Type == id_Plte)
                {
                }
            }
        }
示例#20
0
 private byte[] GetChunkData(ChunkDescriptor chunk)
 {
     return(memory.ReadBytes((int)chunk.DataAddress, (int)chunk.Length));
 }
示例#21
0
		Chunk GetCachedChunks (SyntaxMode mode, Document doc, Mono.TextEditor.Highlighting.Style style, LineSegment line, int offset, int length)
		{
//			return mode.GetChunks (doc, style, line, offset, length);
			ChunkDescriptor descriptor;
			if (chunkDict.TryGetValue (line, out descriptor)) {
				bool isInvalid;
				if (descriptor.Equals (line, offset, length, out isInvalid))
					return descriptor.Chunk;
				chunkDict.Remove (line);
			}

			Chunk chunk = mode.GetChunks (doc, style, line, offset, length);
			descriptor = new ChunkDescriptor (line, offset, length, chunk);
			chunkDict.Add (line, descriptor);
			return chunk;
		}
		IEnumerable<Chunk> GetCachedChunks (ISyntaxMode mode, TextDocument doc, Mono.TextEditor.Highlighting.ColorScheme style, DocumentLine line, int offset, int length)
		{
			ChunkDescriptor descriptor;
			if (chunkDict.TryGetValue (line, out descriptor)) {
				bool isInvalid;
				if (descriptor.Equals (line, offset, length, out isInvalid))
					return descriptor.Chunk;
				chunkDict.Remove (line);
			}

			Chunk[] chunks = mode.GetChunks (style, line, offset, length).ToArray ();
			descriptor = new ChunkDescriptor (line, offset, length, chunks);
			chunkDict [line] = descriptor;
			return chunks;
		}
示例#23
0
 public override void Push(ChunkDescriptor Chunk)
 {
     if (Chunk == null)
         throw new ArgumentNullException ("Chunk");
     string NName = Chunk.Hash.ToHexadecimal ();
     string FPath = path +System.IO.Path.DirectorySeparatorChar + NName;
     Stream ChunkBytes = Chunk.GetRawData();
     new File (Chunk, this, FPath, ChunkBytes);
         using (Mono.Data.Sqlite.SqliteCommand Comm = new Mono.Data.Sqlite.SqliteCommand ("INSERT INTO Files (ID, Name, Hash, Attributes) VALUES (@p0,@p1,@p2,@p3)", Base)) {
             Comm.Parameters.AddWithValue("p0", Chunk.Hash);
             Comm.Parameters.AddWithValue("p1", NName);
             Comm.Parameters.AddWithValue("p2", null);
             Comm.ExecuteNonQuery ();
         }
 }