当前位置: 首页>>代码示例>>C#>>正文


C# Crc32.Validate方法代码示例

本文整理汇总了C#中Crc32.Validate方法的典型用法代码示例。如果您正苦于以下问题:C# Crc32.Validate方法的具体用法?C# Crc32.Validate怎么用?C# Crc32.Validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Crc32的用法示例。


在下文中一共展示了Crc32.Validate方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Read

        public Id3TagInfo Read(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }

            if (!inputStream.CanRead)
            {
                throw new ID3IOException("Cannot read data stream.");
            }

            //
            //  Read the bytes from the I/O stream.
            //
            var tagInfo = new Id3TagInfo();
            byte[] rawTagContent;

            using (var reader = new BinaryReader(inputStream))
            {
                var headerBytes = new byte[10];
                reader.Read(headerBytes, 0, 10);

                var rawTagLength = AnalyseHeader(headerBytes, tagInfo);
                rawTagContent = new byte[rawTagLength];

                reader.Read(rawTagContent, 0, rawTagLength);
            }

            //
            //  Check for Unsynchronisation Bytes
            //
            byte[] tagContent;
            if (tagInfo.UnsynchronisationFlag)
            {
                // Scan for unsynchronisation bytes!
                tagContent = RemoveUnsyncBytes(rawTagContent);
            }
            else
            {
                tagContent = rawTagContent;
            }

            Stream tagStream = new MemoryStream(tagContent);
            var length = tagContent.Length;
            using (var reader = new BinaryReader(tagStream))
            {
                //
                //  Check for Extended Header
                //
                if (tagInfo.ExtendedHeaderAvailable)
                {
                    AnalyseExtendedHeader(reader, tagInfo);
                }

                //
                //  Read all frames
                //
                var frameBytes = new List<byte>();
                var pos = reader.BaseStream.Position;
                while ((pos + 10) < length)
                {
                    var continueReading = AnalyseFrame(reader,tagInfo,frameBytes);
                    if (!continueReading)
                    {
                        break;
                    }

                    pos = reader.BaseStream.Position;
                }

                //
                //  Check CRC if available
                //
                if (tagInfo.ExtendHeaderV3 != null && tagInfo.ExtendHeaderV3.CRCDataPresent)
                {
                    var tagData = frameBytes.ToArray();
                    var crc32Value = tagInfo.ExtendHeaderV3.CRC;

                    var crc32 = new Crc32(Crc32.DefaultPolynom);
                    var crcOK = crc32.Validate(tagData, crc32Value);

                    if (!crcOK)
                    {
                        throw new ID3TagException("The CRC32 validation failed!");
                    }
                }
            }

            return tagInfo;
        }
开发者ID:saitodisse,项目名称:id3tag.net,代码行数:91,代码来源:IOController.cs

示例2: Read

        public Id3TagInfo Read(Stream inputStream)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }

            if (!inputStream.CanRead)
            {
                throw new ID3IOException("Cannot read data stream.");
            }

            //
            //  Read the bytes from the I/O stream.
            //
            var tagInfo = new Id3TagInfo();
            byte[] rawTagContent;

            using (var reader = new BinaryReader(inputStream))
            {
                var headerBytes = new byte[10];
                reader.Read(headerBytes, 0, 10);

                var rawTagLength = AnalyseHeader(headerBytes, tagInfo);
                rawTagContent = new byte[rawTagLength];

                reader.Read(rawTagContent, 0, rawTagLength);
            }

            //
            //  Check for Unsynchronisation Bytes
            //
            byte[] tagContent;
            if (tagInfo.UnsynchronisationFlag)
            {
                // Scan for unsynchronisation bytes!
                tagContent = RemoveUnsyncBytes(rawTagContent);
            }
            else
            {
                tagContent = rawTagContent;
            }

            Stream tagStream = new MemoryStream(tagContent);
            var length = tagContent.Length;
            using (var reader = new BinaryReader(tagStream))
            {
                //
                //  Check for Extended Header
                //
                if (tagInfo.ExtendedHeaderAvailable)
                {
                    AnalyseExtendedHeader(reader, tagInfo);
                }

                //
                //  Read the content
                //
                var frameBytes = new List<byte>();
                var pos = reader.BaseStream.Position;
                while ((pos + 10) < length)
                {
                    var continueReading = ReadContent(reader, tagInfo, frameBytes);
                    if (!continueReading)
                    {
                        break;
                    }

                    pos = reader.BaseStream.Position;
                }

                //
                //  Check CRC if available
                //
                if (tagInfo.ExtendedHeader != null && tagInfo.ExtendedHeader.CrcDataPresent)
                {
                    if (tagInfo.MajorVersion == 3)
                    {
                        var tagData = frameBytes.ToArray();
                        var crc32Value = tagInfo.ExtendedHeader.Crc32;

                        var crc32 = new Crc32(Crc32.DefaultPolynom);
                        var crcOk = crc32.Validate(tagData, crc32Value);

                        if (!crcOk)
                        {
                            throw new ID3TagException("The CRC32 validation failed!");
                        }
                    }
                    else
                    {
                        /*
                         *    c - CRC data present

                             If this flag is set, a CRC-32 [ISO-3309] data is included in the
                             extended header. The CRC is calculated on all the data between the
                             header and footer as indicated by the header's tag length field,
                             minus the extended header. Note that this includes the padding (if
                             there is any), but excludes the footer. The CRC-32 is stored as an
                             35 bit synchsafe integer, leaving the upper four bits always
//.........这里部分代码省略.........
开发者ID:saitodisse,项目名称:id3tag.net,代码行数:101,代码来源:IOController.cs

示例3: Read


//.........这里部分代码省略.........
                reader.Read(rawTagContent, 0, rawTagLength);
            }

            //
            //  Check for Unsynchronisation Bytes
            //
            byte[] tagContent;
            if (tagInfo.Unsynchronised)
            {
                // Scan for unsynchronisation bytes!
                Logger.LogInfo("Remove Unsynchronisatzion bytes.");
                tagContent = RemoveUnsyncBytes(rawTagContent);
            }
            else
            {
                tagContent = rawTagContent;
            }

            Stream tagStream = new MemoryStream(tagContent);
            int length = tagContent.Length;
            using (var reader = new BinaryReader(tagStream))
            {
                //
                //  Check for Extended Header
                //
                if (tagInfo.ExtendedHeaderAvailable)
                {
                    Logger.LogInfo("Analyse Extended Header");
                    AnalyseExtendedHeader(reader, tagInfo);
                }

                Logger.LogInfo(String.Format("Start reading ID3v2.{0} frame.", tagInfo.MajorVersion));

                //
                //  Read the content
                //
                var frameBytes = new List<byte>();
                long pos = reader.BaseStream.Position;
                while ((pos + 10) < length)
                {
                    Logger.LogInfo("Getting frame...");
                    bool continueReading = ReadContent(reader, tagInfo, frameBytes);
                    if (!continueReading)
                    {
                        break;
                    }

                    pos = reader.BaseStream.Position;
                }

                //
                //  Check CRC if available
                //
                if (tagInfo.ExtendedHeader != null && tagInfo.ExtendedHeader.CrcDataPresent)
                {
                    if (tagInfo.MajorVersion == 3)
                    {
                        byte[] tagData = frameBytes.ToArray();
                        ReadOnlyCollection<byte> crc32Value = tagInfo.ExtendedHeader.Crc32;

                        var crc32 = new Crc32(Crc32.DefaultPolynom);
                        bool crcOk = crc32.Validate(tagData, crc32Value);

                        if (!crcOk)
                        {
                            var ex = new Id3TagException("The CRC32 validation failed!");
                            Logger.LogError(ex);

                            throw ex;
                        }
                    }
                    else
                    {
                        /*
                         *    c - CRC data present

                             If this flag is set, a CRC-32 [ISO-3309] data is included in the
                             extended header. The CRC is calculated on all the data between the
                             header and footer as indicated by the header's tag length field,
                             minus the extended header. Note that this includes the padding (if
                             there is any), but excludes the footer. The CRC-32 is stored as an
                             35 bit synchsafe integer, leaving the upper four bits always
                             zeroed.

                                Flag data length       $05
                                Total frame CRC    5 * %0xxxxxxx

                         */

                        // TODO: Implement the CRC32 check for ID3v2.4
                        var ex = new NotSupportedException("CRC32 check is not support for > ID3 V2.3");
                        Logger.LogError(ex);

                        throw ex;
                    }
                }
            }

            return tagInfo;
        }
开发者ID:saitodisse,项目名称:id3tag.net,代码行数:101,代码来源:IOController.cs


注:本文中的Crc32.Validate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。