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 }
31 
IsSystemAppByFullTokenID(uint64_t tokenId)32 bool TokenIdKit::IsSystemAppByFullTokenID(uint64_t tokenId)
33 {
34     ACCESSTOKEN_LOG_DEBUG(LABEL, "Called, tokenId=%{public}" PRId64, tokenId);
35     return (tokenId & SYSTEM_APP_MASK) == SYSTEM_APP_MASK;
36 }
37 
GetRenderTokenID(uint64_t tokenId)38 uint64_t TokenIdKit::GetRenderTokenID(uint64_t tokenId)
39 {
40     AccessTokenID id = tokenId & TOKEN_ID_LOWMASK;
41     if (id == INVALID_TOKENID) {
42         ACCESSTOKEN_LOG_ERROR(LABEL, "TokenID is invalid");
43         return tokenId;
44     }
45     AccessTokenIDInner *idInner = reinterpret_cast<AccessTokenIDInner *>(&id);
46     idInner->renderFlag = 1;
47 
48     id = *reinterpret_cast<AccessTokenID *>(idInner);
49     return static_cast<uint64_t>(id);
50 }
51 }  // namespace AccessToken
52 }  // namespace Security
53 }  // namespace OHOS
54