gdi32: Add more parameter checks in GdiGradientFill.
[wine] / dlls / gdi32 / pen.c
1 /*
2  * GDI pen objects
3  *
4  * Copyright 1993 Alexandre Julliard
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 "config.h"
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "gdi_private.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
35
36   /* GDI logical pen object */
37 typedef struct
38 {
39     GDIOBJHDR header;
40     EXTLOGPEN logpen;
41 } PENOBJ;
42
43
44 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc );
45 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
46 static BOOL PEN_DeleteObject( HGDIOBJ handle );
47
48 static const struct gdi_obj_funcs pen_funcs =
49 {
50     PEN_SelectObject,  /* pSelectObject */
51     PEN_GetObject,     /* pGetObjectA */
52     PEN_GetObject,     /* pGetObjectW */
53     NULL,              /* pUnrealizeObject */
54     PEN_DeleteObject   /* pDeleteObject */
55 };
56
57
58 /***********************************************************************
59  *           CreatePen    (GDI32.@)
60  */
61 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
62 {
63     LOGPEN logpen;
64
65     TRACE("%d %d %06x\n", style, width, color );
66
67     logpen.lopnStyle = style;
68     logpen.lopnWidth.x = width;
69     logpen.lopnWidth.y = 0;
70     logpen.lopnColor = color;
71
72     return CreatePenIndirect( &logpen );
73 }
74
75
76 /***********************************************************************
77  *           CreatePenIndirect    (GDI32.@)
78  */
79 HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
80 {
81     PENOBJ * penPtr;
82     HPEN hpen;
83
84     if (pen->lopnStyle == PS_NULL)
85     {
86         hpen = GetStockObject(NULL_PEN);
87         if (hpen) return hpen;
88     }
89
90     if (!(penPtr = HeapAlloc( GetProcessHeap(), 0, sizeof(*penPtr) ))) return 0;
91
92     if (pen->lopnStyle == PS_USERSTYLE || pen->lopnStyle == PS_ALTERNATE)
93         penPtr->logpen.elpPenStyle = PS_SOLID;
94     else
95         penPtr->logpen.elpPenStyle = pen->lopnStyle;
96     if (pen->lopnStyle == PS_NULL)
97     {
98         penPtr->logpen.elpWidth = 1;
99         penPtr->logpen.elpColor = RGB(0, 0, 0);
100     }
101     else
102     {
103         penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
104         penPtr->logpen.elpColor = pen->lopnColor;
105     }
106     penPtr->logpen.elpBrushStyle = BS_SOLID;
107     penPtr->logpen.elpHatch = 0;
108     penPtr->logpen.elpNumEntries = 0;
109     penPtr->logpen.elpStyleEntry[0] = 0;
110
111     if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_PEN, &pen_funcs )))
112         HeapFree( GetProcessHeap(), 0, penPtr );
113     return hpen;
114 }
115
116 /***********************************************************************
117  *           ExtCreatePen    (GDI32.@)
118  */
119
120 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
121                               const LOGBRUSH * brush, DWORD style_count,
122                               const DWORD *style_bits )
123 {
124     PENOBJ * penPtr;
125     HPEN hpen;
126
127     if ((style & PS_STYLE_MASK) == PS_USERSTYLE)
128     {
129         if(((INT)style_count) <= 0)
130             return 0;
131
132         if ((style_count > 16) || !style_bits)
133         {
134             SetLastError(ERROR_INVALID_PARAMETER);
135             return 0;
136         }
137
138         if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
139         {
140             UINT i;
141             BOOL has_neg = FALSE, all_zero = TRUE;
142
143             for(i = 0; (i < style_count) && !has_neg; i++)
144             {
145                 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
146                 all_zero = all_zero && (style_bits[i] == 0);
147             }
148
149             if(all_zero || has_neg)
150             {
151                 SetLastError(ERROR_INVALID_PARAMETER);
152                 return 0;
153             }
154         }
155     }
156     else
157     {
158         if (style_count || style_bits)
159         {
160             SetLastError(ERROR_INVALID_PARAMETER);
161             return 0;
162         }
163     }
164
165     if ((style & PS_STYLE_MASK) == PS_NULL)
166         return CreatePen( PS_NULL, 0, brush->lbColor );
167
168     if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
169     {
170         /* PS_ALTERNATE is applicable only for cosmetic pens */
171         if ((style & PS_STYLE_MASK) == PS_ALTERNATE)
172         {
173             SetLastError(ERROR_INVALID_PARAMETER);
174             return 0;
175         }
176
177         if (brush->lbHatch && ((brush->lbStyle != BS_SOLID) && (brush->lbStyle != BS_HOLLOW)))
178         {
179             static int fixme_hatches_shown;
180             if (!fixme_hatches_shown++) FIXME("Hatches not implemented\n");
181         }
182     }
183     else
184     {
185         /* PS_INSIDEFRAME is applicable only for geometric pens */
186         if ((style & PS_STYLE_MASK) == PS_INSIDEFRAME || width != 1)
187         {
188             SetLastError(ERROR_INVALID_PARAMETER);
189             return 0;
190         }
191     }
192
193     if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
194         return 0;
195
196     penPtr->logpen.elpPenStyle = style;
197     penPtr->logpen.elpWidth = abs(width);
198     penPtr->logpen.elpBrushStyle = brush->lbStyle;
199     penPtr->logpen.elpColor = brush->lbColor;
200     penPtr->logpen.elpHatch = brush->lbHatch;
201     penPtr->logpen.elpNumEntries = style_count;
202     memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
203
204     if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_EXTPEN, &pen_funcs )))
205         HeapFree( GetProcessHeap(), 0, penPtr );
206     return hpen;
207 }
208
209 /***********************************************************************
210  *           PEN_SelectObject
211  */
212 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
213 {
214     PHYSDEV physdev;
215     HGDIOBJ ret = 0;
216     DC *dc = get_dc_ptr( hdc );
217
218     if (!dc)
219     {
220         SetLastError( ERROR_INVALID_HANDLE );
221         return 0;
222     }
223
224     if (!GDI_inc_ref_count( handle ))
225     {
226         release_dc_ptr( dc );
227         return 0;
228     }
229
230     physdev = GET_DC_PHYSDEV( dc, pSelectPen );
231     if (!physdev->funcs->pSelectPen( physdev, handle ))
232     {
233         GDI_dec_ref_count( handle );
234     }
235     else
236     {
237         ret = dc->hPen;
238         dc->hPen = handle;
239         GDI_dec_ref_count( ret );
240     }
241     release_dc_ptr( dc );
242     return ret;
243 }
244
245
246 /***********************************************************************
247  *           PEN_DeleteObject
248  */
249 static BOOL PEN_DeleteObject( HGDIOBJ handle )
250 {
251     PENOBJ *pen = free_gdi_handle( handle );
252
253     if (!pen) return FALSE;
254     return HeapFree( GetProcessHeap(), 0, pen );
255 }
256
257
258 /***********************************************************************
259  *           PEN_GetObject
260  */
261 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
262 {
263     PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
264     INT ret = 0;
265
266     if (!pen) return 0;
267
268     switch (pen->header.type)
269     {
270     case OBJ_PEN:
271     {
272         LOGPEN *lp;
273
274         if (!buffer) ret = sizeof(LOGPEN);
275         else if (count < sizeof(LOGPEN)) ret = 0;
276         else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
277         {
278             EXTLOGPEN *elp = buffer;
279             *elp = pen->logpen;
280             elp->elpWidth = 0;
281             ret = sizeof(EXTLOGPEN);
282         }
283         else
284         {
285             lp = buffer;
286             lp->lopnStyle = pen->logpen.elpPenStyle;
287             lp->lopnColor = pen->logpen.elpColor;
288             lp->lopnWidth.x = pen->logpen.elpWidth;
289             lp->lopnWidth.y = 0;
290             ret = sizeof(LOGPEN);
291         }
292         break;
293     }
294
295     case OBJ_EXTPEN:
296         ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
297         if (buffer)
298         {
299             if (count < ret) ret = 0;
300             else memcpy(buffer, &pen->logpen, ret);
301         }
302         break;
303     }
304     GDI_ReleaseObj( handle );
305     return ret;
306 }