omnizip 0.3.25 → 0.3.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,206 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright (C) 2025 Ribose Inc.
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a
6
- # copy of this software and associated documentation files (the "Software"),
7
- # to deal in the Software without restriction, including without limitation
8
- # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
- # and/or sell copies of the Software, and to permit persons to whom the
10
- # Software is furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
- # DEALINGS IN THE SOFTWARE.
22
-
23
- module Omnizip
24
- module Algorithms
25
- class BZip2 < Algorithm
26
- # Huffman Coding for BZip2
27
- #
28
- # Implements canonical Huffman coding for the final compression
29
- # stage of BZip2. Huffman coding assigns variable-length codes
30
- # to symbols based on their frequencies, with more frequent
31
- # symbols getting shorter codes.
32
- #
33
- # This implementation:
34
- # 1. Builds a Huffman tree from symbol frequencies
35
- # 2. Generates canonical codes for efficient decoding
36
- # 3. Encodes data as a bit stream
37
- # 4. Decodes bit streams back to symbols
38
- class Huffman
39
- # Huffman tree node
40
- class Node
41
- attr_accessor :symbol, :frequency, :left, :right
42
-
43
- # Initialize node
44
- #
45
- # @param symbol [Integer, nil] Symbol (nil for internal nodes)
46
- # @param frequency [Integer] Frequency/weight
47
- def initialize(symbol, frequency)
48
- @symbol = symbol
49
- @frequency = frequency
50
- @left = nil
51
- @right = nil
52
- end
53
-
54
- # Check if node is a leaf
55
- #
56
- # @return [Boolean] True if leaf node
57
- def leaf?
58
- @left.nil? && @right.nil?
59
- end
60
- end
61
-
62
- # Build Huffman tree from frequency table
63
- #
64
- # @param frequencies [Hash<Integer, Integer>] Symbol => frequency
65
- # @return [Node, nil] Root node of Huffman tree
66
- def build_tree(frequencies)
67
- return nil if frequencies.empty?
68
-
69
- # Create leaf nodes
70
- nodes = frequencies.map do |symbol, freq|
71
- Node.new(symbol, freq)
72
- end
73
-
74
- # Build tree bottom-up
75
- while nodes.length > 1
76
- # Sort by frequency
77
- nodes.sort_by!(&:frequency)
78
-
79
- # Take two lowest frequency nodes
80
- left = nodes.shift
81
- right = nodes.shift
82
-
83
- # Create parent node
84
- parent = Node.new(nil, left.frequency + right.frequency)
85
- parent.left = left
86
- parent.right = right
87
-
88
- # Add back to nodes
89
- nodes << parent
90
- end
91
-
92
- nodes.first
93
- end
94
-
95
- # Generate code table from Huffman tree
96
- #
97
- # @param root [Node] Root of Huffman tree
98
- # @return [Hash<Integer, String>] Symbol => binary code
99
- def generate_codes(root)
100
- return {} if root.nil?
101
-
102
- codes = {}
103
- generate_codes_recursive(root, "", codes)
104
- codes
105
- end
106
-
107
- # Encode data using Huffman codes
108
- #
109
- # @param data [String] Input data to encode
110
- # @param codes [Hash<Integer, String>] Symbol => binary code
111
- # @return [String] Encoded bit stream (as binary string)
112
- def encode(data, codes)
113
- return "".b if data.empty?
114
-
115
- bits = []
116
-
117
- data.each_byte do |byte|
118
- code = codes[byte]
119
- raise "No code for byte #{byte}" unless code
120
-
121
- bits << code
122
- end
123
-
124
- bits_to_bytes(bits.join)
125
- end
126
-
127
- # Decode bit stream using Huffman tree
128
- #
129
- # @param bits [String] Encoded bit stream
130
- # @param root [Node] Root of Huffman tree
131
- # @param length [Integer] Expected output length
132
- # @return [String] Decoded data
133
- def decode(bits, root, length)
134
- return "".b if bits.empty? || root.nil?
135
-
136
- result = []
137
- current = root
138
- bit_string = bytes_to_bits(bits)
139
- bit_index = 0
140
-
141
- while result.length < length && bit_index < bit_string.length
142
- bit = bit_string[bit_index]
143
- bit_index += 1
144
-
145
- # Navigate tree
146
- current = (bit == "0" ? current.left : current.right)
147
-
148
- # Check if we reached a leaf
149
- if current.leaf?
150
- result << current.symbol
151
- current = root
152
- end
153
- end
154
-
155
- result.pack("C*")
156
- end
157
-
158
- private
159
-
160
- # Recursively generate codes for all symbols
161
- #
162
- # @param node [Node] Current node
163
- # @param code [String] Current code path
164
- # @param codes [Hash] Code table being built
165
- # @return [void]
166
- def generate_codes_recursive(node, code, codes)
167
- return if node.nil?
168
-
169
- if node.leaf?
170
- codes[node.symbol] = code.empty? ? "0" : code
171
- else
172
- generate_codes_recursive(node.left, "#{code}0", codes)
173
- generate_codes_recursive(node.right, "#{code}1", codes)
174
- end
175
- end
176
-
177
- # Convert bit string to bytes
178
- #
179
- # @param bits [String] Bit string (e.g., "10110101")
180
- # @return [String] Byte string
181
- def bits_to_bytes(bits)
182
- # Pad to multiple of 8
183
- padding = (8 - (bits.length % 8)) % 8
184
- bits += ("0" * padding)
185
-
186
- # Convert to bytes
187
- bytes = []
188
- (0...bits.length).step(8) do |i|
189
- byte_bits = bits[i, 8]
190
- bytes << byte_bits.to_i(2)
191
- end
192
-
193
- bytes.pack("C*")
194
- end
195
-
196
- # Convert bytes to bit string
197
- #
198
- # @param bytes [String] Byte string
199
- # @return [String] Bit string
200
- def bytes_to_bits(bytes)
201
- bytes.bytes.map { |b| format("%08b", b) }.join
202
- end
203
- end
204
- end
205
- end
206
- end
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright (C) 2025 Ribose Inc.
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a
6
- # copy of this software and associated documentation files (the "Software"),
7
- # to deal in the Software without restriction, including without limitation
8
- # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
- # and/or sell copies of the Software, and to permit persons to whom the
10
- # Software is furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
- # DEALINGS IN THE SOFTWARE.
22
-
23
- module Omnizip
24
- module Algorithms
25
- class BZip2 < Algorithm
26
- # Move-to-Front (MTF) Transform
27
- #
28
- # MTF is a data transformation that exploits locality of reference.
29
- # It maintains a list of symbols and moves accessed symbols to the
30
- # front of the list. This tends to concentrate frequently accessed
31
- # symbols at low indices, making the data more compressible.
32
- #
33
- # After BWT, the data often has runs of the same character. MTF
34
- # converts these to runs of low numbers (often 0), which are then
35
- # efficiently compressed by RLE.
36
- #
37
- # The algorithm:
38
- # 1. Initialize symbol list [0, 1, 2, ..., 255]
39
- # 2. For each byte in input:
40
- # - Find its position in the symbol list
41
- # - Output that position
42
- # - Move the byte to the front of the list
43
- class Mtf
44
- # Encode data using Move-to-Front transform
45
- #
46
- # @param data [String] Input data to transform
47
- # @return [String] MTF-encoded data (byte indices)
48
- def encode(data)
49
- return "".b if data.empty?
50
-
51
- symbols = init_symbol_list
52
- result = []
53
-
54
- data.each_byte do |byte|
55
- # Find position of byte in symbol list
56
- index = symbols.index(byte)
57
- result << index
58
-
59
- # Move byte to front
60
- symbols.delete_at(index)
61
- symbols.unshift(byte)
62
- end
63
-
64
- result.pack("C*")
65
- end
66
-
67
- # Decode MTF-encoded data
68
- #
69
- # @param data [String] MTF-encoded indices
70
- # @return [String] Original data
71
- def decode(data)
72
- return "".b if data.empty?
73
-
74
- symbols = init_symbol_list
75
- result = []
76
-
77
- data.each_byte do |index|
78
- # Get byte at this index
79
- byte = symbols[index]
80
- result << byte
81
-
82
- # Move byte to front
83
- symbols.delete_at(index)
84
- symbols.unshift(byte)
85
- end
86
-
87
- result.pack("C*")
88
- end
89
-
90
- private
91
-
92
- # Initialize symbol list with all possible byte values
93
- #
94
- # @return [Array<Integer>] Symbol list [0, 1, 2, ..., 255]
95
- def init_symbol_list
96
- (0..255).to_a
97
- end
98
- end
99
- end
100
- end
101
- end