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.rb
CHANGED
|
@@ -1,25 +1,120 @@
|
|
|
1
|
-
require "libbin_c
|
|
1
|
+
require "libbin_c"
|
|
2
2
|
|
|
3
|
+
require_relative 'libbin/version'
|
|
3
4
|
require_relative 'libbin/data_types'
|
|
4
5
|
|
|
5
6
|
# Container module, handles the global state default endianness and error output.
|
|
6
7
|
module LibBin
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
DEFAULT_BIG = ([0x12345678].pack("i") == "\x12\x34\x56\x78").freeze
|
|
10
|
+
OUTPUT_SILENT = :__libbin_output_silent
|
|
11
|
+
private_constant :DEFAULT_BIG, :OUTPUT_SILENT
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
if Fiber.respond_to?(:[])
|
|
14
|
+
def self.fiber_local_get(key)
|
|
15
|
+
Fiber[key]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.fiber_local_set(key, val)
|
|
19
|
+
Fiber[key] = val
|
|
20
|
+
end
|
|
21
|
+
else
|
|
22
|
+
def self.fiber_local_get(key)
|
|
23
|
+
Thread.current[key]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.fiber_local_set(key, val)
|
|
27
|
+
Thread.current[key] = val
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
private_class_method :fiber_local_get, :fiber_local_set
|
|
31
|
+
|
|
32
|
+
# Returns the current Fiber-local default endianness (+true+ for big endian).
|
|
33
|
+
def self.big
|
|
34
|
+
val = fiber_local_get(:libbin_big)
|
|
35
|
+
val.nil? ? DEFAULT_BIG : val
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Sets the current Fiber-local default endianness (+nil+ resets to host endianness).
|
|
39
|
+
def self.big=(val)
|
|
40
|
+
fiber_local_set(:libbin_big, val.nil? ? nil : !!val)
|
|
41
|
+
val
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Returns the current Fiber-local error output stream (defaults to +$stderr+).
|
|
45
|
+
def self.output
|
|
46
|
+
val = fiber_local_get(:libbin_output)
|
|
47
|
+
return $stderr if val.nil?
|
|
48
|
+
return nil if val.equal?(OUTPUT_SILENT)
|
|
49
|
+
val
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Sets the current Fiber-local error output stream (+nil+ silences reporting).
|
|
53
|
+
def self.output=(val)
|
|
54
|
+
stored = val.nil? ? OUTPUT_SILENT : (val.equal?($stderr) ? nil : val)
|
|
55
|
+
fiber_local_set(:libbin_output, stored)
|
|
56
|
+
val
|
|
14
57
|
end
|
|
15
58
|
|
|
16
|
-
# Returns true the default endianness is big
|
|
59
|
+
# Returns true if the current Fiber-local default endianness is big endian.
|
|
17
60
|
def self.default_big?
|
|
18
|
-
|
|
61
|
+
big
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Temporarily overrides {LibBin.big} for the duration of the given block.
|
|
65
|
+
def self.with_endian(endian_big)
|
|
66
|
+
prev = fiber_local_get(:libbin_big)
|
|
67
|
+
self.big = endian_big
|
|
68
|
+
yield
|
|
69
|
+
ensure
|
|
70
|
+
fiber_local_set(:libbin_big, prev)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Temporarily overrides {LibBin.output} for the duration of the given block.
|
|
74
|
+
def self.with_output(io)
|
|
75
|
+
prev = fiber_local_get(:libbin_output)
|
|
76
|
+
self.output = io
|
|
77
|
+
yield
|
|
78
|
+
ensure
|
|
79
|
+
fiber_local_set(:libbin_output, prev)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Base exception raised for binary parsing, validation, alignment, and layout errors.
|
|
83
|
+
class Error < RuntimeError
|
|
84
|
+
attr_reader :structure, :field, :offset, :index
|
|
85
|
+
|
|
86
|
+
def initialize(message = nil, structure: nil, field: nil, offset: nil, index: nil)
|
|
87
|
+
@structure = structure
|
|
88
|
+
@field = field
|
|
89
|
+
@offset = offset
|
|
90
|
+
@index = index
|
|
91
|
+
super(message)
|
|
92
|
+
end
|
|
19
93
|
end
|
|
20
94
|
|
|
95
|
+
# @!parse
|
|
96
|
+
# # Raised when reading or skipping past the end of an input stream or buffer.
|
|
97
|
+
# class EOFError < Error; end
|
|
98
|
+
#
|
|
99
|
+
# # Raised when an +expect:+ assertion fails during load, dump, or convert.
|
|
100
|
+
# class ValidationError < Error; end
|
|
101
|
+
#
|
|
102
|
+
# # Raised when an alignment constraint is invalid (e.g. non-power-of-two).
|
|
103
|
+
# class AlignmentError < Error; end
|
|
104
|
+
|
|
21
105
|
class Structure
|
|
22
106
|
|
|
107
|
+
def self.set_fields_array(arr)
|
|
108
|
+
frozen = arr.dup.freeze
|
|
109
|
+
if defined?(Ractor) && frozen.all? { |f| Ractor.shareable?(f) }
|
|
110
|
+
Ractor.make_shareable(frozen)
|
|
111
|
+
end
|
|
112
|
+
@fields = frozen
|
|
113
|
+
end
|
|
114
|
+
private_class_method :set_fields_array
|
|
115
|
+
|
|
116
|
+
set_fields_array([])
|
|
117
|
+
|
|
23
118
|
# @!parse
|
|
24
119
|
# attr_accessor :__parent
|
|
25
120
|
# attr_accessor :__index
|
|
@@ -49,7 +144,29 @@ module LibBin
|
|
|
49
144
|
|
|
50
145
|
# @!visibility private
|
|
51
146
|
def self.inherited(subclass)
|
|
52
|
-
subclass.
|
|
147
|
+
subclass.send(:set_fields_array, @fields ? @fields : [])
|
|
148
|
+
subclass.instance_variable_set(:@stride, defined?(@stride) ? @stride : nil)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @!visibility private
|
|
152
|
+
def self.include(*mods)
|
|
153
|
+
res = super
|
|
154
|
+
__bump_override_gen
|
|
155
|
+
res
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# @!visibility private
|
|
159
|
+
def self.prepend(*mods)
|
|
160
|
+
res = super
|
|
161
|
+
__bump_override_gen
|
|
162
|
+
res
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# @!visibility private
|
|
166
|
+
def self.extend(*mods)
|
|
167
|
+
res = super
|
|
168
|
+
__bump_override_gen
|
|
169
|
+
res
|
|
53
170
|
end
|
|
54
171
|
|
|
55
172
|
# Returns the size of the structure
|
|
@@ -75,15 +192,49 @@ module LibBin
|
|
|
75
192
|
# @return align
|
|
76
193
|
def self.set_always_align(align)
|
|
77
194
|
if align == true
|
|
78
|
-
@always_align = @fields.collect(&:align).select { |v| v }.max
|
|
79
|
-
@always_align = 0 unless align
|
|
195
|
+
@always_align = @fields.collect(&:align).select { |v| v }.max || 0
|
|
80
196
|
else
|
|
81
|
-
|
|
197
|
+
if align && (!align.kind_of?(Integer) || align <= 0 || (align - 1) & align != 0)
|
|
198
|
+
raise LibBin::AlignmentError, "alignment must be a power of 2"
|
|
199
|
+
end
|
|
82
200
|
@always_align = align
|
|
83
201
|
end
|
|
84
202
|
align
|
|
85
203
|
end
|
|
86
204
|
|
|
205
|
+
STRIDE_UNSET = Object.new.freeze
|
|
206
|
+
private_constant :STRIDE_UNSET
|
|
207
|
+
|
|
208
|
+
# Set or get the minimum fixed stride (byte size) for the structure.
|
|
209
|
+
# @param val [Integer, String, Proc, nil] fixed byte stride or expression
|
|
210
|
+
# @return [Integer, Proc, nil]
|
|
211
|
+
def self.stride(val = STRIDE_UNSET)
|
|
212
|
+
return (defined?(@stride) ? @stride : nil) if val.equal?(STRIDE_UNSET)
|
|
213
|
+
self.stride = val
|
|
214
|
+
self
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# Set the fixed stride (byte size) for the structure.
|
|
218
|
+
# @param val [Integer, String, Proc, nil] fixed byte stride or expression
|
|
219
|
+
# @return [Integer, Proc, nil]
|
|
220
|
+
def self.stride=(val)
|
|
221
|
+
case val
|
|
222
|
+
when nil
|
|
223
|
+
@stride = nil
|
|
224
|
+
when Integer
|
|
225
|
+
raise ArgumentError, "stride must be positive: #{val}" if val <= 0
|
|
226
|
+
@stride = val
|
|
227
|
+
when String
|
|
228
|
+
@stride = __preprocess_expression(val)
|
|
229
|
+
when Proc
|
|
230
|
+
@stride = val
|
|
231
|
+
else
|
|
232
|
+
raise TypeError, "stride must be an Integer, String, Proc, or nil"
|
|
233
|
+
end
|
|
234
|
+
Structure.__bump_override_gen
|
|
235
|
+
val
|
|
236
|
+
end
|
|
237
|
+
|
|
87
238
|
class << self
|
|
88
239
|
alias always_align= set_always_align
|
|
89
240
|
attr_reader :always_align
|
|
@@ -98,9 +249,9 @@ module LibBin
|
|
|
98
249
|
# @param length [Integer] if given, the length of the vector
|
|
99
250
|
def self.size(value, offset = 0, parent = nil, index = nil, length = nil)
|
|
100
251
|
if length
|
|
101
|
-
shape(value, offset, parent, index, length).size
|
|
252
|
+
shape(value, offset, parent, index, DataRange, length).size
|
|
102
253
|
else
|
|
103
|
-
value.__shape(offset, parent, index).size
|
|
254
|
+
value.__shape(offset, parent, index, DataRange).size
|
|
104
255
|
end
|
|
105
256
|
end
|
|
106
257
|
|
data/libbin.gemspec
CHANGED
|
@@ -1,14 +1,33 @@
|
|
|
1
|
+
require_relative 'lib/libbin/version'
|
|
2
|
+
|
|
1
3
|
Gem::Specification.new do |s|
|
|
2
4
|
s.name = 'libbin'
|
|
3
|
-
s.version =
|
|
5
|
+
s.version = LibBin::VERSION
|
|
4
6
|
s.author = "Brice Videau"
|
|
5
7
|
s.email = "brice.videau@gmail.com"
|
|
6
8
|
s.homepage = "https://github.com/kerilk/libbin"
|
|
7
9
|
s.summary = "Library for loading and converting binary files"
|
|
8
10
|
s.description = "Read, write and convert Binary data in Ruby."
|
|
9
|
-
s.files =
|
|
11
|
+
s.files = Dir[
|
|
12
|
+
'libbin.gemspec',
|
|
13
|
+
'LICENSE',
|
|
14
|
+
'README.md',
|
|
15
|
+
'CHANGELOG.md',
|
|
16
|
+
'lib/**/*.rb',
|
|
17
|
+
'ext/libbin/extconf.rb',
|
|
18
|
+
'ext/libbin/*.c',
|
|
19
|
+
'ext/libbin/*.h'
|
|
20
|
+
]
|
|
10
21
|
s.extensions << 'ext/libbin/extconf.rb'
|
|
11
22
|
s.license = 'BSD-2-Clause'
|
|
12
|
-
s.required_ruby_version = '>=
|
|
13
|
-
s.
|
|
23
|
+
s.required_ruby_version = '>= 3.1.0'
|
|
24
|
+
s.metadata = {
|
|
25
|
+
'source_code_uri' => s.homepage,
|
|
26
|
+
'documentation_uri' => 'https://kerilk.github.io/libbin/',
|
|
27
|
+
'bug_tracker_uri' => "#{s.homepage}/issues",
|
|
28
|
+
'changelog_uri' => "#{s.homepage}/blob/master/CHANGELOG.md",
|
|
29
|
+
}
|
|
30
|
+
s.add_development_dependency 'float-formats', '~> 0.3', '>= 0.3.0'
|
|
31
|
+
s.add_development_dependency 'minitest', '~> 5.0'
|
|
32
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
|
14
33
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: libbin
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.
|
|
4
|
+
version: 2.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brice Videau
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-12-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: float-formats
|
|
@@ -20,7 +20,7 @@ dependencies:
|
|
|
20
20
|
- - ">="
|
|
21
21
|
- !ruby/object:Gem::Version
|
|
22
22
|
version: 0.3.0
|
|
23
|
-
type: :
|
|
23
|
+
type: :development
|
|
24
24
|
prerelease: false
|
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
26
26
|
requirements:
|
|
@@ -30,6 +30,34 @@ dependencies:
|
|
|
30
30
|
- - ">="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
32
|
version: 0.3.0
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: minitest
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '5.0'
|
|
40
|
+
type: :development
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '5.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: rake
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '13.0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - "~>"
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '13.0'
|
|
33
61
|
description: Read, write and convert Binary data in Ruby.
|
|
34
62
|
email: brice.videau@gmail.com
|
|
35
63
|
executables: []
|
|
@@ -37,7 +65,9 @@ extensions:
|
|
|
37
65
|
- ext/libbin/extconf.rb
|
|
38
66
|
extra_rdoc_files: []
|
|
39
67
|
files:
|
|
68
|
+
- CHANGELOG.md
|
|
40
69
|
- LICENSE
|
|
70
|
+
- README.md
|
|
41
71
|
- ext/libbin/data_types.c
|
|
42
72
|
- ext/libbin/data_types.h
|
|
43
73
|
- ext/libbin/extconf.rb
|
|
@@ -46,15 +76,26 @@ files:
|
|
|
46
76
|
- ext/libbin/libbin_c.c
|
|
47
77
|
- ext/libbin/libbin_c.h
|
|
48
78
|
- ext/libbin/libbin_endian.h
|
|
79
|
+
- ext/libbin/libbin_io.h
|
|
49
80
|
- ext/libbin/pghalf.c
|
|
50
81
|
- ext/libbin/pghalf.h
|
|
51
82
|
- lib/libbin.rb
|
|
83
|
+
- lib/libbin/bitfield.rb
|
|
84
|
+
- lib/libbin/data_shape.rb
|
|
52
85
|
- lib/libbin/data_types.rb
|
|
86
|
+
- lib/libbin/enum.rb
|
|
87
|
+
- lib/libbin/union.rb
|
|
88
|
+
- lib/libbin/utf16.rb
|
|
89
|
+
- lib/libbin/version.rb
|
|
53
90
|
- libbin.gemspec
|
|
54
91
|
homepage: https://github.com/kerilk/libbin
|
|
55
92
|
licenses:
|
|
56
93
|
- BSD-2-Clause
|
|
57
|
-
metadata:
|
|
94
|
+
metadata:
|
|
95
|
+
source_code_uri: https://github.com/kerilk/libbin
|
|
96
|
+
documentation_uri: https://kerilk.github.io/libbin/
|
|
97
|
+
bug_tracker_uri: https://github.com/kerilk/libbin/issues
|
|
98
|
+
changelog_uri: https://github.com/kerilk/libbin/blob/master/CHANGELOG.md
|
|
58
99
|
post_install_message:
|
|
59
100
|
rdoc_options: []
|
|
60
101
|
require_paths:
|
|
@@ -63,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
63
104
|
requirements:
|
|
64
105
|
- - ">="
|
|
65
106
|
- !ruby/object:Gem::Version
|
|
66
|
-
version:
|
|
107
|
+
version: 3.1.0
|
|
67
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
109
|
requirements:
|
|
69
110
|
- - ">="
|