rrb-common 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8830b14ecfcf37cc6afb5779915d9beeaf84a137d38ca984d832d588ca7ecc1b
4
- data.tar.gz: 8b4e8eced92a7c05e6161d9886c6f11e02614d82f9c0257b5ba9342834118867
3
+ metadata.gz: 25249c7b5e3aa642c23637cc4e3bad8ce62434a1d77962287fe7cab331d016c9
4
+ data.tar.gz: 7592bacd9ddfb0d7371bfd24f87a84de502e2500f7a81a5ca3f8bbd5f1d888a5
5
5
  SHA512:
6
- metadata.gz: 6b350710235c41abad111df718f214c408a4ae69b223a7fbfb4f477ea1611720ce072429e8ebc19d6aa198e3d0f5a18c10f469ef5d5e4d988b98c25781ced03d
7
- data.tar.gz: 9e83c03b05779b74f959689d8b5709dc83dbcb0ec2cc6f0ce43a90a6bfc90402cd54ff2334cd5b37e007d70f7eff3b6ac4612ee73706f20c2aad8ffa95d23079
6
+ metadata.gz: 58a9a9da24f64061478f46982beb434b6a3e917c494bf36e437b50570593b2b05bb9870ad50bee4fb08cb82bfec4eb90e8377cae5504e513584582ce1030b6bf
7
+ data.tar.gz: b4810f557135b53ba88e89850d59076f24650df965ec0b497868ec41d7a47e8d47c635c6b5797c5297d994446335a479e1dad3c0f22da04c810503a571af8460
data/lib/rune/common.rb CHANGED
@@ -19,7 +19,8 @@ module RuneRb
19
19
  autoload :Buffer, 'rune/core/buffer'
20
20
  autoload :Constants, 'rune/core/constants'
21
21
  autoload :Identifiable, 'rune/core/identifiable'
22
- autoload :ISAAC, 'rune/core/isaac'
22
+ autoload :JavaISAAC, 'rune/core/java_isaac'
23
+ autoload :RubyISAAC, 'rune/core/ruby_isaac'
23
24
  autoload :Logging, 'rune/core/logging'
24
25
  autoload :ReadableBuffer, 'rune/core/readable_buffer'
25
26
  autoload :Unit, 'rune/core/unit'
@@ -34,4 +34,11 @@ module RuneRb::Core::Constants
34
34
  # The size of a chunk of data
35
35
  # @return [Integer]
36
36
  DATA_SIZE = DATA_BLOCK_SIZE + DATA_HEADER_SIZE
37
+
38
+ # The possible byte orders for reading/writing 24-bit integers. The integers will only be written in big or middle endian byte order.
39
+ # @return [Hash<Symbol, Array<Integer>>]
40
+ INT24_BYTE_ORDERS = {
41
+ BIG: [0, 1, 2].freeze,
42
+ LITTLE: [2, 1, 0].freeze
43
+ }.freeze
37
44
  end
@@ -16,11 +16,10 @@ module RuneRb::Core::ReadableBuffer
16
16
  # @param mutation [Symbol] an option mutation to apply to the read value.
17
17
  def read(type: :byte, signed: false, mutation: :STD, order: :BIG, options: nil)
18
18
  case type
19
- when :bits then read_bits(options[:amount])
20
19
  when :byte then read_byte(signed:, mutation:)
21
20
  when :bytes then read_bytes(options[:amount] || 1, mutation:)
22
21
  when :short then read_short(signed:, mutation:, order:)
23
- when :int24 then read_int24(signed:, mutation:, order:)
22
+ when :int24 then read_int24(mutation:, order:)
24
23
  when :int then read_int(signed:, mutation:, order:)
25
24
  when :long then read_long(signed:, mutation:, order:)
26
25
  when :smart then read_smart(signed:, mutation:)
@@ -51,12 +50,13 @@ module RuneRb::Core::ReadableBuffer
51
50
  end
52
51
 
53
52
  # Probably did this wrong
53
+ # @todo fix and test
54
54
  # @param amount [Integer] the amount of bytes to read
55
55
  # @param mutation [Symbol] an optional mutation to apply to the result.
56
56
  def read_bytes_reverse(amount, mutation)
57
- @data.flip
57
+ @data.reverse!
58
58
  value = amount.times.each_with_object([]) { value << read_byte(signed: false, mutation:) }
59
- @data.flip
59
+ @data.reverse!
60
60
  value
61
61
  end
62
62
 
@@ -65,8 +65,7 @@ module RuneRb::Core::ReadableBuffer
65
65
  # @param mutation [Symbol] mutation that should be applied to the byte value.
66
66
  # @return [Integer] the read byte value.
67
67
  def read_byte(mutation: :STD, signed: false)
68
- directive = signed ? 'c' : 'C'
69
- value = @data.slice!(0).unpack1(directive)
68
+ value = @data.slice!(0).unpack1(signed ? 'c' : 'C')
70
69
  mutate(value, mutation)
71
70
  end
72
71
 
@@ -75,46 +74,32 @@ module RuneRb::Core::ReadableBuffer
75
74
  # @param mutation [String] mutation that should be applied to the short value
76
75
  # @param order [Symbol] they byte order to read the short value
77
76
  def read_short(signed: false, mutation: :STD, order: :BIG)
78
- directive = "#{signed ? 's' : 'S'}"
79
- directive << case order
80
- when :BIG then '>'
81
- when :LITTLE then '<'
82
- else ''
83
- end
77
+ directive = "#{signed ? 's' : 'S'}#{case order
78
+ when :BIG then '>'
79
+ when :LITTLE then '<'
80
+ else ''
81
+ end}"
84
82
  value = @data.slice!(0..1).unpack1(directive)
85
83
  mutate(value, mutation)
86
84
  end
87
85
 
88
- # Reads a 24-Bit Integer value from the {Buffer#data}
89
- # @param signed [Boolean] should the value be signed.
86
+ # Reads a 24-Bit Unsigned Integer value from the {Buffer#data}
90
87
  # @param mutation [Symbol] mutation that should be applied to the value
91
88
  # @param order [String] they byte order to read the value
92
- def read_int24(signed: false, mutation: :STD, order: :BIG)
93
- unpacked = @data.slice!(0..2).unpack('C3')
89
+ def read_int24(mutation: :STD, order: :BIG)
90
+ value = 0
94
91
  case order
95
92
  when :BIG
96
- if signed
97
- value = (unpacked[0] << 16) | (unpacked[1] << 8) | unpacked[2]
98
- unpacked[0] & 0x80 != 0 ? value - 0x10000 : value
99
- else
100
- (unpacked[0] << 16) | (unpacked[1] << 8) | unpacked[2]
101
- end
102
- when :MIDDLE
103
- if signed
104
- value = (unpacked[1] << 16) | (unpacked[2] << 8) | unpacked[0]
105
- unpacked[1] & 0x80 != 0 ? value - 0x10000 : value
106
- else
107
- (unpacked[1] << 16) | (unpacked[2] << 8) | unpacked[0]
108
- end
93
+ value |= read_byte << 16
94
+ value |= read_byte << 8
95
+ value |= read_byte(mutation:)
109
96
  when :LITTLE
110
- if signed
111
- value = (unpacked[2] << 16) | (unpacked[1] << 8) | unpacked[0]
112
- unpacked[0] & 0x80 != 0 ? value - 0x10000 : value
113
- else
114
- (unpacked[2] << 16) | (unpacked[1] << 8) | unpacked[0]
115
- end
116
- else read_int24(signed:, mutation:, order: :BIG)
97
+ value |= read_byte(mutation:)
98
+ value |= read_byte << 8
99
+ value |= read_byte << 16
100
+ else read_int24(mutation:, order: :BIG)
117
101
  end
102
+ value
118
103
  end
119
104
 
120
105
  # Reads a integer value from the {Buffer#data}
@@ -144,17 +129,13 @@ module RuneRb::Core::ReadableBuffer
144
129
  # @param mutation [Symbol] mutation that should be applied to the long value
145
130
  # @param order [String] they byte order to read the long value
146
131
  def read_long(signed: false, mutation: :STD, order: :BIG)
147
- case order
148
- when :BIG
149
- directive = signed ? 'q>' : 'Q>'
150
- value = @data.slice!(0..7).unpack1(directive)
151
- mutate(value, mutation)
152
- when :LITTLE
153
- directive = signed ? 'q<' : 'Q<'
154
- value = @data.slice!(0..7).unpack1(directive)
155
- mutate(value, mutation)
156
- else read_long(signed:, mutation:, order: :BIG)
157
- end
132
+ directive = "#{signed ? 'q' : 'Q'}#{case order
133
+ when :BIG then '>'
134
+ when :LITTLE then '<'
135
+ else ''
136
+ end}"
137
+ value = @data.slice!(0..7).unpack1(directive)
138
+ mutate(value, mutation)
158
139
  end
159
140
 
160
141
  # Read a smart value from the {Buffer#data}
@@ -1,6 +1,6 @@
1
1
  module RuneRb::Core
2
2
  # An implementation of an ISAAC cipher used to generate random numbers for message interchange.
3
- class ISAAC
3
+ class RubyISAAC
4
4
  using RuneRb::Patches::IntegerRefinements
5
5
 
6
6
  # Called when a new ISAAC Cipher is created.
@@ -26,7 +26,7 @@ module RuneRb::Core
26
26
  @randcnt = 256
27
27
  end
28
28
  @randcnt -= 1
29
- @randrsl[@randcnt].signed(:i)
29
+ @randrsl[@randcnt].int
30
30
  end
31
31
 
32
32
  private
@@ -1,6 +1,55 @@
1
1
  # A module adding overflow functions to integers to mimic the behavior of Java primitive overflow behavior.
2
2
  module RuneRb::Patches::IntegerRefinements
3
3
  refine Integer do
4
+ def overflow(i, e = 2 ** 31)
5
+ f = (Math.log(e) / Math.log(2)).to_i+1
6
+ g = (2 ** f) - 1
7
+
8
+ if i < -e
9
+ i & g
10
+ elsif i > e - 1
11
+ -(-(i) & g)
12
+ else
13
+ i
14
+ end
15
+ end
16
+
17
+ def nibble
18
+ overflow(self.to_i & 0xf, 2 ** 4)
19
+ end
20
+
21
+ def byte
22
+ overflow(self.to_i, 2 ** 7)
23
+ end
24
+
25
+ def ubyte
26
+ overflow(self.to_i & 0xff, 2 ** 8)
27
+ end
28
+
29
+ def short
30
+ overflow(self.to_i, 2 ** 15)
31
+ end
32
+
33
+ def ushort
34
+ overflow(self.to_i & 0xffff, 2 ** 16)
35
+ end
36
+
37
+ def int
38
+ overflow(self.to_i, 2 ** 31)
39
+ end
40
+
41
+ def uint
42
+ overflow(self.to_i & 0xffffffff, 2 ** 32)
43
+ end
44
+
45
+ def long
46
+ overflow(self.to_i, 2 ** 63)
47
+ end
48
+
49
+ def ulong
50
+ overflow(self.to_i & 0xffffffffffffffff, 2 ** 64)
51
+ end
52
+
4
53
  # Returns a binary representation in the form of an array of 1's and 0's in their respective digits.
5
54
  # @return [Array] the binary representation
6
55
  def binary_representation
@@ -22,39 +71,6 @@ module RuneRb::Patches::IntegerRefinements
22
71
 
23
72
  alias_method :from_brep, :from_binary_rep
24
73
 
25
- # Adjusts the value of the Integer to be unsigned.
26
- # @param type [Symbol] the type of primitive the value will be returned as
27
- def unsigned(type)
28
- return 0 if negative?
29
- case type
30
- when :Byte, :byte, :b, :B then to_i > 0xFF ? 0xFF : to_i
31
- when :Short, :short, :s, :S then to_i > 0xFFFF ? 0xFFFF : to_i
32
- when :Integer, :Int, :int, :integer, :i, :I then to_i > 0xFFFFFFFF ? 0xFFFFFFFF : to_i
33
- when :Long, :long, :l, :L then to_i > 0xFFFFFFFFFFFFFFFF ? 0xFFFFFFFFFFFFFFFF : to_i
34
- else unsigned(:int)
35
- end
36
- end
37
-
38
- alias_method :u, :unsigned
39
-
40
- # Adjusts the value of the Integer to be signed.
41
- # @param type [Symbol] the type of primitive the value will be returned as
42
- def signed(type)
43
- case type
44
- when :Byte, :byte, :b, :B then adjust(:byte)
45
- when :Short, :short, :s, :S then adjust(:short)
46
- when :Integer, :Int, :int, :integer, :i, :I then adjust(:integer)
47
- when :Long, :long, :l, :L then adjust(:long)
48
- else adjust(:integer)
49
- end
50
- end
51
-
52
- alias_method :s, :signed
53
-
54
- def nibble
55
- adjust(:nibble)
56
- end
57
-
58
74
  # Returns a string with a formatted representation of the Integer as a timestamp.
59
75
  def to_ftime
60
76
  mm, ss = divmod(60)
@@ -63,61 +79,6 @@ module RuneRb::Patches::IntegerRefinements
63
79
  format('%d days, %d hours, %d minutes, and %d seconds', dd, hh, mm, ss)
64
80
  end
65
81
 
66
- private
67
-
68
- # Adjusts the integer based on the passed type
69
- # @param type [Symbol] the type of adjustment to make to the integer.
70
- def adjust(type)
71
- case type
72
- when :Byte, :byte, :b, :B
73
- primitive_max = 2**7 - 1
74
- primitive_min = -2**7
75
- when :Short, :short, :s, :S
76
- primitive_max = 2**15 - 1
77
- primitive_min = -2**15
78
- when :Integer, :Int, :int, :i, :I
79
- primitive_max = 2**31 - 1
80
- primitive_min = -2**31
81
- when :Long, :long, :l, :L
82
- primitive_max = 2**63 - 1
83
- primitive_min = -2**63
84
- when :Nibble, :nibble, :n, :N
85
- primitive_max = 2**4
86
- primitive_min = -2**4
87
- else
88
- primitive_max = 2**31 - 1
89
- primitive_min = -2**31
90
- end
91
- self < -primitive_max ? -1 * (-self & primitive_max) : self
92
- self > primitive_min ? (self & primitive_max) : self
93
- end
82
+ alias_method :ftime, :to_ftime
94
83
  end
95
- end
96
-
97
- # Copyright (c) 2021, Patrick W.
98
- # All rights reserved.
99
- #
100
- # Redistribution and use in source and binary forms, with or without
101
- # modification, are permitted provided that the following conditions are met:
102
- #
103
- # * Redistributions of source code must retain the above copyright notice, this
104
- # list of conditions and the following disclaimer.
105
- #
106
- # * Redistributions in binary form must reproduce the above copyright notice,
107
- # this list of conditions and the following disclaimer in the documentation
108
- # and/or other materials provided with the distribution.
109
- #
110
- # * Neither the name of the copyright holder nor the names of its
111
- # contributors may be used to endorse or promote products derived from
112
- # this software without specific prior written permission.
113
- #
114
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
115
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
117
- # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
118
- # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
119
- # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
120
- # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
121
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
122
- # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
123
- # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
84
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rrb-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick W.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-12 00:00:00.000000000 Z
11
+ date: 2023-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -161,9 +161,9 @@ files:
161
161
  - lib/rune/core/buffer.rb
162
162
  - lib/rune/core/constants.rb
163
163
  - lib/rune/core/identifiable.rb
164
- - lib/rune/core/isaac.rb
165
164
  - lib/rune/core/logging.rb
166
165
  - lib/rune/core/readable_buffer.rb
166
+ - lib/rune/core/ruby_isaac.rb
167
167
  - lib/rune/core/unit.rb
168
168
  - lib/rune/core/writeable_buffer.rb
169
169
  - lib/rune/patches/integer_refinements.rb