Beispiel #1
0
        private static bool WriteCommentVer01(System.IO.Stream stream, XyzCommentReader xyzComment)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (xyzComment == null)
            {
                throw new ArgumentNullException("xyzComment");
            }

            byte[] byteULong = new byte[8];

            // 00 ~ 01: 标识符号 : 是否是评论信息的标识( 0x02 标识为评论信息 )(写入)
            stream.WriteByte(xyzComment.Tag);

            // 01 ~ 02: 评论信息的版本号 ( 当前支持 0x01 版本 )(写入)
            stream.WriteByte((byte)xyzComment.Version);

            // 02 ~ 10: 对应XYZ文件内实体的唯一序号 ( 0 为XYZ主文件的主要评论信息, 1... 为其它XYZ文件内实体的评论信息 )(写入)
            xyzComment.EntryId.ToArrayInByte(ref byteULong, 0);
            stream.Write(byteULong, 0, byteULong.Length);

            // 10 ~ 11: 评论信息的压缩方式 (0xFF 为该评论信息已删除, 0 为无压缩, 1... 为其它压缩方式)(写入)
            stream.WriteByte((byte)xyzComment.CompressionType);

            // 11 ~ 13: 评论信息被压缩后的有效长度 ( 如果该评论信息已删除,当有新增的评论信息压缩后的有效长度小于此评论信息的有效长度则覆盖掉此评论信息,然后在此评论信息尾部新增一个新的空XYZ文件内实体来填充剩余的字节 )(写入)
            xyzComment.CompressedLength.ToArrayInByte(ref byteULong, 0);
            stream.Write(byteULong, 0, byteULong.Length);

            // 13 ~ 15: 原评论信息的有效长度(写入)
            xyzComment.UncompressedLength.ToArrayInByte(ref byteULong, 0);
            stream.Write(byteULong, 0, byteULong.Length);

            // 15 ~ 31: 原评论信息的CRC校验字节(写入)
            stream.Write(xyzComment.Crc, 0, xyzComment.Crc.Length);

            // 31 ~ ..: 评论信息被压缩后的字节
            if (xyzComment.CompressedData == null)
            {
                if (xyzComment.CompressedLength > 0)
                {
                    throw new ArgumentException("xyzComment.CompressedData == null && xyzComment.CompressedData.Length > 0 error!", "xyzComment.CompressedData & xyzComment.CompressedLength");
                }
                else
                {
                    return(true);
                }
            }

            if (xyzComment.CompressedLength > xyzComment.CompressedData.Length)
            {
                throw new ArgumentException("xyzComment.CompressedLength > xyzComment.CompressedData.Length error!", "xyzComment.CompressedData & xyzComment.CompressedLength");
            }

            stream.Write(xyzComment.CompressedData, 0, xyzComment.CompressedLength);

            return(true);
        }
        public static bool WriteComment( System.IO.Stream stream, XyzCommentReader xyzComment )
        {
            switch ( xyzComment.Version )
            {
                case XyzCommentVersion.XyzCommentVer01:

                    return WriteCommentVer01( stream, xyzComment );
                default:
                    break;
            }

            return false;
        }
        private static bool WriteCommentVer01( System.IO.Stream stream, XyzCommentReader xyzComment )
        {
            if ( stream == null )
                throw new ArgumentNullException( "stream" );

            if ( xyzComment == null )
                throw new ArgumentNullException( "xyzComment" );

            byte[] byteULong = new byte[8];

            // 00 ~ 01: 标识符号 : 是否是评论信息的标识( 0x02 标识为评论信息 )(写入)
            stream.WriteByte( xyzComment.Tag );

            // 01 ~ 02: 评论信息的版本号 ( 当前支持 0x01 版本 )(写入)
            stream.WriteByte( (byte)xyzComment.Version );

            // 02 ~ 10: 对应XYZ文件内实体的唯一序号 ( 0 为XYZ主文件的主要评论信息, 1... 为其它XYZ文件内实体的评论信息 )(写入)
            xyzComment.EntryId.ToArrayInByte( ref byteULong, 0 );
            stream.Write( byteULong, 0, byteULong.Length );

            // 10 ~ 11: 评论信息的压缩方式 (0xFF 为该评论信息已删除, 0 为无压缩, 1... 为其它压缩方式)(写入)
            stream.WriteByte( (byte)xyzComment.CompressionType );

            // 11 ~ 13: 评论信息被压缩后的有效长度 ( 如果该评论信息已删除,当有新增的评论信息压缩后的有效长度小于此评论信息的有效长度则覆盖掉此评论信息,然后在此评论信息尾部新增一个新的空XYZ文件内实体来填充剩余的字节 )(写入)
            xyzComment.CompressedLength.ToArrayInByte( ref byteULong, 0 );
            stream.Write( byteULong, 0, byteULong.Length );

            // 13 ~ 15: 原评论信息的有效长度(写入)
            xyzComment.UncompressedLength.ToArrayInByte( ref byteULong, 0 );
            stream.Write( byteULong, 0, byteULong.Length );

            // 15 ~ 31: 原评论信息的CRC校验字节(写入)
            stream.Write( xyzComment.Crc, 0, xyzComment.Crc.Length );

            // 31 ~ ..: 评论信息被压缩后的字节
            if ( xyzComment.CompressedData == null )
            {
                if ( xyzComment.CompressedLength > 0 )
                    throw new ArgumentException( "xyzComment.CompressedData == null && xyzComment.CompressedData.Length > 0 error!", "xyzComment.CompressedData & xyzComment.CompressedLength" );
                else
                    return true;
            }

            if ( xyzComment.CompressedLength > xyzComment.CompressedData.Length )
                throw new ArgumentException( "xyzComment.CompressedLength > xyzComment.CompressedData.Length error!", "xyzComment.CompressedData & xyzComment.CompressedLength" );

            stream.Write( xyzComment.CompressedData, 0, xyzComment.CompressedLength );

            return true;
        }
Beispiel #4
0
        public static bool WriteComment(System.IO.Stream stream, XyzCommentReader xyzComment)
        {
            switch (xyzComment.Version)
            {
            case XyzCommentVersion.XyzCommentVer01:

                return(WriteCommentVer01(stream, xyzComment));

            default:
                break;
            }

            return(false);
        }
        private static XyzCommentReader GetXyzCommentVer01( System.IO.Stream stream )
        {
            XyzCommentReader xyzCommentReader = new XyzCommentReader();
            xyzCommentReader.m_CommentTag = XYZ_COMMENT_TAG;
            xyzCommentReader.m_CommentVersion = XyzCommentVersion.XyzCommentVer01;

            byte[] byteULong = new byte[8];

            // 02 ~ 10: 对应XYZ文件内实体的唯一序号 ( 0 为XYZ主文件的主要评论信息, 1... 为其它XYZ文件内实体的评论信息 )(读取)
            int iCount = stream.Read( byteULong, 0, byteULong.Length );
            if (iCount != byteULong.Length)
                return null;
            else
                xyzCommentReader.m_EntryId = byteULong.ConvertToUInt64();

            // 10 ~ 11: 评论信息的压缩方式 (0xFF 为该评论信息已删除, 0 为无压缩, 1... 为其它压缩方式)(读取)
            int iCompressionType = stream.ReadByte();
            if ( iCompressionType == (int)XyzCommentCompression.Invalid )
            {
                xyzCommentReader.m_CompressionType = XyzCommentCompression.Invalid;
                xyzCommentReader.m_IsCommentDeleted = true;
            }
            else if ( iCompressionType > (int)XyzCommentCompression.MaxValue )
                return null;
            else
            {
                xyzCommentReader.m_CompressionType = (XyzCommentCompression)iCompressionType;
                xyzCommentReader.m_IsCommentDeleted = false;
            }

            byte[] byteUShort = new byte[2];

            // 11 ~ 13: 评论信息被压缩后的有效长度 ( 如果该评论信息已删除,当有新增的评论信息压缩后的有效长度小于此评论信息的有效长度则覆盖掉此评论信息,然后在此评论信息尾部新增一个新的空XYZ文件内实体来填充剩余的字节 )(读取)
            iCount = stream.Read( byteUShort, 0, byteUShort.Length );
            if ( iCount != byteUShort.Length )
                return null;
            else
                xyzCommentReader.m_CompressedLength = byteUShort.ConvertToUInt16();

            // 13 ~ 15: 原评论信息的有效长度(读取)
            iCount = stream.Read( byteUShort, 0, byteUShort.Length );
            if ( iCount != byteUShort.Length )
                return null;
            else
                xyzCommentReader.m_UncompressedLength = byteUShort.ConvertToUInt16();

            byte[] byteCrc = new byte[16];

            // 15 ~ 31: 原评论信息的CRC校验字节(读取)
            iCount = stream.Read( byteCrc, 0, byteCrc.Length );
            if ( iCount != byteCrc.Length )
                return null;
            else
                xyzCommentReader.m_Crc = byteCrc;

            xyzCommentReader.m_ValueOffset = (ulong)stream.Position;

            byte[] byteValue = new byte[xyzCommentReader.CompressedLength];

            // 31 ~ ..: 评论信息被压缩后的字节(读取)
            iCount = stream.Read( byteValue, 0, byteValue.Length );
            if ( iCount != byteValue.Length )
                return null;

            byte[] byteData = null;
            switch ( xyzCommentReader.CompressionType )
            {
                case XyzCommentCompression.None:

                    byteData = byteValue;
                    break;
                case XyzCommentCompression.Deflate:

                    break;
                case XyzCommentCompression.Bzip2:

                    break;
                case XyzCommentCompression.Lzw:

                    break;
                case XyzCommentCompression.Ppmd:

                    break;
                default:

                    return null;
            }

            if ( byteData == null )
                return null;
            else
                xyzCommentReader.m_CommentValue = XyzConstants.UTF8.GetString( byteData );

            return xyzCommentReader;
        }
        private static XyzCommentReader GetXyzCommentVer01(System.IO.Stream stream)
        {
            XyzCommentReader xyzCommentReader = new XyzCommentReader();

            xyzCommentReader.m_CommentTag     = XYZ_COMMENT_TAG;
            xyzCommentReader.m_CommentVersion = XyzCommentVersion.XyzCommentVer01;

            byte[] byteULong = new byte[8];

            // 02 ~ 10: 对应XYZ文件内实体的唯一序号 ( 0 为XYZ主文件的主要评论信息, 1... 为其它XYZ文件内实体的评论信息 )(读取)
            int iCount = stream.Read(byteULong, 0, byteULong.Length);

            if (iCount != byteULong.Length)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_EntryId = byteULong.ConvertToUInt64();
            }

            // 10 ~ 11: 评论信息的压缩方式 (0xFF 为该评论信息已删除, 0 为无压缩, 1... 为其它压缩方式)(读取)
            int iCompressionType = stream.ReadByte();

            if (iCompressionType == (int)XyzCommentCompression.Invalid)
            {
                xyzCommentReader.m_CompressionType  = XyzCommentCompression.Invalid;
                xyzCommentReader.m_IsCommentDeleted = true;
            }
            else if (iCompressionType > (int)XyzCommentCompression.MaxValue)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_CompressionType  = (XyzCommentCompression)iCompressionType;
                xyzCommentReader.m_IsCommentDeleted = false;
            }

            byte[] byteUShort = new byte[2];

            // 11 ~ 13: 评论信息被压缩后的有效长度 ( 如果该评论信息已删除,当有新增的评论信息压缩后的有效长度小于此评论信息的有效长度则覆盖掉此评论信息,然后在此评论信息尾部新增一个新的空XYZ文件内实体来填充剩余的字节 )(读取)
            iCount = stream.Read(byteUShort, 0, byteUShort.Length);
            if (iCount != byteUShort.Length)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_CompressedLength = byteUShort.ConvertToUInt16();
            }

            // 13 ~ 15: 原评论信息的有效长度(读取)
            iCount = stream.Read(byteUShort, 0, byteUShort.Length);
            if (iCount != byteUShort.Length)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_UncompressedLength = byteUShort.ConvertToUInt16();
            }

            byte[] byteCrc = new byte[16];

            // 15 ~ 31: 原评论信息的CRC校验字节(读取)
            iCount = stream.Read(byteCrc, 0, byteCrc.Length);
            if (iCount != byteCrc.Length)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_Crc = byteCrc;
            }

            xyzCommentReader.m_ValueOffset = (ulong)stream.Position;

            byte[] byteValue = new byte[xyzCommentReader.CompressedLength];

            // 31 ~ ..: 评论信息被压缩后的字节(读取)
            iCount = stream.Read(byteValue, 0, byteValue.Length);
            if (iCount != byteValue.Length)
            {
                return(null);
            }

            byte[] byteData = null;
            switch (xyzCommentReader.CompressionType)
            {
            case XyzCommentCompression.None:

                byteData = byteValue;
                break;

            case XyzCommentCompression.Deflate:

                break;

            case XyzCommentCompression.Bzip2:

                break;

            case XyzCommentCompression.Lzw:

                break;

            case XyzCommentCompression.Ppmd:

                break;

            default:

                return(null);
            }

            if (byteData == null)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_CommentValue = XyzConstants.UTF8.GetString(byteData);
            }

            return(xyzCommentReader);
        }
        private static XyzHeadReader ReadXyzHeadVer01(System.IO.Stream stream)
        {
            XyzHeadReader xyzCommentReader = new XyzHeadReader();

            xyzCommentReader.m_HeadTag     = XyzHeadReader.XYZ_HEAD_TAG;
            xyzCommentReader.m_HeadVersion = XyzHeadVersion.XyzHeadVer01;

            // 04 ~ 05: XYZ文件内实体名称的加密方式 ( 0 为无加密, 1... 为其它加密方式 )(读取)
            int iEncryptType = stream.ReadByte();

            if (iEncryptType > (int)XyzHeadEncrypt.MaxValue)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_EncryptType = (XyzHeadEncrypt)iEncryptType;
            }

            switch (xyzCommentReader.EncryptType)
            {
            case XyzHeadEncrypt.None:

                break;

            case XyzHeadEncrypt.Aes:

                break;

            case XyzHeadEncrypt.Des:

                break;

            case XyzHeadEncrypt.RC2:

                break;

            case XyzHeadEncrypt.Rijndael:

                break;

            case XyzHeadEncrypt.TripleDes:

                break;

            default:

                return(null);
            }

            byte[] byteULong = new byte[8];

            // 05 ~ 13: 主评论信息的文件偏移(读取)
            int iCount = stream.Read(byteULong, 0, byteULong.Length);

            if (iCount != byteULong.Length)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_CommentOffset = byteULong.ConvertToUInt64();
            }

            // 13 ~ 21: 第一个XYZ文件内实体的文件偏移(读取)
            iCount = stream.Read(byteULong, 0, byteULong.Length);
            if (iCount != byteULong.Length)
            {
                return(null);
            }
            else
            {
                xyzCommentReader.m_FirstEntryOffset = byteULong.ConvertToUInt64();
            }


            if (xyzCommentReader.CommentOffset == 0)
            {
                return(xyzCommentReader);
            }
            else if ((ulong)stream.Length < xyzCommentReader.CommentOffset)
            {
                return(null);
            }
            else
            {
                stream.Seek((long)xyzCommentReader.CommentOffset, System.IO.SeekOrigin.Begin);

                // 13 ~ ..: 其它XYZ文件内实体或评论信息的数据(读取)
                xyzCommentReader.m_CommentReader = XyzCommentReader.GetXyzComment(stream);
            }

            return(xyzCommentReader);
        }