This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
decode.go
66 lines (64 loc) · 1.55 KB
/
decode.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
package main
import (
"bufio"
"io"
)
// decode all incoming data. Pass data through if not valid encoding
func decode(r *bufio.Reader, w *bufio.Writer) {
var err error
var b byte
var n int
defer w.Flush() // explicit flush at end of stream
t := []byte{0, 0, 0} // buffer used for left-shifting
offs := 0 // offset to fill t
currRound := 0 // current round specified by -r/--rounds
OUTER:
for {
// try to read 3-offs bytes at once
read := 0
for read != 3-offs {
if n, err = r.Read(t[offs+read:]); err != nil {
if err == io.EOF {
// we may have to Write unhandled bytes after EOF
if read > offs {
offs = read
}
break OUTER
}
check(err, w)
}
read += n
}
// t probably contains a valid url code
if t[0] == '%' {
// try to decode
if b, err = unhex(t[1], t[2]); err == nil {
// decode ok!
if b == '%' && currRound < fRounds-1 {
// in case we have multiple rounds to do, keep the decoded
// '%' character in buffer
t[0] = b
offs = 1
currRound++
continue
}
// character is not '%' or we have no more rounds to do
// pass character to output
check(w.WriteByte(b), w)
offs = 0
currRound = 0
continue
}
}
// t probably doesn't contain a valid url code. Write first char in
// t to output and shift t by one in order to get new characters from
// stream
check(w.WriteByte(t[0]), w)
copy(t, t[1:])
offs = 2
currRound = 0
}
// flush everything that remains unhandled in t
_, err = w.Write(t[:offs])
check(err, w)
}