2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2019-02-27 18:39:32 +08:00
|
|
|
* Copyright (C) 2017-2019 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#if defined(__linux__)
|
|
|
|
|
|
|
|
#include <cstring>
|
2019-02-27 18:39:32 +08:00
|
|
|
#include <errno.h>
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <string>
|
|
|
|
|
2018-05-18 05:57:53 +08:00
|
|
|
inline int strcpy_s(char *dst, size_t dstSize, const char *src) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if ((dst == nullptr) || (src == nullptr)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
size_t length = strlen(src);
|
2018-05-18 05:57:53 +08:00
|
|
|
if (dstSize <= length) {
|
2017-12-21 07:45:38 +08:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2018-12-10 22:03:09 +08:00
|
|
|
memcpy(dst, src, length);
|
2018-05-18 05:57:53 +08:00
|
|
|
dst[length] = '\0';
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int strncpy_s(char *dst, size_t numberOfElements, const char *src, size_t count) {
|
|
|
|
if ((dst == nullptr) || (src == nullptr)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (numberOfElements < count) {
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t length = strlen(src);
|
|
|
|
if (length > count) {
|
|
|
|
length = count;
|
|
|
|
}
|
2018-12-10 22:03:09 +08:00
|
|
|
memcpy(dst, src, length);
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
if (length < numberOfElements) {
|
|
|
|
numberOfElements = length;
|
|
|
|
}
|
|
|
|
dst[numberOfElements] = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t strnlen_s(const char *str, size_t count) {
|
|
|
|
if (str == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
if (str[i] == '\0')
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int memcpy_s(void *dst, size_t destSize, const void *src, size_t count) {
|
|
|
|
if ((dst == nullptr) || (src == nullptr)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (destSize < count) {
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dst, src, count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int memmove_s(void *dst, size_t numberOfElements, const void *src, size_t count) {
|
|
|
|
if ((dst == nullptr) || (src == nullptr)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (numberOfElements < count) {
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(dst, src, count);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|