Begin processing read commands.
[minibind.git] / jni / minibind / parcel.cc
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "parcel.h"
6
7 #include "common.h"
8
9 namespace minibind {
10
11 Parcel::Parcel() : data_position_(0), data_(), objects_() {}
12
13 Parcel::~Parcel() {}
14
15 void Parcel::WriteInterfaceToken(const std::string& str) {
16 WriteUTF8(str);
17 }
18
19 void Parcel::WriteUTF8(const std::string& str) {
20 data_.insert(data_.end(), str.begin(), str.end());
21 }
22
23 void Parcel::WriteUInt32(uint32_t ui32) {
24 data_.push_back((ui32 >> 24) & 0xff);
25 data_.push_back((ui32 >> 16) & 0xff);
26 data_.push_back((ui32 >> 8) & 0xff);
27 data_.push_back((ui32 >> 0) & 0xff);
28 }
29
30 bool Parcel::ReadUInt32(uint32_t* ui32) {
31 return ReadBytes(sizeof(*ui32), ui32);
32 }
33
34 bool Parcel::ReadBytes(size_t count, void* buffer) {
35 // TODO: overflow
36 ALOG("count=%d, data_position_=%d, datasize=%d", count, data_position_, DataSize());
37 if (count + data_position_ >= DataSize())
38 return false;
39
40 memcpy(buffer, &data_[data_position_], count);
41 data_position_ += count;
42 return true;
43 }
44
45 binder_uintptr_t Parcel::DataPointer() const {
46 return reinterpret_cast<binder_uintptr_t>(&data_[0]);
47 }
48
49 size_t Parcel::DataSize() const {
50 return data_.size();
51 }
52
53 void Parcel::SetDataSize(size_t size) {
54 // TODO: handle object lifetimes
55 CHECK(size % 4 == 0);
56 data_.resize(size);
57 }
58
59 void Parcel::SetDataPosition(size_t pos) {
60 CHECK(pos < DataSize());
61 data_position_ = pos;
62 }
63
64 size_t Parcel::DataPosition() const {
65 return data_position_;
66 }
67
68 void Parcel::Print() const {
69 for (size_t i = 0; i < data_.size(); i += 4) {
70 ALOG("%0x %0x %0x %0x", data_[i], data_[i+1], data_[i+2], data_[i+3]);
71 }
72 }
73
74 } // namespace minibind