av/rtmp/amf/amf.go

546 lines
11 KiB
Go

/*
NAME
amf.go
DESCRIPTION
Action Message Format (AMF) encoding/decoding functions.
See https://en.wikipedia.org/wiki/Action_Message_Format.
AUTHORS
Saxon Nelson-Milton <saxon@ausocean.org>
Dan Kortschak <dan@ausocean.org>
Jake Lane <jake@ausocean.org>
Alan Noble <alan@ausocean.org>
LICENSE
amf.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
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
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
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
*/
// amf implements Action Message Format (AMF) encoding and decoding.
// See https://en.wikipedia.org/wiki/Action_Message_Format.
package amf
import (
"encoding/binary"
"math"
)
// AMF data types.
// NB: we export these sparingly.
const (
typeNumber = iota
typeBoolean
typeString
TypeObject // ToDo: consider not exporting this
typeMovieClip // reserved, not implemented
TypeNull // ToDo: consider not exporting this
typeUndefined
typeReference
typeEcmaArray
TypeObjectEnd // ToDo: consider not exporting this
typeStrictArray
typeDate
typeLongString
typeUnsupported
typeRecordset // reserved, not implemented
typeXmlDoc
typeTypedObject
typeAvmplus // reserved, not implemented
typeInvalid = 0xff
)
// AMF represents an AMF message (object), which is simply a collection of properties.
type Object struct {
Props []Property // ToDo: consider not exporting this
}
// Property represents an AMF property.
type Property struct {
name string
dtype uint8
number float64
str string
obj Object
}
func DecodeInt16(data []byte) uint16 {
return uint16(binary.BigEndian.Uint16(data))
}
func DecodeInt24(data []byte) uint32 {
return uint32(data[0])<<16 | uint32(data[1])<<8 | uint32(data[2])
}
func DecodeInt32(data []byte) uint32 {
return uint32(binary.BigEndian.Uint32(data))
}
func DecodeString(data []byte) string {
n := DecodeInt16(data)
return string(data[2 : 2+n])
}
func DecodeLongString(data []byte) string {
n := DecodeInt32(data)
return string(data[2 : 2+n])
}
func DecodeNumber(data []byte) float64 {
return math.Float64frombits(binary.BigEndian.Uint64(data))
}
func DecodeBoolean(data []byte) bool {
return data[0] != 0
}
func EncodeInt24(dst []byte, val int32) []byte {
if len(dst) < 3 {
return nil
}
dst[0] = byte(val >> 16)
dst[1] = byte(val >> 8)
dst[2] = byte(val)
if len(dst) == 3 {
return nil
}
return dst[3:]
}
func EncodeInt32(dst []byte, val int32) []byte {
if len(dst) < 4 {
return nil
}
binary.BigEndian.PutUint32(dst, uint32(val))
if len(dst) == 4 {
return nil
}
return dst[4:]
}
func EncodeString(dst []byte, val string) []byte {
const typeSize = 1
if len(val) < 65536 && len(val)+typeSize+binary.Size(int16(0)) > len(dst) {
return nil
}
if len(val)+typeSize+binary.Size(int32(0)) > len(dst) {
return nil
}
if len(val) < 65536 {
dst[0] = typeString
dst = dst[1:]
binary.BigEndian.PutUint16(dst[:2], uint16(len(val)))
dst = dst[2:]
copy(dst, val)
if len(dst) == len(val) {
return nil
}
return dst[len(val):]
}
dst[0] = typeLongString
dst = dst[1:]
binary.BigEndian.PutUint32(dst[:4], uint32(len(val)))
dst = dst[4:]
copy(dst, val)
if len(dst) == len(val) {
return nil
}
return dst[len(val):]
}
func EncodeNumber(dst []byte, val float64) []byte {
if len(dst) < 9 {
return nil
}
dst[0] = typeNumber
dst = dst[1:]
binary.BigEndian.PutUint64(dst, math.Float64bits(val))
return dst[8:]
}
func EncodeBoolean(dst []byte, val bool) []byte {
if len(dst) < 2 {
return nil
}
dst[0] = typeBoolean
if val {
dst[1] = 1
}
if len(dst) == 2 {
return nil
}
return dst[2:]
}
func EncodeNamedString(dst []byte, key, val string) []byte {
if 2+len(key) > len(dst) {
return nil
}
binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
dst = dst[2:]
copy(dst, key)
if len(key) == len(dst) {
return nil
}
return EncodeString(dst[len(key):], val)
}
func EncodeNamedNumber(dst []byte, key string, val float64) []byte {
if 2+len(key) > len(dst) {
return nil
}
binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
dst = dst[2:]
copy(dst, key)
if len(key) == len(dst) {
return nil
}
return EncodeNumber(dst[len(key):], val)
}
func EncodeNamedBoolean(dst []byte, key string, val bool) []byte {
if 2+len(key) > len(dst) {
return nil
}
binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
dst = dst[2:]
copy(dst, key)
if len(key) == len(dst) {
return nil
}
return EncodeBoolean(dst[len(key):], val)
}
func PropSetName(prop *Property, name string) {
prop.name = name
}
func PropGetNumber(prop *Property) float64 {
return prop.number
}
func PropGetString(prop *Property) string {
if prop.dtype == typeString {
return prop.str
}
return ""
}
func PropGetObject(prop *Property, obj *Object) {
if prop.dtype == TypeObject {
*obj = prop.obj
} else {
*obj = Object{}
}
}
func PropEncode(p *Property, dst []byte) []byte {
if p.dtype == typeInvalid {
return nil
}
if p.dtype != TypeNull && len(p.name)+2+1 >= len(dst) {
return nil
}
if p.dtype != TypeNull && len(p.name) != 0 {
binary.BigEndian.PutUint16(dst[:2], uint16(len(p.name)))
dst = dst[2:]
copy(dst, p.name)
dst = dst[len(p.name):]
}
switch p.dtype {
case typeNumber:
dst = EncodeNumber(dst, p.number)
case typeBoolean:
dst = EncodeBoolean(dst, p.number != 0)
case typeString:
dst = EncodeString(dst, p.str)
case TypeNull:
if len(dst) < 2 {
return nil
}
dst[0] = TypeNull
dst = dst[1:]
case TypeObject:
dst = Encode(&p.obj, dst)
case typeEcmaArray:
dst = EncodeEcmaArray(&p.obj, dst)
case typeStrictArray:
dst = EncodeArray(&p.obj, dst)
default:
// ??? log.Println("PropEncode: invalid type!")
dst = nil
}
return dst
}
func PropDecode(prop *Property, data []byte, decodeName int32) int32 {
prop.name = ""
nOriginalSize := len(data)
if len(data) == 0 {
// TODO use new logger here
// RTMLog(RTMLOGDEBUG, "%s: Empty buffer/no buffer pointer!", __FUNCTION__);
return -1
}
if decodeName != 0 && len(data) < 4 {
// at least name (length + at least 1 byte) and 1 byte of data
// TODO use new logger here
// RTMLog(RTMLOGDEBUG, "%s: Not enough data for decoding with name, less than 4 bytes!",__FUNCTION__);
return -1
}
if decodeName != 0 {
nNameSize := DecodeInt16(data[:2])
if int(nNameSize) > len(data)-2 {
// TODO use new logger here
//RTMLog(RTMLOGDEBUG, "%s: Name size out of range: namesize (%d) > len (%d) - 2",__FUNCTION__, nNameSize, nSize);
return -1
}
prop.name = DecodeString(data)
data = data[2+nNameSize:]
}
if len(data) == 0 {
return -1
}
prop.dtype = uint8(data[0])
data = data[1:]
var nRes int32
switch prop.dtype {
case typeNumber:
if len(data) < 8 {
return -1
}
prop.number = DecodeNumber(data[:8])
data = data[8:]
case typeBoolean:
panic("typeBoolean not supported")
case typeString:
nStringSize := DecodeInt16(data[:2])
if len(data) < int(nStringSize+2) {
return -1
}
prop.str = DecodeString(data)
data = data[2+nStringSize:]
case TypeObject:
nRes := Decode(&prop.obj, data, 1)
if nRes == -1 {
return -1
}
data = data[nRes:]
case typeMovieClip:
// TODO use new logger here
// ??? log.Println("PropDecode: MAF_MOVIECLIP reserved!")
//RTMLog(RTMLOGERROR, "MovieClip reserved!");
return -1
case TypeNull, typeUndefined, typeUnsupported:
prop.dtype = TypeNull
case typeReference:
// TODO use new logger here
// ??? log.Println("PropDecode: Reference not supported!")
//RTMLog(RTMLOGERROR, "Reference not supported!");
return -1
case typeEcmaArray:
// next comes the rest, mixed array has a final 0x000009 mark and names, so its an object
data = data[4:]
nRes = Decode(&prop.obj, data, 1)
if nRes == -1 {
return -1
}
data = data[nRes:]
case TypeObjectEnd:
return -1
case typeStrictArray:
panic("StrictArray not supported")
case typeDate:
panic("Date not supported")
case typeLongString, typeXmlDoc:
panic("typeLongString, XmlDoc not supported")
case typeRecordset:
// TODO use new logger here
// ??? log.Println("PropDecode: Recordset reserved!")
//RTMLog(RTMLOGERROR, "Recordset reserved!");
return -1
case typeTypedObject:
// TODO use new logger here
// RTMLog(RTMLOGERROR, "Typed_object not supported!")
return -1
case typeAvmplus:
panic("Avmplus not supported")
default:
// TODO use new logger here
//RTMLog(RTMLOGDEBUG, "%s - unknown datatype 0x%02x, @%p", __FUNCTION__,
//prop.dtype, pBuffer - 1);
return -1
}
return int32(nOriginalSize - len(data))
}
func PropReset(prop *Property) {
if prop.dtype == TypeObject || prop.dtype == typeEcmaArray ||
prop.dtype == typeStrictArray {
Reset(&prop.obj)
} else {
prop.str = ""
}
prop.dtype = typeInvalid
}
func Encode(obj *Object, dst []byte) []byte {
if len(dst) < 5 {
return nil
}
dst[0] = TypeObject
dst = dst[1:]
for i := 0; i < len(obj.Props); i++ {
dst = PropEncode(&obj.Props[i], dst)
if dst == nil {
// ??? log.Println("Encode: failed to encode property in index")
break
}
}
if len(dst) < 4 {
return nil
}
return EncodeInt24(dst, TypeObjectEnd)
}
func EncodeEcmaArray(obj *Object, dst []byte) []byte {
if len(dst) < 5 {
return nil
}
dst[0] = typeEcmaArray
dst = dst[1:]
binary.BigEndian.PutUint32(dst[:4], uint32(len(obj.Props)))
dst = dst[4:]
for i := 0; i < len(obj.Props); i++ {
dst = PropEncode(&obj.Props[i], dst)
if dst == nil {
// ??? log.Println("EncodeEcmaArray: failed to encode property!")
break
}
}
if len(dst) < 4 {
return nil
}
return EncodeInt24(dst, TypeObjectEnd)
}
// not used?
func EncodeArray(obj *Object, dst []byte) []byte {
if len(dst) < 5 {
return nil
}
dst[0] = typeStrictArray
dst = dst[1:]
binary.BigEndian.PutUint32(dst[:4], uint32(len(obj.Props)))
dst = dst[4:]
for i := 0; i < len(obj.Props); i++ {
dst = PropEncode(&obj.Props[i], dst)
if dst == nil {
// ??? log.Println("EncodeArray: failed to encode property!")
break
}
}
return dst
}
func Decode(obj *Object, data []byte, decodeName int32) int32 {
nOriginalSize := len(data)
obj.Props = obj.Props[:0]
for len(data) != 0 {
if len(data) >= 3 && DecodeInt24(data[:3]) == TypeObjectEnd {
data = data[3:]
break
}
var prop Property
nRes := PropDecode(&prop, data, decodeName)
// nRes = int32(C.PropDecode(&prop, (*byte)(unsafe.Pointer(pBuffer)),
// int32(nSize), int32(decodeName)))
if nRes == -1 {
return -1
}
data = data[nRes:]
obj.Props = append(obj.Props, prop)
}
return int32(nOriginalSize - len(data))
}
func GetProp(obj *Object, name string, idx int32) *Property {
if idx >= 0 {
if idx < int32(len(obj.Props)) {
return &obj.Props[idx]
}
} else {
for i, p := range obj.Props {
if p.name == name {
return &obj.Props[i]
}
}
}
return &Property{}
}
func Reset(obj *Object) {
for i := range obj.Props {
PropReset(&obj.Props[i])
}
*obj = Object{}
}