Skip to content

Commit

Permalink
Replace hex to string to match MIT license. 3.0.33
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jul 22, 2018
1 parent 84f8198 commit 41c6e83
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <srs_auto_headers.hpp>
Expand Down
55 changes: 33 additions & 22 deletions trunk/src/kernel/srs_kernel_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,33 +948,44 @@ int av_toupper(int c)
}
return c;
}

// 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';
}
if ('a' <= c && c <= 'f') {
return c - 'a' + 10;
}
if ('A' <= c && c <= 'F') {
return c - 'A' + 10;
}

return -1;
}

int ff_hex_to_data(uint8_t* data, const char* p)
int srs_hex_to_data(uint8_t* data, const char* p, int size)
{
int c, len, v;
if (size <= 0 || (size%2) == 1) {
return -1;
}

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;
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 len;

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)
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/kernel/srs_kernel_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions trunk/src/protocol/srs_rtsp_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
23 changes: 23 additions & 0 deletions trunk/src/utest/srs_utest_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 comment on commit 41c6e83

@winlinvip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.