XRootD
Loading...
Searching...
No Matches
XrdOucHashVal.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d O u c H a s h V a l . c c */
4/* */
5/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* Produced by Andrew Hanushevsky for Stanford University under contract */
7/* DE-AC02-76-SFO0515 with the Department of Energy */
8/* */
9/* This file is part of the XRootD software suite. */
10/* */
11/* XRootD is free software: you can redistribute it and/or modify it under */
12/* the terms of the GNU Lesser General Public License as published by the */
13/* Free Software Foundation, either version 3 of the License, or (at your */
14/* option) any later version. */
15/* */
16/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19/* License for more details. */
20/* */
21/* You should have received a copy of the GNU Lesser General Public License */
22/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24/* */
25/* The copyright holder's institutional names and contributor's names may not */
26/* be used to endorse or promote products derived from this software without */
27/* specific prior written permission of the institution or contributor. */
28/******************************************************************************/
29
30#include "string.h"
31#ifndef WIN32
32#include "strings.h"
33#endif
34
35namespace
36{
37// Load n bytes of the key as a little-endian word, so that the hash does not
38// depend on the byte order of the host.
39//
40unsigned long LoadWord(const char *cp, size_t n = sizeof(unsigned long))
41{
42 unsigned long w = 0;
43 memcpy(&w, cp, n);
44#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
45 if (sizeof(w) == 8) w = __builtin_bswap64(w);
46 else w = __builtin_bswap32(w);
47#endif
48 return w;
49}
50}
51
52unsigned long XrdOucHashVal(const char *KeyVal)
53 {extern unsigned long XrdOucHashVal2(const char *, int);
54 return XrdOucHashVal2(KeyVal, strlen(KeyVal));
55 }
56
57unsigned long XrdOucHashVal2(const char *KeyVal, int KeyLen)
58{ int j;
59 const char *cp;
60 unsigned long hval = 0;
61 int hl = sizeof(hval);
62
63// If name is shorter than the hash length, use the name.
64//
65 if (KeyLen <= hl) return LoadWord(KeyVal, (size_t)KeyLen);
66
67// Compute the length of the name and develop starting hash.
68//
69 hval = KeyLen;
70 j = KeyLen % hl; KeyLen /= hl;
71 if (j) hval ^= LoadWord(KeyVal);
72 cp = &KeyVal[j];
73
74// Compute and return the full hash. The key is walked with a byte pointer
75// since it is not aligned for unsigned long; LoadWord() does the unaligned
76// load, and the word arithmetic happens on the copy.
77//
78 while(KeyLen--)
79 {hval ^= LoadWord(cp);
80 cp += hl;
81 }
82 return (hval ? hval : 1);
83}
unsigned long XrdOucHashVal2(const char *KeyVal, int KeyLen)
unsigned long XrdOucHashVal(const char *KeyVal)