CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/postgres/binary_writer.rb
Views: 1904
1
# -*- coding: binary -*-
2
require 'postgres_msf'
3
require 'postgres/byteorder'
4
5
# Namespace for Metasploit branch.
6
module Msf
7
module Db
8
9
module BinaryWriterMixin
10
11
# == 8 bit
12
13
# no byteorder for 8 bit!
14
15
def write_word8(val)
16
pw(val, 'C')
17
end
18
19
def write_int8(val)
20
pw(val, 'c')
21
end
22
23
alias write_byte write_word8
24
25
# == 16 bit
26
27
# === Unsigned
28
29
def write_word16_native(val)
30
pw(val, 'S')
31
end
32
33
def write_word16_little(val)
34
str = [val].pack('S')
35
str.reverse! if ByteOrder.network? # swap bytes as native=network (and we want little)
36
write(str)
37
end
38
39
def write_word16_network(val)
40
str = [val].pack('S')
41
str.reverse! if ByteOrder.little? # swap bytes as native=little (and we want network)
42
write(str)
43
end
44
45
# === Signed
46
47
def write_int16_native(val)
48
pw(val, 's')
49
end
50
51
def write_int16_little(val)
52
pw(val, 'v')
53
end
54
55
def write_int16_network(val)
56
pw(val, 'n')
57
end
58
59
# == 32 bit
60
61
# === Unsigned
62
63
def write_word32_native(val)
64
pw(val, 'L')
65
end
66
67
def write_word32_little(val)
68
str = [val].pack('L')
69
str.reverse! if ByteOrder.network? # swap bytes as native=network (and we want little)
70
write(str)
71
end
72
73
def write_word32_network(val)
74
str = [val].pack('L')
75
str.reverse! if ByteOrder.little? # swap bytes as native=little (and we want network)
76
write(str)
77
end
78
79
# === Signed
80
81
def write_int32_native(val)
82
pw(val, 'l')
83
end
84
85
def write_int32_little(val)
86
pw(val, 'V')
87
end
88
89
def write_int32_network(val)
90
pw(val, 'N')
91
end
92
93
# add some short-cut functions
94
%w(word16 int16 word32 int32).each do |typ|
95
alias_method "write_#{typ}_big", "write_#{typ}_network"
96
end
97
98
# == Other methods
99
100
private
101
102
# shortcut for pack and write
103
def pw(val, template)
104
write([val].pack(template))
105
end
106
end
107
108
end
109
end
110
111