aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/ulikunitz/xz/lzma/operation.go
blob: a75c9b46c52d2759b9c41486a5d505270a233881 (plain)
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
// Copyright 2014-2019 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package lzma

import (
	"errors"
	"fmt"
	"unicode"
)

// operation represents an operation on the dictionary during encoding or
// decoding.
type operation interface {
	Len() int
}

// rep represents a repetition at the given distance and the given length
type match struct {
	// supports all possible distance values, including the eos marker
	distance int64
	// length
	n int
}

// verify checks whether the match is valid. If that is not the case an
// error is returned.
func (m match) verify() error {
	if !(minDistance <= m.distance && m.distance <= maxDistance) {
		return errors.New("distance out of range")
	}
	if !(1 <= m.n && m.n <= maxMatchLen) {
		return errors.New("length out of range")
	}
	return nil
}

// l return the l-value for the match, which is the difference of length
// n and 2.
func (m match) l() uint32 {
	return uint32(m.n - minMatchLen)
}

// dist returns the dist value for the match, which is one less of the
// distance stored in the match.
func (m match) dist() uint32 {
	return uint32(m.distance - minDistance)
}

// Len returns the number of bytes matched.
func (m match) Len() int {
	return m.n
}

// String returns a string representation for the repetition.
func (m match) String() string {
	return fmt.Sprintf("M{%d,%d}", m.distance, m.n)
}

// lit represents a single byte literal.
type lit struct {
	b byte
}

// Len returns 1 for the single byte literal.
func (l lit) Len() int {
	return 1
}

// String returns a string representation for the literal.
func (l lit) String() string {
	var c byte
	if unicode.IsPrint(rune(l.b)) {
		c = l.b
	} else {
		c = '.'
	}
	return fmt.Sprintf("L{%c/%02x}", c, l.b)
}