-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdump.go
70 lines (60 loc) · 1.41 KB
/
dump.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
package cdb
import (
"bufio"
"encoding/binary"
"fmt"
"io"
)
// Dump reads the cdb-formatted data in r and dumps it as a series of formatted
// records (+klen,dlen:key->data\n) and a final newline to w.
// The output of Dump is suitable as input to Make.
// See http://cr.yp.to/cdb/cdbmake.html for details on the record format.
func Dump(w io.Writer, r io.Reader) (err error) {
defer func() { // Centralize exception handling.
if e := recover(); e != nil {
err = e.(error)
}
}()
rb := bufio.NewReader(r)
readNum := makeNumReader(rb)
rw := &recWriter{bufio.NewWriter(w)}
eod := readNum()
// Read rest of header.
for i := 0; i < 511; i++ {
readNum()
}
pos := headerSize
for pos < eod {
klen, dlen := readNum(), readNum()
rw.writeString(fmt.Sprintf("+%d,%d:", klen, dlen))
rw.copyn(rb, klen)
rw.writeString("->")
rw.copyn(rb, dlen)
rw.writeString("\n")
pos += 8 + klen + dlen
}
rw.writeString("\n")
return rw.Flush()
}
func makeNumReader(r io.Reader) func() uint32 {
buf := make([]byte, 4)
return func() uint32 {
if _, err := r.Read(buf); err != nil {
panic(err)
}
return binary.LittleEndian.Uint32(buf)
}
}
type recWriter struct {
*bufio.Writer
}
func (rw *recWriter) writeString(s string) {
if _, err := rw.WriteString(s); err != nil {
panic(err)
}
}
func (rw *recWriter) copyn(r io.Reader, n uint32) {
if _, err := io.CopyN(rw, r, int64(n)); err != nil {
panic(err)
}
}