github.com/grailbio/base@v0.0.11/fatbin/footer.go (about) 1 // Copyright 2019 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache 2.0 3 // license that can be found in the LICENSE file. 4 5 package fatbin 6 7 import ( 8 "encoding/binary" 9 "errors" 10 "io" 11 12 "github.com/cespare/xxhash" 13 ) 14 15 const ( 16 magic uint32 = 0x5758ba2c 17 headersz = 20 18 ) 19 20 var ( 21 errNoFooter = errors.New("binary contains no footer") 22 23 bin = binary.LittleEndian 24 ) 25 26 func writeFooter(w io.Writer, offset int64) (int, error) { 27 var p [headersz]byte 28 bin.PutUint64(p[:8], uint64(offset)) 29 bin.PutUint32(p[8:12], magic) 30 bin.PutUint64(p[12:20], xxhash.Sum64(p[:12])) 31 return w.Write(p[:]) 32 } 33 34 func readFooter(r io.ReaderAt, size int64) (offset int64, err error) { 35 if size < headersz { 36 return 0, errNoFooter 37 } 38 var p [headersz]byte 39 _, err = r.ReadAt(p[:], size-headersz) 40 if err != nil { 41 return 0, err 42 } 43 if bin.Uint32(p[8:12]) != magic { 44 return 0, errNoFooter 45 } 46 offset = int64(bin.Uint64(p[:8])) 47 if xxhash.Sum64(p[:12]) != bin.Uint64(p[12:20]) { 48 return 0, ErrCorruptedImage 49 } 50 return 51 }