2018-08-24 03:55:36 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
amf.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
|
|
|
Jake Lane <jake@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
amf.go is Copyright (C) 2017 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
|
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
|
|
|
*/
|
2018-08-24 00:17:13 +03:00
|
|
|
package rtmp
|
|
|
|
|
2018-08-24 03:03:05 +03:00
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2018-09-06 07:13:40 +03:00
|
|
|
"encoding/binary"
|
2018-08-24 03:03:05 +03:00
|
|
|
"log"
|
2018-09-12 15:12:10 +03:00
|
|
|
"math"
|
2018-08-24 03:03:05 +03:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2018-08-24 00:19:47 +03:00
|
|
|
var (
|
|
|
|
AMFObj_Invalid C_AMFObject
|
|
|
|
AMFProp_Invalid = C_AMFObjectProperty{p_type: AMF_INVALID}
|
|
|
|
)
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
const (
|
|
|
|
AMF3_INTEGER_MAX = 268435455
|
|
|
|
AMF3_INTEGER_MIN = -268435456
|
|
|
|
)
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-14 15:36:13 +03:00
|
|
|
func pp2b(b, e *byte) []byte {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
base := unsafe.Pointer(b)
|
|
|
|
len := uintptr(unsafe.Pointer(e)) - uintptr(base)
|
|
|
|
return (*[_Gi]byte)(base)[:len]
|
|
|
|
}
|
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
func pl2b(b *byte, l int) []byte {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
base := unsafe.Pointer(b)
|
|
|
|
return (*[_Gi]byte)(base)[:l]
|
|
|
|
}
|
|
|
|
|
2018-09-15 03:53:45 +03:00
|
|
|
func b2pp(buf []byte) (b, e *byte) {
|
|
|
|
if buf == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if len(buf) == 0 {
|
|
|
|
b = *(**byte)(unsafe.Pointer(&buf))
|
|
|
|
return b, b
|
|
|
|
}
|
|
|
|
b = &buf[0]
|
|
|
|
e = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + uintptr(len(buf))))
|
|
|
|
return b, e
|
|
|
|
}
|
|
|
|
|
2018-09-15 04:55:07 +03:00
|
|
|
func bAddr(buf []byte) *byte {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &buf[0]
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// unsigned short AMF_DecodeInt16(const char* data);
|
|
|
|
// amf.c +41
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_AMF_DecodeInt16(data []byte) uint16 {
|
|
|
|
return uint16(data[0])<<8 | uint16(data[1])
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// unsigned int AMF_DecodeInt24(const char* data);
|
|
|
|
// amf.c +50
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_AMF_DecodeInt24(data []byte) uint32 {
|
|
|
|
return uint32(data[0])<<16 | uint32(data[1])<<8 | uint32(data[2])
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// unsigned int AMF_DeocdeInt32(const char* data);
|
|
|
|
// amf.c +59
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_AMF_DecodeInt32(data []byte) uint32 {
|
|
|
|
return uint32(data[0])<<24 | uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// void AMF_DecodeString(const char* data, C_AVal* bv);
|
|
|
|
// amf.c +68
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_AMF_DecodeString(data []byte) string {
|
2018-09-06 07:13:40 +03:00
|
|
|
n := C_AMF_DecodeInt16(data)
|
2018-09-12 15:12:10 +03:00
|
|
|
return string(data[2 : 2+n])
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// void AMF_DecodeLongString(const char *data, AVal *bv);
|
|
|
|
// amf.c +75
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_AMF_DecodeLongString(data []byte) string {
|
2018-09-06 07:13:40 +03:00
|
|
|
n := C_AMF_DecodeInt32(data)
|
2018-09-12 15:12:10 +03:00
|
|
|
return string(data[2 : 2+n])
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// double AMF_DecodeNumber(const char* data);
|
|
|
|
// amf.c +82
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_AMF_DecodeNumber(data []byte) float64 {
|
|
|
|
return math.Float64frombits(binary.BigEndian.Uint64(data))
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// int AMF_DecodeBoolean(const char *data);
|
|
|
|
// amf.c +132
|
2018-09-12 15:12:10 +03:00
|
|
|
func C_AMF_DecodeBoolean(data []byte) bool {
|
|
|
|
return data[0] != 0
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeInt24(char* output, char* outend, int nVal);
|
|
|
|
// amf.c +149
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeInt24(dst []byte, val int32) []byte {
|
2018-09-14 15:36:13 +03:00
|
|
|
if len(dst) < 3 {
|
2018-08-24 00:28:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-14 15:36:13 +03:00
|
|
|
_ = dst[2]
|
|
|
|
dst[0] = byte(val >> 16)
|
|
|
|
dst[1] = byte(val >> 8)
|
|
|
|
dst[2] = byte(val)
|
|
|
|
if len(dst) == 3 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst[3:]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeInt32(char* output, char* outend, int nVal);
|
2018-09-14 15:36:13 +03:00
|
|
|
// amf.c +160
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeInt32(dst []byte, val int32) []byte {
|
2018-09-14 15:36:13 +03:00
|
|
|
if len(dst) < 4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint32(dst, uint32(val))
|
|
|
|
if len(dst) == 4 {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst[4:]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeString(char* output, char* outend, const C_AVal* bv);
|
2018-09-14 15:36:13 +03:00
|
|
|
// amf.c +173
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeString(dst []byte, val string) []byte {
|
2018-09-14 15:36:13 +03:00
|
|
|
const typeSize = 1
|
|
|
|
if len(val) < 65536 && len(val)+typeSize+binary.Size(int16(0)) > len(dst) {
|
2018-08-24 00:28:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-14 15:36:13 +03:00
|
|
|
if len(val)+typeSize+binary.Size(int32(0)) > len(dst) {
|
2018-09-06 07:13:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-14 15:36:13 +03:00
|
|
|
|
|
|
|
if len(val) < 65536 {
|
2018-09-06 07:13:40 +03:00
|
|
|
dst[0] = AMF_STRING
|
2018-09-14 15:36:13 +03:00
|
|
|
dst = dst[1:]
|
|
|
|
binary.BigEndian.PutUint16(dst[:2], uint16(len(val)))
|
|
|
|
dst = dst[2:]
|
|
|
|
copy(dst, val)
|
|
|
|
if len(dst) == len(val) {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst[len(val):]
|
2018-09-14 15:36:13 +03:00
|
|
|
}
|
|
|
|
dst[0] = AMF_LONG_STRING
|
|
|
|
dst = dst[1:]
|
|
|
|
binary.BigEndian.PutUint32(dst[:4], uint32(len(val)))
|
|
|
|
dst = dst[4:]
|
|
|
|
copy(dst, val)
|
|
|
|
if len(dst) == len(val) {
|
|
|
|
return nil
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst[len(val):]
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeNumber(char* output, char* outend, double dVal);
|
|
|
|
// amf.c +199
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeNumber(dst []byte, val float64) []byte {
|
2018-09-15 00:06:48 +03:00
|
|
|
if len(dst) < 9 {
|
2018-08-24 00:28:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-15 00:06:48 +03:00
|
|
|
dst[0] = AMF_NUMBER
|
|
|
|
dst = dst[1:]
|
|
|
|
binary.BigEndian.PutUint64(dst, math.Float64bits(val))
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst[8:]
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeBoolean(char* output, char* outend, int bVal);
|
|
|
|
// amf.c +260
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeBoolean(dst []byte, val bool) []byte {
|
2018-09-14 15:45:31 +03:00
|
|
|
if len(dst) < 2 {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-14 15:45:31 +03:00
|
|
|
dst[0] = AMF_BOOLEAN
|
|
|
|
if val {
|
|
|
|
dst[1] = 1
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-14 15:45:31 +03:00
|
|
|
if len(dst) == 2 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst[2:]
|
2018-09-14 15:45:31 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeNamedString(char* output, char* outend, const C_AVal* strName, const C_AVal* strValue);
|
|
|
|
// amf.c +273
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeNamedString(dst []byte, key, val string) []byte {
|
2018-09-15 02:44:27 +03:00
|
|
|
if 2+len(key) > len(dst) {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-06 07:13:40 +03:00
|
|
|
binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
|
2018-09-15 02:44:27 +03:00
|
|
|
dst = dst[2:]
|
|
|
|
copy(dst, key)
|
|
|
|
if len(key) == len(dst) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return C_AMF_EncodeString(dst[len(key):], val)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeNamedNumber(char* output, char* outend, const C_AVal* strName, double dVal);
|
|
|
|
// amf.c +286
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeNamedNumber(dst []byte, key string, val float64) []byte {
|
2018-09-15 02:44:27 +03:00
|
|
|
if 2+len(key) > len(dst) {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-06 07:13:40 +03:00
|
|
|
binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
|
2018-09-15 02:44:27 +03:00
|
|
|
dst = dst[2:]
|
|
|
|
copy(dst, key)
|
|
|
|
if len(key) == len(dst) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return C_AMF_EncodeNumber(dst[len(key):], val)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeNamedBoolean(char* output, char* outend, const C_AVal* strname, int bVal);
|
|
|
|
// amf.c +299
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeNamedBoolean(dst []byte, key string, val bool) []byte {
|
2018-09-15 02:44:27 +03:00
|
|
|
if 2+len(key) > len(dst) {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-06 07:13:40 +03:00
|
|
|
binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
|
2018-09-15 02:44:27 +03:00
|
|
|
dst = dst[2:]
|
|
|
|
copy(dst, key)
|
|
|
|
if len(key) == len(dst) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return C_AMF_EncodeBoolean(dst[len(key):], val)
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// void AMFProp_SetName(AMFObjectProperty *prop, AVal *name);
|
|
|
|
// amf.c +318
|
2018-09-06 10:00:39 +03:00
|
|
|
func C_AMFProp_SetName(prop *C_AMFObjectProperty, name string) {
|
|
|
|
prop.p_name = name
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// double AMFProp_GetNumber(AMFObjectProperty* prop);
|
|
|
|
// amf.c +330
|
|
|
|
func C_AMFProp_GetNumber(prop *C_AMFObjectProperty) float64 {
|
2018-09-12 05:37:43 +03:00
|
|
|
return prop.p_vu.p_number
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// void AMFProp_GetString(AMFObjectProperty* prop, AVal* str);
|
2018-08-24 16:00:40 +03:00
|
|
|
// amf.c +341
|
2018-09-06 09:40:15 +03:00
|
|
|
func C_AMFProp_GetString(prop *C_AMFObjectProperty) string {
|
2018-08-24 00:28:22 +03:00
|
|
|
if prop.p_type == AMF_STRING {
|
2018-09-06 09:40:15 +03:00
|
|
|
return prop.p_vu.p_aval
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-06 09:40:15 +03:00
|
|
|
return ""
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// void AMFProp_GetObject(AMFObjectProperty *prop, AMFObject *obj);
|
|
|
|
// amf.c +351
|
|
|
|
func C_AMFProp_GetObject(prop *C_AMFObjectProperty, obj *C_AMFObject) {
|
|
|
|
if prop.p_type == AMF_OBJECT {
|
|
|
|
*obj = prop.p_vu.p_object
|
|
|
|
} else {
|
|
|
|
*obj = AMFObj_Invalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMFPropEncode(AMFOBjectProperty* prop, char* pBufer, char* pBufEnd);
|
|
|
|
// amf.c +366
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_PropEncode(p *C_AMFObjectProperty, dst []byte) []byte {
|
2018-08-24 16:00:40 +03:00
|
|
|
if p.p_type == AMF_INVALID {
|
|
|
|
return nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 03:53:45 +03:00
|
|
|
if p.p_type != AMF_NULL && len(p.p_name)+2+1 >= len(dst) {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-06 10:00:39 +03:00
|
|
|
if p.p_type != AMF_NULL && len(p.p_name) != 0 {
|
2018-09-15 03:53:45 +03:00
|
|
|
binary.BigEndian.PutUint16(dst[:2], uint16(len(p.p_name)))
|
|
|
|
dst = dst[2:]
|
|
|
|
copy(dst, p.p_name)
|
|
|
|
dst = dst[len(p.p_name):]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
switch p.p_type {
|
|
|
|
case AMF_NUMBER:
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_EncodeNumber(dst, p.p_vu.p_number)
|
2018-08-24 16:00:40 +03:00
|
|
|
case AMF_BOOLEAN:
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_EncodeBoolean(dst, p.p_vu.p_number != 0)
|
2018-08-24 16:00:40 +03:00
|
|
|
case AMF_STRING:
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_EncodeString(dst, p.p_vu.p_aval)
|
2018-08-24 16:00:40 +03:00
|
|
|
case AMF_NULL:
|
2018-09-15 03:53:45 +03:00
|
|
|
if len(dst) < 2 {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-15 03:53:45 +03:00
|
|
|
dst[0] = AMF_NULL
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = dst[1:]
|
2018-08-24 16:00:40 +03:00
|
|
|
case AMF_OBJECT:
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_Encode(&p.p_vu.p_object, dst)
|
2018-08-24 16:00:40 +03:00
|
|
|
case AMF_ECMA_ARRAY:
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_EncodeEcmaArray(&p.p_vu.p_object, dst)
|
2018-08-24 16:00:40 +03:00
|
|
|
case AMF_STRICT_ARRAY:
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_EncodeArray(&p.p_vu.p_object, dst)
|
2018-08-24 16:00:40 +03:00
|
|
|
default:
|
|
|
|
log.Println("C_AMF_PropEncode: invalid type!")
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = nil
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
|
|
|
// int AMFProp_Decode(C_AMFObjectProperty* prop, const char* pBuffer, int nSize, int bDecodeName);
|
|
|
|
// amf.c +619
|
2018-09-16 02:42:59 +03:00
|
|
|
func C_AMFProp_Decode(prop *C_AMFObjectProperty, data []byte, bDecodeName int32) int32 {
|
2018-09-06 10:00:39 +03:00
|
|
|
prop.p_name = ""
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
nOriginalSize := len(data)
|
|
|
|
if len(data) == 0 {
|
2018-08-24 00:28:22 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s: Empty buffer/no buffer pointer!", __FUNCTION__);
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
if bDecodeName != 0 && len(data) < 4 {
|
2018-08-24 00:28:22 +03:00
|
|
|
// at least name (length + at least 1 byte) and 1 byte of data
|
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGDEBUG, "%s: Not enough data for decoding with name, less than 4 bytes!",__FUNCTION__);
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if bDecodeName != 0 {
|
2018-09-16 02:42:59 +03:00
|
|
|
nNameSize := C_AMF_DecodeInt16(data[:2])
|
|
|
|
if int(nNameSize) > len(data)-2 {
|
2018-08-24 00:28:22 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
//RTMP_Log(RTMP_LOGDEBUG, "%s: Name size out of range: namesize (%d) > len (%d) - 2",__FUNCTION__, nNameSize, nSize);
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
prop.p_name = C_AMF_DecodeString(data)
|
|
|
|
data = data[2+nNameSize:]
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
if len(data) == 0 {
|
2018-08-24 00:28:22 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
prop.p_type = C_AMFDataType(data[0])
|
|
|
|
data = data[1:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
var nRes int32
|
2018-08-24 00:28:22 +03:00
|
|
|
switch prop.p_type {
|
|
|
|
case AMF_NUMBER:
|
2018-09-16 02:42:59 +03:00
|
|
|
if len(data) < 8 {
|
2018-08-24 00:28:22 +03:00
|
|
|
return -1
|
|
|
|
}
|
2018-09-16 02:42:59 +03:00
|
|
|
prop.p_vu.p_number = C_AMF_DecodeNumber(data[:8])
|
|
|
|
data = data[8:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-25 11:07:36 +03:00
|
|
|
case AMF_BOOLEAN:
|
|
|
|
panic("AMF_BOOLEAN not supported")
|
2018-08-28 13:40:13 +03:00
|
|
|
|
2018-08-24 00:28:22 +03:00
|
|
|
case AMF_STRING:
|
2018-09-16 02:42:59 +03:00
|
|
|
nStringSize := C_AMF_DecodeInt16(data[:2])
|
|
|
|
if len(data) < int(nStringSize+2) {
|
2018-08-25 11:07:36 +03:00
|
|
|
return -1
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-16 02:42:59 +03:00
|
|
|
prop.p_vu.p_aval = C_AMF_DecodeString(data)
|
|
|
|
data = data[2+nStringSize:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
|
|
|
case AMF_OBJECT:
|
2018-09-16 02:42:59 +03:00
|
|
|
nRes := C_AMF_Decode(&prop.p_vu.p_object, bAddr(data), int32(len(data)), 1)
|
2018-08-25 11:07:36 +03:00
|
|
|
if nRes == -1 {
|
|
|
|
return -1
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-16 02:42:59 +03:00
|
|
|
data = data[nRes:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
|
|
|
case AMF_MOVIECLIP:
|
2018-08-25 11:07:36 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
log.Println("AMFProp_Decode: MAF_MOVIECLIP reserved!")
|
|
|
|
//RTMP_Log(RTMP_LOGERROR, "AMF_MOVIECLIP reserved!");
|
|
|
|
return -1
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-25 11:07:36 +03:00
|
|
|
case AMF_NULL, AMF_UNDEFINED, AMF_UNSUPPORTED:
|
2018-08-24 00:28:22 +03:00
|
|
|
prop.p_type = AMF_NULL
|
|
|
|
|
|
|
|
case AMF_REFERENCE:
|
2018-08-25 11:07:36 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
log.Println("AMFProp_Decode: AMF_REFERENCE not supported!")
|
|
|
|
//RTMP_Log(RTMP_LOGERROR, "AMF_REFERENCE not supported!");
|
|
|
|
return -1
|
|
|
|
|
|
|
|
case AMF_ECMA_ARRAY:
|
|
|
|
// next comes the rest, mixed array has a final 0x000009 mark and names, so its an object
|
2018-09-16 02:42:59 +03:00
|
|
|
data = data[4:]
|
|
|
|
nRes = C_AMF_Decode(&prop.p_vu.p_object, bAddr(data), int32(len(data)), 1)
|
2018-08-25 11:07:36 +03:00
|
|
|
if nRes == -1 {
|
2018-08-24 00:28:22 +03:00
|
|
|
return -1
|
|
|
|
}
|
2018-09-16 02:42:59 +03:00
|
|
|
data = data[nRes:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-25 11:07:36 +03:00
|
|
|
case AMF_OBJECT_END:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
case AMF_STRICT_ARRAY:
|
|
|
|
panic("AMF_STRICT_ARRAY not supported")
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-25 11:07:36 +03:00
|
|
|
case AMF_DATE:
|
|
|
|
panic("AMF_DATE not supported")
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-25 11:07:36 +03:00
|
|
|
case AMF_LONG_STRING, AMF_XML_DOC:
|
|
|
|
panic("AMF_LONG_STRING, AMF_XML_DOC not supported")
|
2018-08-28 13:40:13 +03:00
|
|
|
|
2018-08-24 00:28:22 +03:00
|
|
|
case AMF_RECORDSET:
|
2018-08-25 11:07:36 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
log.Println("AMFProp_Decode: AMF_RECORDSET reserved!")
|
|
|
|
//RTMP_Log(RTMP_LOGERROR, "AMF_RECORDSET reserved!");
|
|
|
|
return -1
|
2018-08-24 00:28:22 +03:00
|
|
|
|
|
|
|
case AMF_TYPED_OBJECT:
|
2018-08-25 11:07:36 +03:00
|
|
|
// TODO use new logger here
|
|
|
|
// RTMP_Log(RTMP_LOGERROR, "AMF_TYPED_OBJECT not supported!")
|
|
|
|
return -1
|
|
|
|
|
|
|
|
case AMF_AVMPLUS:
|
|
|
|
panic("AMF_AVMPLUS not supported")
|
2018-08-28 13:40:13 +03:00
|
|
|
|
2018-08-24 00:28:22 +03:00
|
|
|
default:
|
|
|
|
// TODO use new logger here
|
|
|
|
//RTMP_Log(RTMP_LOGDEBUG, "%s - unknown datatype 0x%02x, @%p", __FUNCTION__,
|
|
|
|
//prop.p_type, pBuffer - 1);
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-09-16 02:42:59 +03:00
|
|
|
return int32(nOriginalSize - len(data))
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// void AMFProp_Reset(AMFObjectProperty* prop);
|
|
|
|
// amf.c +875
|
|
|
|
func C_AMFProp_Reset(prop *C_AMFObjectProperty) {
|
|
|
|
if prop.p_type == AMF_OBJECT || prop.p_type == AMF_ECMA_ARRAY ||
|
|
|
|
prop.p_type == AMF_STRICT_ARRAY {
|
|
|
|
C_AMF_Reset(&prop.p_vu.p_object)
|
|
|
|
} else {
|
2018-09-06 09:40:15 +03:00
|
|
|
prop.p_vu.p_aval = ""
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-08-24 16:00:40 +03:00
|
|
|
prop.p_type = AMF_INVALID
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_Encode(AMFObject* obj, char* pBuffer, char* pBufEnd);
|
|
|
|
// amf.c +891
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_Encode(obj *C_AMFObject, dst []byte) []byte {
|
2018-09-15 04:12:16 +03:00
|
|
|
if len(dst) < 5 {
|
2018-08-24 00:28:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 04:12:16 +03:00
|
|
|
dst[0] = AMF_OBJECT
|
|
|
|
dst = dst[1:]
|
2018-08-24 16:00:40 +03:00
|
|
|
|
2018-09-15 03:50:44 +03:00
|
|
|
for i := 0; i < len(obj.o_props); i++ {
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_PropEncode(&obj.o_props[i], dst)
|
|
|
|
if dst == nil {
|
2018-08-24 16:00:40 +03:00
|
|
|
log.Println("C_AMF_Encode: failed to encode property in index")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 04:12:16 +03:00
|
|
|
if len(dst) < 4 {
|
2018-08-24 00:28:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return C_AMF_EncodeInt24(dst, AMF_OBJECT_END)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeEcmaArray(AMFObject* obj, char* pBuffer, char* pBufEnd);
|
|
|
|
// amf.c +924
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeEcmaArray(obj *C_AMFObject, dst []byte) []byte {
|
2018-09-15 04:19:17 +03:00
|
|
|
if len(dst) < 5 {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-15 04:19:17 +03:00
|
|
|
dst[0] = AMF_ECMA_ARRAY
|
|
|
|
dst = dst[1:]
|
|
|
|
binary.BigEndian.PutUint32(dst[:4], uint32(len(obj.o_props)))
|
|
|
|
dst = dst[4:]
|
2018-08-24 16:00:40 +03:00
|
|
|
|
2018-09-15 03:50:44 +03:00
|
|
|
for i := 0; i < len(obj.o_props); i++ {
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_PropEncode(&obj.o_props[i], dst)
|
|
|
|
if dst == nil {
|
2018-08-24 16:00:40 +03:00
|
|
|
log.Println("C_AMF_EncodeEcmaArray: failed to encode property!")
|
2018-08-24 00:28:22 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 04:19:17 +03:00
|
|
|
if len(dst) < 4 {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-15 04:55:07 +03:00
|
|
|
return C_AMF_EncodeInt24(dst, AMF_OBJECT_END)
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// char* AMF_EncodeArray(AMFObject* obj, char* pBuffer, char* pBufEnd);
|
|
|
|
// amf.c +959
|
2018-09-15 04:55:07 +03:00
|
|
|
func C_AMF_EncodeArray(obj *C_AMFObject, dst []byte) []byte {
|
2018-09-15 04:22:36 +03:00
|
|
|
if len(dst) < 5 {
|
2018-08-24 16:00:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-15 04:22:36 +03:00
|
|
|
dst[0] = AMF_STRICT_ARRAY
|
|
|
|
dst = dst[1:]
|
|
|
|
binary.BigEndian.PutUint32(dst[:4], uint32(len(obj.o_props)))
|
|
|
|
dst = dst[4:]
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-15 03:50:44 +03:00
|
|
|
for i := 0; i < len(obj.o_props); i++ {
|
2018-09-15 04:55:07 +03:00
|
|
|
dst = C_AMF_PropEncode(&obj.o_props[i], dst)
|
|
|
|
if dst == nil {
|
2018-09-15 03:50:44 +03:00
|
|
|
log.Println("C_AMF_EncodeArray: failed to encode property!")
|
2018-08-24 16:00:40 +03:00
|
|
|
break
|
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-08-24 16:00:40 +03:00
|
|
|
|
2018-09-15 04:55:07 +03:00
|
|
|
return dst
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// int AMF_DecodeArray(AMFObject *obj, const char *pBuffer, int nSize, int nArrayLen, int bDecodeName);
|
|
|
|
// amf.c +993
|
|
|
|
func C_AMF_DecodeArray(obj *C_AMFObject, pBuffer *byte, nSize, nArrayLen, bDecodeName int32) int32 {
|
|
|
|
nOriginalSize := nSize
|
|
|
|
var bError int32 = 0
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-15 03:50:44 +03:00
|
|
|
obj.o_props = obj.o_props[:0]
|
2018-08-24 16:00:40 +03:00
|
|
|
for nArrayLen > 0 {
|
|
|
|
var prop C_AMFObjectProperty
|
|
|
|
var nRes int32
|
|
|
|
nArrayLen--
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
if nSize <= 0 {
|
|
|
|
bError = 1
|
|
|
|
break
|
|
|
|
}
|
2018-09-16 02:42:59 +03:00
|
|
|
nRes = C_AMFProp_Decode(&prop, pl2b(pBuffer, int(nSize)), bDecodeName)
|
2018-08-24 16:00:40 +03:00
|
|
|
if nRes == -1 {
|
|
|
|
bError = 1
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
nSize -= nRes
|
|
|
|
pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), int(nRes)))
|
2018-09-15 03:50:44 +03:00
|
|
|
obj.o_props = append(obj.o_props, prop)
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-08-24 16:00:40 +03:00
|
|
|
if bError != 0 {
|
|
|
|
return -1
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
return nOriginalSize - nSize
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// int AMF_Decode(AMFObject *obj, const char* pBuffer, int nSize, int bDecodeName);
|
|
|
|
// amf.c +1180
|
2018-09-12 05:37:43 +03:00
|
|
|
func C_AMF_Decode(obj *C_AMFObject, pBuffer *byte, nSize, bDecodeName int32) int32 {
|
|
|
|
nOriginalSize := nSize
|
|
|
|
var bError int32
|
2018-08-24 00:28:22 +03:00
|
|
|
|
2018-09-15 03:50:44 +03:00
|
|
|
obj.o_props = obj.o_props[:0]
|
2018-08-24 16:00:40 +03:00
|
|
|
for nSize > 0 {
|
|
|
|
var prop C_AMFObjectProperty
|
|
|
|
var nRes int32
|
|
|
|
|
2018-09-12 15:12:10 +03:00
|
|
|
if nSize >= 3 && C_AMF_DecodeInt24((*[_Gi]byte)(unsafe.Pointer(pBuffer))[:3]) == AMF_OBJECT_END {
|
2018-08-24 16:00:40 +03:00
|
|
|
nSize -= 3
|
|
|
|
bError = 0
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if bError != 0 {
|
|
|
|
// TODO use new logger here
|
|
|
|
log.Println("AMF_Decode: decoding error, ignoring bytes until next known pattern!")
|
|
|
|
nSize--
|
|
|
|
pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), 1))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO port AMFProp_Decode
|
2018-09-16 02:42:59 +03:00
|
|
|
nRes = C_AMFProp_Decode(&prop, pl2b(pBuffer, int(nSize)), bDecodeName)
|
2018-08-24 16:00:40 +03:00
|
|
|
// nRes = int32(C.AMFProp_Decode(&prop, (*byte)(unsafe.Pointer(pBuffer)),
|
|
|
|
// int32(nSize), int32(bDecodeName)))
|
|
|
|
if nRes == -1 {
|
|
|
|
bError = 1
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
nSize -= nRes
|
|
|
|
if nSize < 0 {
|
|
|
|
bError = 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), int(nRes)))
|
2018-09-15 03:50:44 +03:00
|
|
|
obj.o_props = append(obj.o_props, prop)
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
if bError != 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return nOriginalSize - nSize
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// AMFObjectProperty* AMF_GetProp(AMFObject *obj, const AVal* name, int nIndex);
|
|
|
|
// amf.c + 1249
|
2018-09-15 03:50:44 +03:00
|
|
|
func C_AMF_GetProp(obj *C_AMFObject, name string, idx int32) *C_AMFObjectProperty {
|
|
|
|
if idx >= 0 {
|
|
|
|
if idx < int32(len(obj.o_props)) {
|
|
|
|
return &obj.o_props[idx]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-15 03:50:44 +03:00
|
|
|
for i, p := range obj.o_props {
|
|
|
|
if p.p_name == name {
|
|
|
|
return &obj.o_props[i]
|
2018-08-24 16:00:40 +03:00
|
|
|
}
|
|
|
|
}
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
2018-09-12 05:37:43 +03:00
|
|
|
return &AMFProp_Invalid
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:00:40 +03:00
|
|
|
// void AMF_Reset(AMFObject* obj);
|
|
|
|
// amf.c +1282
|
|
|
|
func C_AMF_Reset(obj *C_AMFObject) {
|
2018-09-15 03:50:44 +03:00
|
|
|
for i := range obj.o_props {
|
|
|
|
C_AMFProp_Reset(&obj.o_props[i])
|
|
|
|
}
|
|
|
|
obj.o_props = obj.o_props[:0]
|
2018-08-24 00:28:22 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 03:03:05 +03:00
|
|
|
/*
|
2018-08-24 00:28:22 +03:00
|
|
|
// void AMF3CD_AddProp(AMF3ClassDef *cd, AVal *prop);
|
|
|
|
// amf.c +1298
|
|
|
|
func AMF3CD_AddProp(cd *C.AMF3ClassDef, prop *C_AVal) {
|
|
|
|
if cd.cd_num&0x0f == 0 {
|
|
|
|
cd.cd_props = (*C_AVal)(realloc(unsafe.Pointer(cd.cd_props), int(uintptr(cd.cd_num+16)*unsafe.Sizeof(C_AVal{}))))
|
|
|
|
}
|
|
|
|
*(*C_AVal)(incPtr(unsafe.Pointer(cd.cd_props), int(cd.cd_num), int(unsafe.Sizeof(C_AVal{})))) = *prop
|
|
|
|
cd.cd_num++
|
|
|
|
}
|
2018-08-24 03:03:05 +03:00
|
|
|
*/
|