15bec5421Sopenharmony_ci#!/usr/bin/env python
25bec5421Sopenharmony_ci# -*- coding: utf-8 -*-
35bec5421Sopenharmony_ci# Copyright (c) 2023 Huawei Device Co., Ltd.
45bec5421Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55bec5421Sopenharmony_ci# you may not use this file except in compliance with the License.
65bec5421Sopenharmony_ci# You may obtain a copy of the License at
75bec5421Sopenharmony_ci#
85bec5421Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95bec5421Sopenharmony_ci#
105bec5421Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115bec5421Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125bec5421Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135bec5421Sopenharmony_ci# See the License for the specific language governing permissions and
145bec5421Sopenharmony_ci# limitations under the License.
155bec5421Sopenharmony_ci
165bec5421Sopenharmony_ciimport tarfile
175bec5421Sopenharmony_ciimport argparse
185bec5421Sopenharmony_ciimport os
195bec5421Sopenharmony_ciimport subprocess
205bec5421Sopenharmony_ciimport sys
215bec5421Sopenharmony_ciimport shutil
225bec5421Sopenharmony_ci
235bec5421Sopenharmony_ci
245bec5421Sopenharmony_cidef untar_file(tar_file_path, extract_path):
255bec5421Sopenharmony_ci    try:
265bec5421Sopenharmony_ci        tar_cmd = ['tar', '-zxf', tar_file_path, '-C', extract_path]
275bec5421Sopenharmony_ci        subprocess.run(tar_cmd, check=True)
285bec5421Sopenharmony_ci    except Exception as e:
295bec5421Sopenharmony_ci        print("tar error!")
305bec5421Sopenharmony_ci        return
315bec5421Sopenharmony_ci
325bec5421Sopenharmony_ci
335bec5421Sopenharmony_cidef move_file(src_path, dst_path):
345bec5421Sopenharmony_ci    files = [
355bec5421Sopenharmony_ci        "CVE-2019-6129.patch",
365bec5421Sopenharmony_ci        "huawei_libpng_CMakeList.patch",
375bec5421Sopenharmony_ci        "libpng-fix-arm-neon.patch",
385bec5421Sopenharmony_ci        "libpng-multilib.patch",
395bec5421Sopenharmony_ci        "backport-libpng-1.6.37-enable-valid.patch",
405bec5421Sopenharmony_ci        "pnglibconf.h",
415bec5421Sopenharmony_ci        "CVE-2018-14048.patch",
425bec5421Sopenharmony_ci        "libpng_optimize.patch"
435bec5421Sopenharmony_ci    ]
445bec5421Sopenharmony_ci    for file in files:
455bec5421Sopenharmony_ci        src_file = os.path.join(src_path, file)
465bec5421Sopenharmony_ci        dst_file = os.path.join(dst_path, file)
475bec5421Sopenharmony_ci        shutil.copy(src_file, dst_file)
485bec5421Sopenharmony_ci
495bec5421Sopenharmony_ci
505bec5421Sopenharmony_cidef apply_patch(patch_file, target_dir):
515bec5421Sopenharmony_ci    try:
525bec5421Sopenharmony_ci        if not os.path.exists(target_dir):
535bec5421Sopenharmony_ci            return
545bec5421Sopenharmony_ci        patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_file, '-d', target_dir]
555bec5421Sopenharmony_ci        subprocess.run(patch_cmd, check=True)
565bec5421Sopenharmony_ci    except Exception as e:
575bec5421Sopenharmony_ci        print("apply_patch error!")
585bec5421Sopenharmony_ci        return
595bec5421Sopenharmony_ci
605bec5421Sopenharmony_ci
615bec5421Sopenharmony_cidef do_patch(target_dir):
625bec5421Sopenharmony_ci    patch_file = [
635bec5421Sopenharmony_ci        "CVE-2019-6129.patch",
645bec5421Sopenharmony_ci        "huawei_libpng_CMakeList.patch",
655bec5421Sopenharmony_ci        "libpng-fix-arm-neon.patch",
665bec5421Sopenharmony_ci        "libpng-multilib.patch",
675bec5421Sopenharmony_ci        "backport-libpng-1.6.37-enable-valid.patch",
685bec5421Sopenharmony_ci        "CVE-2018-14048.patch",
695bec5421Sopenharmony_ci        "libpng_optimize.patch"
705bec5421Sopenharmony_ci    ]
715bec5421Sopenharmony_ci
725bec5421Sopenharmony_ci    for patch in patch_file:
735bec5421Sopenharmony_ci        apply_patch(patch, target_dir)
745bec5421Sopenharmony_ci
755bec5421Sopenharmony_ci
765bec5421Sopenharmony_cidef main():
775bec5421Sopenharmony_ci    libpng_path = argparse.ArgumentParser()
785bec5421Sopenharmony_ci    libpng_path.add_argument('--gen-dir', help='generate path of log', required=True)
795bec5421Sopenharmony_ci    libpng_path.add_argument('--source-dir', help='generate path of log', required=True)
805bec5421Sopenharmony_ci    args = libpng_path.parse_args()
815bec5421Sopenharmony_ci    tar_file_path = os.path.join(args.source_dir, "libpng-1.6.38.tar.gz")
825bec5421Sopenharmony_ci    target_dir = os.path.join(args.gen_dir, "libpng-1.6.38")
835bec5421Sopenharmony_ci
845bec5421Sopenharmony_ci    untar_file(tar_file_path, args.gen_dir)
855bec5421Sopenharmony_ci    move_file(args.source_dir, target_dir)
865bec5421Sopenharmony_ci    do_patch(target_dir)
875bec5421Sopenharmony_ci    return 0
885bec5421Sopenharmony_ci
895bec5421Sopenharmony_ciif __name__ == '__main__':
905bec5421Sopenharmony_ci    sys.exit(main())
91