PageRenderTime 30ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 30#include <arpa/inet.h>
 31#include <limits.h>
 32
 33#include <vector>
 34
 35#include "processor/binarystream.h"
 36
 37namespace google_breakpad {
 38using std::string;
 39using std::vector;
 40
 41binarystream &binarystream::operator>>(std::string &str) {
 42  u_int16_t length;
 43  *this >> length;
 44  if (eof())
 45    return *this;
 46  if (length == 0) {
 47    str.clear();
 48    return *this;
 49  }
 50  vector<char> buffer(length);
 51  stream_.read(&buffer[0], length);
 52  if (!eof())
 53    str.assign(&buffer[0], length);
 54  return *this;
 55}
 56
 57binarystream &binarystream::operator>>(u_int8_t &u8) {
 58  stream_.read((char *)&u8, 1);
 59  return *this;
 60}
 61
 62binarystream &binarystream::operator>>(u_int16_t &u16) {
 63  u_int16_t temp;
 64  stream_.read((char *)&temp, 2);
 65  if (!eof())
 66    u16 = ntohs(temp);
 67  return *this;
 68}
 69
 70binarystream &binarystream::operator>>(u_int32_t &u32) {
 71  u_int32_t temp;
 72  stream_.read((char *)&temp, 4);
 73  if (!eof())
 74    u32 = ntohl(temp);
 75  return *this;
 76}
 77
 78binarystream &binarystream::operator>>(u_int64_t &u64) {
 79  u_int32_t lower, upper;
 80  *this >> lower >> upper;
 81  if (!eof())
 82    u64 = static_cast<u_int64_t>(lower) | (static_cast<u_int64_t>(upper) << 32);
 83  return *this;
 84}
 85
 86binarystream &binarystream::operator<<(const std::string &str) {
 87  if (str.length() > USHRT_MAX) {
 88    // truncate to 16-bit length
 89    *this << static_cast<u_int16_t>(USHRT_MAX);
 90    stream_.write(str.c_str(), USHRT_MAX);
 91  } else {
 92    *this << (u_int16_t)(str.length() & 0xFFFF);
 93    stream_.write(str.c_str(), str.length());
 94  }
 95  return *this;
 96}
 97
 98binarystream &binarystream::operator<<(u_int8_t u8) {
 99  stream_.write((const char*)&u8, 1);
100  return *this;
101}
102
103binarystream &binarystream::operator<<(u_int16_t u16) {
104  u16 = htons(u16);
105  stream_.write((const char*)&u16, 2);
106  return *this;
107}
108
109binarystream &binarystream::operator<<(u_int32_t u32) {
110  u32 = htonl(u32);
111  stream_.write((const char*)&u32, 4);
112  return *this;
113}
114
115binarystream &binarystream::operator<<(u_int64_t u64) {
116  // write 64-bit ints as two 32-bit ints, so we can byte-swap them easily
117  u_int32_t lower = static_cast<u_int32_t>(u64 & 0xFFFFFFFF);
118  u_int32_t upper = static_cast<u_int32_t>(u64 >> 32);
119  *this << lower << upper;
120  return *this;
121}
122
123}  // namespace google_breakpad