Back to index

glibc  2.9
dl-hash.h
Go to the documentation of this file.
00001 /* Compute hash value for given string according to ELF standard.
00002    Copyright (C) 1995,1996,1997,1998,2003,2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _DL_HASH_H
00021 #define _DL_HASH_H   1
00022 
00023 
00024 /* This is the hashing function specified by the ELF ABI.  In the
00025    first five operations no overflow is possible so we optimized it a
00026    bit.  */
00027 static unsigned int
00028 _dl_elf_hash (const char *name_arg)
00029 {
00030   const unsigned char *name = (const unsigned char *) name_arg;
00031   unsigned long int hash = 0;
00032   if (*name != '\0')
00033     {
00034       hash = *name++;
00035       if (*name != '\0')
00036        {
00037          hash = (hash << 4) + *name++;
00038          if (*name != '\0')
00039            {
00040              hash = (hash << 4) + *name++;
00041              if (*name != '\0')
00042               {
00043                 hash = (hash << 4) + *name++;
00044                 if (*name != '\0')
00045                   {
00046                     hash = (hash << 4) + *name++;
00047                     while (*name != '\0')
00048                      {
00049                        unsigned long int hi;
00050                        hash = (hash << 4) + *name++;
00051                        hi = hash & 0xf0000000;
00052 
00053                        /* The algorithm specified in the ELF ABI is as
00054                           follows:
00055 
00056                           if (hi != 0)
00057                             hash ^= hi >> 24;
00058 
00059                           hash &= ~hi;
00060 
00061                           But the following is equivalent and a lot
00062                           faster, especially on modern processors.  */
00063 
00064                        hash ^= hi;
00065                        hash ^= hi >> 24;
00066                      }
00067                   }
00068               }
00069            }
00070        }
00071     }
00072   return hash;
00073 }
00074 
00075 #endif /* dl-hash.h */