1// Copyright 2014-2017 Ulrich Kunitz. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package lzma 6 7import ( 8 "errors" 9 "fmt" 10 "unicode" 11) 12 13// operation represents an operation on the dictionary during encoding or 14// decoding. 15type operation interface { 16 Len() int 17} 18 19// rep represents a repetition at the given distance and the given length 20type match struct { 21 // supports all possible distance values, including the eos marker 22 distance int64 23 // length 24 n int 25} 26 27// verify checks whether the match is valid. If that is not the case an 28// error is returned. 29func (m match) verify() error { 30 if !(minDistance <= m.distance && m.distance <= maxDistance) { 31 return errors.New("distance out of range") 32 } 33 if !(1 <= m.n && m.n <= maxMatchLen) { 34 return errors.New("length out of range") 35 } 36 return nil 37} 38 39// l return the l-value for the match, which is the difference of length 40// n and 2. 41func (m match) l() uint32 { 42 return uint32(m.n - minMatchLen) 43} 44 45// dist returns the dist value for the match, which is one less of the 46// distance stored in the match. 47func (m match) dist() uint32 { 48 return uint32(m.distance - minDistance) 49} 50 51// Len returns the number of bytes matched. 52func (m match) Len() int { 53 return m.n 54} 55 56// String returns a string representation for the repetition. 57func (m match) String() string { 58 return fmt.Sprintf("M{%d,%d}", m.distance, m.n) 59} 60 61// lit represents a single byte literal. 62type lit struct { 63 b byte 64} 65 66// Len returns 1 for the single byte literal. 67func (l lit) Len() int { 68 return 1 69} 70 71// String returns a string representation for the literal. 72func (l lit) String() string { 73 var c byte 74 if unicode.IsPrint(rune(l.b)) { 75 c = l.b 76 } else { 77 c = '.' 78 } 79 return fmt.Sprintf("L{%c/%02x}", c, l.b) 80} 81