Ultima attività 6 months ago

scusi ha revisionato questo gist 6 months ago. Vai alla revisione

Nessuna modifica

Florian Walther ha revisionato questo gist 11 years ago. Vai alla revisione

1 file changed, 57 insertions

parseFritzBpjmFile.go(file creato)

@@ -0,0 +1,57 @@
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 + }
Più nuovi Più vecchi