diff --git a/rtmp/amf.go b/rtmp/amf.go index 581b06ef..a72efecf 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -79,14 +79,18 @@ func C_AMF_DecodeInt16(data *byte) uint16 { // unsigned int AMF_DecodeInt24(const char* data); // amf.c +50 func C_AMF_DecodeInt24(data *byte) uint32 { - // TODO Understand logic and simplify - c := (*uint8)(unsafe.Pointer(data)) - dst := uint32(int32(*c) << 16) - dst |= uint32(int32(*((*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) + - (uintptr)(int32(1))*unsafe.Sizeof(*c))))) << 8) - dst |= uint32(int32(*((*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) + - (uintptr)(int32(2))*unsafe.Sizeof(*c)))))) - return dst + c := (*[3]byte)(unsafe.Pointer(data)) + return (uint32(c[0]) << 16) | (uint32(c[1]) << 8) | uint32(c[2]) + /* + // TODO Understand logic and simplify + c := (*uint8)(unsafe.Pointer(data)) + dst := uint32(int32(*c) << 16) + dst |= uint32(int32(*((*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) + + (uintptr)(int32(1))*unsafe.Sizeof(*c))))) << 8) + dst |= uint32(int32(*((*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) + + (uintptr)(int32(2))*unsafe.Sizeof(*c)))))) + return dst + */ } // unsigned int AMF_DeocdeInt32(const char* data); @@ -519,170 +523,136 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa switch prop.p_type { case AMF_NUMBER: - log.Println("1") if nSize < 8 { return -1 } prop.p_vu.p_number = float64(C_AMF_DecodeNumber(pBuffer)) nSize -= 8 + case AMF_BOOLEAN: + panic("AMF_BOOLEAN not supported") /* - case AMF_BOOLEAN: - log.Println("2") - if nSize < 1 { - return -1 - } - prop.p_vu.p_number = float64(C_AMF_DecodeBoolean((*byte)(unsafe.Pointer(pBuffer)))) - nSize-- + if nSize < 1 { + return -1 + } + prop.p_vu.p_number = float64(C_AMF_DecodeBoolean((*byte)(unsafe.Pointer(pBuffer)))) + nSize-- */ case AMF_STRING: - { - log.Println("3") - var nStringSize = C_AMF_DecodeInt16(pBuffer) + var nStringSize = C_AMF_DecodeInt16(pBuffer) - if int64(nSize) < int64(nStringSize)+2 { - return -1 - } - C_AMF_DecodeString(pBuffer, &prop.p_vu.p_aval) - nSize -= int32(2 + nStringSize) + if int64(nSize) < int64(nStringSize)+2 { + return -1 } + C_AMF_DecodeString(pBuffer, &prop.p_vu.p_aval) + nSize -= int32(2 + nStringSize) case AMF_OBJECT: - { - log.Println("4") - var nRes int32 = int32(C_AMF_Decode(&prop.p_vu.p_object, pBuffer, nSize, 1)) - if nRes == -1 { - return -1 - } - nSize -= nRes - + var nRes int32 = int32(C_AMF_Decode(&prop.p_vu.p_object, pBuffer, nSize, 1)) + if nRes == -1 { + return -1 } + nSize -= nRes case AMF_MOVIECLIP: - { - log.Println("5") - // TODO use new logger here - log.Println("AMFProp_Decode: MAF_MOVIECLIP reserved!") - //RTMP_Log(RTMP_LOGERROR, "AMF_MOVIECLIP reserved!"); - return -1 + // TODO use new logger here + log.Println("AMFProp_Decode: MAF_MOVIECLIP reserved!") + //RTMP_Log(RTMP_LOGERROR, "AMF_MOVIECLIP reserved!"); + return -1 - } - case AMF_NULL: - log.Println("6") - case AMF_UNDEFINED: - log.Println("7") - case AMF_UNSUPPORTED: - log.Println("8") + case AMF_NULL, AMF_UNDEFINED, AMF_UNSUPPORTED: prop.p_type = AMF_NULL case AMF_REFERENCE: - { - log.Println("9") - // TODO use new logger here - log.Println("AMFProp_Decode: AMF_REFERENCE not supported!") - //RTMP_Log(RTMP_LOGERROR, "AMF_REFERENCE not supported!"); - return -1 - } + + // 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: - { - log.Println("10") + + nSize -= 4 + + // next comes the rest, mixed array has a final 0x000009 mark and names, so its an object + nRes = C_AMF_Decode(&prop.p_vu.p_object, (*byte)(incBytePtr( + unsafe.Pointer(pBuffer), 4)), nSize, 1) + if nRes == -1 { + return -1 + } + nSize -= nRes + + case AMF_OBJECT_END: + + return -1 + + case AMF_STRICT_ARRAY: + panic("AMF_STRICT_ARRAY not supported") + /* + nArrayLen := int32(C_AMF_DecodeInt32(pBuffer)) nSize -= 4 - // next comes the rest, mixed array has a final 0x000009 mark and names, so its an object - nRes = C_AMF_Decode(&prop.p_vu.p_object, (*byte)(incBytePtr( - unsafe.Pointer(pBuffer), 4)), nSize, 1) + nRes = C_AMF_DecodeArray(&prop.p_vu.p_object, (*byte)(incBytePtr(unsafe.Pointer(pBuffer), 4)), nSize, int32(nArrayLen), FALSE) if nRes == -1 { return -1 } nSize -= nRes - } + */ - case AMF_OBJECT_END: - { - log.Println("11") - return -1 - } - /* - case AMF_STRICT_ARRAY: - { - log.Println("12") - nArrayLen := int32(C_AMF_DecodeInt32(pBuffer)) - nSize -= 4 + case AMF_DATE: + panic("AMF_DATE not supported") - nRes = C_AMF_DecodeArray(&prop.p_vu.p_object, (*byte)(incBytePtr(unsafe.Pointer(pBuffer), 4)), nSize, int32(nArrayLen), FALSE) - if nRes == -1 { - return -1 - } - nSize -= nRes + /* + // TODO use new logger here + //RTMP_Log(RTMP_LOGDEBUG, "AMF_DATE"); + + if nSize < 10 { + return -1 } - */ - /* - case AMF_DATE: - { - log.Println("13") - // TODO use new logger here - //RTMP_Log(RTMP_LOGDEBUG, "AMF_DATE"); - if nSize < 10 { - return -1 - } + prop.p_vu.p_number = float64(C_AMF_DecodeNumber(pBuffer)) + prop.p_UTCoffset = C.int16_t(C_AMF_DecodeInt16((*byte)(incBytePtr(unsafe.Pointer(pBuffer), 8)))) - prop.p_vu.p_number = float64(C_AMF_DecodeNumber(pBuffer)) - prop.p_UTCoffset = C.int16_t(C_AMF_DecodeInt16((*byte)(incBytePtr(unsafe.Pointer(pBuffer), 8)))) - - nSize -= 10 + nSize -= 10 + */ + case AMF_LONG_STRING, AMF_XML_DOC: + panic("AMF_LONG_STRING, AMF_XML_DOC not supported") + /* + var nStringSize uint32 = C_AMF_DecodeInt32(pBuffer) + if int64(nSize) < int64(nStringSize)+4 { + return -1 } - */ - case AMF_LONG_STRING: - log.Println("14") - /* - case AMF_XML_DOC: - { - log.Println("15") - var nStringSize uint32 = C_AMF_DecodeInt32(pBuffer) - if int64(nSize) < int64(nStringSize)+4 { - return -1 - } - C_AMF_DecodeLongString(pBuffer, &prop.p_vu.p_aval) - nSize -= int32(4 + nStringSize) - if prop.p_type == AMF_LONG_STRING { - prop.p_type = AMF_STRING - } + C_AMF_DecodeLongString(pBuffer, &prop.p_vu.p_aval) + nSize -= int32(4 + nStringSize) + if prop.p_type == AMF_LONG_STRING { + prop.p_type = AMF_STRING } - */ + */ case AMF_RECORDSET: - { - log.Println("16") - // TODO use new logger here - log.Println("AMFProp_Decode: AMF_RECORDSET reserved!") - //RTMP_Log(RTMP_LOGERROR, "AMF_RECORDSET reserved!"); - return -1 + // TODO use new logger here + log.Println("AMFProp_Decode: AMF_RECORDSET reserved!") + //RTMP_Log(RTMP_LOGERROR, "AMF_RECORDSET reserved!"); + return -1 - } case AMF_TYPED_OBJECT: - { - log.Println("17") - // TODO use new logger here - // RTMP_Log(RTMP_LOGERROR, "AMF_TYPED_OBJECT not supported!") - return -1 - } - /* - case AMF_AVMPLUS: - { - log.Println("18") - nRes := int32(C.AMF3_Decode(&prop.p_vu.p_object, (*byte)(unsafe.Pointer(pBuffer)), int32(nSize), 1)) - if nRes == -1 { - return -1 - } - nSize -= nRes - prop.p_type = AMF_OBJECT + // TODO use new logger here + // RTMP_Log(RTMP_LOGERROR, "AMF_TYPED_OBJECT not supported!") + return -1 + + case AMF_AVMPLUS: + panic("AMF_AVMPLUS not supported") + /* + log.Println("18") + nRes := int32(C.AMF3_Decode(&prop.p_vu.p_object, (*byte)(unsafe.Pointer(pBuffer)), int32(nSize), 1)) + if nRes == -1 { + return -1 } - */ + nSize -= nRes + prop.p_type = AMF_OBJECT + */ default: - log.Println("19") // TODO use new logger here //RTMP_Log(RTMP_LOGDEBUG, "%s - unknown datatype 0x%02x, @%p", __FUNCTION__, //prop.p_type, pBuffer - 1);