Пример #1
0
        /// <summary>
        /// 文字列のCRC32を計算して文字列で返す
        /// </summary>
        /// <param name="srcStr"></param>
        /// <returns></returns>
        public static string CalcCRC32(string srcStr)
        {
            byte[] bytes = new UTF8Encoding().GetBytes(srcStr);
            uint   crc32 = Crc32CAlgorithm.Compute(bytes);

            return(Convert.ToString(crc32, 16));
        }
Пример #2
0
        public void CRC32PerformanceTest()
        {
            var data   = new byte[65536];
            var random = new Random();

            random.NextBytes(data);

            /*Crc32.Net*/
            long total     = 0;
            var  stopwatch = new Stopwatch();

            stopwatch.Start();

            while (stopwatch.Elapsed < TimeSpan.FromSeconds(3))
            {
                Crc32.NET.Crc32Algorithm.Compute(data, 0, data.Length);
                total += data.Length;
            }

            stopwatch.Stop();
            Console.WriteLine($@"Crc32.Net Throughput: {total / stopwatch.Elapsed.TotalSeconds / 1024 / 1024:0.0}MB/s");
            /*Crc32C.Net*/
            total     = 0;
            stopwatch = new Stopwatch();
            stopwatch.Start();

            while (stopwatch.Elapsed < TimeSpan.FromSeconds(3))
            {
                Crc32CAlgorithm.Compute(data, 0, data.Length);
                total += data.Length;
            }

            stopwatch.Stop();
            Console.WriteLine($@"Crc32C.Net Throughput: {total / stopwatch.Elapsed.TotalSeconds / 1024 / 1024:0.0}MB/s");
        }
Пример #3
0
        public static uint ToCrc32(this string selfStr)
        {
            var  bytes = Encoding.UTF8.GetBytes(selfStr);
            uint crc32 = Crc32CAlgorithm.Compute(bytes);

            return(crc32);
        }
Пример #4
0
        /// <summary>
        /// Write a single record.
        /// </summary>
        /// <param name="stream">Stream containing record in binary form</param>
        public void Write(Stream stream)
        {
            ulong length     = (ulong)stream.Length;
            uint  crcLength  = Crc32CAlgorithm.Compute(BitConverter.GetBytes(length));
            uint  maskLength = MaskCrc32(crcLength);

            _fileStream.Write(BitConverter.GetBytes(length), 0, 8);     // uint64 length
            _fileStream.Write(BitConverter.GetBytes(maskLength), 0, 4); // uint32 masked_crc32_of_length
            stream.Seek(0, SeekOrigin.Begin);                           // Read from head

            byte[] buffer   = new byte[BufferSize];
            int    count    = 0;
            int    readSize = stream.Read(buffer, 0, buffer.Length);
            uint   crcData  = Crc32CAlgorithm.Compute(buffer, 0, readSize);

            for (bool firstRun = true; readSize > 0;
                 count += readSize, readSize = stream.Read(buffer, 0, buffer.Length), firstRun = false)
            {
                if (!firstRun)
                {
                    crcData = Crc32CAlgorithm.Append(crcData, buffer, 0, readSize);
                }
                _fileStream.Write(buffer, 0, readSize); // byte data[length]
            }

            if (count != (int)length)
            {
                throw new Exception("Stream length does not equal to read length.");
            }

            uint maskCrcData = MaskCrc32(crcData);

            _fileStream.Write(BitConverter.GetBytes(maskCrcData), 0, 4); // uint32 masked_crc32_of_data
        }
Пример #5
0
        public async Task <ScrapeJobEvent> ExecuteAsync(Uri url, string searchPattern)
        {
            if (url == null)
            {
                return(null);
            }

            var response = await GetResponseAsync(url);

            var    eventType   = ScrapeJobEventType.Error;
            string fingerprint = "";

            if (!string.IsNullOrWhiteSpace(response.HtmlString))
            {
                var matches = MatchesPattern(response.HtmlString, searchPattern);
                eventType   = matches ? ScrapeJobEventType.Match : ScrapeJobEventType.NoMatch;
                fingerprint = Crc32CAlgorithm.Compute(response.HtmlBytes).ToString(CultureInfo.InvariantCulture);
            }

            return(new ScrapeJobEvent
            {
                HttpResponseCode = response.StatusCode,
                HttpResponseTimeInMs = response.ResponseTimeInMs,
                TimeStamp = DateTime.UtcNow,
                Type = eventType,
                Url = url.AbsoluteUri,
                Fingerprint = fingerprint
            });
        }
Пример #6
0
        public void MethodConsistency(string text, int offset, int tail, int split)
        {
            var checksums = new List <uint>();
            var array     = Encoding.ASCII.GetBytes(text);
            var padded    = Enumerable.Repeat <byte>(17, offset).Concat(array).Concat(Enumerable.Repeat <byte>(93, tail)).ToArray();
            var half1     = array.Take(split).ToArray();
            var half2     = array.Skip(split).ToArray();

            checksums.Add(Crc32CAlgorithm.Compute(array));
            checksums.Add(Crc32CAlgorithm.Compute(padded, offset, array.Length));
            checksums.Add(Crc32CAlgorithm.Append(0, array));
            checksums.Add(Crc32CAlgorithm.Append(0, padded, offset, array.Length));
            checksums.Add(Crc32CAlgorithm.Append(Crc32CAlgorithm.Append(0, half1), half2));
            checksums.Add(Crc32CAlgorithm.Append(Crc32CAlgorithm.Append(0, padded, offset, split), padded, offset + split, array.Length - split));
            using (var hash = new Crc32CAlgorithm())
                checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(array), 0));
            using (var hash = new Crc32CAlgorithm())
                checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(padded, offset, array.Length), 0));
            using (var stream = new MemoryStream(array))
                using (var hash = new Crc32CAlgorithm())
                    checksums.Add(BitConverter.ToUInt32(hash.ComputeHash(stream), 0));
            if (text.Length == 0)
            {
                Assert.AreEqual(0, checksums[0]);
            }
            foreach (var checksum in checksums)
            {
                Assert.AreEqual(checksums[0], checksum);
            }
        }
Пример #7
0
        private async void GetResourceAsync(DownloadFile file)
        {
            try
            {
                log.Debug("zaczynam pobierac " + file.Name);
                using (var client = new WebClient())
                {
                    var responseBody = await client.DownloadStringTaskAsync(file.Url);

                    byte[] bytes = Encoding.ASCII.GetBytes(responseBody);
                    performanceCounter.IncrementBy(bytes.Length);
                    uint crc = Crc32CAlgorithm.Compute(bytes);
                    log.Debug(file.Name + " zasob pobrany pomyslnie.");

                    if (file.Status == Status.Success)
                    {
                        if (crc != file.Crc)
                        {
                            log.Info(file.Name + " rozni sie od poprzednio pobranego.");
                        }
                    }
                    file.Status     = Status.Success;
                    file.Crc        = crc;
                    file.LastAccess = DateTime.Now;
                }
            }
            catch (WebException e)
            {
                file.Status = Status.Failed;
                log.Error(file.Name + " cos poszlo nie tak " + e.Message);
            }
        }
Пример #8
0
        public override async System.Threading.Tasks.Task <List <FileModel> > getFilesOutDatedAsync()
        {
            WebClient client = new WebClient();

            Uri uri = new Uri(config.BaseURL + Routes.UPDATE_FILE_LIST);

            String updateJson;

            try
            {
                updateJson = await client.DownloadStringTaskAsync(uri);
            }
            catch (Exception e)
            {
                throw new CheckUpdateError(e.Message);
            }

            List <FileModel> list = JsonConvert.DeserializeObject <List <FileModel> >(updateJson);

            List <FileModel> lists = list.FindAll(delegate(FileModel model) {
                if (!File.Exists(model.ClientPath))
                {
                    return(true);
                }

                byte[] clientFile = File.ReadAllBytes(model.ClientPath);

                uint crc = Crc32CAlgorithm.Compute(clientFile);

                return(!model.cCRC.Equals(crc.ToString()));
            });

            return(lists);
        }
Пример #9
0
        public void Write(IProgress <Tuple <string, int> > progress)
        {
            List <uint> md5s = new List <uint>();

            using (FileStream arc = new FileStream(Filename, FileMode.Create))
                using (MemoryStream header = new MemoryStream())
                    using (BinaryWriter writer = new BinaryWriter(arc))
                        using (BinaryWriter headerWriter = new BinaryWriter(header))
                        {
                            writer.Write(Encoding.ASCII.GetBytes(ExtendedArchive.Magic));

                            writer.Write(ExtendedArchive.Version);
                            writer.Write((ushort)1);

                            byte[] title = Encoding.Unicode.GetBytes(Name);
                            writer.Write((ushort)title.Length);
                            writer.Write(title);

                            writer.Write((uint)Files.Count);

                            int headerLength = Files.Sum(x => x.HeaderLength);

                            writer.Write((uint)headerLength);

                            long headerPos = writer.BaseStream.Position;
                            writer.BaseStream.Position = headerLength + 1024; //a 1kb space is left incase the header needs to be shifted later

                            int i = 0;
                            foreach (var file in Files)
                            {
                                uint crc = Crc32CAlgorithm.Compute(file.Source.Md5);
                                i++;

                                try
                                {
                                    file.WriteTo(writer, headerWriter, md5s.Contains(crc));
                                }
                                catch
                                {
                                    progress.Report(new Tuple <string, int>(
                                                        "[" + i + " / " + Files.Count + "] Stopped writing " + file.Name + "\n",
                                                        100 * i / Files.Count));

                                    throw;
                                }

                                progress.Report(new Tuple <string, int>(
                                                    "[" + i + " / " + Files.Count + "] Written " + file.Name + "... (" + file.Source.Size + " bytes)\n",
                                                    100 * i / Files.Count));

                                md5s.Add(crc);
                            }

                            writer.BaseStream.Position = headerPos;
                            writer.Write(header.ToArray());
                        }

            progress.Report(new Tuple <string, int>("Finished.\n", 100));
        }
Пример #10
0
        public bool VerifyFile(DistributionFile file, FileInfo fileInfo)
        {
            var bytes = File.ReadAllBytes(fileInfo.FullName);

            return
                (Crc32CAlgorithm.Compute(bytes) == file.Hash &&
                 _signatureTool.Verify(file.Signature, bytes));
        }
Пример #11
0
        private static Guid GetHash(byte[] b1, byte[] b2)
        {
            uint i1 = Crc32Algorithm.Compute(b1);
            uint i2 = Crc32CAlgorithm.Compute(b2);

            byte[] i3 = BitConverter.GetBytes(Crc32Algorithm.Compute(b1));
            byte[] i4 = BitConverter.GetBytes(Crc32CAlgorithm.Compute(b2));

            return(new Guid(i1, (ushort)i2, (ushort)(i2 >> 16), i3[0], i3[1], i3[2], i3[3], i4[0], i4[1], i4[2], i4[3]));
        }
Пример #12
0
        public static string GetCRCVersion(string filePath)
        {
            byte[] buffer = File.ReadAllBytes(filePath);
            //FileStream stream = new FileStream( filePath, FileMode.Open );
            //byte[] buffer = new byte[stream.Length];
            //stream.Read( buffer, 0, (int)stream.Length );
            //stream.Close();
            uint crc32 = Crc32CAlgorithm.Compute(buffer);

            return("?v=" + crc32);
        }
Пример #13
0
        /// <summary>
        /// Sha256 object hash
        /// </summary>
        /// <param name="value">Object to hash</param>
        /// <returns>String hash</returns>
        public static string Crc32(object value)
        {
            if (value == null)
            {
                return(string.Empty);
            }

            var bytes = Byte(value);

            return(Crc32CAlgorithm.Compute(bytes).ToString());
        }
Пример #14
0
        public static string GetFileCRC32(string path)
        {
            // Read by 512 bytes
            // No idea if this is accurate for detecting changes in files
            var file = new FileStream(path, FileMode.Open);
            var data = new byte[512];

            file.Read(data, 0, 512);
            file.Close();

            return(Crc32CAlgorithm.Compute(data).ToString());
        }
Пример #15
0
 public static UInt32 GetHash(string Name)
 {
     try
     {
         return(Crc32CAlgorithm.Compute(File.ReadAllBytes(Name)));
     }
     catch
     {
         MessageBox.Show(Name + " cannot be opened.");
         return(new UInt32());
     }
 }
Пример #16
0
        /// <summary>
        ///  Reads a protocol message from the specified byte-field and calls appropriate methods to process this message.
        /// </summary>
        /// <remarks>
        ///  This function checks all applicable checksums and validates that the message is complete before calling one of the specialized
        ///  methods to handle actual decoding and processing.
        /// </remarks>
        /// <param name="recv_buffer">Byte-field with an Ixian protocol message.</param>
        /// <param name="endpoint">Remote endpoint from where the message was received.</param>
        public static void readProtocolMessage(QueueMessageRaw raw_message, MessagePriority priority, RemoteEndpoint endpoint)
        {
            if (endpoint == null)
            {
                Logging.error("Endpoint was null. readProtocolMessage");
                return;
            }

            ProtocolMessageCode code = raw_message.code;

            // Filter messages
            if (endpoint.presence == null)
            {
                // Check for presence and only accept hello and bye messages if there is no presence.
                if (code != ProtocolMessageCode.hello &&
                    code != ProtocolMessageCode.helloData &&
                    code != ProtocolMessageCode.bye)
                {
                    return;
                }
            }
            if (raw_message.legacyChecksum != null)
            {
                // Compute checksum of received data
                byte[] local_checksum = Crypto.sha512sqTrunc(raw_message.data, 0, 0, 32);

                // Verify the checksum before proceeding
                if (local_checksum.SequenceEqual(raw_message.legacyChecksum) == false)
                {
                    Logging.error("Dropped message (invalid legacy checksum)");
                    return;
                }
            }
            else
            {
                // Compute checksum of received data
                uint local_checksum = Crc32CAlgorithm.Compute(raw_message.data);

                // Verify the checksum before proceeding
                if (local_checksum != raw_message.checksum)
                {
                    Logging.error("Dropped message (invalid checksum)");
                    return;
                }
            }


            // Can proceed to parse the data parameter based on the protocol message code.
            // Data can contain multiple elements.
            //parseProtocolMessage(code, data, socket, endpoint);
            NetworkQueue.receiveProtocolMessage(code, raw_message.data, Crc32CAlgorithm.Compute(raw_message.data), priority, endpoint);
        }
Пример #17
0
        public void Compute(string asciiChars, uint expectedResult)
        {
            // Arrange

            var bytes = Encoding.ASCII.GetBytes(asciiChars);

            // Act

            var result = Crc32CAlgorithm.Compute(bytes);

            // Assert

            Assert.Equal(expectedResult, result);
        }
Пример #18
0
        public static string GetCRCVersion(string filePath)
        {
            //var inputArray = new byte[realDataLength + 4];
            //// write real data to inputArray
            //Crc32Algorithm.ComputeAndWriteToEnd( inputArray ); // last 4 bytes contains CRC
            //                                                   // transferring data or writing reading, and checking as final operation
            //if( !Crc32Algorithm.IsValidWithCrcAtEnd( inputArray ) )
            //    throw new InvalidOperationException( "Data was tampered" );

            byte[] zipdata = File.ReadAllBytes(filePath);
            uint   crc     = Crc32CAlgorithm.Compute(zipdata);

            return("?v=" + crc);
        }
Пример #19
0
        public void Exceptions()
        {
            Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Compute(null));
            Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Compute(null, 0, 0));
            Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Append(0, null));
            Assert.Throws <ArgumentNullException>(() => Crc32CAlgorithm.Append(0, null, 0, 0));
            var buffer = new byte[10];

            Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Compute(buffer, -1, 5));
            Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Compute(buffer, 0, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Compute(buffer, 5, 6));
            Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Append(0, buffer, -1, 5));
            Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Append(0, buffer, 0, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => Crc32CAlgorithm.Append(0, buffer, 5, 6));
        }
Пример #20
0
        private string GetHash(string Name)
        {
            if (Name == string.Empty)
            {
                return(null);
            }

            try
            {
                return(Crc32CAlgorithm.Compute(File.ReadAllBytes(Name)).ToString("x2"));
            }
            catch
            {
                MessageBox.Show(Name + " cannot be opened.");
                return(null);
            }
        }
Пример #21
0
		private void CalculateCrc32(PhotoModel item)
		{
			var crc = Crc32CAlgorithm.Compute(Encoding.Default.GetBytes(item.LocalPath.ToLowerInvariant()));
			var buffer = new byte[8096];
			using (var stream = File.OpenRead(item.LocalPath))
			{
				while (true)
				{
					var read = stream.Read(buffer, 0, 8096);
					crc = Crc32CAlgorithm.Append(crc, buffer, 0, read);
					if (read < 8096)
					{
						item.Crc32 = crc.ToString();
						return;
					}
				}
			}
		}
Пример #22
0
        public SHashedName(string name)
        {
            // Hashes names should be case insensitive thats why we convert to lower before hashing
            string lowerName = name.ToLower();

            hash = Crc32CAlgorithm.Compute(Encoding.Unicode.GetBytes(lowerName));

#if !RELEASE
            // Only check for collisions in non release builds
            if (s_NameTable.ContainsKey(hash))
            {
                string tableName = s_NameTable[hash];
                if (tableName != lowerName)
                {
                    throw new Exception("Hash collision, name " + tableName + " collides with " + lowerName);
                }
            }
#endif
            s_NameTable[hash] = lowerName;
        }
Пример #23
0
        private void btnNext_Click(object sender, EventArgs e)
        {
            cbxTag.Items.Clear();
            foreach (var item in liteCollection.FindAll().ToList())
            {
                cbxTag.Items.AddRange(item.Tags.ToArray());
            }

            if (currentMemoryStream != null && currentMemoryStream.CanRead)
            {
                currentMemoryStream.Dispose();
            }

            liTags.Items.Clear();
            imageItem = new ImageItem()
            {
                Tags = new List <string>()
            };

            lblHash.Text = "Loading";
            Application.DoEvents();

            WebClient client = new WebClient();
            var       data   = client.DownloadData(
                "https://source.unsplash.com/random/256x256");

            lblHash.Text = "Loading..";
            Application.DoEvents();

            var hash = Crc32CAlgorithm.Compute(data).ToString();

            lblHash.Text = "Loading....";
            Application.DoEvents();

            currentMemoryStream = new MemoryStream(data);

            pictureBox1.Image = Image.FromStream(currentMemoryStream);
            lblHash.Text      = hash;
            imageItem.Id      = hash;
        }
Пример #24
0
        static uint ComputeMasked(byte[] data, int offset, int count)
        {
            var checksum = Crc32CAlgorithm.Compute(data, offset, count);

            return(((checksum >> 15) | (checksum << 17)) + 0xa282ead8);
        }
Пример #25
0
        private bool PopulateImageFolder(List <Tile> lTile, string dataFolder)
        {
            try
            {
                List <Tile> lt = lTile.Where(x => x.ImagePath != "").ToList();

                foreach (Tile t in lt)
                {
                    if (t.ImagePath.Length > 0)
                    {
                        t.ImagePath = (!t.ImagePath.Contains(@"\")) ? @"Images\" + t.ImagePath : t.ImagePath;

                        string imgFilePath = dataFolder + t.ImagePath;

                        if (File.Exists(imgFilePath))
                        {
                            uint imgCrc = Crc32CAlgorithm.Compute(File.ReadAllBytes(imgFilePath));

                            if (imgCrc != t.TileImage.ImageCRC)
                            {
                                if (t.TileImage.IsOnlyCRC)
                                {
                                    // no image data, return
                                    return(false);
                                }
                                else
                                {
                                    File.WriteAllBytes(imgFilePath, t.TileImage.ImageData);
                                }
                            }
                            else
                            {
                                this.GetType();
                            }
                        }
                        else
                        {
                            if (t.TileImage.IsOnlyCRC)
                            {
                                // no image data, return
                                return(false);
                            }
                            else
                            {
                                File.WriteAllBytes(imgFilePath, t.TileImage.ImageData);
                            }
                        }
                    }

                    if (t.ChildTiles.Count > 0)
                    {
                        if (!PopulateImageFolder(t.ChildTiles, dataFolder))
                        {
                            return(false);
                        }
                    }
                }

                /*lt = lTile.Where(x => x.ChildTiles != null && x.ChildTiles.Count > 0).ToList();
                 *
                 * foreach (Tile tile in lt)
                 * {
                 *  foreach (Tile t in tile.ChildTiles)
                 *  {
                 *      t.ImagePath = (!t.ImagePath.Contains(@"\")) ? @"Images\" + t.ImagePath : t.ImagePath;
                 *
                 *      string imgFilePath = dataFolder + t.ImagePath;
                 *      if (File.Exists(imgFilePath))
                 *      {
                 *          uint imgCrc = Crc32CAlgorithm.Compute(File.ReadAllBytes(imgFilePath));
                 *
                 *          if (imgCrc != t.TileImage.ImageCRC)
                 *          {
                 *              File.WriteAllBytes(imgFilePath, t.TileImage.ImageData);
                 *          }
                 *
                 *      }
                 *      else
                 *      {
                 *          File.WriteAllBytes(imgFilePath, t.TileImage.ImageData);
                 *      }
                 *
                 *      if (t.ChildTiles.Count > 0)
                 *      {
                 *          PopulateImageFolder(t.ChildTiles, dataFolder);
                 *      }
                 *  }
                 * }*/

                _isGameSelectorImageFolderPopulated = true;
                return(true);
            }
            catch (Exception ex)
            {
                logger.Error("PopulateImageFolder:" + ex.ToString());
                return(false);
            }
        }
Пример #26
0
        public string GetCRC32()
        {
            byte[] messagesToBytes = asciiEncoding.GetBytes(Messages.ToString());

            return(Crc32CAlgorithm.Compute(messagesToBytes).ToString());
        }
Пример #27
0
        public async Task <DocumentDefinition> ParseDocument(DirectoryInfo repositoryPath, FileInfo file, CancellationToken token)
        {
            logger.Debug("ParseDocument: {0}", file);
            Guard.NotNull(() => file, file);
            Guard.NotNull(() => repositoryPath, repositoryPath);
            Guard.IsValid(() => file, file, info => info.Exists, "invalid file");

            var parser = textParserFactory.ConstructParsers(file);

            if (parser == NullTextParser.Instance)
            {
                logger.Debug("Null parser: {0}", file);
                return(null);
            }

            var text = parser.Parse();
            DocumentDefinition definition = new DocumentDefinition();
            var bytes = File.ReadAllBytes(file.FullName);

            token.ThrowIfCancellationRequested();
            definition.Crc32 = Crc32CAlgorithm.Compute(bytes);
            string path = string.IsNullOrEmpty(repositoryPath.FullName) || repositoryPath.FullName[repositoryPath.FullName.Length - 1] == Path.DirectorySeparatorChar
                              ? repositoryPath.FullName
                              : $"{repositoryPath.FullName}{Path.DirectorySeparatorChar}";
            var directory = path.GetRelativePath(file.DirectoryName);

            if (directory == string.Empty)
            {
                if (file.Directory != null)
                {
                    definition.Labels = new[] { file.Directory.Name };
                }
            }
            else
            {
                definition.Labels = new[] { directory.Split(Path.DirectorySeparatorChar).First() };
            }

            definition.Labels = definition.Labels.Select(item => item.CreateLetterText()).ToArray();
            definition.Path   = file.FullName;

            if (!string.IsNullOrWhiteSpace(text))
            {
                var result = await textSplitter.Splitter.Process(new ParseRequest(text)).ConfigureAwait(false);

                token.ThrowIfCancellationRequested();
                var review = result.GetReview(textSplitter.DataLoader);
                token.ThrowIfCancellationRequested();
                var words = review.Items.Where(
                    item =>
                    item.POS.WordType != WordType.Number &&
                    item.POS.WordType != WordType.SeparationSymbol &&
                    item.POS.WordType != WordType.Symbol &&
                    item.POS.WordType != WordType.Conjunction &&
                    item.POS.WordType != WordType.Sentence &&
                    !item.IsStopWord)
                            .Select(item => item.Text)
                            .ToArray();

                foreach (var word in words)
                {
                    token.ThrowIfCancellationRequested();
                    string underlyingWord;
                    if (!wordsTable.TryGetAddItem(word, word, out underlyingWord))
                    {
                        underlyingWord = word;
                    }

                    var total = definition.WordsTable.GetSafe(underlyingWord);
                    total++;
                    definition.WordsTable[underlyingWord] = total;
                }
            }

            return(definition);
        }
Пример #28
0
        //   Adds a line to the spreadsheet indented at the appropriate level.

        private static void AddFileRow(FileInfo fileInfo, int column = 1, int fontSize = 10, bool makeBold = false, int fontColor = (int)System.ConsoleColor.Black)
        {
            Worksheet activeSheet = Globals.ThisAddIn.Application.ActiveSheet;
            Range     formatRange;

            string strS = null;

            if (true == _GroupResults)
            {
                if (_GroupLevel == column)
                {
                    // Mark starting point.  We may need it later for grouping.  Don't reset.
                    if (0 == _GroupStartRow)
                    {
                        _GroupStartRow = _Row;
                    }
                }
            }

            if (_TableSyleOuput)
            {
                formatRange = activeSheet.Cells[_Row, _INITIAL_COL];
            }
            else
            {
                formatRange = activeSheet.Cells[_Row, column];
            }

            // If we are not displaying folders but for some reason
            // are calling AddFileRow(), then use full path.

            if (_ShowFolders)
            {
                formatRange.Value = string.Format(" - {0}", fileInfo.Name);
            }
            else
            {
                //formatRange.Value = string.Format(" - {0}", fileInfo.FullName);
                // HACK(crhodes)
                // This let's us come back and wrap a table around everything to see if there are duplicate files.
                formatRange.Value = string.Format("{0}", fileInfo.DirectoryName);
                formatRange.Offset[0, 1].Value      = string.Format("{0}", fileInfo.Name);
                formatRange.Offset[0, 1].Font.Size  = fontSize;
                formatRange.Offset[0, 1].Font.Color = fontColor;

                if (_CalculateCRC)
                {
                    try
                    {
                        using (var fileStream = fileInfo.OpenRead())
                        {
                            byte[] fileBytes = File.ReadAllBytes(fileInfo.FullName);

                            formatRange.Offset[0, 2].Value = Crc32CAlgorithm.Compute(fileBytes).ToString();
                        }
                    }
                    catch (System.IO.IOException ioex)
                    {
                        formatRange.Offset[0, 2].Value = ioex.ToString();
                    }
                    catch (Exception ex)
                    {
                        var et = ex.GetType();
                        var be = ex.GetBaseException();
                        MessageBox.Show(ex.ToString());
                    }
                }
            }

            formatRange.Font.Bold = makeBold;
            formatRange.Font.Size = fontSize;
            //.ColorIndex = fontColor
            formatRange.Font.Color = fontColor;

            // Start at _FILE_INFO_COL + 1 as we don't display file count on file row.

            formatRange              = activeSheet.Cells[_Row, _FILE_INFO_COL + 1];
            formatRange.Value        = fileInfo.Length;
            formatRange.NumberFormat = "#,##0_);(#,##0)";

            formatRange.Font.Bold = makeBold;
            formatRange.Font.Size = fontSize;

            Range rng = default(Microsoft.Office.Interop.Excel.Range);

            System.DateTime dateD = default(System.DateTime);

            rng       = activeSheet.Cells[_Row, _FILE_INFO_COL + 2];
            dateD     = fileInfo.CreationTime;
            rng.Value = dateD;

            ColorCodeDate(rng, dateD, false, fontSize, _DateType.LastCreate);

            rng       = activeSheet.Cells[_Row, _FILE_INFO_COL + 3];
            dateD     = fileInfo.LastWriteTime;
            rng.Value = dateD;

            ColorCodeDate(rng, dateD, false, fontSize, _DateType.LastWrite);

            rng       = activeSheet.Cells[_Row, _FILE_INFO_COL + 4];
            dateD     = fileInfo.LastAccessTime;
            rng.Value = dateD;

            ColorCodeDate(rng, dateD, false, fontSize, _DateType.LastAccess);

            // Now check if we need to do any grouping.

            if (_GroupStartRow > 0)
            {
                // We have been adding rows to a possible grouping set.
                if (_GroupLevel > column)
                {
                    // We have transitioned up the chain above the grouping set.
                    //strS = _GroupStartRow + ":" + _Row - 1;
                    strS = string.Format("{0}:{1}", _GroupStartRow, _Row - 1);
                    activeSheet.Rows[strS].Group();
                    // Reset the grouping counter.
                    _GroupStartRow = 0;
                }
            }

            // Next row to add content.
            _Row = _Row + 1;
        }
Пример #29
0
        static async Task <int> Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine($"Usage: {AppDomain.CurrentDomain.FriendlyName} [--hash] <svg path> <ico path>\n");
                Console.WriteLine($"Options:");
                Console.WriteLine($"  --hash    Append a CRC32C hash to the ico filename to prevent caching.");
                return(1);
            }

            bool   appendHash = args[0] == "--hash";
            string svgPath    = Path.GetFullPath(appendHash ? args[1] : args[0]);
            string icoPath    = Path.GetFullPath(appendHash ? args[2] : args[1]);

            if (!File.Exists(svgPath))
            {
                Console.WriteLine($"Could not find svg '{svgPath}'.");
                return(1);
            }

            using (var images = new MagickImageCollection())
            {
                Console.WriteLine("Downloading Chromium");
                var downloader = new Downloader(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ".local-chromium"));
                await downloader.DownloadRevisionAsync(Downloader.DefaultRevision);

                Console.WriteLine("Launching headless Chrome");
                var options = new LaunchOptions()
                {
                    Headless       = true,
                    ExecutablePath = downloader.GetExecutablePath(Downloader.DefaultRevision)
                };
                using (var browser = await Puppeteer.LaunchAsync(options, Downloader.DefaultRevision))
                    using (var page = await browser.NewPageAsync())
                    {
                        Console.WriteLine("Opening svg");
                        await page.GoToAsync("file:///" + svgPath.Replace('\\', '/'));

                        foreach (int size in IconSizes)
                        {
                            Console.WriteLine($"Rendering {size}x{size} image");
                            await page.SetViewport(new ViewPortOptions()
                            {
                                Width = size, Height = size
                            });

                            using (var stream = await page.ScreenshotStreamAsync(new ScreenshotOptions()
                            {
                                OmitBackground = true
                            }))
                            {
                                images.Add(new MagickImage(stream));
                            }
                        }
                    }

                Console.WriteLine("Generating ico");
                using (var stream = new MemoryStream())
                {
                    images.Write(stream, MagickFormat.Ico);
                    byte[] bytes = stream.ToArray();

                    if (appendHash)
                    {
                        string hash = Crc32CAlgorithm.Compute(bytes).ToString("x8");
                        icoPath = Path.ChangeExtension(icoPath, hash + Path.GetExtension(icoPath));
                    }

                    File.WriteAllBytes(icoPath, bytes);
                    Console.WriteLine($"Saved {Path.GetFileName(icoPath)}");
                }
            }

            return(0);
        }
Пример #30
0
        public void WriteTo(BinaryWriter dataWriter, BinaryWriter metadataWriter, bool asDuplicate)
        {
            uint  actualsize = 0;
            uint  crc        = 0;
            ulong offset     = (ulong)dataWriter.BaseStream.Position;

            if (!asDuplicate)
            {
                using (MemoryStream buffer = new MemoryStream())
                    using (Stream source = Source.GetStream())
                    {
                        switch (Compression)
                        {
                        case ArchiveFileCompression.LZ4:
                            using (LZ4Stream lz = new LZ4Stream(buffer, LZ4StreamMode.Compress, LZ4StreamFlags.HighCompression | LZ4StreamFlags.IsolateInnerStream, 4 * 1048576))
                                source.CopyTo(lz);
                            break;

                        case ArchiveFileCompression.Zstandard:
                            using (ZstdNet.Compressor zstd = new ZstdNet.Compressor(new ZstdNet.CompressionOptions(3)))
                                using (MemoryStream temp = new MemoryStream())
                                {
                                    source.CopyTo(temp);
                                    byte[] output = zstd.Wrap(temp.ToArray());
                                    buffer.Write(output, 0, output.Length);
                                }
                            break;

                        case ArchiveFileCompression.Uncompressed:
                            source.CopyTo(buffer);
                            break;

                        default:
                            throw new InvalidOperationException("Compression type is invalid.");
                        }

                        buffer.Position = 0;
                        byte[] bBuffer = buffer.ToArray();
                        crc = Crc32CAlgorithm.Compute(bBuffer);
                        dataWriter.Write(bBuffer);
                    }


                long newsize = dataWriter.BaseStream.Position;
                actualsize = (uint)(newsize - (long)offset);
            }

            metadataWriter.Write((byte)Type);
            metadataWriter.Write((byte)Flags);

            if (asDuplicate)
            {
                metadataWriter.Write((byte)ArchiveFileCompression.Duplicate);
            }
            else
            {
                metadataWriter.Write((byte)Compression);
            }

            metadataWriter.Write((byte)0); //reserved

            metadataWriter.Write(crc);
            metadataWriter.Write(Source.Md5);

            metadataWriter.BaseStream.Seek(48, SeekOrigin.Current);

            byte[] uName = Encoding.Unicode.GetBytes(Name);
            metadataWriter.Write((ushort)uName.Length);
            metadataWriter.Write(uName);

            metadataWriter.Write(offset);
            metadataWriter.Write(Source.Size);
            metadataWriter.Write(actualsize);
        }