EncodeInt24 and EncodeInt32 now take unsigned integers for consistency with decoder counterparts.

This commit is contained in:
scruzin 2019-01-14 10:14:25 +10:30
parent ffcd011220
commit b680e3e164
2 changed files with 10 additions and 9 deletions

View File

@ -35,7 +35,8 @@ LICENSE
*/ */
// Package amf implements Action Message Format (AMF) encoding and decoding. // 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. // See https://en.wikipedia.org/wiki/Action_Message_Format.
package amf package amf
@ -134,7 +135,7 @@ func DecodeBoolean(buf []byte) bool {
} }
// EncodeInt24 encodes a 24-bit integer. // 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 { if len(buf) < 3 {
return nil, ErrShortBuffer return nil, ErrShortBuffer
} }
@ -145,11 +146,11 @@ func EncodeInt24(buf []byte, val int32) ([]byte, error) {
} }
// EncodeInt32 encodes a 32-bit integer. // 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 { if len(buf) < 4 {
return nil, ErrShortBuffer return nil, ErrShortBuffer
} }
binary.BigEndian.PutUint32(buf, uint32(val)) binary.BigEndian.PutUint32(buf, val)
return buf[4:], nil return buf[4:], nil
} }
@ -160,7 +161,7 @@ func EncodeString(buf []byte, val string) ([]byte, error) {
return nil, ErrShortBuffer 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 return nil, ErrShortBuffer
} }

View File

@ -41,7 +41,7 @@ var testStrings = [...]string{
"bazz", "bazz",
} }
var testNumbers = [...]int32{ var testNumbers = [...]uint32{
0, 0,
1, 1,
0xababab, 0xababab,
@ -102,7 +102,7 @@ func TestNumbers(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("EncodeInt24 failed") t.Errorf("EncodeInt24 failed")
} }
dn := int32(DecodeInt24(buf)) dn := DecodeInt24(buf)
if n != dn { if n != dn {
t.Errorf("DecodeInt24 did not produce original Number, got %v", dn) t.Errorf("DecodeInt24 did not produce original Number, got %v", dn)
} }
@ -110,7 +110,7 @@ func TestNumbers(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("EncodeInt32 failed") t.Errorf("EncodeInt32 failed")
} }
dn = int32(DecodeInt32(buf)) dn = DecodeInt32(buf)
if n != dn { if n != dn {
t.Errorf("DecodeInt32 did not produce original Number, got %v", dn) t.Errorf("DecodeInt32 did not produce original Number, got %v", dn)
} }
@ -139,7 +139,7 @@ func TestProperties(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("DecodeProperty of Number failed") 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]) t.Errorf("EncodeProperty/DecodeProperty returned wrong Number; got %v, expected %v", int32(prop.Number), testNumbers[i])
} }
dec = dec[n:] dec = dec[n:]