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/binary_reader.rb
Views: 11766
# -*- coding: binary -*-1require 'postgres_msf'2require 'postgres/byteorder'34# Namespace for Metasploit branch.5module Msf6module Db78# This mixin solely depends on method read(n), which must be defined9# in the class/module where you mixin this module.10module BinaryReaderMixin1112# == 8 bit1314# no byteorder for 8 bit!1516def read_word817ru(1, 'C')18end1920def read_int821ru(1, 'c')22end2324alias read_byte read_word82526# == 16 bit2728# === Unsigned2930def read_word16_native31ru(2, 'S')32end3334def read_word16_little35ru(2, 'v')36end3738def read_word16_big39ru(2, 'n')40end4142# === Signed4344def read_int16_native45ru(2, 's')46end4748def read_int16_little49# swap bytes if native=big (but we want little)50ru_swap(2, 's', ByteOrder::Big)51end5253def read_int16_big54# swap bytes if native=little (but we want big)55ru_swap(2, 's', ByteOrder::Little)56end5758# == 32 bit5960# === Unsigned6162def read_word32_native63ru(4, 'L')64end6566def read_word32_little67ru(4, 'V')68end6970def read_word32_big71ru(4, 'N')72end7374# === Signed7576def read_int32_native77ru(4, 'l')78end7980def read_int32_little81# swap bytes if native=big (but we want little)82ru_swap(4, 'l', ByteOrder::Big)83end8485def read_int32_big86# swap bytes if native=little (but we want big)87ru_swap(4, 'l', ByteOrder::Little)88end8990# == Aliases9192alias read_uint8 read_word89394# add some short-cut functions95%w(word16 int16 word32 int32).each do |typ|96alias_method "read_#{typ}_network", "read_#{typ}_big"97end9899{:word16 => :uint16, :word32 => :uint32}.each do |old, new|100['_native', '_little', '_big', '_network'].each do |bo|101alias_method "read_#{new}#{bo}", "read_#{old}#{bo}"102end103end104105# read exactly n characters, otherwise raise an exception.106def readn(n)107str = read(n)108raise "couldn't read #{n} characters" if str.nil? or str.size != n109str110end111112private113114# shortcut method for readn+unpack115def ru(size, template)116readn(size).unpack(template).first117end118119# same as method +ru+, but swap bytes if native byteorder == _byteorder_120def ru_swap(size, template, byteorder)121str = readn(size)122str.reverse! if ByteOrder.byteorder == byteorder123str.unpack(template).first124end125end126127end128end129130131