kaitai-struct 0.5 → 0.6
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/.gitignore +1 -1
- data/lib/kaitai/struct/struct.rb +47 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb2b4ae17b2b8d26cecfe15e7e9ec6855be0d6f9
|
4
|
+
data.tar.gz: 9a1b9b1339dacda683de54c419522f093f7694e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b93c930991c6978f41f16d74d0c273fba39142df82ef78a51497785b8fdee61e921a01d5e02e75373cde64556649c6e623897170fb5b6da9422e2a23ab54bfdd
|
7
|
+
data.tar.gz: 592c8dff971cd6d44ff65102f900b8a02c7f32982a1fd2576ab9ab68f64a9a4192c7bc98387dfa19b69acd32c43cbea2be7eb0309710bf667d1d42c681d915f6
|
data/.gitignore
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
*.gem
|
data/lib/kaitai/struct/struct.rb
CHANGED
@@ -3,7 +3,7 @@ require 'stringio'
|
|
3
3
|
module Kaitai
|
4
4
|
module Struct
|
5
5
|
|
6
|
-
VERSION = '0.
|
6
|
+
VERSION = '0.6'
|
7
7
|
|
8
8
|
##
|
9
9
|
# Common base class for all structured generated by Kaitai Struct.
|
@@ -99,6 +99,7 @@ class Stream
|
|
99
99
|
else
|
100
100
|
raise TypeError.new('can be initialized with IO or String only')
|
101
101
|
end
|
102
|
+
align_to_byte
|
102
103
|
end
|
103
104
|
|
104
105
|
##
|
@@ -274,6 +275,45 @@ class Stream
|
|
274
275
|
read_bytes(8).unpack('E')[0]
|
275
276
|
end
|
276
277
|
|
278
|
+
# ========================================================================
|
279
|
+
# Unaligned bit values
|
280
|
+
# ========================================================================
|
281
|
+
|
282
|
+
def align_to_byte
|
283
|
+
@bits_left = 0
|
284
|
+
@bits = 0
|
285
|
+
end
|
286
|
+
|
287
|
+
def read_bits_int(n)
|
288
|
+
bits_needed = n - @bits_left
|
289
|
+
if bits_needed > 0
|
290
|
+
# 1 bit => 1 byte
|
291
|
+
# 8 bits => 1 byte
|
292
|
+
# 9 bits => 2 bytes
|
293
|
+
bytes_needed = ((bits_needed - 1) / 8) + 1
|
294
|
+
buf = read_bytes(bytes_needed)
|
295
|
+
buf.each_byte { |byte|
|
296
|
+
@bits <<= 8
|
297
|
+
@bits |= byte
|
298
|
+
@bits_left += 8
|
299
|
+
}
|
300
|
+
end
|
301
|
+
|
302
|
+
# raw mask with required number of 1s, starting from lowest bit
|
303
|
+
mask = (1 << n) - 1
|
304
|
+
# shift mask to align with highest bits available in @bits
|
305
|
+
shift_bits = @bits_left - n
|
306
|
+
mask <<= shift_bits
|
307
|
+
# derive reading result
|
308
|
+
res = (@bits & mask) >> shift_bits
|
309
|
+
# clear top bits that we've just read => AND with 1s
|
310
|
+
@bits_left -= n
|
311
|
+
mask = (1 << @bits_left) - 1
|
312
|
+
@bits &= mask
|
313
|
+
|
314
|
+
res
|
315
|
+
end
|
316
|
+
|
277
317
|
# ========================================================================
|
278
318
|
# Byte arrays
|
279
319
|
# ========================================================================
|
@@ -306,18 +346,15 @@ class Stream
|
|
306
346
|
# Reads next len bytes from the stream and ensures that they match
|
307
347
|
# expected fixed byte array. If they differ, throws a
|
308
348
|
# {UnexpectedDataError} runtime exception.
|
309
|
-
# @param len [Fixnum] number of bytes to read
|
310
349
|
# @param expected [String] contents to be expected
|
311
350
|
# @return [String] read bytes as byte array, which are guaranteed to
|
312
351
|
# equal to expected
|
313
352
|
# @raise [UnexpectedDataError]
|
314
|
-
def ensure_fixed_contents(
|
315
|
-
|
316
|
-
actual =
|
317
|
-
if actual != expected
|
318
|
-
|
319
|
-
end
|
320
|
-
buf
|
353
|
+
def ensure_fixed_contents(expected)
|
354
|
+
len = expected.bytesize
|
355
|
+
actual = @_io.read(len)
|
356
|
+
raise UnexpectedDataError.new(actual, expected) if actual != expected
|
357
|
+
actual
|
321
358
|
end
|
322
359
|
|
323
360
|
# ========================================================================
|
@@ -445,7 +482,7 @@ class Stream
|
|
445
482
|
end
|
446
483
|
|
447
484
|
def self.format_hex(arr)
|
448
|
-
arr.
|
485
|
+
arr.unpack('H*')[0].gsub(/(..)/, '\1 ').chop
|
449
486
|
end
|
450
487
|
end
|
451
488
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kaitai-struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.6'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Yakshin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
Kaitai Struct is a declarative language used for describe various binary data structures, laid out in files or in memory: i.e. binary file formats, network stream packet formats, etc.
|
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
45
|
version: '0'
|
46
46
|
requirements: []
|
47
47
|
rubyforge_project:
|
48
|
-
rubygems_version: 2.5.
|
48
|
+
rubygems_version: 2.5.2
|
49
49
|
signing_key:
|
50
50
|
specification_version: 4
|
51
51
|
summary: 'Kaitai Struct: runtime library for Ruby'
|