Beispiel #1
0
        public VSAS FormVSAS()
        {
            VSAS_Header header = new VSAS_Header();

            header.algNumContextCompression = 0;
            header.algNumEntropyCompression = 0;
            header.algNumNoiseProtection    = 0;
            header.algNumEncryption         = 0;
            header.filesAmount = BitConverter.GetBytes(filesToCompress.Count)[0];

            return(new VSAS(header, filesToCompress));
        }
Beispiel #2
0
 public VSAS(VSAS_Header header, List <InnerFile> files)
 {
     this.header = header;
     this.files  = files;
 }
Beispiel #3
0
        /// РАСШИФРОВКА ЗДЕСЬ!!! VVV
        private VSAS BytesToVSAS(byte[] file)
        {
            //VSAS archive = new VSAS();
            VSAS_Header      archiveHeader = new VSAS_Header();
            List <InnerFile> innerFiles    = new List <InnerFile>();
            int counter = 4;

            archiveHeader.algNumEntropyCompression = file[counter];
            counter++;
            archiveHeader.algNumContextCompression = file[counter];
            counter++;
            archiveHeader.algNumNoiseProtection = file[counter];
            counter++;
            archiveHeader.algNumEncryption = file[counter];
            counter++;
            archiveHeader.filesAmount = file[counter];
            counter++;
            archiveHeader.tail = file.Skip(counter).Take(7).ToArray();
            counter           += 7;

            for (int i = 0; i < archiveHeader.filesAmount; i++)
            {
                InnerFileHeader innerFileHeader = new InnerFileHeader();
                innerFileHeader.signature = file.Skip(counter).Take(4).ToArray();
                counter += 4;
                innerFileHeader.compressedSize = file.Skip(counter).Take(4).ToArray();
                counter += 4;
                innerFileHeader.uncompressedSize = file.Skip(counter).Take(4).ToArray();
                counter += 4;
                innerFileHeader.fileNameLength = file[counter];
                counter++;
                innerFileHeader.symTableOffset = file.Skip(counter).Take(4).ToArray();
                counter += 4;
                innerFileHeader.fileDataOffset = file.Skip(counter).Take(4).ToArray();
                counter += 4;
                innerFileHeader.fileName = file.Skip(counter).Take(104).ToArray();
                counter += 104;
                innerFileHeader.tail = file.Skip(counter).Take(3).ToArray();
                counter += 3;

                byte[] encryptionHeader = null;
                if (archiveHeader.algNumEncryption == 1)
                {
                    int len = BitConverter.ToInt32(innerFileHeader.symTableOffset, 0);
                    encryptionHeader = file.Skip(counter).Take(len).ToArray();
                    counter         += len;
                }

                byte[] symCodeTable = null;
                if (archiveHeader.algNumContextCompression == 1)
                {
                    int len = BitConverter.ToInt32(innerFileHeader.fileDataOffset, 0);
                    symCodeTable = file.Skip(counter).Take(len).ToArray();
                    counter     += len;
                }


                Dictionary <string, byte> codeSym = new Dictionary <string, byte>();
                // формирование таблицы кодов в виде словаря
                if (archiveHeader.algNumContextCompression == 1)
                {
                    int scCounter = 0;
                    while (scCounter < symCodeTable.Length)
                    {
                        byte sym = symCodeTable.Skip(scCounter).Take(1).ToArray()[0];
                        scCounter++;
                        int len = symCodeTable.Skip(scCounter).Take(1).ToArray()[0];
                        scCounter++;
                        byte[] code = symCodeTable.Skip(scCounter).Take(len).ToArray();
                        scCounter += len;

                        string strcode = "";
                        foreach (byte b in code)
                        {
                            strcode += (char)b;
                        }
                        codeSym.Add(strcode, sym);
                    }
                }

                int    dataSize = 0;
                byte[] data     = null;



                byte[] uncompressedData        = null;
                int    uncompressedDataCounter = 0;
                //Расшифровка Фано РАЗЖАТИЕ :)
                if (archiveHeader.algNumContextCompression == 1)
                {
                    List <byte> uncompressedDataList = new List <byte>();
                    dataSize = BitConverter.ToInt32(innerFileHeader.compressedSize, 0);

                    BitArray bits = new BitArray(file.Skip(counter).Take(dataSize).ToArray());
                    counter += dataSize;

                    string code = "";
                    foreach (bool bit in bits)
                    {
                        if (bit == true)
                        {
                            code += '1';
                        }
                        else
                        {
                            code += '0';
                        }

                        if (codeSym.ContainsKey(code))
                        {
                            uncompressedDataList.Add(codeSym[code]);
                            code = "";
                        }
                    }

                    innerFileHeader.compressedSize = BitConverter.GetBytes(uncompressedDataList.Count);
                    uncompressedData = uncompressedDataList.ToArray();
                }
                else if (archiveHeader.algNumContextCompression == 2)
                {
                    List <byte> newData = new List <byte>();
                    dataSize = BitConverter.ToInt32(innerFileHeader.compressedSize, 0);
                    List <byte> oldData = new List <byte>();
                    oldData.AddRange(file.Skip(counter).Take(dataSize).ToArray());
                    counter += dataSize;

                    for (int c = 0; c < oldData.Count; c++)
                    {
                        if (oldData[c] == (byte)'^')
                        {
                            int  amount = oldData[c + 1];
                            byte b      = oldData[c + 2];
                            for (int c2 = 0; c2 < amount; c2++)
                            {
                                newData.Add(b);
                            }
                            c += 2;
                        }
                        else
                        {
                            newData.Add(oldData[c]);
                        }
                    }

                    data = newData.ToArray();
                }

                if (archiveHeader.algNumEncryption == 1)
                {/// РАСШИФРОВКА ДАННЫХ
                    List <byte> encryptedData = new List <byte>();
                    dataSize = BitConverter.ToInt32(innerFileHeader.compressedSize, 0);
                    foreach (byte blockLen in encryptionHeader)
                    {
                        if (archiveHeader.algNumContextCompression != 1)
                        {
                            encryptedData.AddRange(file.Skip(counter).Take(blockLen).ToArray());
                            counter += blockLen + 5;
                        }
                        else
                        {
                            encryptedData.AddRange(uncompressedData.Skip(uncompressedDataCounter).Take(blockLen).ToArray());
                            uncompressedDataCounter += blockLen + 5;
                        }
                    }

                    data = encryptedData.ToArray();
                }
                else
                {
                    dataSize = BitConverter.ToInt32(innerFileHeader.uncompressedSize, 0);
                    data     = new byte[dataSize];
                    data     = file.Skip(counter).Take(dataSize).ToArray();
                    counter += dataSize;
                }

                innerFiles.Add(new InnerFile(innerFileHeader, encryptionHeader, null, data));
            }

            return(new VSAS(archiveHeader, innerFiles));
        }