aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

as_util.c (1088B)


      1 #include "../config.h"
      2 
      3 #include "as_util.h"
      4 #ifndef ELF_NOT_SUPPORTED
      5 
      6 #include <assert.h>
      7 #include <stdio.h>
      8 #include <stdlib.h> /* realloc */
      9 #include <string.h> /* memcpy */
     10 
     11 #include "tcutil.h"
     12 
     13 void
     14 strtab_init(struct Strtab *strtab)
     15 {
     16   table_init(&strtab->offsets);
     17   strtab->size = 0;
     18 }
     19 
     20 size_t
     21 strtab_add(struct Strtab *strtab, const struct Name *name)
     22 {
     23   void *result;
     24   if (!table_try_get(&strtab->offsets, name, &result)) {
     25     size_t offset = strtab->size;
     26     table_put(&strtab->offsets, name, (void *)offset);
     27     strtab->size += name->bytes + 1;
     28     return offset;
     29   } else {
     30     return (size_t)result;
     31   }
     32 }
     33 
     34 void *
     35 strtab_dump(const struct Strtab *strtab)
     36 {
     37   void              *buf = malloc_or_die(strtab->size);
     38   unsigned char     *p   = buf;
     39   const struct Name *name;
     40   void              *value;
     41   for (int it = 0; (it = table_iterate(&strtab->offsets, it, &name, &value)) != -1;) {
     42     uintptr_t offset = VOIDP2UINT(value);
     43     memcpy(p + offset, name->chars, name->bytes);
     44     p[offset + name->bytes] = '\0';
     45   }
     46   return buf;
     47 }
     48 #endif /* !ELF_NOT_SUPPORTED */