From aeddfe2c34c88c386749334746e4016431d10952 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 1 Dec 2006 15:11:58 +1100 Subject: [PATCH] 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. --- fdt.c | 12 ++++++++++++ fdt_sw.c | 13 ++++--------- libfdt_internal.h | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/fdt.c b/fdt.c index 0c1a5f0..d3844fc 100644 --- a/fdt.c +++ b/fdt.c @@ -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); diff --git a/fdt_sw.c b/fdt_sw.c index b2545b9..89abacb 100644 --- a/fdt_sw.c +++ b/fdt_sw.c @@ -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; diff --git a/libfdt_internal.h b/libfdt_internal.h index 91361c2..96f5fa8 100644 --- a/libfdt_internal.h +++ b/libfdt_internal.h @@ -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))