mshtml: Added IHTMLStyleSheet::get_rules implementation.
[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     ULONG i;
67
68     TRACE("(%p)\n", pXlatTables);
69
70     /* free the entries in the table */
71     for (i = 0; i < pXlatTables->RefIdToPointer.NumberOfEntries; i++)
72         HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.XlatTable[i]);
73
74     HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.XlatTable);
75     HeapFree(GetProcessHeap(), 0, pXlatTables->RefIdToPointer.StateTable);
76     HeapFree(GetProcessHeap(), 0, pXlatTables->PointerToRefId.XlatTable);
77
78     HeapFree(GetProcessHeap(), 0, pXlatTables);
79 }
80
81 static void expand_pointer_table_if_necessary(PFULL_PTR_XLAT_TABLES pXlatTables, ULONG RefId)
82 {
83     if (RefId >= pXlatTables->RefIdToPointer.NumberOfEntries)
84     {
85         pXlatTables->RefIdToPointer.NumberOfEntries = RefId * 2;
86         pXlatTables->RefIdToPointer.XlatTable =
87             HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
88                 pXlatTables->RefIdToPointer.XlatTable,
89                 sizeof(void *) * pXlatTables->RefIdToPointer.NumberOfEntries);
90         pXlatTables->RefIdToPointer.StateTable =
91             HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
92                 pXlatTables->RefIdToPointer.StateTable,
93                 sizeof(unsigned char) * pXlatTables->RefIdToPointer.NumberOfEntries);
94
95         if (!pXlatTables->RefIdToPointer.XlatTable || !pXlatTables->RefIdToPointer.StateTable)
96             pXlatTables->RefIdToPointer.NumberOfEntries = 0;
97     }
98 }
99
100 int WINAPI NdrFullPointerQueryPointer(PFULL_PTR_XLAT_TABLES pXlatTables,
101                                       void *pPointer, unsigned char QueryType,
102                                       ULONG *pRefId )
103 {
104     ULONG Hash = 0;
105     int i;
106     PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
107
108     TRACE("(%p, %p, %d, %p)\n", pXlatTables, pPointer, QueryType, pRefId);
109
110     if (!pPointer)
111     {
112         *pRefId = 0;
113         return 1;
114     }
115
116     /* simple hashing algorithm, don't know whether it matches native */
117     for (i = 0; i < sizeof(pPointer); i++)
118         Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
119
120     XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
121     for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
122         if (pPointer == XlatTableEntry->Pointer)
123         {
124             *pRefId = XlatTableEntry->RefId;
125             if (XlatTableEntry->State & QueryType)
126                 return 1;
127             XlatTableEntry->State |= QueryType;
128             return 0;
129         }
130
131     XlatTableEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry));
132     XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
133     XlatTableEntry->Pointer = pPointer;
134     XlatTableEntry->RefId = *pRefId = pXlatTables->NextRefId++;
135     XlatTableEntry->State = QueryType;
136     pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
137
138     /* insert pointer into mapping table */
139     expand_pointer_table_if_necessary(pXlatTables, XlatTableEntry->RefId);
140     if (pXlatTables->RefIdToPointer.NumberOfEntries > XlatTableEntry->RefId)
141     {
142         pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
143         pXlatTables->RefIdToPointer.StateTable[XlatTableEntry->RefId] = QueryType;
144     }
145
146     return 0;
147 }
148
149 int WINAPI NdrFullPointerQueryRefId(PFULL_PTR_XLAT_TABLES pXlatTables,
150                                     ULONG RefId, unsigned char QueryType,
151                                     void **ppPointer)
152 {
153     TRACE("(%p, 0x%x, %d, %p)\n", pXlatTables, RefId, QueryType, ppPointer);
154
155     expand_pointer_table_if_necessary(pXlatTables, RefId);
156
157     pXlatTables->NextRefId = max(RefId + 1, pXlatTables->NextRefId);
158
159     if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
160     {
161         *ppPointer = pXlatTables->RefIdToPointer.XlatTable[RefId];
162         if (QueryType)
163         {
164             if (pXlatTables->RefIdToPointer.StateTable[RefId] & QueryType)
165                 return 1;
166             pXlatTables->RefIdToPointer.StateTable[RefId] |= QueryType;
167             return 0;
168         }
169         else
170             return 0;
171     }
172     *ppPointer = NULL;
173     return 0;
174 }
175
176 void WINAPI NdrFullPointerInsertRefId(PFULL_PTR_XLAT_TABLES pXlatTables,
177                                       ULONG RefId, void *pPointer)
178 {
179     ULONG Hash = 0;
180     int i;
181     PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
182
183     TRACE("(%p, 0x%x, %p)\n", pXlatTables, RefId, pPointer);
184
185     /* simple hashing algorithm, don't know whether it matches native */
186     for (i = 0; i < sizeof(pPointer); i++)
187         Hash = (Hash * 3) ^ ((unsigned char *)&pPointer)[i];
188
189     XlatTableEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(*XlatTableEntry));
190     XlatTableEntry->Next = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
191     XlatTableEntry->Pointer = pPointer;
192     XlatTableEntry->RefId = RefId;
193     XlatTableEntry->State = 0;
194     pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask] = XlatTableEntry;
195
196     /* insert pointer into mapping table */
197     expand_pointer_table_if_necessary(pXlatTables, RefId);
198     if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
199         pXlatTables->RefIdToPointer.XlatTable[XlatTableEntry->RefId] = pPointer;
200 }
201
202 int WINAPI NdrFullPointerFree(PFULL_PTR_XLAT_TABLES pXlatTables, void *Pointer)
203 {
204     ULONG Hash = 0;
205     int i;
206     PFULL_PTR_TO_REFID_ELEMENT XlatTableEntry;
207     ULONG RefId = 0;
208
209     TRACE("(%p, %p)\n", pXlatTables, Pointer);
210
211     if (!Pointer)
212         return 1;
213
214     /* simple hashing algorithm, don't know whether it matches native */
215     for (i = 0; i < sizeof(Pointer); i++)
216         Hash = (Hash * 3) ^ ((unsigned char *)&Pointer)[i];
217
218     XlatTableEntry = pXlatTables->PointerToRefId.XlatTable[Hash & pXlatTables->PointerToRefId.HashMask];
219     for (; XlatTableEntry; XlatTableEntry = XlatTableEntry->Next)
220         if (Pointer == XlatTableEntry->Pointer)
221         {
222             if (XlatTableEntry->State & 0x20)
223                 return 0;
224             XlatTableEntry->State |= 0x20;
225             RefId = XlatTableEntry->RefId;
226             break;
227         }
228
229     if (!XlatTableEntry)
230         return 0;
231
232     if (pXlatTables->RefIdToPointer.NumberOfEntries > RefId)
233     {
234         pXlatTables->RefIdToPointer.StateTable[RefId] |= 0x20;
235         return 1;
236     }
237
238     return 0;
239 }