blob: 03faa9290666a08206428049dedc35356530fdbc [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
Yi Jinbdf58942017-11-14 17:58:19 -080017#define LOG_TAG "incidentd"
Yi Jinc23fad22017-09-15 17:24:59 -070018
19#include "PrivacyBuffer.h"
20#include "io_util.h"
21
22#include <android/util/protobuf.h>
Yi Jinbdf58942017-11-14 17:58:19 -080023#include <cutils/log.h>
Yi Jinc23fad22017-09-15 17:24:59 -070024
25using namespace android::util;
26
Yi Jinbdf58942017-11-14 17:58:19 -080027const bool DEBUG = false;
28
Yi Jinc23fad22017-09-15 17:24:59 -070029/**
30 * Write the field to buf based on the wire type, iterator will point to next field.
31 * If skip is set to true, no data will be written to buf. Return number of bytes written.
32 */
Yi Jin42711a02017-10-11 18:20:24 -070033void
34PrivacyBuffer::writeFieldOrSkip(uint32_t fieldTag, bool skip)
Yi Jinc23fad22017-09-15 17:24:59 -070035{
Yi Jinbdf58942017-11-14 17:58:19 -080036 if (DEBUG) ALOGD("%s field %d (wiretype = %d)", skip ? "skip" : "write",
37 read_field_id(fieldTag), read_wire_type(fieldTag));
38
Yi Jin42711a02017-10-11 18:20:24 -070039 uint8_t wireType = read_wire_type(fieldTag);
Yi Jinc23fad22017-09-15 17:24:59 -070040 size_t bytesToWrite = 0;
Yi Jin42711a02017-10-11 18:20:24 -070041 uint32_t varint = 0;
42
Yi Jinc23fad22017-09-15 17:24:59 -070043 switch (wireType) {
44 case WIRE_TYPE_VARINT:
Yi Jin42711a02017-10-11 18:20:24 -070045 varint = mData.readRawVarint();
46 if (!skip) {
47 mProto.writeRawVarint(fieldTag);
48 mProto.writeRawVarint(varint);
49 }
50 return;
Yi Jinc23fad22017-09-15 17:24:59 -070051 case WIRE_TYPE_FIXED64:
Yi Jin42711a02017-10-11 18:20:24 -070052 if (!skip) mProto.writeRawVarint(fieldTag);
Yi Jinc23fad22017-09-15 17:24:59 -070053 bytesToWrite = 8;
54 break;
55 case WIRE_TYPE_LENGTH_DELIMITED:
Yi Jin42711a02017-10-11 18:20:24 -070056 bytesToWrite = mData.readRawVarint();
57 if(!skip) mProto.writeLengthDelimitedHeader(read_field_id(fieldTag), bytesToWrite);
Yi Jinc23fad22017-09-15 17:24:59 -070058 break;
59 case WIRE_TYPE_FIXED32:
Yi Jin42711a02017-10-11 18:20:24 -070060 if (!skip) mProto.writeRawVarint(fieldTag);
Yi Jinc23fad22017-09-15 17:24:59 -070061 bytesToWrite = 4;
62 break;
63 }
Yi Jinbdf58942017-11-14 17:58:19 -080064 if (DEBUG) ALOGD("%s %d bytes of data", skip ? "skip" : "write", (int)bytesToWrite);
Yi Jinc23fad22017-09-15 17:24:59 -070065 if (skip) {
Yi Jin42711a02017-10-11 18:20:24 -070066 mData.rp()->move(bytesToWrite);
Yi Jinc23fad22017-09-15 17:24:59 -070067 } else {
68 for (size_t i=0; i<bytesToWrite; i++) {
Yi Jin42711a02017-10-11 18:20:24 -070069 mProto.writeRawByte(mData.next());
Yi Jinc23fad22017-09-15 17:24:59 -070070 }
71 }
Yi Jinc23fad22017-09-15 17:24:59 -070072}
73
74/**
75 * Strip next field based on its private policy and request spec, then stores data in buf.
76 * Return NO_ERROR if succeeds, otherwise BAD_VALUE is returned to indicate bad data in FdBuffer.
77 *
78 * The iterator must point to the head of a protobuf formatted field for successful operation.
79 * After exit with NO_ERROR, iterator points to the next protobuf field's head.
80 */
Yi Jin42711a02017-10-11 18:20:24 -070081status_t
82PrivacyBuffer::stripField(const Privacy* parentPolicy, const PrivacySpec& spec)
Yi Jinc23fad22017-09-15 17:24:59 -070083{
Yi Jin42711a02017-10-11 18:20:24 -070084 if (!mData.hasNext() || parentPolicy == NULL) return BAD_VALUE;
85 uint32_t fieldTag = mData.readRawVarint();
Yi Jinbdf58942017-11-14 17:58:19 -080086 const Privacy* policy = lookup(parentPolicy, read_field_id(fieldTag));
Yi Jin42711a02017-10-11 18:20:24 -070087
Yi Jinbdf58942017-11-14 17:58:19 -080088 if (policy == NULL || policy->children == NULL) {
89 if (DEBUG) ALOGD("Not a message field %d: dest(%d)", read_field_id(fieldTag),
90 policy != NULL ? policy->dest : parentPolicy->dest);
91
92 bool skip = !spec.CheckPremission(policy, parentPolicy->dest);
Yi Jin42711a02017-10-11 18:20:24 -070093 // iterator will point to head of next field
94 writeFieldOrSkip(fieldTag, skip);
95 return NO_ERROR;
Yi Jinc23fad22017-09-15 17:24:59 -070096 }
97 // current field is message type and its sub-fields have extra privacy policies
Yi Jin42711a02017-10-11 18:20:24 -070098 uint32_t msgSize = mData.readRawVarint();
99 EncodedBuffer::Pointer start = mData.rp()->copy();
Yi Jinbdf58942017-11-14 17:58:19 -0800100 long long token = mProto.start(encode_field_id(policy));
Yi Jin42711a02017-10-11 18:20:24 -0700101 while (mData.rp()->pos() - start.pos() != msgSize) {
Yi Jin42711a02017-10-11 18:20:24 -0700102 status_t err = stripField(policy, spec);
Yi Jinc23fad22017-09-15 17:24:59 -0700103 if (err != NO_ERROR) return err;
Yi Jinc23fad22017-09-15 17:24:59 -0700104 }
Yi Jinbe6de302017-10-24 12:30:24 -0700105 mProto.end(token);
Yi Jinc23fad22017-09-15 17:24:59 -0700106 return NO_ERROR;
107}
108
109// ================================================================================
110PrivacyBuffer::PrivacyBuffer(const Privacy* policy, EncodedBuffer::iterator& data)
111 :mPolicy(policy),
112 mData(data),
Yi Jin42711a02017-10-11 18:20:24 -0700113 mProto(),
Yi Jinc23fad22017-09-15 17:24:59 -0700114 mSize(0)
115{
116}
117
118PrivacyBuffer::~PrivacyBuffer()
119{
120}
121
122status_t
123PrivacyBuffer::strip(const PrivacySpec& spec)
124{
Yi Jinbdf58942017-11-14 17:58:19 -0800125 if (DEBUG) ALOGD("Strip with spec %d", spec.dest);
Yi Jinc23fad22017-09-15 17:24:59 -0700126 // optimization when no strip happens
Yi Jinbdf58942017-11-14 17:58:19 -0800127 if (mPolicy == NULL || mPolicy->children == NULL || spec.RequireAll()) {
Yi Jinc23fad22017-09-15 17:24:59 -0700128 if (spec.CheckPremission(mPolicy)) mSize = mData.size();
129 return NO_ERROR;
130 }
131 while (mData.hasNext()) {
Yi Jin42711a02017-10-11 18:20:24 -0700132 status_t err = stripField(mPolicy, spec);
Yi Jinc23fad22017-09-15 17:24:59 -0700133 if (err != NO_ERROR) return err;
134 }
135 if (mData.bytesRead() != mData.size()) return BAD_VALUE;
Yi Jin42711a02017-10-11 18:20:24 -0700136 mSize = mProto.size();
Yi Jinc23fad22017-09-15 17:24:59 -0700137 mData.rp()->rewind(); // rewind the read pointer back to beginning after the strip.
138 return NO_ERROR;
139}
140
141void
142PrivacyBuffer::clear()
143{
144 mSize = 0;
Yi Jin42711a02017-10-11 18:20:24 -0700145 mProto = ProtoOutputStream();
Yi Jinc23fad22017-09-15 17:24:59 -0700146}
147
148size_t
149PrivacyBuffer::size() const { return mSize; }
150
151status_t
152PrivacyBuffer::flush(int fd)
153{
154 status_t err = NO_ERROR;
Yi Jin42711a02017-10-11 18:20:24 -0700155 EncodedBuffer::iterator iter = size() == mData.size() ? mData : mProto.data();
Yi Jinc23fad22017-09-15 17:24:59 -0700156 while (iter.readBuffer() != NULL) {
157 err = write_all(fd, iter.readBuffer(), iter.currentToRead());
158 iter.rp()->move(iter.currentToRead());
159 if (err != NO_ERROR) return err;
160 }
161 return NO_ERROR;
162}