Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/postgres/buffer.rb
Views: 11766
# -*- coding: binary -*-1require 'postgres_msf'2require 'postgres/binary_writer'3require 'postgres/binary_reader'45# Namespace for Metasploit branch.6module Msf7module Db89# Fixed size buffer.10class Buffer1112class Error < RuntimeError; end13class EOF < Error; end1415def self.from_string(str)16new(str)17end1819def self.of_size(size)20raise ArgumentError if size < 021new('#' * size)22end2324def initialize(content)25@size = content.size26@content = content27@position = 028end2930def size31@size32end3334def position35@position36end3738def peek39@content[@position]40end4142def position=(new_pos)43raise ArgumentError if new_pos < 0 or new_pos > @size44@position = new_pos45end4647def at_end?48@position == @size49end5051def content52@content53end5455def read(n)56raise EOF, 'cannot read beyond the end of buffer' if @position + n > @size57str = @content[@position, n]58@position += n59str60end6162def write(str)63sz = str.size64raise EOF, 'cannot write beyond the end of buffer' if @position + sz > @size65@content[@position, sz] = str66@position += sz67self68end6970def copy_from_stream(stream, n)71raise ArgumentError if n < 072while n > 073str = stream.read(n)74write(str)75n -= str.size76end77raise if n < 078end7980NUL = "\000"8182def write_cstring(cstr)83raise ArgumentError, "Invalid Ruby/cstring" if cstr.include?(NUL)84write(cstr)85write(NUL)86end8788# returns a Ruby string without the trailing NUL character89def read_cstring90nul_pos = @content.index(NUL, @position)91raise Error, "no cstring found!" unless nul_pos9293sz = nul_pos - @position94str = @content[@position, sz]95@position += sz + 196return str97end9899# read till the end of the buffer100def read_rest101read(self.size-@position)102end103104include BinaryWriterMixin105include BinaryReaderMixin106end107108end109end110111112