libbin 2.0.0 → 2.1.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +46 -0
- data/README.md +454 -0
- data/ext/libbin/data_types.c +1055 -284
- data/ext/libbin/data_types.h +32 -1
- data/ext/libbin/extconf.rb +15 -0
- data/ext/libbin/half.c +110 -65
- data/ext/libbin/half.h +2 -0
- data/ext/libbin/libbin_c.c +2066 -420
- data/ext/libbin/libbin_c.h +48 -1
- data/ext/libbin/libbin_io.h +506 -0
- data/ext/libbin/pghalf.c +107 -228
- data/ext/libbin/pghalf.h +2 -0
- data/lib/libbin/bitfield.rb +426 -0
- data/lib/libbin/data_shape.rb +387 -0
- data/lib/libbin/data_types.rb +264 -511
- data/lib/libbin/enum.rb +159 -0
- data/lib/libbin/union.rb +314 -0
- data/lib/libbin/utf16.rb +183 -0
- data/lib/libbin/version.rb +4 -0
- data/lib/libbin.rb +165 -14
- data/libbin.gemspec +23 -4
- metadata +46 -5
data/lib/libbin/enum.rb
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
module LibBin
|
|
2
|
+
class Structure
|
|
3
|
+
|
|
4
|
+
# @abstract A parent class representing an enumeration that is loading integers as symbols.
|
|
5
|
+
# User should inherit this base class.
|
|
6
|
+
class Enum
|
|
7
|
+
class << self
|
|
8
|
+
# Get the enum map.
|
|
9
|
+
attr_reader :map
|
|
10
|
+
# Signedness of the underlying type.
|
|
11
|
+
attr_reader :signed
|
|
12
|
+
# Get or set the underlying type.
|
|
13
|
+
attr_accessor :type
|
|
14
|
+
|
|
15
|
+
# Specify the size of the underlying type.
|
|
16
|
+
def set_type_size(sz, signed = true, big = nil)
|
|
17
|
+
tname = "#{signed ? "" : "U"}Int#{sz}"
|
|
18
|
+
tname << (big ? "_BE" : "_LE") unless big.nil?
|
|
19
|
+
begin
|
|
20
|
+
t = Structure.const_get(tname)
|
|
21
|
+
rescue NameError
|
|
22
|
+
raise "unsupported enum type #{tname}"
|
|
23
|
+
end
|
|
24
|
+
@type = t
|
|
25
|
+
@signed = signed
|
|
26
|
+
sz
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Get the underlying type size in bytes.
|
|
30
|
+
def type_size
|
|
31
|
+
@type.size
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Set the enum map.
|
|
35
|
+
# @param m [Hash{Symbol=>Integer}] enum values
|
|
36
|
+
def map=(m)
|
|
37
|
+
map_from = m.dup.freeze
|
|
38
|
+
map_to = m.invert.freeze
|
|
39
|
+
if defined?(Ractor)
|
|
40
|
+
if map_from.all? { |k, v| Ractor.shareable?(k) && Ractor.shareable?(v) }
|
|
41
|
+
Ractor.make_shareable(map_from)
|
|
42
|
+
end
|
|
43
|
+
if map_to.all? { |k, v| Ractor.shareable?(k) && Ractor.shareable?(v) }
|
|
44
|
+
Ractor.make_shareable(map_to)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
@map_to = map_to
|
|
48
|
+
@map_from = @map = map_from
|
|
49
|
+
end
|
|
50
|
+
alias set_map map=
|
|
51
|
+
|
|
52
|
+
# @!visibility private
|
|
53
|
+
def inherited(subclass)
|
|
54
|
+
subclass.instance_variable_set(:@type, Int32)
|
|
55
|
+
subclass.instance_variable_set(:@signed, true)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def decode_value(val, length)
|
|
61
|
+
if length
|
|
62
|
+
val.collect { |elem| @map_to.fetch(elem, elem) }
|
|
63
|
+
else
|
|
64
|
+
@map_to.fetch(val, val)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Returns the underlying type +always_align+ property.
|
|
70
|
+
def self.always_align
|
|
71
|
+
@type.always_align
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Returns the underlying type +align+ property.
|
|
75
|
+
def self.align
|
|
76
|
+
@type.align
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Forwards the call to the underlying type.
|
|
80
|
+
def self.size(value = nil, previous_offset = 0, parent = nil, index = nil, length = nil)
|
|
81
|
+
@type.size(value, previous_offset, parent, index, length)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Forwards the call to the underlying type.
|
|
85
|
+
def self.shape(value = nil, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
|
|
86
|
+
@type.shape(value, previous_offset, parent, index, kind, length)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Load a field of type {Enum} from +input+, and return it.
|
|
90
|
+
# @param input [IO] the stream to load the field from.
|
|
91
|
+
# @param input_big [Boolean] the endianness of +input+
|
|
92
|
+
# @param parent [Structure] ignored.
|
|
93
|
+
# @param index [Integer] ignored.
|
|
94
|
+
# @param length [Integer] if given the length of the vector. Else
|
|
95
|
+
# the length is considered to be 1.
|
|
96
|
+
# @return [Symbol,Integer,Array<Symbol,Integer>] the Ruby
|
|
97
|
+
# representation of the type, or the Array representation of the
|
|
98
|
+
# vector if +length+ was specified
|
|
99
|
+
def self.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
|
|
100
|
+
decode_value(@type.load(input, input_big, parent, index, length), length)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Convert a field of class {Enum} by loading it from +input+ and
|
|
104
|
+
# dumping it to +output+. Returns the loaded field.
|
|
105
|
+
# @param input [IO] the stream to load the field from.
|
|
106
|
+
# @param output [IO] the stream to dump the field to.
|
|
107
|
+
# @param input_big [Boolean] the endianness of +input+
|
|
108
|
+
# @param output_big [Boolean] the endianness of +output+.
|
|
109
|
+
# @param parent [Structure] ignored.
|
|
110
|
+
# @param index [Integer] ignored.
|
|
111
|
+
# @param length [Integer] if given the length of the vector. Else
|
|
112
|
+
# the length is considered to be 1.
|
|
113
|
+
# @return [Symbol,Integer,Array<Symbol,Integer>] the Ruby
|
|
114
|
+
# representation of the type, or the Array representation of the
|
|
115
|
+
# vector if +length+ was specified
|
|
116
|
+
def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil)
|
|
117
|
+
decode_value(@type.convert(input, output, input_big, output_big, parent, index, length), length)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Dump a field of class {Enum} to +output+.
|
|
121
|
+
# @param value [Numeric, Array<Numeric>] the Ruby representation
|
|
122
|
+
# of the type, or the Array representation of the vector if
|
|
123
|
+
# +length+ is specified.
|
|
124
|
+
# @param output [IO] the stream to dump the field to.
|
|
125
|
+
# @param output_big [Boolean] the endianness of +output+.
|
|
126
|
+
# @param parent [Structure] ignored.
|
|
127
|
+
# @param index [Integer] ignored.
|
|
128
|
+
# @param length [Integer] if given the length of the vector. Else
|
|
129
|
+
# the length is considered to be 1.
|
|
130
|
+
# @return [nil]
|
|
131
|
+
def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
|
|
132
|
+
v =
|
|
133
|
+
if length
|
|
134
|
+
length.times.collect { |i| elem = value[i]; @map_from.fetch(elem, elem) }
|
|
135
|
+
else
|
|
136
|
+
@map_from.fetch(value, value)
|
|
137
|
+
end
|
|
138
|
+
@type.dump(v, output, output_big, parent, index, length)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Create a new field of a type inheriting from {Enum} and name +name+.
|
|
143
|
+
# See {field} for more options.
|
|
144
|
+
# @param name [Symbol,String] name of the field.
|
|
145
|
+
# @param map [Hash{Symbol => Integer}] enum values.
|
|
146
|
+
# @param size [Integer] size in bits of the underlying integer
|
|
147
|
+
# @param signed [Boolean] signedness of the underlying type
|
|
148
|
+
# @param big [Boolean] endianness of the underlying type
|
|
149
|
+
# @return self
|
|
150
|
+
def self.enum(name, map, size: 32, signed: false, big: nil, **opts)
|
|
151
|
+
klass = Class.new(Enum) do |c|
|
|
152
|
+
c.set_type_size(size, signed, big)
|
|
153
|
+
c.set_map(map)
|
|
154
|
+
end
|
|
155
|
+
self.field(name, klass, **opts)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
end
|
|
159
|
+
end
|
data/lib/libbin/union.rb
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
module LibBin
|
|
2
|
+
|
|
3
|
+
# A C-style overlaid binary union sharing a single raw byte buffer among its member fields.
|
|
4
|
+
# Reading any member decodes from the shared buffer; writing re-encodes the prefix and
|
|
5
|
+
# tracks the active member for cross-endian conversion.
|
|
6
|
+
class Union < Structure
|
|
7
|
+
attr_reader :__bytes, :__big
|
|
8
|
+
attr_accessor :__active
|
|
9
|
+
|
|
10
|
+
# @!visibility private
|
|
11
|
+
def self.inherited(subclass)
|
|
12
|
+
super
|
|
13
|
+
subclass.instance_variable_set(:@union_size, nil)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize
|
|
17
|
+
super
|
|
18
|
+
@__bytes = "\x00".b * self.class.union_size
|
|
19
|
+
@__big = LibBin::default_big?
|
|
20
|
+
@__active = nil
|
|
21
|
+
@__dirty_field = nil
|
|
22
|
+
@__dirty_val = nil
|
|
23
|
+
@__dirty_init_bytes = nil
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @!visibility private
|
|
27
|
+
def __init_from_raw(bytes, big)
|
|
28
|
+
@__bytes = bytes
|
|
29
|
+
@__big = !!big
|
|
30
|
+
@__active = nil
|
|
31
|
+
@__dirty_field = nil
|
|
32
|
+
@__dirty_val = nil
|
|
33
|
+
@__dirty_init_bytes = nil
|
|
34
|
+
self
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @!visibility private
|
|
38
|
+
def __flush_dirty
|
|
39
|
+
return unless @__dirty_field
|
|
40
|
+
f = @__dirty_field
|
|
41
|
+
val = @__dirty_val
|
|
42
|
+
init_bytes = @__dirty_init_bytes
|
|
43
|
+
@__dirty_field = nil
|
|
44
|
+
@__dirty_val = nil
|
|
45
|
+
@__dirty_init_bytes = nil
|
|
46
|
+
return if val.nil?
|
|
47
|
+
cur_bytes = __encode_field_bytes(f, val, @__big)
|
|
48
|
+
if init_bytes.nil? || cur_bytes != init_bytes
|
|
49
|
+
__write_prefix_bytes(cur_bytes)
|
|
50
|
+
@__active = f.name.to_sym
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Define a union member sharing offset 0 (or embed an anonymous {Structure}/{Union} when +name+ is nil).
|
|
55
|
+
def self.field(name, type = FIELD_TYPE_UNSET, length: nil, count: nil, offset: nil,
|
|
56
|
+
sequence: false, condition: nil, relative_offset: false,
|
|
57
|
+
align: false, expect: nil, null_offset: 0)
|
|
58
|
+
if type.equal?(FIELD_TYPE_UNSET)
|
|
59
|
+
if name.is_a?(Class) && name < LibBin::Structure
|
|
60
|
+
type = name
|
|
61
|
+
name = nil
|
|
62
|
+
else
|
|
63
|
+
raise ArgumentError, "wrong number of arguments (given 1, expected 2)"
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
if name.nil?
|
|
68
|
+
unless type.is_a?(Class) && type < LibBin::Structure
|
|
69
|
+
raise ArgumentError, "anonymous Union member must be a LibBin::Structure or LibBin::Union class"
|
|
70
|
+
end
|
|
71
|
+
if count || length
|
|
72
|
+
raise ArgumentError, "anonymous embedded structure/union cannot have count or length"
|
|
73
|
+
end
|
|
74
|
+
hidden = :"__anon_#{@fields.length}"
|
|
75
|
+
self.field(hidden, type, offset: offset, sequence: sequence,
|
|
76
|
+
condition: condition, relative_offset: relative_offset,
|
|
77
|
+
align: align, expect: expect, null_offset: null_offset)
|
|
78
|
+
type.__promoted_accessors.each do |mname|
|
|
79
|
+
msetter = :"#{mname}="
|
|
80
|
+
define_method(mname) do
|
|
81
|
+
send(hidden).public_send(mname)
|
|
82
|
+
end
|
|
83
|
+
define_method(msetter) do |val|
|
|
84
|
+
sub = send(hidden)
|
|
85
|
+
sub.public_send(msetter, val)
|
|
86
|
+
send(:"#{hidden}=", sub)
|
|
87
|
+
val
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
return self
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
align = resolve_align(type, align)
|
|
94
|
+
f = Field::new(name, type, length, count, offset, sequence,
|
|
95
|
+
condition, relative_offset, align, expect, null_offset)
|
|
96
|
+
set_fields_array(@fields + [f])
|
|
97
|
+
@union_size = nil
|
|
98
|
+
|
|
99
|
+
mname = name.to_sym
|
|
100
|
+
define_method(mname) do
|
|
101
|
+
return @__dirty_val if @__dirty_field.equal?(f)
|
|
102
|
+
__flush_dirty
|
|
103
|
+
val, consumed = __decode_field_value(f, @__bytes, @__big)
|
|
104
|
+
if val.is_a?(Structure) || val.is_a?(Structure::Bitfield) || val.is_a?(Array)
|
|
105
|
+
@__dirty_field = f
|
|
106
|
+
@__dirty_val = val
|
|
107
|
+
@__dirty_init_bytes = consumed
|
|
108
|
+
end
|
|
109
|
+
val
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
define_method(:"#{mname}=") do |val|
|
|
113
|
+
@__dirty_field = nil
|
|
114
|
+
@__dirty_val = nil
|
|
115
|
+
@__dirty_init_bytes = nil
|
|
116
|
+
encoded = __encode_field_bytes(f, val, @__big)
|
|
117
|
+
__write_prefix_bytes(encoded)
|
|
118
|
+
@__active = mname
|
|
119
|
+
val
|
|
120
|
+
end
|
|
121
|
+
self
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
class << self
|
|
125
|
+
alias register_field field
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Build a sample instance with nested non-conditional structures populated for static sizing.
|
|
129
|
+
def self.sample_instance(klass)
|
|
130
|
+
inst = klass.new
|
|
131
|
+
klass.fields.each do |fld|
|
|
132
|
+
next unless fld.name && fld.condition.nil?
|
|
133
|
+
ft = fld.type
|
|
134
|
+
if ft.is_a?(Class) && ft <= Union
|
|
135
|
+
cnt = fld.count.is_a?(Integer) ? fld.count : nil
|
|
136
|
+
len = fld.length.is_a?(Integer) ? fld.length : nil
|
|
137
|
+
val = cnt ? Array.new(cnt) { ft.new } : (len ? Array.new(len) { ft.new } : ft.new)
|
|
138
|
+
inst.public_send(:"#{fld.name}=", val)
|
|
139
|
+
elsif ft.is_a?(Class) && ft < Structure
|
|
140
|
+
cnt = fld.count.is_a?(Integer) ? fld.count : nil
|
|
141
|
+
len = fld.length.is_a?(Integer) ? fld.length : nil
|
|
142
|
+
val = cnt ? Array.new(cnt) { sample_instance(ft) } : (len ? Array.new(len) { sample_instance(ft) } : sample_instance(ft))
|
|
143
|
+
inst.public_send(:"#{fld.name}=", val)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
inst
|
|
147
|
+
end
|
|
148
|
+
private_class_method :sample_instance
|
|
149
|
+
|
|
150
|
+
# Compute the byte size of one field inside the union.
|
|
151
|
+
def self.field_byte_size(f)
|
|
152
|
+
t = f.type
|
|
153
|
+
raise ArgumentError, "Union member #{f.name} cannot have dynamic Proc type" if t.is_a?(Proc)
|
|
154
|
+
raise ArgumentError, "Union member #{f.name} cannot have dynamic Proc length" if f.length.is_a?(Proc)
|
|
155
|
+
raise ArgumentError, "Union member #{f.name} cannot have dynamic Proc count" if f.count.is_a?(Proc)
|
|
156
|
+
|
|
157
|
+
elem_size =
|
|
158
|
+
if t.is_a?(Class) && t <= Union
|
|
159
|
+
f.length ? t.union_size * Integer(f.length) : t.union_size
|
|
160
|
+
elsif t.is_a?(Class) && t <= Structure
|
|
161
|
+
base = sample_instance(t).__size
|
|
162
|
+
f.length ? base * Integer(f.length) : base
|
|
163
|
+
else
|
|
164
|
+
t.size(nil, 0, nil, nil, f.length)
|
|
165
|
+
end
|
|
166
|
+
cnt = f.count ? Integer(f.count) : 1
|
|
167
|
+
off = f.offset.is_a?(Integer) ? f.offset : 0
|
|
168
|
+
off + elem_size * cnt
|
|
169
|
+
end
|
|
170
|
+
private_class_method :field_byte_size
|
|
171
|
+
|
|
172
|
+
# Maximum byte size across all union members.
|
|
173
|
+
# @return [Integer]
|
|
174
|
+
def self.union_size
|
|
175
|
+
return @union_size if defined?(@union_size) && @union_size
|
|
176
|
+
sz = @fields.empty? ? 0 : @fields.map { |f| field_byte_size(f) }.max
|
|
177
|
+
@union_size = sz if !defined?(Ractor) || Ractor.current == Ractor.main
|
|
178
|
+
sz
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Return the size of the union (or vector of unions).
|
|
182
|
+
def self.size(value = nil, offset = 0, parent = nil, index = nil, length = nil)
|
|
183
|
+
sz = union_size
|
|
184
|
+
return sz unless length
|
|
185
|
+
len = Integer(length)
|
|
186
|
+
raise ArgumentError, "length must be non-negative: #{len}" if len < 0
|
|
187
|
+
sz * len
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Return a single contiguous shape covering the union (never overlapping member spans).
|
|
191
|
+
def self.shape(value = nil, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
|
|
192
|
+
previous_offset ||= 0
|
|
193
|
+
kind ||= DataShape
|
|
194
|
+
sz = union_size
|
|
195
|
+
if length
|
|
196
|
+
len = Integer(length)
|
|
197
|
+
raise ArgumentError, "length must be non-negative: #{len}" if len < 0
|
|
198
|
+
return nil if sz == 0 || len == 0
|
|
199
|
+
pos = previous_offset
|
|
200
|
+
shapes = len.times.collect do
|
|
201
|
+
sh = kind.new(pos, pos + sz - 1)
|
|
202
|
+
pos += sz
|
|
203
|
+
sh
|
|
204
|
+
end
|
|
205
|
+
return kind.new(shapes)
|
|
206
|
+
end
|
|
207
|
+
return nil if sz == 0
|
|
208
|
+
kind.new(previous_offset, previous_offset + sz - 1)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Instance shape delegate so Structure#__shape treats nested Union as one contiguous span.
|
|
212
|
+
def __shape(previous_offset = 0, parent = nil, index = nil, kind = DataShape)
|
|
213
|
+
self.class.shape(self, previous_offset, parent, index, kind)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Load a union (or array of unions when +length+ is given).
|
|
217
|
+
def self.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
|
|
218
|
+
ibig = input_big.nil? ? LibBin::default_big? : !!input_big
|
|
219
|
+
sz = union_size
|
|
220
|
+
if length
|
|
221
|
+
len = Integer(length)
|
|
222
|
+
raise ArgumentError, "length must be non-negative: #{len}" if len < 0
|
|
223
|
+
return [] if len == 0
|
|
224
|
+
raw = Str.load(input, ibig, parent, index, sz * len)
|
|
225
|
+
return len.times.collect do |i|
|
|
226
|
+
new.__init_from_raw(raw.byteslice(i * sz, sz), ibig)
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
raw = sz > 0 ? Str.load(input, ibig, parent, index, sz) : "".b
|
|
230
|
+
new.__init_from_raw(raw, ibig)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Dump a union (or array of unions when +length+ is given).
|
|
234
|
+
def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
|
|
235
|
+
obig = output_big.nil? ? LibBin::default_big? : !!output_big
|
|
236
|
+
sz = union_size
|
|
237
|
+
if length
|
|
238
|
+
len = Integer(length)
|
|
239
|
+
raise ArgumentError, "length must be non-negative: #{len}" if len < 0
|
|
240
|
+
raise TypeError, "no implicit conversion of #{value.class} into Array" unless value.is_a?(Array)
|
|
241
|
+
raise ArgumentError, "array too short for length: got #{value.length} needed #{len}" if value.length < len
|
|
242
|
+
if sz > 0 && len > 0
|
|
243
|
+
buf = "".b
|
|
244
|
+
len.times { |i| buf << encode_instance(value[i], obig, sz) }
|
|
245
|
+
Str.dump(buf, output, obig, parent, index, sz * len)
|
|
246
|
+
end
|
|
247
|
+
elsif sz > 0
|
|
248
|
+
buf = encode_instance(value, obig, sz)
|
|
249
|
+
Str.dump(buf, output, obig, parent, index, sz)
|
|
250
|
+
end
|
|
251
|
+
nil
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# Convert a union between endiannesses.
|
|
255
|
+
def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil)
|
|
256
|
+
ibig = input_big.nil? ? LibBin::default_big? : !!input_big
|
|
257
|
+
obig = output_big.nil? ? !ibig : !!output_big
|
|
258
|
+
val = load(input, ibig, parent, index, length)
|
|
259
|
+
dump(val, output, obig, parent, index, length)
|
|
260
|
+
val
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def self.encode_instance(val, obig, sz)
|
|
264
|
+
return "\x00".b * sz if val.nil?
|
|
265
|
+
val.__flush_dirty
|
|
266
|
+
if val.__big == obig || @fields.empty?
|
|
267
|
+
raw = val.__bytes
|
|
268
|
+
return raw.bytesize == sz ? raw : (raw + "\x00".b * sz).byteslice(0, sz)
|
|
269
|
+
end
|
|
270
|
+
active = val.__active || @fields.first.name.to_sym
|
|
271
|
+
f = @fields.find { |fld| fld.name && fld.name.to_sym == active } || @fields.first
|
|
272
|
+
decoded, _ = val.send(:__decode_field_value, f, val.__bytes, val.__big)
|
|
273
|
+
encoded = val.send(:__encode_field_bytes, f, decoded, obig)
|
|
274
|
+
if encoded.bytesize >= sz
|
|
275
|
+
encoded.byteslice(0, sz)
|
|
276
|
+
else
|
|
277
|
+
encoded + "\x00".b * (sz - encoded.bytesize)
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
private_class_method :encode_instance
|
|
281
|
+
|
|
282
|
+
private
|
|
283
|
+
|
|
284
|
+
def __decode_field_value(f, bytes, big)
|
|
285
|
+
sio = StringIO.new(bytes)
|
|
286
|
+
__set_load_state(sio, big, nil, nil)
|
|
287
|
+
begin
|
|
288
|
+
val = __load_field(f)
|
|
289
|
+
[val, bytes.byteslice(0, sio.pos)]
|
|
290
|
+
ensure
|
|
291
|
+
__unset_load_state
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def __encode_field_bytes(f, val, big)
|
|
296
|
+
sio = StringIO.new("".b)
|
|
297
|
+
__set_dump_state(sio, big, nil, nil)
|
|
298
|
+
begin
|
|
299
|
+
__dump_field(val, f)
|
|
300
|
+
ensure
|
|
301
|
+
__unset_dump_state
|
|
302
|
+
end
|
|
303
|
+
sio.string
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def __write_prefix_bytes(encoded)
|
|
307
|
+
sz = self.class.union_size
|
|
308
|
+
@__bytes = "\x00".b * sz unless @__bytes.bytesize >= sz
|
|
309
|
+
n = encoded.bytesize
|
|
310
|
+
@__bytes[0, n] = encoded
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
end
|
data/lib/libbin/utf16.rb
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
module LibBin
|
|
2
|
+
class Structure
|
|
3
|
+
|
|
4
|
+
# Base class for UTF-16 encoded strings (fixed code-unit length or NUL-terminated).
|
|
5
|
+
class UTF16Base
|
|
6
|
+
class << self
|
|
7
|
+
# Optional fixed endianness override (+true+ BE, +false+ LE, or +nil+ to follow stream).
|
|
8
|
+
def fixed_big
|
|
9
|
+
nil
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Whether fixed-length reads strip trailing NUL padding.
|
|
13
|
+
def trim?
|
|
14
|
+
true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Whether fields of this type auto-align by default.
|
|
18
|
+
def always_align
|
|
19
|
+
false
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Preferred alignment in bytes (2).
|
|
23
|
+
def align
|
|
24
|
+
2
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Return byte size of a fixed-length or NUL-terminated UTF-16 string.
|
|
28
|
+
def size(value = nil, previous_offset = 0, parent = nil, index = nil, length = nil)
|
|
29
|
+
return 2 * validate_length(length) if length
|
|
30
|
+
|
|
31
|
+
raise TypeError, "no implicit conversion of #{value.class} into String" unless value.is_a?(String)
|
|
32
|
+
value.encode(Encoding::UTF_16LE).bytesize + 2
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Return shape of a UTF-16 string field.
|
|
36
|
+
def shape(value = nil, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
|
|
37
|
+
previous_offset ||= 0
|
|
38
|
+
kind ||= DataShape
|
|
39
|
+
if length
|
|
40
|
+
return kind.new(previous_offset, previous_offset - 1 + 2 * validate_length(length))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
raise TypeError, "no implicit conversion of #{value.class} into String" unless value.is_a?(String)
|
|
44
|
+
bytes = value.encode(Encoding::UTF_16LE).bytesize + 2
|
|
45
|
+
kind.new(previous_offset, previous_offset - 1 + bytes)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Load a UTF-16 string from +input+ and return a UTF-8 +String+.
|
|
49
|
+
def load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
|
|
50
|
+
ibig = resolve_big(input_big)
|
|
51
|
+
raw, _terminated = read_raw(input, ibig, parent, length)
|
|
52
|
+
decode_raw(raw, ibig, !length.nil?)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Encode +value+ as UTF-16 and write it to +output+.
|
|
56
|
+
def dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
|
|
57
|
+
raise TypeError, "no implicit conversion of #{value.class} into String" unless value.is_a?(String)
|
|
58
|
+
|
|
59
|
+
obig = resolve_big(output_big)
|
|
60
|
+
encoded = value.encode(encoding_for(obig)).b
|
|
61
|
+
if length
|
|
62
|
+
Str.dump(encoded, output, obig, parent, nil, 2 * validate_length(length))
|
|
63
|
+
else
|
|
64
|
+
Str.dump(encoded + "\x00\x00".b, output, obig, parent)
|
|
65
|
+
end
|
|
66
|
+
nil
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Convert a UTF-16 string between endiannesses while copying from +input+ to +output+.
|
|
70
|
+
def convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil)
|
|
71
|
+
ibig = resolve_big(input_big)
|
|
72
|
+
obig = fixed_big.nil? ? (output_big.nil? ? !ibig : !!output_big) : fixed_big
|
|
73
|
+
raw, terminated = read_raw(input, ibig, parent, length)
|
|
74
|
+
out_bytes = ibig == obig ? raw : swap_utf16_bytes(raw)
|
|
75
|
+
if length
|
|
76
|
+
Str.dump(out_bytes, output, obig, parent, nil, out_bytes.bytesize)
|
|
77
|
+
else
|
|
78
|
+
out_bytes = out_bytes + "\x00\x00".b if terminated
|
|
79
|
+
Str.dump(out_bytes, output, obig, parent)
|
|
80
|
+
end
|
|
81
|
+
decode_raw(raw, ibig, !length.nil?)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def validate_length(length)
|
|
87
|
+
len = Integer(length)
|
|
88
|
+
raise ArgumentError, "length must be non-negative: #{len}" if len < 0
|
|
89
|
+
len
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def resolve_big(flag)
|
|
93
|
+
return fixed_big unless fixed_big.nil?
|
|
94
|
+
flag.nil? ? LibBin::default_big? : !!flag
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def encoding_for(big)
|
|
98
|
+
big ? Encoding::UTF_16BE : Encoding::UTF_16LE
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def read_raw(input, big, parent, length)
|
|
102
|
+
if length
|
|
103
|
+
len = validate_length(length)
|
|
104
|
+
return ["".b, false] if len == 0
|
|
105
|
+
return [Str.load(input, big, parent, nil, 2 * len), false]
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
buf = "".b
|
|
109
|
+
terminated = false
|
|
110
|
+
loop do
|
|
111
|
+
begin
|
|
112
|
+
chunk = Str.load(input, big, parent, nil, 2)
|
|
113
|
+
rescue LibBin::EOFError => e
|
|
114
|
+
if e.message.start_with?("could not read enough data: got 0")
|
|
115
|
+
raise ::EOFError, "end of file reached" if buf.empty?
|
|
116
|
+
break
|
|
117
|
+
end
|
|
118
|
+
raise
|
|
119
|
+
end
|
|
120
|
+
if chunk == "\x00\x00".b
|
|
121
|
+
terminated = true
|
|
122
|
+
break
|
|
123
|
+
end
|
|
124
|
+
buf << chunk
|
|
125
|
+
end
|
|
126
|
+
[buf, terminated]
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def decode_raw(raw, big, fixed_len)
|
|
130
|
+
slice = raw
|
|
131
|
+
if fixed_len && trim?
|
|
132
|
+
i = 0
|
|
133
|
+
n = raw.bytesize
|
|
134
|
+
while i + 1 < n
|
|
135
|
+
if raw.getbyte(i) == 0 && raw.getbyte(i + 1) == 0
|
|
136
|
+
slice = raw.byteslice(0, i)
|
|
137
|
+
break
|
|
138
|
+
end
|
|
139
|
+
i += 2
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
slice.dup.force_encoding(encoding_for(big)).encode(Encoding::UTF_8)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def swap_utf16_bytes(raw)
|
|
146
|
+
raw.unpack("n*").pack("v*")
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Stream-endian NUL-trimmed UTF-16 string.
|
|
152
|
+
class UTF16 < UTF16Base; end
|
|
153
|
+
# Stream-endian untrimmed UTF-16 string.
|
|
154
|
+
class UTF16Raw < UTF16Base; def self.trim?; false; end; end
|
|
155
|
+
# Little-endian NUL-trimmed UTF-16 string.
|
|
156
|
+
class UTF16_LE < UTF16Base; def self.fixed_big; false; end; end
|
|
157
|
+
# Little-endian untrimmed UTF-16 string.
|
|
158
|
+
class UTF16_LERaw < UTF16_LE; def self.trim?; false; end; end
|
|
159
|
+
# Big-endian NUL-trimmed UTF-16 string.
|
|
160
|
+
class UTF16_BE < UTF16Base; def self.fixed_big; true; end; end
|
|
161
|
+
# Big-endian untrimmed UTF-16 string.
|
|
162
|
+
class UTF16_BERaw < UTF16_BE; def self.trim?; false; end; end
|
|
163
|
+
|
|
164
|
+
# Create a UTF-16 string field following stream endianness.
|
|
165
|
+
# @return self
|
|
166
|
+
def self.utf16(field_name, length = nil, trim: true, **opts)
|
|
167
|
+
self.field(field_name, trim ? UTF16 : UTF16Raw, length: length, **opts)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Create a little-endian UTF-16 string field.
|
|
171
|
+
# @return self
|
|
172
|
+
def self.utf16_le(field_name, length = nil, trim: true, **opts)
|
|
173
|
+
self.field(field_name, trim ? UTF16_LE : UTF16_LERaw, length: length, **opts)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Create a big-endian UTF-16 string field.
|
|
177
|
+
# @return self
|
|
178
|
+
def self.utf16_be(field_name, length = nil, trim: true, **opts)
|
|
179
|
+
self.field(field_name, trim ? UTF16_BE : UTF16_BERaw, length: length, **opts)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
end
|
|
183
|
+
end
|