swab函数 (#53)

* swab函数

Signed-off-by: Yuqia <15287042733@163.com>

* 修正格式

Signed-off-by: Yuqia <15287042733@163.com>
Co-authored-by: fslongjin <longjin@RinGoTek.cn>
This commit is contained in:
DaJiYuQia
2022-10-07 15:45:49 +08:00
committed by GitHub
parent 9300757779
commit e62bbf13e5
5 changed files with 95 additions and 9 deletions

View File

@ -3,6 +3,7 @@
#include <libc/errno.h>
#include <libc/stdio.h>
#include <libc/stddef.h>
#include <libc/string.h>
/**
* @brief 关闭文件接口
@ -157,4 +158,27 @@ int execv(const char *path, char *const argv[])
int rmdir(const char *path)
{
return syscall_invoke(SYS_RMDIR, (uint64_t)path, 0, 0, 0, 0, 0, 0, 0);
}
/**
* @brief 交换n字节
* @param src 源地址
* @param dest 目的地址
* @param nbytes 交换字节数
*/
void swab(void *restrict src, void *restrict dest, ssize_t nbytes)
{
unsigned char buf[32];
char *_src = src;
char *_dest = dest;
uint32_t transfer;
for (; nbytes > 0; nbytes -= transfer)
{
transfer = (nbytes > 32) ? 32 : nbytes;
memcpy(buf, _src, transfer);
memcpy(_src, _dest, transfer);
memcpy(_dest, buf, transfer);
_src += transfer;
_dest += transfer;
}
}