Beispiel #1
0
        //public byte[] RawSerialize(object anything)
        //{
        //    int rawsize = Marshal.SizeOf(anything);
        //    IntPtr buffer = Marshal.AllocHGlobal(rawsize);
        //    Marshal.StructureToPtr(anything, buffer, false);
        //    byte[] rawdata = new byte[rawsize];
        //    Marshal.Copy(buffer, rawdata, 0, rawsize);
        //    Marshal.FreeHGlobal(buffer);
        //    return rawdata;
        //}


        // fill STORED_FILE_HEADER
        public STORED_FILE_HEADER FillHeader(
            string FileName,                                         // Short file name
            string Desc,                                             // Description
            string MD5Origin,                                        // String MD5 of origin file
            string MD5Chunk,                                         // String MD5 of chunk
            uint ChunksQty,                                          // Chunks qty
            uint ChunkNum,                                           // Chunks number
            uint OriginSize,                                         // origin file size
            uint ChunkSize                                           // chunk size
            )
        {
            STORED_FILE_HEADER sfh = new STORED_FILE_HEADER();

            sfh.FileName    = FileName;
            sfh.Description = Desc;
            sfh.MD5Origin   = MD5Origin;
            sfh.MD5Chunk    = MD5Chunk;
            sfh.ChunksQty   = ChunksQty;
            sfh.ChunkNum    = ChunkNum;
            sfh.OriginSize  = OriginSize;
            sfh.ChunkSize   = ChunkSize;

            sfh.cb = (uint)Marshal.SizeOf(sfh);  // size of struct

            return(sfh);
        }
Beispiel #2
0
        // @return true if success Add header to file
        public bool WriteFileWithHeader(string sFullPath, byte[] fileBytes, STORED_FILE_HEADER header)
        {
            try
            {
                // create byte arrays with header and file.
                Helper h  = new Helper();
                byte[] bs = h.RawSerialize(header);

                FileInfo fi = new FileInfo(sFullPath);
                fi.Delete();

                FileStream fsSource = new FileStream(sFullPath, FileMode.Append);
                fsSource.Write(bs, 0, bs.Length);
                fsSource.Write(fileBytes, 0, fileBytes.Length);

                fsSource.Close();

                return(true);
            }
            catch (Exception e)
            {
                Logger.Log.Error(e.ToString());
                return(false);
            }
        }
Beispiel #3
0
        // @return true if success Add header to file
        public bool AddHeaderToFile(STORED_FILE_HEADER header, String sFullPath)
        {
            try
            {
                // create byte arrays with header and file.
                Helper h         = new Helper();
                byte[] bs        = h.RawSerialize(header);
                byte[] fileBytes = File.ReadAllBytes(sFullPath);

                // rename file (add ".old" extention)
                ReserveFile(sFullPath);

                FileStream fsSource = new FileStream(sFullPath, FileMode.Append);
                fsSource.Write(bs, 0, bs.Length);
                fsSource.Write(fileBytes, 0, fileBytes.Length);

                fsSource.Close();

                DeleteReserveFile(sFullPath); // Delete .old file

                return(true);
            }
            catch (Exception e)
            {
                Logger.Log.Error(e.ToString());
                return(false);
            }
        }
Beispiel #4
0
        private void CreateNewStorageList()
        {
            DirectoryInfo di = new DirectoryInfo(config.StorageFolder);

            if (di.Exists)
            {
                FileManipulator fm = new FileManipulator();
                foreach (var f in di.GetFiles())
                {
                    try
                    {
                        // get header from file
                        STORED_FILE_HEADER sfh = fm.GetHeaderFromFile(f.FullName);

                        // fill StorageItem from header
                        string      originname = f.Name.Substring(0, f.Name.IndexOf(".part") - 5); // if string not found file name ignored
                        StorageItem si         = new StorageItem(sfh.FileName, originname, sfh.Description, sfh.OriginSize, (uint)sfh.ChunksQty, (uint)sfh.ChunkNum, sfh.MD5Chunk, sfh.MD5Origin);

                        // add Storage item into List
                        Vault.StorageList.Add(si);
                    }
                    catch (Exception e)
                    {
                        Logger.Log.Error("Error in StoredList creation process.");
                        Logger.Log.Error(e.Message);
                        //throw;
                    }
                }

                // Serialize StorageList
                XmlSerializer formatter = new XmlSerializer(typeof(List <StorageItem>));
                using (FileStream fs = new FileStream(config.StorageFolder + "List.xml", FileMode.OpenOrCreate))
                {
                    formatter.Serialize(fs, Vault.StorageList);
                }
            }
            else
            {
                di.Create(); // just create empty StoredFolder
            }
        }
Beispiel #5
0
        private void btnStartProcess_Click(object sender, EventArgs e)
        {
            if (isFileSelected && user.IsSet)
            {
                STORED_FILE_HEADER sh = new STORED_FILE_HEADER();
                sh.Description = tbFileDescription.Text;
                Helper          helper = new Helper();
                FileManipulator fm     = new FileManipulator();

                // pack file
                Directory.CreateDirectory(config.TempFolder);
                var    fileInputName = file.GetFileName();
                byte[] buffer        = File.ReadAllBytes(fileInputName);

                FileInfo fi = new FileInfo(fileInputName);
                sh.cb         = (uint)Marshal.SizeOf(sh); // header size
                sh.OriginSize = (ulong)fi.Length;
                sh.MD5Origin  = new Helper().GetFileMD5(fileInputName);

                var fileOutputName = config.TempFolder + "\\" + Path.GetFileName(file.GetFileName());
                using (var file = File.Open(fileOutputName, FileMode.Create))
                    using (var stream = new DeflateStream(file, CompressionMode.Compress))
                        using (var writer = new BinaryWriter(stream))
                        {
                            writer.Write(buffer);
                        }

                // encrypt file
                byte[] buff = File.ReadAllBytes(fileOutputName);
//                var fileOutputNameEncrypted = config.TempFolder + "\\" + Path.GetFileName(file.GetFileName()) + @".enc";
//                new AESEnDecryption().BinarySaveObjectWithAes(buff, fileOutputNameEncrypted, user.GetName(), user.GetPassword());
                new AESEnDecryption().BinarySaveObjectWithAes(buff, fileOutputName, user.GetName(), user.GetPassword());

                // split file
//                fm.SplitFile(fileOutputNameEncrypted, config.SendFolder, config.Chunks, sh);
                fm.SplitFile(fileOutputName, config.SendFolder, config.Chunks, sh);

                // @TODO Add SendList filling
                DirectoryInfo di = new DirectoryInfo(config.SendFolder);
            }
        }
Beispiel #6
0
        // @return STRO
        public STORED_FILE_HEADER GetHeaderFromFile(string fileName)
        {
            STORED_FILE_HEADER sfh = new STORED_FILE_HEADER();

            sfh.cb = 0; // if 0 than header not initialized

            Helper helper = new Helper();

            try
            {
                FileStream fs = new FileStream(fileName, FileMode.Open);
                sfh = helper.ReadStruct <STORED_FILE_HEADER>(fs);
            }
            catch (Exception e)
            {
                Logger.Log.Error("READ HEADER FROM: " + fileName);
                Logger.Log.Error(e.Message);
            }

            return(sfh);
        }
Beispiel #7
0
        // @return true if success write all parts and add headers
        //
        public bool SplitFile(string FileInputPath, string FolderOutputPath, uint OutputFiles, STORED_FILE_HEADER originSFH)
        {
            try
            {
                //FileManipulator fm = new FileManipulator();
                Helper h = new Helper();

                // Store the file in a byte array
                Byte[] byteSource = System.IO.File.ReadAllBytes(FileInputPath);
                // Get file info
                FileInfo fiSource = new FileInfo(FileInputPath);
                // Calculate the size of each part
                uint partSize = (uint)Math.Ceiling((double)(fiSource.Length / OutputFiles)) + 1;
                // The offset at which to start reading from the source file
                uint fileOffset = 0;

                // Stores the name of each file part
                string currPartPath;

                // Stores the remaining byte length to write to other files
                uint sizeRemaining = (uint)fiSource.Length;

                // Create output folder if not exist
                Directory.CreateDirectory(FolderOutputPath);

                // Loop through as many times we need to create the partial files
                for (uint i = 0; i < OutputFiles; i++)
                {
                    STORED_FILE_HEADER sfh = originSFH;

                    // fill header for each part
                    sfh.ChunkNum = i;
                    sfh.ChunkNum = OutputFiles;

                    // Calculate the remaining size of the whole file
                    sizeRemaining = (uint)fiSource.Length - (i * partSize);

                    // The size of the last part file might differ because a file doesn't always split equally
                    if (sizeRemaining < partSize)
                    {
                        partSize = sizeRemaining;
                    }

                    // create partbuf for part bytes
                    byte[] partbuf = new byte[partSize];

                    // Store the path of the new part
                    currPartPath = FolderOutputPath + "\\" + fiSource.Name + "." + String.Format(@"{0:D4}", i) + ".part";
                    sfh.FileName = Path.GetFileName(currPartPath);

                    // write part of file into file
                    Buffer.BlockCopy(byteSource, (int)fileOffset, partbuf, 0, (int)partSize);
                    sfh.MD5Chunk = h.GetStringMD5(partbuf);

                    WriteFileWithHeader(currPartPath, partbuf, sfh);

                    fileOffset += partSize;
                }
                return(true);
            }
            catch (Exception e)
            {
                Logger.Log.Error(e.ToString());
                return(false);
            }
        }