blob: 3d20548f18b7e077a53c31318e56a62b5439a8de [file] [log] [blame]
Yi Jin99c248f2017-08-25 18:11:58 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "EncodedBuffer.h"
18#include "io_util.h"
19#include "protobuf.h"
20
21#include <deque>
22
23const size_t BUFFER_SIZE = 4 * 1024; // 4 KB
24
25/**
26 * Read varint from iterator, the iterator will point to next available byte.
27 * Return the number of bytes of the varint.
28 */
29static uint32_t
30read_raw_varint(FdBuffer::iterator& it)
31{
32 uint32_t val = 0;
33 int i = 0;
34 bool hasNext = true;
35 while (hasNext) {
36 hasNext = ((*it & 0x80) != 0);
37 val += (*it & 0x7F) << (7*i);
38 it++;
39 i++;
40 }
41 return val;
42}
43
44/**
45 * Write the field to buf based on the wire type, iterator will point to next field.
46 * If skip is set to true, no data will be written to buf. Return number of bytes written.
47 */
48static size_t
49write_field_or_skip(FdBuffer::iterator &iterator, vector<uint8_t> &buf, uint8_t wireType, bool skip)
50{
51 FdBuffer::iterator snapshot = iterator.snapshot();
52 size_t bytesToWrite = 0;
53 uint32_t varint = 0;
54 switch (wireType) {
55 case WIRE_TYPE_VARINT:
56 varint = read_raw_varint(iterator);
57 if(!skip) return write_raw_varint(buf, varint);
58 break;
59 case WIRE_TYPE_FIXED64:
60 bytesToWrite = 8;
61 break;
62 case WIRE_TYPE_LENGTH_DELIMITED:
63 bytesToWrite = read_raw_varint(iterator);
64 if(!skip) write_raw_varint(buf, bytesToWrite);
65 break;
66 case WIRE_TYPE_FIXED32:
67 bytesToWrite = 4;
68 break;
69 }
70 if (skip) {
71 iterator += bytesToWrite;
72 } else {
Yi Jin99c248f2017-08-25 18:11:58 -070073 for (size_t i=0; i<bytesToWrite; i++) {
74 buf.push_back(*iterator);
75 iterator++;
76 }
77 }
78 return skip ? 0 : iterator - snapshot;
79}
80
81/**
82 * Strip next field based on its private policy and request spec, then stores data in buf.
83 * Return NO_ERROR if succeeds, otherwise BAD_VALUE is returned to indicate bad data in FdBuffer.
84 *
85 * The iterator must point to the head of a protobuf formatted field for successful operation.
86 * After exit with NO_ERROR, iterator points to the next protobuf field's head.
87 */
88static status_t
89stripField(FdBuffer::iterator &iterator, vector<uint8_t> &buf, const Privacy* parentPolicy, const PrivacySpec& spec)
90{
91 if (iterator.outOfBound() || parentPolicy == NULL) return BAD_VALUE;
92
93 uint32_t varint = read_raw_varint(iterator);
94 uint8_t wireType = read_wire_type(varint);
95 uint32_t fieldId = read_field_id(varint);
96 const Privacy* policy = parentPolicy->lookup(fieldId);
97
98 if (policy == NULL || !policy->IsMessageType() || !policy->HasChildren()) {
99 bool skip = !spec.CheckPremission(policy);
100 size_t amt = buf.size();
101 if (!skip) amt += write_header(buf, fieldId, wireType);
102 amt += write_field_or_skip(iterator, buf, wireType, skip); // point to head of next field
103 return buf.size() != amt ? BAD_VALUE : NO_ERROR;
104 }
105 // current field is message type and its sub-fields have extra privacy policies
106 deque<vector<uint8_t>> q;
107 uint32_t msgSize = read_raw_varint(iterator);
108 size_t finalSize = 0;
109 FdBuffer::iterator start = iterator.snapshot();
110 while ((iterator - start) != (int)msgSize) {
111 vector<uint8_t> v;
112 status_t err = stripField(iterator, v, policy, spec);
113 if (err != NO_ERROR) return err;
114 if (v.empty()) continue;
115 q.push_back(v);
116 finalSize += v.size();
117 }
118
119 write_header(buf, fieldId, wireType);
120 write_raw_varint(buf, finalSize);
121 buf.reserve(finalSize);
122 while (!q.empty()) {
123 vector<uint8_t> subField = q.front();
124 for (vector<uint8_t>::iterator it = subField.begin(); it != subField.end(); it++) {
125 buf.push_back(*it);
126 }
127 q.pop_front();
128 }
129 return NO_ERROR;
130}
131
132// ================================================================================
133EncodedBuffer::EncodedBuffer(const FdBuffer& buffer, const Privacy* policy)
134 : mFdBuffer(buffer),
135 mPolicy(policy),
136 mBuffers(),
137 mSize(0)
138{
139}
140
141EncodedBuffer::~EncodedBuffer()
142{
143}
144
145status_t
146EncodedBuffer::strip(const PrivacySpec& spec)
147{
148 // optimization when no strip happens
149 if (mPolicy == NULL || !mPolicy->HasChildren() || spec.RequireAll()) {
150 if (spec.CheckPremission(mPolicy)) mSize = mFdBuffer.size();
151 return NO_ERROR;
152 }
153
154 FdBuffer::iterator it = mFdBuffer.begin();
155 vector<uint8_t> field;
156 field.reserve(BUFFER_SIZE);
157
158 while (it != mFdBuffer.end()) {
159 status_t err = stripField(it, field, mPolicy, spec);
160 if (err != NO_ERROR) return err;
161 if (field.size() > BUFFER_SIZE) { // rotate to another chunk if buffer size exceeds
162 mBuffers.push_back(field);
163 mSize += field.size();
164 field.clear();
165 }
166 }
167 if (!field.empty()) {
168 mBuffers.push_back(field);
169 mSize += field.size();
170 }
171 return NO_ERROR;
172}
173
174void
175EncodedBuffer::clear()
176{
177 mSize = 0;
178 mBuffers.clear();
179}
180
181size_t
182EncodedBuffer::size() const { return mSize; }
183
184status_t
185EncodedBuffer::flush(int fd)
186{
187 if (size() == mFdBuffer.size()) return mFdBuffer.flush(fd);
188
189 for (vector<vector<uint8_t>>::iterator it = mBuffers.begin(); it != mBuffers.end(); it++) {
190 status_t err = write_all(fd, it->data(), it->size());
191 if (err != NO_ERROR) return err;
192 }
193 return NO_ERROR;
Yi Jin0f047162017-09-05 13:44:22 -0700194}
195