Skip to content

Instantly share code, notes, and snippets.

@bprosnitz
Last active November 23, 2015 17:24
Show Gist options
  • Save bprosnitz/71eb255ff49af7827f05 to your computer and use it in GitHub Desktop.
Save bprosnitz/71eb255ff49af7827f05 to your computer and use it in GitHub Desktop.
Parse mojo bytes and group by allocations
package main
import (
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"log"
)
func main() {
flag.Parse()
input := flag.Arg(0)
b, err := hex.DecodeString(input)
if err != nil {
panic(err)
}
for i := 0; i < len(b); {
if i+4 > len(b) {
log.Fatalf("ERROR => Partial size in header")
}
if i+8 > len(b) {
log.Fatalf("ERROR => Partial elem count in header")
}
headSize := int(binary.LittleEndian.Uint32(b[i : i+4]))
if headSize != 0 && headSize < 8 {
log.Fatalf("ERROR => Size was %v, but size must be a minimum of 8 in length for just the header.", headSize)
}
elemCount := binary.LittleEndian.Uint32(b[i+4 : i+8])
if i+headSize > len(b) || i+8 > len(b) {
log.Fatalf("ERROR => Cannot perform full read (%v readable elements, but expected %v)", len(b)-i-1, headSize)
}
end := headSize
if headSize == 0 {
end = 8
}
data := b[i+8 : i+end]
padding := 8 - (end % 8)
if padding == 8 {
padding = 0
}
fmt.Printf("%d\t[%d %d] % x (%d bytes padding)\n", i, headSize, elemCount, data, padding)
i += end + padding
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment