riched20: Fix test crash on WinXP-SP2.
[wine] / dlls / rpcrt4 / ndr_fullpointer.c
1 /*
2  * Full Pointer Translation Routines
3  *
4  * Copyright 2006 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "rpc.h"
26 #include "rpcndr.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(rpc);
31
32 PFULL_PTR_XLAT_TABLES WINAPI NdrFullPointerXlatInit(ULONG NumberOfPointers,
33                                                     XLAT_SIDE XlatSide)
34 {
35     ULONG NumberOfBuckets;
36     PFULL_PTR_XLAT_TABLES pXlatTables = HeapAlloc(GetProcessHeap(), 0, sizeof(*pXlatTables));
37
38     TRACE("(%d, %d)\n", NumberOfPointers, XlatSide);
39
40     if (!NumberOfPointers) NumberOfPointers = 512;
41     NumberOfBuckets = ((NumberOfPointers + 3) & ~3) - 1;
42
43     pXlatTables->RefIdToPointer.XlatTable =
44         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
45             sizeof(void *) * NumberOfPointers);
46     pXlatTables->RefIdToPointer.StateTable =
47         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
48             sizeof(unsigned char) * NumberOfPointers);
49     pXlatTables->RefIdToPointer.NumberOfEntries = NumberOfPointers;
50
51     TRACE("NumberOfBuckets = %d\n", NumberOfBuckets);
52     pXlatTables->PointerToRefId.XlatTable =
53         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
54             sizeof(PFULL_PTR_TO_REFID_ELEMENT) * NumberOfBuckets);
55     pXlatTables->PointerToRefId.NumberOfBuckets = NumberOfBuckets;
56     pXlatTables->PointerToRefId.HashMask = NumberOfBuckets - 1;
57
58     pXlatTables->NextRefId = 1;
59     pXlatTables->XlatSide = XlatSide;
60
61     return pXlatTables;
62 }
63
64 void WINAPI NdrFullPointerXlatFree(PFULL_PTR_XLAT_TABLES pXlatTables)
65 {
66     TRACE("(%p)\n", pXlatTables);
67
68     HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.XlatTable);
69     HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.StateTable);
70     HeapFree(GetProcessHeap(), 0, pXlatTables->PointerToRefId.XlatTable);
71
72     HeapFree(GetProcessHeap(), 0, pXlatTables);
73 }
74
75 static void expand_pointer_table_if_necessary(PFULL_PTR_XLAT_TABLES pXlatTables, ULONG RefId)
76 {
77     if (RefId >= pXlatTables->RefIdToPointer.NumberOfEntries)
78     {
79         pXlatTables->RefIdToPointer.NumberOfEntries = RefId * 2;
80         pXlatTables->RefIdToPointer.XlatTable =
81             HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
82                 pXlatTables->RefIdToPointer.XlatTable,
83                 sizeof(void *) * pXlatTables->RefIdToPointer.NumberOfEntries);
84         pXlatTables->RefIdToPointer.StateTable =
85             HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
86                 pXlatTables->RefIdToPointer.StateTable,
87                 sizeof(unsigned char) * pXlatTables->RefIdToPointer.NumberOfEntries);
88
89         if (!pXlatTables->RefIdToPointer.XlatTable || !pXlatTables->RefIdToPointer.StateTable)
90             pXlatTables->RefIdToPointer.NumberOfEntries = 0;
91     }
92 }
93
94 int WINAPI NdrFullPointerQueryPointer(PFULL_PTR_XLAT_TABLES pXlatTables,
95                                       void *pPointer, unsigned char QueryType,
96                                       ULONG *pRefId )
97 {
98     ULONG Hash = 0;
99     int i;
100     PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
101
102     TRACE("(%p, %p, %d, %p)\n", pXlatTables, pPointer, QueryType, pRefId);
103
104     if (!pPointer)
105     {
106         *pRefId = 0;
107         return 1;
108     }
109
110     /* simple hashing algorithm, don't know whether it matches native */
111     for (i = 0; i < sizeof(pPointer); i++)
112         Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
113
114     XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
115     for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
116         if (pPointer == XlatTableEntry->Pointer)
117         {
118             *pRefId = XlatTableEntry->RefId;
119             if (XlatTableEntry->State & QueryType)
120                 return 1;
121             XlatTableEntry->State |= QueryType;
122             return 0;
123         }
124
125     XlatTableEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry));
126     XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
127     XlatTableEntry->Pointer = pPointer;
128     XlatTableEntry->RefId = *pRefId = pXlatTables->NextRefId++;
129     XlatTableEntry->State = QueryType;
130     pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
131
132     /* insert pointer into mapping table */
133     expand_pointer_table_if_necessary(pXlatTables, XlatTableEntry->RefId);
134     if (pXlatTables->RefIdToPointer.NumberOfEntries > XlatTableEntry->RefId)
135     {
136         pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
137         pXlatTables->RefIdToPointer.StateTable[XlatTableEntry->RefId] = QueryType;
138     }
139
140     return 0;
141 }
142
143 int WINAPI NdrFullPointerQueryRefId(PFULL_PTR_XLAT_TABLES pXlatTables,
144                                     ULONG RefId, unsigned char QueryType,
145                                     void **ppPointer)
146 {
147     TRACE("(%p, 0x%x, %d, %p)\n", pXlatTables, RefId, QueryType, ppPointer);
148
149     expand_pointer_table_if_necessary(pXlatTables, RefId);
150
151     pXlatTables->NextRefId = max(RefId + 1, pXlatTables->NextRefId);
152
153     if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
154     {
155         *ppPointer = pXlatTables->RefIdToPointer.XlatTable[RefId];
156         if (QueryType)
157         {
158             if (pXlatTables->RefIdToPointer.StateTable[RefId] & QueryType)
159                 return 1;
160             pXlatTables->RefIdToPointer.StateTable[RefId] |= QueryType;
161             return 0;
162         }
163         else
164             return 0;
165     }
166     *ppPointer = NULL;
167     return 0;
168 }
169
170 void WINAPI NdrFullPointerInsertRefId(PFULL_PTR_XLAT_TABLES pXlatTables,
171                                       ULONG RefId, void *pPointer)
172 {
173     ULONG Hash = 0;
174     int i;
175     PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
176
177     TRACE("(%p, 0x%x, %p)\n", pXlatTables, RefId, pPointer);
178
179     /* simple hashing algorithm, don't know whether it matches native */
180     for (i = 0; i < sizeof(pPointer); i++)
181         Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
182
183     XlatTableEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry));
184     XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
185     XlatTableEntry->Pointer = pPointer;
186     XlatTableEntry->RefId = RefId;
187     XlatTableEntry->State = 0;
188     pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
189
190     /* insert pointer into mapping table */
191     expand_pointer_table_if_necessary(pXlatTables, RefId);
192     if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
193         pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
194 }
195
196 int WINAPI NdrFullPointerFree(PFULL_PTR_XLAT_TABLES pXlatTables, void *Pointer)
197 {
198     ULONG Hash = 0;
199     int i;
200     PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
201     ULONG RefId = 0;
202
203     TRACE("(%p, %p)\n", pXlatTables, Pointer);
204
205     if (!Pointer)
206         return 1;
207
208     /* simple hashing algorithm, don't know whether it matches native */
209     for (i = 0; i < sizeof(Pointer); i++)
210         Hash = (Hash * 3) ^ ((unsigned char *)&Pointer)[i];
211
212     XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
213     for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
214         if (Pointer == XlatTableEntry->Pointer)
215         {
216             if (XlatTableEntry->State & 0x20)
217                 return 0;
218             XlatTableEntry->State |= 0x20;
219             RefId = XlatTableEntry->RefId;
220             break;
221         }
222
223     if (!XlatTableEntry)
224         return 0;
225
226     if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
227     {
228         pXlatTables->RefIdToPointer.StateTable[RefId] |= 0x20;
229         return 1;
230     }
231
232     return 0;
233 }