github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/internal/wasm/binary/limits.go (about) 1 package binary 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/bananabytelabs/wazero/internal/leb128" 8 ) 9 10 // decodeLimitsType returns the `limitsType` (min, max) decoded with the WebAssembly 1.0 (20191205) Binary Format. 11 // 12 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#limits%E2%91%A6 13 func decodeLimitsType(r *bytes.Reader) (min uint32, max *uint32, err error) { 14 var flag byte 15 if flag, err = r.ReadByte(); err != nil { 16 err = fmt.Errorf("read leading byte: %v", err) 17 return 18 } 19 20 switch flag { 21 case 0x00: 22 min, _, err = leb128.DecodeUint32(r) 23 if err != nil { 24 err = fmt.Errorf("read min of limit: %v", err) 25 } 26 case 0x01: 27 min, _, err = leb128.DecodeUint32(r) 28 if err != nil { 29 err = fmt.Errorf("read min of limit: %v", err) 30 return 31 } 32 var m uint32 33 if m, _, err = leb128.DecodeUint32(r); err != nil { 34 err = fmt.Errorf("read max of limit: %v", err) 35 } else { 36 max = &m 37 } 38 default: 39 err = fmt.Errorf("%v for limits: %#x != 0x00 or 0x01", ErrInvalidByte, flag) 40 } 41 return 42 }