1 /*
2  * Copyright 2010 Damjan Jovanovic
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "precomp.h"
20 
21 static unsigned char testico_bad_icondirentry_size[] = {
22     /* ICONDIR */
23     0, 0, /* reserved */
24     1, 0, /* type */
25     1, 0, /* count */
26     /* ICONDIRENTRY */
27     2, /* width */
28     2, /* height */
29     2, /* colorCount */
30     0, /* reserved */
31     1,0, /* planes */
32     8,0, /* bitCount */
33     (40+2*4+16*16+16*4) & 0xFF,((40+2*4+16*16+16*4) >> 8) & 0xFF,0,0, /* bytesInRes */
34     22,0,0,0, /* imageOffset */
35     /* BITMAPINFOHEADER */
36     40,0,0,0, /* header size */
37     16,0,0,0, /* width */
38     2*16,0,0,0, /* height (XOR+AND rows) */
39     1,0, /* planes */
40     8,0, /* bit count */
41     0,0,0,0, /* compression */
42     0,0,0,0, /* sizeImage */
43     0,0,0,0, /* x pels per meter */
44     0,0,0,0, /* y pels per meter */
45     2,0,0,0, /* clrUsed */
46     0,0,0,0, /* clrImportant */
47     /* palette */
48     0,0,0,0,
49     0xFF,0xFF,0xFF,0,
50     /* XOR mask */
51     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
53     0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
54     0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
55     0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
56     0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,
57     0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,
58     0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,
59     0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,
60     0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,
61     0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,
62     0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,
63     0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,
64     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
65     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
66     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
67     /* AND mask */
68     0,0,0,0,
69     0,0,0,0,
70     0,0,0,0,
71     0,0,0,0,
72     0,0,0,0,
73     0,0,0,0,
74     0,0,0,0,
75     0,0,0,0,
76     0,0,0,0,
77     0,0,0,0,
78     0,0,0,0,
79     0,0,0,0,
80     0,0,0,0,
81     0,0,0,0,
82     0,0,0,0,
83     0,0,0,0
84 };
85 
86 static void test_bad_icondirentry_size(void)
87 {
88     IWICBitmapDecoder *decoder;
89     IWICImagingFactory *factory;
90     HRESULT hr;
91     IWICStream *icostream;
92     IWICBitmapFrameDecode *framedecode = NULL;
93 
94     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
95         &IID_IWICImagingFactory, (void**)&factory);
96     ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
97     if (FAILED(hr)) return;
98 
99     hr = IWICImagingFactory_CreateStream(factory, &icostream);
100     ok(hr == S_OK, "CreateStream failed, hr=%x\n", hr);
101     if (SUCCEEDED(hr))
102     {
103         hr = IWICStream_InitializeFromMemory(icostream, testico_bad_icondirentry_size,
104             sizeof(testico_bad_icondirentry_size));
105         ok(hr == S_OK, "InitializeFromMemory failed, hr=%x\n", hr);
106 
107         if (SUCCEEDED(hr))
108         {
109             hr = CoCreateInstance(&CLSID_WICIcoDecoder, NULL, CLSCTX_INPROC_SERVER,
110                 &IID_IWICBitmapDecoder, (void**)&decoder);
111             ok(hr == S_OK, "CoCreateInstance failed, hr=%x\n", hr);
112         }
113 
114         if (SUCCEEDED(hr))
115         {
116             hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)icostream,
117                 WICDecodeMetadataCacheOnDemand);
118             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
119 
120             if (SUCCEEDED(hr))
121             {
122                 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
123                 ok(hr == S_OK, "GetFrame failed, hr=%x\n", hr);
124             }
125 
126             if (SUCCEEDED(hr))
127             {
128                 UINT width, height;
129                 IWICBitmapSource *thumbnail;
130 
131                 width = height = 0;
132                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
133                 ok(hr == S_OK, "GetFrameSize failed, hr=%x\n", hr);
134                 ok(width == 16 && height == 16, "framesize=%ux%u\n", width, height);
135 
136                 hr = IWICBitmapFrameDecode_GetThumbnail(framedecode, &thumbnail);
137                 ok(hr == S_OK, "GetThumbnail failed, hr=%x\n", hr);
138                 if (hr == S_OK)
139                 {
140                     width = height = 0;
141                     hr = IWICBitmapSource_GetSize(thumbnail, &width, &height);
142                     ok(hr == S_OK, "GetFrameSize failed, hr=%x\n", hr);
143                     ok(width == 16 && height == 16, "framesize=%ux%u\n", width, height);
144                     IWICBitmapSource_Release(thumbnail);
145                 }
146                 IWICBitmapFrameDecode_Release(framedecode);
147             }
148 
149             IWICBitmapDecoder_Release(decoder);
150         }
151 
152         IWICStream_Release(icostream);
153     }
154 
155     IWICImagingFactory_Release(factory);
156 }
157 
158 START_TEST(icoformat)
159 {
160     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
161 
162     test_bad_icondirentry_size();
163 
164     CoUninitialize();
165 }
166