Esempio n. 1
0
        public static async Task <Tuple <bool, byte[]> > FillBuffer(IStorageFile file, UploadablePart part)
        {
            try
            {
                if (part.ParentItem.FileNotFound)
                {
                    return(new Tuple <bool, byte[]>(false, null));
                }

                System.Diagnostics.Debug.WriteLine("FillBuffer part=" + part.FilePart);
                using (var inStream = await file.OpenSequentialReadAsync())
                {
                    using (var stream = inStream.AsStreamForRead())
                    //using (var inStream = stream.GetInputStreamAt((ulong) part.Position))
                    {
                        stream.Seek(part.Position, SeekOrigin.Begin);
                        var bytes = new byte[part.Count];

                        stream.Read(bytes, 0, bytes.Length);
                        //using (var reader = new DataReader(inStream))
                        //{
                        //    await reader.LoadAsync((uint) bytes.Length);
                        //    reader.ReadBytes(bytes);
                        //}

                        // encrypting part
                        if (part.ParentItem.Key != null &&
                            part.ParentItem.IV != null)
                        {
                            var key = part.ParentItem.Key;
                            var iv  = part.FilePart.Value == 0 ? part.ParentItem.IV : part.IV;

                            if (iv == null)
                            {
                                return(new Tuple <bool, byte[]>(true, null));
                            }

                            byte[] nextIV;

                            var encryptedBytes = Utils.AesIge(bytes, key.Data, iv.Data, true, out nextIV);
                            bytes = encryptedBytes;

                            var nextPartId = part.FilePart.Value + 1;
                            if (part.ParentItem.Parts.Count > nextPartId)
                            {
                                part.ParentItem.Parts[nextPartId].IV = TLString.FromBigEndianData(nextIV);
                            }
                        }

                        return(new Tuple <bool, byte[]>(true, bytes));
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                Logs.Log.Write(string.Format("FileUtils.FillBuffer bytes=null position={0} count={1} ex={2}", part.Position, part.Count, ex));
                Execute.ShowDebugMessage("FillBuffer FileNotFoundException\n" + ex);
                return(new Tuple <bool, byte[]>(false, null));
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("FillBuffer ex part=" + part.FilePart);
                Logs.Log.Write(string.Format("FileUtils.FillBuffer bytes=null position={0} count={1} ex={2}", part.Position, part.Count, ex));
                Execute.ShowDebugMessage("FillBuffer Exception\n" + ex);
                return(new Tuple <bool, byte[]>(true, null));
            }
        }