toolkit/crashreporter/google-breakpad/src/common/windows/string_utils.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/windows/string_utils.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,133 @@
     1.4 +// Copyright (c) 2006, Google Inc.
     1.5 +// All rights reserved.
     1.6 +//
     1.7 +// Redistribution and use in source and binary forms, with or without
     1.8 +// modification, are permitted provided that the following conditions are
     1.9 +// met:
    1.10 +//
    1.11 +//     * Redistributions of source code must retain the above copyright
    1.12 +// notice, this list of conditions and the following disclaimer.
    1.13 +//     * Redistributions in binary form must reproduce the above
    1.14 +// copyright notice, this list of conditions and the following disclaimer
    1.15 +// in the documentation and/or other materials provided with the
    1.16 +// distribution.
    1.17 +//     * Neither the name of Google Inc. nor the names of its
    1.18 +// contributors may be used to endorse or promote products derived from
    1.19 +// this software without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +#include <cassert>
    1.34 +#include <vector>
    1.35 +
    1.36 +#include "common/windows/string_utils-inl.h"
    1.37 +
    1.38 +namespace google_breakpad {
    1.39 +
    1.40 +// static
    1.41 +wstring WindowsStringUtils::GetBaseName(const wstring &filename) {
    1.42 +  wstring base_name(filename);
    1.43 +  size_t slash_pos = base_name.find_last_of(L"/\\");
    1.44 +  if (slash_pos != wstring::npos) {
    1.45 +    base_name.erase(0, slash_pos + 1);
    1.46 +  }
    1.47 +  return base_name;
    1.48 +}
    1.49 +
    1.50 +// static
    1.51 +bool WindowsStringUtils::safe_mbstowcs(const string &mbs, wstring *wcs) {
    1.52 +  assert(wcs);
    1.53 +
    1.54 +  // First, determine the length of the destination buffer.
    1.55 +  size_t wcs_length;
    1.56 +
    1.57 +#if _MSC_VER >= 1400  // MSVC 2005/8
    1.58 +  errno_t err;
    1.59 +  if ((err = mbstowcs_s(&wcs_length, NULL, 0, mbs.c_str(), _TRUNCATE)) != 0) {
    1.60 +    return false;
    1.61 +  }
    1.62 +  assert(wcs_length > 0);
    1.63 +#else  // _MSC_VER >= 1400
    1.64 +  if ((wcs_length = mbstowcs(NULL, mbs.c_str(), mbs.length())) < 0) {
    1.65 +    return false;
    1.66 +  }
    1.67 +
    1.68 +  // Leave space for the 0-terminator.
    1.69 +  ++wcs_length;
    1.70 +#endif  // _MSC_VER >= 1400
    1.71 +
    1.72 +  std::vector<wchar_t> wcs_v(wcs_length);
    1.73 +
    1.74 +  // Now, convert.
    1.75 +#if _MSC_VER >= 1400  // MSVC 2005/8
    1.76 +  if ((err = mbstowcs_s(NULL, &wcs_v[0], wcs_length, mbs.c_str(),
    1.77 +                        _TRUNCATE)) != 0) {
    1.78 +    return false;
    1.79 +  }
    1.80 +#else  // _MSC_VER >= 1400
    1.81 +  if (mbstowcs(&wcs_v[0], mbs.c_str(), mbs.length()) < 0) {
    1.82 +    return false;
    1.83 +  }
    1.84 +
    1.85 +  // Ensure presence of 0-terminator.
    1.86 +  wcs_v[wcs_length - 1] = '\0';
    1.87 +#endif  // _MSC_VER >= 1400
    1.88 +
    1.89 +  *wcs = &wcs_v[0];
    1.90 +  return true;
    1.91 +}
    1.92 +
    1.93 +// static
    1.94 +bool WindowsStringUtils::safe_wcstombs(const wstring &wcs, string *mbs) {
    1.95 +  assert(mbs);
    1.96 +
    1.97 +  // First, determine the length of the destination buffer.
    1.98 +  size_t mbs_length;
    1.99 +
   1.100 +#if _MSC_VER >= 1400  // MSVC 2005/8
   1.101 +  errno_t err;
   1.102 +  if ((err = wcstombs_s(&mbs_length, NULL, 0, wcs.c_str(), _TRUNCATE)) != 0) {
   1.103 +    return false;
   1.104 +  }
   1.105 +  assert(mbs_length > 0);
   1.106 +#else  // _MSC_VER >= 1400
   1.107 +  if ((mbs_length = wcstombs(NULL, wcs.c_str(), wcs.length())) < 0) {
   1.108 +    return false;
   1.109 +  }
   1.110 +
   1.111 +  // Leave space for the 0-terminator.
   1.112 +  ++mbs_length;
   1.113 +#endif  // _MSC_VER >= 1400
   1.114 +
   1.115 +  std::vector<char> mbs_v(mbs_length);
   1.116 +
   1.117 +  // Now, convert.
   1.118 +#if _MSC_VER >= 1400  // MSVC 2005/8
   1.119 +  if ((err = wcstombs_s(NULL, &mbs_v[0], mbs_length, wcs.c_str(),
   1.120 +                        _TRUNCATE)) != 0) {
   1.121 +    return false;
   1.122 +  }
   1.123 +#else  // _MSC_VER >= 1400
   1.124 +  if (wcstombs(&mbs_v[0], wcs.c_str(), wcs.length()) < 0) {
   1.125 +    return false;
   1.126 +  }
   1.127 +
   1.128 +  // Ensure presence of 0-terminator.
   1.129 +  mbs_v[mbs_length - 1] = '\0';
   1.130 +#endif  // _MSC_VER >= 1400
   1.131 +
   1.132 +  *mbs = &mbs_v[0];
   1.133 +  return true;
   1.134 +}
   1.135 +
   1.136 +}  // namespace google_breakpad

mercurial