Пример #1
0
 public static void WriteULong(ulong val, ByteBufferRef buf, int offset)
 {
     Contract.Requires(offset >= 0);
     Contract.Requires(buf.Length >= offset + sizeof(long));
     for (var i = 0; i < sizeof(long); ++i)
     {
         Contract.Assert(offset + i < buf.Length);
         buf.Set(offset + i, (byte)((val >> (8 * i)) & 0xff));
     }
 }
Пример #2
0
        // like strncpy, but return the number of bytes copied.
        // It never add 0 to terminate the string
        public static int CopyString(ref ByteBufferRef dst, int cursor, ByteBufferRef src)
        {
            Contract.Requires(cursor >= 0 && cursor < dst.Length);

            var i = 0;
            while (i + cursor < dst.Length && i < src.Length && src.Get(i) != 0)
            {
                Contract.Assert(i < src.Length);
                dst.Set(i + cursor, src.Get(i));
                ++i;
            }
            return i;
        }
Пример #3
0
        // like strncpy, but return the number of bytes copied.
        // It never add 0 to terminate the string
        public static int CopyString(ref ByteBufferRef dst, int cursor, ByteBufferRef src)
        {
            Contract.Requires(cursor >= 0 && cursor < dst.Length);

            var i = 0;

            while (i + cursor < dst.Length && i < src.Length && src.Get(i) != 0)
            {
                Contract.Assert(i < src.Length);
                dst.Set(i + cursor, src.Get(i));
                ++i;
            }
            return(i);
        }
Пример #4
0
        private int Read(Process process, ByteBufferRef buffer, bool is_string)
        {
            var bytesLeft = is_string ? buffer.Length - 1 : buffer.Length;
            var bytesRead = 0;

            var src = _value;

            while (bytesLeft > 0)
            {
                var region = process.Space.Find(src);

                // Invalid mapping
                if (region == null || region.IsFixed)
                    break;

                var off = Arch.ArchDefinition.PageOffset(src.ToUInt32());
                var virtualAddr = process.Space.UserToVirt(new UserPtr(src));

                if (virtualAddr == Pointer.Zero)
                {
                    uint permission;

                    Pager.HandlePageFault(process, MemoryRegion.FAULT_MASK, _value, Pointer.Zero, out virtualAddr, out permission);

                    if (virtualAddr == Pointer.Zero)
                        break;
                }

                var virtual_page = Arch.ArchDefinition.PageIndex(virtualAddr.ToUInt32());
                var page_buf = new ByteBufferRef(new IntPtr(virtual_page), Arch.ArchDefinition.PageSize);

                var b = Arch.ArchDefinition.PageSize - off;
                var bytesTobeCopied = b > bytesLeft ? bytesLeft : b;

                var src_buf = page_buf.Slice(off, bytesTobeCopied);

                if (is_string)
                {
                    var res = Util.CopyString(ref buffer, bytesRead, src_buf);
                    bytesRead += res;

                    if (res < bytesTobeCopied)
                    {
                        // We're done.
                        break;
                    }
                }
                else
                {
                    for (var i = 0; i < bytesTobeCopied; ++i)
                    {
                        buffer.Set(i + bytesRead, src_buf.Get(i));
                    }
                    bytesRead += bytesTobeCopied;
                }

                bytesLeft -= bytesTobeCopied;
                src += bytesTobeCopied;
            }

            if (is_string)
            {
                buffer.Set(bytesRead, 0);
            }
            return bytesRead;
        }
Пример #5
0
        private int Read(Process process, ByteBufferRef buffer, bool is_string)
        {
            var bytesLeft = is_string ? buffer.Length - 1 : buffer.Length;
            var bytesRead = 0;

            var src = _value;

            while (bytesLeft > 0)
            {
                var region = process.Space.Find(src);

                // Invalid mapping
                if (region == null || region.IsFixed)
                {
                    break;
                }

                var off         = Arch.ArchDefinition.PageOffset(src.ToUInt32());
                var virtualAddr = process.Space.UserToVirt(new UserPtr(src));

                if (virtualAddr == Pointer.Zero)
                {
                    uint permission;

                    Pager.HandlePageFault(process, MemoryRegion.FAULT_MASK, _value, Pointer.Zero, out virtualAddr, out permission);

                    if (virtualAddr == Pointer.Zero)
                    {
                        break;
                    }
                }

                var virtual_page = Arch.ArchDefinition.PageIndex(virtualAddr.ToUInt32());
                var page_buf     = new ByteBufferRef(new IntPtr(virtual_page), Arch.ArchDefinition.PageSize);

                var b = Arch.ArchDefinition.PageSize - off;
                var bytesTobeCopied = b > bytesLeft ? bytesLeft : b;

                var src_buf = page_buf.Slice(off, bytesTobeCopied);

                if (is_string)
                {
                    var res = Util.CopyString(ref buffer, bytesRead, src_buf);
                    bytesRead += res;

                    if (res < bytesTobeCopied)
                    {
                        // We're done.
                        break;
                    }
                }
                else
                {
                    for (var i = 0; i < bytesTobeCopied; ++i)
                    {
                        buffer.Set(i + bytesRead, src_buf.Get(i));
                    }
                    bytesRead += bytesTobeCopied;
                }

                bytesLeft -= bytesTobeCopied;
                src       += bytesTobeCopied;
            }

            if (is_string)
            {
                buffer.Set(bytesRead, 0);
            }
            return(bytesRead);
        }
Пример #6
0
 public static void WriteULong(ulong val, ByteBufferRef buf, int offset)
 {
     Contract.Requires(offset >= 0);
     Contract.Requires(buf.Length >= offset + sizeof(long));
     for (var i = 0; i < sizeof(long); ++i)
     {
         Contract.Assert(offset + i < buf.Length);
         buf.Set(offset + i, (byte)((val >> (8 * i)) & 0xff));
     }
 }