forked from rsc/pdf
-
Notifications
You must be signed in to change notification settings - Fork 152
/
ascii85.go
53 lines (46 loc) · 989 Bytes
/
ascii85.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
// file with help function for ascii85 decoder
// later if new decoders is going to add it reasonable to rename file and add them here
// also create interfaces to switch between them (like in unidoc)
package pdf
import (
"io"
)
type alphaReader struct {
reader io.Reader
}
func newAlphaReader(reader io.Reader) *alphaReader {
return &alphaReader{reader: reader}
}
func checkASCII85(r byte) byte {
if r >= '!' && r <= 'u' { // 33 <= ascii85 <=117
return r
}
if r == '~' {
return 1 // for marking possible end of data
}
return 0 // if non-ascii85
}
func (a *alphaReader) Read(p []byte) (int, error) {
n, err := a.reader.Read(p)
if err == io.EOF {
}
if err != nil {
return n, err
}
buf := make([]byte, n)
tilda := false
for i := 0; i < n; i++ {
char := checkASCII85(p[i])
if char == '>' && tilda { // end of data
break
}
if char > 1 {
buf[i] = char
}
if char == 1 {
tilda = true // possible end of data
}
}
copy(p, buf)
return n, nil
}