This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
wasm-micro-runtime/core/shared/utils/bh_common.c

97 lines
1.7 KiB
C
Raw Normal View History

/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_common.h"
int
b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
{
char *dest = (char *)s1;
char *src = (char *)s2;
if (n == 0) {
return 0;
}
if (s1 == NULL) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memcpy(dest, src, n);
return 0;
}
int
b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
{
char *dest = (char *)s1;
char *src = (char *)s2;
if (n == 0) {
return 0;
}
if (s1 == NULL) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memmove(dest, src, n);
return 0;
}
int
b_strcat_s(char *s1, unsigned int s1max, const char *s2)
{
if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1)) {
return -1;
}
memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
return 0;
}
int
b_strcpy_s(char *s1, unsigned int s1max, const char *s2)
{
if (NULL == s1 || NULL == s2 || s1max < (strlen(s2) + 1)) {
return -1;
}
memcpy(s1, s2, strlen(s2) + 1);
return 0;
}
char *
bh_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = BH_MALLOC(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}
char *
wa_strdup(const char *s)
{
uint32 size;
char *s1 = NULL;
if (s) {
size = (uint32)(strlen(s) + 1);
if ((s1 = WA_MALLOC(size)))
bh_memcpy_s(s1, size, s, size);
}
return s1;
}