-
Notifications
You must be signed in to change notification settings - Fork 15
/
ZRLT.go
224 lines (179 loc) · 4.61 KB
/
ZRLT.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Copyright 2011-2024 Frederic Langlet
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transform
import (
"errors"
"fmt"
internal "github.com/flanglet/kanzi-go/v2/internal"
)
// ZRLT Zero Run Length Transform
// Zero Length Encoding is a simple encoding algorithm by Wheeler
// closely related to Run Length Encoding. The main difference is
// that only runs of 0 values are processed. Also, the length is
// encoded in a different way (each digit in a different byte)
// This algorithm is well adapted to process post BWT/MTFT data
type ZRLT struct {
}
// NewZRLT creates a new instance of ZRLT
func NewZRLT() (*ZRLT, error) {
this := &ZRLT{}
return this, nil
}
// NewZRLTWithCtx creates a new instance of ZRLT using a
// configuration map as parameter.
func NewZRLTWithCtx(ctx *map[string]any) (*ZRLT, error) {
this := &ZRLT{}
return this, nil
}
// Forward applies the function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *ZRLT) Forward(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
if &src[0] == &dst[0] {
return 0, 0, errors.New("Input and output buffers cannot be equal")
}
if n := this.MaxEncodedLen(len(src)); len(dst) < n {
return 0, 0, fmt.Errorf("Output buffer is too small - size: %d, required %d", len(dst), n)
}
srcEnd := uint(len(src))
dstEnd := uint(len(src)) // do not expand, hence len(src)
srcIdx, dstIdx := uint(0), uint(0)
res := true
for srcIdx < srcEnd {
if src[srcIdx] == 0 {
runStart := srcIdx - 1
srcIdx++
for srcIdx+1 < srcEnd && src[srcIdx]|src[srcIdx+1] == 0 {
srcIdx += 2
}
for srcIdx < srcEnd && src[srcIdx] == 0 {
srcIdx++
}
// Encode length
runLength := srcIdx - runStart
log2 := internal.Log2NoCheck(uint32(runLength))
if dstIdx >= dstEnd-uint(log2) {
res = false
break
}
// Write every bit as a byte except the most significant one
for log2 > 0 {
log2--
dst[dstIdx] = byte((runLength >> log2) & 1)
dstIdx++
}
continue
}
if src[srcIdx] >= 0xFE {
if dstIdx >= dstEnd-1 {
res = false
break
}
dst[dstIdx] = 0xFF
dstIdx++
dst[dstIdx] = src[srcIdx] - 0xFE
} else {
if dstIdx >= dstEnd {
res = false
break
}
dst[dstIdx] = src[srcIdx] + 1
}
srcIdx++
dstIdx++
}
var err error
if srcIdx != srcEnd || res == false {
err = errors.New("Output buffer is too small")
}
return srcIdx, dstIdx, err
}
// Inverse applies the reverse function to the src and writes the result
// to the destination. Returns number of bytes read, number of bytes
// written and possibly an error.
func (this *ZRLT) Inverse(src, dst []byte) (uint, uint, error) {
if len(src) == 0 {
return 0, 0, nil
}
if &src[0] == &dst[0] {
return 0, 0, errors.New("Input and output buffers cannot be equal")
}
srcEnd, dstEnd := len(src), len(dst)
srcIdx, dstIdx := 0, 0
runLength := 0
var err error
for {
if src[srcIdx] <= 1 {
// Generate the run length bit by bit (but force MSB)
runLength = 1
for src[srcIdx] <= 1 {
runLength += (runLength + int(src[srcIdx]))
srcIdx++
if srcIdx >= srcEnd {
goto End
}
}
runLength--
if runLength > 0 {
if dstIdx+runLength > dstEnd {
break
}
for runLength > 0 {
runLength--
dst[dstIdx] = 0
dstIdx++
}
}
}
// Regular data processing
if src[srcIdx] == 0xFF {
srcIdx++
if srcIdx >= srcEnd {
break
}
dst[dstIdx] = 0xFE + src[srcIdx]
} else {
dst[dstIdx] = src[srcIdx] - 1
}
srcIdx++
dstIdx++
if srcIdx >= srcEnd {
break
}
}
End:
if runLength > 0 {
runLength--
// If runLength is not 1, add trailing 0s
if dstIdx+runLength > dstEnd {
err = errors.New("Output buffer is too small")
} else {
for runLength > 0 {
runLength--
dst[dstIdx] = 0
dstIdx++
}
}
}
if srcIdx < srcEnd {
err = errors.New("Output buffer is too small")
}
return uint(srcIdx), uint(dstIdx), err
}
// MaxEncodedLen returns the max size required for the encoding output buffer
func (this *ZRLT) MaxEncodedLen(srcLen int) int {
return srcLen
}