-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmatch.go
101 lines (88 loc) · 2 KB
/
match.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
package lzo
func (ctx *compressor) initMatch(s *swd, flags uint32) {
s.ctx = ctx
s.init()
if flags&1 != 0 {
s.UseBestOff = true
}
}
func (ctx *compressor) findMatch(s *swd, thislen uint, skip uint) {
if skip > 0 {
if thislen < skip {
panic("assert: findMatch: invalid thislen")
}
s.accept(thislen - skip)
ctx.textsize += thislen - skip + 1
} else {
if thislen > 1 {
panic("assert: findMatch: invalid thislen")
}
ctx.textsize += thislen - skip
}
s.MLen = cSWD_THRESHOLD
s.MOff = 0
for i := 0; i < len(s.bestPos); i++ {
s.bestPos[i] = 0
}
s.findbest()
ctx.mlen = int(s.MLen)
ctx.moff = int(s.MOff)
s.getbyte()
if s.BChar < 0 {
ctx.look = 0
ctx.mlen = 0
} else {
ctx.look = s.Look + 1
}
ctx.bp = ctx.ip - int(ctx.look)
}
func (ctx *compressor) betterMatch(s *swd, imlen, imoff int) (mlen int, moff int) {
mlen, moff = imlen, imoff
if mlen <= m2_MIN_LEN {
return
}
if moff <= m2_MAX_OFFSET {
return
}
if moff > m2_MAX_OFFSET && mlen >= m2_MIN_LEN+1 && mlen <= m2_MAX_LEN+1 &&
s.BestOff[mlen-1] > 0 && s.BestOff[mlen-1] <= m2_MAX_OFFSET {
mlen -= 1
moff = int(s.BestOff[mlen])
return
}
if moff > m3_MAX_OFFSET && mlen >= m4_MAX_LEN+1 && mlen <= m2_MAX_LEN+2 &&
s.BestOff[mlen-2] > 0 && s.BestOff[mlen-2] <= m2_MAX_OFFSET {
mlen -= 2
moff = int(s.BestOff[mlen])
return
}
if moff > m3_MAX_OFFSET && mlen >= m4_MAX_LEN+1 && mlen <= m3_MAX_LEN+1 &&
s.BestOff[mlen-1] > 0 && s.BestOff[mlen-1] <= m3_MAX_OFFSET {
mlen -= 1
moff = int(s.BestOff[mlen])
return
}
return
}
func assertMemcmp(b1, b2 []byte, l int) {
b1 = b1[:l]
b2 = b2[:l]
for i := 0; i < len(b1); i++ {
if b1[i] != b2[i] {
panic("assertMemcmp: dosn't match")
}
}
}
func (ctx *compressor) assertMatch(s *swd, mlen, moff int) {
if mlen < 2 {
panic("assertMatch: invalid mlen")
}
if moff <= ctx.bp {
if ctx.bp-moff+mlen >= ctx.ip {
panic("assertMatch: invalid bp")
}
assertMemcmp(ctx.in[ctx.bp:], ctx.in[ctx.bp-moff:], mlen)
} else {
panic("dict should not exit")
}
}