1 | /* $Id: hash.cpp,v 1.2 2001-08-10 19:36:04 sandervl Exp $ */
|
---|
2 | /*
|
---|
3 | * LHash functions.
|
---|
4 | *
|
---|
5 | * 2000/01/03
|
---|
6 | *
|
---|
7 | * Copyright 2000 David J. Raison
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | * Based on Knuth - Sorting & Searching 2nd Ed. 1997 Section 6.4
|
---|
12 | */
|
---|
13 |
|
---|
14 | #include "oleaut32.h"
|
---|
15 | #include "olectl.h"
|
---|
16 | #include "oList.h" // linked list template
|
---|
17 | //#include "itypelib.h"
|
---|
18 |
|
---|
19 | // ======================================================================
|
---|
20 | // Local Data
|
---|
21 | // ======================================================================
|
---|
22 | class HashElem
|
---|
23 | {
|
---|
24 | public:
|
---|
25 | HashElem(LCID lcid, const WCHAR * szName) : m_lcid(lcid)
|
---|
26 | {
|
---|
27 | m_szName = new WCHAR[lstrlenW(szName) + 1];
|
---|
28 | lstrcpyW(m_szName, szName);
|
---|
29 | }
|
---|
30 | virtual ~HashElem()
|
---|
31 | { delete[] m_szName; }
|
---|
32 |
|
---|
33 | LCID lcid() const
|
---|
34 | { return m_lcid; }
|
---|
35 | WCHAR * szName() const
|
---|
36 | { return m_szName; }
|
---|
37 |
|
---|
38 | int compare (HashElem & rhs)
|
---|
39 | {
|
---|
40 | if (rhs.lcid() > m_lcid)
|
---|
41 | return 1;
|
---|
42 | else if (rhs.lcid() < m_lcid)
|
---|
43 | return -1;
|
---|
44 |
|
---|
45 | return lstrcmpW(rhs.szName(), m_szName);
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected:
|
---|
49 |
|
---|
50 | private:
|
---|
51 | LCID m_lcid;
|
---|
52 | WCHAR * m_szName;
|
---|
53 |
|
---|
54 | };
|
---|
55 |
|
---|
56 | class Hash
|
---|
57 | {
|
---|
58 | public:
|
---|
59 | Hash(ULONG size = 1009);
|
---|
60 | virtual ~Hash();
|
---|
61 |
|
---|
62 | ULONG Compute(LCID lcid, const OLECHAR * szName);
|
---|
63 |
|
---|
64 | protected:
|
---|
65 |
|
---|
66 | private:
|
---|
67 | USHORT GetBaseVal(HashElem * pElem);
|
---|
68 | USHORT Lookup(HashElem * pElem);
|
---|
69 |
|
---|
70 | ULONG m_lSize;
|
---|
71 | oList<HashElem *> * m_pTable;
|
---|
72 |
|
---|
73 | };
|
---|
74 |
|
---|
75 | static Hash * m_pHash;
|
---|
76 |
|
---|
77 | // ======================================================================
|
---|
78 | // Local functions.
|
---|
79 | // ======================================================================
|
---|
80 |
|
---|
81 | // ----------------------------------------------------------------------
|
---|
82 | // Hash::Hash
|
---|
83 | //
|
---|
84 | // Constructor
|
---|
85 | // ----------------------------------------------------------------------
|
---|
86 | Hash::Hash(ULONG size)
|
---|
87 | : m_lSize(size)
|
---|
88 | {
|
---|
89 | m_pTable = new oList<HashElem *>[m_lSize];
|
---|
90 | }
|
---|
91 |
|
---|
92 | // ----------------------------------------------------------------------
|
---|
93 | // Hash::~Hash
|
---|
94 | //
|
---|
95 | // Destructor
|
---|
96 | // ----------------------------------------------------------------------
|
---|
97 | Hash::~Hash()
|
---|
98 | {
|
---|
99 | ULONG ii;
|
---|
100 |
|
---|
101 | for (ii = 0; ii < m_lSize; ii++)
|
---|
102 | {
|
---|
103 | oListIter<HashElem *> pIter(m_pTable[ii]);
|
---|
104 |
|
---|
105 | for (pIter.MoveStart(); pIter.IsValid(); pIter.MoveNext())
|
---|
106 | delete pIter.Element();
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | delete[] m_pTable;
|
---|
111 | m_pTable = 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | // ----------------------------------------------------------------------
|
---|
115 | // Hash::GetBaseVal
|
---|
116 | //
|
---|
117 | // Return a 16bit hash value for the string.
|
---|
118 | // ----------------------------------------------------------------------
|
---|
119 | USHORT Hash::GetBaseVal(HashElem * pElem)
|
---|
120 | {
|
---|
121 | USHORT usVal = 0;
|
---|
122 | const WCHAR * pName = pElem->szName();
|
---|
123 |
|
---|
124 | while(*pName)
|
---|
125 | {
|
---|
126 | usVal ^= (int)towupper(*pName);
|
---|
127 | usVal <<= 1;
|
---|
128 | pName++;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return usVal % m_lSize;
|
---|
132 | }
|
---|
133 |
|
---|
134 | // ----------------------------------------------------------------------
|
---|
135 | // Hash::Lookup
|
---|
136 | // ----------------------------------------------------------------------
|
---|
137 | USHORT Hash::Lookup(HashElem * pElem)
|
---|
138 | {
|
---|
139 | USHORT usVal = GetBaseVal(pElem);
|
---|
140 | oListIter<HashElem *> pIter(m_pTable[usVal]);
|
---|
141 |
|
---|
142 | for (pIter.MoveStart(); pIter.IsValid(); pIter.MoveNext())
|
---|
143 | if (pElem->compare(*pIter.Element()) == 0)
|
---|
144 | return usVal;
|
---|
145 |
|
---|
146 | m_pTable[usVal].AddAtEnd(pElem);
|
---|
147 | return usVal;
|
---|
148 | }
|
---|
149 |
|
---|
150 | // ----------------------------------------------------------------------
|
---|
151 | // Hash::Compute
|
---|
152 | // ----------------------------------------------------------------------
|
---|
153 | ULONG Hash::Compute(LCID lcid, const OLECHAR * szName)
|
---|
154 | {
|
---|
155 | HashElem * pElem = new HashElem(lcid, szName);
|
---|
156 | return Lookup(pElem) | (lcid << 16);
|
---|
157 | }
|
---|
158 |
|
---|
159 | // ======================================================================
|
---|
160 | // Public API's
|
---|
161 | // ======================================================================
|
---|
162 |
|
---|
163 | // ----------------------------------------------------------------------
|
---|
164 | // Hash_Initialise
|
---|
165 | // ----------------------------------------------------------------------
|
---|
166 | void Hash_Initialise()
|
---|
167 | {
|
---|
168 | m_pHash = new Hash;
|
---|
169 | }
|
---|
170 |
|
---|
171 | // ----------------------------------------------------------------------
|
---|
172 | // Hash_Terminate
|
---|
173 | // ----------------------------------------------------------------------
|
---|
174 | void Hash_Terminate()
|
---|
175 | {
|
---|
176 | delete m_pHash;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // ----------------------------------------------------------------------
|
---|
180 | // LHashValOfNameSys() [OLEAUT32.165]
|
---|
181 | //
|
---|
182 | // Return a hash to identify an OLE typelib element name.
|
---|
183 | // ----------------------------------------------------------------------
|
---|
184 | ULONG WIN32API LHashValOfNameSys(SYSKIND syskind, LCID lcid, const OLECHAR * szName)
|
---|
185 | {
|
---|
186 | ULONG rc;
|
---|
187 |
|
---|
188 | dprintf(("OLEAUT32: LHashValOfNameSys"));
|
---|
189 |
|
---|
190 | rc = m_pHash->Compute(lcid, szName);
|
---|
191 |
|
---|
192 | return rc;
|
---|
193 | }
|
---|
194 |
|
---|
195 | // ----------------------------------------------------------------------
|
---|
196 | // LHashValOfNameSysA() [OLEAUT32.166]
|
---|
197 | //
|
---|
198 | // Return a hash to identify an OLE typelib element name.
|
---|
199 | // ----------------------------------------------------------------------
|
---|
200 | ULONG WIN32API LHashValOfNameSysA (SYSKIND syskind, LCID lcid, LPCSTR szName)
|
---|
201 | {
|
---|
202 | LPWSTR szWName;
|
---|
203 | HANDLE hHeap = GetProcessHeap();
|
---|
204 | ULONG rc;
|
---|
205 |
|
---|
206 | szWName = (LPWSTR)HeapAlloc(hHeap, 0, (strlen(szName) + 1) * 2);
|
---|
207 | AsciiToUnicode(szName, szWName);
|
---|
208 | rc = LHashValOfNameSys(syskind, lcid, szWName);
|
---|
209 | HeapFree(hHeap, 0, szWName);
|
---|
210 |
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 |
|
---|