github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/embedbin/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 embedbin 6 7 import ( 8 "encoding/binary" 9 "errors" 10 "io" 11 12 "github.com/cespare/xxhash" 13 ) 14 15 const magic uint32 = 0xec90a479 16 const headersz = 20 17 18 var ( 19 errNoFooter = errors.New("binary contains no footer") 20 21 bin = binary.LittleEndian 22 ) 23 24 func writeFooter(w io.Writer, offset int64) (int, error) { 25 var p [headersz]byte 26 bin.PutUint64(p[:8], uint64(offset)) 27 bin.PutUint32(p[8:12], magic) 28 bin.PutUint64(p[12:20], xxhash.Sum64(p[:12])) 29 return w.Write(p[:]) 30 } 31 32 func readFooter(r io.ReaderAt, size int64) (offset int64, err error) { 33 if size < headersz { 34 return 0, errNoFooter 35 } 36 var p [headersz]byte 37 _, err = r.ReadAt(p[:], size-headersz) 38 if err != nil { 39 return 0, err 40 } 41 if bin.Uint32(p[8:12]) != magic { 42 return 0, errNoFooter 43 } 44 offset = int64(bin.Uint64(p[:8])) 45 if xxhash.Sum64(p[:12]) != bin.Uint64(p[12:20]) { 46 return 0, ErrCorruptedImage 47 } 48 return 49 }