leb128 0.1.1 → 1.0.0

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
  SHA1:
3
- metadata.gz: 0ed16554ca16a052d5cfa797d55edc70a79d4c02
4
- data.tar.gz: 46b3d05dd5714fcc60cb4015967037a09e8ba547
3
+ metadata.gz: bf3a19c67617bdb7e0195e364134def4c55f1bd0
4
+ data.tar.gz: 77ae39dca195965e95a4efc0a437f27c11afcf14
5
5
  SHA512:
6
- metadata.gz: ae77aacabcc90c23c515a9e1150dd643c9038ce73cd1f8213234c6a448cf4dc3bcf85ee62edddf9e51431387b2fb8604403a299f0a382117e64022ab69f89ac6
7
- data.tar.gz: 9867aae8687da55df2c60d83b73a2eaf9962a2978df78db2d27a890595d9219d2aacb2aacb24de9792973c680eec377e9f1102a165ed10c2c3cb07140f347e11
6
+ metadata.gz: a95c66b3d490f333055219fa1a2177d73faf63b33f047edc6fc2971857fcf657534bd6837dbb40b638430a658b51dff386420312e641e3f79c0fc5080ddd6bab
7
+ data.tar.gz: ab733a9ddec874581862857d3b8b5d95495716046264d6b63917d38abc0137765e723071cc8b65ad164b2f6da6f04400fb9bc5bf51fef002a9d2f5c959492bc9
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/cl8n/leb128/master/LICENSE.md) [![Build Status](https://travis-ci.org/cl8n/leb128.svg?branch=master)](https://travis-ci.org/cl8n/leb128) [![Documentation](https://img.shields.io/badge/docs-rubydoc.info-orange.svg)](http://www.rubydoc.info/gems/leb128)
4
4
 
5
- This gem is a pair of utility functions for encoding and decoding LEB128-compressed integers.
5
+ This gem is a set of utility functions for encoding and decoding LEB128-compressed integers.
6
6
 
7
7
  As a visual example, this is an excerpt from the [LEB128 Wikipedia page](https://en.wikipedia.org/wiki/LEB128) describing how an integer is encoded into an unsigned LEB128:
8
8
 
@@ -41,20 +41,20 @@ require 'leb128'
41
41
 
42
42
  ### Encoding and Decoding
43
43
 
44
- Encode integers into LEB128-compressed data by using the `encode` method. The first argument takes an integer, and the second takes a bool that tells whether to use a signed datatype or not. The following encodes the given integer and returns a new StringIO containing it:
44
+ Encode integers into LEB128-compressed data by using the `encode_unsigned` and `encode_signed` methods. The functions take one argument, being the integer to encode. The following encodes the given integer and returns a new StringIO containing it:
45
45
 
46
46
  ```ruby
47
- LEB128.encode(-0x143f, true)
48
- #=> #<StringIO:0x00000003533e20>
47
+ LEB128.encode_signed(-0x143f)
48
+ #=> #<StringIO:0x00000000000000>
49
49
  ```
50
50
 
51
- Decode LEB128-compressed data into integers by using the `decode` method. The first argument takes a StringIO, the second takes a bool that tells whether to use a signed datatype or not, and the third takes an integer that sets the position of the StringIO (default is 0). The following decodes the given StringIO and returns a new integer containing it:
51
+ Decode LEB128-compressed data into integers by using the `decode_unsigned` and `decode_signed` methods. The functions take two arguments: a StringIO containing the LEB128-compressed data, and an optional integer that sets the position of the StringIO (default is 0). The following decodes the given StringIO and returns a new integer containing it:
52
52
 
53
53
  ```ruby
54
54
  sio = StringIO.new
55
55
  sio.putc(0xc1)
56
56
  sio.putc(0x57)
57
- LEB128.decode(sio, false)
57
+ LEB128.decode_unsigned(sio)
58
58
  #=> 11201
59
59
  ```
60
60
 
@@ -1,16 +1,16 @@
1
- Gem::Specification.new do |spec|
2
- spec.name = "leb128"
3
- spec.version = "0.1.1"
4
- spec.authors = ["Clayton Bonigut"]
5
-
6
- spec.summary = %q{A pair of utility functions for encoding and decoding LEB128-compressed integers.}
7
- spec.homepage = "https://github.com/cl8n/leb128"
8
- spec.license = "MIT"
9
-
10
- spec.files = `git ls-files -z`.split("\x0")
11
-
12
- spec.add_development_dependency "bundler", "~> 1.12"
13
- spec.add_development_dependency "rake", "~> 10.0"
14
- spec.add_development_dependency "minitest", "~> 5.0"
15
- spec.add_development_dependency "yard"
16
- end
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "leb128"
3
+ spec.version = "1.0.0"
4
+ spec.authors = ["Clayton Bonigut"]
5
+
6
+ spec.summary = %q{A set of utility functions for encoding and decoding LEB128-compressed integers.}
7
+ spec.homepage = "https://github.com/cl8n/leb128"
8
+ spec.license = "MIT"
9
+
10
+ spec.files = `git ls-files -z`.split("\x0")
11
+
12
+ spec.add_development_dependency "bundler", "~> 1.12"
13
+ spec.add_development_dependency "rake", "~> 10.0"
14
+ spec.add_development_dependency "minitest", "~> 5.0"
15
+ spec.add_development_dependency "yard"
16
+ end
@@ -1,18 +1,17 @@
1
1
  require 'stringio'
2
2
 
3
- # Includes a pair of functions to encode and decode the LEB128 integer compression type.
3
+ # Includes a set of functions to encode and decode the LEB128 integer compression type.
4
4
  module LEB128
5
- # Encode an integer into LEB128-compressed data.
5
+ # Encode an unsigned integer into LEB128-compressed data.
6
6
  #
7
7
  # @param value [Integer]
8
- # @param signed [Boolean]
9
8
  # @return [StringIO]
10
9
  #
11
10
  # @example
12
- # LEB128.encode(-0x143f, true) #=> #<StringIO:0x00000003533e20>
11
+ # LEB128.encode_unsigned(0x2bc1) #=> #<StringIO:0x00000000000000>
13
12
 
14
- def self.encode(value, signed)
15
- if value < 0 && !signed
13
+ def self.encode_unsigned(value)
14
+ if value < 0
16
15
  raise(ArgumentError,
17
16
  'Cannot encode a negative integer to an unsigned LEB128')
18
17
  end
@@ -20,24 +19,65 @@ module LEB128
20
19
  loop do
21
20
  byte = value & 0x7F
22
21
  value >>= 7
23
- if signed
24
- byte |= 0x80 unless (value == 0 && byte & 0x40 == 0) ||
25
- (value == -1 && byte & 0x40 != 0)
26
- elsif value != 0
27
- byte |= 0x80
28
- end
22
+ byte |= 0x80 if value != 0
23
+ io.putc(byte)
24
+ break if value == 0
25
+ end
26
+ io.pos = 0
27
+ io
28
+ end
29
+
30
+ # Encode a signed integer into LEB128-compressed data.
31
+ #
32
+ # @param value [Integer]
33
+ # @return [StringIO]
34
+ #
35
+ # @example
36
+ # LEB128.encode_signed(-0x143f) #=> #<StringIO:0x00000000000000>
37
+
38
+ def self.encode_signed(value)
39
+ io = StringIO.new
40
+ loop do
41
+ byte = value & 0x7F
42
+ value >>= 7
43
+ byte |= 0x80 unless (value == 0 && byte & 0x40 == 0) ||
44
+ (value == -1 && byte & 0x40 != 0)
29
45
  io.putc(byte)
30
- break if (signed && byte & 0x80 == 0) ||
31
- (!signed && value == 0)
46
+ break if byte & 0x80 == 0
32
47
  end
33
48
  io.pos = 0
34
49
  io
35
50
  end
36
51
 
37
- # Decode LEB128-compressed data into an integer.
52
+ # Decode unsigned LEB128-compressed data into an integer.
53
+ #
54
+ # @param io [StringIO]
55
+ # @param index [Integer]
56
+ # @return [Integer]
57
+ #
58
+ # @example
59
+ # sio = StringIO.new
60
+ # sio.putc(0xc1)
61
+ # sio.putc(0x57)
62
+ # LEB128.decode_unsigned(sio) #=> 11201
63
+
64
+ def self.decode_unsigned(io, index = 0)
65
+ io.pos = index
66
+ raise(EOFError) if io.eof?
67
+ value = 0
68
+ offset = 0
69
+ loop do
70
+ byte = io.read(1).unpack('C')[0]
71
+ value |= (byte & 0x7F) << offset
72
+ break if byte & 0x80 == 0
73
+ offset += 7
74
+ end
75
+ value
76
+ end
77
+
78
+ # Decode signed LEB128-compressed data into an integer.
38
79
  #
39
80
  # @param io [StringIO]
40
- # @param signed [Boolean]
41
81
  # @param index [Integer]
42
82
  # @return [Integer]
43
83
  #
@@ -45,9 +85,9 @@ module LEB128
45
85
  # sio = StringIO.new
46
86
  # sio.putc(0xc1)
47
87
  # sio.putc(0x57)
48
- # LEB128.decode(sio, false) #=> 11201
88
+ # LEB128.decode_signed(sio) #=> -5183
49
89
 
50
- def self.decode(io, signed, index = 0)
90
+ def self.decode_signed(io, index = 0)
51
91
  io.pos = index
52
92
  raise(EOFError) if io.eof?
53
93
  value = 0
@@ -57,7 +97,7 @@ module LEB128
57
97
  value |= (byte & 0x7F) << offset
58
98
  offset += 7
59
99
  if byte & 0x80 == 0
60
- value |= -(1 << offset) if signed && byte & 0x40 != 0
100
+ value |= -(1 << offset) unless byte & 0x40 == 0
61
101
  break
62
102
  end
63
103
  end
@@ -6,7 +6,7 @@ class LEB128Test < Minitest::Test
6
6
  buffer = StringIO.new
7
7
  buffer.putc(0xc1)
8
8
  buffer.putc(0x57)
9
- actual = LEB128.decode(buffer, true)
9
+ actual = LEB128.decode_signed(buffer)
10
10
  expected = -0x143f
11
11
  assert_equal(expected, actual)
12
12
  end
@@ -15,7 +15,7 @@ class LEB128Test < Minitest::Test
15
15
  buffer = StringIO.new
16
16
  buffer.putc(0x8e)
17
17
  buffer.putc(0x32)
18
- actual = LEB128.decode(buffer, true)
18
+ actual = LEB128.decode_signed(buffer)
19
19
  expected = 0x190e
20
20
  assert_equal(expected, actual)
21
21
  end
@@ -24,7 +24,7 @@ class LEB128Test < Minitest::Test
24
24
  buffer = StringIO.new
25
25
  buffer.putc(0xc1)
26
26
  buffer.putc(0x57)
27
- actual = LEB128.decode(buffer, false)
27
+ actual = LEB128.decode_unsigned(buffer)
28
28
  expected = 0x2bc1
29
29
  assert_equal(expected, actual)
30
30
  end
@@ -33,7 +33,7 @@ class LEB128Test < Minitest::Test
33
33
  assert_raises(EOFError) do
34
34
  buffer = StringIO.new
35
35
  buffer.putc(0x01)
36
- LEB128.decode(buffer, false, 1)
36
+ LEB128.decode_unsigned(buffer, 1)
37
37
  end
38
38
  end
39
39
 
@@ -41,7 +41,7 @@ class LEB128Test < Minitest::Test
41
41
  buffer = StringIO.new
42
42
  buffer.putc(0xc1)
43
43
  buffer.putc(0x57)
44
- actual = LEB128.encode(-0x143f, true).string
44
+ actual = LEB128.encode_signed(-0x143f).string
45
45
  expected = buffer.string
46
46
  assert_equal(expected, actual)
47
47
  end
@@ -50,14 +50,14 @@ class LEB128Test < Minitest::Test
50
50
  buffer = StringIO.new
51
51
  buffer.putc(0x8e)
52
52
  buffer.putc(0x32)
53
- actual = LEB128.encode(0x190e, true).string
53
+ actual = LEB128.encode_signed(0x190e).string
54
54
  expected = buffer.string
55
55
  assert_equal(expected, actual)
56
56
  end
57
57
 
58
58
  def test_encode_unsigned_negative_int_argumenterror
59
59
  exception = assert_raises(ArgumentError) do
60
- LEB128.encode(-0x01, false)
60
+ LEB128.encode_unsigned(-0x01)
61
61
  end
62
62
  assert_equal('Cannot encode a negative integer to an unsigned LEB128', exception.message)
63
63
  end
@@ -66,7 +66,7 @@ class LEB128Test < Minitest::Test
66
66
  buffer = StringIO.new
67
67
  buffer.putc(0xc1)
68
68
  buffer.putc(0x57)
69
- actual = LEB128.encode(0x2bc1, false).string
69
+ actual = LEB128.encode_unsigned(0x2bc1).string
70
70
  expected = buffer.string
71
71
  assert_equal(expected, actual)
72
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leb128
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clayton Bonigut
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-19 00:00:00.000000000 Z
11
+ date: 2016-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -104,6 +104,6 @@ rubyforge_project:
104
104
  rubygems_version: 2.5.1
105
105
  signing_key:
106
106
  specification_version: 4
107
- summary: A pair of utility functions for encoding and decoding LEB128-compressed integers.
107
+ summary: A set of utility functions for encoding and decoding LEB128-compressed integers.
108
108
  test_files: []
109
109
  has_rdoc: