/* NAME amf.go DESCRIPTION Action Message Format (AMF) encoding/decoding functions. See https://en.wikipedia.org/wiki/Action_Message_Format. AUTHORS Saxon Nelson-Milton Dan Kortschak Jake Lane Alan Noble 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 */ // Package amf implements Action Message Format (AMF) encoding and decoding. // See https://en.wikipedia.org/wiki/Action_Message_Format. package amf import ( "encoding/binary" "errors" "math" ) // AMF data types. // NB: we export these sparingly. const ( typeNumber = iota typeBoolean typeString TypeObject typeMovieClip TypeNull typeUndefined typeReference typeEcmaArray TypeObjectEnd typeStrictArray typeDate typeLongString typeUnsupported typeRecordset typeXmlDoc typeTypedObject typeAvmplus 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 } // AMF errors. var ( 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") ) // DecodeInt16 decodes a 16-bit integer. func DecodeInt16(buf []byte) uint16 { return uint16(binary.BigEndian.Uint16(buf)) } // DecodeInt24 decodes a 24-bit integer. func DecodeInt24(buf []byte) uint32 { return uint32(buf[0])<<16 | uint32(buf[1])<<8 | uint32(buf[2]) } // DecodeInt32 decodes a 32-bit integer. func DecodeInt32(buf []byte) uint32 { return uint32(binary.BigEndian.Uint32(buf)) } // 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]) } // DecodeLongString decodes a long string. func DecodeLongString(buf []byte) string { n := DecodeInt32(buf) return string(buf[2 : 2+n]) } // DecodeNumber decodes a 64-bit floating-point number. func DecodeNumber(buf []byte) float64 { return math.Float64frombits(binary.BigEndian.Uint64(buf)) } // DecodeBoolean decodes a boolean. func DecodeBoolean(buf []byte) bool { return buf[0] != 0 } // EncodeInt24 encodes a 24-bit integer. func EncodeInt24(buf []byte, val int32) ([]byte, error) { if len(buf) < 3 { return nil, ErrShortBuffer } buf[0] = byte(val >> 16) buf[1] = byte(val >> 8) buf[2] = byte(val) if len(buf) == 3 { return nil, ErrEndOfBuffer } return buf[3:], nil } // EncodeInt32 encodes a 32-bit integer. func EncodeInt32(buf []byte, val int32) ([]byte, error) { if len(buf) < 4 { return nil, ErrShortBuffer } binary.BigEndian.PutUint32(buf, uint32(val)) if len(buf) == 4 { return nil, ErrEndOfBuffer } return buf[4:], nil } // EncodeString encodes a string. func EncodeString(buf []byte, val string) ([]byte, error) { const typeSize = 1 if len(val) < 65536 && len(val)+typeSize+binary.Size(int16(0)) > len(buf) { return nil, ErrShortBuffer } if len(val)+typeSize+binary.Size(int32(0)) > len(buf) { return nil, ErrShortBuffer } if len(val) < 65536 { 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) { return nil, ErrEndOfBuffer } return buf[len(val):], nil } 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) { return nil, ErrEndOfBuffer } return buf[len(val):], nil } // EncodeNumber encodes a 64-bit floating-point number. func EncodeNumber(buf []byte, val float64) ([]byte, error) { if len(buf) < 9 { return nil, ErrShortBuffer } buf[0] = typeNumber buf = buf[1:] binary.BigEndian.PutUint64(buf, math.Float64bits(val)) return buf[8:], nil } // EncodeBoolean encodes a boolean. func EncodeBoolean(buf []byte, val bool) ([]byte, error) { if len(buf) < 2 { return nil, ErrShortBuffer } buf[0] = typeBoolean if val { buf[1] = 1 } if len(buf) == 2 { return nil, ErrEndOfBuffer } return buf[2:], nil } // 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) { return nil, ErrShortBuffer } binary.BigEndian.PutUint16(buf[:2], uint16(len(key))) buf = buf[2:] copy(buf, key) if len(key) == len(buf) { return nil, ErrEndOfBuffer } return EncodeString(buf[len(key):], val) } // 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) { return nil, ErrShortBuffer } binary.BigEndian.PutUint16(buf[:2], uint16(len(key))) buf = buf[2:] copy(buf, key) if len(key) == len(buf) { return nil, ErrEndOfBuffer } return EncodeNumber(buf[len(key):], val) } // 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) { return nil, ErrShortBuffer } binary.BigEndian.PutUint16(buf[:2], uint16(len(key))) buf = buf[2:] copy(buf, key) if len(key) == len(buf) { return nil, ErrEndOfBuffer } return EncodeBoolean(buf[len(key):], val) } // Property functions. 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{} } } // PropEncode encodes a property. func PropEncode(p *Property, buf []byte) ([]byte, error) { if p.dtype == typeInvalid { return nil, ErrShortBuffer } if p.dtype != TypeNull && len(p.name)+2+1 >= len(buf) { return nil, ErrShortBuffer } if p.dtype != TypeNull && len(p.name) != 0 { binary.BigEndian.PutUint16(buf[:2], uint16(len(p.name))) buf = buf[2:] copy(buf, p.name) buf = buf[len(p.name):] } var err error switch p.dtype { case typeNumber: buf, err = EncodeNumber(buf, p.number) case typeBoolean: buf, err = EncodeBoolean(buf, p.number != 0) case typeString: buf, err = EncodeString(buf, p.str) case TypeNull: if len(buf) < 2 { return nil, ErrShortBuffer } buf[0] = TypeNull buf = buf[1:] case TypeObject: buf, err = Encode(&p.obj, buf) case typeEcmaArray: buf, err = EncodeEcmaArray(&p.obj, buf) case typeStrictArray: buf, err = EncodeArray(&p.obj, buf) default: buf, err = nil, ErrInvalidType } return buf, err } // PropDecode decodes a property, returning the number of bytes consumed from the supplied buffer. func PropDecode(prop *Property, buf []byte, decodeName bool) (int, error) { prop.name = "" sz := len(buf) if len(buf) == 0 { return 0, ErrEndOfBuffer } if decodeName { if len(buf) < 4 { return 0, ErrShortBuffer } n := DecodeInt16(buf[:2]) if int(n) > len(buf)-2 { return 0, ErrDecodingName } prop.name = DecodeString(buf) buf = buf[2+n:] } if len(buf) == 0 { return 0, ErrEndOfBuffer } prop.dtype = uint8(buf[0]) buf = buf[1:] switch prop.dtype { case typeNumber: if len(buf) < 8 { return 0, ErrShortBuffer } prop.number = DecodeNumber(buf[:8]) buf = buf[8:] case typeBoolean: return 0, ErrUnimplemented case typeString: n := DecodeInt16(buf[:2]) if len(buf) < int(n+2) { return 0, ErrShortBuffer } prop.str = DecodeString(buf) buf = buf[2+n:] case TypeObject: n, err := Decode(&prop.obj, buf, true) if err != nil { return 0, err } buf = buf[n:] case TypeNull, typeUndefined, typeUnsupported: prop.dtype = TypeNull case typeEcmaArray: buf = buf[4:] n, err := Decode(&prop.obj, buf, true) if err != nil { return 0, err } buf = buf[n:] case TypeObjectEnd: return 0, ErrUnexpectedEnd default: return 0, ErrUnimplemented } return sz - len(buf), nil } // Encode encodes an Object into its AMF representation. func Encode(obj *Object, buf []byte) ([]byte, error) { if len(buf) < 5 { return nil, ErrShortBuffer } buf[0] = TypeObject buf = buf[1:] for i := 0; i < len(obj.Props); i++ { var err error buf, err = PropEncode(&obj.Props[i], buf) if err != nil { return nil, err } } if len(buf) < 4 { return nil, ErrShortBuffer } return EncodeInt24(buf, TypeObjectEnd) } // EncodeEcmaArray encodes an ECMA array. func EncodeEcmaArray(obj *Object, buf []byte) ([]byte, error) { if len(buf) < 5 { return nil, ErrShortBuffer } buf[0] = typeEcmaArray buf = buf[1:] binary.BigEndian.PutUint32(buf[:4], uint32(len(obj.Props))) buf = buf[4:] for i := 0; i < len(obj.Props); i++ { var err error buf, err = PropEncode(&obj.Props[i], buf) if err != nil { return nil, err } } if len(buf) < 4 { return nil, ErrShortBuffer } return EncodeInt24(buf, TypeObjectEnd) } // EncodeArray encodes an array. func EncodeArray(obj *Object, buf []byte) ([]byte, error) { if len(buf) < 5 { return nil, ErrShortBuffer } buf[0] = typeStrictArray buf = buf[1:] binary.BigEndian.PutUint32(buf[:4], uint32(len(obj.Props))) buf = buf[4:] for i := 0; i < len(obj.Props); i++ { var err error buf, err = PropEncode(&obj.Props[i], buf) if err != nil { return nil, err } } return buf, nil } // 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) obj.Props = obj.Props[:0] for len(buf) != 0 { if len(buf) >= 3 && DecodeInt24(buf[:3]) == TypeObjectEnd { buf = buf[3:] break } var prop Property n, err := PropDecode(&prop, buf, decodeName) if err != nil { return 0, err } buf = buf[n:] obj.Props = append(obj.Props, prop) } return sz - len(buf), nil } // GetProp returns an object's property, either by its index when idx is non-negative, or by its name when name otherwise. // If the matching property is not found nil is returned. func GetProp(obj *Object, name string, idx int) *Property { if idx >= 0 { if idx < len(obj.Props) { return &obj.Props[idx] } } else { for i, p := range obj.Props { if p.name == name { return &obj.Props[i] } } } return nil }