diff options
Diffstat (limited to 'src/common')
-rwxr-xr-x | src/common/Makefile.am | 1 | ||||
-rw-r--r-- | src/common/dllist.c | 154 | ||||
-rw-r--r-- | src/common/dllist.h | 130 |
3 files changed, 285 insertions, 0 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 323ba17..a27a192 100755 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -2,6 +2,7 @@ lib_LIBRARIES = libcommon.a libcommon_a_SOURCES = \ + dllist.h dllist.c \ endianness.h endianness.c libcommon_a_CFLAGS = $(AM_CFLAGS) diff --git a/src/common/dllist.c b/src/common/dllist.c new file mode 100644 index 0000000..10f73e1 --- /dev/null +++ b/src/common/dllist.c @@ -0,0 +1,154 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * dllist.c - implantation simple des listes doublement chaînées + * + * Copyright (C) 2008 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * OpenIDA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "dllist.h" + + +#include <stdbool.h> + + + +/****************************************************************************** +* * +* Paramètres : new = nouvel élément à ajouter. * +* head = adresse d'enregistrement de la tête de la liste. * +* prev = élément précédent dans la liste. * +* next = élément suivant dans la liste. * +* * +* Description : Ajoute un élément dans une liste doublement chaînée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void __dl_list_add(dl_list_item *new, dl_list_head *head, dl_list_item *prev, dl_list_item *next) +{ + if (prev != NULL) prev->next = new; + new->prev = prev; + + new->next = next; + if (next != NULL) next->prev = new; + + if (*head == NULL) + *head = new; + +} + + +/****************************************************************************** +* * +* Paramètres : prev = élément précédent dans la liste. * +* next = élément suivant dans la liste. * +* * +* Description : Supprime un élément d'une liste doublement chaînée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void __dl_list_del(dl_list_item *prev, dl_list_item *next) +{ + next->prev = prev; + prev->next = next; + +} + + +/****************************************************************************** +* * +* Paramètres : head = début de la liste, à mettre éventuellement à jour. * +* a = premier élément à traiter. * +* b = second élément à traiter. * +* * +* Description : Intervertit deux éléments dans une liste doublement chaînée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void swap_dl_list_items(dl_list_head *head, dl_list_item *a, dl_list_item *b) +{ + bool a_is_head; /* Indique si a est le début */ + bool b_is_head; /* Indique si b est le début */ + dl_list_item tmp; /* Stockage temporaire */ + + a_is_head = (*head == a); + b_is_head = (*head == b); + + /* Liens vers l'extérieur et l'intérieur */ + + if (a->prev != b) a->prev->next = b; + if (a->next != b) a->next->prev = b; + + if (b->prev != a) b->prev->next = a; + if (b->next != a) b->next->prev = a; + + /* Liens propres aux éléments */ + + tmp = *a; + + a->prev = (b->prev == a ? b : b->prev); + a->next = (b->next == a ? b : b->next); + + b->prev = (tmp.prev == b ? a : tmp.prev); + b->next = (tmp.next == b ? a : tmp.next); + + /* Mise à jour éventuelle de la tête */ + + if (a_is_head) *head = b; + else if (b_is_head) *head = a; + +} + + +/****************************************************************************** +* * +* Paramètres : list = liste à parcourir. * +* * +* Description : Compte le nombre d'éléments présents dans une liste. * +* * +* Retour : Nombre d'éléments comptabilisés. * +* * +* Remarques : - * +* * +******************************************************************************/ + +unsigned int count_dl_list_items(dl_list_head list) +{ + unsigned int result; /* Résultat à renvoyer */ + dl_list_item *iter; /* Boucle de parcours */ + + result = 0; + + dl_list_for_each(iter, list, dl_list_item *) + result++; + + return result; + +} diff --git a/src/common/dllist.h b/src/common/dllist.h new file mode 100644 index 0000000..970edf9 --- /dev/null +++ b/src/common/dllist.h @@ -0,0 +1,130 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * dllist.h - prototypes de l'implantation simple des listes doublement chaînées + * + * Copyright (C) 2008 Cyrille Bagard + * + * This file is part of OpenIDA. + * + * OpenIDA is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * OpenIDA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _DLLIST_H +#define _DLLIST_H + + +#define NULL ((void *)0) + + + +/* Structure à inclure en tête de structure */ +typedef struct _dl_list_item +{ + struct _dl_list_item *prev; /* Elément précédent */ + struct _dl_list_item *next; /* Elément suivant */ + +} dl_list_item; + + +typedef dl_list_item *dl_list_head; + + +#define DL_LIST_ITEM dl_list_item dummy + +#define DLL_CAST(item) ((dl_list_item *)item) + +#define DL_LIST_HEAD_INIT(head) \ + *(head) = NULL + +#define DL_LIST_ITEM_INIT(item) \ + do \ + { \ + DLL_CAST(item)->prev = NULL; \ + DLL_CAST(item)->next = NULL; \ + \ + } while(0) + +#define DL_LIST_NEXT(item, type) (type)DLL_CAST(item)->next +#define DL_LIST_PREV(item, type) (type)DLL_CAST(item)->prev + +#define DL_LIST_IS_ALONE(head) (DLL_CAST(head)->next == DLL_CAST(head)) + + +/* Ajoute un élément dans une liste doublement chaînée. */ +void __dl_list_add(dl_list_item *, dl_list_head *, dl_list_item *, dl_list_item *); + +/* Supprime un élément d'une liste doublement chaînée. */ +void __dl_list_del(dl_list_item *, dl_list_item *); + +/* Intervertit deux éléments dans une liste doublement chaînée. */ +void swap_dl_list_items(dl_list_head *, dl_list_item *, dl_list_item *); + +/* Compte le nombre d'éléments présents dans une liste. */ +unsigned int count_dl_list_items(dl_list_head); + + +#define dl_list_empty(head) \ + ((head) == NULL) + +#define dl_list_last(head, type) \ + (dl_list_empty(head) ? NULL : (type)((head)->prev == NULL ? (head) : (head)->prev)) + +#define dl_list_add(new, head) \ + __dl_list_add(DLL_CAST(new), (head), (dl_list_empty(*(head)) ? DLL_CAST(new) : dl_list_last(*head, dl_list_item *)), (dl_list_empty(*(head)) ? DLL_CAST(new) : *(head))) + +#define dl_list_add_tail(new, head) \ + __dl_list_add(DLL_CAST(new), (head), (dl_list_empty(*(head)) ? DLL_CAST(new) : (*(head))->prev), (dl_list_empty(*(head)) ? DLL_CAST(new) : *(head))) + +#define dl_list_del(item, head) \ + do \ + { \ + __dl_list_del(DLL_CAST(item)->prev, DLL_CAST(item)->next); \ + if (*head == DLL_CAST(item)) *head = DLL_CAST(item)->next; \ + if (*head == DLL_CAST(item)) *head = NULL; \ + DLL_CAST(item)->next = NULL; \ + DLL_CAST(item)->prev = NULL; \ + \ + } while(0) + +#define dl_list_move_tail(item, head) \ + do \ + { \ + dl_list_del(item, head); \ + dl_list_add_tail(item, head); \ + \ + } while(0) + +#define dl_list_for_each(pos, head, type) \ + for (pos = (type)(head); \ + pos != NULL; \ + pos = (type)(DLL_CAST(pos)->next == (head) ? NULL : DLL_CAST(pos)->next)) + +#define dl_list_for_each_safe(pos, head, tmp, type) \ + for (pos = (type)*(head), \ + tmp = (type)(pos != NULL && DLL_CAST(pos)->next != *(head) ?\ + DLL_CAST(pos)->next : NULL); \ + pos != NULL; \ + pos = tmp, \ + tmp = (type)(pos != NULL && DLL_CAST(pos)->next != *(head) ?\ + DLL_CAST(pos)->next : NULL)) + +#define dl_list_for_each_prev(pos, head, type) \ + for (pos = dl_list_last(head, type); \ + pos != NULL; \ + pos = (type)(DLL_CAST(pos) == (head) ? NULL : DLL_CAST(pos)->prev)) + + + +#endif /* _DLLIST_H */ |