Skip to content

Commit

Permalink
packfile: use network byte order functions in reader&writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen committed Jan 19, 2021
1 parent 665a0dc commit 744417a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packfile/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func readFileHeader(r io.Reader) (uint32, error) {
if buf[0] != 'P' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'K' {
return 0, errors.New("packfile: read header: incorrect signature")
}
version := uint32(buf[4])<<24 | uint32(buf[5])<<16 | uint32(buf[6])<<8 | uint32(buf[7])
version := ntohl(buf[4:])
if version != 2 {
return 0, fmt.Errorf("packfile: read header: version is %d (only supports 2)", version)
}
nobjs := uint32(buf[8])<<24 | uint32(buf[9])<<16 | uint32(buf[10])<<8 | uint32(buf[11])
nobjs := ntohl(buf[8:])
return nobjs, nil
}

Expand Down
12 changes: 5 additions & 7 deletions packfile/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@ func (w *Writer) init() error {
if w.wc.n > 0 {
return nil
}
_, err := w.wc.Write([]byte{
fileHeader := []byte{
'P', 'A', 'C', 'K',
0, 0, 0, 2, // version 2
byte(w.nobjs >> 24),
byte(w.nobjs >> 16),
byte(w.nobjs >> 8),
byte(w.nobjs),
})
if err != nil {
0, 0, 0, 0,
}
htonl(fileHeader[8:], w.nobjs)
if _, err := w.wc.Write(fileHeader); err != nil {
return fmt.Errorf("packfile: write header: %w", err)
}
return nil
Expand Down

0 comments on commit 744417a

Please sign in to comment.