private static unsafe bool ExtractManifestContent(System.Deployment.Internal.Isolation.Manifest.ICMS cms, out MemoryStream ms)
 {
     ms = new MemoryStream();
     try
     {
         System.Runtime.InteropServices.ComTypes.IStream stream = cms as System.Runtime.InteropServices.ComTypes.IStream;
         if (stream == null)
         {
             return(false);
         }
         byte[] pv = new byte[0x1000];
         int    cb = 0x1000;
         do
         {
             stream.Read(pv, cb, new IntPtr((void *)&cb));
             ms.Write(pv, 0, cb);
         }while (cb == 0x1000);
         ms.Position = 0L;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Esempio n. 2
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            IntPtr pcbRead = Marshal.AllocHGlobal(IntPtr.Size);

            _baseStream.Read(buffer, buffer.Length, pcbRead);
            int len = Marshal.ReadInt32(pcbRead);

            Marshal.FreeHGlobal(pcbRead);
            return(len);
        }
Esempio n. 3
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     if (offset == 0)
     {
         using (var len = new SafeStructureInOutBuffer <int>())
         {
             _stm.Read(buffer, count, len.DangerousGetHandle());
             return(len.Result);
         }
     }
     else
     {
         using (var len = new SafeStructureInOutBuffer <int>())
         {
             byte[] temp_buffer = new byte[count];
             _stm.Read(temp_buffer, count, len.DangerousGetHandle());
             int read_len = len.Result;
             Buffer.BlockCopy(temp_buffer, 0, buffer, offset, count);
             return(read_len);
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Reads bytes from the stream
        /// </summary>
        /// <param name="buffer">buffer which will receive the bytes</param>
        /// <param name="offset">offset</param>
        /// <param name="count">number of bytes to be read</param>
        /// <returns>Returns the actual number of bytes read from the stream.</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (fileStream == null)
            {
                throw new ObjectDisposedException("fileStream", "storage stream no longer available");
            }
            int      i        = 0;
            object   local    = i;
            GCHandle gCHandle = new GCHandle();

            try
            {
                gCHandle = GCHandle.Alloc(local, GCHandleType.Pinned);
                IntPtr j = gCHandle.AddrOfPinnedObject();
                if (offset != 0)
                {
                    byte[] bs = new byte[count - 1];
                    fileStream.Read(bs, count, j);
                    i = (int)local;
                    Array.Copy(bs, 0, buffer, offset, i);
                }
                else
                {
                    fileStream.Read(buffer, count, j);
                    i = (int)local;
                }
            }
            finally
            {
                if (gCHandle.IsAllocated)
                {
                    gCHandle.Free();
                }
            }
            return(i);
        }
Esempio n. 5
0
        private void CreateProgressISOFile(COMTypes.IStream imagestream)
        {
            COMTypes.STATSTG stat;

            var  bloksize    = _fsres.BlockSize;
            long totalblocks = _fsres.TotalBlocks;

#if DEBUG
            var tm = new Stopwatch();
            tm.Start();
#endif
            imagestream.Stat(out stat, 0x01);

            if (stat.cbSize == totalblocks * bloksize)
            {
                var buff    = new byte[bloksize];
                var bw      = new BinaryWriter(new FileStream(OutputFileName, FileMode.Create));
                var pcbRead = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(uint)));
                var prg     = _fsres.ProgressItems;
                var enm     = prg.GetEnumerator();
                enm.MoveNext();
                var crtitem = enm.Current as IProgressItem;
                try
                {
                    Marshal.WriteInt32(pcbRead, 0);
                    for (float i = 0; i < totalblocks; i++)
                    {
                        imagestream.Read(buff, bloksize, pcbRead);
                        if (Marshal.ReadInt32(pcbRead) != bloksize)
                        {
                            var err = string.Format(
                                "Failed because Marshal.ReadInt32(pcbRead) = {0} != bloksize = {1}",
                                Marshal.ReadInt32(pcbRead), bloksize);
                            Debug.WriteLine(err);
                            throw new ApplicationException(err);
                        }
                        bw.Write(buff);
                        if (crtitem.LastBlock <= i)
                        {
                            if (enm.MoveNext())
                            {
                                crtitem = enm.Current as IProgressItem;
                            }
                            if (Cancel)
                            {
                                return;
                            }
                        }
                        if (Update != null)
                        {
                            Update(crtitem, i / totalblocks);
                        }

                        if (Cancel)
                        {
                            return;
                        }
                    }

                    if (Update != null)
                    {
                        Update(crtitem, 1);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Exception in : CreateProgressISOFile {0}", ex.Message));

                    throw;
                }
                finally
                {
                    bw.Flush();
                    bw.Close();
                    Marshal.FreeHGlobal(pcbRead);
#if DEBUG
                    tm.Stop();
                    Debug.WriteLine(string.Format("Time spent in CreateProgressISOFile: {0} ms",
                                                  tm.Elapsed.TotalMilliseconds.ToString("#,#")));
#endif
                }
            }
            Debug.WriteLine("failed because stat.cbSize({0}) != totalblocks({1}) * bloksize({2}) ", stat.cbSize,
                            totalblocks, bloksize);
        }
Esempio n. 6
0
        /// <summary>
        /// Read from the current position in the stream, and advance the current position the number of bytes read
        /// </summary>
        /// <param name="buffer">The buffer to read the stream into</param>
        /// <param name="offset">The offset of buffer to start reading into, For performance try and always use 0</param>
        /// <param name="count">The number of bytes to read from the stream</param>
        /// <returns>The number of bytes read into buffer/the number of bytes the position in the stream is advanced</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (offset < 0 || offset + count > buffer.Length)
            {
                throw new AccessViolationException("Tried to read into an invalid index");
            }

            // ensure that length gets accessed outside the lock
            long length          = this.Length;
            int  internal_offset = offset;
            int  totalBytesRead  = 0;


            lock (this)
            {
                // ensure to follow Read's behavior of always filling the buffer if we are not reading till the end of the file

                int MaxBytesToRead = count;

                if ((length - m_position) < (long)MaxBytesToRead)
                {
                    MaxBytesToRead = (int)(length - m_position);
                }

                if (MaxBytesToRead == 0)
                {
                    return(0);
                }

                // if reading from offset 0 we can directly read into the buffer
                if (internal_offset == 0)
                {
                    unsafe
                    {
                        // This memory will be fixed due to it being declared in unsafe and allow taking the address
                        UInt32 numBytesRead = 0;

                        m_cachedIstream.Read(buffer, count, new IntPtr(&numBytesRead));

                        //Note that numBytesRead is a UInt32
                        //But since I read only values of size int, I can can safely assume this value
                        //to be less than MaxInt
                        internal_offset += (Int32)numBytesRead;
                        totalBytesRead  += (Int32)numBytesRead;
                    }
                }

                // we might need to read more data, and can't read directly into the buffer
                if (totalBytesRead < MaxBytesToRead)
                {
                    byte[] byteBuffer = new byte[MaxBytesToRead - totalBytesRead];

                    while (totalBytesRead < MaxBytesToRead)
                    {
                        unsafe
                        {
                            // This memory will be fixed due to it being declared in unsafe and allow taking the address
                            UInt32 numBytesRead = 0;

                            m_cachedIstream.Read(byteBuffer, MaxBytesToRead - totalBytesRead, new IntPtr(&numBytesRead));

                            //Note that numBytesRead is a UInt32
                            //But since I read only values of size int, I can can safely assume this value
                            //to be less than MaxInt
                            Array.Copy(byteBuffer, 0, buffer, internal_offset, (Int32)numBytesRead);
                            internal_offset += (Int32)numBytesRead;
                            totalBytesRead  += (Int32)numBytesRead;
                        }
                    }
                }
                m_position += totalBytesRead;
            }
            return(totalBytesRead);
        }