From 4539e66780941175c87c2344f3f685f3e45f0fb7 Mon Sep 17 00:00:00 2001 From: hyc <hyc@400ebc74-4327-4243-bc38-086b20814532> Date: Wed, 16 Dec 2009 21:16:10 +0000 Subject: [PATCH] Move bytes.c functions out of main namespace git-svn-id: svn://svn.mplayerhq.hu/rtmpdump/trunk@76 400ebc74-4327-4243-bc38-086b20814532 --- Makefile | 5 +- amf.c | 183 +++++++++++++++++++++++++++++++++++++++++-------------- amf.h | 100 +++++++++++++++--------------- bytes.c | 147 -------------------------------------------- bytes.h | 35 ----------- rtmp.c | 26 +++++++- 6 files changed, 215 insertions(+), 281 deletions(-) delete mode 100644 bytes.c diff --git a/Makefile b/Makefile index b526622..4ab0b5c 100644 --- a/Makefile +++ b/Makefile @@ -40,14 +40,13 @@ clean: streams: bytes.o log.o rtmp.o AMFObject.o rtmppacket.o streams.o parseurl.o dh.o handshake.o $(CXX) $(LDFLAGS) $^ -o $@$(EXT) $(SLIBS) -rtmpdump: log.o rtmp.o dh.o amf.o bytes.o rtmpdump.o parseurl.o +rtmpdump: log.o rtmp.o dh.o amf.o rtmpdump.o parseurl.o $(CC) $(LDFLAGS) $^ -o $@$(EXT) $(LIBS) -bytes.o: bytes.c bytes.h Makefile log.o: log.c log.h Makefile parseurl.o: parseurl.c parseurl.h log.h Makefile streams.o: streams.cpp rtmp.h log.h Makefile dh.o: dh.c dh.h log.h Makefile rtmp.o: rtmp.c rtmp.h handshake.h log.h amf.h Makefile -amf.o: amf.c amf.h Makefile +amf.o: amf.c amf.h bytes.h Makefile rtmpdump.o: rtmpdump.c rtmp.h log.h amf.h Makefile diff --git a/amf.c b/amf.c index aa7660a..7c92c6b 100644 --- a/amf.c +++ b/amf.c @@ -58,12 +58,55 @@ AMF_DecodeInt32(const char *data) } void -AMF_DecodeString(const char *data, AVal *bv) +AMF_DecodeString(const char *data, AVal * bv) { bv->av_len = AMF_DecodeInt16(data); bv->av_val = (bv->av_len > 0) ? (char *) data + 2 : NULL; } +double +AMF_DecodeNumber(const char *data) +{ +#if __FLOAT_WORD_ORDER == __BYTE_ORDER +#if __BYTE_ORDER == __BIG_ENDIAN + double dVal; + memcpy(&dVal, data, 8); + return dVal; +#elif __BYTE_ORDER == __LITTLE_ENDIAN + double dVal; + unsigned char *ci, *co; + ci = (unsigned char *) data; + co = (unsigned char *) &dVal; + co[0] = ci[7]; + co[1] = ci[6]; + co[2] = ci[5]; + co[3] = ci[4]; + co[4] = ci[3]; + co[5] = ci[2]; + co[6] = ci[1]; + co[7] = ci[0]; + return dVal; +#endif +#else +#if __BYTE_ORDER == __LITTLE_ENDIAN // __FLOAT_WORD_ORER == __BIG_ENDIAN + uint32_t in1 = *((uint32_t *) data); + uint32_t in2 = *((uint32_t *) (data + 4)); + + in1 = __bswap_32(in1); + in2 = __bswap_32(in2); + + uint64_t res = ((uint64_t) in2 << 32) | (uint64_t) in1; + return *((double *) &res); +#else // __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN + uint32_t in1 = *((uint32_t *) data); + uint32_t in2 = *((uint32_t *) (data + 4)); + + uint64_t res = ((uint64_t) in1 << 32) | (uint64_t) in2; + return *((double *) &res); +#endif +#endif +} + bool AMF_DecodeBoolean(const char *data) { @@ -98,7 +141,7 @@ AMF_EncodeInt32(char *output, int nVal) } int -AMF_EncodeString(char *output, const AVal *bv) +AMF_EncodeString(char *output, const AVal * bv) { char *buf = output; *buf++ = AMF_STRING; @@ -114,11 +157,57 @@ AMF_EncodeString(char *output, const AVal *bv) int AMF_EncodeNumber(char *output, double dVal) { - char *buf = output; - *buf++ = AMF_NUMBER; // type: Number - - WriteNumber(buf, dVal); - buf += 8; + *output++ = AMF_NUMBER; // type: Number + +#if __FLOAT_WORD_ORDER == __BYTE_ORDER +#if __BYTE_ORDER == __BIG_ENDIAN + memcpy(output, &dVal, 8); +#elif __BYTE_ORDER == __LITTLE_ENDIAN + { + unsigned char *ci, *co; + ci = (unsigned char *) &dVal; + co = (unsigned char *) output; + co[0] = ci[7]; + co[1] = ci[6]; + co[2] = ci[5]; + co[3] = ci[4]; + co[4] = ci[3]; + co[5] = ci[2]; + co[6] = ci[1]; + co[7] = ci[0]; + } +#endif +#else +#if __BYTE_ORDER == __LITTLE_ENDIAN /* __FLOAT_WORD_ORER == __BIG_ENDIAN */ + { + unsigned char *ci, *co; + ci = (unsigned char *) &dVal; + co = (unsigned char *) output; + co[0] = ci[3]; + co[1] = ci[2]; + co[2] = ci[1]; + co[3] = ci[0]; + co[4] = ci[7]; + co[5] = ci[6]; + co[6] = ci[5]; + co[7] = ci[4]; + } +#else /* __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN */ + { + unsigned char *ci, *co; + ci = (unsigned char *) &dVal; + co = (unsigned char *) output; + co[0] = ci[4]; + co[1] = ci[5]; + co[2] = ci[6]; + co[3] = ci[7]; + co[4] = ci[0]; + co[5] = ci[1]; + co[6] = ci[2]; + co[7] = ci[3]; + } +#endif +#endif return 9; } @@ -136,55 +225,55 @@ AMF_EncodeBoolean(char *output, bool bVal) } void -AMFProp_GetName(AMFObjectProperty *prop, AVal *name) +AMFProp_GetName(AMFObjectProperty * prop, AVal * name) { *name = prop->p_name; } void -AMFProp_SetName(AMFObjectProperty *prop, AVal *name) +AMFProp_SetName(AMFObjectProperty * prop, AVal * name) { prop->p_name = *name; } AMFDataType -AMFProp_GetType(AMFObjectProperty *prop) +AMFProp_GetType(AMFObjectProperty * prop) { return prop->p_type; } double -AMFProp_GetNumber(AMFObjectProperty *prop) +AMFProp_GetNumber(AMFObjectProperty * prop) { return prop->p_vu.p_number; } int -AMFProp_GetBoolean(AMFObjectProperty *prop) +AMFProp_GetBoolean(AMFObjectProperty * prop) { return prop->p_vu.p_number != 0; } void -AMFProp_GetString(AMFObjectProperty *prop, AVal *str) +AMFProp_GetString(AMFObjectProperty * prop, AVal * str) { *str = prop->p_vu.p_aval; } void -AMFProp_GetObject(AMFObjectProperty *prop, AMFObject *obj) +AMFProp_GetObject(AMFObjectProperty * prop, AMFObject * obj) { *obj = prop->p_vu.p_object; } int -AMFProp_IsValid(AMFObjectProperty *prop) +AMFProp_IsValid(AMFObjectProperty * prop) { return prop->p_type != AMF_INVALID; } int -AMFProp_Encode(AMFObjectProperty *prop, char *pBuffer, int nSize) +AMFProp_Encode(AMFObjectProperty * prop, char *pBuffer, int nSize) { int nBytes = 0; @@ -252,7 +341,7 @@ AMFProp_Encode(AMFObjectProperty *prop, char *pBuffer, int nSize) #define AMF3_INTEGER_MIN -268435456 int -AMF3ReadInteger(const char *data, int32_t *valp) +AMF3ReadInteger(const char *data, int32_t * valp) { int i = 0; int32_t val = 0; @@ -292,7 +381,7 @@ AMF3ReadInteger(const char *data, int32_t *valp) } int -AMF3ReadString(const char *data, AVal *str) +AMF3ReadString(const char *data, AVal * str) { assert(str != 0); @@ -321,7 +410,7 @@ AMF3ReadString(const char *data, AVal *str) } int -AMF3Prop_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, +AMF3Prop_Decode(AMFObjectProperty * prop, const char *pBuffer, int nSize, int bDecodeName) { int nOriginalSize = nSize; @@ -380,7 +469,7 @@ AMF3Prop_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, case AMF3_DOUBLE: if (nSize < 8) return -1; - prop->p_vu.p_number = ReadNumber(pBuffer); + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); prop->p_type = AMF_NUMBER; nSize -= 8; break; @@ -411,7 +500,7 @@ AMF3Prop_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, if (nSize < 8) return -1; - prop->p_vu.p_number = ReadNumber(pBuffer); + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); nSize -= 8; prop->p_type = AMF_NUMBER; } @@ -438,7 +527,7 @@ AMF3Prop_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, } int -AMFProp_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, +AMFProp_Decode(AMFObjectProperty * prop, const char *pBuffer, int nSize, int bDecodeName) { int nOriginalSize = nSize; @@ -489,7 +578,7 @@ AMFProp_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, case AMF_NUMBER: if (nSize < 8) return -1; - prop->p_vu.p_number = ReadNumber(pBuffer); + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); nSize -= 8; break; case AMF_BOOLEAN: @@ -570,7 +659,7 @@ AMFProp_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, if (nSize < 10) return -1; - prop->p_vu.p_number = ReadNumber(pBuffer); + prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); prop->p_UTCoffset = AMF_DecodeInt16(pBuffer + 8); nSize -= 10; @@ -623,7 +712,7 @@ AMFProp_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, } void -AMFProp_Dump(AMFObjectProperty *prop) +AMFProp_Dump(AMFObjectProperty * prop) { char strRes[256]; char str[256]; @@ -641,12 +730,15 @@ AMFProp_Dump(AMFObjectProperty *prop) return; } - if (prop->p_name.av_len) { - name = prop->p_name; - } else { - name.av_val = "no-name."; - name.av_len = sizeof("no-name.")-1; - } + if (prop->p_name.av_len) + { + name = prop->p_name; + } + else + { + name.av_val = "no-name."; + name.av_len = sizeof("no-name.") - 1; + } if (name.av_len > 25) name.av_len = 25; @@ -669,7 +761,8 @@ AMFProp_Dump(AMFObjectProperty *prop) prop->p_vu.p_number != 0.0 ? "TRUE" : "FALSE"); break; case AMF_STRING: - snprintf(str, 255, "STRING:\t%.*s", prop->p_vu.p_aval.av_len, prop->p_vu.p_aval.av_val); + snprintf(str, 255, "STRING:\t%.*s", prop->p_vu.p_aval.av_len, + prop->p_vu.p_aval.av_val); break; case AMF_DATE: snprintf(str, 255, "DATE:\ttimestamp: %.2f, UTC offset: %d", @@ -683,7 +776,7 @@ AMFProp_Dump(AMFObjectProperty *prop) } void -AMFProp_Reset(AMFObjectProperty *prop) +AMFProp_Reset(AMFObjectProperty * prop) { if (prop->p_type == AMF_OBJECT) AMF_Reset(&prop->p_vu.p_object); @@ -698,7 +791,7 @@ AMFProp_Reset(AMFObjectProperty *prop) /* AMFObject */ int -AMF_Encode(AMFObject *obj, char *pBuffer, int nSize) +AMF_Encode(AMFObject * obj, char *pBuffer, int nSize) { int nOriginalSize = nSize; int i; @@ -733,7 +826,7 @@ AMF_Encode(AMFObject *obj, char *pBuffer, int nSize) } int -AMF_DecodeArray(AMFObject *obj, const char *pBuffer, int nSize, +AMF_DecodeArray(AMFObject * obj, const char *pBuffer, int nSize, int nArrayLen, bool bDecodeName) { int nOriginalSize = nSize; @@ -763,7 +856,7 @@ AMF_DecodeArray(AMFObject *obj, const char *pBuffer, int nSize, } int -AMF3_Decode(AMFObject *obj, const char *pBuffer, int nSize, bool bAMFData) +AMF3_Decode(AMFObject * obj, const char *pBuffer, int nSize, bool bAMFData) { int nOriginalSize = nSize; int32_t ref; @@ -899,7 +992,7 @@ AMF3_Decode(AMFObject *obj, const char *pBuffer, int nSize, bool bAMFData) } int -AMF_Decode(AMFObject *obj, const char *pBuffer, int nSize, bool bDecodeName) +AMF_Decode(AMFObject * obj, const char *pBuffer, int nSize, bool bDecodeName) { int nOriginalSize = nSize; bool bError = false; /* if there is an error while decoding - try to at least find the end mark AMF_OBJECT_END */ @@ -945,7 +1038,7 @@ AMF_Decode(AMFObject *obj, const char *pBuffer, int nSize, bool bDecodeName) } void -AMF_AddProp(AMFObject *obj, const AMFObjectProperty *prop) +AMF_AddProp(AMFObject * obj, const AMFObjectProperty * prop) { if (!(obj->o_num & 0x0f)) obj->o_props = @@ -954,13 +1047,13 @@ AMF_AddProp(AMFObject *obj, const AMFObjectProperty *prop) } int -AMF_CountProp(AMFObject *obj) +AMF_CountProp(AMFObject * obj) { return obj->o_num; } AMFObjectProperty * -AMF_GetProp(AMFObject *obj, const AVal *name, int nIndex) +AMF_GetProp(AMFObject * obj, const AVal * name, int nIndex) { if (nIndex >= 0) { @@ -981,7 +1074,7 @@ AMF_GetProp(AMFObject *obj, const AVal *name, int nIndex) } void -AMF_Dump(AMFObject *obj) +AMF_Dump(AMFObject * obj) { int n; for (n = 0; n < obj->o_num; n++) @@ -991,7 +1084,7 @@ AMF_Dump(AMFObject *obj) } void -AMF_Reset(AMFObject *obj) +AMF_Reset(AMFObject * obj) { int n; for (n = 0; n < obj->o_num; n++) @@ -1007,7 +1100,7 @@ AMF_Reset(AMFObject *obj) /* AMF3ClassDefinition */ void -AMF3CD_AddProp(AMF3ClassDef *cd, AVal *prop) +AMF3CD_AddProp(AMF3ClassDef * cd, AVal * prop) { if (!(cd->cd_num & 0x0f)) cd->cd_props = realloc(cd->cd_props, (cd->cd_num + 16) * sizeof(AVal)); @@ -1015,9 +1108,9 @@ AMF3CD_AddProp(AMF3ClassDef *cd, AVal *prop) } AVal * -AMF3CD_GetProp(AMF3ClassDef *cd, int nIndex) +AMF3CD_GetProp(AMF3ClassDef * cd, int nIndex) { if (nIndex >= cd->cd_num) - return (AVal *)&AV_empty; + return (AVal *) & AV_empty; return &cd->cd_props[nIndex]; } diff --git a/amf.h b/amf.h index 1c777f6..fb68358 100644 --- a/amf.h +++ b/amf.h @@ -80,56 +80,58 @@ extern "C" int16_t p_UTCoffset; } AMFObjectProperty; - int AMF_EncodeString (char *output, const AVal * str); - int AMF_EncodeNumber (char *output, double dVal); - int AMF_EncodeInt16 (char *output, short nVal); - int AMF_EncodeInt24 (char *output, int nVal); - int AMF_EncodeInt32 (char *output, int nVal); - int AMF_EncodeBoolean (char *output, bool bVal); - - unsigned short AMF_DecodeInt16 (const char *data); - unsigned int AMF_DecodeInt24 (const char *data); - unsigned int AMF_DecodeInt32 (const char *data); - void AMF_DecodeString (const char *data, AVal * str); - bool AMF_DecodeBoolean (const char *data); - - int AMF_Encode (AMFObject * obj, char *pBuffer, int nSize); - int AMF_Decode (AMFObject * obj, const char *pBuffer, int nSize, + int AMF_EncodeString(char *output, const AVal * str); + int AMF_EncodeNumber(char *output, double dVal); + int AMF_EncodeInt16(char *output, short nVal); + int AMF_EncodeInt24(char *output, int nVal); + int AMF_EncodeInt32(char *output, int nVal); + int AMF_EncodeBoolean(char *output, bool bVal); + + unsigned short AMF_DecodeInt16(const char *data); + unsigned int AMF_DecodeInt24(const char *data); + unsigned int AMF_DecodeInt32(const char *data); + void AMF_DecodeString(const char *data, AVal * str); + bool AMF_DecodeBoolean(const char *data); + double AMF_DecodeNumber(const char *data); + + int AMF_Encode(AMFObject * obj, char *pBuffer, int nSize); + int AMF_Decode(AMFObject * obj, const char *pBuffer, int nSize, + bool bDecodeName); + int AMF_DecodeArray(AMFObject * obj, const char *pBuffer, int nSize, + int nArrayLen, bool bDecodeName); + int AMF3_Decode(AMFObject * obj, const char *pBuffer, int nSize, bool bDecodeName); - int AMF_DecodeArray (AMFObject * obj, const char *pBuffer, int nSize, - int nArrayLen, bool bDecodeName); - int AMF3_Decode (AMFObject * obj, const char *pBuffer, int nSize, - bool bDecodeName); - void AMF_Dump (AMFObject * obj); - void AMF_Reset (AMFObject * obj); - - void AMF_AddProp (AMFObject * obj, const AMFObjectProperty * prop); - int AMF_CountProp (AMFObject * obj); - AMFObjectProperty *AMF_GetProp (AMFObject * obj, const AVal *name, int nIndex); - - AMFDataType AMFProp_GetType (AMFObjectProperty * prop); - void AMFProp_SetNumber (AMFObjectProperty * prop, double dval); - void AMFProp_SetBoolean (AMFObjectProperty * prop, bool bflag); - void AMFProp_SetString (AMFObjectProperty * prop, AVal * str); - void AMFProp_SetObject (AMFObjectProperty * prop, AMFObject * obj); - - void AMFProp_GetName (AMFObjectProperty * prop, AVal * name); - void AMFProp_SetName (AMFObjectProperty * prop, AVal * name); - double AMFProp_GetNumber (AMFObjectProperty * prop); - bool AMFProp_GetBoolean (AMFObjectProperty * prop); - void AMFProp_GetString (AMFObjectProperty * prop, AVal * str); - void AMFProp_GetObject (AMFObjectProperty * prop, AMFObject * obj); - - bool AMFProp_IsValid (AMFObjectProperty * prop); - - int AMFProp_Encode (AMFObjectProperty * prop, char *pBuffer, int nSize); - int AMF3Prop_Decode (AMFObjectProperty * prop, const char *pBuffer, - int nSize, bool bDecodeName); - int AMFProp_Decode (AMFObjectProperty * prop, const char *pBuffer, + void AMF_Dump(AMFObject * obj); + void AMF_Reset(AMFObject * obj); + + void AMF_AddProp(AMFObject * obj, const AMFObjectProperty * prop); + int AMF_CountProp(AMFObject * obj); + AMFObjectProperty *AMF_GetProp(AMFObject * obj, const AVal * name, + int nIndex); + + AMFDataType AMFProp_GetType(AMFObjectProperty * prop); + void AMFProp_SetNumber(AMFObjectProperty * prop, double dval); + void AMFProp_SetBoolean(AMFObjectProperty * prop, bool bflag); + void AMFProp_SetString(AMFObjectProperty * prop, AVal * str); + void AMFProp_SetObject(AMFObjectProperty * prop, AMFObject * obj); + + void AMFProp_GetName(AMFObjectProperty * prop, AVal * name); + void AMFProp_SetName(AMFObjectProperty * prop, AVal * name); + double AMFProp_GetNumber(AMFObjectProperty * prop); + bool AMFProp_GetBoolean(AMFObjectProperty * prop); + void AMFProp_GetString(AMFObjectProperty * prop, AVal * str); + void AMFProp_GetObject(AMFObjectProperty * prop, AMFObject * obj); + + bool AMFProp_IsValid(AMFObjectProperty * prop); + + int AMFProp_Encode(AMFObjectProperty * prop, char *pBuffer, int nSize); + int AMF3Prop_Decode(AMFObjectProperty * prop, const char *pBuffer, int nSize, bool bDecodeName); + int AMFProp_Decode(AMFObjectProperty * prop, const char *pBuffer, + int nSize, bool bDecodeName); - void AMFProp_Dump (AMFObjectProperty * prop); - void AMFProp_Reset (AMFObjectProperty * prop); + void AMFProp_Dump(AMFObjectProperty * prop); + void AMFProp_Reset(AMFObjectProperty * prop); typedef struct AMF3ClassDef { @@ -140,8 +142,8 @@ extern "C" AVal *cd_props; } AMF3ClassDef; - void AMF3CD_AddProp (AMF3ClassDef * cd, AVal * prop); - AVal *AMF3CD_GetProp (AMF3ClassDef * cd, int idx); + void AMF3CD_AddProp(AMF3ClassDef * cd, AVal * prop); + AVal *AMF3CD_GetProp(AMF3ClassDef * cd, int idx); #ifdef __cplusplus } diff --git a/bytes.c b/bytes.c deleted file mode 100644 index 1080aac..0000000 --- a/bytes.c +++ /dev/null @@ -1,147 +0,0 @@ -/* RTMPDump - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009 Howard Chu - * - * This Program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This Program 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 RTMPDump; see the file COPYING. If not, write to - * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -#include <string.h> -#include <sys/types.h> -#include "bytes.h" - -// write dVal as 64bit little endian double -void WriteNumber(char *data, double dVal) -{ -#if __FLOAT_WORD_ORDER == __BYTE_ORDER -#if __BYTE_ORDER == __BIG_ENDIAN - memcpy(data, &dVal, 8); -#elif __BYTE_ORDER == __LITTLE_ENDIAN - unsigned char *ci, *co; - ci = (unsigned char *)&dVal; - co = (unsigned char *)data; - co[0] = ci[7]; - co[1] = ci[6]; - co[2] = ci[5]; - co[3] = ci[4]; - co[4] = ci[3]; - co[5] = ci[2]; - co[6] = ci[1]; - co[7] = ci[0]; -#endif -#else -#if __BYTE_ORDER == __LITTLE_ENDIAN // __FLOAT_WORD_ORER == __BIG_ENDIAN - unsigned char *ci, *co; - ci = (unsigned char *)&dVal; - co = (unsigned char *)data; - co[0] = ci[3]; - co[1] = ci[2]; - co[2] = ci[1]; - co[3] = ci[0]; - co[4] = ci[7]; - co[5] = ci[6]; - co[6] = ci[5]; - co[7] = ci[4]; -#else // __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN - unsigned char *ci, *co; - ci = (unsigned char *)&dVal; - co = (unsigned char *)data; - co[0] = ci[4]; - co[1] = ci[5]; - co[2] = ci[6]; - co[3] = ci[7]; - co[4] = ci[0]; - co[5] = ci[1]; - co[6] = ci[2]; - co[7] = ci[3]; -#endif -#endif -} - -// reads a little endian 64bit double from data -double ReadNumber(const char *data) -{ -#if __FLOAT_WORD_ORDER == __BYTE_ORDER -#if __BYTE_ORDER == __BIG_ENDIAN - double dVal; - memcpy(&dVal, data, 8); - return dVal; -#elif __BYTE_ORDER == __LITTLE_ENDIAN -#if 1 - double dVal; - unsigned char *ci, *co; - ci = (unsigned char *)data; - co = (unsigned char *)&dVal; - co[0] = ci[7]; - co[1] = ci[6]; - co[2] = ci[5]; - co[3] = ci[4]; - co[4] = ci[3]; - co[5] = ci[2]; - co[6] = ci[1]; - co[7] = ci[0]; - return dVal; -#else - uint64_t in = *((uint64_t*)data); - uint64_t res = __bswap_64(in); - return *((double *)&res); -#endif -#endif -#else -#if __BYTE_ORDER == __LITTLE_ENDIAN // __FLOAT_WORD_ORER == __BIG_ENDIAN - uint32_t in1 = *((uint32_t*)data); - uint32_t in2 = *((uint32_t*)(data+4)); - - in1 = __bswap_32(in1); - in2 = __bswap_32(in2); - - uint64_t res = ((uint64_t)in2<<32) | (uint64_t)in1; - return *((double *)&res); -#else // __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN - uint32_t in1 = *((uint32_t*)data); - uint32_t in2 = *((uint32_t*)(data+4)); - - uint64_t res = ((uint64_t)in1<<32) | (uint64_t)in2; - return *((double *)&res); -#endif -#endif -} - -// read little-endian 32bit integer -int ReadInt32LE(const char *data) -{ - int val; - memcpy(&val, data, sizeof(int)); - -#if __BYTE_ORDER == __BIG_ENDIAN - val = __bswap_32(val); -#endif - - return val; -} - -// write little-endian 32bit integer -int EncodeInt32LE(char *output, int nVal) -{ - -#if __BYTE_ORDER == __BIG_ENDIAN - nVal = __bswap_32(nVal); -#endif - - memcpy(output, &nVal, sizeof(int)); - return sizeof(int); -} - diff --git a/bytes.h b/bytes.h index 63c7511..84c91f2 100644 --- a/bytes.h +++ b/bytes.h @@ -3,10 +3,6 @@ #include <stdint.h> -#ifdef __cplusplus -extern "C" { -#endif - #ifdef WIN32 // Windows is little endian only #define __LITTLE_ENDIAN 1234 @@ -15,15 +11,6 @@ extern "C" { #define __FLOAT_WORD_ORDER __BYTE_ORDER typedef unsigned char uint8_t; -/*typedef signed char int8_t; -typedef signed short int16_t; -typedef signed long int int32_t; -typedef signed long long int int64_t; -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned long int uint32_t; -typedef unsigned long long int uint64_t; -*/ #elif (defined(__FreeBSD__) && __FreeBSD_version >= 470000) || defined(__OpenBSD__) || defined(__NetBSD__) // *BSD #include <sys/endian.h> @@ -52,18 +39,6 @@ typedef unsigned long long int uint64_t; (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) #endif -#ifndef __bswap_64 -#define __bswap_64(x) \ - ((((x) & 0xff00000000000000ull) >> 56) \ - | (((x) & 0x00ff000000000000ull) >> 40) \ - | (((x) & 0x0000ff0000000000ull) >> 24) \ - | (((x) & 0x000000ff00000000ull) >> 8) \ - | (((x) & 0x00000000ff000000ull) << 8) \ - | (((x) & 0x0000000000ff0000ull) << 24) \ - | (((x) & 0x000000000000ff00ull) << 40) \ - | (((x) & 0x00000000000000ffull) << 56)) -#endif - // define default endianness #ifndef __LITTLE_ENDIAN #define __LITTLE_ENDIAN 1234 @@ -96,15 +71,5 @@ typedef unsigned long long int uint64_t; #error "Unknown/unsupported byte order!" #endif -void WriteNumber(char *data, double dVal); -double ReadNumber(const char *data); - -int ReadInt32LE(const char *data); -int EncodeInt32LE(char *output, int nVal); - -#ifdef __cplusplus -} -#endif - #endif diff --git a/rtmp.c b/rtmp.c index aba38c8..aca9680 100644 --- a/rtmp.c +++ b/rtmp.c @@ -41,7 +41,6 @@ #include "rtmp.h" #include "log.h" -#include "bytes.h" #define RTMP_SIG_SIZE 1536 #define RTMP_LARGE_HEADER_SIZE 12 @@ -1753,6 +1752,29 @@ HandleClientBW(RTMP * r, const RTMPPacket * packet) r->m_nClientBW2); } +static int +DecodeInt32LE(const char *data) +{ + unsigned char *c = (unsigned char *)data; + unsigned int val; + + val = (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0]; + return val; +} + +static int +EncodeInt32LE(char *output, int nVal) +{ + output[0] = nVal; + nVal >>= 8; + output[1] = nVal; + nVal >>= 8; + output[2] = nVal; + nVal >>= 8; + output[3] = nVal; + return 4; +} + static bool ReadPacket(RTMP * r, RTMPPacket * packet) { @@ -1830,7 +1852,7 @@ ReadPacket(RTMP * r, RTMPPacket * packet) packet->m_packetType = header[6]; if (nSize == 11) - packet->m_nInfoField2 = ReadInt32LE(header + 7); + packet->m_nInfoField2 = DecodeInt32LE(header + 7); } } } -- 2.40.0