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.
This commit is contained in:
12
fdt.c
12
fdt.c
@@ -92,6 +92,18 @@ uint32_t _fdt_next_tag(const struct fdt_header *fdt, int offset, int *nextoffset
|
|||||||
return tag;
|
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)
|
struct fdt_header *fdt_move(const struct fdt_header *fdt, void *buf, int bufsize)
|
||||||
{
|
{
|
||||||
int err = _fdt_check_header(fdt);
|
int err = _fdt_check_header(fdt);
|
||||||
|
|||||||
13
fdt_sw.c
13
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)
|
static int find_add_string(struct fdt_header *fdt, const char *s)
|
||||||
{
|
{
|
||||||
char *strtab = (char *)fdt + fdt_totalsize(fdt);
|
char *strtab = (char *)fdt + fdt_totalsize(fdt);
|
||||||
|
const char *p;
|
||||||
int strtabsize = fdt_size_dt_strings(fdt);
|
int strtabsize = fdt_size_dt_strings(fdt);
|
||||||
int len = strlen(s) + 1;
|
int len = strlen(s) + 1;
|
||||||
int struct_top, offset;
|
int struct_top, offset;
|
||||||
|
|
||||||
/* We treat string offsets as negative from the end of our buffer */
|
p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
|
||||||
/* then fix them up in fdt_finish() */
|
if (p)
|
||||||
offset = -strtabsize;
|
return p - strtab;
|
||||||
while ((offset < 0) && (memcmp(strtab + offset, s, len) != 0))
|
|
||||||
offset++;
|
|
||||||
|
|
||||||
if (offset < 0)
|
|
||||||
/* Found it */
|
|
||||||
return offset;
|
|
||||||
|
|
||||||
/* Add it */
|
/* Add it */
|
||||||
offset = -strtabsize - len;
|
offset = -strtabsize - len;
|
||||||
|
|||||||
@@ -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);
|
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,
|
struct fdt_property *_fdt_getprop(const struct fdt_header *fdt, int nodeoffset,
|
||||||
const char *name, int *lenp);
|
const char *name, int *lenp);
|
||||||
|
const char *_fdt_find_string(const char *strtab, int tabsize, const char *s);
|
||||||
|
|
||||||
#define OFFSET_ERROR(code) -(code)
|
#define OFFSET_ERROR(code) -(code)
|
||||||
#define PTR_ERROR(code) (void *)(-(code))
|
#define PTR_ERROR(code) (void *)(-(code))
|
||||||
|
|||||||
Reference in New Issue
Block a user