leb128 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/leb128.gemspec +1 -1
- data/lib/leb128.rb +6 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ed16554ca16a052d5cfa797d55edc70a79d4c02
|
4
|
+
data.tar.gz: 46b3d05dd5714fcc60cb4015967037a09e8ba547
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae77aacabcc90c23c515a9e1150dd643c9038ce73cd1f8213234c6a448cf4dc3bcf85ee62edddf9e51431387b2fb8604403a299f0a382117e64022ab69f89ac6
|
7
|
+
data.tar.gz: 9867aae8687da55df2c60d83b73a2eaf9962a2978df78db2d27a890595d9219d2aacb2aacb24de9792973c680eec377e9f1102a165ed10c2c3cb07140f347e11
|
data/README.md
CHANGED
@@ -1,7 +1,20 @@
|
|
1
1
|
# LEB128
|
2
2
|
|
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
|
+
|
3
5
|
This gem is a pair of utility functions for encoding and decoding LEB128-compressed integers.
|
4
6
|
|
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
|
+
|
9
|
+
```
|
10
|
+
10011000011101100101 In raw binary
|
11
|
+
010011000011101100101 Padded to a multiple of 7 bits
|
12
|
+
0100110 0001110 1100101 Split into 7-bit groups
|
13
|
+
00100110 10001110 11100101 Add high 1 bits on all but last group to form bytes
|
14
|
+
0x26 0x8E 0xE5 In hexadecimal
|
15
|
+
0xE5 0x8E 0x26 Output stream
|
16
|
+
```
|
17
|
+
|
5
18
|
## Installation
|
6
19
|
|
7
20
|
Add this line to your application's Gemfile:
|
data/leb128.gemspec
CHANGED
data/lib/leb128.rb
CHANGED
@@ -13,19 +13,22 @@ module LEB128
|
|
13
13
|
|
14
14
|
def self.encode(value, signed)
|
15
15
|
if value < 0 && !signed
|
16
|
-
raise(ArgumentError,
|
16
|
+
raise(ArgumentError,
|
17
|
+
'Cannot encode a negative integer to an unsigned LEB128')
|
17
18
|
end
|
18
19
|
io = StringIO.new
|
19
20
|
loop do
|
20
21
|
byte = value & 0x7F
|
21
22
|
value >>= 7
|
22
23
|
if signed
|
23
|
-
byte |= 0x80 unless (value == 0 && byte & 0x40 == 0) ||
|
24
|
+
byte |= 0x80 unless (value == 0 && byte & 0x40 == 0) ||
|
25
|
+
(value == -1 && byte & 0x40 != 0)
|
24
26
|
elsif value != 0
|
25
27
|
byte |= 0x80
|
26
28
|
end
|
27
29
|
io.putc(byte)
|
28
|
-
break if (signed && byte & 0x80 == 0) ||
|
30
|
+
break if (signed && byte & 0x80 == 0) ||
|
31
|
+
(!signed && value == 0)
|
29
32
|
end
|
30
33
|
io.pos = 0
|
31
34
|
io
|