Files
mkrtos-real/mkrtos_user/lib/cpio/cpiofs.c
zhangzheng 3a10b63e5f init
支持解析脚步中的应用并自动执行
2023-11-27 22:25:21 +08:00

87 lines
1.8 KiB
C

#include "u_types.h"
#include "cpiofs.h"
#include "u_util.h"
#include <string.h>
int htoi(char *str, int len)
{
int n = 0;
int i = 0;
if (str == NULL || len <= 0)
return -1;
for (i = 0; i < len; i++)
{
if (str[i] != '0')
{
break;
}
}
str += i;
while (1)
{
if (*str >= '0' && *str <= '9')
{
n = 16 * n + (*str - '0');
}
else if (*str >= 'A' && *str <= 'F') // 十六进制还要判断字符是不是在A-F或者a-f之间
{
n = 16 * n + (*str - 'A' + 10);
}
else if (*str >= 'a' && *str <= 'f')
{
n = 16 * n + (*str - 'a' + 10);
}
else
{
break;
}
str++;
i++;
if (i >= len)
{
break;
}
}
return n;
}
umword_t cpio_find_file(umword_t st, umword_t en, const char *name, umword_t *size)
{
uint8_t rByte;
int32_t bk_inx;
cpio_fs_t *file_info;
for (umword_t i = st; st < en;)
{
file_info = (cpio_fs_t *)i;
if (check_magic((char *)file_info) < 0)
{
return 0;
}
int name_size = htoi(file_info->c_namesize, 8);
const char *f_name = (char *)(i + sizeof(cpio_fs_t));
if (strcmp(f_name, name) == 0)
{
if (size)
{
*size = htoi(file_info->c_filesize, 8);
}
return (umword_t)(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4));
}
if (strcmp("TRAILER!!", f_name) == 0)
{
return 0;
}
int file_size = htoi(file_info->c_filesize, 8);
i = ALIGN(ALIGN(i + sizeof(cpio_fs_t) + name_size, 4) +
file_size,
4);
}
return 0;
}