2018-08-24 03:55:36 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
amf.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
2019-01-11 23:18:33 +03:00
|
|
|
Action Message Format (AMF) encoding/decoding functions.
|
|
|
|
See https://en.wikipedia.org/wiki/Action_Message_Format.
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
Jake Lane <jake@ausocean.org>
|
2019-01-11 23:12:34 +03:00
|
|
|
Alan Noble <alan@ausocean.org>
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
LICENSE
|
2019-01-11 23:12:34 +03:00
|
|
|
amf.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2018-08-30 14:01:19 +03:00
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2018-08-24 03:55:36 +03:00
|
|
|
|
2018-08-30 14:01:19 +03:00
|
|
|
Derived from librtmp under the GNU Lesser General Public License 2.1
|
|
|
|
Copyright (C) 2005-2008 Team XBMC http://www.xbmc.org
|
|
|
|
Copyright (C) 2008-2009 Andrej Stepanchuk
|
|
|
|
Copyright (C) 2009-2010 Howard Chu
|
2018-08-24 03:55:36 +03:00
|
|
|
*/
|
2019-01-11 23:43:27 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// Package amf implements Action Message Format (AMF) encoding and decoding.
|
2019-01-14 02:44:25 +03:00
|
|
|
// In AMF, encoding of numbers is big endian by default, unless specified otherwise,
|
|
|
|
// and numbers are all unsigned.
|
2019-01-11 23:43:27 +03:00
|
|
|
// See https://en.wikipedia.org/wiki/Action_Message_Format.
|
|
|
|
package amf
|
2018-08-24 00:17:13 +03:00
|
|
|
|
2018-08-24 03:03:05 +03:00
|
|
|
import (
|
2018-09-06 07:13:40 +03:00
|
|
|
"encoding/binary"
|
2019-01-12 06:48:50 +03:00
|
|
|
"errors"
|
2018-09-12 15:12:10 +03:00
|
|
|
"math"
|
2018-08-24 03:03:05 +03:00
|
|
|
)
|
|
|
|
|
2019-01-12 10:18:17 +03:00
|
|
|
// AMF data types, as defined by the AMF specification.
|
2019-01-12 01:26:57 +03:00
|
|
|
// NB: we export these sparingly.
|
2019-01-11 23:12:34 +03:00
|
|
|
const (
|
2019-01-12 10:18:17 +03:00
|
|
|
typeNumber = 0x00
|
|
|
|
typeBoolean = 0x01
|
2019-01-20 00:00:24 +03:00
|
|
|
TypeString = 0x02
|
2019-01-12 10:18:17 +03:00
|
|
|
TypeObject = 0x03
|
|
|
|
typeMovieClip = 0x04
|
|
|
|
TypeNull = 0x05
|
|
|
|
typeUndefined = 0x06
|
|
|
|
typeReference = 0x07
|
|
|
|
typeEcmaArray = 0x08
|
|
|
|
TypeObjectEnd = 0x09
|
|
|
|
typeStrictArray = 0x0A
|
|
|
|
typeDate = 0x0B
|
|
|
|
typeLongString = 0x0C
|
|
|
|
typeUnsupported = 0x0D
|
|
|
|
typeRecordset = 0x0E
|
|
|
|
typeXmlDoc = 0x0F
|
|
|
|
typeTypedObject = 0x10
|
|
|
|
typeAvmplus = 0x11
|
|
|
|
typeInvalid = 0xff
|
2018-08-24 00:19:47 +03:00
|
|
|
)
|
|
|
|
|
2019-01-13 00:39:54 +03:00
|
|
|
// AMF represents an AMF object, which is simply a collection of properties.
|
2019-01-12 01:26:57 +03:00
|
|
|
type Object struct {
|
2019-01-12 10:34:04 +03:00
|
|
|
Properties []Property
|
2019-01-11 23:12:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 02:27:59 +03:00
|
|
|
// Property represents an AMF property, which is effectively a
|
|
|
|
// union. The Type is the AMF data type (uint8 per the specification),
|
|
|
|
// and specifies which member holds a value. Numeric types use
|
|
|
|
// Number, string types use String and arrays and objects use
|
|
|
|
// Object. The Name is optional.
|
2019-01-11 23:43:27 +03:00
|
|
|
type Property struct {
|
2020-12-17 14:08:10 +03:00
|
|
|
Type uint8
|
|
|
|
|
2019-01-15 02:27:59 +03:00
|
|
|
Name string
|
2019-01-12 08:40:09 +03:00
|
|
|
Number float64
|
|
|
|
String string
|
|
|
|
Object Object
|
2019-01-11 23:12:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 08:14:54 +03:00
|
|
|
// AMF errors:
|
2019-01-12 06:48:50 +03:00
|
|
|
var (
|
2019-01-13 08:14:54 +03:00
|
|
|
ErrShortBuffer = errors.New("amf: short buffer") // The supplied buffer was too short.
|
|
|
|
ErrInvalidType = errors.New("amf: invalid type") // An invalid type was supplied to the encoder.
|
2019-01-20 00:00:24 +03:00
|
|
|
ErrUnexpectedType = errors.New("amf: unexpected type") // An unexpected type was encountered while decoding.
|
2019-01-13 08:14:54 +03:00
|
|
|
ErrPropertyNotFound = errors.New("amf: property not found") // The requested property was not found.
|
2019-01-12 06:48:50 +03:00
|
|
|
)
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// DecodeInt16 decodes a 16-bit integer.
|
|
|
|
func DecodeInt16(buf []byte) uint16 {
|
2019-01-13 08:48:25 +03:00
|
|
|
return binary.BigEndian.Uint16(buf)
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// DecodeInt24 decodes a 24-bit integer.
|
|
|
|
func DecodeInt24(buf []byte) uint32 {
|
|
|
|
return uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2])
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// DecodeInt32 decodes a 32-bit integer.
|
|
|
|
func DecodeInt32(buf []byte) uint32 {
|
2019-01-13 08:48:25 +03:00
|
|
|
return binary.BigEndian.Uint32(buf)
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 02:29:57 +03:00
|
|
|
// DecodeInt32LE decodes a 32-bit little-endian integer.
|
|
|
|
func DecodeInt32LE(buf []byte) uint32 {
|
2019-01-13 08:48:25 +03:00
|
|
|
return binary.LittleEndian.Uint32(buf)
|
2019-01-13 02:29:57 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// DecodeString decodes a string that is less than 2^16 bytes long.
|
|
|
|
func DecodeString(buf []byte) string {
|
|
|
|
n := DecodeInt16(buf)
|
|
|
|
return string(buf[2 : 2+n])
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// DecodeLongString decodes a long string.
|
|
|
|
func DecodeLongString(buf []byte) string {
|
|
|
|
n := DecodeInt32(buf)
|
|
|
|
return string(buf[2 : 2+n])
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// DecodeNumber decodes a 64-bit floating-point number.
|
|
|
|
func DecodeNumber(buf []byte) float64 {
|
|
|
|
return math.Float64frombits(binary.BigEndian.Uint64(buf))
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// DecodeBoolean decodes a boolean.
|
|
|
|
func DecodeBoolean(buf []byte) bool {
|
|
|
|
return buf[0] != 0
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeInt24 encodes a 24-bit integer.
|
2019-01-14 02:44:25 +03:00
|
|
|
func EncodeInt24(buf []byte, val uint32) ([]byte, error) {
|
2019-01-12 08:03:14 +03:00
|
|
|
if len(buf) < 3 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = byte(val >> 16)
|
|
|
|
buf[1] = byte(val >> 8)
|
|
|
|
buf[2] = byte(val)
|
|
|
|
return buf[3:], nil
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeInt32 encodes a 32-bit integer.
|
2019-01-14 02:44:25 +03:00
|
|
|
func EncodeInt32(buf []byte, val uint32) ([]byte, error) {
|
2019-01-12 08:03:14 +03:00
|
|
|
if len(buf) < 4 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-09-14 15:36:13 +03:00
|
|
|
}
|
2019-01-14 02:44:25 +03:00
|
|
|
binary.BigEndian.PutUint32(buf, val)
|
2019-01-12 08:03:14 +03:00
|
|
|
return buf[4:], nil
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeString encodes a string.
|
2019-01-20 00:00:24 +03:00
|
|
|
// Strings less than 65536 in length are encoded as TypeString, while longer strings are ecodeded as typeLongString.
|
2019-01-12 08:03:14 +03:00
|
|
|
func EncodeString(buf []byte, val string) ([]byte, error) {
|
2018-09-14 15:36:13 +03:00
|
|
|
const typeSize = 1
|
2019-01-12 08:03:14 +03:00
|
|
|
if len(val) < 65536 && len(val)+typeSize+binary.Size(int16(0)) > len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
|
2019-01-14 02:44:25 +03:00
|
|
|
if len(val)+typeSize+binary.Size(uint32(0)) > len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-09-06 07:13:40 +03:00
|
|
|
}
|
2018-09-14 15:36:13 +03:00
|
|
|
|
|
|
|
if len(val) < 65536 {
|
2019-01-20 00:00:24 +03:00
|
|
|
buf[0] = TypeString
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[1:]
|
|
|
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(val)))
|
|
|
|
buf = buf[2:]
|
|
|
|
copy(buf, val)
|
|
|
|
return buf[len(val):], nil
|
2018-09-14 15:36:13 +03:00
|
|
|
}
|
2019-01-12 01:26:57 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = typeLongString
|
|
|
|
buf = buf[1:]
|
|
|
|
binary.BigEndian.PutUint32(buf[:4], uint32(len(val)))
|
|
|
|
buf = buf[4:]
|
|
|
|
copy(buf, val)
|
|
|
|
return buf[len(val):], nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeNumber encodes a 64-bit floating-point number.
|
|
|
|
func EncodeNumber(buf []byte, val float64) ([]byte, error) {
|
|
|
|
if len(buf) < 9 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = typeNumber
|
|
|
|
buf = buf[1:]
|
|
|
|
binary.BigEndian.PutUint64(buf, math.Float64bits(val))
|
|
|
|
return buf[8:], nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeBoolean encodes a boolean.
|
|
|
|
func EncodeBoolean(buf []byte, val bool) ([]byte, error) {
|
|
|
|
if len(buf) < 2 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = typeBoolean
|
2018-09-14 15:45:31 +03:00
|
|
|
if val {
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[1] = 1
|
2019-01-12 10:09:32 +03:00
|
|
|
} else {
|
|
|
|
buf[1] = 0
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
return buf[2:], nil
|
2018-09-14 15:45:31 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeNamedString encodes a named string, where key is the name and val is the string value.
|
|
|
|
func EncodeNamedString(buf []byte, key, val string) ([]byte, error) {
|
|
|
|
if 2+len(key) > len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(key)))
|
|
|
|
buf = buf[2:]
|
|
|
|
copy(buf, key)
|
|
|
|
return EncodeString(buf[len(key):], val)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeNamedNumber encodes a named number, where key is the name and val is the number value.
|
|
|
|
func EncodeNamedNumber(buf []byte, key string, val float64) ([]byte, error) {
|
|
|
|
if 2+len(key) > len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(key)))
|
|
|
|
buf = buf[2:]
|
|
|
|
copy(buf, key)
|
|
|
|
return EncodeNumber(buf[len(key):], val)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 12:37:56 +03:00
|
|
|
// EncodeNamedNumber encodes a named boolean, where key is the name and val is the boolean value.
|
2019-01-12 08:03:14 +03:00
|
|
|
func EncodeNamedBoolean(buf []byte, key string, val bool) ([]byte, error) {
|
|
|
|
if 2+len(key) > len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(key)))
|
|
|
|
buf = buf[2:]
|
|
|
|
copy(buf, key)
|
|
|
|
return EncodeBoolean(buf[len(key):], val)
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 00:21:35 +03:00
|
|
|
// EncodeProperty encodes a property.
|
2019-01-13 00:39:54 +03:00
|
|
|
func EncodeProperty(prop *Property, buf []byte) ([]byte, error) {
|
2019-01-13 08:14:54 +03:00
|
|
|
if prop.Type != TypeNull && prop.Name != "" {
|
|
|
|
if len(buf) < 2+len(prop.Name) {
|
|
|
|
return nil, ErrShortBuffer
|
|
|
|
}
|
2019-01-13 00:39:54 +03:00
|
|
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(prop.Name)))
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[2:]
|
2019-01-13 00:39:54 +03:00
|
|
|
copy(buf, prop.Name)
|
|
|
|
buf = buf[len(prop.Name):]
|
2019-01-11 23:12:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 00:39:54 +03:00
|
|
|
switch prop.Type {
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeNumber:
|
2019-01-13 00:39:54 +03:00
|
|
|
return EncodeNumber(buf, prop.Number)
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeBoolean:
|
2019-01-13 00:39:54 +03:00
|
|
|
return EncodeBoolean(buf, prop.Number != 0)
|
2019-01-20 00:00:24 +03:00
|
|
|
case TypeString:
|
2019-01-13 00:39:54 +03:00
|
|
|
return EncodeString(buf, prop.String)
|
2019-01-12 01:26:57 +03:00
|
|
|
case TypeNull:
|
2019-01-12 08:03:14 +03:00
|
|
|
if len(buf) < 2 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = TypeNull
|
|
|
|
buf = buf[1:]
|
2019-01-12 01:26:57 +03:00
|
|
|
case TypeObject:
|
2019-01-13 00:39:54 +03:00
|
|
|
return Encode(&prop.Object, buf)
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeEcmaArray:
|
2019-01-13 00:39:54 +03:00
|
|
|
return EncodeEcmaArray(&prop.Object, buf)
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeStrictArray:
|
2019-01-13 00:39:54 +03:00
|
|
|
return EncodeArray(&prop.Object, buf)
|
2018-08-24 16:00:40 +03:00
|
|
|
default:
|
2019-01-12 10:09:32 +03:00
|
|
|
return nil, ErrInvalidType
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2019-01-12 10:09:32 +03:00
|
|
|
return buf, nil
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-13 00:21:35 +03:00
|
|
|
// DecodeProperty decodes a property, returning the number of bytes consumed from the supplied buffer.
|
|
|
|
func DecodeProperty(prop *Property, buf []byte, decodeName bool) (int, error) {
|
2019-01-12 08:03:14 +03:00
|
|
|
sz := len(buf)
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 07:25:12 +03:00
|
|
|
if decodeName {
|
2019-01-12 08:03:14 +03:00
|
|
|
if len(buf) < 4 {
|
2019-01-12 07:25:12 +03:00
|
|
|
return 0, ErrShortBuffer
|
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
n := DecodeInt16(buf[:2])
|
|
|
|
if int(n) > len(buf)-2 {
|
2019-01-13 02:40:43 +03:00
|
|
|
return 0, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:40:09 +03:00
|
|
|
prop.Name = DecodeString(buf)
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[2+n:]
|
2019-01-12 08:40:09 +03:00
|
|
|
} else {
|
|
|
|
prop.Name = ""
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 08:48:25 +03:00
|
|
|
prop.Type = buf[0]
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[1:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:40:09 +03:00
|
|
|
switch prop.Type {
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeNumber:
|
2019-01-12 08:03:14 +03:00
|
|
|
if len(buf) < 8 {
|
2019-01-12 07:25:12 +03:00
|
|
|
return 0, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:40:09 +03:00
|
|
|
prop.Number = DecodeNumber(buf[:8])
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[8:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeBoolean:
|
2019-01-12 10:09:32 +03:00
|
|
|
if len(buf) < 1 {
|
|
|
|
return 0, ErrShortBuffer
|
|
|
|
}
|
2019-01-13 08:48:25 +03:00
|
|
|
prop.Number = float64(buf[0])
|
2019-01-12 10:09:32 +03:00
|
|
|
buf = buf[1:]
|
2018-08-28 13:40:13 +03:00
|
|
|
|
2019-01-20 00:00:24 +03:00
|
|
|
case TypeString:
|
2019-01-12 08:03:14 +03:00
|
|
|
n := DecodeInt16(buf[:2])
|
|
|
|
if len(buf) < int(n+2) {
|
2019-01-12 07:25:12 +03:00
|
|
|
return 0, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:40:09 +03:00
|
|
|
prop.String = DecodeString(buf)
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[2+n:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
case TypeObject:
|
2019-01-13 08:14:54 +03:00
|
|
|
n, err := Decode(&prop.Object, buf, true)
|
2019-01-12 07:25:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[n:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
case TypeNull, typeUndefined, typeUnsupported:
|
2019-01-12 08:40:09 +03:00
|
|
|
prop.Type = TypeNull
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeEcmaArray:
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[4:]
|
2019-01-12 08:40:09 +03:00
|
|
|
n, err := Decode(&prop.Object, buf, true)
|
2019-01-12 07:25:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[n:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
|
|
|
default:
|
2019-01-13 08:14:54 +03:00
|
|
|
return 0, ErrUnexpectedType
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
return sz - len(buf), nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// Encode encodes an Object into its AMF representation.
|
|
|
|
func Encode(obj *Object, buf []byte) ([]byte, error) {
|
|
|
|
if len(buf) < 5 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = TypeObject
|
|
|
|
buf = buf[1:]
|
2018-08-24 16:00:40 +03:00
|
|
|
|
2019-01-12 10:34:04 +03:00
|
|
|
for i := 0; i < len(obj.Properties); i++ {
|
2019-01-12 06:48:50 +03:00
|
|
|
var err error
|
2019-01-13 00:21:35 +03:00
|
|
|
buf, err = EncodeProperty(&obj.Properties[i], buf)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 08:14:54 +03:00
|
|
|
if len(buf) < 3 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
return EncodeInt24(buf, TypeObjectEnd)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeEcmaArray encodes an ECMA array.
|
|
|
|
func EncodeEcmaArray(obj *Object, buf []byte) ([]byte, error) {
|
|
|
|
if len(buf) < 5 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = typeEcmaArray
|
|
|
|
buf = buf[1:]
|
2019-01-12 10:34:04 +03:00
|
|
|
binary.BigEndian.PutUint32(buf[:4], uint32(len(obj.Properties)))
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[4:]
|
2018-08-24 16:00:40 +03:00
|
|
|
|
2019-01-12 10:34:04 +03:00
|
|
|
for i := 0; i < len(obj.Properties); i++ {
|
2019-01-12 06:48:50 +03:00
|
|
|
var err error
|
2019-01-13 00:21:35 +03:00
|
|
|
buf, err = EncodeProperty(&obj.Properties[i], buf)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-13 08:14:54 +03:00
|
|
|
if len(buf) < 3 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
return EncodeInt24(buf, TypeObjectEnd)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeArray encodes an array.
|
|
|
|
func EncodeArray(obj *Object, buf []byte) ([]byte, error) {
|
|
|
|
if len(buf) < 5 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
buf[0] = typeStrictArray
|
|
|
|
buf = buf[1:]
|
2019-01-12 10:34:04 +03:00
|
|
|
binary.BigEndian.PutUint32(buf[:4], uint32(len(obj.Properties)))
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[4:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 10:34:04 +03:00
|
|
|
for i := 0; i < len(obj.Properties); i++ {
|
2019-01-12 06:48:50 +03:00
|
|
|
var err error
|
2019-01-13 00:21:35 +03:00
|
|
|
buf, err = EncodeProperty(&obj.Properties[i], buf)
|
2019-01-12 06:48:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-08-24 16:00:40 +03:00
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
return buf, nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// Decode decodes an object. Property names are only decoded if decodeName is true.
|
|
|
|
func Decode(obj *Object, buf []byte, decodeName bool) (int, error) {
|
|
|
|
sz := len(buf)
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 10:34:04 +03:00
|
|
|
obj.Properties = obj.Properties[:0]
|
2019-01-12 08:03:14 +03:00
|
|
|
for len(buf) != 0 {
|
|
|
|
if len(buf) >= 3 && DecodeInt24(buf[:3]) == TypeObjectEnd {
|
|
|
|
buf = buf[3:]
|
2019-01-12 08:40:09 +03:00
|
|
|
break
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2019-01-11 23:43:27 +03:00
|
|
|
var prop Property
|
2019-01-13 00:21:35 +03:00
|
|
|
n, err := DecodeProperty(&prop, buf, decodeName)
|
2019-01-12 07:25:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[n:]
|
2019-01-12 10:34:04 +03:00
|
|
|
obj.Properties = append(obj.Properties, prop)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
return sz - len(buf), nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-13 00:39:54 +03:00
|
|
|
// Object methods:
|
|
|
|
|
|
|
|
// Property returns a property, either by its index when idx is non-negative, or by its name otherwise.
|
2019-01-12 11:13:51 +03:00
|
|
|
// If the requested property is not found or the type does not match, an ErrPropertyNotFound error is returned.
|
2019-01-13 00:39:54 +03:00
|
|
|
func (obj *Object) Property(name string, idx int, typ uint8) (*Property, error) {
|
2019-01-12 11:13:51 +03:00
|
|
|
var prop *Property
|
2018-09-15 03:50:44 +03:00
|
|
|
if idx >= 0 {
|
2019-01-12 10:34:04 +03:00
|
|
|
if idx < len(obj.Properties) {
|
2019-01-12 11:13:51 +03:00
|
|
|
prop = &obj.Properties[idx]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-12 10:34:04 +03:00
|
|
|
for i, p := range obj.Properties {
|
2019-01-12 08:40:09 +03:00
|
|
|
if p.Name == name {
|
2019-01-12 11:13:51 +03:00
|
|
|
prop = &obj.Properties[i]
|
|
|
|
break
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 11:13:51 +03:00
|
|
|
if prop == nil || prop.Type != typ {
|
|
|
|
return nil, ErrPropertyNotFound
|
|
|
|
}
|
|
|
|
return prop, nil
|
|
|
|
}
|
|
|
|
|
2019-01-13 00:39:54 +03:00
|
|
|
// NumberProperty is a wrapper for Property that returns a Number property's value, if any.
|
|
|
|
func (obj *Object) NumberProperty(name string, idx int) (float64, error) {
|
|
|
|
prop, err := obj.Property(name, idx, typeNumber)
|
2019-01-12 11:13:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return prop.Number, nil
|
|
|
|
}
|
|
|
|
|
2019-01-13 00:39:54 +03:00
|
|
|
// StringProperty is a wrapper for Property that returns a String property's value, if any.
|
|
|
|
func (obj *Object) StringProperty(name string, idx int) (string, error) {
|
2019-01-20 00:00:24 +03:00
|
|
|
prop, err := obj.Property(name, idx, TypeString)
|
2019-01-12 11:13:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return prop.String, nil
|
|
|
|
}
|
|
|
|
|
2019-01-13 00:39:54 +03:00
|
|
|
// ObjectProperty is a wrapper for Property that returns an Object property's value, if any.
|
|
|
|
func (obj *Object) ObjectProperty(name string, idx int) (*Object, error) {
|
|
|
|
prop, err := obj.Property(name, idx, TypeObject)
|
2019-01-12 11:13:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &prop.Object, nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|