114cf0368Sopenharmony_ci/* 214cf0368Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 314cf0368Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 414cf0368Sopenharmony_ci * you may not use this file except in compliance with the License. 514cf0368Sopenharmony_ci * You may obtain a copy of the License at 614cf0368Sopenharmony_ci * 714cf0368Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 814cf0368Sopenharmony_ci * 914cf0368Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1014cf0368Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1114cf0368Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1214cf0368Sopenharmony_ci * See the License for the specific language governing permissions and 1314cf0368Sopenharmony_ci * limitations under the License. 1414cf0368Sopenharmony_ci */ 1514cf0368Sopenharmony_ci#define LOG_TAG "EndianConverter" 1614cf0368Sopenharmony_ci#include "endian_converter.h" 1714cf0368Sopenharmony_ci 1814cf0368Sopenharmony_ci#include "error_code.h" 1914cf0368Sopenharmony_ci#include "logger.h" 2014cf0368Sopenharmony_ci#include "securec.h" 2114cf0368Sopenharmony_ci 2214cf0368Sopenharmony_cinamespace OHOS { 2314cf0368Sopenharmony_cinamespace UDMF { 2414cf0368Sopenharmony_cifloat HostToNet(float value) 2514cf0368Sopenharmony_ci{ 2614cf0368Sopenharmony_ci uint32_t temp; 2714cf0368Sopenharmony_ci auto err = memcpy_s(&temp, sizeof(temp), &value, sizeof(temp)); 2814cf0368Sopenharmony_ci if (err != E_OK) { 2914cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 3014cf0368Sopenharmony_ci } 3114cf0368Sopenharmony_ci temp = htole32(temp); 3214cf0368Sopenharmony_ci err = memcpy_s(&value, sizeof(value), &temp, sizeof(value)); 3314cf0368Sopenharmony_ci if (err != E_OK) { 3414cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 3514cf0368Sopenharmony_ci } 3614cf0368Sopenharmony_ci return value; 3714cf0368Sopenharmony_ci} 3814cf0368Sopenharmony_ci 3914cf0368Sopenharmony_cifloat NetToHost(float value) 4014cf0368Sopenharmony_ci{ 4114cf0368Sopenharmony_ci uint32_t temp; 4214cf0368Sopenharmony_ci auto err = memcpy_s(&temp, sizeof(temp), &value, sizeof(temp)); 4314cf0368Sopenharmony_ci if (err != E_OK) { 4414cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 4514cf0368Sopenharmony_ci } 4614cf0368Sopenharmony_ci temp = le32toh(temp); 4714cf0368Sopenharmony_ci err = memcpy_s(&value, sizeof(value), &temp, sizeof(value)); 4814cf0368Sopenharmony_ci if (err != E_OK) { 4914cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 5014cf0368Sopenharmony_ci } 5114cf0368Sopenharmony_ci return value; 5214cf0368Sopenharmony_ci} 5314cf0368Sopenharmony_ci 5414cf0368Sopenharmony_cidouble HostToNet(double value) 5514cf0368Sopenharmony_ci{ 5614cf0368Sopenharmony_ci uint64_t temp; 5714cf0368Sopenharmony_ci auto err = memcpy_s(&temp, sizeof(temp), &value, sizeof(temp)); 5814cf0368Sopenharmony_ci if (err != E_OK) { 5914cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 6014cf0368Sopenharmony_ci } 6114cf0368Sopenharmony_ci temp = htole64(temp); 6214cf0368Sopenharmony_ci err = memcpy_s(&value, sizeof(value), &temp, sizeof(value)); 6314cf0368Sopenharmony_ci if (err != E_OK) { 6414cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 6514cf0368Sopenharmony_ci } 6614cf0368Sopenharmony_ci return value; 6714cf0368Sopenharmony_ci} 6814cf0368Sopenharmony_ci 6914cf0368Sopenharmony_cidouble NetToHost(double value) 7014cf0368Sopenharmony_ci{ 7114cf0368Sopenharmony_ci uint64_t temp; 7214cf0368Sopenharmony_ci auto err = memcpy_s(&temp, sizeof(temp), &value, sizeof(temp)); 7314cf0368Sopenharmony_ci if (err != E_OK) { 7414cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 7514cf0368Sopenharmony_ci } 7614cf0368Sopenharmony_ci temp = le64toh(temp); 7714cf0368Sopenharmony_ci err = memcpy_s(&value, sizeof(value), &temp, sizeof(value)); 7814cf0368Sopenharmony_ci if (err != E_OK) { 7914cf0368Sopenharmony_ci LOG_ERROR(UDMF_KITS_NAPI, "memcpy_s error: %{public}d", err); 8014cf0368Sopenharmony_ci } 8114cf0368Sopenharmony_ci return value; 8214cf0368Sopenharmony_ci} 8314cf0368Sopenharmony_ci} // namespace UDMF 8414cf0368Sopenharmony_ci} // namespace OHOS