InternalExtractIcon16 forgot to close file handle.
[wine] / misc / error.c
1 /*
2  * Log internal errors 
3  *
4  * Copyright 1997 Andrew Taylor
5  */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "winbase.h"
12 #include "windef.h"
13 #include "stackframe.h"
14 #include "debugtools.h"
15
16
17 /* LogParamError and LogError values */
18
19 /* Error modifier bits */
20 #define ERR_WARNING             0x8000
21 #define ERR_PARAM               0x4000
22
23 #define ERR_SIZE_MASK           0x3000
24 #define ERR_BYTE                0x1000
25 #define ERR_WORD                0x2000
26 #define ERR_DWORD               0x3000
27
28
29 /* LogParamError() values */
30
31 /* Generic parameter values */
32 #define ERR_BAD_VALUE           0x6001
33 #define ERR_BAD_FLAGS           0x6002
34 #define ERR_BAD_INDEX           0x6003
35 #define ERR_BAD_DVALUE          0x7004
36 #define ERR_BAD_DFLAGS          0x7005
37 #define ERR_BAD_DINDEX          0x7006
38 #define ERR_BAD_PTR             0x7007
39 #define ERR_BAD_FUNC_PTR        0x7008
40 #define ERR_BAD_SELECTOR        0x6009
41 #define ERR_BAD_STRING_PTR      0x700a
42 #define ERR_BAD_HANDLE          0x600b
43
44 /* KERNEL parameter errors */
45 #define ERR_BAD_HINSTANCE       0x6020
46 #define ERR_BAD_HMODULE         0x6021
47 #define ERR_BAD_GLOBAL_HANDLE   0x6022
48 #define ERR_BAD_LOCAL_HANDLE    0x6023
49 #define ERR_BAD_ATOM            0x6024
50 #define ERR_BAD_HFILE           0x6025
51
52 /* USER parameter errors */
53 #define ERR_BAD_HWND            0x6040
54 #define ERR_BAD_HMENU           0x6041
55 #define ERR_BAD_HCURSOR         0x6042
56 #define ERR_BAD_HICON           0x6043
57 #define ERR_BAD_HDWP            0x6044
58 #define ERR_BAD_CID             0x6045
59 #define ERR_BAD_HDRVR           0x6046
60
61 /* GDI parameter errors */
62 #define ERR_BAD_COORDS          0x7060
63 #define ERR_BAD_GDI_OBJECT      0x6061
64 #define ERR_BAD_HDC             0x6062
65 #define ERR_BAD_HPEN            0x6063
66 #define ERR_BAD_HFONT           0x6064
67 #define ERR_BAD_HBRUSH          0x6065
68 #define ERR_BAD_HBITMAP         0x6066
69 #define ERR_BAD_HRGN            0x6067
70 #define ERR_BAD_HPALETTE        0x6068
71 #define ERR_BAD_HMETAFILE       0x6069
72
73
74 /* LogError() values */
75
76 /* KERNEL errors */
77 #define ERR_GALLOC              0x0001
78 #define ERR_GREALLOC            0x0002
79 #define ERR_GLOCK               0x0003
80 #define ERR_LALLOC              0x0004
81 #define ERR_LREALLOC            0x0005
82 #define ERR_LLOCK               0x0006
83 #define ERR_ALLOCRES            0x0007
84 #define ERR_LOCKRES             0x0008
85 #define ERR_LOADMODULE          0x0009
86
87 /* USER errors */
88 #define ERR_CREATEDLG           0x0040
89 #define ERR_CREATEDLG2          0x0041
90 #define ERR_REGISTERCLASS       0x0042
91 #define ERR_DCBUSY              0x0043
92 #define ERR_CREATEWND           0x0044
93 #define ERR_STRUCEXTRA          0x0045
94 #define ERR_LOADSTR             0x0046
95 #define ERR_LOADMENU            0x0047
96 #define ERR_NESTEDBEGINPAINT    0x0048
97 #define ERR_BADINDEX            0x0049
98 #define ERR_CREATEMENU          0x004a
99
100 /* GDI errors */
101 #define ERR_CREATEDC            0x0080
102 #define ERR_CREATEMETA          0x0081
103 #define ERR_DELOBJSELECTED      0x0082
104 #define ERR_SELBITMAP           0x0083
105
106
107 #define ErrorString(manifest) { manifest, # manifest }
108
109 static const struct {
110         int constant;
111         const char *name;
112 } ErrorStrings[] = {
113
114         ErrorString(ERR_GALLOC),
115         ErrorString(ERR_GREALLOC),
116         ErrorString(ERR_GLOCK),
117         ErrorString(ERR_LALLOC),
118         ErrorString(ERR_LREALLOC),
119         ErrorString(ERR_LLOCK),
120         ErrorString(ERR_ALLOCRES),
121         ErrorString(ERR_LOCKRES),
122         ErrorString(ERR_LOADMODULE),
123         ErrorString(ERR_CREATEDLG),
124         ErrorString(ERR_CREATEDLG2),
125         ErrorString(ERR_REGISTERCLASS),
126         ErrorString(ERR_DCBUSY),
127         ErrorString(ERR_CREATEWND),
128         ErrorString(ERR_STRUCEXTRA),
129         ErrorString(ERR_LOADSTR),
130         ErrorString(ERR_LOADMENU),
131         ErrorString(ERR_NESTEDBEGINPAINT),
132         ErrorString(ERR_BADINDEX),
133         ErrorString(ERR_CREATEMENU),
134         ErrorString(ERR_CREATEDC),
135         ErrorString(ERR_CREATEMETA),
136         ErrorString(ERR_DELOBJSELECTED),
137         ErrorString(ERR_SELBITMAP)
138 };
139
140 static const struct {
141         int constant;
142         const char *name;
143 } ParamErrorStrings[] = {
144
145         ErrorString(ERR_BAD_VALUE),
146         ErrorString(ERR_BAD_FLAGS),
147         ErrorString(ERR_BAD_INDEX),
148         ErrorString(ERR_BAD_DVALUE),
149         ErrorString(ERR_BAD_DFLAGS),
150         ErrorString(ERR_BAD_DINDEX),
151         ErrorString(ERR_BAD_PTR),
152         ErrorString(ERR_BAD_FUNC_PTR),
153         ErrorString(ERR_BAD_SELECTOR),
154         ErrorString(ERR_BAD_STRING_PTR),
155         ErrorString(ERR_BAD_HANDLE),
156         ErrorString(ERR_BAD_HINSTANCE),
157         ErrorString(ERR_BAD_HMODULE),
158         ErrorString(ERR_BAD_GLOBAL_HANDLE),
159         ErrorString(ERR_BAD_LOCAL_HANDLE),
160         ErrorString(ERR_BAD_ATOM),
161         ErrorString(ERR_BAD_HFILE),
162         ErrorString(ERR_BAD_HWND),
163         ErrorString(ERR_BAD_HMENU),
164         ErrorString(ERR_BAD_HCURSOR),
165         ErrorString(ERR_BAD_HICON),
166         ErrorString(ERR_BAD_HDWP),
167         ErrorString(ERR_BAD_CID),
168         ErrorString(ERR_BAD_HDRVR),
169         ErrorString(ERR_BAD_COORDS),
170         ErrorString(ERR_BAD_GDI_OBJECT),
171         ErrorString(ERR_BAD_HDC),
172         ErrorString(ERR_BAD_HPEN),
173         ErrorString(ERR_BAD_HFONT),
174         ErrorString(ERR_BAD_HBRUSH),
175         ErrorString(ERR_BAD_HBITMAP),
176         ErrorString(ERR_BAD_HRGN),
177         ErrorString(ERR_BAD_HPALETTE),
178         ErrorString(ERR_BAD_HMETAFILE)
179 };
180
181 #undef  ErrorString
182 #define ErrorStringCount (sizeof(ErrorStrings) / sizeof(ErrorStrings[0]))
183 #define ParamErrorStringCount (sizeof(ParamErrorStrings) / sizeof(ParamErrorStrings[0]))
184
185 /***********************************************************************
186 *       GetErrorString (internal)
187 */
188 static const char *GetErrorString(UINT16 uErr) 
189 {
190   static char buffer[80];
191   unsigned int n;
192
193   for (n = 0; n < ErrorStringCount; n++) {
194     if (uErr == ErrorStrings[n].constant)
195       return ErrorStrings[n].name;
196   }
197
198   sprintf(buffer, "%x", uErr);
199   return buffer;
200 }
201
202
203 /***********************************************************************
204 *       GetParamErrorString (internal)
205 */
206 static const char *GetParamErrorString(UINT16 uErr) {
207         static char buffer[80];
208
209         if (uErr & ERR_WARNING) {
210                 strcpy(buffer, "ERR_WARNING | ");
211                 uErr &= ~ERR_WARNING;
212         } else
213                 buffer[0] = '\0';
214
215         {
216                 unsigned int n;
217
218                 for (n = 0; n < ParamErrorStringCount; n++) {
219                         if (uErr == ParamErrorStrings[n].constant) {
220                                 strcat(buffer, ParamErrorStrings[n].name);
221                                 return buffer;
222                         }
223                 }
224         }
225
226         sprintf(buffer + strlen(buffer), "%x", uErr);
227         return buffer;
228 }
229
230
231 /***********************************************************************
232 *       LogError (KERNEL.324)
233 */
234 VOID WINAPI LogError16(UINT16 uErr, LPVOID lpvInfo)
235 {
236         MESSAGE("(%s, %p)\n", GetErrorString(uErr), lpvInfo);
237 }
238
239
240 /***********************************************************************
241 *       LogParamError (KERNEL.325)
242 */
243 void WINAPI LogParamError16(UINT16 uErr, FARPROC16 lpfn, LPVOID lpvParam)
244 {
245         /* FIXME: is it possible to get the module name/function
246          * from the lpfn param?
247          */
248         MESSAGE("(%s, %p, %p)\n", GetParamErrorString(uErr), lpfn, lpvParam);
249 }
250
251 /***********************************************************************
252 *       K327 (KERNEL.327)
253 */
254 void WINAPI HandleParamError( CONTEXT86 *context )
255 {
256         UINT16 uErr = LOWORD(context->Ebx);
257         FARPROC16 lpfn = (FARPROC16)MAKESEGPTR( context->SegCs, context->Eip );
258         LPVOID lpvParam = (LPVOID)MAKELONG( LOWORD(context->Eax), LOWORD(context->Ecx) );
259         
260         LogParamError16( uErr, lpfn, lpvParam );
261
262         if (!(uErr & ERR_WARNING))
263         {
264                 /* Abort current procedure: Unwind stack frame and jump
265                    to error handler (location at [bp-2]) */
266
267                 WORD *stack = MapSL( MAKESEGPTR( context->SegSs, LOWORD(context->Ebp) ));
268                 context->Esp = LOWORD(context->Ebp) - 2;
269                 context->Ebp = stack[0] & 0xfffe;
270
271                 context->Eip = stack[-1];
272
273                 context->Eax = context->Ecx = context->Edx = 0;
274                 context->SegEs = 0;
275         }
276 }
277