diff --git a/README.md b/README.md index f2d2d52c9..a791194dd 100755 --- a/README.md +++ b/README.md @@ -184,6 +184,7 @@ Please select according to languages: ### V3 changes +* v3.0, 2018-07-22, Replace hex to string to match MIT license. 3.0.33 * v3.0, 2018-07-22, Replace base64 to match MIT license. 3.0.32 * v3.0, 2018-07-22, Replace crc32 IEEE and MPEG by pycrc to match MIT license. 3.0.31 * v3.0, 2018-07-21, Replace crc32 IEEE by golang to match MIT license. 3.0.30 diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index b62795c9f..8e76a8d70 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -27,7 +27,7 @@ // current release version #define VERSION_MAJOR 3 #define VERSION_MINOR 0 -#define VERSION_REVISION 32 +#define VERSION_REVISION 33 // generated by configure, only macros. #include diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index a247bf27d..eba746293 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -948,33 +948,44 @@ int av_toupper(int c) } return c; } - -int ff_hex_to_data(uint8_t* data, const char* p) -{ - int c, len, v; - len = 0; - v = 1; - for (;;) { - p += strspn(p, SPACE_CHARS); - if (*p == '\0') - break; - c = av_toupper((unsigned char) *p++); - if (c >= '0' && c <= '9') - c = c - '0'; - else if (c >= 'A' && c <= 'F') - c = c - 'A' + 10; - else - break; - v = (v << 4) | c; - if (v & 0x100) { - if (data) - data[len] = v; - len++; - v = 1; - } +// fromHexChar converts a hex character into its value and a success flag. +uint8_t srs_from_hex_char(uint8_t c) +{ + if ('0' <= c && c <= '9') { + return c - '0'; } - return len; + if ('a' <= c && c <= 'f') { + return c - 'a' + 10; + } + if ('A' <= c && c <= 'F') { + return c - 'A' + 10; + } + + return -1; +} + +int srs_hex_to_data(uint8_t* data, const char* p, int size) +{ + if (size <= 0 || (size%2) == 1) { + return -1; + } + + for (int i = 0; i < size / 2; i++) { + uint8_t a = srs_from_hex_char(p[i*2]); + if (a == (uint8_t)-1) { + return -1; + } + + uint8_t b = srs_from_hex_char(p[i*2 + 1]); + if (b == (uint8_t)-1) { + return -1; + } + + data[i] = (a << 4) | b; + } + + return size / 2; } int srs_chunk_header_c0(int perfer_cid, uint32_t timestamp, int32_t payload_length, int8_t message_type, int32_t stream_id, char* cache, int nb_cache) diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp index 34b63eaca..19677d776 100644 --- a/trunk/src/kernel/srs_kernel_utility.hpp +++ b/trunk/src/kernel/srs_kernel_utility.hpp @@ -161,7 +161,7 @@ extern srs_error_t srs_av_base64_decode(std::string cipher, std::string& plainte * for example, p=config='139056E5A0' * output hex to data={0x13, 0x90, 0x56, 0xe5, 0xa0} */ -extern int ff_hex_to_data(uint8_t* data, const char* p); +extern int srs_hex_to_data(uint8_t* data, const char* p, int size); /** * generate the c0 chunk header for msg. diff --git a/trunk/src/protocol/srs_rtsp_stack.cpp b/trunk/src/protocol/srs_rtsp_stack.cpp index 55a306c7f..ac0083d82 100644 --- a/trunk/src/protocol/srs_rtsp_stack.cpp +++ b/trunk/src/protocol/srs_rtsp_stack.cpp @@ -537,8 +537,12 @@ srs_error_t SrsRtspSdp::parse_fmtp_attribute(string attr) char* tmp_sh = new char[item_value.length()]; SrsAutoFreeA(char, tmp_sh); - int nb_tmp_sh = ff_hex_to_data((uint8_t*)tmp_sh, item_value.c_str()); - srs_assert(nb_tmp_sh > 0); + + int nb_tmp_sh = srs_hex_to_data((uint8_t*)tmp_sh, item_value.c_str(), item_value.length()); + if (nb_tmp_sh <= 0) { + return srs_error_new(ERROR_RTSP_AUDIO_CONFIG, "audio config"); + } + audio_sh.append(tmp_sh, nb_tmp_sh); } } diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index 4b6364e1f..e2e66f794 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -1684,5 +1684,28 @@ VOID TEST(KernelUtility, Base64Decode) EXPECT_TRUE(expect == plaintext); } +VOID TEST(KernelUtility, StringToHex) +{ + if (true) { + uint8_t h[16]; + EXPECT_EQ(-1, srs_hex_to_data(h, NULL, 0)); + EXPECT_EQ(-1, srs_hex_to_data(h, "0", 1)); + EXPECT_EQ(-1, srs_hex_to_data(h, "0g", 2)); + } + + if (true) { + string s = "139056E5A0"; + uint8_t h[16]; + + int n = srs_hex_to_data(h, s.data(), s.length()); + EXPECT_EQ(n, 5); + EXPECT_EQ(0x13, h[0]); + EXPECT_EQ(0x90, h[1]); + EXPECT_EQ(0x56, h[2]); + EXPECT_EQ(0xe5, h[3]); + EXPECT_EQ(0xa0, h[4]); + } +} + #endif