1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci#include "uid_range.h"
16b1b8bc3fSopenharmony_ci
17b1b8bc3fSopenharmony_ci#include "netnative_log_wrapper.h"
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_cinamespace OHOS {
20b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
21b1b8bc3fSopenharmony_ci
22b1b8bc3fSopenharmony_cinamespace {
23b1b8bc3fSopenharmony_ciconstexpr int32_t INVLID_VALUE = -1;
24b1b8bc3fSopenharmony_ci}
25b1b8bc3fSopenharmony_ci
26b1b8bc3fSopenharmony_ciUidRange::UidRange(int32_t begin, int32_t end) : begin_(begin), end_(end) {}
27b1b8bc3fSopenharmony_ci
28b1b8bc3fSopenharmony_ciuint32_t UidRange::Size() const
29b1b8bc3fSopenharmony_ci{
30b1b8bc3fSopenharmony_ci    if (begin_ == INVLID_VALUE || end_ == INVLID_VALUE || end_ < begin_) {
31b1b8bc3fSopenharmony_ci        return 0;
32b1b8bc3fSopenharmony_ci    }
33b1b8bc3fSopenharmony_ci    return static_cast<uint32_t>(end_ - begin_ + 1);
34b1b8bc3fSopenharmony_ci}
35b1b8bc3fSopenharmony_ci
36b1b8bc3fSopenharmony_cibool UidRange::Marshalling(Parcel &parcel) const
37b1b8bc3fSopenharmony_ci{
38b1b8bc3fSopenharmony_ci    return parcel.WriteInt32(begin_) && parcel.WriteInt32(end_);
39b1b8bc3fSopenharmony_ci}
40b1b8bc3fSopenharmony_ci
41b1b8bc3fSopenharmony_cisptr<UidRange> UidRange::Unmarshalling(Parcel &parcel)
42b1b8bc3fSopenharmony_ci{
43b1b8bc3fSopenharmony_ci    sptr<UidRange> ptr = new (std::nothrow) UidRange();
44b1b8bc3fSopenharmony_ci    if (ptr == nullptr) {
45b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("UidRange Unmarshalling new object is failed.");
46b1b8bc3fSopenharmony_ci        return nullptr;
47b1b8bc3fSopenharmony_ci    }
48b1b8bc3fSopenharmony_ci
49b1b8bc3fSopenharmony_ci    bool allOK = parcel.ReadInt32(ptr->begin_) && parcel.ReadInt32(ptr->end_);
50b1b8bc3fSopenharmony_ci    return allOK ? ptr : nullptr;
51b1b8bc3fSopenharmony_ci}
52b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
53b1b8bc3fSopenharmony_ci} // namespace OHOS