diff --git a/packfile/reader.go b/packfile/reader.go index f25c8d5..ec597fb 100644 --- a/packfile/reader.go +++ b/packfile/reader.go @@ -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 } diff --git a/packfile/writer.go b/packfile/writer.go index 1f7a602..c40509d 100644 --- a/packfile/writer.go +++ b/packfile/writer.go @@ -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