13d58139fSopenharmony_ci/*
23d58139fSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
33d58139fSopenharmony_ci * Licensed under Mulan PSL v2.
43d58139fSopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
53d58139fSopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
63d58139fSopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
73d58139fSopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
83d58139fSopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
93d58139fSopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
103d58139fSopenharmony_ci * See the Mulan PSL v2 for more details.
113d58139fSopenharmony_ci * Description: memcpy_s function
123d58139fSopenharmony_ci * Create: 2014-02-25
133d58139fSopenharmony_ci */
143d58139fSopenharmony_ci/*
153d58139fSopenharmony_ci * [Standardize-exceptions] Use unsafe function: Portability
163d58139fSopenharmony_ci * [reason] Use unsafe function to implement security function to maintain platform compatibility.
173d58139fSopenharmony_ci *          And sufficient input validation is performed before calling
183d58139fSopenharmony_ci */
193d58139fSopenharmony_ci
203d58139fSopenharmony_ci#include "securecutil.h"
213d58139fSopenharmony_ci
223d58139fSopenharmony_ci#if SECUREC_WITH_PERFORMANCE_ADDONS
233d58139fSopenharmony_ci#ifndef SECUREC_MEMCOPY_THRESHOLD_SIZE
243d58139fSopenharmony_ci#define SECUREC_MEMCOPY_THRESHOLD_SIZE 64UL
253d58139fSopenharmony_ci#endif
263d58139fSopenharmony_ci
273d58139fSopenharmony_ci#define SECUREC_SMALL_MEM_COPY(dest, src, count) do { \
283d58139fSopenharmony_ci    if (SECUREC_ADDR_ALIGNED_8(dest) && SECUREC_ADDR_ALIGNED_8(src)) { \
293d58139fSopenharmony_ci        /* Use struct assignment */ \
303d58139fSopenharmony_ci        switch (count) { \
313d58139fSopenharmony_ci            case 1: \
323d58139fSopenharmony_ci                *(unsigned char *)(dest) = *(const unsigned char *)(src); \
333d58139fSopenharmony_ci                break; \
343d58139fSopenharmony_ci            case 2: \
353d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 2); \
363d58139fSopenharmony_ci                break; \
373d58139fSopenharmony_ci            case 3: \
383d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 3); \
393d58139fSopenharmony_ci                break; \
403d58139fSopenharmony_ci            case 4: \
413d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 4); \
423d58139fSopenharmony_ci                break; \
433d58139fSopenharmony_ci            case 5: \
443d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 5); \
453d58139fSopenharmony_ci                break; \
463d58139fSopenharmony_ci            case 6: \
473d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 6); \
483d58139fSopenharmony_ci                break; \
493d58139fSopenharmony_ci            case 7: \
503d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 7); \
513d58139fSopenharmony_ci                break; \
523d58139fSopenharmony_ci            case 8: \
533d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 8); \
543d58139fSopenharmony_ci                break; \
553d58139fSopenharmony_ci            case 9: \
563d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 9); \
573d58139fSopenharmony_ci                break; \
583d58139fSopenharmony_ci            case 10: \
593d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 10); \
603d58139fSopenharmony_ci                break; \
613d58139fSopenharmony_ci            case 11: \
623d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 11); \
633d58139fSopenharmony_ci                break; \
643d58139fSopenharmony_ci            case 12: \
653d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 12); \
663d58139fSopenharmony_ci                break; \
673d58139fSopenharmony_ci            case 13: \
683d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 13); \
693d58139fSopenharmony_ci                break; \
703d58139fSopenharmony_ci            case 14: \
713d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 14); \
723d58139fSopenharmony_ci                break; \
733d58139fSopenharmony_ci            case 15: \
743d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 15); \
753d58139fSopenharmony_ci                break; \
763d58139fSopenharmony_ci            case 16: \
773d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 16); \
783d58139fSopenharmony_ci                break; \
793d58139fSopenharmony_ci            case 17: \
803d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 17); \
813d58139fSopenharmony_ci                break; \
823d58139fSopenharmony_ci            case 18: \
833d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 18); \
843d58139fSopenharmony_ci                break; \
853d58139fSopenharmony_ci            case 19: \
863d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 19); \
873d58139fSopenharmony_ci                break; \
883d58139fSopenharmony_ci            case 20: \
893d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 20); \
903d58139fSopenharmony_ci                break; \
913d58139fSopenharmony_ci            case 21: \
923d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 21); \
933d58139fSopenharmony_ci                break; \
943d58139fSopenharmony_ci            case 22: \
953d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 22); \
963d58139fSopenharmony_ci                break; \
973d58139fSopenharmony_ci            case 23: \
983d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 23); \
993d58139fSopenharmony_ci                break; \
1003d58139fSopenharmony_ci            case 24: \
1013d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 24); \
1023d58139fSopenharmony_ci                break; \
1033d58139fSopenharmony_ci            case 25: \
1043d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 25); \
1053d58139fSopenharmony_ci                break; \
1063d58139fSopenharmony_ci            case 26: \
1073d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 26); \
1083d58139fSopenharmony_ci                break; \
1093d58139fSopenharmony_ci            case 27: \
1103d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 27); \
1113d58139fSopenharmony_ci                break; \
1123d58139fSopenharmony_ci            case 28: \
1133d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 28); \
1143d58139fSopenharmony_ci                break; \
1153d58139fSopenharmony_ci            case 29: \
1163d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 29); \
1173d58139fSopenharmony_ci                break; \
1183d58139fSopenharmony_ci            case 30: \
1193d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 30); \
1203d58139fSopenharmony_ci                break; \
1213d58139fSopenharmony_ci            case 31: \
1223d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 31); \
1233d58139fSopenharmony_ci                break; \
1243d58139fSopenharmony_ci            case 32: \
1253d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 32); \
1263d58139fSopenharmony_ci                break; \
1273d58139fSopenharmony_ci            case 33: \
1283d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 33); \
1293d58139fSopenharmony_ci                break; \
1303d58139fSopenharmony_ci            case 34: \
1313d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 34); \
1323d58139fSopenharmony_ci                break; \
1333d58139fSopenharmony_ci            case 35: \
1343d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 35); \
1353d58139fSopenharmony_ci                break; \
1363d58139fSopenharmony_ci            case 36: \
1373d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 36); \
1383d58139fSopenharmony_ci                break; \
1393d58139fSopenharmony_ci            case 37: \
1403d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 37); \
1413d58139fSopenharmony_ci                break; \
1423d58139fSopenharmony_ci            case 38: \
1433d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 38); \
1443d58139fSopenharmony_ci                break; \
1453d58139fSopenharmony_ci            case 39: \
1463d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 39); \
1473d58139fSopenharmony_ci                break; \
1483d58139fSopenharmony_ci            case 40: \
1493d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 40); \
1503d58139fSopenharmony_ci                break; \
1513d58139fSopenharmony_ci            case 41: \
1523d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 41); \
1533d58139fSopenharmony_ci                break; \
1543d58139fSopenharmony_ci            case 42: \
1553d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 42); \
1563d58139fSopenharmony_ci                break; \
1573d58139fSopenharmony_ci            case 43: \
1583d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 43); \
1593d58139fSopenharmony_ci                break; \
1603d58139fSopenharmony_ci            case 44: \
1613d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 44); \
1623d58139fSopenharmony_ci                break; \
1633d58139fSopenharmony_ci            case 45: \
1643d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 45); \
1653d58139fSopenharmony_ci                break; \
1663d58139fSopenharmony_ci            case 46: \
1673d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 46); \
1683d58139fSopenharmony_ci                break; \
1693d58139fSopenharmony_ci            case 47: \
1703d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 47); \
1713d58139fSopenharmony_ci                break; \
1723d58139fSopenharmony_ci            case 48: \
1733d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 48); \
1743d58139fSopenharmony_ci                break; \
1753d58139fSopenharmony_ci            case 49: \
1763d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 49); \
1773d58139fSopenharmony_ci                break; \
1783d58139fSopenharmony_ci            case 50: \
1793d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 50); \
1803d58139fSopenharmony_ci                break; \
1813d58139fSopenharmony_ci            case 51: \
1823d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 51); \
1833d58139fSopenharmony_ci                break; \
1843d58139fSopenharmony_ci            case 52: \
1853d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 52); \
1863d58139fSopenharmony_ci                break; \
1873d58139fSopenharmony_ci            case 53: \
1883d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 53); \
1893d58139fSopenharmony_ci                break; \
1903d58139fSopenharmony_ci            case 54: \
1913d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 54); \
1923d58139fSopenharmony_ci                break; \
1933d58139fSopenharmony_ci            case 55: \
1943d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 55); \
1953d58139fSopenharmony_ci                break; \
1963d58139fSopenharmony_ci            case 56: \
1973d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 56); \
1983d58139fSopenharmony_ci                break; \
1993d58139fSopenharmony_ci            case 57: \
2003d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 57); \
2013d58139fSopenharmony_ci                break; \
2023d58139fSopenharmony_ci            case 58: \
2033d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 58); \
2043d58139fSopenharmony_ci                break; \
2053d58139fSopenharmony_ci            case 59: \
2063d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 59); \
2073d58139fSopenharmony_ci                break; \
2083d58139fSopenharmony_ci            case 60: \
2093d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 60); \
2103d58139fSopenharmony_ci                break; \
2113d58139fSopenharmony_ci            case 61: \
2123d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 61); \
2133d58139fSopenharmony_ci                break; \
2143d58139fSopenharmony_ci            case 62: \
2153d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 62); \
2163d58139fSopenharmony_ci                break; \
2173d58139fSopenharmony_ci            case 63: \
2183d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 63); \
2193d58139fSopenharmony_ci                break; \
2203d58139fSopenharmony_ci            case 64: \
2213d58139fSopenharmony_ci                SECUREC_COPY_VALUE_BY_STRUCT((dest), (src), 64); \
2223d58139fSopenharmony_ci                break; \
2233d58139fSopenharmony_ci            default: \
2243d58139fSopenharmony_ci                /* Do nothing */ \
2253d58139fSopenharmony_ci                break; \
2263d58139fSopenharmony_ci        } /* END switch */ \
2273d58139fSopenharmony_ci    } else { \
2283d58139fSopenharmony_ci        unsigned char *tmpDest_ = (unsigned char *)(dest); \
2293d58139fSopenharmony_ci        const unsigned char *tmpSrc_ = (const unsigned char *)(src); \
2303d58139fSopenharmony_ci        switch (count) { \
2313d58139fSopenharmony_ci            case 64: \
2323d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2333d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2343d58139fSopenharmony_ci            case 63: \
2353d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2363d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2373d58139fSopenharmony_ci            case 62: \
2383d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2393d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2403d58139fSopenharmony_ci            case 61: \
2413d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2423d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2433d58139fSopenharmony_ci            case 60: \
2443d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2453d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2463d58139fSopenharmony_ci            case 59: \
2473d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2483d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2493d58139fSopenharmony_ci            case 58: \
2503d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2513d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2523d58139fSopenharmony_ci            case 57: \
2533d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2543d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2553d58139fSopenharmony_ci            case 56: \
2563d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2573d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2583d58139fSopenharmony_ci            case 55: \
2593d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2603d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2613d58139fSopenharmony_ci            case 54: \
2623d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2633d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2643d58139fSopenharmony_ci            case 53: \
2653d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2663d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2673d58139fSopenharmony_ci            case 52: \
2683d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2693d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2703d58139fSopenharmony_ci            case 51: \
2713d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2723d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2733d58139fSopenharmony_ci            case 50: \
2743d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2753d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2763d58139fSopenharmony_ci            case 49: \
2773d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2783d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2793d58139fSopenharmony_ci            case 48: \
2803d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2813d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2823d58139fSopenharmony_ci            case 47: \
2833d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2843d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2853d58139fSopenharmony_ci            case 46: \
2863d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2873d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2883d58139fSopenharmony_ci            case 45: \
2893d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2903d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2913d58139fSopenharmony_ci            case 44: \
2923d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2933d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2943d58139fSopenharmony_ci            case 43: \
2953d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2963d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
2973d58139fSopenharmony_ci            case 42: \
2983d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
2993d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3003d58139fSopenharmony_ci            case 41: \
3013d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3023d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3033d58139fSopenharmony_ci            case 40: \
3043d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3053d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3063d58139fSopenharmony_ci            case 39: \
3073d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3083d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3093d58139fSopenharmony_ci            case 38: \
3103d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3113d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3123d58139fSopenharmony_ci            case 37: \
3133d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3143d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3153d58139fSopenharmony_ci            case 36: \
3163d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3173d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3183d58139fSopenharmony_ci            case 35: \
3193d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3203d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3213d58139fSopenharmony_ci            case 34: \
3223d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3233d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3243d58139fSopenharmony_ci            case 33: \
3253d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3263d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3273d58139fSopenharmony_ci            case 32: \
3283d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3293d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3303d58139fSopenharmony_ci            case 31: \
3313d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3323d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3333d58139fSopenharmony_ci            case 30: \
3343d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3353d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3363d58139fSopenharmony_ci            case 29: \
3373d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3383d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3393d58139fSopenharmony_ci            case 28: \
3403d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3413d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3423d58139fSopenharmony_ci            case 27: \
3433d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3443d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3453d58139fSopenharmony_ci            case 26: \
3463d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3473d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3483d58139fSopenharmony_ci            case 25: \
3493d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3503d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3513d58139fSopenharmony_ci            case 24: \
3523d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3533d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3543d58139fSopenharmony_ci            case 23: \
3553d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3563d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3573d58139fSopenharmony_ci            case 22: \
3583d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3593d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3603d58139fSopenharmony_ci            case 21: \
3613d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3623d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3633d58139fSopenharmony_ci            case 20: \
3643d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3653d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3663d58139fSopenharmony_ci            case 19: \
3673d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3683d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3693d58139fSopenharmony_ci            case 18: \
3703d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3713d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3723d58139fSopenharmony_ci            case 17: \
3733d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3743d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3753d58139fSopenharmony_ci            case 16: \
3763d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3773d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3783d58139fSopenharmony_ci            case 15: \
3793d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3803d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3813d58139fSopenharmony_ci            case 14: \
3823d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3833d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3843d58139fSopenharmony_ci            case 13: \
3853d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3863d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3873d58139fSopenharmony_ci            case 12: \
3883d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3893d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3903d58139fSopenharmony_ci            case 11: \
3913d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3923d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3933d58139fSopenharmony_ci            case 10: \
3943d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3953d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3963d58139fSopenharmony_ci            case 9: \
3973d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
3983d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
3993d58139fSopenharmony_ci            case 8: \
4003d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4013d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4023d58139fSopenharmony_ci            case 7: \
4033d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4043d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4053d58139fSopenharmony_ci            case 6: \
4063d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4073d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4083d58139fSopenharmony_ci            case 5: \
4093d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4103d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4113d58139fSopenharmony_ci            case 4: \
4123d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4133d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4143d58139fSopenharmony_ci            case 3: \
4153d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4163d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4173d58139fSopenharmony_ci            case 2: \
4183d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4193d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4203d58139fSopenharmony_ci            case 1: \
4213d58139fSopenharmony_ci                *(tmpDest_++) = *(tmpSrc_++); \
4223d58139fSopenharmony_ci                /* fall-through */ /* FALLTHRU */ \
4233d58139fSopenharmony_ci            default: \
4243d58139fSopenharmony_ci                /* Do nothing */ \
4253d58139fSopenharmony_ci                break; \
4263d58139fSopenharmony_ci        } \
4273d58139fSopenharmony_ci    } \
4283d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
4293d58139fSopenharmony_ci
4303d58139fSopenharmony_ci/*
4313d58139fSopenharmony_ci * Performance optimization
4323d58139fSopenharmony_ci */
4333d58139fSopenharmony_ci#define SECUREC_MEMCPY_OPT(dest, src, count) do { \
4343d58139fSopenharmony_ci    if ((count) > SECUREC_MEMCOPY_THRESHOLD_SIZE) { \
4353d58139fSopenharmony_ci        SECUREC_MEMCPY_WARP_OPT((dest), (src), (count)); \
4363d58139fSopenharmony_ci    } else { \
4373d58139fSopenharmony_ci        SECUREC_SMALL_MEM_COPY((dest), (src), (count)); \
4383d58139fSopenharmony_ci    } \
4393d58139fSopenharmony_ci} SECUREC_WHILE_ZERO
4403d58139fSopenharmony_ci#endif
4413d58139fSopenharmony_ci
4423d58139fSopenharmony_ci/*
4433d58139fSopenharmony_ci * Handling errors
4443d58139fSopenharmony_ci */
4453d58139fSopenharmony_ciSECUREC_INLINE errno_t SecMemcpyError(void *dest, size_t destMax, const void *src, size_t count)
4463d58139fSopenharmony_ci{
4473d58139fSopenharmony_ci    if (destMax == 0 || destMax > SECUREC_MEM_MAX_LEN) {
4483d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_RANGE("memcpy_s");
4493d58139fSopenharmony_ci        return ERANGE;
4503d58139fSopenharmony_ci    }
4513d58139fSopenharmony_ci    if (dest == NULL || src == NULL) {
4523d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("memcpy_s");
4533d58139fSopenharmony_ci        if (dest != NULL) {
4543d58139fSopenharmony_ci            (void)SECUREC_MEMSET_FUNC_OPT(dest, 0, destMax);
4553d58139fSopenharmony_ci            return EINVAL_AND_RESET;
4563d58139fSopenharmony_ci        }
4573d58139fSopenharmony_ci        return EINVAL;
4583d58139fSopenharmony_ci    }
4593d58139fSopenharmony_ci    if (count > destMax) {
4603d58139fSopenharmony_ci        (void)SECUREC_MEMSET_FUNC_OPT(dest, 0, destMax);
4613d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_RANGE("memcpy_s");
4623d58139fSopenharmony_ci        return ERANGE_AND_RESET;
4633d58139fSopenharmony_ci    }
4643d58139fSopenharmony_ci    if (SECUREC_MEMORY_IS_OVERLAP(dest, src, count)) {
4653d58139fSopenharmony_ci        (void)SECUREC_MEMSET_FUNC_OPT(dest, 0, destMax);
4663d58139fSopenharmony_ci        SECUREC_ERROR_BUFFER_OVERLAP("memcpy_s");
4673d58139fSopenharmony_ci        return EOVERLAP_AND_RESET;
4683d58139fSopenharmony_ci    }
4693d58139fSopenharmony_ci    /* Count is 0 or dest equal src also ret EOK */
4703d58139fSopenharmony_ci    return EOK;
4713d58139fSopenharmony_ci}
4723d58139fSopenharmony_ci
4733d58139fSopenharmony_ci#if defined(SECUREC_COMPATIBLE_WIN_FORMAT)
4743d58139fSopenharmony_ci    /*
4753d58139fSopenharmony_ci     * The fread API in windows will call memcpy_s and pass 0xffffffff to destMax.
4763d58139fSopenharmony_ci     * To avoid the failure of fread, we don't check desMax limit.
4773d58139fSopenharmony_ci     */
4783d58139fSopenharmony_ci#define SECUREC_MEMCPY_PARAM_OK(dest, destMax, src, count) (SECUREC_LIKELY((count) <= (destMax) && \
4793d58139fSopenharmony_ci    (dest) != NULL && (src) != NULL && \
4803d58139fSopenharmony_ci    (count) > 0 && SECUREC_MEMORY_NO_OVERLAP((dest), (src), (count))))
4813d58139fSopenharmony_ci#else
4823d58139fSopenharmony_ci#define SECUREC_MEMCPY_PARAM_OK(dest, destMax, src, count) (SECUREC_LIKELY((count) <= (destMax) && \
4833d58139fSopenharmony_ci    (dest) != NULL && (src) != NULL && (destMax) <= SECUREC_MEM_MAX_LEN && \
4843d58139fSopenharmony_ci    (count) > 0 && SECUREC_MEMORY_NO_OVERLAP((dest), (src), (count))))
4853d58139fSopenharmony_ci#endif
4863d58139fSopenharmony_ci
4873d58139fSopenharmony_ci/*
4883d58139fSopenharmony_ci * <FUNCTION DESCRIPTION>
4893d58139fSopenharmony_ci *    The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest
4903d58139fSopenharmony_ci *
4913d58139fSopenharmony_ci * <INPUT PARAMETERS>
4923d58139fSopenharmony_ci *    dest                      Destination buffer.
4933d58139fSopenharmony_ci *    destMax                   Size of the destination buffer.
4943d58139fSopenharmony_ci *    src                       Buffer to copy from.
4953d58139fSopenharmony_ci *    count                     Number of characters to copy
4963d58139fSopenharmony_ci *
4973d58139fSopenharmony_ci * <OUTPUT PARAMETERS>
4983d58139fSopenharmony_ci *    dest buffer               is updated.
4993d58139fSopenharmony_ci *
5003d58139fSopenharmony_ci * <RETURN VALUE>
5013d58139fSopenharmony_ci *    EOK                      Success
5023d58139fSopenharmony_ci *    EINVAL                   dest is  NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN
5033d58139fSopenharmony_ci *    EINVAL_AND_RESET         dest != NULL and src is NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN
5043d58139fSopenharmony_ci *    ERANGE                   destMax > SECUREC_MEM_MAX_LEN or destMax is 0
5053d58139fSopenharmony_ci *    ERANGE_AND_RESET         count > destMax and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN
5063d58139fSopenharmony_ci *                             and dest  !=  NULL  and src != NULL
5073d58139fSopenharmony_ci *    EOVERLAP_AND_RESET       dest buffer and source buffer are overlapped and
5083d58139fSopenharmony_ci *                             count <= destMax destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN and dest  !=  NULL
5093d58139fSopenharmony_ci *                             and src != NULL  and dest != src
5103d58139fSopenharmony_ci *
5113d58139fSopenharmony_ci *    if an error occurred, dest will be filled with 0.
5123d58139fSopenharmony_ci *    If the source and destination overlap, the behavior of memcpy_s is undefined.
5133d58139fSopenharmony_ci *    Use memmove_s to handle overlapping regions.
5143d58139fSopenharmony_ci */
5153d58139fSopenharmony_cierrno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count)
5163d58139fSopenharmony_ci{
5173d58139fSopenharmony_ci    if (SECUREC_MEMCPY_PARAM_OK(dest, destMax, src, count)) {
5183d58139fSopenharmony_ci        SECUREC_MEMCPY_WARP_OPT(dest, src, count);
5193d58139fSopenharmony_ci        return EOK;
5203d58139fSopenharmony_ci    }
5213d58139fSopenharmony_ci    /* Meet some runtime violation, return error code */
5223d58139fSopenharmony_ci    return SecMemcpyError(dest, destMax, src, count);
5233d58139fSopenharmony_ci}
5243d58139fSopenharmony_ci
5253d58139fSopenharmony_ci#if SECUREC_EXPORT_KERNEL_SYMBOL
5263d58139fSopenharmony_ciEXPORT_SYMBOL(memcpy_s);
5273d58139fSopenharmony_ci#endif
5283d58139fSopenharmony_ci
5293d58139fSopenharmony_ci#if SECUREC_WITH_PERFORMANCE_ADDONS
5303d58139fSopenharmony_ci/*
5313d58139fSopenharmony_ci * Performance optimization
5323d58139fSopenharmony_ci */
5333d58139fSopenharmony_cierrno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count)
5343d58139fSopenharmony_ci{
5353d58139fSopenharmony_ci    if (SECUREC_MEMCPY_PARAM_OK(dest, destMax, src, count)) {
5363d58139fSopenharmony_ci        SECUREC_MEMCPY_OPT(dest, src, count);
5373d58139fSopenharmony_ci        return EOK;
5383d58139fSopenharmony_ci    }
5393d58139fSopenharmony_ci    /* Meet some runtime violation, return error code */
5403d58139fSopenharmony_ci    return SecMemcpyError(dest, destMax, src, count);
5413d58139fSopenharmony_ci}
5423d58139fSopenharmony_ci
5433d58139fSopenharmony_ci/* Trim judgement on "destMax <= SECUREC_MEM_MAX_LEN" */
5443d58139fSopenharmony_cierrno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count)
5453d58139fSopenharmony_ci{
5463d58139fSopenharmony_ci    if (SECUREC_LIKELY(count <= destMax && dest != NULL && src != NULL && \
5473d58139fSopenharmony_ci                       count > 0 && SECUREC_MEMORY_NO_OVERLAP((dest), (src), (count)))) {
5483d58139fSopenharmony_ci        SECUREC_MEMCPY_OPT(dest, src, count);
5493d58139fSopenharmony_ci        return EOK;
5503d58139fSopenharmony_ci    }
5513d58139fSopenharmony_ci    /* Meet some runtime violation, return error code */
5523d58139fSopenharmony_ci    return SecMemcpyError(dest, destMax, src, count);
5533d58139fSopenharmony_ci}
5543d58139fSopenharmony_ci#endif
5553d58139fSopenharmony_ci
556