diff --git a/frameworks/intl/include/i18n_timezone.h b/frameworks/intl/include/i18n_timezone.h index 8cb570be..142c2ebc 100644 --- a/frameworks/intl/include/i18n_timezone.h +++ b/frameworks/intl/include/i18n_timezone.h @@ -17,6 +17,7 @@ #include #include +#include #include #include #include "i18n_types.h" @@ -81,6 +82,7 @@ private: static void GetTimezoneIDFromZoneInfo(std::set &availableIDs, std::string &parentPath, std::string &parentName); static std::vector GetColorData(const int x, const int y); + static int InitPngptr(png_structp &png_ptr, png_infop &info_ptr, FILE **fp); }; } // namespace I18n } // namespace Global diff --git a/frameworks/intl/src/i18n_timezone.cpp b/frameworks/intl/src/i18n_timezone.cpp index 0a872323..e4bf0dc0 100644 --- a/frameworks/intl/src/i18n_timezone.cpp +++ b/frameworks/intl/src/i18n_timezone.cpp @@ -34,7 +34,6 @@ #include "unicode/locid.h" #include "unicode/unistr.h" #include "utils.h" -#include #include namespace OHOS { @@ -1002,25 +1001,11 @@ std::vector I18nTimeZone::GetColorData(const int x, const int y) return result; } FILE *fp; + png_structp png_ptr; png_infop info_ptr; png_bytep row_pointers; - fp = fopen(TZ_PIXEL_PATH, "rb"); - if (fp == NULL) { - HiLog::Error(LABEL, "timezone data resource file not exists."); - return result; - } - png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png_ptr) { - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, 0); - HiLog::Error(LABEL, "create read_struct failed."); - return result; - } - info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { - fclose(fp); - png_destroy_read_struct(&png_ptr, &info_ptr, 0); - HiLog::Error(LABEL, "create info_struct failed."); + int code = InitPngptr(png_ptr, info_ptr, &fp); + if (code != 0) { return result; } try { @@ -1029,7 +1014,7 @@ std::vector I18nTimeZone::GetColorData(const int x, const int y) png_read_info(png_ptr, info_ptr); unsigned int rowbytes = png_get_rowbytes(png_ptr, info_ptr); row_pointers = (png_bytep)png_malloc(png_ptr, rowbytes); - if (row_pointers == NULL) { + if (row_pointers == nullptr) { fclose(fp); png_destroy_read_struct(&png_ptr, &info_ptr, 0); HiLog::Error(LABEL, "malloc rowbytes failed."); @@ -1051,6 +1036,30 @@ std::vector I18nTimeZone::GetColorData(const int x, const int y) fclose(fp); return result; } + +int I18nTimeZone::InitPngptr(png_structp &png_ptr, png_infop &info_ptr, FILE **fp) +{ + *fp = fopen(TZ_PIXEL_PATH, "rb"); + if (*fp == NULL) { + HiLog::Error(LABEL, "timezone data resource file not exists."); + return 1; + } + png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!png_ptr) { + fclose(*fp); + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + HiLog::Error(LABEL, "create read_struct failed."); + return 1; + } + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) { + fclose(*fp); + png_destroy_read_struct(&png_ptr, &info_ptr, 0); + HiLog::Error(LABEL, "create info_struct failed."); + return 1; + } + return 0; +} } } } diff --git a/frameworks/intl/test/BUILD.gn b/frameworks/intl/test/BUILD.gn index 1c033931..6cf1642e 100644 --- a/frameworks/intl/test/BUILD.gn +++ b/frameworks/intl/test/BUILD.gn @@ -37,6 +37,7 @@ ohos_unittest("intl_test") { "//third_party/googletest:gtest_main", "//third_party/icu/icu4c:shared_icui18n", "//third_party/icu/icu4c:shared_icuuc", + "//third_party/libpng:libpng", ] } diff --git a/frameworks/intl/test/fuzztest/BUILD.gn b/frameworks/intl/test/fuzztest/BUILD.gn index e5982bd1..715d22bf 100644 --- a/frameworks/intl/test/fuzztest/BUILD.gn +++ b/frameworks/intl/test/fuzztest/BUILD.gn @@ -28,5 +28,6 @@ group("fuzztest") { "relativetimeformat_fuzzer:RelativeTimeFormatFuzzTest", "timezone_fuzzer:TimeZoneFuzzTest", "utils_fuzzer:UtilsFuzzTest", + "//third_party/libpng:libpng", ] } diff --git a/frameworks/intl/test/fuzztest/timezone_fuzzer/BUILD.gn b/frameworks/intl/test/fuzztest/timezone_fuzzer/BUILD.gn index 32237513..40e23c6a 100644 --- a/frameworks/intl/test/fuzztest/timezone_fuzzer/BUILD.gn +++ b/frameworks/intl/test/fuzztest/timezone_fuzzer/BUILD.gn @@ -43,6 +43,7 @@ ohos_fuzztest("TimeZoneFuzzTest") { "//base/global/i18n/frameworks/intl:intl_util", "//third_party/icu/icu4c:shared_icui18n", "//third_party/icu/icu4c:shared_icuuc", + "//third_party/libpng:libpng", ] subsystem_name = "global" diff --git a/frameworks/zone/BUILD.gn b/frameworks/zone/BUILD.gn index 5f05fb24..8ff2f914 100644 --- a/frameworks/zone/BUILD.gn +++ b/frameworks/zone/BUILD.gn @@ -42,6 +42,7 @@ ohos_shared_library("zone_util") { "//third_party/icu/icu4c:shared_icui18n", "//third_party/icu/icu4c:shared_icuuc", "//third_party/libphonenumber/cpp:phonenumber_standard", + "//third_party/libpng:libpng", "//third_party/libxml2:xml2", ] external_deps = [ "hiviewdfx_hilog_native:libhilog" ] diff --git a/interfaces/js/kits/BUILD.gn b/interfaces/js/kits/BUILD.gn index aa356292..1d1aa1ba 100644 --- a/interfaces/js/kits/BUILD.gn +++ b/interfaces/js/kits/BUILD.gn @@ -75,6 +75,7 @@ ohos_shared_library("i18n") { "//base/global/i18n/frameworks/intl:preferred_language", "//third_party/icu/icu4c:shared_icui18n", "//third_party/icu/icu4c:shared_icuuc", + "//third_party/libpng:libpng", ] external_deps = [ "hiviewdfx_hilog_native:libhilog",