~ubuntu-core-dev/module-init-tools/ubuntu

« back to all changes in this revision

Viewing changes to util.c

  • Committer: Scott James Remnant
  • Date: 2009-07-16 15:24:17 UTC
  • mfrom: (152.1.38)
  • Revision ID: scott@netsplit.com-20090716152417-7ak1sklxb59cs4fz
MergeĀ 3.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
114
114
}
115
115
 
116
116
/*
 
117
 * strtbl_add - add a string to a string table.
 
118
 *
 
119
 * @str: string to add
 
120
 * @tbl: current string table. NULL = allocate new table
 
121
 *
 
122
 * Allocates an array of pointers to strings.
 
123
 * The strings themselves are not actually kept in the table.
 
124
 *
 
125
 * Returns reallocated and updated string table. NULL = out of memory.
 
126
 *
 
127
 * Implementation note: The string table is designed to be lighter-weight
 
128
 * and faster than a more conventional linked list that stores the strings
 
129
 * in the list elements, as it does far fewer malloc/realloc calls
 
130
 * and avoids copying entirely.
 
131
 */
 
132
struct string_table *strtbl_add(const char *str, struct string_table *tbl)
 
133
{
 
134
        if (tbl == NULL) {
 
135
                const char max = 100;
 
136
                tbl = malloc(sizeof(*tbl) + sizeof(char *) * max);
 
137
                if (!tbl)
 
138
                        return NULL;
 
139
                tbl->max = max;
 
140
                tbl->cnt = 0;
 
141
        }
 
142
        if (tbl->cnt >= tbl->max) {
 
143
                tbl->max *= 2;
 
144
                tbl = realloc(tbl, sizeof(*tbl) + sizeof(char *) * tbl->max);
 
145
                if (!tbl)
 
146
                        return NULL;
 
147
        }
 
148
        tbl->str[tbl->cnt] = str;
 
149
        tbl->cnt += 1;
 
150
 
 
151
        return tbl;
 
152
}
 
153
 
 
154
/*
 
155
 * strtbl_free - string table destructor
 
156
 */
 
157
void strtbl_free(struct string_table *tbl)
 
158
{
 
159
        free(tbl);
 
160
}
 
161
 
 
162
/*
117
163
 * Get the basename in a pathname.
118
164
 * Unlike the standard implementation, this does not copy the string.
119
165
 */
157
203
        return (char) *((uint32_t*)("\1\0\0\2"));
158
204
}
159
205
 
160
 
/*
161
 
 * Check ELF file header.
162
 
 */
163
 
int elf_ident(void *file, unsigned long fsize, int *conv)
164
 
{
165
 
        /* "\177ELF" <byte> where byte = 001 for 32-bit, 002 for 64 */
166
 
        unsigned char *ident = file;
167
 
 
168
 
        if (fsize < EI_CLASS || memcmp(file, ELFMAG, SELFMAG) != 0)
169
 
                return -ENOEXEC;        /* Not an ELF object */
170
 
        if (ident[EI_DATA] == 0 || ident[EI_DATA] > 2)
171
 
                return -EINVAL;         /* Unknown endianness */
172
 
 
173
 
        if (conv != NULL)
174
 
                *conv = native_endianness() != ident[EI_DATA];
175
 
        return ident[EI_CLASS];
176
 
}
177
 
 
178
 
#define PERBIT(x) x##32
179
 
#define ElfPERBIT(x) Elf32_##x
180
 
#define ELFPERBIT(x) ELF32_##x
181
 
#include "elf_core.c"
182
 
 
183
 
#undef PERBIT
184
 
#undef ElfPERBIT
185
 
#undef ELFPERBIT
186
 
#define PERBIT(x) x##64
187
 
#define ElfPERBIT(x) Elf64_##x
188
 
#define ELFPERBIT(x) ELF64_##x
189
 
#include "elf_core.c"
190
 
 
191
 
void *get_section(void *file, unsigned long filesize,
192
 
                  const char *secname, unsigned long *secsize)
193
 
{
194
 
        int conv;
195
 
 
196
 
        switch (elf_ident(file, filesize, &conv)) {
197
 
        case ELFCLASS32:
198
 
                return get_section32(file, filesize, secname, secsize, conv);
199
 
        case ELFCLASS64:
200
 
                return get_section64(file, filesize, secname, secsize, conv);
201
 
        default:
202
 
                return NULL;
203
 
        }
204
 
}