forked from chrisfarms/nntp
/
yenc_reader.go
96 lines (83 loc) · 1.67 KB
/
yenc_reader.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
package nntp
import (
"bufio"
"bytes"
"fmt"
"io"
)
type yencReader struct {
r *bufio.Reader
readHeader bool
awaitingSpecial bool
eof bool
buf []byte
}
func (y *yencReader) Read(p []byte) (n int, err error) {
if len(y.buf) == 0 {
if err = y.nextLine(); err != nil {
return
}
}
n = copy(p, y.buf)
y.buf = y.buf[n:]
return
}
func (y *yencReader) Close() {
// read until EOF
for {
if y.eof {
break
}
y.nextLine()
}
}
func (y *yencReader) nextLine() error {
if y.eof {
return io.EOF
}
// read a whole line
line, err := y.r.ReadBytes('\n')
if err != nil {
return err
}
// chomp the line ending
line = bytes.TrimRight(line, "\r\n")
if !y.readHeader {
// expect a =ybegin line
if len(line) >= 7 && string(line[:7]) == "=ybegin" {
y.readHeader = true
return nil
} else {
return fmt.Errorf("expected =ybegin, got %q", string(line))
}
}
// are we at the end of the yenc blob?
if len(line) >= 5 && string(line[:5]) == "=yend" {
// remember this and signal the caller
y.eof = true
return io.EOF
}
y.buf = y.decode(line)
return nil
}
func (y *yencReader) decode(line []byte) []byte {
i, j := 0, 0
for ; i < len(line); i, j = i+1, j+1 {
// escaped chars yenc42+yenc64
if y.awaitingSpecial {
line[j] = (((line[i] - 42) & 255) - 64) & 255
y.awaitingSpecial = false
// if escape char - then skip and backtrack j
} else if line[i] == '=' {
y.awaitingSpecial = true
j--
continue
// normal char, yenc42
} else {
line[j] = (line[i] - 42) & 255
}
}
// return the new (possibly shorter) slice
// shorter because of the escaped chars
return line[:len(line)-(i-j)]
}