Browse Source

libfdt: Factor out string search function

This patch pulls out the logic for finding a string in the string table
into _fdt_find_string(), from fdt_sw.c's find_add_string().  This function
will be useful for random-access read-write functions.  In the process
clean up the search logic a little.
main
David Gibson 18 years ago
parent
commit
aeddfe2c34
  1. 12
      fdt.c
  2. 13
      fdt_sw.c
  3. 2
      libfdt_internal.h

12
fdt.c

@ -92,6 +92,18 @@ uint32_t _fdt_next_tag(const struct fdt_header *fdt, int offset, int *nextoffset @@ -92,6 +92,18 @@ uint32_t _fdt_next_tag(const struct fdt_header *fdt, int offset, int *nextoffset
return tag;
}

const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
{
int len = strlen(s) + 1;
const char *last = strtab + tabsize - len;
const char *p;

for (p = strtab; p <= last; p++)
if (memeq(p, s, len))
return p;
return NULL;
}

struct fdt_header *fdt_move(const struct fdt_header *fdt, void *buf, int bufsize)
{
int err = _fdt_check_header(fdt);

13
fdt_sw.c

@ -131,19 +131,14 @@ int fdt_end_node(struct fdt_header *fdt) @@ -131,19 +131,14 @@ int fdt_end_node(struct fdt_header *fdt)
static int find_add_string(struct fdt_header *fdt, const char *s)
{
char *strtab = (char *)fdt + fdt_totalsize(fdt);
const char *p;
int strtabsize = fdt_size_dt_strings(fdt);
int len = strlen(s) + 1;
int struct_top, offset;

/* We treat string offsets as negative from the end of our buffer */
/* then fix them up in fdt_finish() */
offset = -strtabsize;
while ((offset < 0) && (memcmp(strtab + offset, s, len) != 0))
offset++;

if (offset < 0)
/* Found it */
return offset;
p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
if (p)
return p - strtab;

/* Add it */
offset = -strtabsize - len;

2
libfdt_internal.h

@ -30,7 +30,7 @@ int _fdt_check_header(const struct fdt_header *fdt); @@ -30,7 +30,7 @@ int _fdt_check_header(const struct fdt_header *fdt);
uint32_t _fdt_next_tag(const struct fdt_header *fdt, int startoffset, int *nextoffset);
struct fdt_property *_fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
const char *name, int *lenp);

const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);

#define OFFSET_ERROR(code) -(code)
#define PTR_ERROR(code) (void *)(-(code))

Loading…
Cancel
Save