2 7d2c0d24 2012-09-11 matthias * Copyright (c) 2006 Matthias Schmidt <xhr @ giessen.ccc.de>
4 ec08c9c0 2006-08-15 matthias> * Redistribution and use in source and binary forms, with or without
5 ec08c9c0 2006-08-15 matthias> * modification, are permitted provided that the following conditions
6 ec08c9c0 2006-08-15 matthias> * are met:
8 ec08c9c0 2006-08-15 matthias> * 1. Redistributions of source code must retain the above copyright
9 ec08c9c0 2006-08-15 matthias> * notice, this list of conditions and the following disclaimer.
10 ec08c9c0 2006-08-15 matthias> * 2. Redistributions in binary form must reproduce the above copyright
11 ec08c9c0 2006-08-15 matthias> * notice, this list of conditions and the following disclaimer in the
12 ec08c9c0 2006-08-15 matthias> * documentation and/or other materials provided with the distribution.
14 ec08c9c0 2006-08-15 matthias> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
15 ec08c9c0 2006-08-15 matthias> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ec08c9c0 2006-08-15 matthias> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ec08c9c0 2006-08-15 matthias> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
18 ec08c9c0 2006-08-15 matthias> * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ec08c9c0 2006-08-15 matthias> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ec08c9c0 2006-08-15 matthias> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ec08c9c0 2006-08-15 matthias> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ec08c9c0 2006-08-15 matthias> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ec08c9c0 2006-08-15 matthias> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 ec08c9c0 2006-08-15 matthias> * THE POSSIBILITY OF SUCH DAMAGE.
27 ec08c9c0 2006-08-15 matthias> /* $Id: list.c,v 1.2 2006/08/15 12:52:48 matthias Exp $ */
29 d327f82d 2006-08-15 matthias> #include <errno.h>
31 d327f82d 2006-08-15 matthias> #include "dermob.h"
32 d327f82d 2006-08-15 matthias> #include "list.h"
33 d327f82d 2006-08-15 matthias> #include "mach.h"
35 d327f82d 2006-08-15 matthias> void print_section(struct section *);
36 d327f82d 2006-08-15 matthias> void print_load_command(struct load_command *);
37 d327f82d 2006-08-15 matthias> void print_mo_header(struct mach_header *);
38 d327f82d 2006-08-15 matthias> void print_fat_header(struct fat_header *);
39 d327f82d 2006-08-15 matthias> void print_fat_arch(struct fat_arch *);
40 d327f82d 2006-08-15 matthias> void print_lc_towlevel_hints(struct twolevel_hints_command *two);
41 d327f82d 2006-08-15 matthias> void display_cmd_name(int);
42 d327f82d 2006-08-15 matthias> void display_cpu_arch(int);
43 d327f82d 2006-08-15 matthias> void print_lc_towlevel_hints(struct twolevel_hints_command *two);
44 d327f82d 2006-08-15 matthias> void print_lc_dysymtab(struct dysymtab_command *dsym);
45 d327f82d 2006-08-15 matthias> void print_lc_load_dylib(struct dylib_command *dly);
46 d327f82d 2006-08-15 matthias> void print_lc_segment(struct segment_command *sc);
47 d327f82d 2006-08-15 matthias> void print_lc_symtab(struct symtab_command *symc);
48 d327f82d 2006-08-15 matthias> void mprintf(const char *fmt, ...);
50 d327f82d 2006-08-15 matthias> struct list *
51 d327f82d 2006-08-15 matthias> list_create_list()
53 d327f82d 2006-08-15 matthias> struct list *list;
55 d327f82d 2006-08-15 matthias> list = malloc(sizeof(struct list));
57 d327f82d 2006-08-15 matthias> if (list != NULL) {
58 d327f82d 2006-08-15 matthias> list->head = list->tail = NULL;
59 d327f82d 2006-08-15 matthias> list->len = 0;
60 d327f82d 2006-08-15 matthias> return (list);
63 d327f82d 2006-08-15 matthias> return (NULL);
67 d327f82d 2006-08-15 matthias> list_insert_node(struct list *list, void *content, unsigned int code)
69 d327f82d 2006-08-15 matthias> struct node *new;
71 d327f82d 2006-08-15 matthias> new = malloc(sizeof(struct node));
73 d327f82d 2006-08-15 matthias> if (!new) {
74 d327f82d 2006-08-15 matthias> printf("malloc: %s\n", strerror(errno));
75 d327f82d 2006-08-15 matthias> return;
78 d327f82d 2006-08-15 matthias> new->content = content;
79 d327f82d 2006-08-15 matthias> new->code = code;
81 d327f82d 2006-08-15 matthias> if (list->head == NULL) {
82 d327f82d 2006-08-15 matthias> list->head = new;
83 d327f82d 2006-08-15 matthias> list->tail = new;
84 d327f82d 2006-08-15 matthias> /* We'll get two uninitialised pointer at the first node, if we
85 d327f82d 2006-08-15 matthias> * don't set prev and next to NULL */
86 d327f82d 2006-08-15 matthias> new->next = new->prev = NULL;
87 d327f82d 2006-08-15 matthias> list->len = 1;
88 d327f82d 2006-08-15 matthias> } else {
89 d327f82d 2006-08-15 matthias> list->head->prev = new;
90 d327f82d 2006-08-15 matthias> new->next = list->head;
91 d327f82d 2006-08-15 matthias> new->prev = NULL;
92 d327f82d 2006-08-15 matthias> list->head = new;
93 d327f82d 2006-08-15 matthias> list->len++;
98 d327f82d 2006-08-15 matthias> list_traverse_list(struct list *list)
100 d327f82d 2006-08-15 matthias> struct node *cursor;
101 d327f82d 2006-08-15 matthias> int na, nl, ns;
103 d327f82d 2006-08-15 matthias> na = nl = ns = 0;
105 d327f82d 2006-08-15 matthias> // Traverse the list backwards
106 d327f82d 2006-08-15 matthias> for (cursor = list->tail; cursor != NULL; cursor = cursor->prev) {
107 d327f82d 2006-08-15 matthias> if (cursor->code == 0x1)
108 d327f82d 2006-08-15 matthias> print_fat_header((struct fat_header *)cursor->content);
109 d327f82d 2006-08-15 matthias> else if (cursor->code == 0x2) {
110 d327f82d 2006-08-15 matthias> mprintf(" Architecture %d\n", ++na);
111 d327f82d 2006-08-15 matthias> print_fat_arch((struct fat_arch *) cursor->content);
112 d327f82d 2006-08-15 matthias> } else if (cursor->code == 0x3)
113 d327f82d 2006-08-15 matthias> print_mo_header((struct mach_header *) cursor->content);
114 d327f82d 2006-08-15 matthias> else if (cursor->code == 0x4) {
115 d327f82d 2006-08-15 matthias> mprintf(" - Load command: %d\n", ++nl);
116 d327f82d 2006-08-15 matthias> print_load_command((struct load_command *) cursor->content);
117 d327f82d 2006-08-15 matthias> } else if (cursor->code == 0x5) {
118 d327f82d 2006-08-15 matthias> mprintf(" + Section %d\n", ++ns);
119 d327f82d 2006-08-15 matthias> print_section((struct section *) cursor->content);
120 d327f82d 2006-08-15 matthias> } else if (cursor->code == 0x6)
121 d327f82d 2006-08-15 matthias> print_lc_segment((struct segment_command *) cursor->content);
122 d327f82d 2006-08-15 matthias> else if (cursor->code == 0x7)
123 d327f82d 2006-08-15 matthias> print_lc_symtab((struct symtab_command *) cursor->content);
124 d327f82d 2006-08-15 matthias> else if (cursor->code == 0x8)
125 d327f82d 2006-08-15 matthias> print_lc_load_dylib((struct dylib_command *) cursor->content);
126 d327f82d 2006-08-15 matthias> else if (cursor->code == 0x9)
127 d327f82d 2006-08-15 matthias> print_lc_dysymtab((struct dysymtab_command *) cursor->content);
135 d327f82d 2006-08-15 matthias> list_free_list(struct list *list)
137 d327f82d 2006-08-15 matthias> struct node *cursor;
139 d327f82d 2006-08-15 matthias> while ((cursor = list->head)) {
140 d327f82d 2006-08-15 matthias> list->head = cursor->next;
141 d327f82d 2006-08-15 matthias> free(cursor->content);
142 d327f82d 2006-08-15 matthias> free(cursor);
145 d327f82d 2006-08-15 matthias> free(list);
149 d327f82d 2006-08-15 matthias> list_remove_node(struct list *list, struct node *temp)
151 d327f82d 2006-08-15 matthias> /* cursor is the head of the list */
152 d327f82d 2006-08-15 matthias> if ((temp == list->head) && (temp != list->tail)) {
153 d327f82d 2006-08-15 matthias> list->head = temp->next;
154 d327f82d 2006-08-15 matthias> list->head->prev = NULL;
155 d327f82d 2006-08-15 matthias> /* cursor is the tail of the list */
156 d327f82d 2006-08-15 matthias> } else if ((temp != list->head) && (temp == list->tail)) {
157 d327f82d 2006-08-15 matthias> list->tail = temp->prev;
158 d327f82d 2006-08-15 matthias> list->tail->next = NULL;
159 d327f82d 2006-08-15 matthias> /* cursor is the only node */
160 d327f82d 2006-08-15 matthias> } else if ((temp == list->head) && (temp == list->tail)) {
161 d327f82d 2006-08-15 matthias> list->tail = list->head = NULL;
162 d327f82d 2006-08-15 matthias> /* cursor is between head and tail */
163 d327f82d 2006-08-15 matthias> } else {
164 d327f82d 2006-08-15 matthias> temp->prev->next = temp->next;
165 d327f82d 2006-08-15 matthias> temp->next->prev = temp->prev;
168 d327f82d 2006-08-15 matthias> free(temp->content);
169 d327f82d 2006-08-15 matthias> free(temp);
173 d327f82d 2006-08-15 matthias> list_remove(struct list *list, int (*cond)(struct node*))
175 d327f82d 2006-08-15 matthias> struct node *cursor,
176 d327f82d 2006-08-15 matthias> *temp;
178 d327f82d 2006-08-15 matthias> cursor = list->head;
179 d327f82d 2006-08-15 matthias> while (cursor != NULL) {
180 d327f82d 2006-08-15 matthias> temp = cursor;
182 d327f82d 2006-08-15 matthias> cursor = (cursor != list->tail) ? cursor->next : NULL;
184 d327f82d 2006-08-15 matthias> if ((*cond)(temp))
185 d327f82d 2006-08-15 matthias> list_remove_node(list, temp);