2009-03-14 17:50:36 +01:00
|
|
|
/*
|
2007-11-17 21:05:40 +01:00
|
|
|
Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of version 2 of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "dutil.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2009-04-03 16:47:28 +02:00
|
|
|
#include <errno.h>
|
2007-11-17 21:05:40 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2009-03-18 15:08:23 +01:00
|
|
|
void *zalloc(const size_t size)
|
|
|
|
{
|
|
|
|
void *s = malloc(size);
|
|
|
|
if (s != NULL)
|
|
|
|
memset(s, 0, size);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-06-18 19:02:29 +02:00
|
|
|
struct str_node *str_node__new(const char *s, bool dupstr)
|
2007-11-17 21:05:40 +01:00
|
|
|
{
|
2009-06-18 19:02:29 +02:00
|
|
|
struct str_node *self = malloc(sizeof(*self));
|
|
|
|
|
|
|
|
if (self != NULL){
|
|
|
|
if (dupstr) {
|
|
|
|
s = strdup(s);
|
|
|
|
if (s == NULL)
|
|
|
|
goto out_delete;
|
|
|
|
}
|
|
|
|
self->s = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
|
|
|
out_delete:
|
|
|
|
free(self);
|
|
|
|
return NULL;
|
2007-11-17 21:05:40 +01:00
|
|
|
}
|
|
|
|
|
2009-06-18 19:02:29 +02:00
|
|
|
static void str_node__delete(struct str_node *self, bool dupstr)
|
2007-11-17 21:05:40 +01:00
|
|
|
{
|
2009-06-18 19:02:29 +02:00
|
|
|
if (dupstr)
|
|
|
|
free((void *)self->s);
|
|
|
|
free(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
int strlist__add(struct strlist *self, const char *new_entry)
|
|
|
|
{
|
|
|
|
struct rb_node **p = &self->entries.rb_node;
|
|
|
|
struct rb_node *parent = NULL;
|
|
|
|
struct str_node *sn;
|
|
|
|
|
|
|
|
while (*p != NULL) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
parent = *p;
|
|
|
|
sn = rb_entry(parent, struct str_node, rb_node);
|
|
|
|
rc = strcmp(sn->s, new_entry);
|
|
|
|
|
|
|
|
if (rc > 0)
|
|
|
|
p = &(*p)->rb_left;
|
|
|
|
else if (rc < 0)
|
|
|
|
p = &(*p)->rb_right;
|
|
|
|
else
|
2009-04-03 16:47:28 +02:00
|
|
|
return -EEXIST;
|
2009-06-18 19:02:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sn = str_node__new(new_entry, self->dupstr);
|
|
|
|
if (sn == NULL)
|
2009-04-03 16:47:28 +02:00
|
|
|
return -ENOMEM;
|
2007-11-17 21:05:40 +01:00
|
|
|
|
2009-06-18 19:02:29 +02:00
|
|
|
rb_link_node(&sn->rb_node, parent, p);
|
|
|
|
rb_insert_color(&sn->rb_node, &self->entries);
|
|
|
|
|
2007-11-17 21:05:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-16 17:55:50 +01:00
|
|
|
int strlist__load(struct strlist *self, const char *filename)
|
2007-11-17 21:05:40 +01:00
|
|
|
{
|
|
|
|
char entry[1024];
|
|
|
|
int err = -1;
|
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (fgets(entry, sizeof(entry), fp) != NULL) {
|
|
|
|
const size_t len = strlen(entry);
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
continue;
|
|
|
|
entry[len - 1] = '\0';
|
2009-03-14 17:50:36 +01:00
|
|
|
|
2007-12-16 17:55:50 +01:00
|
|
|
if (strlist__add(self, entry) != 0)
|
2007-11-17 21:05:40 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2009-03-14 17:50:36 +01:00
|
|
|
|
2007-11-17 21:05:40 +01:00
|
|
|
err = 0;
|
|
|
|
out:
|
|
|
|
fclose(fp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-16 17:55:50 +01:00
|
|
|
struct strlist *strlist__new(bool dupstr)
|
2007-11-17 21:05:40 +01:00
|
|
|
{
|
2007-12-16 17:55:50 +01:00
|
|
|
struct strlist *self = malloc(sizeof(*self));
|
2007-11-17 21:05:40 +01:00
|
|
|
|
|
|
|
if (self != NULL) {
|
2009-06-18 19:02:29 +02:00
|
|
|
self->entries = RB_ROOT;
|
2007-12-16 17:55:50 +01:00
|
|
|
self->dupstr = dupstr;
|
2007-11-17 21:05:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-12-16 17:55:50 +01:00
|
|
|
void strlist__delete(struct strlist *self)
|
2007-11-17 21:05:40 +01:00
|
|
|
{
|
|
|
|
if (self != NULL) {
|
2009-06-18 19:02:29 +02:00
|
|
|
struct str_node *pos;
|
|
|
|
struct rb_node *next = rb_first(&self->entries);
|
|
|
|
|
|
|
|
while (next) {
|
|
|
|
pos = rb_entry(next, struct str_node, rb_node);
|
|
|
|
next = rb_next(&pos->rb_node);
|
|
|
|
strlist__remove(self, pos);
|
|
|
|
}
|
|
|
|
self->entries = RB_ROOT;
|
2007-11-17 21:05:40 +01:00
|
|
|
free(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-18 19:02:29 +02:00
|
|
|
void strlist__remove(struct strlist *self, struct str_node *sn)
|
|
|
|
{
|
|
|
|
rb_erase(&sn->rb_node, &self->entries);
|
|
|
|
str_node__delete(sn, self->dupstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool strlist__has_entry(struct strlist *self, const char *entry)
|
2007-11-17 21:05:40 +01:00
|
|
|
{
|
2009-06-18 19:02:29 +02:00
|
|
|
struct rb_node **p = &self->entries.rb_node;
|
|
|
|
struct rb_node *parent = NULL;
|
|
|
|
|
|
|
|
while (*p != NULL) {
|
|
|
|
struct str_node *sn;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
parent = *p;
|
|
|
|
sn = rb_entry(parent, struct str_node, rb_node);
|
|
|
|
rc = strcmp(sn->s, entry);
|
|
|
|
|
|
|
|
if (rc > 0)
|
|
|
|
p = &(*p)->rb_left;
|
|
|
|
else if (rc < 0)
|
|
|
|
p = &(*p)->rb_right;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-11-17 21:05:40 +01:00
|
|
|
}
|
2009-03-24 20:56:19 +01:00
|
|
|
|
|
|
|
Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
|
2009-04-03 16:53:05 +02:00
|
|
|
GElf_Shdr *shp, const char *name, size_t *index)
|
2009-03-24 20:56:19 +01:00
|
|
|
{
|
|
|
|
Elf_Scn *sec = NULL;
|
2009-04-03 16:53:05 +02:00
|
|
|
size_t cnt = 1;
|
2009-03-24 20:56:19 +01:00
|
|
|
|
|
|
|
while ((sec = elf_nextscn(elf, sec)) != NULL) {
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
gelf_getshdr(sec, shp);
|
|
|
|
str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
|
2009-04-03 16:53:05 +02:00
|
|
|
if (!strcmp(name, str)) {
|
|
|
|
if (index)
|
|
|
|
*index = cnt;
|
2009-03-24 20:56:19 +01:00
|
|
|
break;
|
2009-04-03 16:53:05 +02:00
|
|
|
}
|
|
|
|
++cnt;
|
2009-03-24 20:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sec;
|
|
|
|
}
|