summaryrefslogtreecommitdiff
path: root/cmds/incidentd/src/Privacy.cpp
blob: 44adaecfe97f136a1dff4fcc7e99085c8fb02fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Privacy.h"

#include <android/os/IncidentReportArgs.h>
#include <stdlib.h>

uint64_t encode_field_id(const Privacy* p) { return (uint64_t)p->type << 32 | p->field_id; }

const Privacy* lookup(const Privacy* p, uint32_t fieldId)
{
    if (p->children == NULL) return NULL;
    for (int i=0; p->children[i] != NULL; i++) { // NULL-terminated.
        if (p->children[i]->field_id == fieldId) return p->children[i];
        // Incident section gen tool guarantees field ids in ascending order.
        if (p->children[i]->field_id > fieldId) return NULL;
    }
    return NULL;
}

static bool allowDest(const uint8_t dest, const uint8_t policy)
{
    switch (policy) {
    case android::os::DEST_LOCAL:
        return dest == android::os::DEST_LOCAL;
    case android::os::DEST_EXPLICIT:
    case DEST_UNSET:
        return dest == android::os::DEST_LOCAL ||
            dest == android::os::DEST_EXPLICIT ||
            dest == DEST_UNSET;
    case android::os::DEST_AUTOMATIC:
        return true;
    default:
        return false;
    }
}

bool
PrivacySpec::operator<(const PrivacySpec& other) const
{
  return dest < other.dest;
}

bool
PrivacySpec::CheckPremission(const Privacy* privacy, const uint8_t defaultDest) const
{
    uint8_t policy = privacy != NULL ? privacy->dest : defaultDest;
    return allowDest(dest, policy);
}

bool
PrivacySpec::RequireAll() const { return dest == android::os::DEST_LOCAL; }

PrivacySpec new_spec_from_args(int dest)
{
    switch (dest) {
        case android::os::DEST_AUTOMATIC:
        case android::os::DEST_EXPLICIT:
        case android::os::DEST_LOCAL:
            return PrivacySpec(dest);
        default:
            return PrivacySpec();
    }
}

PrivacySpec get_default_dropbox_spec() { return PrivacySpec(android::os::DEST_AUTOMATIC); }