minidump_writer_util.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "minidump/minidump_writer_util.h"
  15. #include "base/logging.h"
  16. #include "base/numerics/safe_conversions.h"
  17. #include "base/strings/utf_string_conversions.h"
  18. #include "util/stdlib/strlcpy.h"
  19. namespace crashpad {
  20. namespace internal {
  21. // static
  22. void MinidumpWriterUtil::AssignTimeT(uint32_t* destination, time_t source) {
  23. if (!base::IsValueInRangeForNumericType<uint32_t>(source)) {
  24. LOG(WARNING) << "timestamp " << source << " out of range";
  25. }
  26. *destination = static_cast<uint32_t>(source);
  27. }
  28. // static
  29. base::string16 MinidumpWriterUtil::ConvertUTF8ToUTF16(const std::string& utf8) {
  30. base::string16 utf16;
  31. if (!base::UTF8ToUTF16(utf8.data(), utf8.length(), &utf16)) {
  32. LOG(WARNING) << "string " << utf8
  33. << " cannot be converted to UTF-16 losslessly";
  34. }
  35. return utf16;
  36. }
  37. // static
  38. void MinidumpWriterUtil::AssignUTF8ToUTF16(base::char16* destination,
  39. size_t destination_size,
  40. const std::string& source) {
  41. base::string16 source_utf16 = ConvertUTF8ToUTF16(source);
  42. if (source_utf16.size() > destination_size - 1) {
  43. LOG(WARNING) << "string " << source << " UTF-16 length "
  44. << source_utf16.size()
  45. << " will be truncated to UTF-16 length "
  46. << destination_size - 1;
  47. }
  48. source_utf16.resize(destination_size - 1);
  49. c16lcpy(destination, source_utf16.c_str(), destination_size);
  50. }
  51. } // namespace internal
  52. } // namespace crashpad