Skip to content

Commit

Permalink
Expose constants (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored Dec 20, 2019
1 parent cb2a64f commit 6a60d21
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
6 changes: 3 additions & 3 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func marshalImpl(vo reflect.Value, w io.Writer, pos uint64, parent *Element, opt
return pos, ErrUnsupportedElement
}

unknown := tag.size == sizeUnknown
unknown := tag.size == SizeUnknown

lst, ok := pealElem(vn, e.t == DataTypeBinary, tag.omitEmpty)
if !ok {
Expand All @@ -151,7 +151,7 @@ func marshalImpl(vo reflect.Value, w io.Writer, pos uint64, parent *Element, opt
var bw io.Writer
if unknown {
// Directly write length unspecified element
bsz := encodeDataSize(uint64(sizeUnknown), 0)
bsz := encodeDataSize(uint64(SizeUnknown), 0)
n, err := w.Write(bsz)
if err != nil {
return pos, err
Expand All @@ -169,7 +169,7 @@ func marshalImpl(vo reflect.Value, w io.Writer, pos uint64, parent *Element, opt
Name: tag.name,
Type: t,
Position: pos,
Size: sizeUnknown,
Size: SizeUnknown,
Parent: parent,
}
}
Expand Down
4 changes: 2 additions & 2 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func parseTag(rawtag string) (*structTag, error) {
tag.omitEmpty = true
case "inf":
os.Stderr.WriteString("Deprecated: \"inf\" tag is replaced by \"size=unknown\"\n")
tag.size = sizeUnknown
tag.size = SizeUnknown
default:
return nil, ErrInvalidTag
}
Expand All @@ -65,7 +65,7 @@ func parseTag(rawtag string) (*structTag, error) {
return nil, ErrEmptyTag
case "size":
if kv[1] == "unknown" {
tag.size = sizeUnknown
tag.size = SizeUnknown
} else {
s, err := strconv.Atoi(kv[1])
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func TestParseTag(t *testing.T) {
},
"UnknownSize": {
"Name123,size=unknown",
&structTag{name: "Name123", size: sizeUnknown}, nil,
&structTag{name: "Name123", size: SizeUnknown}, nil,
},
"UnknownSizeDeprecated": {
"Name123,inf",
&structTag{name: "Name123", size: sizeUnknown}, nil,
&structTag{name: "Name123", size: SizeUnknown}, nil,
},
"InvalidSize": {
"Name123,size=a",
Expand Down
4 changes: 2 additions & 2 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Unmarshal(r io.Reader, val interface{}, opts ...UnmarshalOption) error {

voe := vo.Elem()
for {
if _, err := readElement(r, sizeUnknown, voe, 0, 0, nil, options); err != nil {
if _, err := readElement(r, SizeUnknown, voe, 0, 0, nil, options); err != nil {
if err == io.EOF {
return nil
}
Expand All @@ -61,7 +61,7 @@ func Unmarshal(r io.Reader, val interface{}, opts ...UnmarshalOption) error {

func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64, parent *Element, options *UnmarshalOptions) (io.Reader, error) {
var r io.Reader
if n != sizeUnknown {
if n != SizeUnknown {
r = io.LimitReader(r0, n)
} else {
r = r0
Expand Down
12 changes: 7 additions & 5 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
)

const (
dateEpochInUnixtime = 978307200
sizeUnknown = 0xffffffffffffff
// DateEpochInUnixtime is the Unixtime of EBML date epoch.
DateEpochInUnixtime = 978307200
// SizeUnknown is the longest unknown size value.
SizeUnknown = 0xffffffffffffff
)

// ErrInvalidFloatSize means that a element size is invalid for float type. Float must be 4 or 8 bytes.
Expand Down Expand Up @@ -167,7 +169,7 @@ func readDate(r io.Reader, n uint64) (interface{}, error) {
if err != nil {
return time.Unix(0, 0), err
}
return time.Unix(dateEpochInUnixtime, i.(int64)), nil
return time.Unix(DateEpochInUnixtime, i.(int64)), nil
}
func readFloat(r io.Reader, n uint64) (interface{}, error) {
if n != 4 && n != 8 {
Expand Down Expand Up @@ -226,7 +228,7 @@ func encodeDataSize(v, n uint64) []byte {
return []byte{byte(v>>40) | 0x4, byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
case v < 0x2000000000000-1 && n < 8:
return []byte{byte(v>>48) | 0x2, byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
case v < sizeUnknown:
case v < SizeUnknown:
return []byte{0x1, byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
default:
return []byte{0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
Expand Down Expand Up @@ -329,7 +331,7 @@ func encodeDate(i interface{}, n uint64) ([]byte, error) {
if !ok {
return []byte{}, ErrInvalidType
}
dtns := v.Sub(time.Unix(dateEpochInUnixtime, 0)).Nanoseconds()
dtns := v.Sub(time.Unix(DateEpochInUnixtime, 0)).Nanoseconds()
return encodeInt(int64(dtns), n)
}
func encodeFloat32(i float32) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDataSize(t *testing.T) {
"7 bytes (upper bound)": {[]byte{0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 0x2000000000000 - 2},
"8 bytes (lower bound)": {[]byte{0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 0x2000000000000 - 1},
"8 bytes (upper bound)": {[]byte{0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 0xffffffffffffff - 1},
"Indefinite": {[]byte{0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, sizeUnknown},
"Indefinite": {[]byte{0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, SizeUnknown},
}

for n, c := range testCases {
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestValue(t *testing.T) {
"Int48(7B)": {[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, DataTypeInt, int64(0x010203040506), 7, nil},
"Int56(8B)": {[]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 07}, DataTypeInt, int64(0x01020304050607), 8, nil},
"UInt": {[]byte{0x01, 0x02, 0x03}, DataTypeUInt, uint64(0x010203), 0, nil},
"Date": {[]byte{0x01, 0x02, 0x03}, DataTypeDate, time.Unix(dateEpochInUnixtime, 0x010203), 0, nil},
"Date": {[]byte{0x01, 0x02, 0x03}, DataTypeDate, time.Unix(DateEpochInUnixtime, 0x010203), 0, nil},
"Float32": {[]byte{0x40, 0x10, 0x00, 0x00}, DataTypeFloat, float64(2.25), 0, float32(2.25)},
"Float64": {[]byte{0x40, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, DataTypeFloat, float64(2.25), 0, nil},
"Float32(8B)": {[]byte{0x40, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, DataTypeFloat, float64(2.25), 8, float32(2.25)},
Expand Down

0 comments on commit 6a60d21

Please sign in to comment.