blob: 07a064cf044b68f99361121e6944f83ab8c35d78 [file] [log] [blame]
Yi Jinc23fad22017-09-15 17:24:59 -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
18
19#include "PrivacyBuffer.h"
20#include "io_util.h"
21
22#include <android/util/protobuf.h>
23#include <deque>
24
25using namespace android::util;
26
27/**
28 * Write the field to buf based on the wire type, iterator will point to next field.
29 * If skip is set to true, no data will be written to buf. Return number of bytes written.
30 */
31static size_t
32write_field_or_skip(EncodedBuffer::iterator* iter, EncodedBuffer* buf, uint8_t wireType, bool skip)
33{
34 EncodedBuffer::Pointer snapshot = iter->rp()->copy();
35 size_t bytesToWrite = 0;
36 uint32_t varint = 0;
37 switch (wireType) {
38 case WIRE_TYPE_VARINT:
39 varint = iter->readRawVarint();
40 if(!skip) return buf->writeRawVarint(varint);
41 break;
42 case WIRE_TYPE_FIXED64:
43 bytesToWrite = 8;
44 break;
45 case WIRE_TYPE_LENGTH_DELIMITED:
46 bytesToWrite = iter->readRawVarint();
47 if(!skip) buf->writeRawVarint(bytesToWrite);
48 break;
49 case WIRE_TYPE_FIXED32:
50 bytesToWrite = 4;
51 break;
52 }
53 if (skip) {
54 iter->rp()->move(bytesToWrite);
55 } else {
56 for (size_t i=0; i<bytesToWrite; i++) {
57 *buf->writeBuffer() = iter->next();
58 buf->wp()->move();
59 }
60 }
61 return skip ? 0 : iter->rp()->pos() - snapshot.pos();
62}
63
64/**
65 * Strip next field based on its private policy and request spec, then stores data in buf.
66 * Return NO_ERROR if succeeds, otherwise BAD_VALUE is returned to indicate bad data in FdBuffer.
67 *
68 * The iterator must point to the head of a protobuf formatted field for successful operation.
69 * After exit with NO_ERROR, iterator points to the next protobuf field's head.
70 */
71static status_t
72stripField(EncodedBuffer::iterator* iter, EncodedBuffer* buf, const Privacy* parentPolicy, const PrivacySpec& spec)
73{
74 if (!iter->hasNext() || parentPolicy == NULL) return BAD_VALUE;
75 uint32_t varint = iter->readRawVarint();
76 uint8_t wireType = read_wire_type(varint);
77 uint32_t fieldId = read_field_id(varint);
78 const Privacy* policy = parentPolicy->lookup(fieldId);
79
80 if (policy == NULL || !policy->IsMessageType() || !policy->HasChildren()) {
81 bool skip = !spec.CheckPremission(policy);
82 size_t amt = buf->size();
83 if (!skip) amt += buf->writeHeader(fieldId, wireType);
84 amt += write_field_or_skip(iter, buf, wireType, skip); // point to head of next field
85 return buf->size() != amt ? BAD_VALUE : NO_ERROR;
86 }
87 // current field is message type and its sub-fields have extra privacy policies
88 deque<EncodedBuffer*> q;
89 uint32_t msgSize = iter->readRawVarint();
90 size_t finalSize = 0;
91 EncodedBuffer::Pointer start = iter->rp()->copy();
92 while (iter->rp()->pos() - start.pos() != msgSize) {
93 EncodedBuffer* v = new EncodedBuffer();
94 status_t err = stripField(iter, v, policy, spec);
95 if (err != NO_ERROR) return err;
96 if (v->size() == 0) continue;
97 q.push_back(v);
98 finalSize += v->size();
99 }
100
101 buf->writeHeader(fieldId, wireType);
102 buf->writeRawVarint(finalSize);
103 while (!q.empty()) {
104 EncodedBuffer* subField = q.front();
105 EncodedBuffer::iterator it = subField->begin();
106 while (it.hasNext()) {
107 *buf->writeBuffer() = it.next();
108 buf->wp()->move();
109 }
110 q.pop_front();
111 delete subField;
112 }
113 return NO_ERROR;
114}
115
116// ================================================================================
117PrivacyBuffer::PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator& data)
118 :mPolicy(policy),
119 mData(data),
120 mBuffer(0),
121 mSize(0)
122{
123}
124
125PrivacyBuffer::~PrivacyBuffer()
126{
127}
128
129status_t
130PrivacyBuffer::strip(const PrivacySpec& spec)
131{
132 // optimization when no strip happens
133 if (mPolicy == NULL || !mPolicy->HasChildren() || spec.RequireAll()) {
134 if (spec.CheckPremission(mPolicy)) mSize = mData.size();
135 return NO_ERROR;
136 }
137 while (mData.hasNext()) {
138 status_t err = stripField(&mData, &mBuffer, mPolicy, spec);
139 if (err != NO_ERROR) return err;
140 }
141 if (mData.bytesRead() != mData.size()) return BAD_VALUE;
142 mSize = mBuffer.size();
143 mData.rp()->rewind(); // rewind the read pointer back to beginning after the strip.
144 return NO_ERROR;
145}
146
147void
148PrivacyBuffer::clear()
149{
150 mSize = 0;
151 mBuffer.wp()->rewind();
152}
153
154size_t
155PrivacyBuffer::size() const { return mSize; }
156
157status_t
158PrivacyBuffer::flush(int fd)
159{
160 status_t err = NO_ERROR;
161 EncodedBuffer::iterator iter = size() == mData.size() ? mData : mBuffer.begin();
162 while (iter.readBuffer() != NULL) {
163 err = write_all(fd, iter.readBuffer(), iter.currentToRead());
164 iter.rp()->move(iter.currentToRead());
165 if (err != NO_ERROR) return err;
166 }
167 return NO_ERROR;
168}