parseFritzBpjmFile.go
· 1.2 KiB · Go
Raw
// parse a FritzBox Bpjm File
package main
import (
"os"
"fmt"
"bytes"
"io"
"io/ioutil"
)
// error checking function that panics on error
func check(err error) {
if err != nil {
panic(err)
}
}
// cut away null byte padding
func cutNullBytes (data []byte) (nd []byte) {
nd = bytes.Trim(data, string(0x00))
return nd
}
func main() {
// get filename to parse
filename := os.Args[1]
// read in file
dat, err := ioutil.ReadFile(filename)
check(err)
// make a bytes buffer of file content
buf := bytes.NewBuffer(dat)
// print legth of buffer
fmt.Printf("Länge der eingelesenen Daten: %d bytes\n", buf.Len())
// extract magick number
magick := buf.Next(5)
fmt.Printf("Magick: %x\n", magick)
// extract embedded filename
embed_filename := buf.Next(59)
fmt.Printf("Filename: %s\n", string(cutNullBytes(embed_filename)))
// parse the rest of the file (actual bpjm list data)
n := 0
for {
entry := make([]byte, 33)
_, err := buf.Read(entry)
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
//fmt.Printf("%x_%04d: %x %x %x\n", magick, n, entry[0:15], entry[16:31], entry[32])
fmt.Printf("%s_%04d: %x %x %x\n", cutNullBytes(embed_filename), n, entry[0:15], entry[16:31], entry[32])
n++
}
}
| 1 | // parse a FritzBox Bpjm File |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "fmt" |
| 8 | "bytes" |
| 9 | "io" |
| 10 | "io/ioutil" |
| 11 | ) |
| 12 | |
| 13 | // error checking function that panics on error |
| 14 | func check(err error) { |
| 15 | if err != nil { |
| 16 | panic(err) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // cut away null byte padding |
| 21 | func cutNullBytes (data []byte) (nd []byte) { |
| 22 | nd = bytes.Trim(data, string(0x00)) |
| 23 | return nd |
| 24 | } |
| 25 | |
| 26 | func main() { |
| 27 | // get filename to parse |
| 28 | filename := os.Args[1] |
| 29 | // read in file |
| 30 | dat, err := ioutil.ReadFile(filename) |
| 31 | check(err) |
| 32 | // make a bytes buffer of file content |
| 33 | buf := bytes.NewBuffer(dat) |
| 34 | // print legth of buffer |
| 35 | fmt.Printf("Länge der eingelesenen Daten: %d bytes\n", buf.Len()) |
| 36 | // extract magick number |
| 37 | magick := buf.Next(5) |
| 38 | fmt.Printf("Magick: %x\n", magick) |
| 39 | // extract embedded filename |
| 40 | embed_filename := buf.Next(59) |
| 41 | fmt.Printf("Filename: %s\n", string(cutNullBytes(embed_filename))) |
| 42 | // parse the rest of the file (actual bpjm list data) |
| 43 | n := 0 |
| 44 | for { |
| 45 | entry := make([]byte, 33) |
| 46 | _, err := buf.Read(entry) |
| 47 | if err != nil { |
| 48 | if err == io.EOF { |
| 49 | break |
| 50 | } |
| 51 | panic(err) |
| 52 | } |
| 53 | //fmt.Printf("%x_%04d: %x %x %x\n", magick, n, entry[0:15], entry[16:31], entry[32]) |
| 54 | fmt.Printf("%s_%04d: %x %x %x\n", cutNullBytes(embed_filename), n, entry[0:15], entry[16:31], entry[32]) |
| 55 | n++ |
| 56 | } |
| 57 | } |