summaryrefslogtreecommitdiffstats
path: root/chromeos/binder/transaction_data_reader.cc
blob: b1006ab1b88a26413f41987a3c0a4f9150405891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/binder/transaction_data_reader.h"

#include <stddef.h>
#include <stdint.h>

#include <linux/android/binder.h>

#include "base/logging.h"
#include "chromeos/binder/local_object.h"
#include "chromeos/binder/object.h"
#include "chromeos/binder/remote_object.h"
#include "chromeos/binder/transaction_data.h"

namespace binder {

namespace {

// Adds appropriate padding to the given size to make it 4-byte aligned.
size_t AddPadding(size_t n) {
  return (n + 3) & (~3);
}

}  // namespace

TransactionDataReader::TransactionDataReader(const TransactionData& data)
    : data_(data),
      reader_(reinterpret_cast<const char*>(data.GetData()),
              data.GetDataSize()) {}

TransactionDataReader::~TransactionDataReader() {}

bool TransactionDataReader::HasMoreData() const {
  return reader_.HasMoreData();
}

bool TransactionDataReader::ReadData(void* buf, size_t n) {
  DCHECK(buf);
  return reader_.Read(buf, n) && reader_.Skip(AddPadding(n) - n);
}

bool TransactionDataReader::ReadInt32(int32_t* value) {
  return ReadData(value, sizeof(*value));
}

bool TransactionDataReader::ReadUint32(uint32_t* value) {
  return ReadData(value, sizeof(*value));
}

bool TransactionDataReader::ReadInt64(int64_t* value) {
  return ReadData(value, sizeof(*value));
}

bool TransactionDataReader::ReadUint64(uint64_t* value) {
  return ReadData(value, sizeof(*value));
}

bool TransactionDataReader::ReadFloat(float* value) {
  return ReadData(value, sizeof(*value));
}

bool TransactionDataReader::ReadDouble(double* value) {
  return ReadData(value, sizeof(*value));
}

bool TransactionDataReader::ReadCString(const char** value) {
  *value = reader_.current();
  for (size_t len = 0; reader_.HasMoreData(); ++len) {
    char c = 0;
    if (!reader_.Read(&c, sizeof(c))) {
      return false;
    }
    if (c == 0) {
      return reader_.Skip(AddPadding(len + 1) - (len + 1));
    }
  }
  return false;
}

bool TransactionDataReader::ReadString(std::string* value) {
  int32_t len = 0;
  if (!ReadInt32(&len)) {
    return false;
  }
  if (len == 0) {
    // Read only when the string is not empty.
    // This is different from ReadString16().
    value->clear();
    return true;
  }
  const char* start = reader_.current();
  if (!reader_.Skip(AddPadding(len + 1))) {
    return false;
  }
  value->assign(start, len);
  return true;
}

bool TransactionDataReader::ReadString16(base::string16* value) {
  int32_t len = 0;
  if (!ReadInt32(&len)) {
    return false;
  }
  const base::char16* start =
      reinterpret_cast<const base::char16*>(reader_.current());
  if (!reader_.Skip(AddPadding((len + 1) * sizeof(base::char16)))) {
    return false;
  }
  value->assign(start, len);
  return true;
}

scoped_refptr<Object> TransactionDataReader::ReadObject(
    CommandBroker* command_broker) {
  DCHECK(command_broker);
  flat_binder_object obj = {};
  if (!ReadData(&obj, sizeof(obj))) {
    return scoped_refptr<Object>();
  }
  switch (obj.type) {
    case BINDER_TYPE_HANDLE:
      return make_scoped_refptr(new RemoteObject(command_broker, obj.handle));
    case BINDER_TYPE_BINDER:
      return make_scoped_refptr(reinterpret_cast<LocalObject*>(obj.cookie));
  }
  return scoped_refptr<Object>();
}

bool TransactionDataReader::ReadFileDescriptor(int* fd) {
  flat_binder_object obj = {};
  if (!ReadData(&obj, sizeof(obj)) || obj.type != BINDER_TYPE_FD) {
    return false;
  }
  *fd = obj.handle;
  return true;
}

}  // namespace binder