Пример #1
0
        // write .ecc from .cod
        public void EncodeECC(byte[] bytes, byte[] header, string filePath)
        {
            filePath = Path.ChangeExtension(filePath, FileController.ECC_EXTENSION);

            Ecc  a    = new Ecc();
            byte crc8 = a.Crc(header);

            byte[] crcHeader = new byte[3] {
                header[0], header[1], crc8
            };

            BitArray hamming = a.Hamming(bytes);                         // hamming bits
            BitArray head    = new BitArray(crcHeader);                  // header bits
            BitArray bits8   = new BitArray(8);                          // aux to perform bit to byte

            int      tam       = (crcHeader.Length * 8) + hamming.Count; // total number of bits
            BitArray eccResult = new BitArray(tam);

            int index = 0;

            for (int i = 0; i < head.Length; i++)               // add header to eccResult BitArray
            {
                eccResult[index++] = head[i];
            }
            for (int i = index, j = 0; i < tam; i++, j++)        // add hamming codewords to eccResult BitArray
            {
                eccResult[index++] = hamming[j];
            }

            byte[] eccBytes = bitToByte(eccResult, tam);

            File.WriteAllBytes(filePath, eccBytes);
        }
Пример #2
0
        // decode .ecc, check crc, write .cod
        public void DecodeECC(string filePath, byte[] bytes)
        {
            filePath = Path.ChangeExtension(filePath, FileController.COMPRESSING_EXTENSION);
            // first need to generate .cod from ecc

            Ecc a = new Ecc();

            byte[] header = new byte[2] {
                bytes[0], bytes[1]
            };
            byte crc8 = a.Crc(header);

            if (!crc8.Equals(bytes[2]))                        // check crc
            {
                Console.WriteLine("CRC ERROR: header is incorrect or corrupted! Ending file compressor...");
                return;
            }

            byte[] bytesAux = new byte[bytes.Length - 3];      // remove heading
            Buffer.BlockCopy(bytes, 3, bytesAux, 0, bytesAux.Length);

            BitArray hammingBits = new BitArray(bytesAux);
            BitArray hammingDec  = a.HammingDec(hammingBits);

            byte[] codedBytes = bitToByte(hammingDec, hammingDec.Length);

            byte[] shiftRight = new byte[codedBytes.Length + 3]; //codedBytes
            for (int i = 0; i < codedBytes.Length; i++)
            {
                shiftRight[(i + 3) % shiftRight.Length] = codedBytes[i];
            }

            shiftRight[0] = 2;                                  // Fibonacci number
            shiftRight[1] = 0;                                  // Only for Golomb K
            shiftRight[2] = crc8;
            File.WriteAllBytes(filePath, shiftRight);           // generate .cod
        }