1 /* 2 * Copyright (c) 2023 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 "base_config.h" 17 18 #include "nlohmann/json.hpp" 19 20 #include "security_guard_log.h" 21 22 namespace OHOS::Security::SecurityGuard { 23 namespace { 24 constexpr int32_t CFG_FILE_MAX_SIZE = 1 * 1024 * 1024; // byte 25 } 26 Check()27bool BaseConfig::Check() 28 { 29 if (!stream_.is_open() ||!stream_) { 30 SGLOGE("stream error"); 31 return false; 32 } 33 34 stream_.seekg(0, std::ios::end); 35 int len = static_cast<int>(stream_.tellg()); 36 if (len == 0 || len > CFG_FILE_MAX_SIZE) { 37 SGLOGE("stream is empty or too large, len = %{public}d", len); 38 stream_.close(); 39 return false; 40 } 41 stream_.seekg(0, std::ios::beg); 42 return true; 43 } 44 ~BaseConfig()45BaseConfig::~BaseConfig() 46 { 47 if (stream_.is_open()) { 48 stream_.close(); 49 } 50 } 51 } // OHOS::Security::SecurityGuard 52