func lengthValue(d *Decoder, element Element) (*Value, error) {
	start, err := d.f.Tell()
	if err != nil {
		return nil, err
	}
	length, err := d.Bytes(element.Length(), element.LengthUnit())
	if err != nil {
		return nil, err
	}
	end := d.ParentBounds().End
	maxLen := end - start
	if maxLen == 0 {
		return nil, io.EOF
	}
	if length > maxLen {
		return nil, io.ErrUnexpectedEOF
	}
	return &Value{
		Offset:  start,
		Len:     length,
		Element: element,
	}, nil
}
lengthValue returns a value based on this Element's length field
TODO Reconsider having this method, it seems to add little value
package ufwb
import (
	"fmt"
	"io"
	"reflect"
	"bramp.net/dsector/input"
	"bytes"
	"encoding/binary"
	log "github.com/Sirupsen/logrus"
)
var padElement = &Padding{Base: Base{"Padding", 0, "", ""}}
func (u *Ufwb) Read(d *Decoder) (*Value, error) {
	return d.read(u.Grammar)
}
func (g *Grammar) Read(d *Decoder) (*Value, error) {
	start, err := d.f.Tell()
	if err != nil {
		return nil, &validationError{e: g, err: err}
	}
	value := &Value{
		Offset:  start,
		Len:     0,
		Element: g,
	}
	elements := Elements([]Element{g.Start})
	return elements.Read(d, value, FixedOrder)
}
func isEof(err error) bool {
	if err == io.EOF {
		return true
	}
	if e, ok := err.(Eof); ok {
		return e.IsEof()
	}
	return false
}
func (elements Elements) Read(d *Decoder, value *Value, order Order) (*Value, error) {
	parent := value.Element
	start := value.Offset
	bounds := d.ParentBounds()
	bounds_remaining := bounds.End - start
	if DEBUG && bounds.Start > start {
		panic(fmt.Sprintf("Starting before bounds %d < %d", start, bounds.Start))
	}
	childrenCount := make(map[ElementId]int64)
	i := 0
	eof := false
	log.Debugf("[0x%x] Starting %s (bounds: [0x%x-0x%x], max length: %d)", start, parent.IdString(), bounds.Start, bounds.End, bounds_remaining)
	for value.Len <= bounds_remaining && i < len(elements) {
		e := elements[i]
		max, err := d.eval(e.RepeatMax())
		if err != nil {
			return nil, &validationError{e: e, err: fmt.Errorf("RepeatMax eval failed: %s", err.Error())}
		}
		if childrenCount[e] >= max {
			log.Debugf("Skipping %s found %d of %d", e.IdString(), childrenCount[e], max)
			i++
			continue
		}
		min, err := d.eval(e.RepeatMin())
		if err != nil {
			return nil, &validationError{e: e, err: fmt.Errorf("RepeatMin eval failed: %s", err.Error())}
		}
		_, err = d.f.Seek(start+value.Len, io.SeekStart)
		if err != nil {
			return nil, &validationError{e: parent, err: err}
		}
		v, err := d.read(e)
		eof = isEof(err)
		if v != nil && (err == nil || eof) {
			value.Children = append(value.Children, v)
			value.Len += v.Len
			childrenCount[v.Element]++
			if order == VariableOrder {
				log.Debugf("reset")
				i = 0
			}
		}
		if err != nil {
			switch order {
			case FixedOrder:
				if childrenCount[e] >= min {
					i++
					continue
				}
				return nil, err
			case VariableOrder:
				if i < len(elements)-1 {
					log.Debugf("[0x%x] Move on from: %s to: %s", start, elements[i].IdString(), elements[i+1].IdString())
				} else {
					log.Debugf("[0x%x] Move on from: %s to: end", start, elements[i].IdString())
				}
				i++
			default:
				return nil, &validationError{e: parent, err: fmt.Errorf("unknown order: %d", order)}
			}
		}
	}
	if parent.Length() != nil {
		log.Debugf("%s Loop %v, %v < %v, %v < %v", parent.IdString(), eof, value.Len, bounds_remaining, i, len(elements))
		if bounds_remaining > value.Len {
			log.Debugf("parent larger than children parent: %v, child: %v", bounds, value)
			padding := &Value{
				Offset:  start + value.Len,
				Len:     bounds_remaining - value.Len,
				Element: padElement,
			}
			value.Children = append(value.Children, padding)
			value.Len = bounds_remaining
			panic(fmt.Sprintf("While developing we shouldn't need to add any padding! %v", padding))
		} else if value.Len > bounds_remaining {
			panic(fmt.Sprintf("children's length is greater than the parent length, %d vs %d", value.Len, bounds_remaining))
		}
	}
	for _, e := range elements {
		min, err := d.eval(e.RepeatMin())
		if err != nil {
			return nil, &validationError{e: e, err: fmt.Errorf("RepeatMin eval failed: %s", err.Error())}
		}
		if childrenCount[e] < min {
			return nil, &validationError{e: parent, err: fmt.Errorf("child %s repeated %d times, less than %d times required", e.IdString(), childrenCount[e], min)}
		}
	}
	if eof {
		return value, io.EOF
	}
	return value, nil
}
func (s *Structure) Read(d *Decoder) (*Value, error) {
	start, err := d.f.Tell()
	if err != nil {
		return nil, &validationError{e: s, err: err}
	}
	value := &Value{
		Offset:  start,
		Element: s,
	}
	elements := Elements(s.Elements())
	return elements.Read(d, value, s.Order())
}
func (s *String) read(d *Decoder) (*Value, error) {
	start, err := d.f.Tell()
	if err != nil {
		return nil, err
	}
	end := d.ParentBounds().End
	maxLen := end - start
	var v *Value
	switch s.Typ() {
	case "zero-terminated", "delimiter-terminated":
		n, err := input.ReadUntil(d.f, s.delimiter, maxLen)
		if err != nil {
			return nil, err
		}
		v = &Value{Offset: start, Len: n, Element: s}
	case "fixed-length":
		v, err = lengthValue(d, s)
		if err != nil {
			return nil, err
		}
		if _, err = input.ReadAndDiscard(d.f, v.Len); err != nil {
			return nil, err
		}
	case "pascal":
		i, err := readInt(d.f, 1, false, binary.LittleEndian)
		if err != nil {
			return nil, err
		}
		length := int64(i.(uint8))
		if _, err = input.ReadAndDiscard(d.f, length); err != nil {
			return nil, err
		}
		v = &Value{Offset: start, Len: length + 1, Element: s}
	default:
		return nil, fmt.Errorf("unknown string type %q", s.Typ())
	}
	return v, nil
}
func (s *String) Read(d *Decoder) (*Value, error) {
	v, err := s.read(d)
	if err != nil && err != io.EOF {
		err = &validationError{e: s, err: err}
	}
	return v, err
}