ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

hash.c
Go to the documentation of this file.
00001 /* hash.c
00002 
00003    Routines for manipulating hash tables... */
00004 
00005 /*
00006  * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. Neither the name of The Internet Software Consortium nor the names
00019  *    of its contributors may be used to endorse or promote products derived
00020  *    from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
00023  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
00024  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00025  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00026  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
00027  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00030  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00033  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  *
00036  * This software has been written for the Internet Software Consortium
00037  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
00038  * Enterprises.  To learn more about the Internet Software Consortium,
00039  * see ``http://www.vix.com/isc''.  To learn more about Vixie
00040  * Enterprises, see ``http://www.vix.com''.
00041  */
00042 
00043 #define lint
00044 #ifndef lint
00045 static char copyright[] =
00046 "$Id: hash.c,v 1.9.2.3 1999/04/09 17:39:41 mellon Exp $ Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium.  All rights reserved.\n";
00047 #endif /* not lint */
00048 
00049 #include "rosdhcp.h"
00050 
00051 static __inline int do_hash PROTO ((unsigned char *, int, int));
00052 
00053 struct hash_table *new_hash ()
00054 {
00055     struct hash_table *rv = new_hash_table (DEFAULT_HASH_SIZE);
00056     if (!rv)
00057         return rv;
00058     memset (&rv -> buckets [0], 0,
00059         DEFAULT_HASH_SIZE * sizeof (struct hash_bucket *));
00060     return rv;
00061 }
00062 
00063 static __inline int do_hash (name, len, size)
00064     unsigned char *name;
00065     int len;
00066     int size;
00067 {
00068     register int accum = 0;
00069     register unsigned char *s = name;
00070     int i = len;
00071     while (i--) {
00072         /* Add the character in... */
00073         accum += *s++;
00074         /* Add carry back in... */
00075         while (accum > 255) {
00076             accum = (accum & 255) + (accum >> 8);
00077         }
00078     }
00079     return accum % size;
00080 }
00081 
00082 void add_hash (table, name, len, pointer)
00083     struct hash_table *table;
00084     int len;
00085     unsigned char *name;
00086     unsigned char *pointer;
00087 {
00088     int hashno;
00089     struct hash_bucket *bp;
00090 
00091     if (!table)
00092         return;
00093     if (!len)
00094         len = strlen ((char *)name);
00095 
00096     hashno = do_hash (name, len, table -> hash_count);
00097     bp = new_hash_bucket ();
00098 
00099     if (!bp) {
00100         warn ("Can't add %s to hash table.", name);
00101         return;
00102     }
00103     bp -> name = name;
00104     bp -> value = pointer;
00105     bp -> next = table -> buckets [hashno];
00106     bp -> len = len;
00107     table -> buckets [hashno] = bp;
00108 }
00109 
00110 void delete_hash_entry (table, name, len)
00111     struct hash_table *table;
00112     int len;
00113     unsigned char *name;
00114 {
00115     int hashno;
00116     struct hash_bucket *bp, *pbp = (struct hash_bucket *)0;
00117 
00118     if (!table)
00119         return;
00120     if (!len)
00121         len = strlen ((char *)name);
00122 
00123     hashno = do_hash (name, len, table -> hash_count);
00124 
00125     /* Go through the list looking for an entry that matches;
00126        if we find it, delete it. */
00127     for (bp = table -> buckets [hashno]; bp; bp = bp -> next) {
00128         if ((!bp -> len &&
00129              !strcmp ((char *)bp -> name, (char *)name)) ||
00130             (bp -> len == len &&
00131              !memcmp (bp -> name, name, len))) {
00132             if (pbp) {
00133                 pbp -> next = bp -> next;
00134             } else {
00135                 table -> buckets [hashno] = bp -> next;
00136             }
00137             free_hash_bucket (bp, "delete_hash_entry");
00138             break;
00139         }
00140         pbp = bp;   /* jwg, 9/6/96 - nice catch! */
00141     }
00142 }
00143 
00144 unsigned char *hash_lookup (table, name, len)
00145     struct hash_table *table;
00146     unsigned char *name;
00147     int len;
00148 {
00149     int hashno;
00150     struct hash_bucket *bp;
00151 
00152     if (!table)
00153         return (unsigned char *)0;
00154 
00155     if (!len)
00156         len = strlen ((char *)name);
00157 
00158     hashno = do_hash (name, len, table -> hash_count);
00159 
00160     for (bp = table -> buckets [hashno]; bp; bp = bp -> next) {
00161         if (len == bp -> len && !memcmp (bp -> name, name, len))
00162             return bp -> value;
00163     }
00164     return (unsigned char *)0;
00165 }

Generated on Sun May 27 2012 04:20:18 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.