14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include <fstream>
174514f5e3Sopenharmony_ci#include <iostream>
184514f5e3Sopenharmony_ci#include <string>
194514f5e3Sopenharmony_ci#include "ecmascript/log_wrapper.h"
204514f5e3Sopenharmony_ci#include "ecmascript/platform/file.h"
214514f5e3Sopenharmony_ci#include "prof_dump_json.h"
224514f5e3Sopenharmony_ci#include "securec.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cistatic constexpr size_t OUTPUT_FILE_ARG = 2;
254514f5e3Sopenharmony_cistatic constexpr size_t ARGS_NUMBER = 3;
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_ciint main(int argc, char** argv)
284514f5e3Sopenharmony_ci{
294514f5e3Sopenharmony_ci    if (argc != ARGS_NUMBER) {
304514f5e3Sopenharmony_ci        std::cout << "please use this tools like:" << std::endl;
314514f5e3Sopenharmony_ci        std::cout << "    ./apViewer input.ap out.json" << std::endl;
324514f5e3Sopenharmony_ci        return 0;
334514f5e3Sopenharmony_ci    }
344514f5e3Sopenharmony_ci    std::string input = argv[1];
354514f5e3Sopenharmony_ci    std::string output = argv[OUTPUT_FILE_ARG];
364514f5e3Sopenharmony_ci    size_t length = native::ConvertApToJson(input.c_str(), input.length());
374514f5e3Sopenharmony_ci    if (length == 0 || length >= SIZE_MAX) {
384514f5e3Sopenharmony_ci        LOG_NO_TAG(ERROR) << "ConvertApToJson returned zero or invalid length!";
394514f5e3Sopenharmony_ci        return 0;
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    char *buffer = (char *)malloc(length);
424514f5e3Sopenharmony_ci    if (buffer == nullptr) {
434514f5e3Sopenharmony_ci        LOG_NO_TAG(ERROR) << "malloc buffer failed";
444514f5e3Sopenharmony_ci        return 0;
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci    if (memset_s(buffer, length, 0, length) != EOK) {
474514f5e3Sopenharmony_ci        LOG_NO_TAG(ERROR) << "clean malloc buffer failed";
484514f5e3Sopenharmony_ci        free(buffer);
494514f5e3Sopenharmony_ci        return 0;
504514f5e3Sopenharmony_ci    }
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    auto result = native::GetConvertResult(buffer, length);
534514f5e3Sopenharmony_ci    if (result) {
544514f5e3Sopenharmony_ci        std::cout << "convert " << argv[1] << " to " << argv[OUTPUT_FILE_ARG] << " success!" << std::endl;
554514f5e3Sopenharmony_ci    } else {
564514f5e3Sopenharmony_ci        std::cout << "convert " << argv[1] << " to " << argv[OUTPUT_FILE_ARG] << " failed!" << std::endl;
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_ci    std::string realOutPath;
604514f5e3Sopenharmony_ci    if (!panda::ecmascript::RealPath(output, realOutPath, false)) {
614514f5e3Sopenharmony_ci        LOG_NO_TAG(ERROR) << "Can not load filepath " << output;
624514f5e3Sopenharmony_ci        free(buffer);
634514f5e3Sopenharmony_ci        return 0;
644514f5e3Sopenharmony_ci    }
654514f5e3Sopenharmony_ci    std::ofstream outFile(realOutPath);
664514f5e3Sopenharmony_ci    if (!outFile.is_open()) {
674514f5e3Sopenharmony_ci        LOG_NO_TAG(ERROR) << "open " << realOutPath << " failed!";
684514f5e3Sopenharmony_ci        free(buffer);
694514f5e3Sopenharmony_ci        return 0;
704514f5e3Sopenharmony_ci    }
714514f5e3Sopenharmony_ci    std::string strRet(buffer, length);
724514f5e3Sopenharmony_ci    outFile << strRet;
734514f5e3Sopenharmony_ci    outFile.close();
744514f5e3Sopenharmony_ci    free(buffer);
754514f5e3Sopenharmony_ci    return 0;
764514f5e3Sopenharmony_ci}
77