Remove hex_encode functions with raw buffer output from the header file
Moved into the anonymous namespace in string_encode.cc.
Bug: webrtc:6424
Change-Id: I5e8ea0f02c94d6de82ca4f875d16862eb2db3d2b
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/138073
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28034}
diff --git a/rtc_base/string_encode.cc b/rtc_base/string_encode.cc
index 85c7a3f..7b2eda7 100644
--- a/rtc_base/string_encode.cc
+++ b/rtc_base/string_encode.cc
@@ -22,13 +22,16 @@
// String Encoding Utilities
/////////////////////////////////////////////////////////////////////////////
-static const char HEX[] = "0123456789abcdef";
+namespace {
+const char HEX[] = "0123456789abcdef";
+// Convert an unsigned value from 0 to 15 to the hex character equivalent...
char hex_encode(unsigned char val) {
RTC_DCHECK_LT(val, 16);
return (val < 16) ? HEX[val] : '!';
}
+// ...and vice-versa.
bool hex_decode(char ch, unsigned char* val) {
if ((ch >= '0') && (ch <= '9')) {
*val = ch - '0';
@@ -42,13 +45,9 @@
return true;
}
-size_t hex_encode(char* buffer,
- size_t buflen,
- const char* csource,
- size_t srclen) {
- return hex_encode_with_delimiter(buffer, buflen, csource, srclen, 0);
-}
-
+// hex_encode, but separate each byte representation with a delimiter.
+// |delimiter| == 0 means no delimiter
+// If the buffer is too short, we return 0
size_t hex_encode_with_delimiter(char* buffer,
size_t buflen,
const char* csource,
@@ -84,6 +83,8 @@
return bufpos;
}
+} // namespace
+
std::string hex_encode(const std::string& str) {
return hex_encode(str.c_str(), str.size());
}
diff --git a/rtc_base/string_encode.h b/rtc_base/string_encode.h
index d0f323d..2ea09e2 100644
--- a/rtc_base/string_encode.h
+++ b/rtc_base/string_encode.h
@@ -26,27 +26,6 @@
// String Encoding Utilities
//////////////////////////////////////////////////////////////////////
-// Convert an unsigned value from 0 to 15 to the hex character equivalent...
-char hex_encode(unsigned char val);
-// ...and vice-versa.
-bool hex_decode(char ch, unsigned char* val);
-
-// hex_encode shows the hex representation of binary data in ascii.
-size_t hex_encode(char* buffer,
- size_t buflen,
- const char* source,
- size_t srclen);
-
-// hex_encode, but separate each byte representation with a delimiter.
-// |delimiter| == 0 means no delimiter
-// If the buffer is too short, we return 0
-size_t hex_encode_with_delimiter(char* buffer,
- size_t buflen,
- const char* source,
- size_t srclen,
- char delimiter);
-
-// Helper functions for hex_encode.
std::string hex_encode(const std::string& str);
std::string hex_encode(const char* source, size_t srclen);
std::string hex_encode_with_delimiter(const char* source,
diff --git a/rtc_base/string_encode_unittest.cc b/rtc_base/string_encode_unittest.cc
index d4fe2c5..4854299 100644
--- a/rtc_base/string_encode_unittest.cc
+++ b/rtc_base/string_encode_unittest.cc
@@ -19,7 +19,7 @@
class HexEncodeTest : public ::testing::Test {
public:
- HexEncodeTest() : enc_res_(0), dec_res_(0) {
+ HexEncodeTest() : dec_res_(0) {
for (size_t i = 0; i < sizeof(data_); ++i) {
data_[i] = (i + 128) & 0xff;
}
@@ -27,111 +27,74 @@
}
char data_[10];
- char encoded_[31];
char decoded_[11];
- size_t enc_res_;
size_t dec_res_;
};
// Test that we can convert to/from hex with no delimiter.
TEST_F(HexEncodeTest, TestWithNoDelimiter) {
- enc_res_ = hex_encode(encoded_, sizeof(encoded_), data_, sizeof(data_));
- ASSERT_EQ(sizeof(data_) * 2, enc_res_);
- ASSERT_STREQ("80818283848586878889", encoded_);
- dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
+ std::string encoded = hex_encode(data_, sizeof(data_));
+ EXPECT_EQ("80818283848586878889", encoded);
+ dec_res_ =
+ hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
ASSERT_EQ(sizeof(data_), dec_res_);
ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
}
// Test that we can convert to/from hex with a colon delimiter.
TEST_F(HexEncodeTest, TestWithDelimiter) {
- enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), data_,
- sizeof(data_), ':');
- ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
- ASSERT_STREQ("80:81:82:83:84:85:86:87:88:89", encoded_);
- dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
- enc_res_, ':');
+ std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
+ EXPECT_EQ("80:81:82:83:84:85:86:87:88:89", encoded);
+ dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
+ encoded.data(), encoded.size(), ':');
ASSERT_EQ(sizeof(data_), dec_res_);
ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
}
// Test that encoding with one delimiter and decoding with another fails.
TEST_F(HexEncodeTest, TestWithWrongDelimiter) {
- enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), data_,
- sizeof(data_), ':');
- ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
- dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
- enc_res_, '/');
+ std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
+ dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
+ encoded.data(), encoded.size(), '/');
ASSERT_EQ(0U, dec_res_);
}
// Test that encoding without a delimiter and decoding with one fails.
TEST_F(HexEncodeTest, TestExpectedDelimiter) {
- enc_res_ = hex_encode(encoded_, sizeof(encoded_), data_, sizeof(data_));
- ASSERT_EQ(sizeof(data_) * 2, enc_res_);
- dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
- enc_res_, ':');
+ std::string encoded = hex_encode(data_, sizeof(data_));
+ EXPECT_EQ(sizeof(data_) * 2, encoded.size());
+ dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
+ encoded.data(), encoded.size(), ':');
ASSERT_EQ(0U, dec_res_);
}
// Test that encoding with a delimiter and decoding without one fails.
TEST_F(HexEncodeTest, TestExpectedNoDelimiter) {
- enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), data_,
- sizeof(data_), ':');
- ASSERT_EQ(sizeof(data_) * 3 - 1, enc_res_);
- dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
+ std::string encoded = hex_encode_with_delimiter(data_, sizeof(data_), ':');
+ EXPECT_EQ(sizeof(data_) * 3 - 1, encoded.size());
+ dec_res_ =
+ hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
ASSERT_EQ(0U, dec_res_);
}
// Test that we handle a zero-length buffer with no delimiter.
TEST_F(HexEncodeTest, TestZeroLengthNoDelimiter) {
- enc_res_ = hex_encode(encoded_, sizeof(encoded_), "", 0);
- ASSERT_EQ(0U, enc_res_);
- dec_res_ = hex_decode(decoded_, sizeof(decoded_), encoded_, enc_res_);
+ std::string encoded = hex_encode("", 0);
+ EXPECT_TRUE(encoded.empty());
+ dec_res_ =
+ hex_decode(decoded_, sizeof(decoded_), encoded.data(), encoded.size());
ASSERT_EQ(0U, dec_res_);
}
// Test that we handle a zero-length buffer with a delimiter.
TEST_F(HexEncodeTest, TestZeroLengthWithDelimiter) {
- enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(encoded_), "", 0, ':');
- ASSERT_EQ(0U, enc_res_);
- dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), encoded_,
- enc_res_, ':');
+ std::string encoded = hex_encode_with_delimiter("", 0, ':');
+ EXPECT_TRUE(encoded.empty());
+ dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_),
+ encoded.data(), encoded.size(), ':');
ASSERT_EQ(0U, dec_res_);
}
-// Test the std::string variants that take no delimiter.
-TEST_F(HexEncodeTest, TestHelpersNoDelimiter) {
- std::string result = hex_encode(data_, sizeof(data_));
- ASSERT_EQ("80818283848586878889", result);
- dec_res_ = hex_decode(decoded_, sizeof(decoded_), result);
- ASSERT_EQ(sizeof(data_), dec_res_);
- ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
-}
-
-// Test the std::string variants that use a delimiter.
-TEST_F(HexEncodeTest, TestHelpersWithDelimiter) {
- std::string result = hex_encode_with_delimiter(data_, sizeof(data_), ':');
- ASSERT_EQ("80:81:82:83:84:85:86:87:88:89", result);
- dec_res_ = hex_decode_with_delimiter(decoded_, sizeof(decoded_), result, ':');
- ASSERT_EQ(sizeof(data_), dec_res_);
- ASSERT_EQ(0, memcmp(data_, decoded_, dec_res_));
-}
-
-// Test that encoding into a too-small output buffer (without delimiter) fails.
-TEST_F(HexEncodeTest, TestEncodeTooShort) {
- enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(data_) * 2, data_,
- sizeof(data_), 0);
- ASSERT_EQ(0U, enc_res_);
-}
-
-// Test that encoding into a too-small output buffer (with delimiter) fails.
-TEST_F(HexEncodeTest, TestEncodeWithDelimiterTooShort) {
- enc_res_ = hex_encode_with_delimiter(encoded_, sizeof(data_) * 3 - 1, data_,
- sizeof(data_), ':');
- ASSERT_EQ(0U, enc_res_);
-}
-
// Test that decoding into a too-small output buffer fails.
TEST_F(HexEncodeTest, TestDecodeTooShort) {
dec_res_ = hex_decode_with_delimiter(decoded_, 4, "0123456789", 10, 0);