diff options
author | alexeypa@chromium.org <alexeypa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-30 04:04:57 +0000 |
---|---|---|
committer | alexeypa@chromium.org <alexeypa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-30 04:04:57 +0000 |
commit | 31014afc104172d9607ba2a1f14cec4b3a671216 (patch) | |
tree | 5e34843f6f9b8dd6ec572b52a991d2c15bfa7ece /remoting/base | |
parent | df61e2ba8a51419639ed39dc58a25e783005a2cd (diff) | |
download | chromium_src-31014afc104172d9607ba2a1f14cec4b3a671216.zip chromium_src-31014afc104172d9607ba2a1f14cec4b3a671216.tar.gz chromium_src-31014afc104172d9607ba2a1f14cec4b3a671216.tar.bz2 |
Added support of keyboard, mouse and clipboard events to DesktopSessionAgent.
BUG=134694
Review URL: https://chromiumcodereview.appspot.com/11417094
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170393 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/base')
-rw-r--r-- | remoting/base/util.cc | 32 | ||||
-rw-r--r-- | remoting/base/util.h | 3 | ||||
-rw-r--r-- | remoting/base/util_unittest.cc | 34 |
3 files changed, 69 insertions, 0 deletions
diff --git a/remoting/base/util.cc b/remoting/base/util.cc index 77b1c5d..6e656bf 100644 --- a/remoting/base/util.cc +++ b/remoting/base/util.cc @@ -302,4 +302,36 @@ std::string ReplaceCrLfByLf(const std::string& in) { return out; } +bool StringIsUtf8(const char* data, size_t length) { + const char* ptr = data; + const char* ptr_end = data + length; + while (ptr != ptr_end) { + if ((*ptr & 0x80) == 0) { + // Single-byte symbol. + ++ptr; + } else if ((*ptr & 0xc0) == 0x80 || (*ptr & 0xfe) == 0xfe) { + // Invalid first byte. + return false; + } else { + // First byte of a multi-byte symbol. The bits from 2 to 6 are the count + // of continuation bytes (up to 5 of them). + for (char first = *ptr << 1; first & 0x80; first <<= 1) { + ++ptr; + + // Missing continuation byte. + if (ptr == ptr_end) + return false; + + // Invalid continuation byte. + if ((*ptr & 0xc0) != 0x80) + return false; + } + + ++ptr; + } + } + + return true; +} + } // namespace remoting diff --git a/remoting/base/util.h b/remoting/base/util.h index a628cb0..a627c92 100644 --- a/remoting/base/util.h +++ b/remoting/base/util.h @@ -96,6 +96,9 @@ std::string ReplaceLfByCrLf(const std::string& in); // Replaces every occurrence of "\r\n" in a string by "\n". std::string ReplaceCrLfByLf(const std::string& in); +// Checks if the given string is a valid UTF-8 string. +bool StringIsUtf8(const char* data, size_t length); + } // namespace remoting #endif // REMOTING_BASE_UTIL_H_ diff --git a/remoting/base/util_unittest.cc b/remoting/base/util_unittest.cc index 19579776..4fc190d 100644 --- a/remoting/base/util_unittest.cc +++ b/remoting/base/util_unittest.cc @@ -253,4 +253,38 @@ TEST(ReplaceCrLfByLfTest, Speed) { } } +TEST(StringIsUtf8Test, Basic) { + EXPECT_TRUE(StringIsUtf8("", 0)); + EXPECT_TRUE(StringIsUtf8("\0", 1)); + EXPECT_TRUE(StringIsUtf8("abc", 3)); + EXPECT_TRUE(StringIsUtf8("\xc0\x80", 2)); + EXPECT_TRUE(StringIsUtf8("\xe0\x80\x80", 3)); + EXPECT_TRUE(StringIsUtf8("\xf0\x80\x80\x80", 4)); + EXPECT_TRUE(StringIsUtf8("\xf8\x80\x80\x80\x80", 5)); + EXPECT_TRUE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80", 6)); + + // Not enough continuation characters + EXPECT_FALSE(StringIsUtf8("\xc0", 1)); + EXPECT_FALSE(StringIsUtf8("\xe0\x80", 2)); + EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80", 3)); + EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80", 4)); + EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80", 5)); + + // One more continuation character than needed + EXPECT_FALSE(StringIsUtf8("\xc0\x80\x80", 3)); + EXPECT_FALSE(StringIsUtf8("\xe0\x80\x80\x80", 4)); + EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80\x80\x80", 5)); + EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80\x80\x80", 6)); + EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80\x80", 7)); + + // Invalid first byte + EXPECT_FALSE(StringIsUtf8("\xfe\x80\x80\x80\x80\x80\x80", 7)); + EXPECT_FALSE(StringIsUtf8("\xff\x80\x80\x80\x80\x80\x80", 7)); + + // Invalid continuation byte + EXPECT_FALSE(StringIsUtf8("\xc0\x00", 2)); + EXPECT_FALSE(StringIsUtf8("\xc0\x40", 2)); + EXPECT_FALSE(StringIsUtf8("\xc0\xc0", 2)); +} + } // namespace remoting |