diff --git a/BUILD.gn b/BUILD.gn new file mode 100755 index 0000000000000000000000000000000000000000..5d8d98054f3787ec53faa10b701467f0b058b71e --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +config("pubilc") { + include_dirs = [ + "include", + ] +} + +sources_platform_common = [ + "./src/syscap_tool.c", + "./src/endian_internal.c", +] + +ohos_executable("syscap_tool") { + install_enable = true + include_dirs = [ "src" ] + public_configs = [ ":pubilc" ] + defines = [] + + if (is_mingw) { + defines += [ "_POSIX_" ] + } + + sources = [ "./src/main.c" ] + sources += sources_platform_common + + deps = [ + "//third_party/bounds_checking_function:libsec_static", + ] + + if (defined(ohos_lite)) { + deps += [ "//build/lite/config/component/cJSON:cjson_static" ] + } else { + deps += [ "//third_party/cJSON:cjson_static" ] + } + + subsystem_name = "developtools" + part_name = "syscap_codec" +} + +ohos_shared_library("syscap_tool_shared") { + output_name = "syscap_codec" + include_dirs = [ "src" ] + public_configs = [ ":pubilc" ] + + sources = sources_platform_common + + deps = [ "//third_party/bounds_checking_function:libsec_static" ] + + if (defined(ohos_lite)) { + deps += [ "//build/lite/config/component/cJSON:cjson_static" ] + } else { + deps += [ "//third_party/cJSON:cjson_static" ] + } + + subsystem_name = "developtools" + part_name = "syscap_codec" +} + +group("syscap_codec") { + deps = [ ":syscap_tool_shared" ] +} \ No newline at end of file diff --git a/README.md b/README.md index 7c6bd3a99d472a3f6b65dcfed406c3fb12248f6e..3d74be289154563806cc9cf3b5230ff9e7594950 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,89 @@ -# developtools_syscap_codec +# SysCap编解码工具 -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} +- [SysCap编解码工具](#) + - [简介](#简介) + - [架构](#架构) + - [目录](#目录) + - [pc端编译说明](#pc端编译说明) + - [pc端获取说明](#pc端获取说明) + - [命令帮助](#命令帮助) -#### 软件架构 -软件架构说明 +## 简介 +SysCap编解码工具的应用场景: -#### 安装教程 +1.设备开发时,设备厂商对OS不见进行拼装后,编译构建生成Syscap列表,厂商ID和API版本信息作为Syscap编解码工具的输入,生成PCID。 -1. xxxx -2. xxxx -3. xxxx +2.应用开发时,IDE根据应用配置的Syscap和API版本生成RPCID。开发者导入PCID时,使用Syscap编解码工具解码出设备的Syscap集合。 -#### 使用说明 +3.应用分发时,应用市场使用Syscap工具对应用RPCID和设备的PCID进行解码。 -1. xxxx -2. xxxx -3. xxxx +4.应用安装时,包管理使用Syscap工具解码应用需要的Syscap集合。 -#### 参与贡献 +## 架构 -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +SysCap编解码工具主要有两部分组成: +1. syscap_tool部分:运用于应用开发场景,供IDE使用,用于编码生成RPCID和解码PCID -#### 特技 +2. syscap_tool_shared部分:用于设备开发、应用安装等场景,完成RPCID和PCID的编解码。 + +## 目录 + +``` +/developtools +├── syscap_codec # syscap codec代码目录 +│ ├── include +│ │ └── syscap_tool.h # syscap_tool_shared对外接口定义 +│ └── src +│ ├── endian_internel.h # 内部实现的大小端转换接口定义(便于win、mac、linux平台通用) +│ ├── endian_internel.c # 大小端转换实现 +│ ├── main.c # syscap_tool命令行工具代码实现 +│ └── syscap_tool.c # syscap_tool编解码接口的实现 +``` + +### pc端编译说明 + + +syscap_tool pc端可执行文件编译步骤: + +1. 编译命令:编译sdk命令 请参考https://gitee.com/openharmony/build/blob/master/README_zh.md 仓编译sdk说明, 执行其指定的sdk编译命令来编译整个sdk, syscap_tool会被编译打包到里面。 + +2. 编译:在目标开发机上运行上面调整好的sdk编译命令, 正常编译syscap_tool会输出到sdk平台相关目录下; 注意: ubuntu环境下只能编译windows/linux版本工具,mac版需要在macos开发机上编译。 + +### pc端获取说明 + +[1.下载sdk获取(建议)](#section161941989591) +``` +通过访问本社区网站下载dailybuilds或正式发布的sdk压缩包,从中根据自己平台到相应的目录toolchain下解压提取 +``` + +[2.自行编译](#section161941989592) + +编译请参考上面单独小节,本项目仓prebuilt目录下不再提供预制 + + +[3.支持运行环境](#section161941989593) + +linux版本建议ubuntu 18.04以上 64位,其他相近版本也可。 + +### 命令帮助 + +使用./syscap_tool -h或者./syscap_tool --help查看: + +./syscap_tool -R/P -e/d -i filepath [-o outpath] + +-h, --help : how to use + +-R, --RPCID : encode or decode RPCID + +-P, --PCID : encode or decode PCID + +-e, --encode : to encode + +-d, --encode : to decode + +-i filepath, --input filepath : input file + +-o outpath, --input outpath : output path -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/bundle.json b/bundle.json new file mode 100755 index 0000000000000000000000000000000000000000..fb5544db9555cc20a1c944e3e031779f9af8be52 --- /dev/null +++ b/bundle.json @@ -0,0 +1,31 @@ +{ + "name": "@ohos/syscap_codec", + "description": "SystemCapability encode/decode.", + "version": "3.1", + "license": "Apache License 2.0", + "pubiishAs": "code-segment", + "segment": { + "destPath": "developtools/syscap_codec" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "syscap_codec", + "subsystem": "developtools", + "syscap": [], + "feature": [], + "adapted_system_type": [ "small", "standard" ], + "deps": { + "components": [], + "third_party": [ + "bounds_checking_function", + "thirdparty_cjson" + ] + }, + "build": { + "sub_component": [ "//developtools/syscap_codec:syscap_codec" ], + "inner_kits": [], + "test": [] + } + } +} \ No newline at end of file diff --git a/include/syscap_tool.h b/include/syscap_tool.h new file mode 100755 index 0000000000000000000000000000000000000000..a1b0917611d564cdd6c0506fb8a1047afdc24624 --- /dev/null +++ b/include/syscap_tool.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYSCAP_TOOL_H +#define _SYSCAP_TOOL_H + +#include "stdint.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/* in: inputFile, out: outputPath/.sc */ +int32_t PCIDEncode(char *inputFile, char *outputPath); +/* in: inputFile, file name format: .sc, out: outputPath/.json */ +int32_t PCIDDecode(char *inputFile, char *outputPath); +/* in: inputFile, out: outputPath/rpcid.sc */ +int32_t RPCIDEncode(char *inputFile, char *outputPath); +/* in: inputFile, out: outputPath/rpcid.json */ +int32_t RPCIDDecode(char *inputFile, char *outputPath); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _SYSCAP_TOOL_H */ \ No newline at end of file diff --git a/src/endian_internal.c b/src/endian_internal.c new file mode 100755 index 0000000000000000000000000000000000000000..5af5a9df28eb2004efe43953093f3ba4134188b8 --- /dev/null +++ b/src/endian_internal.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "endian_internal.h" +#include + +#define B_L_SWAP16(A) ((((uint16_t)(A) & 0xff00) >> 8) | (((uint16_t)(A) & 0x00ff) << 8)) +#define B_L_SWAP32(A) ((((uint32_t)(A) & 0xff000000) >> 24) | (((uint32_t)(A) & 0x00ff0000) >> 8) | \ + (((uint32_t)(A) & 0x0000ff00) << 8) | (((uint32_t)(A) & 0x000000ff) << 24)) + +int32_t CheckEndian() +{ + union { + int32_t i; + uint8_t s[4]; + } c; + c.i = 0x12345678; + + return (0x12 == c.s[0]); +} + +uint32_t HtonlInter(uint32_t h) +{ + return CheckEndian() ? h : B_L_SWAP32(h); +} + +uint32_t NtohlInter(uint32_t n) +{ + return CheckEndian() ? n : B_L_SWAP32(n); +} + +uint16_t HtonsInter(uint16_t h) +{ + return CheckEndian() ? h : B_L_SWAP16(h); +} + +uint16_t NtohsInter(uint16_t n) +{ + return CheckEndian() ? n : B_L_SWAP16(n); +} \ No newline at end of file diff --git a/src/endian_internal.h b/src/endian_internal.h new file mode 100755 index 0000000000000000000000000000000000000000..dd9728523c5d1c90bf49bd55b1b52ed31c731426 --- /dev/null +++ b/src/endian_internal.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _ENDIAN_INTERNAL_H +#define _ENDIAN_INTERNAL_H + +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +extern uint32_t HtonlInter(uint32_t h); +extern uint32_t NtohlInter(uint32_t n); +extern uint16_t HtonsInter(uint16_t h); +extern uint16_t NtohsInter(uint16_t n); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _ENDIAN_INTERNAL_H */ \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100755 index 0000000000000000000000000000000000000000..bd902cefc2744b1a636962c283e51f85b7a08b10 --- /dev/null +++ b/src/main.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "syscap_tool.h" +#include +#include +#include +#include +#include +#include + +extern char *optarg; + +int main(int argc, char **argv) +{ + int32_t ret, optIndex; + char curpath[PATH_MAX] = {0}; + int32_t rpcid = 0; + int32_t pcid = 0; + int32_t encode = 0; + int32_t decode = 0; + int32_t help = 0; + char *inputfile = NULL; + char *outputpath = getcwd(curpath, sizeof(curpath)); + + while (1) { + static struct option long_options[] = { + {"help", no_argument, 0, 'h' }, + {"RPCID", no_argument, 0, 'R' }, + {"PCID", no_argument, 0, 'P' }, + {"encode", no_argument, 0, 'e' }, + {"decode", no_argument, 0, 'd' }, + {"input", required_argument, 0, 'i' }, + {"output", required_argument, 0, 'o' }, + {0, 0, 0, 0 } + }; + + int32_t flag = getopt_long(argc, argv, "hRPedi:o:", long_options, &optIndex); + if (flag == -1) { + break; + } + switch (flag) { + case 'e': + encode = 1; + break; + case 'd': + decode = 1; + break; + case 'R': + rpcid = 1; + break; + case 'P': + pcid = 1; + break; + case 'i': + inputfile = optarg; + break; + case 'o': + outputpath = optarg; + break; + case 'h': + default: + help = 1; + } + } + + if (rpcid && !pcid && encode && !decode && inputfile && !help) { + ret = RPCIDEncode(inputfile, outputpath); + } else if (rpcid && !pcid && !encode && decode && inputfile && !help) { + ret = RPCIDDecode(inputfile, outputpath); + } else if (!rpcid && pcid && encode && !decode && inputfile && !help) { + ret = PCIDEncode(inputfile, outputpath); + } else if (!rpcid && pcid && !encode && decode && inputfile && !help) { + ret = PCIDDecode(inputfile, outputpath); + } else { + printf("syscap_tool -R/P -e/d -i filepath [-o outpath]\n"); + printf("-h, --help : how to use\n"); + printf("-R, --RPCID : encode or decode RPCID\n"); + printf("-P, --PCID : encode or decode PCID\n"); + printf("-e, --encode : to encode\n"); + printf("-d, --encode : to decode\n"); + printf("-i filepath, --input filepath : input file\n"); + printf("-o outpath, --input outpath : output path\n"); + exit(0); + } + + if (ret != 0) { + printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); + printf("input file(%s) prase failed\n", inputfile); + } + + return 0; +} \ No newline at end of file diff --git a/src/syscap_tool.c b/src/syscap_tool.c new file mode 100755 index 0000000000000000000000000000000000000000..f9bd31053436012b0141529b70c0667bf40a9bd7 --- /dev/null +++ b/src/syscap_tool.c @@ -0,0 +1,795 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "syscap_tool.h" +#include "endian_internal.h" +#include "cJSON.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct ProductCompatibilityIDHead { + uint16_t apiVersion : 15; + uint16_t apiVersionType : 1; + uint16_t systemType : 3; + uint16_t reserved : 13; + uint32_t manufacturerID; +} PCIDHead; + +typedef struct RequiredProductCompatibilityIDHead { + uint16_t apiVersion : 15; + uint16_t apiVersionType : 1; +} RPCIDHead; + +#define SINGLE_FEAT_LENGTH (32 * 8) + +#define PRINT_ERR(...) \ + do { \ + printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + } while (0); + +static void FreeContextBuffer(char *contextBuffer) +{ + (void)free(contextBuffer); +} + +static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *bufferLen) +{ + uint32_t ret; + FILE *fp = NULL; + struct stat statBuf; + char *contextBuffer = NULL; + + ret = stat(inputFile, &statBuf); + if (ret != 0) { + PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno); + return -1; + } + if (!(statBuf.st_mode & S_IRUSR)) { + PRINT_ERR("don't have permission to read the file(%s)\n", inputFile); + return -1; + } + contextBuffer = (char *)malloc(statBuf.st_size + 1); + if (contextBuffer == NULL) { + PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); + return -1; + } + fp = fopen(inputFile, "r"); + if (fp == NULL) { + PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno); + FreeContextBuffer(contextBuffer); + return -1; + } + ret = fread(contextBuffer, statBuf.st_size, 1, fp); + if (ret != 1) { + PRINT_ERR("read file(%s) failed, errno = %d\n", inputFile, errno); + FreeContextBuffer(contextBuffer); + fclose(fp); + return -1; + } + contextBuffer[statBuf.st_size] = '\0'; + fclose(fp); + + *contextBufPtr = contextBuffer; + *bufferLen = statBuf.st_size + 1; + return 0; +} + +static int32_t ConvertedContextSaveAsFile(char *outDirPath, char *filename, char *convertedBuffer, uint32_t bufferLen) +{ + int32_t ret; + FILE *fp = NULL; + char fileFullPath[PATH_MAX] = {0}; + int32_t pathLen = strlen(outDirPath); + + ret = strncpy_s(fileFullPath, PATH_MAX, outDirPath, pathLen + 1); + if (ret != 0) { + PRINT_ERR("strncpy_s failed, source string:%s, len = %d, errno = %d\n", outDirPath, pathLen + 1, errno); + return -1; + } + + if (fileFullPath[pathLen - 1] != '/' && fileFullPath[pathLen - 1] != '\\') { + fileFullPath[pathLen] = '/'; + } + + ret = strncat_s(fileFullPath, PATH_MAX, filename, strlen(filename) + 1); + if (ret != 0) { + PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", + fileFullPath, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); + return -1; + } + + fp = fopen(fileFullPath, "w"); + if (fp == NULL) { + PRINT_ERR("can`t create file(%s), errno = %d\n", fileFullPath, errno); + return -1; + } + + ret = fwrite(convertedBuffer, bufferLen, 1, fp); + if (ret != 1) { + PRINT_ERR("can`t write file(%s),errno = %d\n", fileFullPath, errno); + fclose(fp); + return -1; + } + + fclose(fp); + + return 0; +} + +int32_t PCIDEncode(char *inputFile, char *outDirPath) +{ + int32_t ret; + char productName[NAME_MAX] = {0}; + char *contextBuffer = NULL; + uint32_t bufferLen; + char *convertedBuffer = NULL; + uint32_t convertedBufLen = sizeof(PCIDHead); + char *systemType = NULL; + int32_t osCapSize, privateCapSize; + PCIDHead *headPtr = NULL; + char *fillTmpPtr = NULL; + cJSON *cjsonObjectRoot = NULL; + cJSON *cjsonObjectPtr = NULL; + cJSON *osCapPtr = NULL; + cJSON *privateCapPtr = NULL; + cJSON *arrayItemPtr = NULL; + + ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); + if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + return -1; + } + + cjsonObjectRoot = cJSON_ParseWithLength(contextBuffer, bufferLen); + if (cjsonObjectRoot == NULL) { + PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "syscap"); + if (cjsonObjectPtr == NULL || !cJSON_IsObject(cjsonObjectPtr)) { + PRINT_ERR("get \"syscap\" object failed, cjsonObjectPtr = %p\n", cjsonObjectPtr); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + osCapPtr = cJSON_GetObjectItem(cjsonObjectPtr, "os"); + if (osCapPtr == NULL || !cJSON_IsArray(osCapPtr)) { + PRINT_ERR("get \"os\" array failed, osCapPtr = %p\n", osCapPtr); + ret = -1; + goto FREE_CONTEXT_OUT; + } + osCapSize = cJSON_GetArraySize(osCapPtr); + if (osCapSize <= 0) { + PRINT_ERR("\"os\" array is empty\n"); + ret = -1; + goto FREE_CONTEXT_OUT; + } + // 2, to save osSysCaptype & osSysCapLength + convertedBufLen += (2 * sizeof(uint16_t) + osCapSize * SINGLE_FEAT_LENGTH); + + privateCapPtr = cJSON_GetObjectItem(cjsonObjectPtr, "private"); + if (privateCapPtr != NULL && cJSON_IsArray(privateCapPtr)) { + privateCapSize = cJSON_GetArraySize(privateCapPtr); + // 2, to save privateSysCaptype & privateSysCapLength + convertedBufLen += (2 * sizeof(uint16_t) * !!privateCapSize + + privateCapSize * SINGLE_FEAT_LENGTH); + } else if (privateCapPtr == NULL) { + privateCapSize = 0; + } else { + PRINT_ERR("get \"private\" array failed, privateCapPtr = %p\n", privateCapPtr); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + convertedBuffer = (char *)malloc(convertedBufLen); + + (void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen); + + headPtr = (PCIDHead *)convertedBuffer; + cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "api_version"); + if (cjsonObjectPtr == NULL || !cJSON_IsNumber(cjsonObjectPtr)) { + PRINT_ERR("get \"api_version\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr); + ret = -1; + goto FREE_CONVERT_OUT; + } + headPtr->apiVersion = HtonsInter((uint16_t)cjsonObjectPtr->valueint); + headPtr->apiVersionType = 0; + + cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "system_type"); + if (cjsonObjectPtr == NULL || !cJSON_IsString(cjsonObjectPtr)) { + PRINT_ERR("get \"system_type\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr); + ret = -1; + goto FREE_CONVERT_OUT; + } + systemType = cjsonObjectPtr->valuestring; + headPtr->systemType = !strcmp(systemType, "mini") ? 0b001 : + (!strcmp(systemType, "small") ? 0b010 : + (!strcmp(systemType, "standard") ? 0b100 : 0)); + if (headPtr->systemType == 0) { + PRINT_ERR("\"system_type\" is invaild, systemType = \"%s\"\n", systemType); + ret = -1; + goto FREE_CONVERT_OUT; + } + + cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "manufacturer_id"); + if (cjsonObjectPtr == NULL || !cJSON_IsNumber(cjsonObjectPtr)) { + PRINT_ERR("get \"manufacturer_id\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr); + ret = -1; + goto FREE_CONVERT_OUT; + } + headPtr->manufacturerID = HtonlInter((uint32_t)cjsonObjectPtr->valueint); + + fillTmpPtr = convertedBuffer + sizeof(PCIDHead); + + *(uint16_t *)fillTmpPtr = HtonsInter(0); // 0, SysCap Type, 0: osCap + fillTmpPtr += sizeof(uint16_t); + // fill osCap Length + *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(osCapSize * SINGLE_FEAT_LENGTH)); + fillTmpPtr += sizeof(uint16_t); + for (int32_t i = 0; i < osCapSize; i++) { + arrayItemPtr = cJSON_GetArrayItem(osCapPtr, i); + char *pointPos = strchr(arrayItemPtr->valuestring, '.'); + if (pointPos == NULL) { + PRINT_ERR("context of \"os\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + ret = strncmp(arrayItemPtr->valuestring, "SystemCapability.", pointPos - arrayItemPtr->valuestring + 1); + if (ret != 0) { + PRINT_ERR("context of \"os\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + + ret = memcpy_s(fillTmpPtr, SINGLE_FEAT_LENGTH, pointPos + 1, strlen(pointPos + 1)); + if (ret != 0) { + PRINT_ERR("context of \"os\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + fillTmpPtr += SINGLE_FEAT_LENGTH; + } + + if (privateCapSize != 0) { + *(uint16_t *)fillTmpPtr = HtonsInter(1); // 1, SysCap Type, 1: privateCap + fillTmpPtr += sizeof(uint16_t); + // fill privateCap Length + *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(privateCapSize * SINGLE_FEAT_LENGTH)); + fillTmpPtr += sizeof(uint16_t); + for (int32_t i = 0; i < privateCapSize; i++) { + arrayItemPtr = cJSON_GetArrayItem(privateCapPtr, i); + char *pointPos = strchr(arrayItemPtr->valuestring, '.'); + if (pointPos == NULL) { + PRINT_ERR("context of \"private\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + ret = strncmp(arrayItemPtr->valuestring, "SystemCapability.", pointPos - arrayItemPtr->valuestring + 1); + if (ret != 0) { + PRINT_ERR("context of \"private\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + + ret = memcpy_s(fillTmpPtr, SINGLE_FEAT_LENGTH, pointPos + 1, strlen(pointPos + 1)); + if (ret != 0) { + PRINT_ERR("context of \"private\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + fillTmpPtr += SINGLE_FEAT_LENGTH; + } + } + + cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "product"); + if (cjsonObjectPtr == NULL || !cJSON_IsString(cjsonObjectPtr)) { + PRINT_ERR("get \"product\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr); + ret = -1; + goto FREE_CONVERT_OUT; + } + + ret = strncpy_s(productName, NAME_MAX, cjsonObjectPtr->valuestring, strlen(cjsonObjectPtr->valuestring)); + if (ret != 0) { + PRINT_ERR("strncpy_s failed, source string:%s, len = %d, errno = %d\n", + cjsonObjectPtr->valuestring, (int32_t)strlen(cjsonObjectPtr->valuestring), errno); + ret = -1; + goto FREE_CONVERT_OUT; + } + + ret = strncat_s(productName, NAME_MAX, ".sc", 4); // 4. '.' 's' 'c' '\0' + if (ret != 0) { + PRINT_ERR("strncat_s failed, (%s, %d, \".sc\", 4), errno = %d\n", productName, NAME_MAX, errno); + ret = -1; + goto FREE_CONVERT_OUT; + } + + productName[NAME_MAX - 1] = '\0'; + ret = ConvertedContextSaveAsFile(outDirPath, productName, convertedBuffer, convertedBufLen); + if (ret != 0) { + PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, productName); + ret = -1; + goto FREE_CONVERT_OUT; + } + ret = 0; + +FREE_CONVERT_OUT: + free(convertedBuffer); +FREE_CONTEXT_OUT: + FreeContextBuffer(contextBuffer); + return ret; +} + +int32_t PCIDDecode(char *inputFile, char *outDirPath) +{ + int32_t ret; + char *contextBuffer = NULL; + char *contextBufferTail = NULL; + uint32_t bufferLen; + char *convertedBuffer = NULL; + uint16_t sysCaptype, sysCapLength; + PCIDHead *headPtr = NULL; + char *osCapArrayPtr = NULL; + char *privateCapArrayPtr = NULL; + cJSON *cjsonObjectRoot = NULL; + cJSON *sysCapObjectPtr = NULL; + cJSON *capVectorPtr = NULL; + char productName[NAME_MAX] = {0}; + char *inputFileName = basename(inputFile); + uint16_t inputFileNameLen = strlen(inputFileName); + char *pointPos = strchr(inputFileName, '.'); + uint16_t productNameLen = pointPos ? (pointPos - inputFileName) : inputFileNameLen; + + ret = strncpy_s(productName, NAME_MAX, inputFileName, productNameLen); + if (ret != 0) { + PRINT_ERR("strncpy_s failed, source string:%s, len = %d\n", inputFileName, productNameLen); + return -1; + } + productName[NAME_MAX - 1] = '\0'; + + ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); + if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + return -1; + } + + contextBufferTail = contextBuffer + bufferLen; + // 2, to save osSysCaptype & osSysCapLength + osCapArrayPtr = contextBuffer + sizeof(PCIDHead) + 2 * sizeof(uint16_t); + if (contextBufferTail <= osCapArrayPtr) { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_CONTEXT_OUT; + } + headPtr = (PCIDHead *)contextBuffer; + if (headPtr->apiVersionType != 0) { + PRINT_ERR("prase file failed, apiVersionType is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + char *systemType = headPtr->systemType == 0b001 ? "mini" : + (headPtr->systemType == 0b010 ? "small" : + (headPtr->systemType == 0b100 ? "standard" : NULL)); + if (systemType == NULL) { + PRINT_ERR("prase file failed, systemType is invaild, %d\n", headPtr->systemType); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + sysCaptype = NtohsInter(*(uint16_t *)(osCapArrayPtr - 2 * sizeof(uint16_t))); // 2, for type & length + if (sysCaptype != 0) { + PRINT_ERR("prase file failed, sysCaptype is invaild, %d\n", sysCaptype); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + sysCapLength = NtohsInter(*(uint16_t *)(osCapArrayPtr - sizeof(uint16_t))); + if (contextBufferTail < osCapArrayPtr + sysCapLength) { + PRINT_ERR("prase file(%s) failed\n", inputFile); + ret = -1; + goto FREE_CONTEXT_OUT; + } + sysCapObjectPtr = cJSON_CreateObject(); + if (sysCapObjectPtr == NULL) { + PRINT_ERR("cJSON_CreateObject failed\n"); + ret = -1; + goto FREE_CONTEXT_OUT; + } + capVectorPtr = cJSON_CreateArray(); + if (capVectorPtr == NULL) { + PRINT_ERR("cJSON_CreateArray failed\n"); + ret = -1; + goto FREE_SYSCAP_OUT; + } + for (int32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) { + if (*(osCapArrayPtr + (i + 1) * SINGLE_FEAT_LENGTH - 1) != '\0') { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_VECTOR_OUT; + } + char buffer[SINGLE_FEAT_LENGTH + 17] = "SystemCapability."; // 17, sizeof "SystemCapability." + + ret = strncat_s(buffer, sizeof(buffer), osCapArrayPtr + i * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH); + if (ret != 0) { + PRINT_ERR("strncat_s failed, (%s, %d, %s, %d)\n", + buffer, (int32_t)sizeof(buffer), osCapArrayPtr + i * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH); + ret = -1; + goto FREE_CONVERT_OUT; + } + if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(buffer))) { + PRINT_ERR("cJSON_AddItemToArray or cJSON_CreateString failed\n"); + ret = -1; + goto FREE_VECTOR_OUT; + } + } + if (!cJSON_AddItemToObject(sysCapObjectPtr, "os", capVectorPtr)) { + PRINT_ERR("cJSON_AddItemToObject failed\n"); + ret = -1; + goto FREE_VECTOR_OUT; + } + capVectorPtr = NULL; + privateCapArrayPtr = osCapArrayPtr + sysCapLength + 2 * sizeof(uint16_t); // 2, for type & length + if (contextBufferTail >= privateCapArrayPtr) { + sysCaptype = NtohsInter(*(uint16_t *)(privateCapArrayPtr - 2 * sizeof(uint16_t))); // 2, for type & length + if (sysCaptype != 1) { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_SYSCAP_OUT; + } + sysCapLength = NtohsInter(*(uint16_t *)(privateCapArrayPtr - sizeof(uint16_t))); + if (contextBufferTail < privateCapArrayPtr + sysCapLength) { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_SYSCAP_OUT; + } + + capVectorPtr = cJSON_CreateArray(); + if (capVectorPtr == NULL) { + PRINT_ERR("cJSON_CreateArray failed\n"); + ret = -1; + goto FREE_SYSCAP_OUT; + } + + for (int32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) { + if (*(privateCapArrayPtr + (i + 1) * SINGLE_FEAT_LENGTH - 1) != '\0') { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_VECTOR_OUT; + } + + char buffer[SINGLE_FEAT_LENGTH + 17] = "SystemCapability."; // 17, sizeof "SystemCapability." + + ret = strncat_s(buffer, sizeof(buffer), privateCapArrayPtr + i * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH); + if (ret != 0) { + PRINT_ERR("strncat_s failed, (%s, %d, %s, %d)\n", buffer, + (int32_t)sizeof(buffer), privateCapArrayPtr + i * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH); + ret = -1; + goto FREE_CONVERT_OUT; + } + + if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(buffer))) { + PRINT_ERR("cJSON_AddItemToArray or cJSON_CreateString failed\n"); + ret = -1; + goto FREE_VECTOR_OUT; + } + } + if (!cJSON_AddItemToObject(sysCapObjectPtr, "private", capVectorPtr)) { + PRINT_ERR("cJSON_AddItemToObject failed\n"); + ret = -1; + goto FREE_VECTOR_OUT; + } + capVectorPtr = NULL; + } + + cjsonObjectRoot = cJSON_CreateObject(); + if (cjsonObjectRoot == NULL) { + PRINT_ERR("cJSON_CreateObject failed\n"); + ret = -1; + goto FREE_SYSCAP_OUT; + } + if (!cJSON_AddStringToObject(cjsonObjectRoot, "product", productName)) { + PRINT_ERR("cJSON_AddStringToObject failed\n"); + ret = -1; + goto FREE_ROOT_OUT; + } + if (!cJSON_AddNumberToObject(cjsonObjectRoot, "api_version", NtohsInter(headPtr->apiVersion))) { + PRINT_ERR("cJSON_AddNumberToObject failed\n"); + ret = -1; + goto FREE_ROOT_OUT; + } + if (!cJSON_AddNumberToObject(cjsonObjectRoot, "manufacturer_id", NtohlInter(headPtr->manufacturerID))) { + PRINT_ERR("cJSON_AddNumberToObject failed\n"); + ret = -1; + goto FREE_ROOT_OUT; + } + if (!cJSON_AddStringToObject(cjsonObjectRoot, "system_type", systemType)) { + PRINT_ERR("cJSON_AddStringToObject failed\n"); + ret = -1; + goto FREE_ROOT_OUT; + } + if (!cJSON_AddItemToObject(cjsonObjectRoot, "syscap", sysCapObjectPtr)) { + PRINT_ERR("cJSON_AddItemToObject failed\n"); + ret = -1; + goto FREE_ROOT_OUT; + } + sysCapObjectPtr = NULL; + convertedBuffer = cJSON_Print(cjsonObjectRoot); + + ret = strncat_s(productName, NAME_MAX, ".json", 6); // 6. '.' 'j' 's' 'o' 'n' '\0' + if (ret != 0) { + PRINT_ERR("strncat_s failed, (%s, %d, .json, 6), errno = %d\n", productName, NAME_MAX, errno); + goto FREE_CONVERT_OUT; + } + + productName[NAME_MAX - 1] = '\0'; + ret = ConvertedContextSaveAsFile(outDirPath, productName, convertedBuffer, strlen(convertedBuffer)); + if (ret != 0) { + PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, productName); + goto FREE_CONVERT_OUT; + } + +FREE_CONVERT_OUT: + free(convertedBuffer); +FREE_ROOT_OUT: + cJSON_Delete(cjsonObjectRoot); +FREE_VECTOR_OUT: + cJSON_Delete(capVectorPtr); +FREE_SYSCAP_OUT: + cJSON_Delete(sysCapObjectPtr); +FREE_CONTEXT_OUT: + FreeContextBuffer(contextBuffer); + return ret; +} + +int32_t RPCIDEncode(char *inputFile, char *outDirPath) +{ + int32_t ret; + char *contextBuffer = NULL; + uint32_t bufferLen; + char *convertedBuffer = NULL; + uint32_t convertedBufLen = sizeof(RPCIDHead); + int32_t sysCapSize; + RPCIDHead *headPtr = NULL; + char *fillTmpPtr = NULL; + cJSON *cjsonObjectRoot = NULL; + cJSON *apiVerItem = NULL; + cJSON *sysCapPtr = NULL; + cJSON *arrayItemPtr = NULL; + + ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); + if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + return ret; + } + + cjsonObjectRoot = cJSON_ParseWithLength(contextBuffer, bufferLen); + if (cjsonObjectRoot == NULL) { + PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + sysCapPtr = cJSON_GetObjectItem(cjsonObjectRoot, "syscap"); + if (sysCapPtr == NULL || !cJSON_IsArray(sysCapPtr)) { + PRINT_ERR("get \"syscap\" object failed, sysCapPtr = %p\n", sysCapPtr); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + sysCapSize = cJSON_GetArraySize(sysCapPtr); + if (sysCapSize <= 0) { + PRINT_ERR("\"syscap\" array is empty\n"); + ret = -1; + goto FREE_CONTEXT_OUT; + } + // 2, to save SysCaptype & SysCapLength + convertedBufLen += (2 * sizeof(uint16_t) + sysCapSize * SINGLE_FEAT_LENGTH); + + convertedBuffer = (char *)malloc(convertedBufLen); + (void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen); + + headPtr = (RPCIDHead *)convertedBuffer; + apiVerItem = cJSON_GetObjectItem(cjsonObjectRoot, "api_version"); + if (apiVerItem == NULL || !cJSON_IsNumber(apiVerItem)) { + PRINT_ERR("get \"api_version\" failed, apiVerItem = %p\n", apiVerItem); + ret = -1; + goto FREE_CONVERT_OUT; + } + headPtr->apiVersion = HtonsInter((uint16_t)apiVerItem->valueint); + headPtr->apiVersionType = 1; + + fillTmpPtr = convertedBuffer + sizeof(RPCIDHead); + + *(uint16_t *)fillTmpPtr = HtonsInter(2); // 2, SysCap Type, 2: request Cap + fillTmpPtr += sizeof(uint16_t); + // fill osCap Length + *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(sysCapSize * SINGLE_FEAT_LENGTH)); + fillTmpPtr += sizeof(uint16_t); + for (int32_t i = 0; i < sysCapSize; i++) { + arrayItemPtr = cJSON_GetArrayItem(sysCapPtr, i); + char *pointPos = strchr(arrayItemPtr->valuestring, '.'); + if (pointPos == NULL) { + PRINT_ERR("context of \"syscap\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + ret = strncmp(arrayItemPtr->valuestring, "SystemCapability.", pointPos - arrayItemPtr->valuestring + 1); + if (ret != 0) { + PRINT_ERR("context of \"syscap\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + + ret = memcpy_s(fillTmpPtr, SINGLE_FEAT_LENGTH, pointPos + 1, strlen(pointPos + 1)); + if (ret != 0) { + PRINT_ERR("context of \"syscap\" array is invaild\n"); + ret = -1; + goto FREE_CONVERT_OUT; + } + fillTmpPtr += SINGLE_FEAT_LENGTH; + } + + ret = ConvertedContextSaveAsFile(outDirPath, "rpcid.sc", convertedBuffer, convertedBufLen); + if (ret != 0) { + PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:rpcid.sc\n", outDirPath); + goto FREE_CONVERT_OUT; + } + +FREE_CONVERT_OUT: + free(convertedBuffer); +FREE_CONTEXT_OUT: + FreeContextBuffer(contextBuffer); + return ret; +} + +int32_t RPCIDDecode(char *inputFile, char *outDirPath) +{ + uint32_t ret; + char *contextBuffer = NULL; + char *contextBufferTail = NULL; + uint32_t bufferLen; + char *convertedBuffer = NULL; + uint16_t sysCaptype, sysCapLength; + RPCIDHead *headPtr = NULL; + char *sysCapArrayPtr = NULL; + cJSON *cjsonObjectRoot = NULL; + cJSON *sysCapObjectPtr = NULL; + + ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); + if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + return -1; + } + + contextBufferTail = contextBuffer + bufferLen; + // 2, to save osSysCaptype & osSysCapLength + sysCapArrayPtr = contextBuffer + sizeof(RPCIDHead) + 2 * sizeof(uint16_t); + if (contextBufferTail <= sysCapArrayPtr) { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_CONTEXT_OUT; + } + headPtr = (RPCIDHead *)contextBuffer; + if (headPtr->apiVersionType != 1) { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + sysCaptype = NtohsInter(*(uint16_t *)(sysCapArrayPtr - 2 * sizeof(uint16_t))); // 2, for type & length + if (sysCaptype != 2) { // 2, app syscap type + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + sysCapLength = NtohsInter(*(uint16_t *)(sysCapArrayPtr - sizeof(uint16_t))); + if (contextBufferTail < sysCapArrayPtr + sysCapLength) { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_CONTEXT_OUT; + } + + sysCapObjectPtr = cJSON_CreateArray(); + if (sysCapObjectPtr == NULL) { + PRINT_ERR("cJSON_CreateArray failed\n"); + ret = -1; + goto FREE_CONTEXT_OUT; + } + for (int32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) { + if (*(sysCapArrayPtr + (i + 1) * SINGLE_FEAT_LENGTH - 1) != '\0') { + PRINT_ERR("prase file failed, format is invaild, input file : %s\n", inputFile); + ret = -1; + goto FREE_SYSCAP_OUT; + } + char buffer[SINGLE_FEAT_LENGTH + 17] = "SystemCapability."; // 17, sizeof "SystemCapability." + + ret = strncat_s(buffer, sizeof(buffer), sysCapArrayPtr + i * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH); + if (ret != 0) { + PRINT_ERR("strncat_s failed, (%s, %d, %s, %d)\n", + buffer, (int32_t)sizeof(buffer), sysCapArrayPtr + i * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH); + ret = -1; + goto FREE_SYSCAP_OUT; + } + if (!cJSON_AddItemToArray(sysCapObjectPtr, cJSON_CreateString(buffer))) { + PRINT_ERR("cJSON_AddItemToArray or cJSON_CreateString failed\n"); + ret = -1; + goto FREE_SYSCAP_OUT; + } + } + + cjsonObjectRoot = cJSON_CreateObject(); + if (cjsonObjectRoot == NULL) { + PRINT_ERR("cJSON_CreateObject failed\n"); + ret = -1; + goto FREE_SYSCAP_OUT; + } + if (!cJSON_AddNumberToObject(cjsonObjectRoot, "api_version", NtohsInter(headPtr->apiVersion))) { + PRINT_ERR("cJSON_AddNumberToObject failed\n"); + ret = -1; + goto FREE_ROOT_OUT; + } + if (!cJSON_AddItemToObject(cjsonObjectRoot, "syscap", sysCapObjectPtr)) { + PRINT_ERR("cJSON_AddNumberToObject failed\n"); + ret = -1; + goto FREE_ROOT_OUT; + } + sysCapObjectPtr = NULL; + + convertedBuffer = cJSON_Print(cjsonObjectRoot); + + ret = ConvertedContextSaveAsFile(outDirPath, "rpcid.json", convertedBuffer, strlen(convertedBuffer)); + if (ret != 0) { + PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:rpcid.json\n", outDirPath); + goto FREE_CONVERT_OUT; + } + +FREE_CONVERT_OUT: + free(convertedBuffer); +FREE_ROOT_OUT: + cJSON_Delete(cjsonObjectRoot); +FREE_SYSCAP_OUT: + cJSON_Delete(sysCapObjectPtr); +FREE_CONTEXT_OUT: + FreeContextBuffer(contextBuffer); + return ret; +}