I'm trying to make a MOBI file parser and I'm running into a bit of an issue with trying to parse some of the binary into a struct with binary.Read().
I'm thinking it's an alignment issue, but I'm at a loss for why I'm not getting expected values. I've run the .mobi file through libmobi to test my code's output against, as well as inspected the binary of the .mobi in order to verify that I'm not crazy and the libmobi code wasn't doing something weird (which it's not).
Here's a stripped-down example:
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
type Header struct {
Type [4]byte
Creator [4]byte
Uid uint32
Next uint32
RecordCount uint16
}
func main() {
testBytes := []byte{66, 79, 79, 75, 77, 79, 66, 73, 0, 0, 1, 17, 0, 0, 0, 0, 0, 136}
h := Header{}
buf := bytes.NewBuffer(testBytes)
binary.Read(buf, binary.LittleEndian, &h)
fmt.Printf("%s
", h.Type) // BOOK, as expected
fmt.Printf("%s
", h.Creator) // MOBI, as expected
fmt.Printf("%d
", h.Next) // 0, as expected
fmt.Printf("%d
", h.Uid)
// expecting Uid to be 273, but it's 285278208...
fmt.Printf("%d
", h.RecordCount)
// expecting RecordCount to be 136, but it's 34816...
}
Any help would be greatly appreciated!
EDIT: here are the hex bytes from doing a xxd
on book.mobi
:
424f 4f4b 4d4f 4249 0000 0111 0000 0000 0088