1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/binarystream.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +// Copyright (c) 2010, 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 <arpa/inet.h> 1.34 +#include <limits.h> 1.35 + 1.36 +#include <string> 1.37 +#include <vector> 1.38 + 1.39 +#include "common/using_std_string.h" 1.40 +#include "processor/binarystream.h" 1.41 + 1.42 +namespace google_breakpad { 1.43 +using std::vector; 1.44 + 1.45 +binarystream &binarystream::operator>>(string &str) { 1.46 + uint16_t length; 1.47 + *this >> length; 1.48 + if (eof()) 1.49 + return *this; 1.50 + if (length == 0) { 1.51 + str.clear(); 1.52 + return *this; 1.53 + } 1.54 + vector<char> buffer(length); 1.55 + stream_.read(&buffer[0], length); 1.56 + if (!eof()) 1.57 + str.assign(&buffer[0], length); 1.58 + return *this; 1.59 +} 1.60 + 1.61 +binarystream &binarystream::operator>>(uint8_t &u8) { 1.62 + stream_.read((char *)&u8, 1); 1.63 + return *this; 1.64 +} 1.65 + 1.66 +binarystream &binarystream::operator>>(uint16_t &u16) { 1.67 + uint16_t temp; 1.68 + stream_.read((char *)&temp, 2); 1.69 + if (!eof()) 1.70 + u16 = ntohs(temp); 1.71 + return *this; 1.72 +} 1.73 + 1.74 +binarystream &binarystream::operator>>(uint32_t &u32) { 1.75 + uint32_t temp; 1.76 + stream_.read((char *)&temp, 4); 1.77 + if (!eof()) 1.78 + u32 = ntohl(temp); 1.79 + return *this; 1.80 +} 1.81 + 1.82 +binarystream &binarystream::operator>>(uint64_t &u64) { 1.83 + uint32_t lower, upper; 1.84 + *this >> lower >> upper; 1.85 + if (!eof()) 1.86 + u64 = static_cast<uint64_t>(lower) | (static_cast<uint64_t>(upper) << 32); 1.87 + return *this; 1.88 +} 1.89 + 1.90 +binarystream &binarystream::operator<<(const string &str) { 1.91 + if (str.length() > USHRT_MAX) { 1.92 + // truncate to 16-bit length 1.93 + *this << static_cast<uint16_t>(USHRT_MAX); 1.94 + stream_.write(str.c_str(), USHRT_MAX); 1.95 + } else { 1.96 + *this << (uint16_t)(str.length() & 0xFFFF); 1.97 + stream_.write(str.c_str(), str.length()); 1.98 + } 1.99 + return *this; 1.100 +} 1.101 + 1.102 +binarystream &binarystream::operator<<(uint8_t u8) { 1.103 + stream_.write((const char*)&u8, 1); 1.104 + return *this; 1.105 +} 1.106 + 1.107 +binarystream &binarystream::operator<<(uint16_t u16) { 1.108 + u16 = htons(u16); 1.109 + stream_.write((const char*)&u16, 2); 1.110 + return *this; 1.111 +} 1.112 + 1.113 +binarystream &binarystream::operator<<(uint32_t u32) { 1.114 + u32 = htonl(u32); 1.115 + stream_.write((const char*)&u32, 4); 1.116 + return *this; 1.117 +} 1.118 + 1.119 +binarystream &binarystream::operator<<(uint64_t u64) { 1.120 + // write 64-bit ints as two 32-bit ints, so we can byte-swap them easily 1.121 + uint32_t lower = static_cast<uint32_t>(u64 & 0xFFFFFFFF); 1.122 + uint32_t upper = static_cast<uint32_t>(u64 >> 32); 1.123 + *this << lower << upper; 1.124 + return *this; 1.125 +} 1.126 + 1.127 +} // namespace google_breakpad