/thirdparty/breakpad/processor/binarystream.cc

http://github.com/tomahawk-player/tomahawk · C++ · 123 lines · 78 code · 15 blank · 30 comment · 9 complexity · befdeaf50dd79af8090f46ca10d5efc1 MD5 · raw file

  1. // Copyright (c) 2010, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #include <arpa/inet.h>
  30. #include <limits.h>
  31. #include <vector>
  32. #include "processor/binarystream.h"
  33. namespace google_breakpad {
  34. using std::string;
  35. using std::vector;
  36. binarystream &binarystream::operator>>(std::string &str) {
  37. u_int16_t length;
  38. *this >> length;
  39. if (eof())
  40. return *this;
  41. if (length == 0) {
  42. str.clear();
  43. return *this;
  44. }
  45. vector<char> buffer(length);
  46. stream_.read(&buffer[0], length);
  47. if (!eof())
  48. str.assign(&buffer[0], length);
  49. return *this;
  50. }
  51. binarystream &binarystream::operator>>(u_int8_t &u8) {
  52. stream_.read((char *)&u8, 1);
  53. return *this;
  54. }
  55. binarystream &binarystream::operator>>(u_int16_t &u16) {
  56. u_int16_t temp;
  57. stream_.read((char *)&temp, 2);
  58. if (!eof())
  59. u16 = ntohs(temp);
  60. return *this;
  61. }
  62. binarystream &binarystream::operator>>(u_int32_t &u32) {
  63. u_int32_t temp;
  64. stream_.read((char *)&temp, 4);
  65. if (!eof())
  66. u32 = ntohl(temp);
  67. return *this;
  68. }
  69. binarystream &binarystream::operator>>(u_int64_t &u64) {
  70. u_int32_t lower, upper;
  71. *this >> lower >> upper;
  72. if (!eof())
  73. u64 = static_cast<u_int64_t>(lower) | (static_cast<u_int64_t>(upper) << 32);
  74. return *this;
  75. }
  76. binarystream &binarystream::operator<<(const std::string &str) {
  77. if (str.length() > USHRT_MAX) {
  78. // truncate to 16-bit length
  79. *this << static_cast<u_int16_t>(USHRT_MAX);
  80. stream_.write(str.c_str(), USHRT_MAX);
  81. } else {
  82. *this << (u_int16_t)(str.length() & 0xFFFF);
  83. stream_.write(str.c_str(), str.length());
  84. }
  85. return *this;
  86. }
  87. binarystream &binarystream::operator<<(u_int8_t u8) {
  88. stream_.write((const char*)&u8, 1);
  89. return *this;
  90. }
  91. binarystream &binarystream::operator<<(u_int16_t u16) {
  92. u16 = htons(u16);
  93. stream_.write((const char*)&u16, 2);
  94. return *this;
  95. }
  96. binarystream &binarystream::operator<<(u_int32_t u32) {
  97. u32 = htonl(u32);
  98. stream_.write((const char*)&u32, 4);
  99. return *this;
  100. }
  101. binarystream &binarystream::operator<<(u_int64_t u64) {
  102. // write 64-bit ints as two 32-bit ints, so we can byte-swap them easily
  103. u_int32_t lower = static_cast<u_int32_t>(u64 & 0xFFFFFFFF);
  104. u_int32_t upper = static_cast<u_int32_t>(u64 >> 32);
  105. *this << lower << upper;
  106. return *this;
  107. }
  108. } // namespace google_breakpad