diff --git a/czi-format/inspector/czi-inspector.sln b/czi-format/inspector/czi-inspector.sln index 55ae1e4dfaedb83e549c65dfa88045bb3f2d0e2c..fa68d0df1f7ad1cdceadb17829c3e18b73426a42 100644 --- a/czi-format/inspector/czi-inspector.sln +++ b/czi-format/inspector/czi-inspector.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.28307.168 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "czi-inspector", "czi-inspector\czi-inspector.csproj", "{CC796EBE-77A0-442F-A44F-0863E03B2F54}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "czi-inspector", "czi-inspector\czi-inspector.csproj", "{22AC8E5E-5E7C-482E-B600-B1B2E7B93143}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,15 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {CC796EBE-77A0-442F-A44F-0863E03B2F54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CC796EBE-77A0-442F-A44F-0863E03B2F54}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CC796EBE-77A0-442F-A44F-0863E03B2F54}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CC796EBE-77A0-442F-A44F-0863E03B2F54}.Release|Any CPU.Build.0 = Release|Any CPU + {22AC8E5E-5E7C-482E-B600-B1B2E7B93143}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {22AC8E5E-5E7C-482E-B600-B1B2E7B93143}.Debug|Any CPU.Build.0 = Debug|Any CPU + {22AC8E5E-5E7C-482E-B600-B1B2E7B93143}.Release|Any CPU.ActiveCfg = Release|Any CPU + {22AC8E5E-5E7C-482E-B600-B1B2E7B93143}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {E88F4F44-F469-4F79-B35D-08A631D14253} + SolutionGuid = {A64E8A42-D10C-4B53-AF19-A5717B0F97DF} EndGlobalSection EndGlobal diff --git a/czi-format/inspector/czi-inspector/App.config b/czi-format/inspector/czi-inspector/App.config new file mode 100644 index 0000000000000000000000000000000000000000..56efbc7b5f15b5166cc89dae0406895b57de0b67 --- /dev/null +++ b/czi-format/inspector/czi-inspector/App.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8" ?> +<configuration> + <startup> + <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /> + </startup> +</configuration> \ No newline at end of file diff --git a/czi-format/inspector/czi-inspector/CziFile.cs b/czi-format/inspector/czi-inspector/CziFile.cs new file mode 100644 index 0000000000000000000000000000000000000000..21807645950f1addf3ca1dd8791d694cba17d1cb --- /dev/null +++ b/czi-format/inspector/czi-inspector/CziFile.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace czi_inspector +{ + class CziFile + { + public bool IsMasterFile => (Header.FilePart == 0) && (Header.MasterFileGuid == Header.FileGuid); + public CziFileHeader Header { get; set; } = new CziFileHeader(); + } +} diff --git a/czi-format/inspector/czi-inspector/CziFileHeader.cs b/czi-format/inspector/czi-inspector/CziFileHeader.cs new file mode 100644 index 0000000000000000000000000000000000000000..db8a3af21bd0a8f97d49c628f33142a353094552 --- /dev/null +++ b/czi-format/inspector/czi-inspector/CziFileHeader.cs @@ -0,0 +1,18 @@ +namespace czi_inspector +{ + class CziFileHeader + { + public SegmentHeader Header { get; set; } + + public int MajorVersion { get; set; } + public int MinorVersion { get; set; } + public string MasterFileGuid { get; set; } + public string FileGuid { get; set; } + + public int FilePart { get; set; } + public long DirectoryPosition { get; set; } + public long MetadataPosition { get; set; } + public bool UpdatePending { get; set; } + public long AttachmentDirectoryPosition { get; set; } + } +} diff --git a/czi-format/inspector/czi-inspector/CziParser.cs b/czi-format/inspector/czi-inspector/CziParser.cs new file mode 100644 index 0000000000000000000000000000000000000000..97deb37a9f7fefdc91199b67153cd1539d143023 --- /dev/null +++ b/czi-format/inspector/czi-inspector/CziParser.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace czi_inspector +{ + class CziParser + { + const int SegmentHeaderSize = 32; + const int FileHeaderSegmentSize = SegmentHeaderSize + 80; + const int IntSize = 4; + const int BoolSize = 4; + const int LongSize = 8; + const int GuidSize = 16; + + private int _bufferOffset = 0; + + + public CziParser() + { + + } + + public CziFile Parse(string file) + { + CziFile result = new CziFile(); + + byte[] buffer; + using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) + { + using (BinaryReader reader = new BinaryReader(fileStream)) + { + buffer = new byte[FileHeaderSegmentSize]; + reader.Read(buffer, 0, FileHeaderSegmentSize); + result.Header = ParseFileHeader(buffer); + + fileStream.Seek(result.Header.MetadataPosition, SeekOrigin.Begin); + buffer = new byte[10000]; + reader.Read(buffer, 0, 10000); + ParseMetadata(buffer); + } + } + + return result; + } + + private void ParseMetadata(byte[] buffer) + { + SegmentHeader metadataHeader = ParseSegmentHeader(buffer); + byte[] metadata = new byte[metadataHeader.UsedSize]; + Array.Copy(buffer, _bufferOffset, metadata, 0, metadataHeader.UsedSize); + + Console.WriteLine(Encoding.UTF8.GetString(metadata)); + } + + private CziFileHeader ParseFileHeader(byte[] buffer) + { + CziFileHeader fileHeader = new CziFileHeader(); + fileHeader.Header = ParseSegmentHeader(buffer); + + fileHeader.MajorVersion = BitConverter.ToInt32(buffer, _bufferOffset); + _bufferOffset += IntSize; + fileHeader.MinorVersion = BitConverter.ToInt32(buffer, _bufferOffset); + _bufferOffset += IntSize; + _bufferOffset += 8; // 8 Bytes are reserved. + + fileHeader.MasterFileGuid = BitConverter.ToString(buffer, _bufferOffset, 16); + _bufferOffset += GuidSize; + fileHeader.FileGuid = BitConverter.ToString(buffer, _bufferOffset, 16); + _bufferOffset += GuidSize; + fileHeader.FilePart = BitConverter.ToInt32(buffer, _bufferOffset); + _bufferOffset += IntSize; + fileHeader.DirectoryPosition = BitConverter.ToInt64(buffer, _bufferOffset); + _bufferOffset += LongSize; + fileHeader.MetadataPosition = BitConverter.ToInt64(buffer, _bufferOffset); + _bufferOffset += LongSize; + fileHeader.UpdatePending = BitConverter.ToBoolean(buffer, _bufferOffset); + _bufferOffset += BoolSize; + fileHeader.AttachmentDirectoryPosition = BitConverter.ToInt32(buffer, _bufferOffset); + _bufferOffset += IntSize; + + return fileHeader; + } + + private SegmentHeader ParseSegmentHeader(byte[] buffer) + { + Utils.Assert(buffer.Length >= (16 + 8 + 8)); + + SegmentHeader header = new SegmentHeader + { + Id = Encoding.UTF8.GetString(buffer, 0, 16).RemoveNullCharacters(), + AllocatedSize = BitConverter.ToInt64(buffer, 16), + UsedSize = BitConverter.ToInt64(buffer, 24) + }; + + _bufferOffset += SegmentHeaderSize; + return header; + } + } +} diff --git a/czi-format/inspector/czi-inspector/Program.cs b/czi-format/inspector/czi-inspector/Program.cs index 04d40e5d48449e536d26fdb412be152377a2e676..fdb53d0e723ea27982b411f959739ea64896306b 100644 --- a/czi-format/inspector/czi-inspector/Program.cs +++ b/czi-format/inspector/czi-inspector/Program.cs @@ -10,45 +10,12 @@ namespace czi_inspector { static void Main(string[] args) { - string file = args[0]; - string substring = args[1]; - string encoding = args[2]; - Regex regex = new Regex(substring, RegexOptions.IgnoreCase); - Encoding selectedEncoding = GetEncoding(encoding); - - const int bufferSize = 1024; - byte[] buffer = new byte[bufferSize]; - int count = 0; - Stopwatch stopwatch = new Stopwatch(); - stopwatch.Start(); - - //var wholeFileBuffer = File.ReadAllBytes(file); - //string wholeFileString = selectedEncoding.GetString(wholeFileBuffer); - //count = regex.Matches(substring).Count; - using (FileStream reader = new FileStream(file, FileMode.Open, FileAccess.Read)) + if (args.Length < 1) { - while (reader.Read(buffer, 0, bufferSize) > 0) - { - string bufferString = selectedEncoding.GetString(buffer); - var matches = regex.Matches(bufferString).Count; - count += matches; - if (matches > 0) - Console.WriteLine(bufferString); - } + Console.WriteLine("Wrong method or operand..."); + return; } - stopwatch.Stop(); - Console.WriteLine("Found: {0} occurencies of {1} in file {2} in {3} ms.", count, substring, file, stopwatch.Elapsed.TotalMilliseconds); - - } - - private static Encoding GetEncoding(string encoding) - { - encoding = encoding.ToUpper(); - if (encoding == "ASCII") return Encoding.ASCII; - if (encoding == "UTF8") return Encoding.UTF8; - if (encoding == "UNICODE") return Encoding.Unicode; - if (encoding == "UTF32") return Encoding.UTF32; - throw new Exception("Try different encoding, currently supported: ASCII, UTF8, UTF32, UNICODE."); + CziFile parsed = new CziParser().Parse(args[0]); } } } diff --git a/czi-format/inspector/czi-inspector/Properties/AssemblyInfo.cs b/czi-format/inspector/czi-inspector/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..d2879dc61a2a28ef4827ab6487b3632b0f63300d --- /dev/null +++ b/czi-format/inspector/czi-inspector/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("czi-inspector")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("czi-inspector")] +[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("22ac8e5e-5e7c-482e-b600-b1b2e7b93143")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/czi-format/inspector/czi-inspector/SegmentHeader.cs b/czi-format/inspector/czi-inspector/SegmentHeader.cs new file mode 100644 index 0000000000000000000000000000000000000000..f9ed30b751b081f8bd5f1f4d0b25d139b56fe314 --- /dev/null +++ b/czi-format/inspector/czi-inspector/SegmentHeader.cs @@ -0,0 +1,9 @@ +namespace czi_inspector +{ + public class SegmentHeader + { + public string Id { get; set; } + public long AllocatedSize { get; set; } + public long UsedSize { get; set; } + } +} diff --git a/czi-format/inspector/czi-inspector/Utils.cs b/czi-format/inspector/czi-inspector/Utils.cs new file mode 100644 index 0000000000000000000000000000000000000000..7783156cc856f86eaee71e8a3778406457c589f8 --- /dev/null +++ b/czi-format/inspector/czi-inspector/Utils.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace czi_inspector +{ + internal static class Utils + { + internal static string RemoveNullCharacters(this string src) => src.Replace("\0", string.Empty); + + internal static void Assert(bool exp) => Trace.Assert(exp); + } +} diff --git a/czi-format/inspector/czi-inspector/czi-inspector.csproj b/czi-format/inspector/czi-inspector/czi-inspector.csproj index 68783fa7bd0fbea2c2ab063e839a1b8c9ea23f77..cd7f98ffae80a27530b57346d91abf348733a374 100644 --- a/czi-format/inspector/czi-inspector/czi-inspector.csproj +++ b/czi-format/inspector/czi-inspector/czi-inspector.csproj @@ -1,9 +1,58 @@ -<Project Sdk="Microsoft.NET.Sdk"> - +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{22AC8E5E-5E7C-482E-B600-B1B2E7B93143}</ProjectGuid> <OutputType>Exe</OutputType> - <TargetFramework>netcoreapp2.1</TargetFramework> <RootNamespace>czi_inspector</RootNamespace> + <AssemblyName>czi-inspector</AssemblyName> + <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> + <Deterministic>true</Deterministic> </PropertyGroup> - -</Project> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Xml.Linq" /> + <Reference Include="System.Data.DataSetExtensions" /> + <Reference Include="Microsoft.CSharp" /> + <Reference Include="System.Data" /> + <Reference Include="System.Net.Http" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="CziFile.cs" /> + <Compile Include="CziFileHeader.cs" /> + <Compile Include="CziParser.cs" /> + <Compile Include="Program.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="SegmentHeader.cs" /> + <Compile Include="Utils.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="App.config" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> +</Project> \ No newline at end of file diff --git a/czi-format/inspector/czi-inspector/czi-inspector.csproj.user b/czi-format/inspector/czi-inspector/czi-inspector.csproj.user new file mode 100644 index 0000000000000000000000000000000000000000..d7496f1176cc96f8f1509cfb62a6de3f9b242e1f --- /dev/null +++ b/czi-format/inspector/czi-inspector/czi-inspector.csproj.user @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'"> + <StartArguments>--dump data\CZT-Stack-Anno.czi cztstackdump.txt</StartArguments> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'"> + <StartArguments>data\CZT-Stack-Anno.czi</StartArguments> + </PropertyGroup> +</Project> \ No newline at end of file