blob: 5db2239810e2fb2f5f5a55c83187b77bfbc82531 [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 "Privacy.h"
18
Yi Jinbdf58942017-11-14 17:58:19 -080019#include <android/os/IncidentReportArgs.h>
Yi Jin7e0b4e52017-09-12 20:00:25 -070020#include <stdlib.h>
21
Yi Jinbdf58942017-11-14 17:58:19 -080022uint64_t encode_field_id(const Privacy* p) { return (uint64_t)p->type << 32 | p->field_id; }
Yi Jin99c248f2017-08-25 18:11:58 -070023
Yi Jinbdf58942017-11-14 17:58:19 -080024const Privacy* lookup(const Privacy* p, uint32_t fieldId)
Yi Jin99c248f2017-08-25 18:11:58 -070025{
Yi Jinbdf58942017-11-14 17:58:19 -080026 if (p->children == NULL) return NULL;
27 for (int i=0; p->children[i] != NULL; i++) { // NULL-terminated.
28 if (p->children[i]->field_id == fieldId) return p->children[i];
29 // Incident section gen tool guarantees field ids in ascending order.
30 if (p->children[i]->field_id > fieldId) return NULL;
Yi Jin99c248f2017-08-25 18:11:58 -070031 }
32 return NULL;
33}
34
35static bool allowDest(const uint8_t dest, const uint8_t policy)
36{
37 switch (policy) {
Yi Jinbdf58942017-11-14 17:58:19 -080038 case android::os::DEST_LOCAL:
39 return dest == android::os::DEST_LOCAL;
40 case android::os::DEST_EXPLICIT:
41 case DEST_UNSET:
42 return dest == android::os::DEST_LOCAL ||
43 dest == android::os::DEST_EXPLICIT ||
44 dest == DEST_UNSET;
45 case android::os::DEST_AUTOMATIC:
Yi Jin99c248f2017-08-25 18:11:58 -070046 return true;
47 default:
48 return false;
49 }
50}
51
52bool
Yi Jin0f047162017-09-05 13:44:22 -070053PrivacySpec::operator<(const PrivacySpec& other) const
54{
55 return dest < other.dest;
56}
57
58bool
Yi Jinbdf58942017-11-14 17:58:19 -080059PrivacySpec::CheckPremission(const Privacy* privacy, const uint8_t defaultDest) const
Yi Jin99c248f2017-08-25 18:11:58 -070060{
Yi Jinbdf58942017-11-14 17:58:19 -080061 uint8_t policy = privacy != NULL ? privacy->dest : defaultDest;
Yi Jin99c248f2017-08-25 18:11:58 -070062 return allowDest(dest, policy);
63}
64
65bool
Yi Jinbdf58942017-11-14 17:58:19 -080066PrivacySpec::RequireAll() const { return dest == android::os::DEST_LOCAL; }
Yi Jin99c248f2017-08-25 18:11:58 -070067
Yi Jinbdf58942017-11-14 17:58:19 -080068PrivacySpec new_spec_from_args(int dest)
69{
Yi Jin0f047162017-09-05 13:44:22 -070070 if (dest < 0) return PrivacySpec();
71 return PrivacySpec(dest);
72}
73
Yi Jinbdf58942017-11-14 17:58:19 -080074PrivacySpec get_default_dropbox_spec() { return PrivacySpec(android::os::DEST_AUTOMATIC); }