1fc223305Sopenharmony_ci/*
2fc223305Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc223305Sopenharmony_ci * You may obtain a copy of the License at
6fc223305Sopenharmony_ci *
7fc223305Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc223305Sopenharmony_ci *
9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and
13fc223305Sopenharmony_ci * limitations under the License.
14fc223305Sopenharmony_ci */
15fc223305Sopenharmony_ci#include "preferences_utils.h"
16fc223305Sopenharmony_ci
17fc223305Sopenharmony_ci#include <string>
18fc223305Sopenharmony_ci
19fc223305Sopenharmony_ci#include "log_print.h"
20fc223305Sopenharmony_ci#include "preferences_errno.h"
21fc223305Sopenharmony_ci#include "preferences_value.h"
22fc223305Sopenharmony_ci
23fc223305Sopenharmony_cinamespace OHOS {
24fc223305Sopenharmony_cinamespace NativePreferences {
25fc223305Sopenharmony_ci
26fc223305Sopenharmony_cistd::string MakeFilePath(const std::string &prefPath, const std::string &suffix)
27fc223305Sopenharmony_ci{
28fc223305Sopenharmony_ci    return prefPath + suffix;
29fc223305Sopenharmony_ci}
30fc223305Sopenharmony_ci
31fc223305Sopenharmony_ciint CheckKey(const std::string &key)
32fc223305Sopenharmony_ci{
33fc223305Sopenharmony_ci    if (key.empty()) {
34fc223305Sopenharmony_ci        LOG_ERROR("The key string is null or empty.");
35fc223305Sopenharmony_ci        return E_KEY_EMPTY;
36fc223305Sopenharmony_ci    }
37fc223305Sopenharmony_ci    if (MAX_KEY_LENGTH < key.length()) {
38fc223305Sopenharmony_ci        LOG_ERROR("The key string length should shorter than 1024.");
39fc223305Sopenharmony_ci        return E_KEY_EXCEED_MAX_LENGTH;
40fc223305Sopenharmony_ci    }
41fc223305Sopenharmony_ci    return E_OK;
42fc223305Sopenharmony_ci}
43fc223305Sopenharmony_ci
44fc223305Sopenharmony_ciint CheckValue(const PreferencesValue &value)
45fc223305Sopenharmony_ci{
46fc223305Sopenharmony_ci    auto lengthCheck = [] (uint32_t length, const std::string &errMsg) {
47fc223305Sopenharmony_ci        if (MAX_VALUE_LENGTH < length) {
48fc223305Sopenharmony_ci            LOG_ERROR("%{public}s", errMsg.c_str());
49fc223305Sopenharmony_ci            return E_VALUE_EXCEED_MAX_LENGTH;
50fc223305Sopenharmony_ci        }
51fc223305Sopenharmony_ci        return E_OK;
52fc223305Sopenharmony_ci    };
53fc223305Sopenharmony_ci
54fc223305Sopenharmony_ci    if (value.IsString()) {
55fc223305Sopenharmony_ci        std::string val = value;
56fc223305Sopenharmony_ci        return lengthCheck(val.length(), "the value string length should shorter than 16 * 1024 * 1024.");
57fc223305Sopenharmony_ci    }
58fc223305Sopenharmony_ci
59fc223305Sopenharmony_ci    if (value.IsObject()) {
60fc223305Sopenharmony_ci        Object obj = value;
61fc223305Sopenharmony_ci        return lengthCheck(obj.valueStr.length(), "the length of the object converted to JSON should be less than 16 *"
62fc223305Sopenharmony_ci                                                  " 1024 * 1024");
63fc223305Sopenharmony_ci    }
64fc223305Sopenharmony_ci
65fc223305Sopenharmony_ci    if (value.IsBigInt()) {
66fc223305Sopenharmony_ci        BigInt bigint = value;
67fc223305Sopenharmony_ci        if (bigint.words_.empty()) {
68fc223305Sopenharmony_ci            LOG_ERROR("BigInt words cannot be empty.");
69fc223305Sopenharmony_ci            return E_ERROR;
70fc223305Sopenharmony_ci        }
71fc223305Sopenharmony_ci        return E_OK;
72fc223305Sopenharmony_ci    }
73fc223305Sopenharmony_ci    return E_OK;
74fc223305Sopenharmony_ci}
75fc223305Sopenharmony_ci} // End of namespace NativePreferences
76fc223305Sopenharmony_ci} // End of namespace OHOS
77