gogyou 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.ja.md +57 -0
- data/LICENSE +1 -1
- data/README.md +129 -44
- data/Rakefile +4 -1
- data/gemstub.rb +2 -2
- data/lib/gogyou.rb +66 -40
- data/lib/gogyou/accessor.rb +80 -57
- data/lib/gogyou/bytearray.rb +856 -0
- data/lib/gogyou/extensions.rb +477 -0
- data/lib/gogyou/ffi.rb +52 -0
- data/lib/gogyou/fiddle.rb +53 -0
- data/lib/gogyou/primitives.rb +160 -52
- data/lib/gogyou/typespec.rb +1 -1
- data/lib/gogyou/version.rb +1 -1
- data/mkprims.rb +74 -38
- data/test/test_loadstore.rb +65 -0
- data/test/test_packbin.rb +65 -0
- metadata +21 -8
- data/lib/gogyou/mixin.rb +0 -640
@@ -0,0 +1,65 @@
|
|
1
|
+
#!ruby
|
2
|
+
|
3
|
+
require "test-unit"
|
4
|
+
require "gogyou"
|
5
|
+
|
6
|
+
class TestLoadStore < Test::Unit::TestCase
|
7
|
+
@@str = [0, 0, 0, 0, 0, 0, 0, 0, 160, 164, 221, 186, 248, 213, 191, 215, 137, 242, 225, 169, 153, 224, 148, 134].pack("C*")
|
8
|
+
|
9
|
+
def test_load
|
10
|
+
16.times do |i|
|
11
|
+
assert_equal @@str.unpack("@#{i}C")[0], @@str.loadu8(i)
|
12
|
+
assert_equal @@str.unpack("@#{i}S")[0], @@str.loadu16(i)
|
13
|
+
assert_equal @@str.unpack("@#{i}L")[0], @@str.loadu32(i)
|
14
|
+
assert_equal @@str.unpack("@#{i}Q")[0], @@str.loadu64(i)
|
15
|
+
assert_equal @@str.unpack("@#{i}c")[0], @@str.loadi8(i)
|
16
|
+
assert_equal @@str.unpack("@#{i}s")[0], @@str.loadi16(i)
|
17
|
+
assert_equal @@str.unpack("@#{i}l")[0], @@str.loadi32(i)
|
18
|
+
assert_equal @@str.unpack("@#{i}q")[0], @@str.loadi64(i)
|
19
|
+
assert_equal @@str.unpack("@#{i}s!")[0], @@str.load_short(i)
|
20
|
+
assert_equal @@str.unpack("@#{i}i!")[0], @@str.load_int(i)
|
21
|
+
assert_equal @@str.unpack("@#{i}l!")[0], @@str.load_long(i)
|
22
|
+
assert_equal @@str.unpack("@#{i}q")[0], @@str.load_longlong(i)
|
23
|
+
assert_equal @@str.unpack("@#{i}S!")[0], @@str.load_ushort(i)
|
24
|
+
assert_equal @@str.unpack("@#{i}I!")[0], @@str.load_uint(i)
|
25
|
+
assert_equal @@str.unpack("@#{i}L!")[0], @@str.load_ulong(i)
|
26
|
+
assert_equal @@str.unpack("@#{i}Q")[0], @@str.load_ulonglong(i)
|
27
|
+
assert_equal @@str.unpack("@#{i}f")[0], @@str.loadf32(i)
|
28
|
+
assert_equal @@str.unpack("@#{i}d")[0], @@str.loadf64(i)
|
29
|
+
assert_equal @@str.unpack("@#{i}n")[0].unpack_binary16, @@str.loadf16be(i)
|
30
|
+
assert_equal @@str.unpack("@#{i}N")[0].unpack_binary32, @@str.loadf32be(i)
|
31
|
+
assert_equal @@str.unpack("@#{i}Q>")[0].unpack_binary64, @@str.loadf64be(i)
|
32
|
+
assert_equal @@str.unpack("@#{i}v")[0].unpack_binary16, @@str.loadf16le(i)
|
33
|
+
assert_equal @@str.unpack("@#{i}V")[0].unpack_binary32, @@str.loadf32le(i)
|
34
|
+
assert_equal @@str.unpack("@#{i}Q<")[0].unpack_binary64, @@str.loadf64le(i)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class TestSwap < Test::Unit::TestCase
|
40
|
+
def test_swap
|
41
|
+
assert_equal 0x0011, 0x1100.swap16
|
42
|
+
assert_equal 0x001122, 0x221100.swap24
|
43
|
+
assert_equal 0x00112233, 0x33221100.swap32
|
44
|
+
assert_equal 0x001122334455, 0x554433221100.swap48
|
45
|
+
assert_equal 0x0011223344556677, 0x7766554433221100.swap64
|
46
|
+
|
47
|
+
assert_equal 0xffee, 0xeeff.swap16
|
48
|
+
assert_equal 0xffeedd, 0xddeeff.swap24
|
49
|
+
assert_equal 0xffeeddcc, 0xccddeeff.swap32
|
50
|
+
assert_equal 0xffeeddccbbaa, 0xaabbccddeeff.swap48
|
51
|
+
assert_equal 0xffeeddccbbaa9988, 0x8899aabbccddeeff.swap64
|
52
|
+
|
53
|
+
assert_equal 0x0011, 0x1100.swap16s
|
54
|
+
assert_equal 0x001122, 0x221100.swap24s
|
55
|
+
assert_equal 0x00112233, 0x33221100.swap32s
|
56
|
+
assert_equal 0x001122334455, 0x554433221100.swap48s
|
57
|
+
assert_equal 0x0011223344556677, 0x7766554433221100.swap64s
|
58
|
+
|
59
|
+
assert_equal ~0x0011, 0xeeff.swap16s
|
60
|
+
assert_equal ~0x001122, 0xddeeff.swap24s
|
61
|
+
assert_equal ~0x00112233, 0xccddeeff.swap32s
|
62
|
+
assert_equal ~0x001122334455, 0xaabbccddeeff.swap48s
|
63
|
+
assert_equal ~0x0011223344556677, 0x8899aabbccddeeff.swap64s
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#!ruby
|
2
|
+
|
3
|
+
require "test-unit"
|
4
|
+
require "gogyou"
|
5
|
+
|
6
|
+
class TestBinaryPacker < Test::Unit::TestCase
|
7
|
+
def test_pack_binary16
|
8
|
+
assert_equal 0x0000, 0.0.pack_binary16
|
9
|
+
assert_equal 0x8000, -0.0.pack_binary16
|
10
|
+
assert_equal 0x3800, 0.5.pack_binary16
|
11
|
+
assert_equal 0x3c00, 1.0.pack_binary16
|
12
|
+
assert_equal 0x4000, 2.0.pack_binary16
|
13
|
+
assert_equal 0x7c00, (+Float::INFINITY).pack_binary16
|
14
|
+
assert_equal 0xfc00, (-Float::INFINITY).pack_binary16
|
15
|
+
assert_equal 0x7e00, Float::NAN.pack_binary16
|
16
|
+
assert_equal 0x7c00, 65536.pack_binary16 # infinity
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_unpack_binary16
|
20
|
+
assert_equal +Float::INFINITY, 0x7c00.unpack_binary16
|
21
|
+
assert_equal -Float::INFINITY, 0xfc00.unpack_binary16
|
22
|
+
assert_equal "NaN", 0x7e00.unpack_binary16.to_s
|
23
|
+
assert_equal "NaN", 0xfe00.unpack_binary16.to_s
|
24
|
+
assert_equal "-0.0", 0x8000.unpack_binary16.to_s
|
25
|
+
assert_equal 0.0, 0x0000.unpack_binary16
|
26
|
+
assert_equal 0.5, 0x3800.unpack_binary16
|
27
|
+
assert_equal 1.0, 0x3c00.unpack_binary16
|
28
|
+
assert_equal 2.0, 0x4000.unpack_binary16
|
29
|
+
assert_equal 0x07ff / 0x0400.to_f, 0x3fff.unpack_binary16
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_unpack_binary32
|
33
|
+
assert_equal +Float::INFINITY, 0x7f800000.unpack_binary32
|
34
|
+
assert_equal -Float::INFINITY, 0xff800000.unpack_binary32
|
35
|
+
assert_equal "NaN", 0x7fc00000.unpack_binary32.to_s
|
36
|
+
assert_equal "NaN", 0xffc00000.unpack_binary32.to_s
|
37
|
+
assert_equal "-0.0", 0x80000000.unpack_binary32.to_s
|
38
|
+
assert_equal 0.0, 0x00000000.unpack_binary32
|
39
|
+
assert_equal 0.5, 0x3f000000.unpack_binary32
|
40
|
+
assert_equal 1.0, 0x3f800000.unpack_binary32
|
41
|
+
assert_equal 2.0, 0x40000000.unpack_binary32
|
42
|
+
assert_equal 0x00ffffff / 0x00800000.to_f, 0x3fffffff.unpack_binary32
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_unpack_binary64
|
46
|
+
assert_equal +Float::INFINITY, 0x7ff0000000000000.unpack_binary64
|
47
|
+
assert_equal -Float::INFINITY, 0xfff0000000000000.unpack_binary64
|
48
|
+
assert_equal "NaN", 0x7ff8000000000000.unpack_binary64.to_s
|
49
|
+
assert_equal "NaN", 0xfff8000000000000.unpack_binary64.to_s
|
50
|
+
assert_equal "-0.0", 0x8000000000000000.unpack_binary64.to_s
|
51
|
+
assert_equal 0.0, 0x0000000000000000.unpack_binary64
|
52
|
+
assert_equal 0.5, 0x3fe0000000000000.unpack_binary64
|
53
|
+
assert_equal 1.0, 0x3ff0000000000000.unpack_binary64
|
54
|
+
assert_equal 2.0, 0x4000000000000000.unpack_binary64
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_pack_and_unpack_32
|
58
|
+
200.times do
|
59
|
+
n = [rand].pack("g").unpack("g")[0]
|
60
|
+
assert_equal n, [n].pack("g").unpack("N")[0].unpack_binary32
|
61
|
+
n = [1 / n].pack("g").unpack("g")[0]
|
62
|
+
assert_equal n, [n].pack("g").unpack("N")[0].unpack_binary32
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gogyou
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dearblue
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -48,7 +48,7 @@ description: |
|
|
48
48
|
* Available const field.
|
49
49
|
* Available packed field.
|
50
50
|
* Available user definition types.
|
51
|
-
email: dearblue@users.
|
51
|
+
email: dearblue@users.osdn.me
|
52
52
|
executables: []
|
53
53
|
extensions: []
|
54
54
|
extra_rdoc_files:
|
@@ -57,7 +57,10 @@ extra_rdoc_files:
|
|
57
57
|
- README.md
|
58
58
|
- lib/gogyou.rb
|
59
59
|
- lib/gogyou/accessor.rb
|
60
|
-
- lib/gogyou/
|
60
|
+
- lib/gogyou/bytearray.rb
|
61
|
+
- lib/gogyou/extensions.rb
|
62
|
+
- lib/gogyou/ffi.rb
|
63
|
+
- lib/gogyou/fiddle.rb
|
61
64
|
- lib/gogyou/model.rb
|
62
65
|
- lib/gogyou/primitives.rb
|
63
66
|
- lib/gogyou/typespec.rb
|
@@ -70,19 +73,28 @@ files:
|
|
70
73
|
- gemstub.rb
|
71
74
|
- lib/gogyou.rb
|
72
75
|
- lib/gogyou/accessor.rb
|
73
|
-
- lib/gogyou/
|
76
|
+
- lib/gogyou/bytearray.rb
|
77
|
+
- lib/gogyou/extensions.rb
|
78
|
+
- lib/gogyou/ffi.rb
|
79
|
+
- lib/gogyou/fiddle.rb
|
74
80
|
- lib/gogyou/model.rb
|
75
81
|
- lib/gogyou/primitives.rb
|
76
82
|
- lib/gogyou/typespec.rb
|
77
83
|
- lib/gogyou/version.rb
|
78
84
|
- mkprims.rb
|
79
85
|
- spec/gogyou_spec.rb
|
80
|
-
|
86
|
+
- test/test_loadstore.rb
|
87
|
+
- test/test_packbin.rb
|
88
|
+
homepage: https://osdn.jp/projects/rutsubo/
|
81
89
|
licenses:
|
82
90
|
- 2-clause BSD License
|
83
91
|
metadata: {}
|
84
92
|
post_install_message:
|
85
|
-
rdoc_options:
|
93
|
+
rdoc_options:
|
94
|
+
- "--charset"
|
95
|
+
- UTF-8
|
96
|
+
- "-m"
|
97
|
+
- README.md
|
86
98
|
require_paths:
|
87
99
|
- lib
|
88
100
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -97,8 +109,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
109
|
version: '0'
|
98
110
|
requirements: []
|
99
111
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.4.
|
112
|
+
rubygems_version: 2.4.8
|
101
113
|
signing_key:
|
102
114
|
specification_version: 4
|
103
115
|
summary: binary data operation library with the C liked struct and union
|
104
116
|
test_files: []
|
117
|
+
has_rdoc:
|
data/lib/gogyou/mixin.rb
DELETED
@@ -1,640 +0,0 @@
|
|
1
|
-
#vim set fileencoding:utf-8
|
2
|
-
|
3
|
-
require_relative "typespec"
|
4
|
-
|
5
|
-
module Gogyou
|
6
|
-
module Extensions
|
7
|
-
module Object
|
8
|
-
module Mixin
|
9
|
-
def infect_from(*obj)
|
10
|
-
obj.each { |o| taint if o.tainted? }
|
11
|
-
self
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class ::Object
|
17
|
-
extend Gogyou::Extensions::Object
|
18
|
-
include Gogyou::Extensions::Object::Mixin
|
19
|
-
end
|
20
|
-
|
21
|
-
UNIT_B = 1 << 0
|
22
|
-
UNIT_KiB = 1 << 10
|
23
|
-
UNIT_MiB = 1 << 20
|
24
|
-
UNIT_GiB = 1 << 30
|
25
|
-
UNIT_TiB = 1 << 40
|
26
|
-
UNIT_PiB = 1 << 50
|
27
|
-
UNIT_EiB = 1 << 60
|
28
|
-
UNIT_ZiB = 1 << 70
|
29
|
-
UNIT_YiB = 1 << 80
|
30
|
-
|
31
|
-
module Numeric
|
32
|
-
module Mixin
|
33
|
-
def B
|
34
|
-
UNIT_B * self
|
35
|
-
end
|
36
|
-
|
37
|
-
def KiB
|
38
|
-
UNIT_KiB * self
|
39
|
-
end
|
40
|
-
|
41
|
-
def MiB
|
42
|
-
UNIT_MiB * self
|
43
|
-
end
|
44
|
-
|
45
|
-
def GiB
|
46
|
-
UNIT_GiB * self
|
47
|
-
end
|
48
|
-
|
49
|
-
def TiB
|
50
|
-
UNIT_TiB * self
|
51
|
-
end
|
52
|
-
|
53
|
-
def PiB
|
54
|
-
UNIT_PiB * self
|
55
|
-
end
|
56
|
-
|
57
|
-
def EiB
|
58
|
-
UNIT_EiB * self
|
59
|
-
end
|
60
|
-
|
61
|
-
def ZiB
|
62
|
-
UNIT_ZiB * self
|
63
|
-
end
|
64
|
-
|
65
|
-
def YiB
|
66
|
-
UNIT_YiB * self
|
67
|
-
end
|
68
|
-
|
69
|
-
def unit_floor(unit)
|
70
|
-
(self / unit).to_i
|
71
|
-
end
|
72
|
-
|
73
|
-
def unit_ceil(unit)
|
74
|
-
((self + (unit - 1)) / unit).to_i
|
75
|
-
end
|
76
|
-
|
77
|
-
def align_floor(unit)
|
78
|
-
((self / unit).to_i * unit).to_i
|
79
|
-
end
|
80
|
-
|
81
|
-
def align_ceil(unit)
|
82
|
-
(((self + (unit - 1)) / unit).to_i * unit).to_i
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
class ::Numeric
|
88
|
-
extend Gogyou::Extensions::Numeric
|
89
|
-
include Gogyou::Extensions::Numeric::Mixin
|
90
|
-
end
|
91
|
-
|
92
|
-
module Integer
|
93
|
-
module_function
|
94
|
-
def bitmask(shift, bits)
|
95
|
-
~(~0 << bits) << shift
|
96
|
-
end
|
97
|
-
|
98
|
-
public_class_method :bitmask
|
99
|
-
|
100
|
-
module Mixin
|
101
|
-
def getbit(shift, bits)
|
102
|
-
(self >> shift) & Integer.bitmask(0, bits)
|
103
|
-
end
|
104
|
-
|
105
|
-
def getbits(shift, bits)
|
106
|
-
getbit(shift, bits).extendsign(bits)
|
107
|
-
end
|
108
|
-
|
109
|
-
def getbitset(*bitsize)
|
110
|
-
shift = 0
|
111
|
-
list = []
|
112
|
-
bitsize.each do |bits|
|
113
|
-
if bits > 0
|
114
|
-
list << getbit(shift, bits)
|
115
|
-
shift += bits
|
116
|
-
else
|
117
|
-
list << 0
|
118
|
-
end
|
119
|
-
end
|
120
|
-
list
|
121
|
-
end
|
122
|
-
|
123
|
-
def setbit(shift, bits, num)
|
124
|
-
mask = Integer.bitmask(shift, bits)
|
125
|
-
(self & ~mask) | ((num << shift) & mask)
|
126
|
-
end
|
127
|
-
|
128
|
-
def extendsign(bits)
|
129
|
-
n = self & Integer.bitmask(0, bits)
|
130
|
-
if (n >> (bits - 1)) == 0
|
131
|
-
n
|
132
|
-
else
|
133
|
-
n | (~0 << bits)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def swapbyte(bytesize)
|
138
|
-
num = 0
|
139
|
-
bytesize.times do |i|
|
140
|
-
num <<= 8
|
141
|
-
num |= (self >> (i * 8)) & 0xff
|
142
|
-
end
|
143
|
-
num
|
144
|
-
end
|
145
|
-
|
146
|
-
def swap16
|
147
|
-
((self >> 8) & 0xff) | ((self & 0xff) << 8)
|
148
|
-
end
|
149
|
-
|
150
|
-
def swap24
|
151
|
-
((self >> 16) & 0xff) |
|
152
|
-
(self & 0xff00) |
|
153
|
-
((self & 0xff) << 16)
|
154
|
-
end
|
155
|
-
|
156
|
-
def swap32
|
157
|
-
((self >> 24) & 0xff) |
|
158
|
-
((self >> 8) & 0xff00) |
|
159
|
-
((self & 0xff00) << 8) |
|
160
|
-
((self & 0xff) << 24)
|
161
|
-
end
|
162
|
-
|
163
|
-
def swap48
|
164
|
-
((self >> 40) & 0xff) |
|
165
|
-
((self >> 24) & 0xff00) |
|
166
|
-
((self >> 8) & 0xff0000) |
|
167
|
-
((self & 0xff0000) << 8) |
|
168
|
-
((self & 0xff00) << 24) |
|
169
|
-
((self & 0xff) << 40)
|
170
|
-
end
|
171
|
-
|
172
|
-
def swap64
|
173
|
-
((self >> 56) & 0xff) |
|
174
|
-
((self >> 40) & 0xff00) |
|
175
|
-
((self >> 24) & 0xff0000) |
|
176
|
-
((self >> 8) & 0xff000000) |
|
177
|
-
((self & 0xff000000) << 8) |
|
178
|
-
((self & 0xff0000) << 24) |
|
179
|
-
((self & 0xff00) << 40) |
|
180
|
-
((self & 0xff) << 56)
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
class ::Integer
|
186
|
-
extend Gogyou::Extensions::Integer
|
187
|
-
include Gogyou::Extensions::Integer::Mixin
|
188
|
-
end
|
189
|
-
|
190
|
-
module String
|
191
|
-
BITS_PER_BYTE = 8
|
192
|
-
|
193
|
-
module_function
|
194
|
-
def alloc(bytesize)
|
195
|
-
?\0.force_encoding(Encoding::BINARY) * bytesize
|
196
|
-
end
|
197
|
-
|
198
|
-
public :alloc
|
199
|
-
|
200
|
-
ZERO_BUFFER = alloc(64.KiB).freeze
|
201
|
-
|
202
|
-
module Mixin
|
203
|
-
def to_ptr
|
204
|
-
[self].pack("p").load_sizet(0)
|
205
|
-
end
|
206
|
-
|
207
|
-
def binary_operation
|
208
|
-
enc = encoding
|
209
|
-
force_encoding(Encoding::BINARY) rescue (enc = nil; raise)
|
210
|
-
yield
|
211
|
-
ensure
|
212
|
-
force_encoding(enc) rescue nil if enc
|
213
|
-
end
|
214
|
-
|
215
|
-
def resize(newsize)
|
216
|
-
binary_operation do
|
217
|
-
left = newsize - bytesize
|
218
|
-
case
|
219
|
-
when left > 0
|
220
|
-
while left >= ZERO_BUFFER.bytesize
|
221
|
-
concat(ZERO_BUFFER)
|
222
|
-
left -= ZERO_BUFFER.bytesize
|
223
|
-
end
|
224
|
-
concat(ZERO_BUFFER[ZERO_BUFFER.bytesize - left, left]) # make shared string
|
225
|
-
when left < 0
|
226
|
-
left = - left
|
227
|
-
self[bytesize - left, left] = ""
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
self
|
232
|
-
end
|
233
|
-
|
234
|
-
def setbinary(index, str, offset = 0, bytesize = str.bytesize)
|
235
|
-
offset = offset.to_i
|
236
|
-
bytesize = bytesize.to_i
|
237
|
-
size1 = str.bytesize - offset
|
238
|
-
bytesize = size1 if bytesize > size1
|
239
|
-
if bytesize > 0
|
240
|
-
binary_operation do
|
241
|
-
self[index.to_i, bytesize] = str.byteslice(offset, bytesize)
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
self
|
246
|
-
end
|
247
|
-
|
248
|
-
def swapbyte(index, bytesize)
|
249
|
-
i = index.to_i
|
250
|
-
j = i + bytesize.to_i - 1
|
251
|
-
while i < j
|
252
|
-
w = getbyte(i)
|
253
|
-
setbyte(i, getbyte(j))
|
254
|
-
setbyte(j, w)
|
255
|
-
i += 1
|
256
|
-
j -= 1
|
257
|
-
end
|
258
|
-
|
259
|
-
self
|
260
|
-
end
|
261
|
-
|
262
|
-
def storebe(index, num, bytesize)
|
263
|
-
raise RangeError unless index >= 0 &&
|
264
|
-
index < bytesize &&
|
265
|
-
index + bytesize <= bytesize &&
|
266
|
-
index + bytesize >= 0
|
267
|
-
while bytesize > 0
|
268
|
-
bytesize -= 1
|
269
|
-
setbyte(index, 0xff & (num >> (bytesize * BITS_PER_BYTE)))
|
270
|
-
index += 1
|
271
|
-
end
|
272
|
-
|
273
|
-
self
|
274
|
-
end
|
275
|
-
|
276
|
-
def storele(index, num, bytesize)
|
277
|
-
raise RangeError unless index >= 0 &&
|
278
|
-
index < bytesize &&
|
279
|
-
index + bytesize <= bytesize &&
|
280
|
-
index + bytesize >= 0
|
281
|
-
while bytesize > 0
|
282
|
-
bytesize -= 1
|
283
|
-
setbyte(index, 0xff & num)
|
284
|
-
num >>= BITS_PER_BYTE
|
285
|
-
index += 1
|
286
|
-
end
|
287
|
-
|
288
|
-
self
|
289
|
-
end
|
290
|
-
|
291
|
-
def loadube(index, bytesize)
|
292
|
-
n = 0
|
293
|
-
while bytesize > 0
|
294
|
-
bytesize -= 1
|
295
|
-
n <<= BITS_PER_BYTE
|
296
|
-
n |= getbyte(index)
|
297
|
-
index += 1
|
298
|
-
end
|
299
|
-
n
|
300
|
-
end
|
301
|
-
|
302
|
-
def loadibe(index, bytesize)
|
303
|
-
loadube(index, bytesize).extendsign(bytesize * BITS_PER_BYTE)
|
304
|
-
end
|
305
|
-
|
306
|
-
def loadule(index, bytesize)
|
307
|
-
n = 0
|
308
|
-
c = 0
|
309
|
-
while bytesize > 0
|
310
|
-
bytesize -= 1
|
311
|
-
n |= getbyte(index) << (BITS_PER_BYTE * c)
|
312
|
-
index += 1
|
313
|
-
c += 1
|
314
|
-
end
|
315
|
-
n
|
316
|
-
end
|
317
|
-
|
318
|
-
def loadile(index, bytesize)
|
319
|
-
loadule(index, bytesize).extendsign(bytesize * BITS_PER_BYTE)
|
320
|
-
end
|
321
|
-
|
322
|
-
def loadu8(index)
|
323
|
-
getbyte(index)
|
324
|
-
end
|
325
|
-
|
326
|
-
def loadi8(index)
|
327
|
-
getbyte(index).extendsign(8)
|
328
|
-
end
|
329
|
-
|
330
|
-
def loadu16be(index)
|
331
|
-
(getbyte(index) << 8) | getbyte(index + 1)
|
332
|
-
end
|
333
|
-
|
334
|
-
def loadi16be(index)
|
335
|
-
loadu16be(index).extendsign(16)
|
336
|
-
end
|
337
|
-
|
338
|
-
def loadu16le(index)
|
339
|
-
getbyte(index) | (getbyte(index + 1) << 8)
|
340
|
-
end
|
341
|
-
|
342
|
-
def loadi16le(index)
|
343
|
-
loadu16le(index).extendsign(16)
|
344
|
-
end
|
345
|
-
|
346
|
-
def loadu24be(index)
|
347
|
-
(getbyte(index) << 16) | (getbyte(index + 1) << 8) | getbyte(index + 2)
|
348
|
-
end
|
349
|
-
|
350
|
-
def loadi24be(index)
|
351
|
-
loadu24be(index).extendsign(24)
|
352
|
-
end
|
353
|
-
|
354
|
-
def loadu24le(index)
|
355
|
-
getbyte(index) | (getbyte(index + 1) << 8) | (getbyte(index + 2) << 16)
|
356
|
-
end
|
357
|
-
|
358
|
-
def loadi24le(index)
|
359
|
-
loadu24le(index).extendsign(24)
|
360
|
-
end
|
361
|
-
|
362
|
-
def loadu32be(index)
|
363
|
-
(getbyte(index) << 24) | (getbyte(index + 1) << 16) | (getbyte(index + 2) << 8) | getbyte(index + 3)
|
364
|
-
end
|
365
|
-
|
366
|
-
def loadi32be(index)
|
367
|
-
loadu32be(index).extendsign(32)
|
368
|
-
end
|
369
|
-
|
370
|
-
def loadu32le(index)
|
371
|
-
getbyte(index) | (getbyte(index + 1) << 8) | (getbyte(index + 2) << 16) | (getbyte(index + 3) << 24)
|
372
|
-
end
|
373
|
-
|
374
|
-
def loadi32le(index)
|
375
|
-
loadu32le(index).extendsign(32)
|
376
|
-
end
|
377
|
-
|
378
|
-
def loadu48be(index)
|
379
|
-
(loadu24be(index) << 24) | loadu24be(index + 3)
|
380
|
-
end
|
381
|
-
|
382
|
-
def loadi48be(index)
|
383
|
-
loadu48be(index).extendsign(48)
|
384
|
-
end
|
385
|
-
|
386
|
-
def loadu48le(index)
|
387
|
-
loadu24le(index) | (loadu24le(index + 3) << 24)
|
388
|
-
end
|
389
|
-
|
390
|
-
def loadi48le(index)
|
391
|
-
loadu48le(index).extendsign(48)
|
392
|
-
end
|
393
|
-
|
394
|
-
def loadu64be(index)
|
395
|
-
(loadu32be(index) << 32) | loadu32be(index + 4)
|
396
|
-
end
|
397
|
-
|
398
|
-
def loadi64be(index)
|
399
|
-
loadu64be(index).extendsign(64)
|
400
|
-
end
|
401
|
-
|
402
|
-
def loadu64le(index)
|
403
|
-
loadu32le(index) | (loadu32le(index + 4) << 32)
|
404
|
-
end
|
405
|
-
|
406
|
-
def loadi64le(index)
|
407
|
-
loadu64le(index).extendsign(64)
|
408
|
-
end
|
409
|
-
|
410
|
-
def loadf32be(index)
|
411
|
-
unpack("@#{index.to_i}g")[0]
|
412
|
-
end
|
413
|
-
|
414
|
-
def loadf32le(index)
|
415
|
-
unpack("@#{index.to_i}e")[0]
|
416
|
-
end
|
417
|
-
|
418
|
-
def loadf64be(index)
|
419
|
-
unpack("@#{index.to_i}G")[0]
|
420
|
-
end
|
421
|
-
|
422
|
-
def loadf64le(index)
|
423
|
-
unpack("@#{index.to_i}E")[0]
|
424
|
-
end
|
425
|
-
|
426
|
-
def store8(index, num)
|
427
|
-
setbyte(index.to_i, num.to_i & 0xff)
|
428
|
-
end
|
429
|
-
|
430
|
-
def store16be(index, num)
|
431
|
-
store8(index, num >> 8)
|
432
|
-
store8(index + 1, num)
|
433
|
-
end
|
434
|
-
|
435
|
-
def store16le(index, num)
|
436
|
-
store8(index, num)
|
437
|
-
store8(index + 1, num >> 8)
|
438
|
-
end
|
439
|
-
|
440
|
-
def store24be(index, num)
|
441
|
-
store8(index, num >> 16)
|
442
|
-
store8(index + 1, num >> 8)
|
443
|
-
store8(index + 2, num)
|
444
|
-
end
|
445
|
-
|
446
|
-
def store24le(index, num)
|
447
|
-
store8(index, num)
|
448
|
-
store8(index + 1, num >> 8)
|
449
|
-
store8(index + 2, num >> 16)
|
450
|
-
end
|
451
|
-
|
452
|
-
def store32be(index, num)
|
453
|
-
store8(index, num >> 24)
|
454
|
-
store8(index + 1, num >> 16)
|
455
|
-
store8(index + 2, num >> 8)
|
456
|
-
store8(index + 3, num)
|
457
|
-
end
|
458
|
-
|
459
|
-
def store32le(index, num)
|
460
|
-
store8(index, num)
|
461
|
-
store8(index + 1, num >> 8)
|
462
|
-
store8(index + 2, num >> 16)
|
463
|
-
store8(index + 3, num >> 24)
|
464
|
-
end
|
465
|
-
|
466
|
-
def store48be(index, num)
|
467
|
-
store24be(index, num >> 24)
|
468
|
-
store24be(index + 3, num)
|
469
|
-
end
|
470
|
-
|
471
|
-
def store48le(index, num)
|
472
|
-
store24le(index, num)
|
473
|
-
store24le(index + 3, num >> 24)
|
474
|
-
end
|
475
|
-
|
476
|
-
def store64be(index, num)
|
477
|
-
store32be(index, num >> 32)
|
478
|
-
store32be(index + 4, num)
|
479
|
-
end
|
480
|
-
|
481
|
-
def store64le(index, num)
|
482
|
-
store32le(index, num)
|
483
|
-
store32le(index + 4, num >> 32)
|
484
|
-
end
|
485
|
-
|
486
|
-
def storef32be(index, num)
|
487
|
-
setbinary(index, [num].pack("g"))
|
488
|
-
end
|
489
|
-
|
490
|
-
def storef32le(index, num)
|
491
|
-
setbinary(index, [num].pack("e"))
|
492
|
-
end
|
493
|
-
|
494
|
-
def storef64be(index, num)
|
495
|
-
setbinary(index, [num].pack("G"))
|
496
|
-
end
|
497
|
-
|
498
|
-
def storef64le(index, num)
|
499
|
-
setbinary(index, [num].pack("E"))
|
500
|
-
end
|
501
|
-
|
502
|
-
#---
|
503
|
-
## native byte order operations
|
504
|
-
#+++
|
505
|
-
|
506
|
-
def loadf32(index)
|
507
|
-
unpack("@#{index.to_i}f")[0]
|
508
|
-
end
|
509
|
-
|
510
|
-
def loadf64(index)
|
511
|
-
unpack("@#{index.to_i}d")[0]
|
512
|
-
end
|
513
|
-
|
514
|
-
def storef32(index, num)
|
515
|
-
setbinary(index, [num].pack("f"))
|
516
|
-
end
|
517
|
-
|
518
|
-
def storef64(index, num)
|
519
|
-
setbinary(index, [num].pack("d"))
|
520
|
-
end
|
521
|
-
|
522
|
-
case "\0\1\2\3".unpack("I")[0]
|
523
|
-
when 0x00010203 # big endian (network byte order)
|
524
|
-
alias store storebe
|
525
|
-
alias store16 store16be
|
526
|
-
alias store24 store24be
|
527
|
-
alias store32 store32be
|
528
|
-
alias store48 store48be
|
529
|
-
alias store64 store64be
|
530
|
-
alias loadi loadibe
|
531
|
-
alias loadi16 loadi16be
|
532
|
-
alias loadi24 loadi24be
|
533
|
-
alias loadi32 loadi32be
|
534
|
-
alias loadi48 loadi48be
|
535
|
-
alias loadi64 loadi64be
|
536
|
-
alias loadu loadube
|
537
|
-
alias loadu16 loadu16be
|
538
|
-
alias loadu24 loadu24be
|
539
|
-
alias loadu32 loadu32be
|
540
|
-
alias loadu48 loadu48be
|
541
|
-
alias loadu64 loadu64be
|
542
|
-
|
543
|
-
alias storeswap storele
|
544
|
-
alias store16swap store16le
|
545
|
-
alias store24swap store24le
|
546
|
-
alias store32swap store32le
|
547
|
-
alias store48swap store48le
|
548
|
-
alias store64swap store64le
|
549
|
-
alias loadiswap loadile
|
550
|
-
alias loadi16swap loadi16le
|
551
|
-
alias loadi24swap loadi24le
|
552
|
-
alias loadi32swap loadi32le
|
553
|
-
alias loadi48swap loadi48le
|
554
|
-
alias loadi64swap loadi64le
|
555
|
-
alias loaduswap loadule
|
556
|
-
alias loadu16swap loadu16le
|
557
|
-
alias loadu24swap loadu24le
|
558
|
-
alias loadu32swap loadu32le
|
559
|
-
alias loadu48swap loadu48le
|
560
|
-
alias loadu64swap loadu64le
|
561
|
-
alias loadf32swap loadf32le
|
562
|
-
alias loadf64swap loadf64le
|
563
|
-
when 0x03020100 # little endian (vax byte order)
|
564
|
-
alias store storele
|
565
|
-
alias store16 store16le
|
566
|
-
alias store24 store24le
|
567
|
-
alias store32 store32le
|
568
|
-
alias store48 store48le
|
569
|
-
alias store64 store64le
|
570
|
-
alias loadi loadile
|
571
|
-
alias loadi16 loadi16le
|
572
|
-
alias loadi24 loadi24le
|
573
|
-
alias loadi32 loadi32le
|
574
|
-
alias loadi48 loadi48le
|
575
|
-
alias loadi64 loadi64le
|
576
|
-
alias loadu loadule
|
577
|
-
alias loadu16 loadu16le
|
578
|
-
alias loadu24 loadu24le
|
579
|
-
alias loadu32 loadu32le
|
580
|
-
alias loadu48 loadu48le
|
581
|
-
alias loadu64 loadu64le
|
582
|
-
|
583
|
-
alias storeswap storebe
|
584
|
-
alias store16swap store16be
|
585
|
-
alias store24swap store24be
|
586
|
-
alias store32swap store32be
|
587
|
-
alias store48swap store48be
|
588
|
-
alias store64swap store64be
|
589
|
-
alias loadiswap loadibe
|
590
|
-
alias loadi16swap loadi16be
|
591
|
-
alias loadi24swap loadi24be
|
592
|
-
alias loadi32swap loadi32be
|
593
|
-
alias loadi48swap loadi48be
|
594
|
-
alias loadi64swap loadi64be
|
595
|
-
alias loaduswap loadube
|
596
|
-
alias loadu16swap loadu16be
|
597
|
-
alias loadu24swap loadu24be
|
598
|
-
alias loadu32swap loadu32be
|
599
|
-
alias loadu48swap loadu48be
|
600
|
-
alias loadu64swap loadu64be
|
601
|
-
alias loadf32swap loadf32be
|
602
|
-
alias loadf64swap loadf64be
|
603
|
-
else
|
604
|
-
raise NotImplementedError
|
605
|
-
end
|
606
|
-
|
607
|
-
case TypeSpec::SIZEOF_SIZE_T
|
608
|
-
when 4
|
609
|
-
alias store_sizet store32
|
610
|
-
alias load_sizet loadu32
|
611
|
-
alias load_ssizet loadi32
|
612
|
-
when 8
|
613
|
-
alias store_sizet store64
|
614
|
-
alias load_sizet loadu64
|
615
|
-
alias load_ssizet loadi64
|
616
|
-
else
|
617
|
-
raise NotImplementedError
|
618
|
-
end
|
619
|
-
|
620
|
-
case TypeSpec::SIZEOF_LONG
|
621
|
-
when 4
|
622
|
-
alias store_long store32
|
623
|
-
alias load_long loadi32
|
624
|
-
alias load_ulong loadu32
|
625
|
-
when 8
|
626
|
-
alias store_long store64
|
627
|
-
alias load_long loadi64
|
628
|
-
alias load_ulong loadu64
|
629
|
-
else
|
630
|
-
raise NotImplementedError
|
631
|
-
end
|
632
|
-
end
|
633
|
-
end
|
634
|
-
|
635
|
-
class ::String
|
636
|
-
extend Gogyou::Extensions::String
|
637
|
-
include Gogyou::Extensions::String::Mixin
|
638
|
-
end
|
639
|
-
end
|
640
|
-
end
|