bindata 2.4.2 → 2.4.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bindata might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ChangeLog.rdoc +4 -0
- data/lib/bindata.rb +1 -0
- data/lib/bindata/uint8_array.rb +62 -0
- data/lib/bindata/version.rb +1 -1
- data/test/uint8_array_test.rb +44 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dd9ce2f3336fc2e9f8e3250a278732351d6a1af220603fb62763b3ef83caaf5e
|
4
|
+
data.tar.gz: f8f15a3836564c6a51891231a276ed0848cc92bdb4663c5aca6d4eecd0f53ec4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5abcc6829c6b6ea410bc3c542d4b60d40a83e6ac883bb3a4b09dfefdc1e5a4ae1d37307d75e53cbbc1593a17dd327269974835a6f0936f2ab21f66fe69f120ba
|
7
|
+
data.tar.gz: ba8d7abcaac0ceb95acb6ffa8b24b7253495241fbd483ef887103a63d5c65c9649954a1998a70289e64823d0fb85bef1ba22a5bfb126b1cae9708a7412658220
|
data/ChangeLog.rdoc
CHANGED
data/lib/bindata.rb
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "bindata/base_primitive"
|
2
|
+
|
3
|
+
module BinData
|
4
|
+
# Uint8Array is a specialised type of array that only contains
|
5
|
+
# bytes (Uint8). It is a faster and more memory efficient version
|
6
|
+
# of `BinData::Array.new(:type => :uint8)`.
|
7
|
+
#
|
8
|
+
# require 'bindata'
|
9
|
+
#
|
10
|
+
# obj = BinData::Uint8Array.new(initial_length: 5)
|
11
|
+
# obj.read("abcdefg") #=> [97, 98, 99, 100, 101]
|
12
|
+
# obj[2] #=> 99
|
13
|
+
# obj.collect { |x| x.chr }.join #=> "abcde"
|
14
|
+
#
|
15
|
+
# == Parameters
|
16
|
+
#
|
17
|
+
# Parameters may be provided at initialisation to control the behaviour of
|
18
|
+
# an object. These params are:
|
19
|
+
#
|
20
|
+
# <tt>:initial_length</tt>:: The initial length of the array.
|
21
|
+
# <tt>:read_until</tt>:: May only have a value of `:eof`. This parameter
|
22
|
+
# instructs the array to read as much data from
|
23
|
+
# the stream as possible.
|
24
|
+
class Uint8Array < BinData::BasePrimitive
|
25
|
+
optional_parameters :initial_length, :read_until
|
26
|
+
mutually_exclusive_parameters :initial_length, :read_until
|
27
|
+
arg_processor :uint8_array
|
28
|
+
|
29
|
+
#---------------
|
30
|
+
private
|
31
|
+
|
32
|
+
def value_to_binary_string(val)
|
33
|
+
val.pack("C*")
|
34
|
+
end
|
35
|
+
|
36
|
+
def read_and_return_value(io)
|
37
|
+
if has_parameter?(:initial_length)
|
38
|
+
data = io.readbytes(eval_parameter(:initial_length))
|
39
|
+
else
|
40
|
+
data = io.read_all_bytes
|
41
|
+
end
|
42
|
+
|
43
|
+
data.unpack("C*")
|
44
|
+
end
|
45
|
+
|
46
|
+
def sensible_default
|
47
|
+
[]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Uint8ArrayArgProcessor < BaseArgProcessor
|
52
|
+
def sanitize_parameters!(obj_class, params) #:nodoc:
|
53
|
+
# ensure one of :initial_length and :read_until exists
|
54
|
+
unless params.has_at_least_one_of?(:initial_length, :read_until)
|
55
|
+
params[:initial_length] = 0
|
56
|
+
end
|
57
|
+
|
58
|
+
msg = "Parameter :read_until must have a value of :eof"
|
59
|
+
params.sanitize(:read_until) { |val| raise ArgumentError, msg unless val == :eof }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/bindata/version.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
|
4
|
+
|
5
|
+
describe BinData::Uint8Array, "when initialising" do
|
6
|
+
it "with mutually exclusive parameters :initial_length and :read_until" do
|
7
|
+
params = {initial_length: 5, read_until: :eof}
|
8
|
+
lambda { BinData::Uint8Array.new(params) }.must_raise ArgumentError
|
9
|
+
end
|
10
|
+
|
11
|
+
it "with :read_until" do
|
12
|
+
params = {read_until: :not_eof}
|
13
|
+
lambda { BinData::Uint8Array.new(params) }.must_raise ArgumentError
|
14
|
+
end
|
15
|
+
|
16
|
+
it "with no parameters" do
|
17
|
+
arr = BinData::Uint8Array.new
|
18
|
+
arr.num_bytes.must_equal 0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe BinData::Uint8Array, "with :read_until" do
|
23
|
+
it "reads until :eof" do
|
24
|
+
arr = BinData::Uint8Array.new(read_until: :eof)
|
25
|
+
arr.read("\xff\xfe\xfd\xfc")
|
26
|
+
arr.must_equal([255, 254, 253, 252])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe BinData::Uint8Array, "with :initial_length" do
|
31
|
+
it "reads" do
|
32
|
+
arr = BinData::Uint8Array.new(initial_length: 3)
|
33
|
+
arr.read("\xff\xfe\xfd\xfc")
|
34
|
+
arr.must_equal([255, 254, 253])
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe BinData::Uint8Array, "when assigning" do
|
39
|
+
it "copies data" do
|
40
|
+
arr = BinData::Uint8Array.new
|
41
|
+
arr.assign([1, 2, 3])
|
42
|
+
arr.must_equal([1, 2, 3])
|
43
|
+
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bindata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.4.
|
4
|
+
version: 2.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dion Mendel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/bindata/stringz.rb
|
112
112
|
- lib/bindata/struct.rb
|
113
113
|
- lib/bindata/trace.rb
|
114
|
+
- lib/bindata/uint8_array.rb
|
114
115
|
- lib/bindata/version.rb
|
115
116
|
- lib/bindata/virtual.rb
|
116
117
|
- lib/bindata/warnings.rb
|
@@ -139,6 +140,7 @@ files:
|
|
139
140
|
- test/struct_test.rb
|
140
141
|
- test/system_test.rb
|
141
142
|
- test/test_helper.rb
|
143
|
+
- test/uint8_array_test.rb
|
142
144
|
- test/virtual_test.rb
|
143
145
|
- test/warnings_test.rb
|
144
146
|
homepage: http://github.com/dmendel/bindata
|