File: hash.c

package info (click to toggle)
ncurses-hexedit 0.9.7%2Borig-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,824 kB
  • sloc: ansic: 7,418; sh: 332; makefile: 45
file content (280 lines) | stat: -rw-r--r-- 6,383 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* hash.c by Adam Rogoyski <apoc@laker.net> Temperanc on EFNet irc
 * Copyright (C) 1998, 1999 Adam Rogoyski
 * --- GNU General Public License Disclaimer ---
 * This program 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 2 of the License, or
 * (at your option) any later version.
 * This program 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.
 */


/* This file contains a simple hash for looking up changes we may have
 * made to specific offsets in the file.  When we are editing a large
 * file that cannot fit in memory, when we reread a portion of the file
 * from disk, it will be the original, and not the changed, so we need
 * to lookup changes and add them to the current buffer in memory.
 * The Hash function is just a mod 1009 into the hash table of 0-1008
 * entries.  Collisions are handled with just a list of entries for
 * that index in the hash.  The key/pair is offset/byte-value.
 */

#include "hexedit.h"


   /* All the books say this should usually be a prime number */
#define HASH_TABLE_SIZE   1009

   /* The really complex and thought out hash function.  */
#define HASH(x)    (x % HASH_TABLE_SIZE)


   /* list of chained collisions */
struct hash_list
{
   unsigned long offset;
   unsigned char value;
   struct hash_list *next;
};

struct hash_table
{
   struct hash_list *chain;
};

   /* hash table */
static struct hash_table *table = NULL;



void
init_hash ()
   /* set each location empty by setting *next chain to -1 */
{
   int i = 0;

   if (table)
   {
      struct hash_list *p = NULL;

      for (i = 0; i < HASH_TABLE_SIZE; i++)
      {
         while (table[i].chain)
         {
            p = table[i].chain->next;
            free (table[i].chain);
            table[i].chain = p;
         }
      }
      
      free (table);
   }

   table = malloc (HASH_TABLE_SIZE * sizeof (struct hash_table));
   if (!table)
      die_horribly ("Not enough memory to create hash table\n", NULL);

   for (i = 0; i < HASH_TABLE_SIZE; i++)
   {
      table[i].chain = NULL;
   }
}


void
clear_hash ()
{
   int i = 0;
   struct hash_list *p = NULL;

   for (i = 0; i < HASH_TABLE_SIZE; i++)
   {
      while (table[i].chain)
      {
         p = table[i].chain;
         table[i].chain = table[i].chain->next;
         free (p);
      }
   }
}


void
insert_hash_entry (unsigned long offset, unsigned char value)
{
   int i = HASH(offset);

   if (!table[i].chain)
      /* empty location in table */
   {
      table[i].chain = malloc (sizeof (struct hash_list));
      if (!table[i].chain)
         die_horribly (NOT_ENOUGH_MEMORY, NULL);

      table[i].chain->offset = offset;
      table[i].chain->value  = value;
      table[i].chain->next   = NULL;
   }
   else
      /* Has at least one element in table.  Insert the new collision
       * into the front of the chain.
       */
   {
      struct hash_list *p = table[i].chain;

         /* First, make sure entry isn't already here, if it is, we 
          * change it to the new value.
          */
      while (p)
      {
         if (p->offset == offset)
         {
            p->value = value;
            break;
         }
         p = p->next;
      }
      if (p)
         return;       /* already in hash */

      p = malloc (sizeof (struct hash_list));
      if (!p)
         die_horribly (NOT_ENOUGH_MEMORY, NULL);

         /* build the new entry */
      p->offset = offset;
      p->value  = value;
      p->next   = table[i].chain;

         /* now put entry in the front of the list */
      table[i].chain = p;
   }
}


void
delete_hash_entry (unsigned long offset)
{
   int i = HASH(offset);
   struct hash_list *p = table[i].chain;

      /* hash entry not here */
   if (!p)
      return;

      /* one or more at this index */
   else
   {
      struct hash_list *last = NULL;
      while (p)
      {
         if (p->offset == offset)
            break;
         last = p;
         p = p->next;
      }
         /* if we found it delete it */
      if (p)
      {
            /* if item isn't first element */
         if (last)
         {
            last->next = p->next;   /* skip over p */
            free (p);               /* then delete it */
         }
         else
         {
            table[i].chain = p->next;  /* skip over p */
            free (p);                   /* then delete it */
         }
      }
   }
}


int
hash_lookup (unsigned long offset, unsigned char *c)
   /* return -1 on lookup failure.  Store value in *c if successful */
{
   int i = HASH(offset);
   struct hash_list *p = table[i].chain;
   
   while (p)
   {
      if (p->offset == offset)
         break;
      p = p->next;
   }

   if (p)
   {
      if (c)
         *c = p->value;
      return 0;
   }

   return -1;
}


#ifdef DEBUG_HASH
void
print_hash_table ()
{
   int i = 0;

   for (i = 0; i < HASH_TABLE_SIZE; i++)
   {
      fprintf (stderr, "%d: ", i);
         /* empty entry */
      if (!table[i].chain)
         fprintf (stderr, "empty\n");
      else
      {
         struct hash_list *p = table[i].chain;
         while (p)
         {
            fprintf (stderr, "(%ld,%d) ", p->offset, p->value);
            p = p->next;
         }
         fprintf (stderr, "\n");
      }
   }
}
#endif


void
commit_changes_in_hash (FILE *fp)
   /* cycle through the hash and write back all the entries, and clear
    * the hash while we're at it.
    */
{
   int i = 0;
   int result = 0;
   struct hash_list *p = NULL;

   for (i = 0; i < HASH_TABLE_SIZE; i++)
   {
      p = table[i].chain;
      while (table[i].chain)
      {
         result = fseek (fp, table[i].chain->offset, SEEK_SET);
         if (result)
            die_horribly ("Cannot write new file correctly", "fseek");

         result = fwrite (&table[i].chain->value, 1, 1, fp);
         if (!result && ferror (fp))
            die_horribly ("File writing error", "ferror");
         else if (!result && feof (fp))
            die_horribly ("End of File Reached?", "feof");

         p = table[i].chain->next;
         free (table[i].chain);
         table[i].chain = p;
      }
   }
}