security/sandbox/chromium/base/strings/string16.h

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

michael@0 1 // Copyright 2013 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #ifndef BASE_STRINGS_STRING16_H_
michael@0 6 #define BASE_STRINGS_STRING16_H_
michael@0 7
michael@0 8 // WHAT:
michael@0 9 // A version of std::basic_string that provides 2-byte characters even when
michael@0 10 // wchar_t is not implemented as a 2-byte type. You can access this class as
michael@0 11 // string16. We also define char16, which string16 is based upon.
michael@0 12 //
michael@0 13 // WHY:
michael@0 14 // On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2
michael@0 15 // data. Plenty of existing code operates on strings encoded as UTF-16.
michael@0 16 //
michael@0 17 // On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make
michael@0 18 // it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails
michael@0 19 // at run time, because it calls some functions (like wcslen) that come from
michael@0 20 // the system's native C library -- which was built with a 4-byte wchar_t!
michael@0 21 // It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's
michael@0 22 // entirely improper on those systems where the encoding of wchar_t is defined
michael@0 23 // as UTF-32.
michael@0 24 //
michael@0 25 // Here, we define string16, which is similar to std::wstring but replaces all
michael@0 26 // libc functions with custom, 2-byte-char compatible routines. It is capable
michael@0 27 // of carrying UTF-16-encoded data.
michael@0 28
michael@0 29 #include <stdio.h>
michael@0 30 #include <string>
michael@0 31
michael@0 32 #include "base/base_export.h"
michael@0 33 #include "base/basictypes.h"
michael@0 34
michael@0 35 #if defined(WCHAR_T_IS_UTF16)
michael@0 36
michael@0 37 namespace base {
michael@0 38
michael@0 39 typedef wchar_t char16;
michael@0 40 typedef std::wstring string16;
michael@0 41 typedef std::char_traits<wchar_t> string16_char_traits;
michael@0 42
michael@0 43 } // namespace base
michael@0 44
michael@0 45 #elif defined(WCHAR_T_IS_UTF32)
michael@0 46
michael@0 47 namespace base {
michael@0 48
michael@0 49 typedef uint16 char16;
michael@0 50
michael@0 51 // char16 versions of the functions required by string16_char_traits; these
michael@0 52 // are based on the wide character functions of similar names ("w" or "wcs"
michael@0 53 // instead of "c16").
michael@0 54 BASE_EXPORT int c16memcmp(const char16* s1, const char16* s2, size_t n);
michael@0 55 BASE_EXPORT size_t c16len(const char16* s);
michael@0 56 BASE_EXPORT const char16* c16memchr(const char16* s, char16 c, size_t n);
michael@0 57 BASE_EXPORT char16* c16memmove(char16* s1, const char16* s2, size_t n);
michael@0 58 BASE_EXPORT char16* c16memcpy(char16* s1, const char16* s2, size_t n);
michael@0 59 BASE_EXPORT char16* c16memset(char16* s, char16 c, size_t n);
michael@0 60
michael@0 61 struct string16_char_traits {
michael@0 62 typedef char16 char_type;
michael@0 63 typedef int int_type;
michael@0 64
michael@0 65 // int_type needs to be able to hold each possible value of char_type, and in
michael@0 66 // addition, the distinct value of eof().
michael@0 67 COMPILE_ASSERT(sizeof(int_type) > sizeof(char_type), unexpected_type_width);
michael@0 68
michael@0 69 typedef std::streamoff off_type;
michael@0 70 typedef mbstate_t state_type;
michael@0 71 typedef std::fpos<state_type> pos_type;
michael@0 72
michael@0 73 static void assign(char_type& c1, const char_type& c2) {
michael@0 74 c1 = c2;
michael@0 75 }
michael@0 76
michael@0 77 static bool eq(const char_type& c1, const char_type& c2) {
michael@0 78 return c1 == c2;
michael@0 79 }
michael@0 80 static bool lt(const char_type& c1, const char_type& c2) {
michael@0 81 return c1 < c2;
michael@0 82 }
michael@0 83
michael@0 84 static int compare(const char_type* s1, const char_type* s2, size_t n) {
michael@0 85 return c16memcmp(s1, s2, n);
michael@0 86 }
michael@0 87
michael@0 88 static size_t length(const char_type* s) {
michael@0 89 return c16len(s);
michael@0 90 }
michael@0 91
michael@0 92 static const char_type* find(const char_type* s, size_t n,
michael@0 93 const char_type& a) {
michael@0 94 return c16memchr(s, a, n);
michael@0 95 }
michael@0 96
michael@0 97 static char_type* move(char_type* s1, const char_type* s2, int_type n) {
michael@0 98 return c16memmove(s1, s2, n);
michael@0 99 }
michael@0 100
michael@0 101 static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
michael@0 102 return c16memcpy(s1, s2, n);
michael@0 103 }
michael@0 104
michael@0 105 static char_type* assign(char_type* s, size_t n, char_type a) {
michael@0 106 return c16memset(s, a, n);
michael@0 107 }
michael@0 108
michael@0 109 static int_type not_eof(const int_type& c) {
michael@0 110 return eq_int_type(c, eof()) ? 0 : c;
michael@0 111 }
michael@0 112
michael@0 113 static char_type to_char_type(const int_type& c) {
michael@0 114 return char_type(c);
michael@0 115 }
michael@0 116
michael@0 117 static int_type to_int_type(const char_type& c) {
michael@0 118 return int_type(c);
michael@0 119 }
michael@0 120
michael@0 121 static bool eq_int_type(const int_type& c1, const int_type& c2) {
michael@0 122 return c1 == c2;
michael@0 123 }
michael@0 124
michael@0 125 static int_type eof() {
michael@0 126 return static_cast<int_type>(EOF);
michael@0 127 }
michael@0 128 };
michael@0 129
michael@0 130 typedef std::basic_string<char16, base::string16_char_traits> string16;
michael@0 131
michael@0 132 BASE_EXPORT extern std::ostream& operator<<(std::ostream& out,
michael@0 133 const string16& str);
michael@0 134
michael@0 135 // This is required by googletest to print a readable output on test failures.
michael@0 136 BASE_EXPORT extern void PrintTo(const string16& str, std::ostream* out);
michael@0 137
michael@0 138 } // namespace base
michael@0 139
michael@0 140 // The string class will be explicitly instantiated only once, in string16.cc.
michael@0 141 //
michael@0 142 // std::basic_string<> in GNU libstdc++ contains a static data member,
michael@0 143 // _S_empty_rep_storage, to represent empty strings. When an operation such
michael@0 144 // as assignment or destruction is performed on a string, causing its existing
michael@0 145 // data member to be invalidated, it must not be freed if this static data
michael@0 146 // member is being used. Otherwise, it counts as an attempt to free static
michael@0 147 // (and not allocated) data, which is a memory error.
michael@0 148 //
michael@0 149 // Generally, due to C++ template magic, _S_empty_rep_storage will be marked
michael@0 150 // as a coalesced symbol, meaning that the linker will combine multiple
michael@0 151 // instances into a single one when generating output.
michael@0 152 //
michael@0 153 // If a string class is used by multiple shared libraries, a problem occurs.
michael@0 154 // Each library will get its own copy of _S_empty_rep_storage. When strings
michael@0 155 // are passed across a library boundary for alteration or destruction, memory
michael@0 156 // errors will result. GNU libstdc++ contains a configuration option,
michael@0 157 // --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which
michael@0 158 // disables the static data member optimization, but it's a good optimization
michael@0 159 // and non-STL code is generally at the mercy of the system's STL
michael@0 160 // configuration. Fully-dynamic strings are not the default for GNU libstdc++
michael@0 161 // libstdc++ itself or for the libstdc++ installations on the systems we care
michael@0 162 // about, such as Mac OS X and relevant flavors of Linux.
michael@0 163 //
michael@0 164 // See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 .
michael@0 165 //
michael@0 166 // To avoid problems, string classes need to be explicitly instantiated only
michael@0 167 // once, in exactly one library. All other string users see it via an "extern"
michael@0 168 // declaration. This is precisely how GNU libstdc++ handles
michael@0 169 // std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring).
michael@0 170 //
michael@0 171 // This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2),
michael@0 172 // in which the linker does not fully coalesce symbols when dead code
michael@0 173 // stripping is enabled. This bug causes the memory errors described above
michael@0 174 // to occur even when a std::basic_string<> does not cross shared library
michael@0 175 // boundaries, such as in statically-linked executables.
michael@0 176 //
michael@0 177 // TODO(mark): File this bug with Apple and update this note with a bug number.
michael@0 178
michael@0 179 extern template
michael@0 180 class BASE_EXPORT std::basic_string<base::char16, base::string16_char_traits>;
michael@0 181
michael@0 182 #endif // WCHAR_T_IS_UTF32
michael@0 183
michael@0 184 // TODO(brettw) update users of string16 to use the namespace and remove
michael@0 185 // this "using".
michael@0 186 using base::char16;
michael@0 187 using base::string16;
michael@0 188
michael@0 189 #endif // BASE_STRINGS_STRING16_H_

mercurial