Exemple #1
0
        private static void WriteBlocksInternal(List <CompressedBlock> blocksSorted, BinaryWriter bw)
        {
            foreach (var compressedBlock in blocksSorted)
            {
                var cbh = new CIPHeader();
                cbh.Magic = 0x55441122;
                cbh.CPos  = compressedBlock.CPos;
                cbh.USize = compressedBlock.DataRaw.Length;
                cbh.CSize = 24 + compressedBlock.DataComp.Length;
                cbh.UPos  = compressedBlock.UPos;

                BinaryHelpers.WriteStruct(bw, cbh);
                bw.Write(compressedBlock.DataComp);
            }
        }
Exemple #2
0
        /// <summary>
        /// Writes a compressed-in-place file. Compresses the input data as blocks,
        /// then writes a header followed by the blocks.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="data"></param>
        /// <param name="blockSize"></param>
        public static void WriteBlockFile(Stream stream, IEnumerable <byte> data, int blockSize = 32768)
        {
            if ((blockSize & (blockSize - 1)) != 0)
            {
                throw new ArgumentException("blockSize must be a power of two!");
            }

            var blocksSorted = BlockCompress(data, blockSize);

            using (var bw = new BinaryWriter(stream))
            {
                var header = new CIPFileHeader();
                header.Magic   = 0x66113388;
                header.CSize   = blocksSorted.Sum(b => b.DataComp.Length + 24);
                header.USize   = blocksSorted.Sum(b => b.DataRaw.Length);
                header.Unknown = 0;

                BinaryHelpers.WriteStruct(bw, header);

                WriteBlocksInternal(blocksSorted, bw);
            }
        }