func (s *StrucStream) Pack(vals ...interface{}) error { for _, val := range vals { err := struc.PackWithOptions(s.Stream, val, s.Options) if err != nil { return err } } return nil }
func (s *SaveHeader) PackBody(b *SaveBody) error { var tmp bytes.Buffer gz := zlib.NewWriter(&tmp) err := struc.PackWithOptions(gz, b, &struc.Options{Order: binary.BigEndian}) if err != nil { return err } s.Compressed = tmp.Bytes() return nil }
// TODO: pack using all structs above instead of just header func Save(u Usercorn) ([]byte, error) { var buf bytes.Buffer arch := u.Arch() options := &struc.Options{Order: binary.BigEndian} // build compressed body s := StrucStream{&buf, options} // register list s.Pack(uint64(len(arch.Regs))) for _, enum := range arch.Regs { val, _ := u.RegRead(enum) s.Pack(uint64(enum), uint64(val)) } // memory mappings mappings := u.Mappings() s.Pack(uint64(len(mappings))) for _, m := range mappings { s.Pack(uint64(m.Addr), uint64(m.Size), uint32(m.Prot)) mem, err := u.MemRead(m.Addr, m.Size) if err != nil { fmt.Fprintf(os.Stderr, "Warning: error saving memory at 0x%x-0x%x: %s\n", m.Addr, m.Addr+m.Size, err) continue } buf.Write(mem) } // compress body var tmp bytes.Buffer gz := zlib.NewWriter(&tmp) buf.WriteTo(gz) gz.Close() buf.Reset() pc, _ := u.RegRead(arch.PC) // write header / combine everything major, minor := uc.Version() header := &SaveHeader{ Magic: SAVE_MAGIC, Version: 1, UcMajor: uint32(major), UcMinor: uint32(minor), UcArch: uint32(arch.UC_ARCH), UcMode: uint32(arch.UC_MODE), PC: pc, Compressed: tmp.Bytes(), } var final bytes.Buffer struc.PackWithOptions(&final, header, options) return final.Bytes(), nil }
func SetupElfAuxv(u models.Usercorn) ([]byte, error) { var buf bytes.Buffer auxv, err := setupElfAuxv(u) if err != nil { return nil, err } options := &struc.Options{ PtrSize: int(u.Bits()), Order: u.ByteOrder(), } for _, a := range auxv { if err := struc.PackWithOptions(&buf, &a, options); err != nil { return nil, err } } return buf.Bytes(), err }