// 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; }
// 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); }
private int Write(Process process, Pointer dst, int length) { var bytesLeft = length; var src = _value; var dst_buf = new ByteBufferRef(dst.ToIntPtr(), length); var cursor = 0; while (bytesLeft > 0) { var region = process.Space.Find(src); // Invalid mapping if (region == null || region.IsFixed) { return(bytesLeft); } var off = Arch.ArchDefinition.PageOffset(src.ToUInt32()); var virtualAddr = process.Space.UserToVirt(new UserPtr(src)); if (virtualAddr == Pointer.Zero) { // Page isn't present, try to bring it in. uint permission; Pager.HandlePageFault(process, MemoryRegion.FAULT_MASK, src, 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 dst_buf_page = page_buf.Slice(off, bytesTobeCopied); for (var i = 0; i < bytesTobeCopied; ++i) { dst_buf_page.Set(i, dst_buf.Get(cursor + i)); } bytesLeft -= bytesTobeCopied; src += bytesTobeCopied; cursor += bytesTobeCopied; } return(bytesLeft); }
public static short ReadShort(ByteBufferRef buf, int offset) { Contract.Requires(offset >= 0); Contract.Requires(buf.Length >= offset + sizeof(ushort)); uint res = 0; for (var i = 0; i < sizeof(short); ++i) { Contract.Assert(offset + i < buf.Length); res += (uint)buf.Get(offset + i) << (8 * i); } return (short)res; }
public static ulong ReadUlong(ByteBufferRef buf, int offset) { Contract.Requires(offset >= 0); Contract.Requires(buf.Length >= offset + sizeof(long)); ulong res = 0; for (var i = 0; i < sizeof(long); ++i) { Contract.Assert(offset + i < buf.Length); res += (ulong)buf.Get(offset + i) << (8 * i); } return(res); }
public static short ReadShort(ByteBufferRef buf, int offset) { Contract.Requires(offset >= 0); Contract.Requires(buf.Length >= offset + sizeof(ushort)); uint res = 0; for (var i = 0; i < sizeof(short); ++i) { Contract.Assert(offset + i < buf.Length); res += (uint)buf.Get(offset + i) << (8 * i); } return((short)res); }
// Duplicated from Input(byte[]) public int Input(ByteBufferRef message_array) { if (message_array.Length == 0) { return(0); } var length = message_array.Length; if (Computed) { Corrupted = true; return(-1); } if (Corrupted) { return(-1); } int i = 0; Contract.Assert(i + length == message_array.Length); while (length > 0 && !Corrupted) { Contract.Assert(i >= 0); Contract.Assert(i + length == message_array.Length); Message_Block[Message_Block_Index++] = message_array.Get(i); Length_Low += 8; if (Length_Low == 0) { Length_High++; if (Length_High == 0) { /* Message is too long */ Corrupted = true; } } if (Message_Block_Index == 64) { SHA1ProcessMessageBlock(); } i++; length--; } return(0); }
public static int Strnlen(ByteBufferRef buf, int max_len) { var i = 0; while (i < max_len && i < buf.Length) { if (buf.Get(i) == 0) { return(i); } ++i; } return(i); }
private static timespec GetTimeSpec(ByteBufferRef buf) { Contract.Requires(buf.Length >= timespec.Size); var r = new timespec(); r.tv_sec = 0; r.tv_nsec = 0; for (var i = 0; i < sizeof(uint); ++i) { Contract.Assume(buf.Length >= timespec.Size); Contract.Assert(i < buf.Length); r.tv_sec += (uint)buf.Get(i) << (8 * i); } for (var i = 0; i < sizeof(uint); ++i) { Contract.Assume(buf.Length >= timespec.Size); Contract.Assert(i + sizeof(uint) < buf.Length); r.tv_nsec += (uint)buf.Get(i + sizeof(uint)) << (8 * i); } return(r); }
private int Write(Process process, Pointer dst, int length) { var bytesLeft = length; var src = _value; var dst_buf = new ByteBufferRef(dst.ToIntPtr(), length); var cursor = 0; while (bytesLeft > 0) { var region = process.Space.Find(src); // Invalid mapping if (region == null || region.IsFixed) { return bytesLeft; } var off = Arch.ArchDefinition.PageOffset(src.ToUInt32()); var virtualAddr = process.Space.UserToVirt(new UserPtr(src)); if (virtualAddr == Pointer.Zero) { // Page isn't present, try to bring it in. uint permission; Pager.HandlePageFault(process, MemoryRegion.FAULT_MASK, src, 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 dst_buf_page = page_buf.Slice(off, bytesTobeCopied); for (var i = 0; i < bytesTobeCopied; ++i) { dst_buf_page.Set(i, dst_buf.Get(cursor + i)); } bytesLeft -= bytesTobeCopied; src += bytesTobeCopied; cursor += bytesTobeCopied; } return bytesLeft; }
private static timespec GetTimeSpec(ByteBufferRef buf) { Contract.Requires(buf.Length >= timespec.Size); var r = new timespec(); r.tv_sec = 0; r.tv_nsec = 0; for (var i = 0; i < sizeof(uint); ++i) { Contract.Assume(buf.Length >= timespec.Size); Contract.Assert(i < buf.Length); r.tv_sec += (uint)buf.Get(i) << (8 * i); } for (var i = 0; i < sizeof(uint); ++i) { Contract.Assume(buf.Length >= timespec.Size); Contract.Assert(i + sizeof(uint) < buf.Length); r.tv_nsec += (uint)buf.Get(i + sizeof(uint)) << (8 * i); } return r; }
// Duplicated from Input(byte[]) public int Input(ByteBufferRef message_array) { if (message_array.Length == 0) return 0; var length = message_array.Length; if (Computed) { Corrupted = true; return -1; } if (Corrupted) { return -1; } int i = 0; Contract.Assert(i + length == message_array.Length); while (length > 0 && !Corrupted) { Contract.Assert(i >= 0); Contract.Assert(i + length == message_array.Length); Message_Block[Message_Block_Index++] = message_array.Get(i); Length_Low += 8; if (Length_Low == 0) { Length_High++; if (Length_High == 0) { /* Message is too long */ Corrupted = true; } } if (Message_Block_Index == 64) { SHA1ProcessMessageBlock(); } i++; length--; } return 0; }
public static int Strnlen(ByteBufferRef buf, int max_len) { var i = 0; while (i < max_len && i < buf.Length) { if (buf.Get(i) == 0) return i; ++i; } return i; }
public static ulong ReadUlong(ByteBufferRef buf, int offset) { Contract.Requires(offset >= 0); Contract.Requires(buf.Length >= offset + sizeof(long)); ulong res = 0; for (var i = 0; i < sizeof(long); ++i) { Contract.Assert(offset + i < buf.Length); res += (ulong)buf.Get(offset + i) << (8 * i); } return res; }