1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "tokenid_kit.h"
17
18 #include <cinttypes>
19
20 #include "accesstoken_log.h"
21 #include "access_token.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenIdKit"};
28 static const uint64_t SYSTEM_APP_MASK = (static_cast<uint64_t>(1) << 32);
29 static const uint64_t TOKEN_ID_LOWMASK = 0xffffffff;
30 }
IsSystemAppByFullTokenID(uint64_t tokenId)31 bool TokenIdKit::IsSystemAppByFullTokenID(uint64_t tokenId)
32 {
33 ACCESSTOKEN_LOG_DEBUG(LABEL, "called, tokenId=%{public}" PRId64, tokenId);
34 return (tokenId & SYSTEM_APP_MASK) == SYSTEM_APP_MASK;
35 }
GetRenderTokenID(uint64_t tokenId)36 uint64_t TokenIdKit::GetRenderTokenID(uint64_t tokenId)
37 {
38 AccessTokenID id = tokenId & TOKEN_ID_LOWMASK;
39 if (id == INVALID_TOKENID) {
40 ACCESSTOKEN_LOG_ERROR(LABEL, "tokenID is invalid");
41 return tokenId;
42 }
43 AccessTokenIDInner *idInner = reinterpret_cast<AccessTokenIDInner *>(&id);
44 idInner->renderFlag = 1;
45
46 id = *reinterpret_cast<AccessTokenID *>(idInner);
47 return static_cast<uint64_t>(id);
48 }
49 } // namespace AccessToken
50 } // namespace Security
51 } // namespace OHOS
52