diff options
Diffstat (limited to 'vendor/github.com/lunixbochs/struc')
-rw-r--r-- | vendor/github.com/lunixbochs/struc/.travis.yml | 10 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/LICENSE | 19 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/README.md | 103 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/binary.go | 52 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/custom.go | 33 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/custom_float16.go | 78 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/field.go | 288 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/fields.go | 172 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/legacy.go | 16 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/packer.go | 13 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/parse.go | 230 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/struc.go | 117 | ||||
-rw-r--r-- | vendor/github.com/lunixbochs/struc/types.go | 136 |
13 files changed, 0 insertions, 1267 deletions
diff --git a/vendor/github.com/lunixbochs/struc/.travis.yml b/vendor/github.com/lunixbochs/struc/.travis.yml deleted file mode 100644 index 8316e89..0000000 --- a/vendor/github.com/lunixbochs/struc/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -language: go -sudo: false - -script: go test -v - -go: - - 1.2 - - 1.3 - - 1.4 - - tip diff --git a/vendor/github.com/lunixbochs/struc/LICENSE b/vendor/github.com/lunixbochs/struc/LICENSE deleted file mode 100644 index 42e8263..0000000 --- a/vendor/github.com/lunixbochs/struc/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2015 Ryan Hileman - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/vendor/github.com/lunixbochs/struc/README.md b/vendor/github.com/lunixbochs/struc/README.md deleted file mode 100644 index c813497..0000000 --- a/vendor/github.com/lunixbochs/struc/README.md +++ /dev/null @@ -1,103 +0,0 @@ -[![Build Status](https://travis-ci.org/lunixbochs/struc.svg?branch=master)](https://travis-ci.org/lunixbochs/struc) - -struc -==== - -Struc exists to pack and unpack C-style structures from bytes, which is useful for binary files and network protocols. It could be considered an alternative to `encoding/binary`, which requires massive boilerplate for some similar operations. - -Take a look at an [example comparing `struc` and `encoding/binary`](https://bochs.info/p/cxvm9) - -Struc considers usability first. That said, it does cache reflection data and aims to be competitive with `encoding/binary` struct packing in every way, including performance. - -Example struct ----- - -```Go -type Example struct { - Var int `struc:"int32,sizeof=Str"` - Str string - Weird []byte `struc:"[8]int64"` - Var []int `struc:"[]int32,little"` -} -``` - -Struct tag format ----- - - - ```Var []int `struc:"[]int32,little,sizeof=StringField"` ``` will pack Var as a slice of little-endian int32, and link it as the size of `StringField`. - - `sizeof=`: Indicates this field is a number used to track the length of a another field. `sizeof` fields are automatically updated on `Pack()` based on the current length of the tracked field, and are used to size the target field during `Unpack()`. - - Bare values will be parsed as type and endianness. - -Endian formats ----- - - - `big` (default) - - `little` - -Recognized types ----- - - - `pad` - this type ignores field contents and is backed by a `[length]byte` containing nulls - - `bool` - - `byte` - - `int8`, `uint8` - - `int16`, `uint16` - - `int32`, `uint32` - - `int64`, `uint64` - - `float32` - - `float64` - -Types can be indicated as arrays/slices using `[]` syntax. Example: `[]int64`, `[8]int32`. - -Bare slice types (those with no `[size]`) must have a linked `Sizeof` field. - -Private fields are ignored when packing and unpacking. - -Example code ----- - -```Go -package main - -import ( - "bytes" - "github.com/lunixbochs/struc" -) - -type Example struct { - A int `struc:"big"` - - // B will be encoded/decoded as a 16-bit int (a "short") - // but is stored as a native int in the struct - B int `struc:"int16"` - - // the sizeof key links a buffer's size to any int field - Size int `struc:"int8,little,sizeof=Str"` - Str string - - // you can get freaky if you want - Str2 string `struc:"[5]int64"` -} - -func main() { - var buf bytes.Buffer - t := &Example{1, 2, 0, "test", "test2"} - err := struc.Pack(&buf, t) - o := &Example{} - err = struc.Unpack(&buf, o) -} -``` - -Benchmark ----- - -`BenchmarkEncode` uses struc. `Stdlib` benchmarks use equivalent `encoding/binary` code. `Manual` encodes without any reflection, and should be considered an upper bound on performance (which generated code based on struc definitions should be able to achieve). - -``` -BenchmarkEncode 1000000 1265 ns/op -BenchmarkStdlibEncode 1000000 1855 ns/op -BenchmarkManualEncode 5000000 284 ns/op -BenchmarkDecode 1000000 1259 ns/op -BenchmarkStdlibDecode 1000000 1656 ns/op -BenchmarkManualDecode 20000000 89.0 ns/op -``` diff --git a/vendor/github.com/lunixbochs/struc/binary.go b/vendor/github.com/lunixbochs/struc/binary.go deleted file mode 100644 index 4899d08..0000000 --- a/vendor/github.com/lunixbochs/struc/binary.go +++ /dev/null @@ -1,52 +0,0 @@ -package struc - -import ( - "encoding/binary" - "io" - "reflect" -) - -type byteWriter struct { - buf []byte - pos int -} - -func (b byteWriter) Write(p []byte) (int, error) { - capacity := len(b.buf) - b.pos - if capacity < len(p) { - p = p[:capacity] - } - if len(p) > 0 { - copy(b.buf[b.pos:], p) - b.pos += len(p) - } - return len(p), nil -} - -type binaryFallback reflect.Value - -func (b binaryFallback) String() string { - return b.String() -} - -func (b binaryFallback) Sizeof(val reflect.Value, options *Options) int { - return binary.Size(val.Interface()) -} - -func (b binaryFallback) Pack(buf []byte, val reflect.Value, options *Options) (int, error) { - tmp := byteWriter{buf: buf} - var order binary.ByteOrder = binary.BigEndian - if options.Order != nil { - order = options.Order - } - err := binary.Write(tmp, order, val.Interface()) - return tmp.pos, err -} - -func (b binaryFallback) Unpack(r io.Reader, val reflect.Value, options *Options) error { - var order binary.ByteOrder = binary.BigEndian - if options.Order != nil { - order = options.Order - } - return binary.Read(r, order, val.Interface()) -} diff --git a/vendor/github.com/lunixbochs/struc/custom.go b/vendor/github.com/lunixbochs/struc/custom.go deleted file mode 100644 index c468dce..0000000 --- a/vendor/github.com/lunixbochs/struc/custom.go +++ /dev/null @@ -1,33 +0,0 @@ -package struc - -import ( - "io" - "reflect" -) - -type Custom interface { - Pack(p []byte, opt *Options) (int, error) - Unpack(r io.Reader, length int, opt *Options) error - Size(opt *Options) int - String() string -} - -type customFallback struct { - custom Custom -} - -func (c customFallback) Pack(p []byte, val reflect.Value, opt *Options) (int, error) { - return c.custom.Pack(p, opt) -} - -func (c customFallback) Unpack(r io.Reader, val reflect.Value, opt *Options) error { - return c.custom.Unpack(r, 1, opt) -} - -func (c customFallback) Sizeof(val reflect.Value, opt *Options) int { - return c.custom.Size(opt) -} - -func (c customFallback) String() string { - return c.custom.String() -} diff --git a/vendor/github.com/lunixbochs/struc/custom_float16.go b/vendor/github.com/lunixbochs/struc/custom_float16.go deleted file mode 100644 index 722be76..0000000 --- a/vendor/github.com/lunixbochs/struc/custom_float16.go +++ /dev/null @@ -1,78 +0,0 @@ -package struc - -import ( - "encoding/binary" - "io" - "math" - "strconv" -) - -type Float16 float64 - -func (f *Float16) Pack(p []byte, opt *Options) (int, error) { - order := opt.Order - if order == nil { - order = binary.BigEndian - } - sign := uint16(0) - if *f < 0 { - sign = 1 - } - var frac, exp uint16 - if math.IsInf(float64(*f), 0) { - exp = 0x1f - frac = 0 - } else if math.IsNaN(float64(*f)) { - exp = 0x1f - frac = 1 - } else { - bits := math.Float64bits(float64(*f)) - exp64 := (bits >> 52) & 0x7ff - if exp64 != 0 { - exp = uint16((exp64 - 1023 + 15) & 0x1f) - } - frac = uint16((bits >> 42) & 0x3ff) - } - var out uint16 - out |= sign << 15 - out |= exp << 10 - out |= frac & 0x3ff - order.PutUint16(p, out) - return 2, nil -} -func (f *Float16) Unpack(r io.Reader, length int, opt *Options) error { - order := opt.Order - if order == nil { - order = binary.BigEndian - } - var tmp [2]byte - if _, err := r.Read(tmp[:]); err != nil { - return err - } - val := order.Uint16(tmp[:2]) - sign := (val >> 15) & 1 - exp := int16((val >> 10) & 0x1f) - frac := val & 0x3ff - if exp == 0x1f { - if frac != 0 { - *f = Float16(math.NaN()) - } else { - *f = Float16(math.Inf(int(sign)*-2 + 1)) - } - } else { - var bits uint64 - bits |= uint64(sign) << 63 - bits |= uint64(frac) << 42 - if exp > 0 { - bits |= uint64(exp-15+1023) << 52 - } - *f = Float16(math.Float64frombits(bits)) - } - return nil -} -func (f *Float16) Size(opt *Options) int { - return 2 -} -func (f *Float16) String() string { - return strconv.FormatFloat(float64(*f), 'g', -1, 32) -} diff --git a/vendor/github.com/lunixbochs/struc/field.go b/vendor/github.com/lunixbochs/struc/field.go deleted file mode 100644 index 6e59c2a..0000000 --- a/vendor/github.com/lunixbochs/struc/field.go +++ /dev/null @@ -1,288 +0,0 @@ -package struc - -import ( - "bytes" - "encoding/binary" - "fmt" - "math" - "reflect" -) - -type Field struct { - Name string - Ptr bool - Index int - Type Type - defType Type - Array bool - Slice bool - Len int - Order binary.ByteOrder - Sizeof []int - Sizefrom []int - Fields Fields - kind reflect.Kind -} - -func (f *Field) String() string { - var out string - if f.Type == Pad { - return fmt.Sprintf("{type: Pad, len: %d}", f.Len) - } else { - out = fmt.Sprintf("type: %s, order: %v", f.Type.String(), f.Order) - } - if f.Sizefrom != nil { - out += fmt.Sprintf(", sizefrom: %v", f.Sizefrom) - } else if f.Len > 0 { - out += fmt.Sprintf(", len: %d", f.Len) - } - if f.Sizeof != nil { - out += fmt.Sprintf(", sizeof: %v", f.Sizeof) - } - return "{" + out + "}" -} - -func (f *Field) Size(val reflect.Value, options *Options) int { - typ := f.Type.Resolve(options) - size := 0 - if typ == Struct { - vals := []reflect.Value{val} - if f.Slice { - vals = make([]reflect.Value, val.Len()) - for i := 0; i < val.Len(); i++ { - vals[i] = val.Index(i) - } - } - for _, val := range vals { - size += f.Fields.Sizeof(val, options) - } - } else if typ == Pad { - size = f.Len - } else if f.Slice || f.kind == reflect.String { - length := val.Len() - if f.Len > 1 { - length = f.Len - } - size = length * typ.Size() - } else if typ == CustomType { - return val.Addr().Interface().(Custom).Size(options) - } else { - size = typ.Size() - } - align := options.ByteAlign - if align > 0 && size < align { - size = align - } - return size -} - -func (f *Field) packVal(buf []byte, val reflect.Value, length int, options *Options) (size int, err error) { - order := f.Order - if options.Order != nil { - order = options.Order - } - if f.Ptr { - val = val.Elem() - } - typ := f.Type.Resolve(options) - switch typ { - case Struct: - return f.Fields.Pack(buf, val, options) - case Bool, Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64: - size = typ.Size() - var n uint64 - switch f.kind { - case reflect.Bool: - if val.Bool() { - n = 1 - } else { - n = 0 - } - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - n = uint64(val.Int()) - default: - n = val.Uint() - } - switch typ { - case Bool: - if n != 0 { - buf[0] = 1 - } else { - buf[0] = 0 - } - case Int8, Uint8: - buf[0] = byte(n) - case Int16, Uint16: - order.PutUint16(buf, uint16(n)) - case Int32, Uint32: - order.PutUint32(buf, uint32(n)) - case Int64, Uint64: - order.PutUint64(buf, uint64(n)) - } - case Float32, Float64: - size = typ.Size() - n := val.Float() - switch typ { - case Float32: - order.PutUint32(buf, math.Float32bits(float32(n))) - case Float64: - order.PutUint64(buf, math.Float64bits(n)) - } - case String: - switch f.kind { - case reflect.String: - size = val.Len() - copy(buf, []byte(val.String())) - default: - // TODO: handle kind != bytes here - size = val.Len() - copy(buf, val.Bytes()) - } - case CustomType: - return val.Addr().Interface().(Custom).Pack(buf, options) - default: - panic(fmt.Sprintf("no pack handler for type: %s", typ)) - } - return -} - -func (f *Field) Pack(buf []byte, val reflect.Value, length int, options *Options) (int, error) { - typ := f.Type.Resolve(options) - if typ == Pad { - for i := 0; i < length; i++ { - buf[i] = 0 - } - return length, nil - } - if f.Slice { - // special case strings and byte slices for performance - end := val.Len() - if !f.Array && typ == Uint8 && (f.defType == Uint8 || f.kind == reflect.String) { - var tmp []byte - if f.kind == reflect.String { - tmp = []byte(val.String()) - } else { - tmp = val.Bytes() - } - copy(buf, tmp) - if end < length { - // TODO: allow configuring pad byte? - rep := bytes.Repeat([]byte{0}, length-end) - copy(buf[end:], rep) - return length, nil - } - return val.Len(), nil - } - pos := 0 - var zero reflect.Value - if end < length { - zero = reflect.Zero(val.Type().Elem()) - } - for i := 0; i < length; i++ { - cur := zero - if i < end { - cur = val.Index(i) - } - if n, err := f.packVal(buf[pos:], cur, 1, options); err != nil { - return pos, err - } else { - pos += n - } - } - return pos, nil - } else { - return f.packVal(buf, val, length, options) - } -} - -func (f *Field) unpackVal(buf []byte, val reflect.Value, length int, options *Options) error { - order := f.Order - if options.Order != nil { - order = options.Order - } - if f.Ptr { - val = val.Elem() - } - typ := f.Type.Resolve(options) - switch typ { - case Float32, Float64: - var n float64 - switch typ { - case Float32: - n = float64(math.Float32frombits(order.Uint32(buf))) - case Float64: - n = math.Float64frombits(order.Uint64(buf)) - } - switch f.kind { - case reflect.Float32, reflect.Float64: - val.SetFloat(n) - default: - return fmt.Errorf("struc: refusing to unpack float into field %s of type %s", f.Name, f.kind.String()) - } - case Bool, Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64: - var n uint64 - switch typ { - case Int8: - n = uint64(int64(int8(buf[0]))) - case Int16: - n = uint64(int64(int16(order.Uint16(buf)))) - case Int32: - n = uint64(int64(int32(order.Uint32(buf)))) - case Int64: - n = uint64(int64(order.Uint64(buf))) - case Bool, Uint8: - n = uint64(buf[0]) - case Uint16: - n = uint64(order.Uint16(buf)) - case Uint32: - n = uint64(order.Uint32(buf)) - case Uint64: - n = uint64(order.Uint64(buf)) - } - switch f.kind { - case reflect.Bool: - val.SetBool(n != 0) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - val.SetInt(int64(n)) - default: - val.SetUint(n) - } - default: - panic(fmt.Sprintf("no unpack handler for type: %s", typ)) - } - return nil -} - -func (f *Field) Unpack(buf []byte, val reflect.Value, length int, options *Options) error { - typ := f.Type.Resolve(options) - if typ == Pad || f.kind == reflect.String { - if typ == Pad { - return nil - } else { - val.SetString(string(buf)) - return nil - } - } else if f.Slice { - if val.Cap() < length { - val.Set(reflect.MakeSlice(val.Type(), length, length)) - } else if val.Len() < length { - val.Set(val.Slice(0, length)) - } - // special case byte slices for performance - if !f.Array && typ == Uint8 && f.defType == Uint8 { - copy(val.Bytes(), buf[:length]) - return nil - } - pos := 0 - size := typ.Size() - for i := 0; i < length; i++ { - if err := f.unpackVal(buf[pos:pos+size], val.Index(i), 1, options); err != nil { - return err - } - pos += size - } - return nil - } else { - return f.unpackVal(buf, val, length, options) - } -} diff --git a/vendor/github.com/lunixbochs/struc/fields.go b/vendor/github.com/lunixbochs/struc/fields.go deleted file mode 100644 index 5d591bf..0000000 --- a/vendor/github.com/lunixbochs/struc/fields.go +++ /dev/null @@ -1,172 +0,0 @@ -package struc - -import ( - "encoding/binary" - "fmt" - "io" - "reflect" - "strings" -) - -type Fields []*Field - -func (f Fields) SetByteOrder(order binary.ByteOrder) { - for _, field := range f { - if field != nil { - field.Order = order - } - } -} - -func (f Fields) String() string { - fields := make([]string, len(f)) - for i, field := range f { - if field != nil { - fields[i] = field.String() - } - } - return "{" + strings.Join(fields, ", ") + "}" -} - -func (f Fields) Sizeof(val reflect.Value, options *Options) int { - for val.Kind() == reflect.Ptr { - val = val.Elem() - } - size := 0 - for i, field := range f { - if field != nil { - size += field.Size(val.Field(i), options) - } - } - return size -} - -func (f Fields) sizefrom(val reflect.Value, index []int) int { - field := val.FieldByIndex(index) - switch field.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return int(field.Int()) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - n := int(field.Uint()) - // all the builtin array length types are native int - // so this guards against weird truncation - if n < 0 { - return 0 - } - return n - default: - name := val.Type().FieldByIndex(index).Name - panic(fmt.Sprintf("sizeof field %T.%s not an integer type", val.Interface(), name)) - } -} - -func (f Fields) Pack(buf []byte, val reflect.Value, options *Options) (int, error) { - for val.Kind() == reflect.Ptr { - val = val.Elem() - } - pos := 0 - for i, field := range f { - if field == nil { - continue - } - v := val.Field(i) - length := field.Len - if field.Sizefrom != nil { - length = f.sizefrom(val, field.Sizefrom) - } - if length <= 0 && field.Slice { - length = v.Len() - } - if field.Sizeof != nil { - length := val.FieldByIndex(field.Sizeof).Len() - switch field.kind { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - // allocating a new int here has fewer side effects (doesn't update the original struct) - // but it's a wasteful allocation - // the old method might work if we just cast the temporary int/uint to the target type - v = reflect.New(v.Type()).Elem() - v.SetInt(int64(length)) - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - v = reflect.New(v.Type()).Elem() - v.SetUint(uint64(length)) - default: - panic(fmt.Sprintf("sizeof field is not int or uint type: %s, %s", field.Name, v.Type())) - } - } - if n, err := field.Pack(buf[pos:], v, length, options); err != nil { - return n, err - } else { - pos += n - } - } - return pos, nil -} - -func (f Fields) Unpack(r io.Reader, val reflect.Value, options *Options) error { - for val.Kind() == reflect.Ptr { - val = val.Elem() - } - var tmp [8]byte - var buf []byte - for i, field := range f { - if field == nil { - continue - } - v := val.Field(i) - length := field.Len - if field.Sizefrom != nil { - length = f.sizefrom(val, field.Sizefrom) - } - if v.Kind() == reflect.Ptr && !v.Elem().IsValid() { - v.Set(reflect.New(v.Type().Elem())) - } - if field.Type == Struct { - if field.Slice { - vals := reflect.MakeSlice(v.Type(), length, length) - for i := 0; i < length; i++ { - v := vals.Index(i) - fields, err := parseFields(v) - if err != nil { - return err - } - if err := fields.Unpack(r, v, options); err != nil { - return err - } - } - v.Set(vals) - } else { - // TODO: DRY (we repeat the inner loop above) - fields, err := parseFields(v) - if err != nil { - return err - } - if err := fields.Unpack(r, v, options); err != nil { - return err - } - } - continue - } else { - typ := field.Type.Resolve(options) - if typ == CustomType { - if err := v.Addr().Interface().(Custom).Unpack(r, length, options); err != nil { - return err - } - } else { - size := length * field.Type.Resolve(options).Size() - if size < 8 { - buf = tmp[:size] - } else { - buf = make([]byte, size) - } - if _, err := io.ReadFull(r, buf); err != nil { - return err - } - err := field.Unpack(buf[:size], v, length, options) - if err != nil { - return err - } - } - } - } - return nil -} diff --git a/vendor/github.com/lunixbochs/struc/legacy.go b/vendor/github.com/lunixbochs/struc/legacy.go deleted file mode 100644 index 5baf70d..0000000 --- a/vendor/github.com/lunixbochs/struc/legacy.go +++ /dev/null @@ -1,16 +0,0 @@ -package struc - -import ( - "encoding/binary" - "io" -) - -// Deprecated. Use PackWithOptions. -func PackWithOrder(w io.Writer, data interface{}, order binary.ByteOrder) error { - return PackWithOptions(w, data, &Options{Order: order}) -} - -// Deprecated. Use UnpackWithOptions. -func UnpackWithOrder(r io.Reader, data interface{}, order binary.ByteOrder) error { - return UnpackWithOptions(r, data, &Options{Order: order}) -} diff --git a/vendor/github.com/lunixbochs/struc/packer.go b/vendor/github.com/lunixbochs/struc/packer.go deleted file mode 100644 index a3a91a2..0000000 --- a/vendor/github.com/lunixbochs/struc/packer.go +++ /dev/null @@ -1,13 +0,0 @@ -package struc - -import ( - "io" - "reflect" -) - -type Packer interface { - Pack(buf []byte, val reflect.Value, options *Options) (int, error) - Unpack(r io.Reader, val reflect.Value, options *Options) error - Sizeof(val reflect.Value, options *Options) int - String() string -} diff --git a/vendor/github.com/lunixbochs/struc/parse.go b/vendor/github.com/lunixbochs/struc/parse.go deleted file mode 100644 index 43c5875..0000000 --- a/vendor/github.com/lunixbochs/struc/parse.go +++ /dev/null @@ -1,230 +0,0 @@ -package struc - -import ( - "encoding/binary" - "errors" - "fmt" - "reflect" - "regexp" - "strconv" - "strings" - "sync" -) - -// struc:"int32,big,sizeof=Data,skip,sizefrom=Len" - -type strucTag struct { - Type string - Order binary.ByteOrder - Sizeof string - Skip bool - Sizefrom string -} - -func parseStrucTag(tag reflect.StructTag) *strucTag { - t := &strucTag{ - Order: binary.BigEndian, - } - tagStr := tag.Get("struc") - if tagStr == "" { - // someone's going to typo this (I already did once) - // sorry if you made a module actually using this tag - // and you're mad at me now - tagStr = tag.Get("struct") - } - for _, s := range strings.Split(tagStr, ",") { - if strings.HasPrefix(s, "sizeof=") { - tmp := strings.SplitN(s, "=", 2) - t.Sizeof = tmp[1] - } else if strings.HasPrefix(s, "sizefrom=") { - tmp := strings.SplitN(s, "=", 2) - t.Sizefrom = tmp[1] - } else if s == "big" { - t.Order = binary.BigEndian - } else if s == "little" { - t.Order = binary.LittleEndian - } else if s == "skip" { - t.Skip = true - } else { - t.Type = s - } - } - return t -} - -var typeLenRe = regexp.MustCompile(`^\[(\d*)\]`) - -func parseField(f reflect.StructField) (fd *Field, tag *strucTag, err error) { - tag = parseStrucTag(f.Tag) - var ok bool - fd = &Field{ - Name: f.Name, - Len: 1, - Order: tag.Order, - Slice: false, - kind: f.Type.Kind(), - } - switch fd.kind { - case reflect.Array: - fd.Slice = true - fd.Array = true - fd.Len = f.Type.Len() - fd.kind = f.Type.Elem().Kind() - case reflect.Slice: - fd.Slice = true - fd.Len = -1 - fd.kind = f.Type.Elem().Kind() - case reflect.Ptr: - fd.Ptr = true - fd.kind = f.Type.Elem().Kind() - } - // check for custom types - tmp := reflect.New(f.Type) - if _, ok := tmp.Interface().(Custom); ok { - fd.Type = CustomType - return - } - var defTypeOk bool - fd.defType, defTypeOk = reflectTypeMap[fd.kind] - // find a type in the struct tag - pureType := typeLenRe.ReplaceAllLiteralString(tag.Type, "") - if fd.Type, ok = typeLookup[pureType]; ok { - fd.Len = 1 - match := typeLenRe.FindAllStringSubmatch(tag.Type, -1) - if len(match) > 0 && len(match[0]) > 1 { - fd.Slice = true - first := match[0][1] - // Field.Len = -1 indicates a []slice - if first == "" { - fd.Len = -1 - } else { - fd.Len, err = strconv.Atoi(first) - } - } - return - } - // the user didn't specify a type - switch f.Type { - case reflect.TypeOf(Size_t(0)): - fd.Type = SizeType - case reflect.TypeOf(Off_t(0)): - fd.Type = OffType - default: - if defTypeOk { - fd.Type = fd.defType - } else { - err = errors.New("struc: Could not find field type.") - } - } - return -} - -func parseFieldsLocked(v reflect.Value) (Fields, error) { - // we need to repeat this logic because parseFields() below can't be recursively called due to locking - for v.Kind() == reflect.Ptr { - v = v.Elem() - } - t := v.Type() - if v.NumField() < 1 { - return nil, errors.New("struc: Struct has no fields.") - } - sizeofMap := make(map[string][]int) - fields := make(Fields, v.NumField()) - for i := 0; i < t.NumField(); i++ { - field := t.Field(i) - f, tag, err := parseField(field) - if tag.Skip { - continue - } - if err != nil { - return nil, err - } - if !v.Field(i).CanSet() { - continue - } - f.Index = i - if tag.Sizeof != "" { - target, ok := t.FieldByName(tag.Sizeof) - if !ok { - return nil, fmt.Errorf("struc: `sizeof=%s` field does not exist", tag.Sizeof) - } - f.Sizeof = target.Index - sizeofMap[tag.Sizeof] = field.Index - } - if sizefrom, ok := sizeofMap[field.Name]; ok { - f.Sizefrom = sizefrom - } - if tag.Sizefrom != "" { - source, ok := t.FieldByName(tag.Sizefrom) - if !ok { - return nil, fmt.Errorf("struc: `sizefrom=%s` field does not exist", tag.Sizefrom) - } - f.Sizefrom = source.Index - } - if f.Len == -1 && f.Sizefrom == nil { - return nil, fmt.Errorf("struc: field `%s` is a slice with no length or sizeof field", field.Name) - } - // recurse into nested structs - // TODO: handle loops (probably by indirecting the []Field and putting pointer in cache) - if f.Type == Struct { - typ := field.Type - if f.Ptr { - typ = typ.Elem() - } - if f.Slice { - typ = typ.Elem() - } - f.Fields, err = parseFieldsLocked(reflect.New(typ)) - if err != nil { - return nil, err - } - } - fields[i] = f - } - return fields, nil -} - -var fieldCache = make(map[reflect.Type]Fields) -var fieldCacheLock sync.RWMutex -var parseLock sync.Mutex - -func fieldCacheLookup(t reflect.Type) Fields { - fieldCacheLock.RLock() - defer fieldCacheLock.RUnlock() - if cached, ok := fieldCache[t]; ok { - return cached - } - return nil -} - -func parseFields(v reflect.Value) (Fields, error) { - for v.Kind() == reflect.Ptr { - v = v.Elem() - } - t := v.Type() - - // fast path: hopefully the field parsing is already cached - if cached := fieldCacheLookup(t); cached != nil { - return cached, nil - } - - // hold a global lock so multiple goroutines can't parse (the same) fields at once - parseLock.Lock() - defer parseLock.Unlock() - - // check cache a second time, in case parseLock was just released by - // another thread who filled the cache for us - if cached := fieldCacheLookup(t); cached != nil { - return cached, nil - } - - // no luck, time to parse and fill the cache ourselves - fields, err := parseFieldsLocked(v) - if err != nil { - return nil, err - } - fieldCacheLock.Lock() - fieldCache[t] = fields - fieldCacheLock.Unlock() - return fields, nil -} diff --git a/vendor/github.com/lunixbochs/struc/struc.go b/vendor/github.com/lunixbochs/struc/struc.go deleted file mode 100644 index 3d85fe3..0000000 --- a/vendor/github.com/lunixbochs/struc/struc.go +++ /dev/null @@ -1,117 +0,0 @@ -package struc - -import ( - "encoding/binary" - "fmt" - "io" - "reflect" -) - -type Options struct { - ByteAlign int - PtrSize int - Order binary.ByteOrder -} - -func (o *Options) Validate() error { - if o.PtrSize == 0 { - o.PtrSize = 32 - } else { - switch o.PtrSize { - case 8, 16, 32, 64: - default: - return fmt.Errorf("Invalid Options.PtrSize: %d. Must be in (8, 16, 32, 64)", o.PtrSize) - } - } - return nil -} - -var emptyOptions = &Options{} - -func prep(data interface{}) (reflect.Value, Packer, error) { - value := reflect.ValueOf(data) - for value.Kind() == reflect.Ptr { - next := value.Elem().Kind() - if next == reflect.Struct || next == reflect.Ptr { - value = value.Elem() - } else { - break - } - } - switch value.Kind() { - case reflect.Struct: - fields, err := parseFields(value) - return value, fields, err - default: - if !value.IsValid() { - return reflect.Value{}, nil, fmt.Errorf("Invalid reflect.Value for %+v", data) - } - if c, ok := data.(Custom); ok { - return value, customFallback{c}, nil - } - return value, binaryFallback(value), nil - } -} - -func Pack(w io.Writer, data interface{}) error { - return PackWithOptions(w, data, nil) -} - -func PackWithOptions(w io.Writer, data interface{}, options *Options) error { - if options == nil { - options = emptyOptions - } - if err := options.Validate(); err != nil { - return err - } - val, packer, err := prep(data) - if err != nil { - return err - } - if val.Type().Kind() == reflect.String { - val = val.Convert(reflect.TypeOf([]byte{})) - } - size := packer.Sizeof(val, options) - buf := make([]byte, size) - if _, err := packer.Pack(buf, val, options); err != nil { - return err - } - _, err = w.Write(buf) - return err -} - -func Unpack(r io.Reader, data interface{}) error { - return UnpackWithOptions(r, data, nil) -} - -func UnpackWithOptions(r io.Reader, data interface{}, options *Options) error { - if options == nil { - options = emptyOptions - } - if err := options.Validate(); err != nil { - return err - } - val, packer, err := prep(data) - if err != nil { - return err - } - return packer.Unpack(r, val, options) -} - -func Sizeof(data interface{}) (int, error) { - return SizeofWithOptions(data, nil) -} - -func SizeofWithOptions(data interface{}, options *Options) (int, error) { - if options == nil { - options = emptyOptions - } - if err := options.Validate(); err != nil { - return 0, err - } - val, packer, err := prep(data) - if err != nil { - return 0, err - } - return packer.Sizeof(val, options), nil -} diff --git a/vendor/github.com/lunixbochs/struc/types.go b/vendor/github.com/lunixbochs/struc/types.go deleted file mode 100644 index 6ca97f4..0000000 --- a/vendor/github.com/lunixbochs/struc/types.go +++ /dev/null @@ -1,136 +0,0 @@ -package struc - -import ( - "fmt" - "reflect" -) - -type Type int - -const ( - Invalid Type = iota - Pad - Bool - Int - Int8 - Uint8 - Int16 - Uint16 - Int32 - Uint32 - Int64 - Uint64 - Float32 - Float64 - String - Struct - Ptr - - SizeType - OffType - CustomType -) - -func (t Type) Resolve(options *Options) Type { - switch t { - case OffType: - switch options.PtrSize { - case 8: - return Int8 - case 16: - return Int16 - case 32: - return Int32 - case 64: - return Int64 - default: - panic(fmt.Sprintf("unsupported ptr bits: %d", options.PtrSize)) - } - case SizeType: - switch options.PtrSize { - case 8: - return Uint8 - case 16: - return Uint16 - case 32: - return Uint32 - case 64: - return Uint64 - default: - panic(fmt.Sprintf("unsupported ptr bits: %d", options.PtrSize)) - } - } - return t -} - -func (t Type) String() string { - return typeNames[t] -} - -func (t Type) Size() int { - switch t { - case SizeType, OffType: - panic("Size_t/Off_t types must be converted to another type using options.PtrSize") - case Pad, String, Int8, Uint8, Bool: - return 1 - case Int16, Uint16: - return 2 - case Int32, Uint32, Float32: - return 4 - case Int64, Uint64, Float64: - return 8 - default: - panic("Cannot resolve size of type:" + t.String()) - } -} - -var typeLookup = map[string]Type{ - "pad": Pad, - "bool": Bool, - "byte": Uint8, - "int8": Int8, - "uint8": Uint8, - "int16": Int16, - "uint16": Uint16, - "int32": Int32, - "uint32": Uint32, - "int64": Int64, - "uint64": Uint64, - "float32": Float32, - "float64": Float64, - - "size_t": SizeType, - "off_t": OffType, -} - -var typeNames = map[Type]string{ - CustomType: "Custom", -} - -func init() { - for name, enum := range typeLookup { - typeNames[enum] = name - } -} - -type Size_t uint64 -type Off_t int64 - -var reflectTypeMap = map[reflect.Kind]Type{ - reflect.Bool: Bool, - reflect.Int8: Int8, - reflect.Int16: Int16, - reflect.Int: Int32, - reflect.Int32: Int32, - reflect.Int64: Int64, - reflect.Uint8: Uint8, - reflect.Uint16: Uint16, - reflect.Uint: Uint32, - reflect.Uint32: Uint32, - reflect.Uint64: Uint64, - reflect.Float32: Float32, - reflect.Float64: Float64, - reflect.String: String, - reflect.Struct: Struct, - reflect.Ptr: Ptr, -} |