Delete unused code in rtc_base/stringencode.cc

Bug: webrtc:6424
Change-Id: I3d72d2a80c502b3afc31af2456d220f48272b348
Reviewed-on: https://webrtc-review.googlesource.com/33006
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Taylor Brandstetter <deadbeef@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21290}
diff --git a/rtc_base/stringencode.cc b/rtc_base/stringencode.cc
index efd9843..d77245f 100644
--- a/rtc_base/stringencode.cc
+++ b/rtc_base/stringencode.cc
@@ -44,127 +44,6 @@
   return bufpos;
 }
 
-size_t unescape(char * buffer, size_t buflen,
-                const char * source, size_t srclen,
-                char escape) {
-  RTC_DCHECK(buffer);  // TODO(grunell): estimate output size
-  if (buflen <= 0)
-    return 0;
-
-  size_t srcpos = 0, bufpos = 0;
-  while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
-    char ch = source[srcpos++];
-    if ((ch == escape) && (srcpos < srclen)) {
-      ch = source[srcpos++];
-    }
-    buffer[bufpos++] = ch;
-  }
-  buffer[bufpos] = '\0';
-  return bufpos;
-}
-
-size_t encode(char * buffer, size_t buflen,
-              const char * source, size_t srclen,
-              const char * illegal, char escape) {
-  RTC_DCHECK(buffer);  // TODO(grunell): estimate output size
-  if (buflen <= 0)
-    return 0;
-
-  size_t srcpos = 0, bufpos = 0;
-  while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
-    char ch = source[srcpos++];
-    if ((ch != escape) && !::strchr(illegal, ch)) {
-      buffer[bufpos++] = ch;
-    } else if (bufpos + 3 >= buflen) {
-      break;
-    } else {
-      buffer[bufpos+0] = escape;
-      buffer[bufpos+1] = hex_encode((static_cast<unsigned char>(ch) >> 4) & 0xF);
-      buffer[bufpos+2] = hex_encode((static_cast<unsigned char>(ch)     ) & 0xF);
-      bufpos += 3;
-    }
-  }
-  buffer[bufpos] = '\0';
-  return bufpos;
-}
-
-size_t decode(char * buffer, size_t buflen,
-              const char * source, size_t srclen,
-              char escape) {
-  if (buflen <= 0)
-    return 0;
-
-  unsigned char h1, h2;
-  size_t srcpos = 0, bufpos = 0;
-  while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
-    char ch = source[srcpos++];
-    if ((ch == escape)
-        && (srcpos + 1 < srclen)
-        && hex_decode(source[srcpos], &h1)
-        && hex_decode(source[srcpos+1], &h2)) {
-      buffer[bufpos++] = (h1 << 4) | h2;
-      srcpos += 2;
-    } else {
-      buffer[bufpos++] = ch;
-    }
-  }
-  buffer[bufpos] = '\0';
-  return bufpos;
-}
-
-const char* unsafe_filename_characters() {
-  // It might be better to have a single specification which is the union of
-  // all operating systems, unless one system is overly restrictive.
-#if defined(WEBRTC_WIN)
-  return "\\/:*?\"<>|";
-#else  // !WEBRTC_WIN
-  // TODO(grunell): Should this never be reached?
-  RTC_NOTREACHED();
-  return "";
-#endif  // !WEBRTC_WIN
-}
-
-const unsigned char URL_UNSAFE  = 0x1; // 0-33 "#$%&+,/:;<=>?@[\]^`{|} 127
-const unsigned char XML_UNSAFE  = 0x2; // "&'<>
-const unsigned char HTML_UNSAFE = 0x2; // "&'<>
-
-//  ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 6 5 7 8 9 : ; < = > ?
-//@ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _
-//` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
-
-const unsigned char ASCII_CLASS[128] = {
-  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
-  1,0,3,1,1,1,3,2,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,3,1,3,1,
-  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,
-  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1,
-};
-
-size_t url_encode(char * buffer, size_t buflen,
-                  const char * source, size_t srclen) {
-  if (nullptr == buffer)
-    return srclen * 3 + 1;
-  if (buflen <= 0)
-    return 0;
-
-  size_t srcpos = 0, bufpos = 0;
-  while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
-    unsigned char ch = source[srcpos++];
-    if ((ch < 128) && (ASCII_CLASS[ch] & URL_UNSAFE)) {
-      if (bufpos + 3 >= buflen) {
-        break;
-      }
-      buffer[bufpos+0] = '%';
-      buffer[bufpos+1] = hex_encode((ch >> 4) & 0xF);
-      buffer[bufpos+2] = hex_encode((ch     ) & 0xF);
-      bufpos += 3;
-    } else {
-      buffer[bufpos++] = ch;
-    }
-  }
-  buffer[bufpos] = '\0';
-  return bufpos;
-}
-
 size_t url_decode(char * buffer, size_t buflen,
                   const char * source, size_t srclen) {
   if (nullptr == buffer)
@@ -254,159 +133,6 @@
   return 0;
 }
 
-size_t html_encode(char * buffer, size_t buflen,
-                   const char * source, size_t srclen) {
-  RTC_DCHECK(buffer);  // TODO(grunell): estimate output size
-  if (buflen <= 0)
-    return 0;
-
-  size_t srcpos = 0, bufpos = 0;
-  while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
-    unsigned char ch = source[srcpos];
-    if (ch < 128) {
-      srcpos += 1;
-      if (ASCII_CLASS[ch] & HTML_UNSAFE) {
-        const char * escseq = 0;
-        size_t esclen = 0;
-        switch (ch) {
-          case '<':  escseq = "&lt;";   esclen = 4; break;
-          case '>':  escseq = "&gt;";   esclen = 4; break;
-          case '\'': escseq = "&#39;";  esclen = 5; break;
-          case '\"': escseq = "&quot;"; esclen = 6; break;
-          case '&':  escseq = "&amp;";  esclen = 5; break;
-          default: RTC_NOTREACHED();
-        }
-        if (bufpos + esclen >= buflen) {
-          break;
-        }
-        memcpy(buffer + bufpos, escseq, esclen);
-        bufpos += esclen;
-      } else {
-        buffer[bufpos++] = ch;
-      }
-    } else {
-      // Largest value is 0x1FFFFF => &#2097151;  (10 characters)
-      const size_t kEscseqSize = 11;
-      char escseq[kEscseqSize];
-      unsigned long val;
-      if (size_t vallen = utf8_decode(&source[srcpos], srclen - srcpos, &val)) {
-        srcpos += vallen;
-      } else {
-        // Not a valid utf8 sequence, just use the raw character.
-        val = static_cast<unsigned char>(source[srcpos++]);
-      }
-      size_t esclen = sprintfn(escseq, kEscseqSize, "&#%lu;", val);
-      if (bufpos + esclen >= buflen) {
-        break;
-      }
-      memcpy(buffer + bufpos, escseq, esclen);
-      bufpos += esclen;
-    }
-  }
-  buffer[bufpos] = '\0';
-  return bufpos;
-}
-
-size_t html_decode(char * buffer, size_t buflen,
-                   const char * source, size_t srclen) {
-  RTC_DCHECK(buffer);  // TODO(grunell): estimate output size
-  return xml_decode(buffer, buflen, source, srclen);
-}
-
-size_t xml_encode(char * buffer, size_t buflen,
-                  const char * source, size_t srclen) {
-  RTC_DCHECK(buffer);  // TODO(grunell): estimate output size
-  if (buflen <= 0)
-    return 0;
-
-  size_t srcpos = 0, bufpos = 0;
-  while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
-    unsigned char ch = source[srcpos++];
-    if ((ch < 128) && (ASCII_CLASS[ch] & XML_UNSAFE)) {
-      const char * escseq = 0;
-      size_t esclen = 0;
-      switch (ch) {
-        case '<':  escseq = "&lt;";   esclen = 4; break;
-        case '>':  escseq = "&gt;";   esclen = 4; break;
-        case '\'': escseq = "&apos;"; esclen = 6; break;
-        case '\"': escseq = "&quot;"; esclen = 6; break;
-        case '&':  escseq = "&amp;";  esclen = 5; break;
-        default: RTC_NOTREACHED();
-      }
-      if (bufpos + esclen >= buflen) {
-        break;
-      }
-      memcpy(buffer + bufpos, escseq, esclen);
-      bufpos += esclen;
-    } else {
-      buffer[bufpos++] = ch;
-    }
-  }
-  buffer[bufpos] = '\0';
-  return bufpos;
-}
-
-size_t xml_decode(char * buffer, size_t buflen,
-                  const char * source, size_t srclen) {
-  RTC_DCHECK(buffer);  // TODO(grunell): estimate output size
-  if (buflen <= 0)
-    return 0;
-
-  size_t srcpos = 0, bufpos = 0;
-  while ((srcpos < srclen) && (bufpos + 1 < buflen)) {
-    unsigned char ch = source[srcpos++];
-    if (ch != '&') {
-      buffer[bufpos++] = ch;
-    } else if ((srcpos + 2 < srclen)
-               && (memcmp(source + srcpos, "lt;", 3) == 0)) {
-      buffer[bufpos++] = '<';
-      srcpos += 3;
-    } else if ((srcpos + 2 < srclen)
-               && (memcmp(source + srcpos, "gt;", 3) == 0)) {
-      buffer[bufpos++] = '>';
-      srcpos += 3;
-    } else if ((srcpos + 4 < srclen)
-               && (memcmp(source + srcpos, "apos;", 5) == 0)) {
-      buffer[bufpos++] = '\'';
-      srcpos += 5;
-    } else if ((srcpos + 4 < srclen)
-               && (memcmp(source + srcpos, "quot;", 5) == 0)) {
-      buffer[bufpos++] = '\"';
-      srcpos += 5;
-    } else if ((srcpos + 3 < srclen)
-               && (memcmp(source + srcpos, "amp;", 4) == 0)) {
-      buffer[bufpos++] = '&';
-      srcpos += 4;
-    } else if ((srcpos < srclen) && (source[srcpos] == '#')) {
-      int int_base = 10;
-      if ((srcpos + 1 < srclen) && (source[srcpos+1] == 'x')) {
-        int_base = 16;
-        srcpos += 1;
-      }
-      char * ptr;
-      // TODO(grunell): Fix hack (ptr may go past end of data)
-      unsigned long val = strtoul(source + srcpos + 1, &ptr, int_base);
-      if ((static_cast<size_t>(ptr - source) < srclen) && (*ptr == ';')) {
-        srcpos = ptr - source + 1;
-      } else {
-        // Not a valid escape sequence.
-        break;
-      }
-      if (size_t esclen = utf8_encode(buffer + bufpos, buflen - bufpos, val)) {
-        bufpos += esclen;
-      } else {
-        // Not enough room to encode the character, or illegal character
-        break;
-      }
-    } else {
-      // Unrecognized escape sequence.
-      break;
-    }
-  }
-  buffer[bufpos] = '\0';
-  return bufpos;
-}
-
 static const char HEX[] = "0123456789abcdef";
 
 char hex_encode(unsigned char val) {
@@ -682,37 +408,4 @@
   return fields->size();
 }
 
-char make_char_safe_for_filename(char c) {
-  if (c < 32)
-    return '_';
-
-  switch (c) {
-    case '<':
-    case '>':
-    case ':':
-    case '"':
-    case '/':
-    case '\\':
-    case '|':
-    case '*':
-    case '?':
-      return '_';
-
-    default:
-      return c;
-  }
-}
-
-/*
-void sprintf(std::string& value, size_t maxlen, const char * format, ...) {
-  char * buffer = STACK_ARRAY(char, maxlen + 1);
-  va_list args;
-  va_start(args, format);
-  value.assign(buffer, vsprintfn(buffer, maxlen + 1, format, args));
-  va_end(args);
-}
-*/
-
-/////////////////////////////////////////////////////////////////////////////
-
 }  // namespace rtc
diff --git a/rtc_base/stringencode.h b/rtc_base/stringencode.h
index d99dd0d..5d675a2 100644
--- a/rtc_base/stringencode.h
+++ b/rtc_base/stringencode.h
@@ -23,61 +23,18 @@
 // String Encoding Utilities
 //////////////////////////////////////////////////////////////////////
 
-// Convert an unsigned value to it's utf8 representation.  Returns the length
-// of the encoded string, or 0 if the encoding is longer than buflen - 1.
-size_t utf8_encode(char* buffer, size_t buflen, unsigned long value);
-// Decode the utf8 encoded value pointed to by source.  Returns the number of
-// bytes used by the encoding, or 0 if the encoding is invalid.
-size_t utf8_decode(const char* source, size_t srclen, unsigned long* value);
-
+// TODO(nisse): Used only in httpcommon.c. Delete when that file is deleted, or
+// possibly if the HttpComposeAttributes funtion can be deleted earlier.
 // Escaping prefixes illegal characters with the escape character.  Compact, but
 // illegal characters still appear in the string.
 size_t escape(char * buffer, size_t buflen,
               const char * source, size_t srclen,
               const char * illegal, char escape);
-// Note: in-place unescaping (buffer == source) is allowed.
-size_t unescape(char * buffer, size_t buflen,
-                const char * source, size_t srclen,
-                char escape);
 
-// Encoding replaces illegal characters with the escape character and 2 hex
-// chars, so it's a little less compact than escape, but completely removes
-// illegal characters.  note that hex digits should not be used as illegal
-// characters.
-size_t encode(char * buffer, size_t buflen,
-              const char * source, size_t srclen,
-              const char * illegal, char escape);
-// Note: in-place decoding (buffer == source) is allowed.
-size_t decode(char * buffer, size_t buflen,
-              const char * source, size_t srclen,
-              char escape);
-
-// Returns a list of characters that may be unsafe for use in the name of a
-// file, suitable for passing to the 'illegal' member of escape or encode.
-const char* unsafe_filename_characters();
-
-// url_encode is an encode operation with a predefined set of illegal characters
-// and escape character (for use in URLs, obviously).
-size_t url_encode(char * buffer, size_t buflen,
-                  const char * source, size_t srclen);
 // Note: in-place decoding (buffer == source) is allowed.
 size_t url_decode(char * buffer, size_t buflen,
                   const char * source, size_t srclen);
 
-// html_encode prevents data embedded in html from containing markup.
-size_t html_encode(char * buffer, size_t buflen,
-                   const char * source, size_t srclen);
-// Note: in-place decoding (buffer == source) is allowed.
-size_t html_decode(char * buffer, size_t buflen,
-                   const char * source, size_t srclen);
-
-// xml_encode makes data suitable for inside xml attributes and values.
-size_t xml_encode(char * buffer, size_t buflen,
-                  const char * source, size_t srclen);
-// Note: in-place decoding (buffer == source) is allowed.
-size_t xml_decode(char * buffer, size_t buflen,
-                  const char * source, size_t srclen);
-
 // Convert an unsigned value from 0 to 15 to the hex character equivalent...
 char hex_encode(unsigned char val);
 // ...and vice-versa.
@@ -129,9 +86,6 @@
 std::string s_transform(const std::string& source, Transform t);
 
 // Convenience wrappers.
-inline std::string s_url_encode(const std::string& source) {
-  return s_transform(source, url_encode);
-}
 inline std::string s_url_decode(const std::string& source) {
   return s_transform(source, url_decode);
 }
@@ -177,10 +131,6 @@
                     std::string* token,
                     std::string* rest);
 
-// Safe sprintf to std::string
-//void sprintf(std::string& value, size_t maxlen, const char * format, ...)
-//     PRINTF_FORMAT(3);
-
 // Convert arbitrary values to/from a string.
 
 template <class T>
@@ -217,10 +167,6 @@
   T val(defaultValue); FromString(str, &val); return val;
 }
 
-// simple function to strip out characters which shouldn't be
-// used in filenames
-char make_char_safe_for_filename(char c);
-
 //////////////////////////////////////////////////////////////////////
 
 }  // namespace rtc
diff --git a/rtc_base/stringencode_unittest.cc b/rtc_base/stringencode_unittest.cc
index e4b5c96..63d8290 100644
--- a/rtc_base/stringencode_unittest.cc
+++ b/rtc_base/stringencode_unittest.cc
@@ -15,51 +15,6 @@
 
 namespace rtc {
 
-TEST(Utf8EncodeTest, EncodeDecode) {
-  const struct Utf8Test {
-    const char* encoded;
-    size_t encsize, enclen;
-    unsigned long decoded;
-  } kTests[] = {
-      {"a    ",             5, 1, 'a'},
-      {"\x7F    ",          5, 1, 0x7F},
-      {"\xC2\x80   ",       5, 2, 0x80},
-      {"\xDF\xBF   ",       5, 2, 0x7FF},
-      {"\xE0\xA0\x80  ",    5, 3, 0x800},
-      {"\xEF\xBF\xBF  ",    5, 3, 0xFFFF},
-      {"\xF0\x90\x80\x80 ", 5, 4, 0x10000},
-      {"\xF0\x90\x80\x80 ", 3, 0, 0x10000},
-      {"\xF0\xF0\x80\x80 ", 5, 0, 0},
-      {"\xF0\x90\x80  ",    5, 0, 0},
-      {"\x90\x80\x80  ",    5, 0, 0},
-      {nullptr, 0, 0},
-  };
-  for (size_t i = 0; kTests[i].encoded; ++i) {
-    unsigned long val = 0;
-    ASSERT_EQ(kTests[i].enclen, utf8_decode(kTests[i].encoded,
-                                            kTests[i].encsize,
-                                            &val));
-    unsigned long result = (kTests[i].enclen == 0) ? 0 : kTests[i].decoded;
-    ASSERT_EQ(result, val);
-
-    if (kTests[i].decoded == 0) {
-      // Not an interesting encoding test case
-      continue;
-    }
-
-    char buffer[5];
-    memset(buffer, 0x01, arraysize(buffer));
-    ASSERT_EQ(kTests[i].enclen, utf8_encode(buffer,
-                                            kTests[i].encsize,
-                                            kTests[i].decoded));
-    ASSERT_TRUE(memcmp(buffer, kTests[i].encoded, kTests[i].enclen) == 0);
-    // Make sure remainder of buffer is unchanged
-    ASSERT_TRUE(memory_check(buffer + kTests[i].enclen,
-                             0x1,
-                             arraysize(buffer) - kTests[i].enclen));
-  }
-}
-
 class HexEncodeTest : public testing::Test {
  public:
   HexEncodeTest() : enc_res_(0), dec_res_(0) {