/*
 * Copyright (c) 2026 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef SECUREC_H
#define SECUREC_H

#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#ifndef SECUREC_ATTRIBUTE
#if defined(__GNUC__) || defined(__clang__)
#define SECUREC_ATTRIBUTE(x, y) __attribute__((format(printf, (x), (y))))
#else
#define SECUREC_ATTRIBUTE(x, y)
#endif
#endif

#ifndef EOK
#define EOK 0
#endif

#ifndef EINVAL
#define EINVAL 22
#endif

#ifndef ERANGE
#define ERANGE 34
#endif

#ifndef errno_t
typedef int errno_t;
#endif

#ifdef __cplusplus
extern "C" {
#endif

static inline errno_t memset_s(void* dest, size_t destMax, int c, size_t count)
{
    if (dest == NULL || count > destMax) {
        return (dest == NULL) ? EINVAL : ERANGE;
    }
    memset(dest, c, count);
    return EOK;
}

static inline errno_t memmove_s(void* dest, size_t destMax, const void* src, size_t count)
{
    if (dest == NULL || src == NULL || count > destMax) {
        return (dest == NULL || src == NULL) ? EINVAL : ERANGE;
    }
    memmove(dest, src, count);
    return EOK;
}

static inline errno_t memcpy_s(void* dest, size_t destMax, const void* src, size_t count)
{
    if (dest == NULL || src == NULL || count > destMax) {
        return (dest == NULL || src == NULL) ? EINVAL : ERANGE;
    }
    memcpy(dest, src, count);
    return EOK;
}

static inline errno_t strcpy_s(char* strDest, size_t destMax, const char* strSrc)
{
    if (strDest == NULL || strSrc == NULL || destMax == 0) {
        return EINVAL;
    }
    if (strlen(strSrc) >= destMax) {
        /* C11 Annex K:目标空间不足属运行期约束违规,置空目标串再报错 */
        strDest[0] = '\0';
        return ERANGE;
    }
    strcpy(strDest, strSrc);
    return EOK;
}

static inline errno_t strncpy_s(char* strDest, size_t destMax, const char* strSrc, size_t count)
{
    if (strDest == NULL || destMax == 0) {
        return EINVAL;
    }
    if (strSrc == NULL) {
        strDest[0] = '\0';
        return EINVAL;
    }
    size_t copy_len = 0;
    while (copy_len < count && copy_len < destMax - 1 && strSrc[copy_len] != '\0') {
        strDest[copy_len] = strSrc[copy_len];
        copy_len++;
    }
    /* 目标空间不足导致的截断(源未拷完、且不是 count 的有意截断):
     * C11 K.3.7.1.4 要求置空目标串并返回错误,调用方能据返回值发现截断。
     * 注意 count 限长拷贝(copy_len == count)是标准定义的成功路径,
     * 仓内调用点(如 strncpy_s(dst, N, src, N - 1))依赖该语义有意截断。 */
    if (copy_len == destMax - 1 && copy_len < count && strSrc[copy_len] != '\0') {
        strDest[0] = '\0';
        return ERANGE;
    }
    /* null 终止并清零剩余空间(C11 Annex K),防止栈上敏感数据残留 */
    memset(strDest + copy_len, 0, destMax - copy_len);
    return EOK;
}

static inline errno_t strcat_s(char* strDest, size_t destMax, const char* strSrc)
{
    if (strDest == NULL || strSrc == NULL || destMax == 0) {
        return EINVAL;
    }
    size_t dest_len = strlen(strDest);
    if (dest_len >= destMax || strlen(strSrc) >= destMax - dest_len) {
        return ERANGE;
    }
    strcat(strDest, strSrc);
    return EOK;
}

static inline int sprintf_s(char* strDest, size_t destMax, const char* format, ...) SECUREC_ATTRIBUTE(3, 4);

static inline int sprintf_s(char* strDest, size_t destMax, const char* format, ...)
{
    if (strDest == NULL || format == NULL || destMax == 0) {
        return -1;
    }
    va_list args;
    va_start(args, format);
    int ret = vsnprintf(strDest, destMax, format, args);
    va_end(args);
    return (ret >= 0 && (size_t)ret < destMax) ? ret : -1;
}

static inline int vsnprintf_s(char* strDest, size_t destMax, size_t count, const char* format, va_list argList)
    SECUREC_ATTRIBUTE(4, 0);

static inline int vsnprintf_s(char* strDest, size_t destMax, size_t count, const char* format, va_list argList)
{
    if (strDest == NULL || format == NULL || destMax == 0) {
        return -1;
    }
    size_t limit = (count < destMax - 1) ? count : destMax - 1;
    int    ret   = vsnprintf(strDest, limit + 1, format, argList);
    return (ret >= 0 && (size_t)ret <= limit) ? ret : -1;
}

static inline int snprintf_s(char* strDest, size_t destMax, size_t count, const char* format, ...)
    SECUREC_ATTRIBUTE(4, 5);

static inline int snprintf_s(char* strDest, size_t destMax, size_t count, const char* format, ...)
{
    if (strDest == NULL || format == NULL || destMax == 0) {
        return -1;
    }
    size_t  limit = (count < destMax - 1) ? count : destMax - 1;
    va_list args;
    va_start(args, format);
    int ret = vsnprintf(strDest, limit + 1, format, args);
    va_end(args);
    return (ret >= 0 && (size_t)ret <= limit) ? ret : -1;
}

/* 刻意不提供 sscanf_s:当前全仓无消费者,且最小实现无法兑现 C11 Annex K
 * 对 %s/%c/%[ 必须带缓冲区大小参数的核心安全语义——提供一个按普通 vsscanf
 * 转发的假 _s 版本只会给调用方虚假的安全感。未来若有消费者,需按 Annex K
 * 语义完整实现(解析格式串并强制宽度限定),而非在此转发。 */

#ifdef __cplusplus
}
#endif

#endif