check header again

master
Jordan Orelli 8 years ago
parent 6151a1a0cc
commit 8c53711fbe

@ -68,12 +68,8 @@ func main() {
bail(1, "input error: %v", err)
}
buf := make([]byte, 8)
if _, err := r.Read(buf); err != nil {
bail(1, "error reading header: %v", err)
p := newParser(r)
if _, err := p.checkHeader(); err != nil {
bail(1, "parse error: %v", err)
}
if string(buf) != replay_header {
bail(1, "unexpected replay header: %s", string(buf))
}
fmt.Println(string(buf))
}

@ -0,0 +1,24 @@
package main
import (
"fmt"
"io"
)
type parser struct {
// the source of replay bytes. Must NOT be compressed.
source io.Reader
}
func newParser(r io.Reader) *parser {
return &parser{source: r}
}
// checks whether we have an acceptable header at the current reader position.
func (p *parser) checkHeader() (bool, error) {
buf := make([]byte, 8)
if _, err := p.source.Read(buf); err != nil {
return false, fmt.Errorf("unable to read header bytes: %v", err)
}
return string(buf) == replay_header, nil
}
Loading…
Cancel
Save