bin_tools 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: afa83c1999ac186ee2e917d137c27a3926cf69bf
4
+ data.tar.gz: 4b59fc2d1afd8f42ff96b9a2814ca0ef04fb866a
5
+ SHA512:
6
+ metadata.gz: 59df6d09f30b27b08e64828e16e8d2ce1e53c45483eb010b1b8c69630c3fc184d26a9ebabcae1133a0a502a8b5e1ed14f436985980daa8dfd0ad1277fdbe2a6f
7
+ data.tar.gz: fbf60d1eb431b742a537d6c49b8d0fca83b01911b69e028596e0cb1a883aa9edfd1dda44b364989770bff906c958804cf95bee296d4b7ef84998ca67cf1b8d4d
@@ -0,0 +1,3 @@
1
+ module BinTools; end
2
+ require 'bin_tools/reader'
3
+ require 'bin_tools/writer'
@@ -0,0 +1,151 @@
1
+ # General purpose binary reader
2
+
3
+ # 8 16 32 64
4
+ # C S L Q - unsigned
5
+ # c s l q - unsigned
6
+ # < LE
7
+ # > BE
8
+ # A - binary string
9
+ # z - null terminated
10
+ # H - hex string
11
+
12
+ class BinTools::Reader
13
+ def initialize str
14
+ @rom = str
15
+ @cur = 0
16
+ @base = 0
17
+ end
18
+
19
+ def self.from_file fn
20
+ data = File.open(fn, "rb") { |io| io.read }
21
+ self.new data
22
+ end
23
+
24
+ def set_base pos
25
+ @base = pos
26
+ end
27
+
28
+ def seek pos
29
+ @cur = pos + @base
30
+ end
31
+
32
+ def seek_rel pos
33
+ @cur += pos
34
+ end
35
+
36
+ def tell
37
+ @cur - @base
38
+ end
39
+
40
+ def read_str len
41
+ r = @rom[@cur..(@cur + len - 1)].unpack("A#{len}").first
42
+ @cur += len
43
+ r
44
+ end
45
+
46
+ def read_str_varlen
47
+ len = read_varlen_le
48
+ r = @rom[@cur..(@cur + len - 1)].unpack("A#{len}").first
49
+ @cur += len
50
+ r
51
+ end
52
+
53
+ def read_byte
54
+ r = @rom[@cur].ord
55
+ @cur += 1
56
+ r
57
+ end
58
+
59
+ def read_bool
60
+ read_byte == 1
61
+ end
62
+
63
+ def read_s8
64
+ r = @rom[@cur].unpack("c").first
65
+ @cur += 1
66
+ r
67
+ end
68
+
69
+ def read_u16_le
70
+ r = @rom[@cur..(@cur + 3)].unpack('S<').first
71
+ @cur += 2
72
+ r
73
+ end
74
+
75
+ def read_s16_le
76
+ r = @rom[@cur..(@cur + 3)].unpack('s<').first
77
+ @cur += 2
78
+ r
79
+ end
80
+
81
+ def read_u16_be
82
+ r = @rom[@cur..(@cur + 3)].unpack('S>').first
83
+ @cur += 2
84
+ r
85
+ end
86
+
87
+ def read_u32_le
88
+ r = @rom[@cur..(@cur + 3)].unpack('L<').first
89
+ @cur += 4
90
+ r
91
+ end
92
+
93
+ def read_s32_le
94
+ r = @rom[@cur..(@cur + 3)].unpack('l<').first
95
+ @cur += 4
96
+ r
97
+ end
98
+
99
+ def read_u32_be
100
+ r = @rom[@cur..(@cur + 3)].unpack('L>').first
101
+ @cur += 4
102
+ r
103
+ end
104
+
105
+ def read_f32_le
106
+ r = @rom[@cur..(@cur + 3)].unpack('e').first
107
+ @cur += 4
108
+ r
109
+ end
110
+
111
+ def read_bin len
112
+ r = @rom[@cur..(@cur + len - 1)]
113
+ @cur += len
114
+ r
115
+ end
116
+
117
+ def read_binswap len
118
+ togo = len
119
+ bin_data = "".b
120
+ while togo > 0
121
+ r = @rom[@cur..(@cur + 3)].unpack('L>')
122
+ bin_data += r.pack('L<')
123
+ @cur += 4
124
+ togo -= 4
125
+ end
126
+ bin_data
127
+ end
128
+
129
+ def read_varlen_le
130
+ val = 0
131
+ r = read_byte
132
+ val = r & 0x7F
133
+ return val if r < 0x80
134
+ r = read_byte << 7
135
+ val + r
136
+ end
137
+
138
+ def read_varlen_be
139
+ val = 0
140
+ r = read_byte
141
+ val = r & 0x7F
142
+ return val if r < 0x80
143
+ r = read_byte
144
+ val = (val << 8) + r
145
+ val
146
+ end
147
+
148
+ def msg str
149
+ puts "%08X(%08X): %s" % [@cur, tell, str]
150
+ end
151
+ end
@@ -0,0 +1,123 @@
1
+ # 8 16 32 64
2
+ # C S L Q - unsigned
3
+ # c s l q - unsigned
4
+ # < LE
5
+ # > BE
6
+ # A - binary string
7
+ # z - null terminated
8
+ # H - hex string
9
+
10
+
11
+ class BinTools::Writer
12
+ def initialize fn
13
+ @f = File.new fn, "wb"
14
+ end
15
+
16
+ def self.open fn, &block
17
+ f = self.new fn
18
+ block.call f
19
+ f.close
20
+ end
21
+
22
+ def seek pos
23
+ @f.seek pos
24
+ end
25
+
26
+ def tell
27
+ @f.tell
28
+ end
29
+
30
+ def write_str str
31
+ @f.write str
32
+ end
33
+
34
+ def write_str_varlen str
35
+ write_varlen_le str.size
36
+ @f.write str
37
+ end
38
+
39
+ def write_byte val
40
+ @f.write [val].pack("C")
41
+ end
42
+
43
+ def write_bool val
44
+ write_byte val ? 1 : 0
45
+ end
46
+
47
+ def write_u16_le val
48
+ @f.write [val].pack("S<")
49
+ end
50
+
51
+ def write_s16_le val
52
+ @f.write [val].pack("s<")
53
+ end
54
+
55
+ def write_u16_be val
56
+ @f.write [val].pack("S>")
57
+ end
58
+
59
+ def write_u32_le val
60
+ @f.write [val].pack("L<")
61
+ end
62
+
63
+ def write_s32_le val
64
+ @f.write [val].pack("l<")
65
+ end
66
+
67
+ def write_u32_be val
68
+ @f.write [val].pack("L>")
69
+ end
70
+
71
+ def write_f32_le val
72
+ @f.write [val].pack("e")
73
+ end
74
+
75
+ def write_u24_be val
76
+ write_byte val >> 16
77
+ write_u16_be val & 0xFFFF
78
+ end
79
+
80
+ def write_bin str
81
+ @f.write str
82
+ end
83
+
84
+ def write_varlen_be val
85
+ out = [val & 0x7F]
86
+ val = val >> 7
87
+ while val > 0
88
+ out << (val & 0x7f) + 0x80
89
+ val = val >> 7
90
+ end
91
+ out.reverse.each do |x|
92
+ write_byte x
93
+ end
94
+ end
95
+
96
+ def write_varlen_le val
97
+ out = [val & 0x7F]
98
+ val = val >> 7
99
+ while val > 0
100
+ out << (val & 0x7f)
101
+ val = val >> 7
102
+ end
103
+ out.each_with_index do |x, i|
104
+ write_byte(x + ((i == out.size - 1) ? 0 : 0x80))
105
+ end
106
+ end
107
+
108
+ def write_binswap str
109
+ togo = str.size
110
+ pos = 0
111
+ while togo > 0
112
+ r = @rom[pos..(pos+3)].unpack('L>')
113
+ d = r.pack('L<')
114
+ write_bin d
115
+ pos += 4
116
+ togo -= 4
117
+ end
118
+ end
119
+
120
+ def close
121
+ @f.close
122
+ end
123
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bin_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Madeline Lim
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Binary I/O library for Ruby that is similar to using the C# Binary Reader
14
+ and Writer.
15
+ email: deltabouche@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/bin_tools.rb
21
+ - lib/bin_tools/reader.rb
22
+ - lib/bin_tools/writer.rb
23
+ homepage: https://github.com/deltabouche/bin_tools
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.6.14
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Binary I/O library for Ruby
47
+ test_files: []