mips/masm/asm/strtab.c

55 lines
1 KiB
C
Raw Permalink Normal View History

2024-09-11 16:06:09 +00:00
#include <merror.h>
#include <string.h>
#include <stdlib.h>
#include "elf32.h"
2024-09-11 16:06:09 +00:00
2024-10-04 23:41:10 +00:00
int strtab_get_str(struct elf_str_table *strtab, const char *str, size_t *res)
2024-09-11 16:06:09 +00:00
{
for (size_t i = 0; i < strtab->size; i ++) {
if (strcmp(strtab->ptr + i, str) == 0) {
if (res != NULL)
*res = i;
return M_SUCCESS;
}
}
return M_ERROR;
}
2024-10-04 23:41:10 +00:00
int strtab_write_str(struct elf_str_table *strtab, const char *str, size_t *res)
2024-09-11 16:06:09 +00:00
{
if (strtab_get_str(strtab, str, res) == M_SUCCESS)
return M_SUCCESS;
size_t len = strlen(str);
char *new = realloc(strtab->ptr, strtab->size + len + 1);
if (new == NULL)
return M_ERROR;
strtab->ptr = new;
memcpy(strtab->ptr + strtab->size, str, len + 1);
if (res != NULL)
*res = strtab->size;
strtab->size += len + 1;
return M_SUCCESS;
}
2024-10-04 23:41:10 +00:00
int strtab_init(struct elf_str_table *strtab)
2024-09-11 16:06:09 +00:00
{
strtab->size = 1;
strtab->ptr = malloc(1);
if (strtab->ptr == NULL) {
2024-09-22 20:02:42 +00:00
PERROR("cannot alloc");
2024-09-11 16:06:09 +00:00
return M_ERROR;
}
*strtab->ptr = '\0';
return M_SUCCESS;
}
2024-10-04 23:41:10 +00:00
void strtab_free(struct elf_str_table *strtab)
2024-09-11 16:06:09 +00:00
{
free(strtab->ptr);
}