Exemplo n.º 1
0
        internal static List <ChecksumType> GetChecksums(byte[] data)
        {
            Adler32Context   adler32ctxData   = new Adler32Context();
            Crc16Context     crc16ctxData     = new Crc16Context();
            Crc32Context     crc32ctxData     = new Crc32Context();
            Crc64Context     crc64ctxData     = new Crc64Context();
            Md5Context       md5ctxData       = new Md5Context();
            Ripemd160Context ripemd160ctxData = new Ripemd160Context();
            Sha1Context      sha1ctxData      = new Sha1Context();
            Sha256Context    sha256ctxData    = new Sha256Context();
            Sha384Context    sha384ctxData    = new Sha384Context();
            Sha512Context    sha512ctxData    = new Sha512Context();
            SpamSumContext   ssctxData        = new SpamSumContext();

            Thread adlerThreadData     = new Thread(updateAdler);
            Thread crc16ThreadData     = new Thread(updateCRC16);
            Thread crc32ThreadData     = new Thread(updateCRC32);
            Thread crc64ThreadData     = new Thread(updateCRC64);
            Thread md5ThreadData       = new Thread(updateMD5);
            Thread ripemd160ThreadData = new Thread(updateRIPEMD160);
            Thread sha1ThreadData      = new Thread(updateSHA1);
            Thread sha256ThreadData    = new Thread(updateSHA256);
            Thread sha384ThreadData    = new Thread(updateSHA384);
            Thread sha512ThreadData    = new Thread(updateSHA512);
            Thread spamsumThreadData   = new Thread(updateSpamSum);

            adlerPacket     adlerPktData     = new adlerPacket();
            crc16Packet     crc16PktData     = new crc16Packet();
            crc32Packet     crc32PktData     = new crc32Packet();
            crc64Packet     crc64PktData     = new crc64Packet();
            md5Packet       md5PktData       = new md5Packet();
            ripemd160Packet ripemd160PktData = new ripemd160Packet();
            sha1Packet      sha1PktData      = new sha1Packet();
            sha256Packet    sha256PktData    = new sha256Packet();
            sha384Packet    sha384PktData    = new sha384Packet();
            sha512Packet    sha512PktData    = new sha512Packet();
            spamsumPacket   spamsumPktData   = new spamsumPacket();

            adler32ctxData.Init();
            adlerPktData.context = adler32ctxData;
            crc16ctxData.Init();
            crc16PktData.context = crc16ctxData;
            crc32ctxData.Init();
            crc32PktData.context = crc32ctxData;
            crc64ctxData.Init();
            crc64PktData.context = crc64ctxData;
            md5ctxData.Init();
            md5PktData.context = md5ctxData;
            ripemd160ctxData.Init();
            ripemd160PktData.context = ripemd160ctxData;
            sha1ctxData.Init();
            sha1PktData.context = sha1ctxData;
            sha256ctxData.Init();
            sha256PktData.context = sha256ctxData;
            sha384ctxData.Init();
            sha384PktData.context = sha384ctxData;
            sha512ctxData.Init();
            sha512PktData.context = sha512ctxData;
            ssctxData.Init();
            spamsumPktData.context = ssctxData;

            adlerPktData.data = data;
            adlerThreadData.Start(adlerPktData);
            crc16PktData.data = data;
            crc16ThreadData.Start(crc16PktData);
            crc32PktData.data = data;
            crc32ThreadData.Start(crc32PktData);
            crc64PktData.data = data;
            crc64ThreadData.Start(crc64PktData);
            md5PktData.data = data;
            md5ThreadData.Start(md5PktData);
            ripemd160PktData.data = data;
            ripemd160ThreadData.Start(ripemd160PktData);
            sha1PktData.data = data;
            sha1ThreadData.Start(sha1PktData);
            sha256PktData.data = data;
            sha256ThreadData.Start(sha256PktData);
            sha384PktData.data = data;
            sha384ThreadData.Start(sha384PktData);
            sha512PktData.data = data;
            sha512ThreadData.Start(sha512PktData);
            spamsumPktData.data = data;
            spamsumThreadData.Start(spamsumPktData);

            while (adlerThreadData.IsAlive || crc16ThreadData.IsAlive || crc32ThreadData.IsAlive ||
                   crc64ThreadData.IsAlive || md5ThreadData.IsAlive || ripemd160ThreadData.IsAlive ||
                   sha1ThreadData.IsAlive || sha256ThreadData.IsAlive || sha384ThreadData.IsAlive ||
                   sha512ThreadData.IsAlive || spamsumThreadData.IsAlive)
            {
            }

            List <ChecksumType> dataChecksums = new List <ChecksumType>();

            ChecksumType chk = new ChecksumType {
                type = ChecksumTypeType.adler32, Value = adler32ctxData.End()
            };

            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.crc16, Value = crc16ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.crc32, Value = crc32ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.crc64, Value = crc64ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.md5, Value = md5ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.ripemd160, Value = ripemd160ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.sha1, Value = sha1ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.sha256, Value = sha256ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.sha384, Value = sha384ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.sha512, Value = sha512ctxData.End()
            };
            dataChecksums.Add(chk);

            chk = new ChecksumType {
                type = ChecksumTypeType.spamsum, Value = ssctxData.End()
            };
            dataChecksums.Add(chk);

            return(dataChecksums);
        }
Exemplo n.º 2
0
        internal Checksum()
        {
            adler32ctx   = new Adler32Context();
            crc16ctx     = new Crc16Context();
            crc32ctx     = new Crc32Context();
            crc64ctx     = new Crc64Context();
            md5ctx       = new Md5Context();
            ripemd160ctx = new Ripemd160Context();
            sha1ctx      = new Sha1Context();
            sha256ctx    = new Sha256Context();
            sha384ctx    = new Sha384Context();
            sha512ctx    = new Sha512Context();
            ssctx        = new SpamSumContext();

            adlerThread     = new Thread(updateAdler);
            crc16Thread     = new Thread(updateCRC16);
            crc32Thread     = new Thread(updateCRC32);
            crc64Thread     = new Thread(updateCRC64);
            md5Thread       = new Thread(updateMD5);
            ripemd160Thread = new Thread(updateRIPEMD160);
            sha1Thread      = new Thread(updateSHA1);
            sha256Thread    = new Thread(updateSHA256);
            sha384Thread    = new Thread(updateSHA384);
            sha512Thread    = new Thread(updateSHA512);
            spamsumThread   = new Thread(updateSpamSum);

            adlerPkt     = new adlerPacket();
            crc16Pkt     = new crc16Packet();
            crc32Pkt     = new crc32Packet();
            crc64Pkt     = new crc64Packet();
            md5Pkt       = new md5Packet();
            ripemd160Pkt = new ripemd160Packet();
            sha1Pkt      = new sha1Packet();
            sha256Pkt    = new sha256Packet();
            sha384Pkt    = new sha384Packet();
            sha512Pkt    = new sha512Packet();
            spamsumPkt   = new spamsumPacket();

            adler32ctx.Init();
            adlerPkt.context = adler32ctx;
            crc16ctx.Init();
            crc16Pkt.context = crc16ctx;
            crc32ctx.Init();
            crc32Pkt.context = crc32ctx;
            crc64ctx.Init();
            crc64Pkt.context = crc64ctx;
            md5ctx.Init();
            md5Pkt.context = md5ctx;
            ripemd160ctx.Init();
            ripemd160Pkt.context = ripemd160ctx;
            sha1ctx.Init();
            sha1Pkt.context = sha1ctx;
            sha256ctx.Init();
            sha256Pkt.context = sha256ctx;
            sha384ctx.Init();
            sha384Pkt.context = sha384ctx;
            sha512ctx.Init();
            sha512Pkt.context = sha512ctx;
            ssctx.Init();
            spamsumPkt.context = ssctx;
        }
Exemplo n.º 3
0
        public static void CompressFiles()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Context.DbInfo.Developer))
                {
                    Failed?.Invoke("Developer cannot be empty");
                    return;
                }

                if (string.IsNullOrWhiteSpace(Context.DbInfo.Product))
                {
                    Failed?.Invoke("Product cannot be empty");
                    return;
                }

                if (string.IsNullOrWhiteSpace(Context.DbInfo.Version))
                {
                    Failed?.Invoke("Version cannot be empty");
                    return;
                }

                string destinationFolder = "";
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Developer);
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Product);
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Version);
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Languages))
                {
                    destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Languages);
                }
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Architecture))
                {
                    destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Architecture);
                }
                if (Context.DbInfo.Oem)
                {
                    destinationFolder = Path.Combine(destinationFolder, "oem");
                }
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Machine))
                {
                    destinationFolder = Path.Combine(destinationFolder, "for " + Context.DbInfo.Machine);
                }

                string destinationFile = "";
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Format))
                {
                    destinationFile += "[" + Context.DbInfo.Format + "]";
                }
                if (Context.DbInfo.Files)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "files";
                }

                if (Context.DbInfo.Netinstall)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "netinstall";
                }

                if (Context.DbInfo.Source)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "source";
                }

                if (Context.DbInfo.Update)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "update";
                }

                if (Context.DbInfo.Upgrade)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "upgrade";
                }

                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Description))
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += Context.DbInfo.Description;
                }
                else if (destinationFile == "")
                {
                    destinationFile = "archive";
                }

                string destination = Path.Combine(destinationFolder, destinationFile) + ".zip";

                Md5Context md5 = new Md5Context();
                md5.Init();
                byte[] tmp;
                string mdid = md5.Data(Encoding.UTF8.GetBytes(destination), out tmp);
                Console.WriteLine("MDID: {0}", mdid);

                if (dbCore.DbOps.ExistsOs(mdid))
                {
                    if (File.Exists(destination))
                    {
                        Failed?.Invoke("OS already exists.");
                        return;
                    }

                    Failed?.Invoke("OS already exists in the database but not in the repository, check for inconsistencies.");
                    return;
                }

                if (File.Exists(destination))
                {
                    Failed?.Invoke("OS already exists in the repository but not in the database, check for inconsistencies.");
                    return;
                }

                Context.DbInfo.Mdid = mdid;

                string filesPath;

                if (!string.IsNullOrEmpty(Context.TmpFolder) && Directory.Exists(Context.TmpFolder))
                {
                    filesPath = Context.TmpFolder;
                }
                else
                {
                    filesPath = Context.Path;
                }

                string extension = null;

                switch (Settings.Current.CompressionAlgorithm)
                {
                case AlgoEnum.GZip:
                    extension = ".gz";
                    break;

                case AlgoEnum.BZip2:
                    extension = ".bz2";
                    break;

                case AlgoEnum.LZMA:
                    extension = ".lzma";
                    break;

                case AlgoEnum.LZip:
                    extension = ".lz";
                    break;
                }

                long totalSize = 0, currentSize = 0;
                foreach (KeyValuePair <string, DbOsFile> file in Context.Hashes)
                {
                    totalSize += file.Value.Length;
                }

                #if DEBUG
                stopwatch.Restart();
                #endif
                foreach (KeyValuePair <string, DbOsFile> file in Context.Hashes)
                {
                    UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                    destinationFolder = Path.Combine(Settings.Current.RepositoryPath, file.Value.Sha256[0].ToString(),
                                                     file.Value.Sha256[1].ToString(), file.Value.Sha256[2].ToString(),
                                                     file.Value.Sha256[3].ToString(), file.Value.Sha256[4].ToString());
                    Directory.CreateDirectory(destinationFolder);

                    destinationFile = Path.Combine(destinationFolder, file.Value.Sha256 + extension);

                    if (!File.Exists(destinationFile))
                    {
                        FileStream inFs = new FileStream(Path.Combine(filesPath, file.Value.Path), FileMode.Open,
                                                         FileAccess.Read);
                        FileStream outFs   = new FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write);
                        Stream     zStream = null;

                        switch (Settings.Current.CompressionAlgorithm)
                        {
                        case AlgoEnum.GZip:
                            zStream = new GZipStream(outFs, CompressionMode.Compress,
                                                     CompressionLevel.BestCompression);
                            break;

                        case AlgoEnum.BZip2:
                            zStream = new BZip2Stream(outFs, CompressionMode.Compress);
                            break;

                        case AlgoEnum.LZMA:
                            zStream = new LzmaStream(new LzmaEncoderProperties(), false, outFs);
                            outFs.Write(((LzmaStream)zStream).Properties, 0,
                                        ((LzmaStream)zStream).Properties.Length);
                            outFs.Write(BitConverter.GetBytes(inFs.Length), 0, 8);
                            break;

                        case AlgoEnum.LZip:
                            zStream = new LZipStream(outFs, CompressionMode.Compress);
                            break;
                        }

                        byte[] buffer = new byte[BUFFER_SIZE];

                        while (inFs.Position + BUFFER_SIZE <= inFs.Length)
                        {
                            UpdateProgress2?.Invoke($"{inFs.Position / (double)inFs.Length:P}",
                                                    $"{inFs.Position} / {inFs.Length} bytes", inFs.Position,
                                                    inFs.Length);
                            UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                            inFs.Read(buffer, 0, buffer.Length);
                            zStream.Write(buffer, 0, buffer.Length);
                            currentSize += buffer.Length;
                        }

                        buffer = new byte[inFs.Length - inFs.Position];
                        UpdateProgress2?.Invoke($"{inFs.Position / (double)inFs.Length:P}",
                                                $"{inFs.Position} / {inFs.Length} bytes", inFs.Position, inFs.Length);
                        UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                        inFs.Read(buffer, 0, buffer.Length);
                        zStream.Write(buffer, 0, buffer.Length);
                        currentSize += buffer.Length;

                        UpdateProgress2?.Invoke($"{inFs.Length / (double)inFs.Length:P}", "Finishing...", inFs.Length,
                                                inFs.Length);

                        inFs.Close();
                        zStream.Close();
                        outFs.Dispose();
                    }
                    else
                    {
                        currentSize += file.Value.Length;
                    }
                }
                #if DEBUG
                stopwatch.Stop();
                Console.WriteLine("Core.CompressFiles(): Took {0} seconds to compress files",
                                  stopwatch.Elapsed.TotalSeconds);
                #endif

                if (Context.Metadata != null)
                {
                    MemoryStream  xms = new MemoryStream();
                    XmlSerializer xs  = new XmlSerializer(typeof(CICMMetadataType));
                    xs.Serialize(xms, Context.Metadata);
                    xms.Position = 0;

                    JsonSerializer js = new JsonSerializer
                    {
                        Formatting        = Formatting.Indented,
                        NullValueHandling = NullValueHandling.Ignore
                    };
                    MemoryStream jms = new MemoryStream();
                    StreamWriter sw  = new StreamWriter(jms, Encoding.UTF8, 1048576, true);
                    js.Serialize(sw, Context.Metadata, typeof(CICMMetadataType));
                    sw.Close();
                    jms.Position = 0;

                    destinationFolder = Path.Combine(Settings.Current.RepositoryPath, "metadata", mdid[0].ToString(),
                                                     mdid[1].ToString(), mdid[2].ToString(), mdid[3].ToString(),
                                                     mdid[4].ToString());
                    Directory.CreateDirectory(destinationFolder);

                    FileStream xfs = new FileStream(Path.Combine(destinationFolder, mdid + ".xml"), FileMode.CreateNew,
                                                    FileAccess.Write);
                    xms.CopyTo(xfs);
                    xfs.Close();
                    FileStream jfs = new FileStream(Path.Combine(destinationFolder, mdid + ".json"), FileMode.CreateNew,
                                                    FileAccess.Write);
                    jms.CopyTo(jfs);
                    jfs.Close();

                    xms.Position = 0;
                    jms.Position = 0;
                }

                FinishedWithText?.Invoke($"Correctly added operating system with MDID {mdid}");
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    throw;
                }

                Failed?.Invoke($"Exception {ex.Message}\n{ex.InnerException}");
                #if DEBUG
                Console.WriteLine("Exception {0}\n{1}", ex.Message, ex.InnerException);
                #endif
            }
        }