windowscodecs: Add stub JPEG encoder.
[wine] / dlls / shell32 / debughlp.c
1 /*
2  * Helper functions for debugging
3  *
4  * Copyright 1998, 2002 Juergen Schmied
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 <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "pidl.h"
28 #include "shldisp.h"
29 #include "wine/debug.h"
30 #include "debughlp.h"
31 #include "docobj.h"
32 #include "shell32_main.h"
33
34
35 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
36
37 static
38 LPCITEMIDLIST _dbg_ILGetNext(LPCITEMIDLIST pidl)
39 {
40         WORD len;
41
42         if(pidl)
43         {
44           len =  pidl->mkid.cb;
45           if (len)
46           {
47             return (LPCITEMIDLIST) (((const BYTE*)pidl)+len);
48           }
49         }
50         return NULL;
51 }
52
53 static
54 BOOL _dbg_ILIsDesktop(LPCITEMIDLIST pidl)
55 {
56         return ( !pidl || (pidl && pidl->mkid.cb == 0x00) );
57 }
58
59 static
60 const PIDLDATA* _dbg_ILGetDataPointer(LPCITEMIDLIST pidl)
61 {
62         if(pidl && pidl->mkid.cb != 0x00)
63           return (const PIDLDATA*)pidl->mkid.abID;
64         return NULL;
65 }
66
67 static
68 LPCSTR _dbg_ILGetTextPointer(LPCITEMIDLIST pidl)
69 {
70         const PIDLDATA* pdata =_dbg_ILGetDataPointer(pidl);
71
72         if (pdata)
73         {
74           switch (pdata->type)
75           {
76             case PT_GUID:
77             case PT_SHELLEXT:
78             case PT_YAGUID:
79               return NULL;
80
81             case PT_DRIVE:
82             case PT_DRIVE1:
83             case PT_DRIVE2:
84             case PT_DRIVE3:
85               return pdata->u.drive.szDriveName;
86
87             case PT_FOLDER:
88             case PT_FOLDER1:
89             case PT_VALUE:
90             case PT_IESPECIAL1:
91             case PT_IESPECIAL2:
92               return pdata->u.file.szNames;
93
94             case PT_WORKGRP:
95             case PT_COMP:
96             case PT_NETWORK:
97             case PT_NETPROVIDER:
98             case PT_SHARE:
99               return pdata->u.network.szNames;
100           }
101         }
102         return NULL;
103 }
104
105 static
106 LPCWSTR _dbg_ILGetTextPointerW(LPCITEMIDLIST pidl)
107 {
108         const PIDLDATA* pdata =_dbg_ILGetDataPointer(pidl);
109
110         if (pdata)
111         {
112           switch (pdata->type)
113           {
114             case PT_GUID:
115             case PT_SHELLEXT:
116             case PT_YAGUID:
117               return NULL;
118
119             case PT_DRIVE:
120             case PT_DRIVE1:
121             case PT_DRIVE2:
122             case PT_DRIVE3:
123               /* return (LPSTR)&(pdata->u.drive.szDriveName);*/
124               return NULL;
125
126             case PT_FOLDER:
127             case PT_FOLDER1:
128             case PT_VALUE:
129             case PT_IESPECIAL1:
130             case PT_IESPECIAL2:
131               /* return (LPSTR)&(pdata->u.file.szNames); */
132               return NULL;
133
134             case PT_WORKGRP:
135             case PT_COMP:
136             case PT_NETWORK:
137             case PT_NETPROVIDER:
138             case PT_SHARE:
139               /* return (LPSTR)&(pdata->u.network.szNames); */
140               return NULL;
141
142             case PT_VALUEW:
143               return (LPCWSTR)pdata->u.file.szNames;
144           }
145         }
146         return NULL;
147 }
148
149
150 static
151 LPCSTR _dbg_ILGetSTextPointer(LPCITEMIDLIST pidl)
152 {
153         const PIDLDATA* pdata =_dbg_ILGetDataPointer(pidl);
154
155         if (pdata)
156         {
157           switch (pdata->type)
158           {
159             case PT_FOLDER:
160             case PT_VALUE:
161             case PT_IESPECIAL1:
162             case PT_IESPECIAL2:
163               return pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1;
164
165             case PT_WORKGRP:
166               return pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1;
167           }
168         }
169         return NULL;
170 }
171
172 static
173 LPCWSTR _dbg_ILGetSTextPointerW(LPCITEMIDLIST pidl)
174 {
175         const PIDLDATA* pdata =_dbg_ILGetDataPointer(pidl);
176
177         if (pdata)
178         {
179           switch (pdata->type)
180           {
181             case PT_FOLDER:
182             case PT_VALUE:
183             case PT_IESPECIAL1:
184             case PT_IESPECIAL2:
185               /*return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1); */
186               return NULL;
187
188             case PT_WORKGRP:
189               /* return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1); */
190               return NULL;
191
192             case PT_VALUEW:
193               return (LPCWSTR)(pdata->u.file.szNames + lstrlenW ((LPCWSTR)pdata->u.file.szNames) + 1);
194           }
195         }
196         return NULL;
197 }
198
199
200 static
201 IID* _dbg_ILGetGUIDPointer(LPCITEMIDLIST pidl)
202 {
203         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
204
205         if (pdata)
206         {
207           switch (pdata->type)
208           {
209             case PT_SHELLEXT:
210             case PT_GUID:
211             case PT_YAGUID:
212               return &(pdata->u.guid.guid);
213           }
214         }
215         return NULL;
216 }
217
218 static
219 void _dbg_ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
220 {
221         LPCSTR          szSrc;
222         LPCWSTR         szSrcW;
223         GUID const *    riid;
224
225         if (!pidl) return;
226
227         if (szOut)
228           *szOut = 0;
229
230         if (_dbg_ILIsDesktop(pidl))
231         {
232          /* desktop */
233           if (szOut) lstrcpynA(szOut, "Desktop", uOutSize);
234         }
235         else if (( szSrc = _dbg_ILGetTextPointer(pidl) ))
236         {
237           /* filesystem */
238           if (szOut) lstrcpynA(szOut, szSrc, uOutSize);
239         }
240         else if (( szSrcW = _dbg_ILGetTextPointerW(pidl) ))
241         {
242           CHAR tmp[MAX_PATH];
243           /* unicode filesystem */
244           WideCharToMultiByte(CP_ACP,0,szSrcW, -1, tmp, MAX_PATH, NULL, NULL);
245           if (szOut) lstrcpynA(szOut, tmp, uOutSize);
246         }
247         else if (( riid = _dbg_ILGetGUIDPointer(pidl) ))
248         {
249           if (szOut)
250             sprintf( szOut, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
251                  riid->Data1, riid->Data2, riid->Data3,
252                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
253                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
254         }
255 }
256
257
258
259
260 void pdump (LPCITEMIDLIST pidl)
261 {
262         LPCITEMIDLIST pidltemp = pidl;
263
264         if (!TRACE_ON(pidl)) return;
265
266         if (! pidltemp)
267         {
268           MESSAGE ("-------- pidl=NULL (Desktop)\n");
269         }
270         else
271         {
272           MESSAGE ("-------- pidl=%p\n", pidl);
273           if (pidltemp->mkid.cb)
274           {
275             do
276             {
277           if (_ILIsUnicode(pidltemp))
278           {
279               DWORD dwAttrib = 0;
280               const PIDLDATA* pData   = _dbg_ILGetDataPointer(pidltemp);
281               DWORD type = pData ? pData->type : 0;
282               LPCWSTR szLongName   = _dbg_ILGetTextPointerW(pidltemp);
283               LPCWSTR szShortName  = _dbg_ILGetSTextPointerW(pidltemp);
284               char szName[MAX_PATH];
285
286               _dbg_ILSimpleGetText(pidltemp, szName, MAX_PATH);
287               if ( pData && (PT_FOLDER == type || PT_VALUE == type) )
288                 dwAttrib = pData->u.file.uFileAttribs;
289
290               MESSAGE ("[%p] size=%04u type=%x attr=0x%08x name=%s (%s,%s)\n",
291                        pidltemp, pidltemp->mkid.cb, type, dwAttrib,
292                            debugstr_a(szName), debugstr_w(szLongName), debugstr_w(szShortName));
293           }
294           else
295           {
296               DWORD dwAttrib = 0;
297               const PIDLDATA* pData   = _dbg_ILGetDataPointer(pidltemp);
298               DWORD type = pData ? pData->type : 0;
299               LPCSTR szLongName   = _dbg_ILGetTextPointer(pidltemp);
300               LPCSTR szShortName  = _dbg_ILGetSTextPointer(pidltemp);
301               char szName[MAX_PATH];
302
303               _dbg_ILSimpleGetText(pidltemp, szName, MAX_PATH);
304               if ( pData && (PT_FOLDER == type || PT_VALUE == type) )
305                 dwAttrib = pData->u.file.uFileAttribs;
306
307               MESSAGE ("[%p] size=%04u type=%x attr=0x%08x name=%s (%s,%s)\n",
308                        pidltemp, pidltemp->mkid.cb, type, dwAttrib,
309                            debugstr_a(szName), debugstr_a(szLongName), debugstr_a(szShortName));
310           }
311
312               pidltemp = _dbg_ILGetNext(pidltemp);
313
314             } while (pidltemp && pidltemp->mkid.cb);
315           }
316           else
317           {
318             MESSAGE ("empty pidl (Desktop)\n");
319           }
320           pcheck(pidl);
321         }
322 }
323
324 static void dump_pidl_hex( LPCITEMIDLIST pidl )
325 {
326     const unsigned char *p = (const unsigned char *)pidl;
327     const int max_bytes = 0x80;
328 #define max_line 0x10
329     char szHex[max_line*3+1], szAscii[max_line+1];
330     int i, n;
331
332     n = pidl->mkid.cb;
333     if( n>max_bytes )
334         n = max_bytes;
335     for( i=0; i<n; i++ )
336     {
337         sprintf( &szHex[ (i%max_line)*3 ], "%02X ", p[i] );
338         szAscii[ (i%max_line) ] = isprint( p[i] ) ? p[i] : '.';
339
340         /* print out at the end of each line and when we're finished */
341         if( i!=(n-1) && (i%max_line) != (max_line-1) )
342             continue;
343         szAscii[ (i%max_line)+1 ] = 0;
344         ERR("%-*s   %s\n", max_line*3, szHex, szAscii );
345     }
346 }
347
348 BOOL pcheck( LPCITEMIDLIST pidl )
349 {
350     DWORD type;
351     LPCITEMIDLIST pidltemp = pidl;
352
353     while( pidltemp && pidltemp->mkid.cb )
354     {
355         type = _dbg_ILGetDataPointer(pidltemp)->type;
356         switch( type )
357         {
358         case PT_CPLAPPLET:
359         case PT_GUID:
360         case PT_SHELLEXT:
361         case PT_DRIVE:
362         case PT_DRIVE1:
363         case PT_DRIVE2:
364         case PT_DRIVE3:
365         case PT_FOLDER:
366         case PT_VALUE:
367         case PT_VALUEW:
368         case PT_FOLDER1:
369         case PT_WORKGRP:
370         case PT_COMP:
371         case PT_NETPROVIDER:
372         case PT_NETWORK:
373         case PT_IESPECIAL1:
374         case PT_YAGUID:
375         case PT_IESPECIAL2:
376         case PT_SHARE:
377             break;
378         default:
379             ERR("unknown IDLIST %p [%p] size=%u type=%x\n",
380                 pidl, pidltemp, pidltemp->mkid.cb,type );
381             dump_pidl_hex( pidltemp );
382             return FALSE;
383         }
384         pidltemp = _dbg_ILGetNext(pidltemp);
385     }
386     return TRUE;
387 }
388
389 static const struct {
390         REFIID riid;
391         const char *name;
392 } InterfaceDesc[] = {
393         {&IID_IUnknown,                 "IID_IUnknown"},
394         {&IID_IClassFactory,            "IID_IClassFactory"},
395         {&IID_IShellView,               "IID_IShellView"},
396         {&IID_IOleCommandTarget,        "IID_IOleCommandTarget"},
397         {&IID_IDropTarget,              "IID_IDropTarget"},
398         {&IID_IDropSource,              "IID_IDropSource"},
399         {&IID_IViewObject,              "IID_IViewObject"},
400         {&IID_IContextMenu,             "IID_IContextMenu"},
401         {&IID_IShellExtInit,            "IID_IShellExtInit"},
402         {&IID_IShellFolder,             "IID_IShellFolder"},
403         {&IID_IShellFolder2,            "IID_IShellFolder2"},
404         {&IID_IPersist,                 "IID_IPersist"},
405         {&IID_IPersistFolder,           "IID_IPersistFolder"},
406         {&IID_IPersistFolder2,          "IID_IPersistFolder2"},
407         {&IID_IPersistFolder3,          "IID_IPersistFolder3"},
408         {&IID_IExtractIconA,            "IID_IExtractIconA"},
409         {&IID_IExtractIconW,            "IID_IExtractIconW"},
410         {&IID_IDataObject,              "IID_IDataObject"},
411         {&IID_IAutoComplete,            "IID_IAutoComplete"},
412         {&IID_IAutoComplete2,           "IID_IAutoComplete2"},
413         {&IID_IShellLinkA,              "IID_IShellLinkA"},
414         {&IID_IShellLinkW,              "IID_IShellLinkW"},
415         {NULL,NULL}};
416
417 const char * shdebugstr_guid( const struct _GUID *id )
418 {
419         int i;
420         const char* name = NULL;
421         char clsidbuf[100];
422
423         if (!id) return "(null)";
424
425         for (i = 0; InterfaceDesc[i].riid && !name; i++) {
426             if (IsEqualIID(InterfaceDesc[i].riid, id)) name = InterfaceDesc[i].name;
427         }
428         if (!name) {
429             if (HCR_GetClassNameA(id, clsidbuf, 100))
430                 name = clsidbuf;
431         }
432
433         return wine_dbg_sprintf( "%s (%s)", debugstr_guid(id), name ? name : "unknown" );
434 }