Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Goでバイナリを読む+α

 Goでバイナリを読む+α

Fukuoka.go #10 で話した内容です。
#golang #go #webassembly #wasm

Atsushi Takayama

June 14, 2018
Tweet

More Decks by Atsushi Takayama

Other Decks in Technology

Transcript

  1.   % cat testdata/hello.wasm | xxd -g1 | head

    -n 30 00000000: 00 61 73 6d 0d 00 00 00 01 ae 83 80 80 00 30 60 .asm..........0` 00000010: 01 7f 01 7f 60 03 7f 7f 7f 01 7f 60 01 7c 01 7f ....`......`.|.. 00000020: 60 02 7f 7f 01 7f 60 03 7f 7f 7f 00 60 05 7f 7f `.....`.....`... 00000030: 7f 7f 7f 01 7f 60 08 7f 7f 7f 7f 7f 7f 7f 7f 01 .....`.......... 00000040: 7f 60 01 7f 00 60 06 7f 7f 7f 7f 7f 7f 01 7f 60 .`...`.........` 00000050: 02 7f 7f 00 60 04 7f 7f 7f 7f 01 7f 60 00 00 60 ....`.......`..` 00000060: 04 7f 7f 7f 7f 00 60 06 7f 7f 7f 7f 7f 7f 00 60 ......`........` 00000070: 05 7f 7f 7f 7f 7f 00 60 07 7f 7f 7f 7f 7f 7f 7f .......`........ 00000080: 01 7f 60 06 7f 7f 7f 7f 7f 7c 01 7f 60 05 7f 7f ..`......|..`... 00000090: 7e 7f 7f 00 60 0b 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f ~...`........... 000000a0: 7f 01 7f 60 0f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f 7f ...`............ 000000b0: 7f 7f 7f 7f 00 60 03 7f 7f 7f 01 7d 60 07 7f 7f .....`.....}`... 000000c0: 7f 7f 7f 7f 7f 00 60 0a 7f 7f 7f 7f 7f 7f 7f 7f ......`......... 000000d0: 7f 7f 00 60 03 7f 7f 7f 01 7c 60 00 01 7f 60 04 ...`.....|`...`. 000000e0: 7f 7f 7f 7f 01 7e 60 05 7f 7f 7f 7f 7e 01 7f 60 .....~`.....~..` 000000f0: 05 7f 7f 7f 7f 7c 01 7f 60 0c 7f 7f 7f 7f 7f 7f .....|..`....... 00000100: 7f 7f 7f 7f 7f 7f 01 7f 60 10 7f 7f 7f 7f 7f 7f ........`.......
  2.   type Module struct { Header ModuleHeader Sections []Section

    } type ModuleHeader struct { Magic [4]byte Version uint32 } type Section struct { ... }
  3.   type Reader interface { Read(p []byte) (n int,

    err error) } ετϦʔϜΛҙࣝ͢Δͱ͖͸ io.Reader Λ࢖͏ io.Reader
  4.   // 4όΠτΛio.Reader͔ΒಡΈࠐΉ var buf [4]byte num, err :=

    r.Read(buf[:]) [4]byte ͸ʮ഑ྻʯ buf[0:4] ͱ͔ buf[:] Ͱ഑ྻͷҰ෦΁ͷϙΠϯλʔΛؚΉʮεϥΠεʯ Readʹ౉ͤΔͷ͸ʮεϥΠεʯ NόΠτಡΉ
  5.   import "encoding/binary" var num int64 err := binary.Read(r,

    binary.LittleEndian, &num) binary.Read ͷୈࡾҾ਺͸ interface{} ৭ΜͳܕͷϙΠϯλʔ͕౉ͤΔ int/floatΛಡΉ
  6.   b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21,

    0x09, 0x40, 0xff, 0x01, 0x02, 0x03, 0xbe, 0xef} r := bytes.NewReader(b) var data struct { PI float64 Uate uint8 Mine [3]byte Too uint16 } err := binary.Read(r, binary.LittleEndian, &data) ߏ଄ମΛؙ͝ͱಡΉ
  7.   fmt.Printf("%v\n", data) // => {3.141592653589793 255 [1 2

    3] 61374} fmt.Printf("%+v\n", data) // => {PI:3.141592653589793 Uate:255 Mine:[1 2 3] Too:61374} fmt.Printf("%#v\n", data) // => struct { PI float64; Uate uint8; Mine [3]uint8; Too uint16 }{PI: 3.141592653589793, Uate:0xff, Mine:[3]uint8{0x1, 0x2, 0x3}, Too:0xefbe} fmtͷ%v
  8.   var buf [4]byte num, err := r.Read(buf[:]) if

    err != nil { // Τϥʔॲཧ } var num int64 err = binary.Read(r, binary.LittleEndian, &num) if err != nil { // Τϥʔॲཧ } err = binary.Read(r, binary.LittleEndian, &data) if err != nil { // Τϥʔॲཧ
  9.   type decoder struct { r io.Reader err error

    } func (d *decoder) read(r io.Reader, buf []byte) { if d.err != nil || len(buf) == 0 { return } _, d.err = r.Read(buf) } // https://github.com/sbinet/wasm/blob/master/decoder.go
  10.   • Rob Pikeઌੜᐌ͘ɺ • Errors are values (Τϥʔ͸ͨͩͷฦΓ஋)

    • Τϥʔ͸ྫ֎ͱ͸ҧ͏ɻνΣοΫ͢ΔͷͰ͸ͳ͘ɺ ద੾ʹॲཧͤΑ • https://youtu.be/PAAkCSZUG1c?t=16m14s