e2771e43ee326e6ef4843b994c24c4158fa9ce83
[reactos.git] / reactos / subsystems / win32 / win32k / objects / gdiobj.c
1 /*
2 * PROJECT: ReactOS win32 kernel mode subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/objects/gdiobj.c
5 * PURPOSE: General GDI object manipulation routines
6 * PROGRAMMERS: ...
7 */
8
9 /** INCLUDES ******************************************************************/
10
11 //#define GDI_DEBUG
12
13 #include <win32k.h>
14 #define NDEBUG
15 #include <debug.h>
16
17 #define GDI_ENTRY_TO_INDEX(ht, e) \
18 (((ULONG_PTR)(e) - (ULONG_PTR)&((ht)->Entries[0])) / sizeof(GDI_TABLE_ENTRY))
19 #define GDI_HANDLE_GET_ENTRY(HandleTable, h) \
20 (&(HandleTable)->Entries[GDI_HANDLE_GET_INDEX((h))])
21
22 /* apparently the first 10 entries are never used in windows as they are empty */
23 #define RESERVE_ENTRIES_COUNT 10
24
25 #define BASE_OBJTYPE_COUNT 32
26
27 #define DelayExecution() \
28 DPRINT("%s:%i: Delay\n", __FILE__, __LINE__); \
29 KeDelayExecutionThread(KernelMode, FALSE, &ShortDelay)
30
31 #include "gdidbg.c"
32
33 static
34 BOOL INTERNAL_CALL GDI_CleanupDummy(PVOID ObjectBody);
35
36 /** GLOBALS *******************************************************************/
37
38 typedef struct
39 {
40 BOOL bUseLookaside;
41 ULONG_PTR ulBodySize;
42 ULONG Tag;
43 GDICLEANUPPROC CleanupProc;
44 } OBJ_TYPE_INFO, *POBJ_TYPE_INFO;
45
46 static const
47 OBJ_TYPE_INFO ObjTypeInfo[BASE_OBJTYPE_COUNT] =
48 {
49 {0, 0, 0, NULL}, /* 00 reserved entry */
50 {1, sizeof(DC), TAG_DC, DC_Cleanup}, /* 01 DC */
51 {1, 0, 0, NULL}, /* 02 UNUSED1 */
52 {1, 0, 0, NULL}, /* 03 UNUSED2 */
53 {1, sizeof(ROSRGNDATA), TAG_REGION, REGION_Cleanup}, /* 04 RGN */
54 {1, sizeof(SURFACE), TAG_SURFACE, SURFACE_Cleanup}, /* 05 SURFACE */
55 {1, sizeof(CLIENTOBJ), TAG_CLIENTOBJ, GDI_CleanupDummy}, /* 06 CLIENTOBJ: METADC,... */
56 {1, sizeof(PATH), TAG_PATH, GDI_CleanupDummy}, /* 07 PATH */
57 {1, sizeof(PALETTE), TAG_PALETTE, PALETTE_Cleanup}, /* 08 PAL */
58 {1, sizeof(COLORSPACE), TAG_ICMLCS, GDI_CleanupDummy}, /* 09 ICMLCS, */
59 {1, sizeof(TEXTOBJ), TAG_LFONT, GDI_CleanupDummy}, /* 0a LFONT */
60 {0, 0, TAG_RFONT, NULL}, /* 0b RFONT, unused */
61 {0, 0, TAG_PFE, NULL}, /* 0c PFE, unused */
62 {0, 0, TAG_PFT, NULL}, /* 0d PFT, unused */
63 {0, sizeof(GDICLRXFORM), TAG_ICMCXF, GDI_CleanupDummy}, /* 0e ICMCXF, */
64 {0, 0, TAG_SPRITE, NULL}, /* 0f SPRITE, unused */
65 {1, sizeof(BRUSH), TAG_BRUSH, BRUSH_Cleanup}, /* 10 BRUSH, PEN, EXTPEN */
66 {0, 0, TAG_UMPD, NULL}, /* 11 UMPD, unused */
67 {0, 0, 0, NULL}, /* 12 UNUSED4 */
68 {0, 0, TAG_SPACE, NULL}, /* 13 SPACE, unused */
69 {0, 0, 0, NULL}, /* 14 UNUSED5 */
70 {0, 0, TAG_META, NULL}, /* 15 META, unused */
71 {0, 0, TAG_EFSTATE, NULL}, /* 16 EFSTATE, unused */
72 {0, 0, TAG_BMFD, NULL}, /* 17 BMFD, unused */
73 {0, 0, TAG_VTFD, NULL}, /* 18 VTFD, unused */
74 {0, 0, TAG_TTFD, NULL}, /* 19 TTFD, unused */
75 {0, 0, TAG_RC, NULL}, /* 1a RC, unused */
76 {0, 0, TAG_TEMP, NULL}, /* 1b TEMP, unused */
77 {0, sizeof(EDRIVEROBJ), TAG_DRVOBJ, DRIVEROBJ_Cleanup},/* 1c DRVOBJ */
78 {0, 0, TAG_DCIOBJ, NULL}, /* 1d DCIOBJ, unused */
79 {0, 0, TAG_SPOOL, NULL}, /* 1e SPOOL, unused */
80 {0, 0, 0, NULL}, /* 1f reserved entry */
81 };
82
83 static LARGE_INTEGER ShortDelay;
84
85 /** INTERNAL FUNCTIONS ********************************************************/
86
87 // Audit Functions
88 int tDC = 0;
89 int tBRUSH = 0;
90 int tBITMAP = 0;
91 int tFONT = 0;
92 int tRGN = 0;
93
94 VOID
95 AllocTypeDataDump(INT TypeInfo)
96 {
97 switch( TypeInfo & GDI_HANDLE_TYPE_MASK )
98 {
99 case GDILoObjType_LO_BRUSH_TYPE:
100 tBRUSH++;
101 break;
102 case GDILoObjType_LO_DC_TYPE:
103 tDC++;
104 break;
105 case GDILoObjType_LO_BITMAP_TYPE:
106 tBITMAP++;
107 break;
108 case GDILoObjType_LO_FONT_TYPE:
109 tFONT++;
110 break;
111 case GDILoObjType_LO_REGION_TYPE:
112 tRGN++;
113 break;
114 }
115 }
116
117 VOID
118 DeAllocTypeDataDump(INT TypeInfo)
119 {
120 switch( TypeInfo & GDI_HANDLE_TYPE_MASK )
121 {
122 case GDILoObjType_LO_BRUSH_TYPE:
123 tBRUSH--;
124 break;
125 case GDILoObjType_LO_DC_TYPE:
126 tDC--;
127 break;
128 case GDILoObjType_LO_BITMAP_TYPE:
129 tBITMAP--;
130 break;
131 case GDILoObjType_LO_FONT_TYPE:
132 tFONT--;
133 break;
134 case GDILoObjType_LO_REGION_TYPE:
135 tRGN--;
136 break;
137 }
138 }
139
140 /*
141 * Dummy GDI Cleanup Callback
142 */
143 static
144 BOOL INTERNAL_CALL
145 GDI_CleanupDummy(PVOID ObjectBody)
146 {
147 return TRUE;
148 }
149
150 /*!
151 * Allocate GDI object table.
152 * \param Size - number of entries in the object table.
153 */
154 PGDI_HANDLE_TABLE INTERNAL_CALL
155 GDIOBJ_iAllocHandleTable(OUT PSECTION_OBJECT *SectionObject)
156 {
157 PGDI_HANDLE_TABLE HandleTable = NULL;
158 LARGE_INTEGER htSize;
159 UINT ObjType;
160 ULONG ViewSize = 0;
161 NTSTATUS Status;
162
163 ASSERT(SectionObject != NULL);
164
165 htSize.QuadPart = sizeof(GDI_HANDLE_TABLE);
166
167 Status = MmCreateSection((PVOID*)SectionObject,
168 SECTION_ALL_ACCESS,
169 NULL,
170 &htSize,
171 PAGE_READWRITE,
172 SEC_COMMIT,
173 NULL,
174 NULL);
175 if (!NT_SUCCESS(Status))
176 return NULL;
177
178 /* FIXME - use MmMapViewInSessionSpace once available! */
179 Status = MmMapViewInSystemSpace(*SectionObject,
180 (PVOID*)&HandleTable,
181 &ViewSize);
182 if (!NT_SUCCESS(Status))
183 {
184 ObDereferenceObject(*SectionObject);
185 *SectionObject = NULL;
186 return NULL;
187 }
188
189 RtlZeroMemory(HandleTable, sizeof(GDI_HANDLE_TABLE));
190
191 HandleTable->LookasideLists = ExAllocatePoolWithTag(NonPagedPool,
192 BASE_OBJTYPE_COUNT * sizeof(PAGED_LOOKASIDE_LIST),
193 TAG_GDIHNDTBLE);
194 if (HandleTable->LookasideLists == NULL)
195 {
196 MmUnmapViewInSystemSpace(HandleTable);
197 ObDereferenceObject(*SectionObject);
198 *SectionObject = NULL;
199 return NULL;
200 }
201
202 for (ObjType = 0; ObjType < BASE_OBJTYPE_COUNT; ObjType++)
203 {
204 if (ObjTypeInfo[ObjType].bUseLookaside)
205 {
206 ExInitializePagedLookasideList(HandleTable->LookasideLists + ObjType,
207 NULL,
208 NULL,
209 0,
210 ObjTypeInfo[ObjType].ulBodySize,
211 ObjTypeInfo[ObjType].Tag,
212 0);
213 }
214 }
215
216 ShortDelay.QuadPart = -5000LL; /* FIXME - 0.5 ms? */
217
218 HandleTable->FirstFree = 0;
219 HandleTable->FirstUnused = RESERVE_ENTRIES_COUNT;
220
221 return HandleTable;
222 }
223
224 static void FASTCALL
225 LockErrorDebugOutput(HGDIOBJ hObj, PGDI_TABLE_ENTRY Entry, LPSTR Function)
226 {
227 if ((Entry->Type & GDI_ENTRY_BASETYPE_MASK) == 0)
228 {
229 DPRINT1("%s: Attempted to lock object 0x%x that is deleted!\n", Function, hObj);
230 GDIDBG_TRACEDELETER(hObj);
231 }
232 else if (GDI_HANDLE_GET_REUSECNT(hObj) != GDI_ENTRY_GET_REUSECNT(Entry->Type))
233 {
234 DPRINT1("%s: Attempted to lock object 0x%x, wrong reuse counter (Handle: 0x%x, Entry: 0x%x)\n",
235 Function, hObj, GDI_HANDLE_GET_REUSECNT(hObj), GDI_ENTRY_GET_REUSECNT(Entry->Type));
236 }
237 else if (GDI_HANDLE_GET_TYPE(hObj) != ((Entry->Type << GDI_ENTRY_UPPER_SHIFT) & GDI_HANDLE_TYPE_MASK))
238 {
239 DPRINT1("%s: Attempted to lock object 0x%x, type mismatch (Handle: 0x%x, Entry: 0x%x)\n",
240 Function, hObj, GDI_HANDLE_GET_TYPE(hObj), (Entry->Type << GDI_ENTRY_UPPER_SHIFT) & GDI_HANDLE_TYPE_MASK);
241 }
242 else
243 {
244 DPRINT1("%s: Attempted to lock object 0x%x, something went wrong, typeinfo = 0x%x\n",
245 Function, hObj, Entry->Type);
246 }
247 GDIDBG_TRACECALLER();
248 }
249
250 ULONG
251 FASTCALL
252 InterlockedPopFreeEntry(VOID)
253 {
254 ULONG idxFirst, idxNext, idxPrev;
255 PGDI_TABLE_ENTRY pEntry;
256 DWORD PrevProcId;
257
258 DPRINT("Enter InterLockedPopFreeEntry\n");
259
260 while (TRUE)
261 {
262 idxFirst = GdiHandleTable->FirstFree;
263
264 if (!idxFirst)
265 {
266 /* Increment FirstUnused and get the new index */
267 idxFirst = InterlockedIncrement((LONG*)&GdiHandleTable->FirstUnused) - 1;
268
269 /* Check if we have entries left */
270 if (idxFirst >= GDI_HANDLE_COUNT)
271 {
272 DPRINT1("No more gdi handles left!\n");
273 return 0;
274 }
275
276 /* Return the old index */
277 return idxFirst;
278 }
279
280 /* Get a pointer to the first free entry */
281 pEntry = GdiHandleTable->Entries + idxFirst;
282
283 /* Try to lock the entry */
284 PrevProcId = InterlockedCompareExchange((LONG*)&pEntry->ProcessId, 1, 0);
285 if (PrevProcId != 0)
286 {
287 /* The entry was locked or not free, wait and start over */
288 DelayExecution();
289 continue;
290 }
291
292 /* Sanity check: is entry really free? */
293 ASSERT(((ULONG_PTR)pEntry->KernelData & ~GDI_HANDLE_INDEX_MASK) == 0);
294
295 /* Try to exchange the FirstFree value */
296 idxNext = (ULONG_PTR)pEntry->KernelData;
297 idxPrev = InterlockedCompareExchange((LONG*)&GdiHandleTable->FirstFree,
298 idxNext,
299 idxFirst);
300
301 /* Unlock the free entry */
302 (void)InterlockedExchange((LONG*)&pEntry->ProcessId, 0);
303
304 /* If we succeeded, break out of the loop */
305 if (idxPrev == idxFirst)
306 {
307 break;
308 }
309 }
310
311 return idxFirst;
312 }
313
314 /* Pushes an entry of the handle table to the free list,
315 The entry must be unlocked and the base type field must be 0 */
316 VOID
317 FASTCALL
318 InterlockedPushFreeEntry(ULONG idxToFree)
319 {
320 ULONG idxFirstFree, idxPrev;
321 PGDI_TABLE_ENTRY pFreeEntry;
322
323 DPRINT("Enter InterlockedPushFreeEntry\n");
324
325 pFreeEntry = GdiHandleTable->Entries + idxToFree;
326 ASSERT((pFreeEntry->Type & GDI_ENTRY_BASETYPE_MASK) == 0);
327 ASSERT(pFreeEntry->ProcessId == 0);
328 pFreeEntry->UserData = NULL;
329
330 do
331 {
332 idxFirstFree = GdiHandleTable->FirstFree;
333 pFreeEntry->KernelData = (PVOID)(ULONG_PTR)idxFirstFree;
334
335 idxPrev = InterlockedCompareExchange((LONG*)&GdiHandleTable->FirstFree,
336 idxToFree,
337 idxFirstFree);
338 }
339 while (idxPrev != idxFirstFree);
340 }
341
342
343 BOOL
344 INTERNAL_CALL
345 GDIOBJ_ValidateHandle(HGDIOBJ hObj, ULONG ObjectType)
346 {
347 PGDI_TABLE_ENTRY Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, hObj);
348 if ((((ULONG_PTR)hObj & GDI_HANDLE_TYPE_MASK) == ObjectType) &&
349 (Entry->Type << GDI_ENTRY_UPPER_SHIFT) == GDI_HANDLE_GET_UPPER(hObj))
350 {
351 HANDLE pid = (HANDLE)((ULONG_PTR)Entry->ProcessId & ~0x1);
352 if (pid == NULL || pid == PsGetCurrentProcessId())
353 {
354 return TRUE;
355 }
356 }
357 return FALSE;
358 }
359
360 POBJ INTERNAL_CALL
361 GDIOBJ_AllocObj(UCHAR BaseType)
362 {
363 POBJ pObject;
364
365 ASSERT((BaseType & ~GDIObjTypeTotal) == 0);
366 // BaseType &= GDI_HANDLE_BASETYPE_MASK;
367
368 if (ObjTypeInfo[BaseType].bUseLookaside)
369 {
370 PPAGED_LOOKASIDE_LIST LookasideList;
371
372 LookasideList = GdiHandleTable->LookasideLists + BaseType;
373 pObject = ExAllocateFromPagedLookasideList(LookasideList);
374 }
375 else
376 {
377 pObject = ExAllocatePoolWithTag(PagedPool,
378 ObjTypeInfo[BaseType].ulBodySize,
379 ObjTypeInfo[BaseType].Tag);
380 }
381
382 if (pObject)
383 {
384 RtlZeroMemory(pObject, ObjTypeInfo[BaseType].ulBodySize);
385 }
386
387 return pObject;
388 }
389
390
391 /*!
392 * Allocate memory for GDI object and return handle to it.
393 *
394 * \param ObjectType - type of object \ref GDI object types
395 *
396 * \return Pointer to the allocated object, which is locked.
397 */
398 POBJ INTERNAL_CALL
399 GDIOBJ_AllocObjWithHandle(ULONG ObjectType)
400 {
401 PPROCESSINFO W32Process;
402 POBJ newObject = NULL;
403 HANDLE CurrentProcessId, LockedProcessId;
404 UCHAR TypeIndex;
405 UINT Index;
406 PGDI_TABLE_ENTRY Entry;
407 LONG TypeInfo;
408
409 GDIDBG_INITLOOPTRACE();
410
411 W32Process = PsGetCurrentProcessWin32Process();
412 /* HACK HACK HACK: simplest-possible quota implementation - don't allow a process
413 to take too many GDI objects, itself. */
414 if (W32Process && W32Process->GDIHandleCount >= 0x2710)
415 {
416 DPRINT1("Too many objects for process!!!\n");
417 DPRINT1("DC %d BRUSH %d BITMAP %d FONT %d RGN %d\n",tDC,tBRUSH,tBITMAP,tFONT,tRGN);
418 GDIDBG_DUMPHANDLETABLE();
419 return NULL;
420 }
421
422 ASSERT(ObjectType != GDI_OBJECT_TYPE_DONTCARE);
423
424 TypeIndex = GDI_OBJECT_GET_TYPE_INDEX(ObjectType);
425
426 newObject = GDIOBJ_AllocObj(TypeIndex);
427 if (!newObject)
428 {
429 DPRINT1("Not enough memory to allocate gdi object!\n");
430 return NULL;
431 }
432
433 CurrentProcessId = PsGetCurrentProcessId();
434 LockedProcessId = (HANDLE)((ULONG_PTR)CurrentProcessId | 0x1);
435
436 // RtlZeroMemory(newObject, ObjTypeInfo[TypeIndex].ulBodySize);
437
438 /* On Windows the higher 16 bit of the type field don't contain the
439 full type from the handle, but the base type.
440 (type = BRSUH, PEN, EXTPEN, basetype = BRUSH) */
441 TypeInfo = (ObjectType & GDI_HANDLE_BASETYPE_MASK) | (ObjectType >> GDI_ENTRY_UPPER_SHIFT);
442
443 Index = InterlockedPopFreeEntry();
444 if (Index != 0)
445 {
446 HANDLE PrevProcId;
447
448 Entry = &GdiHandleTable->Entries[Index];
449
450 LockHandle:
451 PrevProcId = InterlockedCompareExchangePointer((PVOID*)&Entry->ProcessId, LockedProcessId, 0);
452 if (PrevProcId == NULL)
453 {
454 PTHREADINFO Thread = (PTHREADINFO)PsGetCurrentThreadWin32Thread();
455 HGDIOBJ Handle;
456
457 Entry->KernelData = newObject;
458
459 /* copy the reuse-counter */
460 TypeInfo |= Entry->Type & GDI_ENTRY_REUSE_MASK;
461
462 /* we found a free entry, no need to exchange this field atomically
463 since we're holding the lock */
464 Entry->Type = TypeInfo;
465
466 /* Create a handle */
467 Handle = (HGDIOBJ)((Index & 0xFFFF) | (TypeInfo << GDI_ENTRY_UPPER_SHIFT));
468
469 /* Initialize BaseObject fields */
470 newObject->hHmgr = Handle;
471 newObject->ulShareCount = 0;
472 newObject->cExclusiveLock = 1;
473 newObject->Tid = Thread;
474
475 AllocTypeDataDump(TypeInfo);
476
477 /* unlock the entry */
478 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, CurrentProcessId);
479
480 GDIDBG_CAPTUREALLOCATOR(Index);
481
482 if (W32Process != NULL)
483 {
484 InterlockedIncrement(&W32Process->GDIHandleCount);
485 }
486
487 DPRINT("GDIOBJ_AllocObj: 0x%x ob: 0x%x\n", Handle, newObject);
488 return newObject;
489 }
490 else
491 {
492 GDIDBG_TRACELOOP(Index, PrevProcId, CurrentProcessId);
493 /* damn, someone is trying to lock the object even though it doesn't
494 even exist anymore, wait a little and try again!
495 FIXME - we shouldn't loop forever! Give up after some time! */
496 DelayExecution();
497 /* try again */
498 goto LockHandle;
499 }
500 }
501
502 GDIOBJ_FreeObj(newObject, TypeIndex);
503
504 DPRINT1("Failed to insert gdi object into the handle table, no handles left!\n");
505 GDIDBG_DUMPHANDLETABLE();
506
507 return NULL;
508 }
509
510
511 VOID INTERNAL_CALL
512 GDIOBJ_FreeObj(POBJ pObject, UCHAR BaseType)
513 {
514 /* Object must not have a handle! */
515 ASSERT(pObject->hHmgr == NULL);
516
517 if (ObjTypeInfo[BaseType].bUseLookaside)
518 {
519 PPAGED_LOOKASIDE_LIST LookasideList;
520
521 LookasideList = GdiHandleTable->LookasideLists + BaseType;
522 ExFreeToPagedLookasideList(LookasideList, pObject);
523 }
524 else
525 {
526 ExFreePool(pObject);
527 }
528 }
529
530 /*!
531 * Free memory allocated for the GDI object. For each object type this function calls the
532 * appropriate cleanup routine.
533 *
534 * \param hObj - handle of the object to be deleted.
535 *
536 * \return Returns TRUE if succesful.
537 * \return Returns FALSE if the cleanup routine returned FALSE or the object doesn't belong
538 * to the calling process.
539 *
540 * \bug This function should return VOID and kill the object no matter what...
541 */
542 BOOL INTERNAL_CALL
543 GDIOBJ_FreeObjByHandle(HGDIOBJ hObj, DWORD ExpectedType)
544 {
545 PGDI_TABLE_ENTRY Entry;
546 HANDLE ProcessId, LockedProcessId, PrevProcId;
547 ULONG HandleType, HandleUpper, TypeIndex;
548 BOOL Silent;
549
550 GDIDBG_INITLOOPTRACE();
551
552 DPRINT("GDIOBJ_FreeObj: hObj: 0x%08x\n", hObj);
553
554 if (GDI_HANDLE_IS_STOCKOBJ(hObj))
555 {
556 DPRINT1("GDIOBJ_FreeObj() failed, can't delete stock object handle: 0x%x !!!\n", hObj);
557 GDIDBG_TRACECALLER();
558 return FALSE;
559 }
560
561 ProcessId = PsGetCurrentProcessId();
562 LockedProcessId = (HANDLE)((ULONG_PTR)ProcessId | 0x1);
563
564 Silent = (ExpectedType & GDI_OBJECT_TYPE_SILENT);
565 ExpectedType &= ~GDI_OBJECT_TYPE_SILENT;
566
567 HandleType = GDI_HANDLE_GET_TYPE(hObj);
568 HandleUpper = GDI_HANDLE_GET_UPPER(hObj);
569
570 /* Check if we have the requested type */
571 if ( (ExpectedType != GDI_OBJECT_TYPE_DONTCARE &&
572 HandleType != ExpectedType) ||
573 HandleType == 0 )
574 {
575 DPRINT1("Attempted to free object 0x%x of wrong type (Handle: 0x%x, expected: 0x%x)\n",
576 hObj, HandleType, ExpectedType);
577 GDIDBG_TRACECALLER();
578 return FALSE;
579 }
580
581 Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, hObj);
582
583 LockHandle:
584 /* lock the object, we must not delete global objects, so don't exchange the locking
585 process ID to zero when attempting to lock a global object... */
586 PrevProcId = InterlockedCompareExchangePointer((PVOID*)&Entry->ProcessId, LockedProcessId, ProcessId);
587 if (PrevProcId == ProcessId)
588 {
589 if ( (Entry->KernelData != NULL) &&
590 ((Entry->Type << GDI_ENTRY_UPPER_SHIFT) == HandleUpper) &&
591 ((Entry->Type & GDI_ENTRY_BASETYPE_MASK) == (HandleUpper & GDI_ENTRY_BASETYPE_MASK)) )
592 {
593 POBJ Object;
594
595 Object = Entry->KernelData;
596
597 if ((Object->cExclusiveLock == 0 ||
598 Object->Tid == (PTHREADINFO)PsGetCurrentThreadWin32Thread()) &&
599 Object->ulShareCount == 0)
600 {
601 BOOL Ret;
602 PPROCESSINFO W32Process = PsGetCurrentProcessWin32Process();
603
604 /* Clear the basetype field so when unlocking the handle it gets finally deleted and increment reuse counter */
605 Entry->Type = (Entry->Type + GDI_ENTRY_REUSE_INC) & ~GDI_ENTRY_BASETYPE_MASK;
606
607 /* unlock the handle slot */
608 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, NULL);
609
610 /* push this entry to the free list */
611 InterlockedPushFreeEntry(GDI_ENTRY_TO_INDEX(GdiHandleTable, Entry));
612
613 Object->hHmgr = NULL;
614
615 if (W32Process != NULL)
616 {
617 InterlockedDecrement(&W32Process->GDIHandleCount);
618 }
619
620 /* call the cleanup routine. */
621 TypeIndex = GDI_OBJECT_GET_TYPE_INDEX(HandleType);
622 Ret = ObjTypeInfo[TypeIndex].CleanupProc(Object);
623
624 DeAllocTypeDataDump(HandleType);
625
626 /* Now it's time to free the memory */
627 GDIOBJ_FreeObj(Object, TypeIndex);
628
629 GDIDBG_CAPTUREDELETER(hObj);
630 return Ret;
631 }
632 else if (Object->ulShareCount != 0)
633 {
634 NTSTATUS Status;
635 PEPROCESS OldProcess;
636 Object->BaseFlags |= BASEFLAG_READY_TO_DIE;
637 DPRINT("Object %p, ulShareCount = %d\n", Object->hHmgr, Object->ulShareCount);
638 //GDIDBG_TRACECALLER();
639 //GDIDBG_TRACESHARELOCKER(GDI_HANDLE_GET_INDEX(hObj));
640 /* Set NULL owner. This will permit an other process to kill the object
641 * Do the work here to avoid race conditions */
642 Status = PsLookupProcessByProcessId((HANDLE)((ULONG_PTR)PrevProcId & ~0x1), &OldProcess);
643 if (NT_SUCCESS(Status))
644 {
645 PPROCESSINFO W32Process = (PPROCESSINFO)OldProcess->Win32Process;
646 if (W32Process != NULL)
647 {
648 InterlockedDecrement(&W32Process->GDIHandleCount);
649 }
650 ObDereferenceObject(OldProcess);
651 }
652 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
653 /* Don't wait on shared locks */
654 return FALSE;
655 }
656 else
657 {
658 /*
659 * The object is currently locked by another thread, so freeing is forbidden!
660 */
661 DPRINT1("Object->cExclusiveLock = %d\n", Object->cExclusiveLock);
662 GDIDBG_TRACECALLER();
663 GDIDBG_TRACELOCKER(GDI_HANDLE_GET_INDEX(hObj));
664 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
665 /* do not assert here for it will call again from dxg.sys it being call twice */
666
667 DelayExecution();
668 goto LockHandle;
669 }
670 }
671 else
672 {
673 LockErrorDebugOutput(hObj, Entry, "GDIOBJ_FreeObj");
674 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
675 }
676 }
677 else if (PrevProcId == LockedProcessId)
678 {
679 GDIDBG_TRACELOOP(hObj, PrevProcId, ProcessId);
680
681 /* the object is currently locked, wait some time and try again.
682 FIXME - we shouldn't loop forever! Give up after some time! */
683 DelayExecution();
684 /* try again */
685 goto LockHandle;
686 }
687 else
688 {
689 if (!Silent)
690 {
691 if ((Entry->Type & GDI_ENTRY_BASETYPE_MASK) == 0)
692 {
693 DPRINT1("Attempted to free gdi handle 0x%x that is already deleted!\n", hObj);
694 }
695 else if (((ULONG_PTR)PrevProcId & ~0x1) == 0)
696 {
697 DPRINT1("Attempted to free global gdi handle 0x%x, caller needs to get ownership first!!!\n", hObj);
698 }
699 else
700 {
701 DPRINT1("Attempted to free foreign handle: 0x%x Owner: 0x%x from Caller: 0x%x\n", hObj, (ULONG_PTR)PrevProcId & ~0x1, (ULONG_PTR)ProcessId & ~0x1);
702 }
703 DPRINT1("Type = 0x%lx, KernelData = 0x%p, ProcessId = 0x%p\n", Entry->Type, Entry->KernelData, Entry->ProcessId);
704 GDIDBG_TRACECALLER();
705 GDIDBG_TRACEALLOCATOR(GDI_HANDLE_GET_INDEX(hObj));
706 }
707 }
708
709 return FALSE;
710 }
711
712 BOOL
713 FASTCALL
714 IsObjectDead(HGDIOBJ hObject)
715 {
716 INT Index = GDI_HANDLE_GET_INDEX(hObject);
717 PGDI_TABLE_ENTRY Entry = &GdiHandleTable->Entries[Index];
718 // We check to see if the objects are knocking on deaths door.
719 if ((Entry->Type & GDI_ENTRY_BASETYPE_MASK) != 0)
720 return FALSE;
721 else
722 {
723 DPRINT1("Object 0x%x currently being destroyed!!!\n",hObject);
724 return TRUE; // return true and move on.
725 }
726 }
727
728
729 BOOL
730 FASTCALL
731 bPEBCacheHandle(HGDIOBJ Handle, int oType, PVOID pAttr)
732 {
733 PGDIHANDLECACHE GdiHandleCache;
734 HGDIOBJ *hPtr;
735 BOOL Ret = FALSE;
736 int Offset = 0, Number;
737 HANDLE Lock;
738
739 GdiHandleCache = (PGDIHANDLECACHE)NtCurrentTeb()->ProcessEnvironmentBlock->GdiHandleBuffer;
740
741 switch (oType)
742 {
743 case hctBrushHandle:
744 Offset = 0;
745 break;
746
747 case hctPenHandle:
748 Offset = CACHE_BRUSH_ENTRIES;
749 break;
750
751 case hctRegionHandle:
752 Offset = CACHE_BRUSH_ENTRIES+CACHE_PEN_ENTRIES;
753 break;
754
755 default:
756 return FALSE;
757 }
758
759 Lock = InterlockedCompareExchangePointer( (PVOID*)&GdiHandleCache->ulLock,
760 NtCurrentTeb(),
761 NULL );
762 if (Lock) return FALSE;
763
764 _SEH2_TRY
765 {
766 Number = GdiHandleCache->ulNumHandles[oType];
767
768 hPtr = GdiHandleCache->Handle + Offset;
769
770 if ( pAttr && oType == hctRegionHandle)
771 {
772 if ( Number < CACHE_REGION_ENTRIES )
773 {
774 ((PRGN_ATTR)pAttr)->AttrFlags |= ATTR_CACHED;
775 hPtr[Number] = Handle;
776 GdiHandleCache->ulNumHandles[oType]++;
777 DPRINT("Put Handle Count %d PEB 0x%x\n", GdiHandleCache->ulNumHandles[oType], NtCurrentTeb()->ProcessEnvironmentBlock);
778 Ret = TRUE;
779 }
780 }
781 }
782 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
783 {
784 Ret = FALSE;
785 }
786 _SEH2_END;
787
788 (void)InterlockedExchangePointer((PVOID*)&GdiHandleCache->ulLock, Lock);
789 return Ret;
790 }
791
792 /*!
793 * Delete GDI object
794 * \param hObject object handle
795 * \return if the function fails the returned value is FALSE.
796 */
797 BOOL
798 FASTCALL
799 GreDeleteObject(HGDIOBJ hObject)
800 {
801 INT Index;
802 PGDI_TABLE_ENTRY Entry;
803 DWORD dwObjectType;
804 PVOID pAttr = NULL;
805
806 DPRINT("NtGdiDeleteObject handle 0x%08x\n", hObject);
807 if (!IsObjectDead(hObject))
808 {
809 dwObjectType = GDIOBJ_GetObjectType(hObject);
810
811 Index = GDI_HANDLE_GET_INDEX(hObject);
812 Entry = &GdiHandleTable->Entries[Index];
813 pAttr = Entry->UserData;
814
815 switch (dwObjectType)
816 {
817 case GDI_OBJECT_TYPE_BRUSH:
818 break;
819
820 case GDI_OBJECT_TYPE_REGION:
821 /* If pAttr NULL, the probability is high for System Region. */
822 if ( pAttr &&
823 bPEBCacheHandle(hObject, hctRegionHandle, pAttr))
824 {
825 /* User space handle only! */
826 return TRUE;
827 }
828 if (pAttr)
829 {
830 FreeObjectAttr(pAttr);
831 Entry->UserData = NULL;
832 }
833 break;
834
835 case GDI_OBJECT_TYPE_DC:
836 DC_FreeDcAttr(hObject);
837 break;
838 }
839
840 return NULL != hObject
841 ? GDIOBJ_FreeObjByHandle(hObject, dwObjectType) : FALSE;
842 }
843 else
844 {
845 DPRINT1("Attempt DeleteObject 0x%x currently being destroyed!!!\n",hObject);
846 return TRUE; // return true and move on.
847 }
848 }
849
850 VOID
851 FASTCALL
852 IntDeleteHandlesForProcess(struct _EPROCESS *Process, ULONG ObjectType)
853 {
854 PGDI_TABLE_ENTRY Entry, End;
855 ULONG Index = RESERVE_ENTRIES_COUNT;
856 HANDLE ProcId;
857 PPROCESSINFO W32Process;
858
859 W32Process = (PPROCESSINFO)Process->Win32Process;
860 ASSERT(W32Process);
861
862 if (W32Process->GDIHandleCount > 0)
863 {
864 ProcId = Process->UniqueProcessId;
865
866 /* FIXME - Instead of building the handle here and delete it using GDIOBJ_FreeObj
867 we should delete it directly here! */
868
869 End = &GdiHandleTable->Entries[GDI_HANDLE_COUNT];
870 for (Entry = &GdiHandleTable->Entries[RESERVE_ENTRIES_COUNT];
871 Entry != End;
872 Entry++, Index++)
873 {
874 /* ignore the lock bit */
875 if ( (HANDLE)((ULONG_PTR)Entry->ProcessId & ~0x1) == ProcId)
876 {
877 if ( (Entry->Type & GDI_ENTRY_BASETYPE_MASK) == ObjectType ||
878 ObjectType == GDI_OBJECT_TYPE_DONTCARE)
879 {
880 HGDIOBJ ObjectHandle;
881
882 /* Create the object handle for the entry, the lower(!) 16 bit of the
883 Type field includes the type of the object including the stock
884 object flag - but since stock objects don't have a process id we can
885 simply ignore this fact here. */
886 ObjectHandle = (HGDIOBJ)(Index | (Entry->Type << GDI_ENTRY_UPPER_SHIFT));
887
888 if (!GDIOBJ_FreeObjByHandle(ObjectHandle, GDI_OBJECT_TYPE_DONTCARE))
889 {
890 DPRINT1("Failed to delete object %p!\n", ObjectHandle);
891 }
892
893 if (W32Process->GDIHandleCount == 0)
894 {
895 /* there are no more gdi handles for this process, bail */
896 break;
897 }
898 }
899 }
900 }
901 }
902 }
903
904
905 /*!
906 * Internal function. Called when the process is destroyed to free the remaining GDI handles.
907 * \param Process - PID of the process that will be destroyed.
908 */
909 BOOL INTERNAL_CALL
910 GDI_CleanupForProcess(struct _EPROCESS *Process)
911 {
912 PEPROCESS CurrentProcess;
913 PPROCESSINFO W32Process;
914
915 DPRINT("Starting CleanupForProcess prochandle %x Pid %d\n", Process, Process->UniqueProcessId);
916 CurrentProcess = PsGetCurrentProcess();
917 if (CurrentProcess != Process)
918 {
919 KeAttachProcess(&Process->Pcb);
920 }
921
922 W32Process = (PPROCESSINFO)CurrentProcess->Win32Process;
923
924 /* Delete objects. Begin with types that are not referenced by other types */
925 IntDeleteHandlesForProcess(Process, GDILoObjType_LO_DC_TYPE);
926 IntDeleteHandlesForProcess(Process, GDILoObjType_LO_BRUSH_TYPE);
927 IntDeleteHandlesForProcess(Process, GDILoObjType_LO_BITMAP_TYPE);
928
929 /* Finally finish with what's left */
930 IntDeleteHandlesForProcess(Process, GDI_OBJECT_TYPE_DONTCARE);
931
932 if (CurrentProcess != Process)
933 {
934 KeDetachProcess();
935 }
936
937 #ifdef GDI_DEBUG
938 GdiDbgHTIntegrityCheck();
939 #endif
940
941 DPRINT("Completed cleanup for process %d\n", Process->UniqueProcessId);
942 if (W32Process->GDIHandleCount > 0)
943 {
944 DPRINT1("Leaking %d handles!\n", W32Process->GDIHandleCount);
945 }
946
947 return TRUE;
948 }
949
950 /*!
951 * Return pointer to the object by handle.
952 *
953 * \param hObj Object handle
954 * \return Pointer to the object.
955 *
956 * \note Process can only get pointer to the objects it created or global objects.
957 *
958 * \todo Get rid of the ExpectedType parameter!
959 */
960 PGDIOBJ INTERNAL_CALL
961 GDIOBJ_LockObj(HGDIOBJ hObj, DWORD ExpectedType)
962 {
963 ULONG HandleIndex;
964 PGDI_TABLE_ENTRY Entry;
965 HANDLE ProcessId, HandleProcessId, LockedProcessId, PrevProcId;
966 POBJ Object = NULL;
967 ULONG HandleType, HandleUpper;
968
969 /* Check for dummy call */
970 if(hObj == NULL)
971 return NULL ;
972
973 HandleIndex = GDI_HANDLE_GET_INDEX(hObj);
974 HandleType = GDI_HANDLE_GET_TYPE(hObj);
975 HandleUpper = GDI_HANDLE_GET_UPPER(hObj);
976
977 /* Check that the handle index is valid. */
978 if (HandleIndex >= GDI_HANDLE_COUNT)
979 return NULL;
980
981 Entry = &GdiHandleTable->Entries[HandleIndex];
982
983 /* Check if we have the requested type */
984 if ( (ExpectedType != GDI_OBJECT_TYPE_DONTCARE &&
985 HandleType != ExpectedType) ||
986 HandleType == 0 )
987 {
988 DPRINT("Attempted to lock object 0x%x of wrong type (Handle: 0x%x, requested: 0x%x)\n",
989 hObj, HandleType, ExpectedType);
990 GDIDBG_TRACECALLER();
991 GDIDBG_TRACEALLOCATOR(hObj);
992 GDIDBG_TRACEDELETER(hObj);
993 return NULL;
994 }
995
996 ProcessId = (HANDLE)((ULONG_PTR)PsGetCurrentProcessId() & ~1);
997 HandleProcessId = (HANDLE)((ULONG_PTR)Entry->ProcessId & ~1);
998
999 /* Check for invalid owner. */
1000 if (ProcessId != HandleProcessId && HandleProcessId != NULL)
1001 {
1002 DPRINT1("Tried to lock object (0x%p) of wrong owner! ProcessId = %p, HandleProcessId = %p\n", hObj, ProcessId, HandleProcessId);
1003 GDIDBG_TRACECALLER();
1004 GDIDBG_TRACEALLOCATOR(GDI_HANDLE_GET_INDEX(hObj));
1005 return NULL;
1006 }
1007
1008 /*
1009 * Prevent the thread from being terminated during the locking process.
1010 * It would result in undesired effects and inconsistency of the global
1011 * handle table.
1012 */
1013
1014 KeEnterCriticalRegion();
1015
1016 /*
1017 * Loop until we either successfully lock the handle entry & object or
1018 * fail some of the check.
1019 */
1020
1021 for (;;)
1022 {
1023 /* Lock the handle table entry. */
1024 LockedProcessId = (HANDLE)((ULONG_PTR)HandleProcessId | 0x1);
1025 PrevProcId = InterlockedCompareExchangePointer((PVOID*)&Entry->ProcessId,
1026 LockedProcessId,
1027 HandleProcessId);
1028
1029 if (PrevProcId == HandleProcessId)
1030 {
1031 /*
1032 * We're locking an object that belongs to our process or it's a
1033 * global object if HandleProcessId is 0 here.
1034 */
1035
1036 if ( (Entry->KernelData != NULL) &&
1037 ((Entry->Type << GDI_ENTRY_UPPER_SHIFT) == HandleUpper) )
1038 {
1039 PTHREADINFO Thread = (PTHREADINFO)PsGetCurrentThreadWin32Thread();
1040 Object = Entry->KernelData;
1041
1042 if (Object->cExclusiveLock == 0)
1043 {
1044 Object->Tid = Thread;
1045 Object->cExclusiveLock = 1;
1046 GDIDBG_CAPTURELOCKER(GDI_HANDLE_GET_INDEX(hObj))
1047 }
1048 else
1049 {
1050 if (Object->Tid != Thread)
1051 {
1052 /* Unlock the handle table entry. */
1053 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
1054
1055 DelayExecution();
1056 continue;
1057 }
1058 InterlockedIncrement((PLONG)&Object->cExclusiveLock);
1059 }
1060 }
1061 else
1062 {
1063 /*
1064 * Debugging code. Report attempts to lock deleted handles and
1065 * locking type mismatches.
1066 */
1067 LockErrorDebugOutput(hObj, Entry, "GDIOBJ_LockObj");
1068 }
1069
1070 /* Unlock the handle table entry. */
1071 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
1072
1073 break;
1074 }
1075 else
1076 {
1077 /*
1078 * The handle is currently locked, wait some time and try again.
1079 */
1080
1081 DelayExecution();
1082 continue;
1083 }
1084 }
1085
1086 KeLeaveCriticalRegion();
1087
1088 return Object;
1089 }
1090
1091
1092 /*!
1093 * Return pointer to the object by handle (and allow sharing of the handle
1094 * across threads).
1095 *
1096 * \param hObj Object handle
1097 * \return Pointer to the object.
1098 *
1099 * \note Process can only get pointer to the objects it created or global objects.
1100 *
1101 * \todo Get rid of the ExpectedType parameter!
1102 */
1103 PGDIOBJ INTERNAL_CALL
1104 GDIOBJ_ShareLockObj(HGDIOBJ hObj, DWORD ExpectedType)
1105 {
1106 ULONG HandleIndex;
1107 PGDI_TABLE_ENTRY Entry;
1108 HANDLE ProcessId, HandleProcessId, LockedProcessId, PrevProcId;
1109 POBJ Object = NULL;
1110 ULONG_PTR HandleType, HandleUpper;
1111
1112 /* Check for dummy call */
1113 if(hObj == NULL)
1114 return NULL ;
1115
1116 HandleIndex = GDI_HANDLE_GET_INDEX(hObj);
1117 HandleType = GDI_HANDLE_GET_TYPE(hObj);
1118 HandleUpper = GDI_HANDLE_GET_UPPER(hObj);
1119
1120 /* Check that the handle index is valid. */
1121 if (HandleIndex >= GDI_HANDLE_COUNT)
1122 return NULL;
1123
1124 /* Check if we have the requested type */
1125 if ( (ExpectedType != GDI_OBJECT_TYPE_DONTCARE &&
1126 HandleType != ExpectedType) ||
1127 HandleType == 0 )
1128 {
1129 DPRINT1("Attempted to lock object 0x%x of wrong type (Handle: 0x%x, requested: 0x%x)\n",
1130 hObj, HandleType, ExpectedType);
1131 return NULL;
1132 }
1133
1134 Entry = &GdiHandleTable->Entries[HandleIndex];
1135
1136 ProcessId = (HANDLE)((ULONG_PTR)PsGetCurrentProcessId() & ~1);
1137 HandleProcessId = (HANDLE)((ULONG_PTR)Entry->ProcessId & ~1);
1138
1139 /* Check for invalid owner. */
1140 if (ProcessId != HandleProcessId && HandleProcessId != NULL)
1141 {
1142 return NULL;
1143 }
1144
1145 /*
1146 * Prevent the thread from being terminated during the locking process.
1147 * It would result in undesired effects and inconsistency of the global
1148 * handle table.
1149 */
1150
1151 KeEnterCriticalRegion();
1152
1153 /*
1154 * Loop until we either successfully lock the handle entry & object or
1155 * fail some of the check.
1156 */
1157
1158 for (;;)
1159 {
1160 /* Lock the handle table entry. */
1161 LockedProcessId = (HANDLE)((ULONG_PTR)HandleProcessId | 0x1);
1162 PrevProcId = InterlockedCompareExchangePointer((PVOID*)&Entry->ProcessId,
1163 LockedProcessId,
1164 HandleProcessId);
1165
1166 if (PrevProcId == HandleProcessId)
1167 {
1168 /*
1169 * We're locking an object that belongs to our process or it's a
1170 * global object if HandleProcessId is 0 here.
1171 */
1172
1173 if ( (Entry->KernelData != NULL) &&
1174 (HandleUpper == (Entry->Type << GDI_ENTRY_UPPER_SHIFT)) )
1175 {
1176 Object = (POBJ)Entry->KernelData;
1177
1178 GDIDBG_CAPTURESHARELOCKER(HandleIndex);
1179 #ifdef GDI_DEBUG3
1180 if (InterlockedIncrement((PLONG)&Object->ulShareCount) == 1)
1181 {
1182 memset(GDIHandleLocker[HandleIndex], 0x00, GDI_STACK_LEVELS * sizeof(ULONG));
1183 RtlCaptureStackBackTrace(1, GDI_STACK_LEVELS, (PVOID*)GDIHandleShareLocker[HandleIndex], NULL);
1184 }
1185 #else
1186 InterlockedIncrement((PLONG)&Object->ulShareCount);
1187 #endif
1188 }
1189 else
1190 {
1191 /*
1192 * Debugging code. Report attempts to lock deleted handles and
1193 * locking type mismatches.
1194 */
1195 LockErrorDebugOutput(hObj, Entry, "GDIOBJ_ShareLockObj");
1196 }
1197
1198 /* Unlock the handle table entry. */
1199 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
1200
1201 break;
1202 }
1203 else
1204 {
1205 /*
1206 * The handle is currently locked, wait some time and try again.
1207 */
1208
1209 DelayExecution();
1210 continue;
1211 }
1212 }
1213
1214 KeLeaveCriticalRegion();
1215
1216 return Object;
1217 }
1218
1219 BOOL INTERNAL_CALL
1220 GDIOBJ_OwnedByCurrentProcess(HGDIOBJ ObjectHandle)
1221 {
1222 PGDI_TABLE_ENTRY Entry;
1223 HANDLE ProcessId;
1224 BOOL Ret;
1225
1226 DPRINT("GDIOBJ_OwnedByCurrentProcess: ObjectHandle: 0x%08x\n", ObjectHandle);
1227
1228 if (!GDI_HANDLE_IS_STOCKOBJ(ObjectHandle))
1229 {
1230 ProcessId = PsGetCurrentProcessId();
1231
1232 Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, ObjectHandle);
1233 Ret = Entry->KernelData != NULL &&
1234 (Entry->Type & GDI_ENTRY_BASETYPE_MASK) != 0 &&
1235 (HANDLE)((ULONG_PTR)Entry->ProcessId & ~0x1) == ProcessId;
1236
1237 return Ret;
1238 }
1239
1240 return FALSE;
1241 }
1242
1243 BOOL INTERNAL_CALL
1244 GDIOBJ_ConvertToStockObj(HGDIOBJ *phObj)
1245 {
1246 /*
1247 * FIXME !!!!! THIS FUNCTION NEEDS TO BE FIXED - IT IS NOT SAFE WHEN OTHER THREADS
1248 * MIGHT ATTEMPT TO LOCK THE OBJECT DURING THIS CALL!!!
1249 */
1250 PGDI_TABLE_ENTRY Entry;
1251 HANDLE ProcessId, LockedProcessId, PrevProcId;
1252 PTHREADINFO Thread;
1253 HGDIOBJ hObj;
1254
1255 GDIDBG_INITLOOPTRACE();
1256
1257 ASSERT(phObj);
1258 hObj = *phObj;
1259
1260 DPRINT("GDIOBJ_ConvertToStockObj: hObj: 0x%08x\n", hObj);
1261
1262 Thread = (PTHREADINFO)PsGetCurrentThreadWin32Thread();
1263
1264 if (!GDI_HANDLE_IS_STOCKOBJ(hObj))
1265 {
1266 ProcessId = PsGetCurrentProcessId();
1267 LockedProcessId = (HANDLE)((ULONG_PTR)ProcessId | 0x1);
1268
1269 Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, hObj);
1270
1271 LockHandle:
1272 /* lock the object, we must not convert stock objects, so don't check!!! */
1273 PrevProcId = InterlockedCompareExchangePointer((PVOID*)&Entry->ProcessId, LockedProcessId, ProcessId);
1274 if (PrevProcId == ProcessId)
1275 {
1276 LONG NewType, PrevType, OldType;
1277
1278 /* we're locking an object that belongs to our process. First calculate
1279 the new object type including the stock object flag and then try to
1280 exchange it.*/
1281 /* On Windows the higher 16 bit of the type field don't contain the
1282 full type from the handle, but the base type.
1283 (type = BRSUH, PEN, EXTPEN, basetype = BRUSH) */
1284 OldType = ((ULONG)hObj & GDI_HANDLE_BASETYPE_MASK) | ((ULONG)hObj >> GDI_ENTRY_UPPER_SHIFT);
1285 /* We are currently not using bits 24..31 (flags) of the type field, but for compatibility
1286 we copy them as we can't get them from the handle */
1287 OldType |= Entry->Type & GDI_ENTRY_FLAGS_MASK;
1288
1289 /* As the object should be a stock object, set it's flag, but only in the lower 16 bits */
1290 NewType = OldType | GDI_ENTRY_STOCK_MASK;
1291
1292 /* Try to exchange the type field - but only if the old (previous type) matches! */
1293 PrevType = InterlockedCompareExchange(&Entry->Type, NewType, OldType);
1294 if (PrevType == OldType && Entry->KernelData != NULL)
1295 {
1296 PTHREADINFO PrevThread;
1297 POBJ Object;
1298
1299 /* We successfully set the stock object flag.
1300 KernelData should never be NULL here!!! */
1301 ASSERT(Entry->KernelData);
1302
1303 Object = Entry->KernelData;
1304
1305 PrevThread = Object->Tid;
1306 if (Object->cExclusiveLock == 0 || PrevThread == Thread)
1307 {
1308 /* dereference the process' object counter */
1309 if (PrevProcId != GDI_GLOBAL_PROCESS)
1310 {
1311 PEPROCESS OldProcess;
1312 PPROCESSINFO W32Process;
1313 NTSTATUS Status;
1314
1315 /* FIXME */
1316 Status = PsLookupProcessByProcessId((HANDLE)((ULONG_PTR)PrevProcId & ~0x1), &OldProcess);
1317 if (NT_SUCCESS(Status))
1318 {
1319 W32Process = (PPROCESSINFO)OldProcess->Win32Process;
1320 if (W32Process != NULL)
1321 {
1322 InterlockedDecrement(&W32Process->GDIHandleCount);
1323 }
1324 ObDereferenceObject(OldProcess);
1325 }
1326 }
1327
1328 hObj = (HGDIOBJ)((ULONG)(hObj) | GDI_HANDLE_STOCK_MASK);
1329 *phObj = hObj;
1330 Object->hHmgr = hObj;
1331
1332 /* remove the process id lock and make it global */
1333 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, GDI_GLOBAL_PROCESS);
1334
1335 /* we're done, successfully converted the object */
1336 return TRUE;
1337 }
1338 else
1339 {
1340 GDIDBG_TRACELOOP(hObj, PrevThread, Thread);
1341
1342 /* WTF?! The object is already locked by a different thread!
1343 Release the lock, wait a bit and try again!
1344 FIXME - we should give up after some time unless we want to wait forever! */
1345 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
1346
1347 DelayExecution();
1348 goto LockHandle;
1349 }
1350 }
1351 else
1352 {
1353 DPRINT1("Attempted to convert object 0x%x that is deleted! Should never get here!!!\n", hObj);
1354 DPRINT1("OldType = 0x%x, Entry->Type = 0x%x, NewType = 0x%x, Entry->KernelData = 0x%x\n", OldType, Entry->Type, NewType, Entry->KernelData);
1355 }
1356 }
1357 else if (PrevProcId == LockedProcessId)
1358 {
1359 GDIDBG_TRACELOOP(hObj, PrevProcId, ProcessId);
1360
1361 /* the object is currently locked, wait some time and try again.
1362 FIXME - we shouldn't loop forever! Give up after some time! */
1363 DelayExecution();
1364 /* try again */
1365 goto LockHandle;
1366 }
1367 else
1368 {
1369 DPRINT1("Attempted to convert invalid handle: 0x%x\n", hObj);
1370 }
1371 }
1372
1373 return FALSE;
1374 }
1375
1376 BOOL INTERNAL_CALL
1377 GDIOBJ_SetOwnership(HGDIOBJ ObjectHandle, PEPROCESS NewOwner)
1378 {
1379 PGDI_TABLE_ENTRY Entry;
1380 HANDLE ProcessId, LockedProcessId, PrevProcId;
1381 PTHREADINFO Thread;
1382 BOOL Ret = TRUE;
1383
1384 GDIDBG_INITLOOPTRACE();
1385
1386 DPRINT("GDIOBJ_SetOwnership: hObj: 0x%x, NewProcess: 0x%x\n", ObjectHandle, (NewOwner ? PsGetProcessId(NewOwner) : 0));
1387
1388 Thread = (PTHREADINFO)PsGetCurrentThreadWin32Thread();
1389
1390 if (!GDI_HANDLE_IS_STOCKOBJ(ObjectHandle))
1391 {
1392 ProcessId = PsGetCurrentProcessId();
1393 LockedProcessId = (HANDLE)((ULONG_PTR)ProcessId | 0x1);
1394
1395 Entry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, ObjectHandle);
1396
1397 LockHandle:
1398 /* lock the object, we must not convert stock objects, so don't check!!! */
1399 PrevProcId = InterlockedCompareExchangePointer((PVOID*)&Entry->ProcessId, ProcessId, LockedProcessId);
1400 if (PrevProcId == ProcessId)
1401 {
1402 PTHREADINFO PrevThread;
1403
1404 if ((Entry->Type & GDI_ENTRY_BASETYPE_MASK) != 0)
1405 {
1406 POBJ Object = Entry->KernelData;
1407
1408 PrevThread = Object->Tid;
1409 if (Object->cExclusiveLock == 0 || PrevThread == Thread)
1410 {
1411 PEPROCESS OldProcess;
1412 PPROCESSINFO W32Process;
1413 NTSTATUS Status;
1414
1415 /* dereference the process' object counter */
1416 /* FIXME */
1417 if ((ULONG_PTR)PrevProcId & ~0x1)
1418 {
1419 Status = PsLookupProcessByProcessId((HANDLE)((ULONG_PTR)PrevProcId & ~0x1), &OldProcess);
1420 if (NT_SUCCESS(Status))
1421 {
1422 W32Process = (PPROCESSINFO)OldProcess->Win32Process;
1423 if (W32Process != NULL)
1424 {
1425 InterlockedDecrement(&W32Process->GDIHandleCount);
1426 }
1427 ObDereferenceObject(OldProcess);
1428 }
1429 }
1430
1431 if (NewOwner != NULL)
1432 {
1433 ProcessId = PsGetProcessId(NewOwner);
1434
1435 /* Increase the new process' object counter */
1436 W32Process = (PPROCESSINFO)NewOwner->Win32Process;
1437 if (W32Process != NULL)
1438 {
1439 InterlockedIncrement(&W32Process->GDIHandleCount);
1440 }
1441 }
1442 else
1443 ProcessId = 0;
1444
1445 /* remove the process id lock and change it to the new process id */
1446 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, ProcessId);
1447
1448 /* we're done! */
1449 return Ret;
1450 }
1451 else
1452 {
1453 GDIDBG_TRACELOOP(ObjectHandle, PrevThread, Thread);
1454
1455 /* WTF?! The object is already locked by a different thread!
1456 Release the lock, wait a bit and try again! DO reset the pid lock
1457 so we make sure we don't access invalid memory in case the object is
1458 being deleted in the meantime (because we don't have aquired a reference
1459 at this point).
1460 FIXME - we should give up after some time unless we want to wait forever! */
1461 (void)InterlockedExchangePointer((PVOID*)&Entry->ProcessId, PrevProcId);
1462
1463 DelayExecution();
1464 goto LockHandle;
1465 }
1466 }
1467 else
1468 {
1469 DPRINT1("Attempted to change ownership of an object 0x%x currently being destroyed!!!\n", ObjectHandle);
1470 DPRINT1("Entry->Type = 0x%lx, Entry->KernelData = 0x%p\n", Entry->Type, Entry->KernelData);
1471 Ret = FALSE;
1472 }
1473 }
1474 else if (PrevProcId == LockedProcessId)
1475 {
1476 GDIDBG_TRACELOOP(ObjectHandle, PrevProcId, ProcessId);
1477
1478 /* the object is currently locked, wait some time and try again.
1479 FIXME - we shouldn't loop forever! Give up after some time! */
1480 DelayExecution();
1481 /* try again */
1482 goto LockHandle;
1483 }
1484 else if (((ULONG_PTR)PrevProcId & ~0x1) == 0)
1485 {
1486 /* allow changing ownership of global objects */
1487 ProcessId = NULL;
1488 LockedProcessId = (HANDLE)((ULONG_PTR)ProcessId | 0x1);
1489 goto LockHandle;
1490 }
1491 else if ((HANDLE)((ULONG_PTR)PrevProcId & ~0x1) != PsGetCurrentProcessId())
1492 {
1493 DPRINT1("Attempted to change ownership of object 0x%x (pid: 0x%x) from pid 0x%x!!!\n", ObjectHandle, (ULONG_PTR)PrevProcId & ~0x1, PsGetCurrentProcessId());
1494 Ret = FALSE;
1495 }
1496 else
1497 {
1498 DPRINT1("Attempted to change owner of invalid handle: 0x%x\n", ObjectHandle);
1499 Ret = FALSE;
1500 }
1501 }
1502 return Ret;
1503 }
1504
1505 BOOL INTERNAL_CALL
1506 GDIOBJ_CopyOwnership(HGDIOBJ CopyFrom, HGDIOBJ CopyTo)
1507 {
1508 PGDI_TABLE_ENTRY FromEntry;
1509 PTHREADINFO Thread;
1510 HANDLE FromProcessId, FromLockedProcessId, FromPrevProcId;
1511 BOOL Ret = TRUE;
1512
1513 GDIDBG_INITLOOPTRACE();
1514
1515 DPRINT("GDIOBJ_CopyOwnership: from: 0x%x, to: 0x%x\n", CopyFrom, CopyTo);
1516
1517 Thread = (PTHREADINFO)PsGetCurrentThreadWin32Thread();
1518
1519 if (!GDI_HANDLE_IS_STOCKOBJ(CopyFrom) && !GDI_HANDLE_IS_STOCKOBJ(CopyTo))
1520 {
1521 FromEntry = GDI_HANDLE_GET_ENTRY(GdiHandleTable, CopyFrom);
1522
1523 FromProcessId = (HANDLE)((ULONG_PTR)FromEntry->ProcessId & ~0x1);
1524 FromLockedProcessId = (HANDLE)((ULONG_PTR)FromProcessId | 0x1);
1525
1526 LockHandleFrom:
1527 /* lock the object, we must not convert stock objects, so don't check!!! */
1528 FromPrevProcId = InterlockedCompareExchangePointer((PVOID*)&FromEntry->ProcessId, FromProcessId, FromLockedProcessId);
1529 if (FromPrevProcId == FromProcessId)
1530 {
1531 PTHREADINFO PrevThread;
1532 POBJ Object;
1533
1534 if ((FromEntry->Type & GDI_ENTRY_BASETYPE_MASK) != 0)
1535 {
1536 Object = FromEntry->KernelData;
1537
1538 /* save the pointer to the calling thread so we know it was this thread
1539 that locked the object */
1540 PrevThread = Object->Tid;
1541 if (Object->cExclusiveLock == 0 || PrevThread == Thread)
1542 {
1543 /* now let's change the ownership of the target object */
1544
1545 if (((ULONG_PTR)FromPrevProcId & ~0x1) != 0)
1546 {
1547 PEPROCESS ProcessTo;
1548 /* FIXME */
1549 if (NT_SUCCESS(PsLookupProcessByProcessId((HANDLE)((ULONG_PTR)FromPrevProcId & ~0x1), &ProcessTo)))
1550 {
1551 GDIOBJ_SetOwnership(CopyTo, ProcessTo);
1552 ObDereferenceObject(ProcessTo);
1553 }
1554 }
1555 else
1556 {
1557 /* mark the object as global */
1558 GDIOBJ_SetOwnership(CopyTo, NULL);
1559 }
1560
1561 (void)InterlockedExchangePointer((PVOID*)&FromEntry->ProcessId, FromPrevProcId);
1562 }
1563 else
1564 {
1565 GDIDBG_TRACELOOP(CopyFrom, PrevThread, Thread);
1566
1567 /* WTF?! The object is already locked by a different thread!
1568 Release the lock, wait a bit and try again! DO reset the pid lock
1569 so we make sure we don't access invalid memory in case the object is
1570 being deleted in the meantime (because we don't have aquired a reference
1571 at this point).
1572 FIXME - we should give up after some time unless we want to wait forever! */
1573 (void)InterlockedExchangePointer((PVOID*)&FromEntry->ProcessId, FromPrevProcId);
1574
1575 DelayExecution();
1576 goto LockHandleFrom;
1577 }
1578 }
1579 else
1580 {
1581 DPRINT1("Attempted to copy ownership from an object 0x%x currently being destroyed!!!\n", CopyFrom);
1582 Ret = FALSE;
1583 }
1584 }
1585 else if (FromPrevProcId == FromLockedProcessId)
1586 {
1587 GDIDBG_TRACELOOP(CopyFrom, FromPrevProcId, FromProcessId);
1588
1589 /* the object is currently locked, wait some time and try again.
1590 FIXME - we shouldn't loop forever! Give up after some time! */
1591 DelayExecution();
1592 /* try again */
1593 goto LockHandleFrom;
1594 }
1595 else if ((HANDLE)((ULONG_PTR)FromPrevProcId & ~0x1) != PsGetCurrentProcessId())
1596 {
1597 /* FIXME - should we really allow copying ownership from objects that we don't even own? */
1598 DPRINT1("WARNING! Changing copying ownership of object 0x%x (pid: 0x%x) to pid 0x%x!!!\n", CopyFrom, (ULONG_PTR)FromPrevProcId & ~0x1, PsGetCurrentProcessId());
1599 FromProcessId = (HANDLE)((ULONG_PTR)FromPrevProcId & ~0x1);
1600 FromLockedProcessId = (HANDLE)((ULONG_PTR)FromProcessId | 0x1);
1601 goto LockHandleFrom;
1602 }
1603 else
1604 {
1605 DPRINT1("Attempted to copy ownership from invalid handle: 0x%x\n", CopyFrom);
1606 Ret = FALSE;
1607 }
1608 }
1609 return Ret;
1610 }
1611
1612 PVOID INTERNAL_CALL
1613 GDI_MapHandleTable(PSECTION_OBJECT SectionObject, PEPROCESS Process)
1614 {
1615 PVOID MappedView = NULL;
1616 NTSTATUS Status;
1617 LARGE_INTEGER Offset;
1618 ULONG ViewSize = sizeof(GDI_HANDLE_TABLE);
1619
1620 Offset.QuadPart = 0;
1621
1622 ASSERT(SectionObject != NULL);
1623 ASSERT(Process != NULL);
1624
1625 Status = MmMapViewOfSection(SectionObject,
1626 Process,
1627 &MappedView,
1628 0,
1629 0,
1630 &Offset,
1631 &ViewSize,
1632 ViewUnmap,
1633 SEC_NO_CHANGE,
1634 PAGE_READONLY);
1635
1636 if (!NT_SUCCESS(Status))
1637 return NULL;
1638
1639 return MappedView;
1640 }
1641
1642 /** PUBLIC FUNCTIONS **********************************************************/
1643
1644 BOOL
1645 FASTCALL
1646 IntGdiSetRegionOwner(HRGN hRgn, DWORD OwnerMask)
1647 {
1648 INT Index;
1649 PGDI_TABLE_ENTRY Entry;
1650 /*
1651 System Regions:
1652 These regions do not use attribute sections and when allocated, use gdiobj
1653 level functions.
1654 */
1655 // FIXME! HAX!!! Remove this once we get everything right!
1656 Index = GDI_HANDLE_GET_INDEX(hRgn);
1657 Entry = &GdiHandleTable->Entries[Index];
1658 if (Entry->UserData) FreeObjectAttr(Entry->UserData);
1659 Entry->UserData = NULL;
1660 //
1661 if ((OwnerMask == GDI_OBJ_HMGR_PUBLIC) || OwnerMask == GDI_OBJ_HMGR_NONE)
1662 {
1663 return GDIOBJ_SetOwnership(hRgn, NULL);
1664 }
1665 if (OwnerMask == GDI_OBJ_HMGR_POWNED)
1666 {
1667 return GDIOBJ_SetOwnership((HGDIOBJ) hRgn, PsGetCurrentProcess() );
1668 }
1669 return FALSE;
1670 }
1671
1672 BOOL
1673 FASTCALL
1674 IntGdiSetBrushOwner(PBRUSH pbr, DWORD OwnerMask)
1675 {
1676 HBRUSH hBR;
1677 PEPROCESS Owner = NULL;
1678 PGDI_TABLE_ENTRY pEntry = NULL;
1679
1680 if (!pbr) return FALSE;
1681
1682 hBR = pbr->BaseObject.hHmgr;
1683
1684 if (!hBR || (GDI_HANDLE_GET_TYPE(hBR) != GDI_OBJECT_TYPE_BRUSH))
1685 return FALSE;
1686 else
1687 {
1688 INT Index = GDI_HANDLE_GET_INDEX((HGDIOBJ)hBR);
1689 pEntry = &GdiHandleTable->Entries[Index];
1690 }
1691
1692 if (pbr->flAttrs & GDIBRUSH_IS_GLOBAL)
1693 {
1694 GDIOBJ_ShareUnlockObjByPtr((POBJ)pbr);
1695 return TRUE;
1696 }
1697
1698 if ((OwnerMask == GDI_OBJ_HMGR_PUBLIC) || OwnerMask == GDI_OBJ_HMGR_NONE)
1699 {
1700 // Set this Brush to inaccessible mode and to an Owner of NONE.
1701 // if (OwnerMask == GDI_OBJ_HMGR_NONE) Owner = OwnerMask;
1702
1703 if (!GDIOBJ_SetOwnership((HGDIOBJ) hBR, Owner))
1704 return FALSE;
1705
1706 // Deny user access to User Data.
1707 pEntry->UserData = NULL; // This hBR is inaccessible!
1708 }
1709
1710 if (OwnerMask == GDI_OBJ_HMGR_POWNED)
1711 {
1712 if (!GDIOBJ_SetOwnership((HGDIOBJ) hBR, PsGetCurrentProcess() ))
1713 return FALSE;
1714
1715 // Allow user access to User Data.
1716 pEntry->UserData = pbr->pBrushAttr;
1717 }
1718 return TRUE;
1719 }
1720
1721 BOOL
1722 FASTCALL
1723 IntGdiSetDCOwnerEx( HDC hDC, DWORD OwnerMask, BOOL NoSetBrush)
1724 {
1725 PDC pDC;
1726 BOOL Ret = FALSE;
1727
1728 if (!hDC || (GDI_HANDLE_GET_TYPE(hDC) != GDI_OBJECT_TYPE_DC)) return FALSE;
1729
1730 if ((OwnerMask == GDI_OBJ_HMGR_PUBLIC) || OwnerMask == GDI_OBJ_HMGR_NONE)
1731 {
1732 pDC = DC_LockDc ( hDC );
1733 MmCopyFromCaller(&pDC->dcattr, pDC->pdcattr, sizeof(DC_ATTR));
1734 DC_UnlockDc( pDC );
1735
1736 DC_FreeDcAttr( hDC ); // Free the dcattr!
1737
1738 if (!DC_SetOwnership( hDC, NULL )) // This hDC is inaccessible!
1739 return Ret;
1740 }
1741
1742 if (OwnerMask == GDI_OBJ_HMGR_POWNED)
1743 {
1744 pDC = DC_LockDc ( hDC );
1745 ASSERT(pDC->pdcattr == &pDC->dcattr);
1746 DC_UnlockDc( pDC );
1747
1748 if (!DC_SetOwnership( hDC, PsGetCurrentProcess() )) return Ret;
1749
1750 DC_AllocateDcAttr( hDC ); // Allocate new dcattr
1751
1752 DCU_SynchDcAttrtoUser( hDC ); // Copy data from dc to dcattr
1753 }
1754
1755 if ((OwnerMask != GDI_OBJ_HMGR_NONE) && !NoSetBrush)
1756 {
1757 pDC = DC_LockDc ( hDC );
1758 if (IntGdiSetBrushOwner((PBRUSH)pDC->dclevel.pbrFill, OwnerMask))
1759 IntGdiSetBrushOwner((PBRUSH)pDC->dclevel.pbrLine, OwnerMask);
1760 DC_UnlockDc( pDC );
1761 }
1762 return TRUE;
1763 }
1764
1765 INT
1766 FASTCALL
1767 GreGetObjectOwner(HGDIOBJ Handle, GDIOBJTYPE ObjType)
1768 {
1769 INT Ret = GDI_OBJ_HMGR_RESTRICTED;
1770
1771 if ( GDI_HANDLE_GET_INDEX(Handle) < GDI_HANDLE_COUNT )
1772 {
1773 PGDI_TABLE_ENTRY pEntry = &GdiHandleTable->Entries[GDI_HANDLE_GET_INDEX(Handle)];
1774
1775 if (pEntry->ObjectType == ObjType)
1776 {
1777 if (pEntry->FullUnique == (GDI_HANDLE_GET_UPPER(Handle) >> GDI_ENTRY_UPPER_SHIFT))
1778 Ret = pEntry->ProcessId & ~1;
1779 }
1780 }
1781 return Ret;
1782 }
1783
1784 W32KAPI
1785 HANDLE
1786 APIENTRY
1787 NtGdiCreateClientObj(
1788 IN ULONG ulType
1789 )
1790 {
1791 POBJ pObject;
1792 HANDLE handle;
1793
1794 /* Mask out everything that would change the type in a wrong manner */
1795 ulType &= (GDI_HANDLE_TYPE_MASK & ~GDI_HANDLE_BASETYPE_MASK);
1796
1797 /* Allocate a new object */
1798 pObject = GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_CLIOBJ | ulType);
1799 if (!pObject)
1800 {
1801 return NULL;
1802 }
1803
1804 /* get the handle */
1805 handle = pObject->hHmgr;
1806
1807 /* Unlock it */
1808 GDIOBJ_UnlockObjByPtr(pObject);
1809
1810 return handle;
1811 }
1812
1813 W32KAPI
1814 BOOL
1815 APIENTRY
1816 NtGdiDeleteClientObj(
1817 IN HANDLE h
1818 )
1819 {
1820 /* We first need to get the real type from the handle */
1821 ULONG type = GDI_HANDLE_GET_TYPE(h);
1822
1823 /* Check if it's really a CLIENTOBJ */
1824 if ((type & GDI_HANDLE_BASETYPE_MASK) != GDILoObjType_LO_CLIENTOBJ_TYPE)
1825 {
1826 /* FIXME: SetLastError? */
1827 return FALSE;
1828 }
1829 return GDIOBJ_FreeObjByHandle(h, type);
1830 }
1831
1832 INT
1833 FASTCALL
1834 IntGdiGetObject(IN HANDLE Handle,
1835 IN INT cbCount,
1836 IN LPVOID lpBuffer)
1837 {
1838 PVOID pGdiObject;
1839 INT Result = 0;
1840 DWORD dwObjectType;
1841
1842 pGdiObject = GDIOBJ_LockObj(Handle, GDI_OBJECT_TYPE_DONTCARE);
1843 if (!pGdiObject)
1844 {
1845 SetLastWin32Error(ERROR_INVALID_HANDLE);
1846 return 0;
1847 }
1848
1849 dwObjectType = GDIOBJ_GetObjectType(Handle);
1850 switch (dwObjectType)
1851 {
1852 case GDI_OBJECT_TYPE_PEN:
1853 case GDI_OBJECT_TYPE_EXTPEN:
1854 Result = PEN_GetObject((PBRUSH) pGdiObject, cbCount, (PLOGPEN) lpBuffer); // IntGdiCreatePenIndirect
1855 break;
1856
1857 case GDI_OBJECT_TYPE_BRUSH:
1858 Result = BRUSH_GetObject((PBRUSH ) pGdiObject, cbCount, (LPLOGBRUSH)lpBuffer);
1859 break;
1860
1861 case GDI_OBJECT_TYPE_BITMAP:
1862 Result = BITMAP_GetObject((SURFACE *) pGdiObject, cbCount, lpBuffer);
1863 break;
1864 case GDI_OBJECT_TYPE_FONT:
1865 Result = FontGetObject((PTEXTOBJ) pGdiObject, cbCount, lpBuffer);
1866 #if 0
1867 // Fix the LOGFONT structure for the stock fonts
1868 if (FIRST_STOCK_HANDLE <= Handle && Handle <= LAST_STOCK_HANDLE)
1869 {
1870 FixStockFontSizeW(Handle, cbCount, lpBuffer);
1871 }
1872 #endif
1873 break;
1874
1875 case GDI_OBJECT_TYPE_PALETTE:
1876 Result = PALETTE_GetObject((PPALETTE) pGdiObject, cbCount, lpBuffer);
1877 break;
1878
1879 default:
1880 DPRINT1("GDI object type 0x%08x not implemented\n", dwObjectType);
1881 break;
1882 }
1883
1884 GDIOBJ_UnlockObjByPtr(pGdiObject);
1885
1886 return Result;
1887 }
1888
1889
1890
1891 W32KAPI
1892 INT
1893 APIENTRY
1894 NtGdiExtGetObjectW(IN HANDLE hGdiObj,
1895 IN INT cbCount,
1896 OUT LPVOID lpBuffer)
1897 {
1898 INT iRetCount = 0;
1899 INT cbCopyCount;
1900 union
1901 {
1902 BITMAP bitmap;
1903 DIBSECTION dibsection;
1904 LOGPEN logpen;
1905 LOGBRUSH logbrush;
1906 LOGFONTW logfontw;
1907 EXTLOGFONTW extlogfontw;
1908 ENUMLOGFONTEXDVW enumlogfontexdvw;
1909 } Object;
1910
1911 // Normalize to the largest supported object size
1912 cbCount = min((UINT)cbCount, sizeof(Object));
1913
1914 // Now do the actual call
1915 iRetCount = IntGdiGetObject(hGdiObj, cbCount, lpBuffer ? &Object : NULL);
1916 cbCopyCount = min((UINT)cbCount, (UINT)iRetCount);
1917
1918 // Make sure we have a buffer and a copy size
1919 if ((cbCopyCount) && (lpBuffer))
1920 {
1921 // Enter SEH for buffer transfer
1922 _SEH2_TRY
1923 {
1924 // Probe the buffer and copy it
1925 ProbeForWrite(lpBuffer, cbCopyCount, sizeof(WORD));
1926 RtlCopyMemory(lpBuffer, &Object, cbCopyCount);
1927 }
1928 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1929 {
1930 // Clear the return value.
1931 // Do *NOT* set last error here!
1932 iRetCount = 0;
1933 }
1934 _SEH2_END;
1935 }
1936 // Return the count
1937 return iRetCount;
1938 }
1939
1940 /* EOF */