2018-10-08 16:25:57 +02:00
|
|
|
package readers
|
|
|
|
|
2020-06-13 11:35:36 +02:00
|
|
|
import (
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2020-06-13 11:35:36 +02:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is the smallest prime less than 256
|
|
|
|
//
|
|
|
|
// Using a prime here means we are less likely to hit repeating patterns
|
|
|
|
const patternReaderModulo = 251
|
2018-10-08 16:25:57 +02:00
|
|
|
|
|
|
|
// NewPatternReader creates a reader, that returns a deterministic byte pattern.
|
|
|
|
// After length bytes are read
|
2020-06-13 11:35:36 +02:00
|
|
|
func NewPatternReader(length int64) io.ReadSeeker {
|
2018-10-08 16:25:57 +02:00
|
|
|
return &patternReader{
|
|
|
|
length: length,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type patternReader struct {
|
2020-06-13 11:35:36 +02:00
|
|
|
offset int64
|
2018-10-08 16:25:57 +02:00
|
|
|
length int64
|
|
|
|
c byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *patternReader) Read(p []byte) (n int, err error) {
|
|
|
|
for i := range p {
|
2020-06-13 11:35:36 +02:00
|
|
|
if r.offset >= r.length {
|
2018-10-08 16:25:57 +02:00
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
p[i] = r.c
|
2020-06-13 11:35:36 +02:00
|
|
|
r.c = (r.c + 1) % patternReaderModulo
|
|
|
|
r.offset++
|
2018-10-08 16:25:57 +02:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-06-13 11:35:36 +02:00
|
|
|
|
|
|
|
// Seek implements the io.Seeker interface.
|
|
|
|
func (r *patternReader) Seek(offset int64, whence int) (abs int64, err error) {
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
abs = offset
|
|
|
|
case io.SeekCurrent:
|
|
|
|
abs = r.offset + offset
|
|
|
|
case io.SeekEnd:
|
|
|
|
abs = r.length + offset
|
|
|
|
default:
|
|
|
|
return 0, errors.New("patternReader: invalid whence")
|
|
|
|
}
|
|
|
|
if abs < 0 {
|
|
|
|
return 0, errors.New("patternReader: negative position")
|
|
|
|
}
|
|
|
|
r.offset = abs
|
|
|
|
r.c = byte(abs % patternReaderModulo)
|
|
|
|
return abs, nil
|
|
|
|
}
|