summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/nwaples/rardecode/decode29_ppm.go
blob: 39c31995841f6ab30bda3872665c0d3a349f9c86 (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
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
package rardecode

import "io"

type ppm29Decoder struct {
	m   model // ppm model
	esc byte  // escape character
	br  io.ByteReader
}

func (d *ppm29Decoder) init(br *rarBitReader) error {
	maxOrder, err := br.readBits(7)
	if err != nil {
		return err
	}
	reset := maxOrder&0x20 > 0

	// Should have flushed all unread bits from bitReader by now,
	// use underlying ByteReader
	d.br = br.r

	var maxMB int
	if reset {
		c, err := d.br.ReadByte()
		if err != nil {
			return err
		}
		maxMB = int(c) + 1
	}

	if maxOrder&0x40 > 0 {
		d.esc, err = d.br.ReadByte()
		if err != nil {
			return err
		}
	}

	maxOrder = (maxOrder & 0x1f) + 1
	if maxOrder > 16 {
		maxOrder = 16 + (maxOrder-16)*3
	}

	return d.m.init(d.br, reset, maxOrder, maxMB)
}

func (d *ppm29Decoder) reset() {
	d.esc = 2
}

func (d *ppm29Decoder) readFilterData() ([]byte, error) {
	c, err := d.m.ReadByte()
	if err != nil {
		return nil, err
	}
	n := int(c&7) + 1
	if n == 7 {
		b, err := d.m.ReadByte()
		if err != nil {
			return nil, err
		}
		n += int(b)
	} else if n == 8 {
		b, err := d.m.ReadByte()
		if err != nil {
			return nil, err
		}
		n = int(b) << 8
		b, err = d.m.ReadByte()
		if err != nil {
			return nil, err
		}
		n |= int(b)
	}

	n++
	buf := make([]byte, n)
	buf[0] = byte(c)
	for i := 1; i < n; i++ {
		buf[i], err = d.m.ReadByte()
		if err != nil {
			return nil, err
		}
	}
	return buf, nil
}

func (d *ppm29Decoder) decode(w *window) ([]byte, error) {
	c, err := d.m.ReadByte()
	if err != nil {
		return nil, err
	}
	if c != d.esc {
		w.writeByte(c)
		return nil, nil
	}
	c, err = d.m.ReadByte()
	if err != nil {
		return nil, err
	}

	switch c {
	case 0:
		return nil, endOfBlock
	case 2:
		return nil, endOfBlockAndFile
	case 3:
		return d.readFilterData()
	case 4:
		offset := 0
		for i := 0; i < 3; i++ {
			c, err = d.m.ReadByte()
			if err != nil {
				return nil, err
			}
			offset = offset<<8 | int(c)
		}
		len, err := d.m.ReadByte()
		if err != nil {
			return nil, err
		}
		w.copyBytes(int(len)+32, offset+2)
	case 5:
		len, err := d.m.ReadByte()
		if err != nil {
			return nil, err
		}
		w.copyBytes(int(len)+4, 1)
	default:
		w.writeByte(d.esc)
	}
	return nil, nil
}