Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to parse a binary file in Go? Snippet based on MoPaQ SC2 replay parsing. Related blog post: http://www.jonathan-petitcolas.com/2014/09/25/parsing-binary-files-in-go.html
package main
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"os"
)
type Header struct {
UserDataMaxSize uint32
HeaderOffset uint32
UserDataSize uint32
_ [5]byte
Starcraft2 [22]byte
}
func main() {
path := "replay.SC2Replay"
file, err := os.Open(path)
if err != nil {
log.Fatal("Error while opening file", err)
}
defer file.Close()
fmt.Printf("%s opened\n", path)
formatName := readNextBytes(file, 4)
fmt.Printf("Parsed format: %s\n", formatName)
if string(formatName) != "MPQ\x1b" {
log.Fatal("Provided replay file is not in correct format. Are you sure this is a SC2 replay file?")
}
header := Header{}
data := readNextBytes(file, 39) // 3 * uint32 (4) + 5 * byte (1) + 22 * byte (1) = 43
buffer := bytes.NewBuffer(data)
err = binary.Read(buffer, binary.LittleEndian, &header)
if err != nil {
log.Fatal("binary.Read failed", err)
}
fmt.Printf("Parsed data:\n%+v\n", header)
}
func readNextBytes(file *os.File, number int) []byte {
bytes := make([]byte, number)
_, err := file.Read(bytes)
if err != nil {
log.Fatal(err)
}
return bytes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment