libbin 1.0.8 → 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.
data/lib/libbin.rb CHANGED
@@ -1,49 +1,266 @@
1
- require "libbin_c.so"
1
+ require "libbin_c"
2
2
 
3
- require_relative 'libbin/alignment'
3
+ require_relative 'libbin/version'
4
4
  require_relative 'libbin/data_types'
5
5
 
6
+ # Container module, handles the global state default endianness and error output.
6
7
  module LibBin
7
8
 
8
- BIG_ARCHITECTURE = [0x12345678].pack("i") == "\x12\x34\x56\x78"
9
- @__big = nil
10
- @__output = $stderr
9
+ DEFAULT_BIG = ([0x12345678].pack("i") == "\x12\x34\x56\x78").freeze
10
+ OUTPUT_SILENT = :__libbin_output_silent
11
+ private_constant :DEFAULT_BIG, :OUTPUT_SILENT
11
12
 
12
- class << self
13
- attr_accessor :__output
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
14
29
  end
30
+ private_class_method :fiber_local_get, :fiber_local_set
15
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
57
+ end
58
+
59
+ # Returns true if the current Fiber-local default endianness is big endian.
16
60
  def self.default_big?
17
- if @__big.nil?
18
- BIG_ARCHITECTURE
19
- else
20
- @__big
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)
21
92
  end
22
93
  end
23
94
 
24
- class DataConverter
25
- include Alignment
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
+
105
+ class Structure
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
+
118
+ # @!parse
119
+ # attr_accessor :__parent
120
+ # attr_accessor :__index
121
+ # attr_accessor :__position
122
+ # attr_accessor :__cur_position
123
+ # attr_accessor :__input
124
+ # attr_accessor :__output
125
+ # attr_accessor :__input_big
126
+ # attr_accessor :__output_big
127
+ #
128
+ # attr_accessor :__offset
129
+ # attr_accessor :__condition
130
+ # attr_accessor :__type
131
+ # attr_accessor :__length
132
+ # attr_accessor :__count
133
+ # attr_accessor :__iterator
134
+ # attr_accessor :__value
135
+ #
136
+ # # @method __dump_fields
137
+ # # Dump fields according to the internal state of +self+
138
+ # # @return [Structure] self
26
139
 
140
+ # @!visibility private
27
141
  def inspect
28
142
  to_s
29
143
  end
30
144
 
145
+ # @!visibility private
31
146
  def self.inherited(subclass)
32
- subclass.instance_variable_set(:@fields, [])
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
33
156
  end
34
157
 
35
- def __size(previous_offset = 0, parent = nil, index = nil)
36
- __shape(previous_offset, parent, index, DataRange).size
158
+ # @!visibility private
159
+ def self.prepend(*mods)
160
+ res = super
161
+ __bump_override_gen
162
+ res
37
163
  end
38
164
 
39
- def self.size(value, previous_offset = 0, parent = nil, index = nil, length = nil)
165
+ # @!visibility private
166
+ def self.extend(*mods)
167
+ res = super
168
+ __bump_override_gen
169
+ res
170
+ end
171
+
172
+ # Returns the size of the structure
173
+ # @param offset [Integer] position in the stream
174
+ # @param parent [Structure] if given, parent structure
175
+ # @param index [Integer] index if part of a repeated field inside parent
176
+ # @return [Integer] size of the structure
177
+ def __size(offset = 0, parent = nil, index = nil)
178
+ __shape(offset, parent, index, DataRange).size
179
+ end
180
+
181
+ # Returns the alignement of the structure
182
+ # @return [Integer] alignment of the structure
183
+ def self.align
184
+ return @always_align if @always_align
185
+ align = @fields.collect(&:align).select { |v| v }.max
186
+ align = 0 unless align
187
+ align
188
+ end
189
+
190
+ # Set the structure as needing to always be aligned
191
+ # @param align [true, Integer] if true use the fields' maximum alignment
192
+ # @return align
193
+ def self.set_always_align(align)
194
+ if align == true
195
+ @always_align = @fields.collect(&:align).select { |v| v }.max || 0
196
+ else
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
200
+ @always_align = align
201
+ end
202
+ align
203
+ end
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
+
238
+ class << self
239
+ alias always_align= set_always_align
240
+ attr_reader :always_align
241
+ attr_reader :fields
242
+ end
243
+
244
+ # Returns the size of a structure
245
+ # @param value [Structure,Array<Structure>] field or array of field to get the size of
246
+ # @param offset [Integer] position in the stream
247
+ # @param parent [Structure] if given, parent structure
248
+ # @param index [Integer] index if part of a repeated field inside parent
249
+ # @param length [Integer] if given, the length of the vector
250
+ def self.size(value, offset = 0, parent = nil, index = nil, length = nil)
40
251
  if length
41
- shape(value, previous_offset, parent, index, length).size
252
+ shape(value, offset, parent, index, DataRange, length).size
42
253
  else
43
- value.__shape(previous_offset, parent, index).size
254
+ value.__shape(offset, parent, index, DataRange).size
44
255
  end
45
256
  end
46
257
 
258
+ # Return the structure class fields
259
+ # @return [Array<Field>]
260
+ def __fields
261
+ return self.class.fields
262
+ end
263
+
47
264
  end
48
265
 
49
266
  end
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 = "1.0.8"
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 = Dir[ 'libbin.gemspec', 'LICENSE', 'lib/**/*.rb', 'ext/libbin/extconf.rb', 'ext/libbin/*.c', 'ext/libbin/*.h' ]
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 = '>= 2.0.0'
13
- s.add_dependency 'float-formats', '~> 0.3', '>=0.3.0'
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: 1.0.8
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: 2021-11-01 00:00:00.000000000 Z
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: :runtime
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,16 +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
52
- - lib/libbin/alignment.rb
83
+ - lib/libbin/bitfield.rb
84
+ - lib/libbin/data_shape.rb
53
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
54
90
  - libbin.gemspec
55
91
  homepage: https://github.com/kerilk/libbin
56
92
  licenses:
57
93
  - BSD-2-Clause
58
- 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
59
99
  post_install_message:
60
100
  rdoc_options: []
61
101
  require_paths:
@@ -64,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
104
  requirements:
65
105
  - - ">="
66
106
  - !ruby/object:Gem::Version
67
- version: 2.0.0
107
+ version: 3.1.0
68
108
  required_rubygems_version: !ruby/object:Gem::Requirement
69
109
  requirements:
70
110
  - - ">="
@@ -1,14 +0,0 @@
1
- module LibBin
2
-
3
- module Alignment
4
-
5
- def align(val, alignment)
6
- remainder = val % alignment
7
- val += alignment - remainder if remainder > 0
8
- val
9
- end
10
- private :align
11
-
12
- end
13
-
14
- end