コード例 #1
0
        /// <summary>
        /// This method is used to chunk the file data.
        /// </summary>
        /// <returns>A list of LeafNodeObjectData.</returns>
        public override List <LeafNodeObject> Chunking()
        {
            int maxChunkSize           = 1 * 1024 * 1024;
            List <LeafNodeObject> list = new List <LeafNodeObject>();

            LeafNodeObject.IntermediateNodeObjectBuilder builder = new LeafNodeObject.IntermediateNodeObjectBuilder();
            int chunkStart = 0;

            if (this.FileContent.Length <= maxChunkSize)
            {
                list.Add(builder.Build(this.FileContent, this.GetSignature(this.FileContent)));

                return(list);
            }

            while (chunkStart < this.FileContent.Length)
            {
                int    chunkLength = chunkStart + maxChunkSize >= this.FileContent.Length ? this.FileContent.Length - chunkStart : maxChunkSize;
                byte[] temp        = AdapterHelper.GetBytes(this.FileContent, chunkStart, chunkLength);
                list.Add(builder.Build(temp, this.GetSignature(temp)));
                chunkStart += chunkLength;
            }

            return(list);
        }
コード例 #2
0
        /// <summary>
        /// This method is used to chunk the file data.
        /// </summary>
        /// <returns>A list of LeafNodeObjectData.</returns>
        public override List <LeafNodeObject> Chunking()
        {
            List <LeafNodeObject> list = new List <LeafNodeObject>();

            LeafNodeObject.IntermediateNodeObjectBuilder builder = new LeafNodeObject.IntermediateNodeObjectBuilder();

            int index = 0;

            while (ZipHeader.IsFileHeader(this.FileContent, index))
            {
                byte[] dataFileSignatureBytes;
                byte[] header         = this.AnalyzeFileHeader(this.FileContent, index, out dataFileSignatureBytes);
                int    headerLength   = header.Length;
                int    compressedSize = (int)this.GetCompressedSize(dataFileSignatureBytes);

                if (headerLength + compressedSize <= 4096)
                {
                    list.Add(builder.Build(AdapterHelper.GetBytes(this.FileContent, index, headerLength + compressedSize), this.GetSingleChunkSignature(header, dataFileSignatureBytes)));
                    index += headerLength += compressedSize;
                }
                else
                {
                    list.Add(builder.Build(header, this.GetSHA1Signature(header)));
                    index += headerLength;

                    byte[] dataFile = AdapterHelper.GetBytes(this.FileContent, index, compressedSize);

                    if (dataFile.Length <= 1048576)
                    {
                        list.Add(builder.Build(dataFile, this.GetDataFileSignature(dataFileSignatureBytes)));
                    }
                    else
                    {
                        list.AddRange(this.GetSubChunkList(dataFile));
                    }

                    index += compressedSize;
                }
            }

            if (0 == index)
            {
                return(null);
            }

            byte[] final = AdapterHelper.GetBytes(this.FileContent, index, this.FileContent.Length - index);

            if (final.Length <= 1048576)
            {
                list.Add(builder.Build(final, this.GetSHA1Signature(final)));
            }
            else
            {
                // In current, it has no idea about how to compute the signature for final part larger than 1MB.
                throw new NotImplementedException("If the final chunk is larger than 1MB, the signature method is not implemented.");
            }

            return(list);
        }
コード例 #3
0
        /// <summary>
        /// Check the input data is a local file header.
        /// </summary>
        /// <param name="byteArray">The content of a file.</param>
        /// <param name="index">The index where to start.</param>
        /// <returns>True if the input data is a local file header, otherwise false.</returns>
        public static bool IsFileHeader(byte[] byteArray, int index)
        {
            if (AdapterHelper.ByteArrayEquals(LocalFileHeader, AdapterHelper.GetBytes(byteArray, index, 4)))
            {
                return(true);
            }

            return(false);
        }
コード例 #4
0
        /// <summary>
        /// Convert chunk data to LeafNodeObjectData from byte array.
        /// </summary>
        /// <param name="chunkData">A byte array that contains the data.</param>
        /// <returns>A list of LeafNodeObjectData.</returns>
        private List <LeafNodeObject> GetSubChunkList(byte[] chunkData)
        {
            List <LeafNodeObject> subChunkList = new List <LeafNodeObject>();
            int index = 0;

            while (index < chunkData.Length)
            {
                int    length = chunkData.Length - index < 1048576 ? chunkData.Length - index : 1048576;
                byte[] temp   = AdapterHelper.GetBytes(chunkData, index, length);
                subChunkList.Add(new LeafNodeObject.IntermediateNodeObjectBuilder().Build(temp, this.GetSubChunkSignature()));
                index += length;
            }

            return(subChunkList);
        }
コード例 #5
0
        /// <summary>
        /// This method is used to analyze the zip file header.
        /// </summary>
        /// <param name="content">Specify the zip content.</param>
        /// <param name="index">Specify the start position.</param>
        /// <param name="dataFileSignature">Specify the output value for the data file signature.</param>
        /// <returns>Return the data file content.</returns>
        private byte[] AnalyzeFileHeader(byte[] content, int index, out byte[] dataFileSignature)
        {
            int crc32             = BitConverter.ToInt32(content, index + 14);
            int compressedSize    = BitConverter.ToInt32(content, index + 18);
            int uncompressedSize  = BitConverter.ToInt32(content, index + 22);
            int fileNameLength    = BitConverter.ToInt16(content, index + 26);
            int extraFileldLength = BitConverter.ToInt16(content, index + 28);
            int headerLength      = 30 + fileNameLength + extraFileldLength;

            BitWriter writer = new BitWriter(20);

            writer.AppendInit32(crc32, 32);
            writer.AppendUInt64((ulong)compressedSize, 64);
            writer.AppendUInt64((ulong)uncompressedSize, 64);
            dataFileSignature = writer.Bytes;

            return(AdapterHelper.GetBytes(content, index, headerLength));
        }
コード例 #6
0
        /// <summary>
        /// Get a chunk with the input bytes.
        /// </summary>
        /// <param name="chunkStart">The start index of the chunk.</param>
        /// <param name="chunkEnd">The end index of the chunk.</param>
        /// <returns>An IntermediateNodeObject which contains a chunk.</returns>
        private IntermediateNodeObject GetChunk(uint chunkStart, uint chunkEnd)
        {
            if (chunkEnd <= chunkStart || (chunkEnd - chunkStart > this.maxChunkSize) || chunkStart > uint.MaxValue)
            {
                throw new ArgumentOutOfRangeException("chunkStart");
            }

            byte[] temp = AdapterHelper.GetBytes(this.FileContent, (int)chunkStart, (int)(chunkEnd - chunkStart));

            byte[] signatureBytes = null;
            using (RDCSignatureGenerator generator = new RDCSignatureGenerator())
            {
                signatureBytes = generator.ComputeHash(temp);
            }

            SignatureObject signature = new SignatureObject();

            signature.SignatureData = new BinaryItem(signatureBytes);

            return(new IntermediateNodeObject.IntermediateNodeObjectBuilder().Build(temp, signature));
        }