From b680e3e1640cfd0e49065b32af21e03df7e9402e Mon Sep 17 00:00:00 2001 From: scruzin Date: Mon, 14 Jan 2019 10:14:25 +1030 Subject: [PATCH] EncodeInt24 and EncodeInt32 now take unsigned integers for consistency with decoder counterparts. --- rtmp/amf/amf.go | 11 ++++++----- rtmp/amf/amf_test.go | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/rtmp/amf/amf.go b/rtmp/amf/amf.go index 196dac10..6ea5e4b1 100644 --- a/rtmp/amf/amf.go +++ b/rtmp/amf/amf.go @@ -35,7 +35,8 @@ LICENSE */ // Package amf implements Action Message Format (AMF) encoding and decoding. -// In AMF, encoding of numbers is big endian by default, unless specified otherwise. +// In AMF, encoding of numbers is big endian by default, unless specified otherwise, +// and numbers are all unsigned. // See https://en.wikipedia.org/wiki/Action_Message_Format. package amf @@ -134,7 +135,7 @@ func DecodeBoolean(buf []byte) bool { } // EncodeInt24 encodes a 24-bit integer. -func EncodeInt24(buf []byte, val int32) ([]byte, error) { +func EncodeInt24(buf []byte, val uint32) ([]byte, error) { if len(buf) < 3 { return nil, ErrShortBuffer } @@ -145,11 +146,11 @@ func EncodeInt24(buf []byte, val int32) ([]byte, error) { } // EncodeInt32 encodes a 32-bit integer. -func EncodeInt32(buf []byte, val int32) ([]byte, error) { +func EncodeInt32(buf []byte, val uint32) ([]byte, error) { if len(buf) < 4 { return nil, ErrShortBuffer } - binary.BigEndian.PutUint32(buf, uint32(val)) + binary.BigEndian.PutUint32(buf, val) return buf[4:], nil } @@ -160,7 +161,7 @@ func EncodeString(buf []byte, val string) ([]byte, error) { return nil, ErrShortBuffer } - if len(val)+typeSize+binary.Size(int32(0)) > len(buf) { + if len(val)+typeSize+binary.Size(uint32(0)) > len(buf) { return nil, ErrShortBuffer } diff --git a/rtmp/amf/amf_test.go b/rtmp/amf/amf_test.go index 437e49c9..f8668ff6 100644 --- a/rtmp/amf/amf_test.go +++ b/rtmp/amf/amf_test.go @@ -41,7 +41,7 @@ var testStrings = [...]string{ "bazz", } -var testNumbers = [...]int32{ +var testNumbers = [...]uint32{ 0, 1, 0xababab, @@ -102,7 +102,7 @@ func TestNumbers(t *testing.T) { if err != nil { t.Errorf("EncodeInt24 failed") } - dn := int32(DecodeInt24(buf)) + dn := DecodeInt24(buf) if n != dn { t.Errorf("DecodeInt24 did not produce original Number, got %v", dn) } @@ -110,7 +110,7 @@ func TestNumbers(t *testing.T) { if err != nil { t.Errorf("EncodeInt32 failed") } - dn = int32(DecodeInt32(buf)) + dn = DecodeInt32(buf) if n != dn { t.Errorf("DecodeInt32 did not produce original Number, got %v", dn) } @@ -139,7 +139,7 @@ func TestProperties(t *testing.T) { if err != nil { t.Errorf("DecodeProperty of Number failed") } - if int32(prop.Number) != testNumbers[i] { + if prop.Number != float64(testNumbers[i]) { t.Errorf("EncodeProperty/DecodeProperty returned wrong Number; got %v, expected %v", int32(prop.Number), testNumbers[i]) } dec = dec[n:]