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-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-11 23:43:27 +03:00
|
|
|
// AMF data types.
|
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 01:26:57 +03:00
|
|
|
typeNumber = iota
|
|
|
|
typeBoolean
|
|
|
|
typeString
|
2019-01-12 08:03:14 +03:00
|
|
|
TypeObject
|
|
|
|
typeMovieClip
|
|
|
|
TypeNull
|
2019-01-12 01:26:57 +03:00
|
|
|
typeUndefined
|
|
|
|
typeReference
|
|
|
|
typeEcmaArray
|
2019-01-12 08:03:14 +03:00
|
|
|
TypeObjectEnd
|
2019-01-12 01:26:57 +03:00
|
|
|
typeStrictArray
|
|
|
|
typeDate
|
|
|
|
typeLongString
|
|
|
|
typeUnsupported
|
2019-01-12 08:03:14 +03:00
|
|
|
typeRecordset
|
2019-01-12 01:26:57 +03:00
|
|
|
typeXmlDoc
|
|
|
|
typeTypedObject
|
2019-01-12 08:03:14 +03:00
|
|
|
typeAvmplus
|
2019-01-12 01:26:57 +03:00
|
|
|
typeInvalid = 0xff
|
2018-08-24 00:19:47 +03:00
|
|
|
)
|
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
// AMF represents an AMF message (object), which is simply a collection of properties.
|
|
|
|
type Object struct {
|
2019-01-12 08:40:09 +03:00
|
|
|
Props []Property
|
2019-01-11 23:12:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:43:27 +03:00
|
|
|
// Property represents an AMF property.
|
|
|
|
type Property struct {
|
2019-01-12 08:40:09 +03:00
|
|
|
Name string
|
|
|
|
Type uint8
|
|
|
|
Number float64
|
|
|
|
String string
|
|
|
|
Object Object
|
2019-01-11 23:12:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 06:48:50 +03:00
|
|
|
// AMF errors.
|
|
|
|
var (
|
2019-01-12 08:40:09 +03:00
|
|
|
ErrShortBuffer = errors.New("amf: short buffer")
|
|
|
|
ErrEndOfBuffer = errors.New("amf: end of buffer")
|
|
|
|
ErrInvalidType = errors.New("amf: invalid type")
|
|
|
|
ErrUnimplemented = errors.New("amf: unimplemented feature")
|
|
|
|
ErrDecodingName = errors.New("amf: name decoding error")
|
|
|
|
ErrDecodingString = errors.New("amf: string decoding error")
|
|
|
|
ErrUnexpectedEnd = errors.New("amf: unexpected end")
|
|
|
|
ErrPropertyNotFound = errors.New("amf: property 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 {
|
|
|
|
return uint16(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 {
|
|
|
|
return uint32(binary.BigEndian.Uint32(buf))
|
2018-08-24 16:00:40 +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.
|
|
|
|
func EncodeInt24(buf []byte, val int32) ([]byte, error) {
|
|
|
|
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)
|
|
|
|
if len(buf) == 3 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-09-14 15:36:13 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
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.
|
|
|
|
func EncodeInt32(buf []byte, val int32) ([]byte, error) {
|
|
|
|
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-12 08:03:14 +03:00
|
|
|
binary.BigEndian.PutUint32(buf, uint32(val))
|
|
|
|
if len(buf) == 4 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
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.
|
|
|
|
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-12 08:03:14 +03:00
|
|
|
if len(val)+typeSize+binary.Size(int32(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-12 08:03:14 +03:00
|
|
|
buf[0] = typeString
|
|
|
|
buf = buf[1:]
|
|
|
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(val)))
|
|
|
|
buf = buf[2:]
|
|
|
|
copy(buf, val)
|
|
|
|
if len(buf) == len(val) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-09-14 15:36:13 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
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)
|
|
|
|
if len(buf) == len(val) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
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
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
if len(buf) == 2 {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-09-14 15:45:31 +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)
|
|
|
|
if len(key) == len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-09-15 02:44:27 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
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)
|
|
|
|
if len(key) == len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-09-15 02:44:27 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
return EncodeNumber(buf[len(key):], val)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:03:14 +03:00
|
|
|
// EncodeNamedNumber encodes a named boolean, where key is the name and val is the booelean value.
|
|
|
|
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)
|
|
|
|
if len(key) == len(buf) {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrEndOfBuffer
|
2018-09-15 02:44:27 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
return EncodeBoolean(buf[len(key):], val)
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 06:48:50 +03:00
|
|
|
// PropEncode encodes a property.
|
2019-01-12 08:03:14 +03:00
|
|
|
func PropEncode(p *Property, buf []byte) ([]byte, error) {
|
2019-01-12 08:40:09 +03:00
|
|
|
if p.Type == typeInvalid {
|
2019-01-12 06:48:50 +03:00
|
|
|
return nil, ErrShortBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:40:09 +03:00
|
|
|
if p.Type != TypeNull && len(p.Name)+2+1 >= len(buf) {
|
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:40:09 +03:00
|
|
|
if p.Type != TypeNull && len(p.Name) != 0 {
|
|
|
|
binary.BigEndian.PutUint16(buf[:2], uint16(len(p.Name)))
|
2019-01-12 08:03:14 +03:00
|
|
|
buf = buf[2:]
|
2019-01-12 08:40:09 +03:00
|
|
|
copy(buf, p.Name)
|
|
|
|
buf = buf[len(p.Name):]
|
2019-01-11 23:12:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 06:48:50 +03:00
|
|
|
var err error
|
2019-01-12 08:40:09 +03:00
|
|
|
switch p.Type {
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeNumber:
|
2019-01-12 08:40:09 +03:00
|
|
|
buf, err = EncodeNumber(buf, p.Number)
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeBoolean:
|
2019-01-12 08:40:09 +03:00
|
|
|
buf, err = EncodeBoolean(buf, p.Number != 0)
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeString:
|
2019-01-12 08:40:09 +03:00
|
|
|
buf, err = EncodeString(buf, p.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-12 08:40:09 +03:00
|
|
|
buf, err = Encode(&p.Object, buf)
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeEcmaArray:
|
2019-01-12 08:40:09 +03:00
|
|
|
buf, err = EncodeEcmaArray(&p.Object, buf)
|
2019-01-12 01:26:57 +03:00
|
|
|
case typeStrictArray:
|
2019-01-12 08:40:09 +03:00
|
|
|
buf, err = EncodeArray(&p.Object, buf)
|
2018-08-24 16:00:40 +03:00
|
|
|
default:
|
2019-01-12 08:03:14 +03:00
|
|
|
buf, err = nil, ErrInvalidType
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2019-01-12 08:03:14 +03:00
|
|
|
return buf, err
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 07:25:12 +03:00
|
|
|
// PropDecode decodes a property, returning the number of bytes consumed from the supplied buffer.
|
2019-01-12 08:03:14 +03:00
|
|
|
func PropDecode(prop *Property, buf []byte, decodeName bool) (int, error) {
|
|
|
|
sz := len(buf)
|
|
|
|
if len(buf) == 0 {
|
2019-01-12 07:25:12 +03:00
|
|
|
return 0, ErrEndOfBuffer
|
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-12 07:25:12 +03:00
|
|
|
return 0, ErrDecodingName
|
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-12 08:03:14 +03:00
|
|
|
if len(buf) == 0 {
|
2019-01-12 07:25:12 +03:00
|
|
|
return 0, ErrEndOfBuffer
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-12 08:40:09 +03:00
|
|
|
prop.Type = uint8(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 07:25:12 +03:00
|
|
|
return 0, ErrUnimplemented
|
2018-08-28 13:40:13 +03:00
|
|
|
|
2019-01-12 01:26:57 +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-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
|
|
|
|
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
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
case TypeObjectEnd:
|
2019-01-12 07:25:12 +03:00
|
|
|
return 0, ErrUnexpectedEnd
|
2018-08-28 13:40:13 +03:00
|
|
|
|
2018-08-24 00:28:22 +03:00
|
|
|
default:
|
2019-01-12 07:25:12 +03:00
|
|
|
return 0, ErrUnimplemented
|
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 01:26:57 +03:00
|
|
|
for i := 0; i < len(obj.Props); i++ {
|
2019-01-12 06:48:50 +03:00
|
|
|
var err error
|
2019-01-12 08:03:14 +03:00
|
|
|
buf, err = PropEncode(&obj.Props[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-12 08:03:14 +03:00
|
|
|
if len(buf) < 4 {
|
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:]
|
|
|
|
binary.BigEndian.PutUint32(buf[:4], uint32(len(obj.Props)))
|
|
|
|
buf = buf[4:]
|
2018-08-24 16:00:40 +03:00
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
for i := 0; i < len(obj.Props); i++ {
|
2019-01-12 06:48:50 +03:00
|
|
|
var err error
|
2019-01-12 08:03:14 +03:00
|
|
|
buf, err = PropEncode(&obj.Props[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-12 08:03:14 +03:00
|
|
|
if len(buf) < 4 {
|
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:]
|
|
|
|
binary.BigEndian.PutUint32(buf[:4], uint32(len(obj.Props)))
|
|
|
|
buf = buf[4:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2019-01-12 01:26:57 +03:00
|
|
|
for i := 0; i < len(obj.Props); i++ {
|
2019-01-12 06:48:50 +03:00
|
|
|
var err error
|
2019-01-12 08:03:14 +03:00
|
|
|
buf, err = PropEncode(&obj.Props[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 01:26:57 +03:00
|
|
|
obj.Props = obj.Props[: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-12 08:03:14 +03:00
|
|
|
n, err := PropDecode(&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 01:26:57 +03:00
|
|
|
obj.Props = append(obj.Props, 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-12 08:40:09 +03:00
|
|
|
// GetProp returns an object's property, either by its index when idx is non-negative, or by its name otherwise.
|
|
|
|
// If the requested property is not found an ErrPropertyNotFound error is returned.
|
|
|
|
func (obj *Object)GetProp(name string, idx int) (*Property, error) {
|
2018-09-15 03:50:44 +03:00
|
|
|
if idx >= 0 {
|
2019-01-12 08:03:14 +03:00
|
|
|
if idx < len(obj.Props) {
|
2019-01-12 08:40:09 +03:00
|
|
|
return &obj.Props[idx], nil
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-12 01:26:57 +03:00
|
|
|
for i, p := range obj.Props {
|
2019-01-12 08:40:09 +03:00
|
|
|
if p.Name == name {
|
|
|
|
return &obj.Props[i], nil
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2019-01-12 08:40:09 +03:00
|
|
|
return nil, ErrPropertyNotFound
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|