github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/pkg/abi/flag.go (about) 1 // Copyright 2018 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package abi 16 17 import ( 18 "fmt" 19 "math" 20 "strconv" 21 "strings" 22 ) 23 24 // A FlagSet is a slice of bit-flags and their name. 25 type FlagSet []struct { 26 Flag uint64 27 Name string 28 } 29 30 // Parse returns a pretty version of val, using the flag names for known flags. 31 // Unknown flags remain numeric. 32 func (s FlagSet) Parse(val uint64) string { 33 var flags []string 34 35 for _, f := range s { 36 if val&f.Flag == f.Flag { 37 flags = append(flags, f.Name) 38 val &^= f.Flag 39 } 40 } 41 42 if val != 0 { 43 flags = append(flags, "0x"+strconv.FormatUint(val, 16)) 44 } 45 46 return strings.Join(flags, "|") 47 } 48 49 // ValueSet is a slice of syscall values and their name. Parse will replace 50 // values that exactly match an entry with its name. 51 type ValueSet []struct { 52 Value uint64 53 Name string 54 } 55 56 // Parse returns the name of the value associated with `val`. Unknown values 57 // are converted to hex. 58 func (e ValueSet) Parse(val uint64) string { 59 for _, f := range e { 60 if val == f.Value { 61 return f.Name 62 } 63 } 64 return fmt.Sprintf("%#x", val) 65 } 66 67 // ParseName returns the flag value associated with 'name'. Returns false 68 // if no value is found. 69 func (e ValueSet) ParseName(name string) (uint64, bool) { 70 for _, f := range e { 71 if name == f.Name { 72 return f.Value, true 73 } 74 } 75 return math.MaxUint64, false 76 }