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