1 /*
2 *
3 * Ophcrack is a Lanmanager/NTLM hash cracker based on the faster time-memory
4 * trade-off using rainbow tables.
5 *
6 * Created with the help of: Maxime Mueller, Luca Wullschleger, Claude
7 * Hochreutiner, Andreas Huber and Etienne Dysli.
8 *
9 * Copyright (c) 2008 Philippe Oechslin, Cedric Tissieres, Bertrand Mesot
10 *
11 * Ophcrack is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Ophcrack is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Ophcrack; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * This program is released under the GPL with the additional exemption
26 * that compiling, linking, and/or using OpenSSL is allowed.
27 *
28 *
29 *
30 *
31 */
32 /** @file list.c
33 * Implementation of a doubly linked list.
34 */
35
36 #include <stdlib.h>
37 #include <assert.h>
38
39 #include "list.h"
40 /*-------------------------------------------------------------------------*/
list_alloc(void)41 list_t *list_alloc(void) {
42 list_t *l = (list_t*)malloc(sizeof(list_t));
43
44 l->size = 0;
45 l->head = 0;
46 l->tail = 0;
47
48 return l;
49 }
50 /*-------------------------------------------------------------------------*/
list_free(list_t * l)51 void list_free(list_t *l) {
52 list_clear(l);
53 free(l);
54 }
55 /*-------------------------------------------------------------------------*/
list_clear(list_t * l)56 void list_clear(list_t *l) {
57 while (l->size)
58 list_rem_head(l);
59 }
60 /*-------------------------------------------------------------------------*/
list_add_head(list_t * l,void * data)61 void list_add_head(list_t *l, void *data) {
62 list_nd_t *nd = list_nd_alloc(data);
63 list_nd_t *old = l->head;
64
65 nd->next = old;
66 l->head = nd;
67 l->size += 1;
68
69 if (old != 0)
70 old->prev = nd;
71 else
72 l->tail = nd;
73 }
74 /*-------------------------------------------------------------------------*/
list_add_tail(list_t * l,void * data)75 void list_add_tail(list_t *l, void *data) {
76 list_nd_t *nd = list_nd_alloc(data);
77 list_nd_t *old = l->tail;
78
79 nd->prev = old;
80 l->tail = nd;
81 l->size += 1;
82
83 if (old != 0)
84 old->next = nd;
85 else
86 l->head = nd;
87 }
88 /*-------------------------------------------------------------------------*/
list_rem_head(list_t * l)89 void *list_rem_head(list_t *l) {
90 list_nd_t *nd = l->head;
91
92 if (nd == 0) return 0;
93
94 list_nd_t *old = nd->next;
95
96 l->head = old;
97 l->size -= 1;
98
99 if (old != 0)
100 old->prev = 0;
101 else
102 l->tail = 0;
103
104 return list_nd_free(nd);
105 }
106 /*-------------------------------------------------------------------------*/
list_rem_tail(list_t * l)107 void *list_rem_tail(list_t *l) {
108 list_nd_t *nd = l->tail;
109
110 if (nd == 0) return 0;
111
112 list_nd_t *old = nd->prev;
113
114 l->tail = old;
115 l->size -= 1;
116
117 if (old != 0)
118 old->next = 0;
119 else
120 l->head = 0;
121
122 return list_nd_free(nd);
123 }
124 /*-------------------------------------------------------------------------*/
list_nd_alloc(void * data)125 list_nd_t *list_nd_alloc(void *data) {
126 list_nd_t *nd = (list_nd_t*)malloc(sizeof(list_nd_t));
127
128 nd->next = 0;
129 nd->prev = 0;
130 nd->data = data;
131
132 return nd;
133 }
134 /*-------------------------------------------------------------------------*/
list_nd_free(list_nd_t * nd)135 void *list_nd_free(list_nd_t *nd) {
136 void *data = nd->data;
137
138 free(nd);
139
140 return data;
141 }
142