riched20: Fix test crash on WinXP-SP2.
[wine] / dlls / msi / streams.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2007 James Hawkins
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 <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "objbase.h"
31 #include "msipriv.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
36
37 #define NUM_STREAMS_COLS    2
38 #define MAX_STREAM_NAME_LEN 62
39
40 typedef struct tabSTREAM
41 {
42     int str_index;
43     LPWSTR name;
44     IStream *stream;
45 } STREAM;
46
47 typedef struct tagMSISTREAMSVIEW
48 {
49     MSIVIEW view;
50     MSIDATABASE *db;
51     STREAM **streams;
52     UINT max_streams;
53     UINT num_rows;
54     UINT row_size;
55 } MSISTREAMSVIEW;
56
57 static BOOL add_stream_to_table(MSISTREAMSVIEW *sv, STREAM *stream, int index)
58 {
59     if (index >= sv->max_streams)
60     {
61         sv->max_streams *= 2;
62         sv->streams = msi_realloc(sv->streams, sv->max_streams * sizeof(STREAM *));
63         if (!sv->streams)
64             return FALSE;
65     }
66
67     sv->streams[index] = stream;
68     return TRUE;
69 }
70
71 static STREAM *create_stream(MSISTREAMSVIEW *sv, LPWSTR name, BOOL encoded, IStream *stm)
72 {
73     STREAM *stream;
74     WCHAR decoded[MAX_STREAM_NAME_LEN];
75     LPWSTR ptr = name;
76
77     stream = msi_alloc(sizeof(STREAM));
78     if (!stream)
79         return NULL;
80
81     if (encoded)
82     {
83         decode_streamname(name, decoded);
84         ptr = decoded;
85         TRACE("stream -> %s %s\n", debugstr_w(name), debugstr_w(decoded));
86     }
87
88     stream->name = strdupW(ptr);
89     if (!stream->name)
90     {
91         msi_free(stream);
92         return NULL;
93     }
94
95     stream->str_index = msi_addstringW(sv->db->strings, 0, stream->name, -1, 1, StringNonPersistent);
96     stream->stream = stm;
97     return stream;
98 }
99
100 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
101 {
102     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
103
104     TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
105
106     if (col != 1)
107         return ERROR_INVALID_PARAMETER;
108
109     if (row >= sv->num_rows)
110         return ERROR_NO_MORE_ITEMS;
111
112     *val = sv->streams[row]->str_index;
113
114     return ERROR_SUCCESS;
115 }
116
117 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
118 {
119     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
120
121     TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
122
123     if (row >= sv->num_rows)
124         return ERROR_FUNCTION_FAILED;
125
126     IStream_AddRef(sv->streams[row]->stream);
127     *stm = sv->streams[row]->stream;
128
129     return ERROR_SUCCESS;
130 }
131
132 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
133 {
134     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
135
136     FIXME("%p %d %p\n", sv, row, rec);
137
138     return ERROR_CALL_NOT_IMPLEMENTED;
139 }
140
141 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
142 {
143     FIXME("(%p, %d, %p, %d): stub!\n", view, row, rec, mask);
144     return ERROR_SUCCESS;
145 }
146
147 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary)
148 {
149     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
150     STREAM *stream;
151     IStream *stm;
152     STATSTG stat;
153     LPWSTR name = NULL;
154     USHORT *data = NULL;
155     HRESULT hr;
156     ULONG count;
157     UINT r = ERROR_FUNCTION_FAILED;
158
159     TRACE("(%p, %p, %d)\n", view, rec, temporary);
160
161     r = MSI_RecordGetIStream(rec, 2, &stm);
162     if (r != ERROR_SUCCESS)
163         return r;
164
165     hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
166     if (FAILED(hr))
167     {
168         WARN("failed to stat stream: %08x\n", hr);
169         goto done;
170     }
171
172     if (stat.cbSize.QuadPart >> 32)
173         goto done;
174
175     data = msi_alloc(stat.cbSize.QuadPart);
176     if (!data)
177         goto done;
178
179     hr = IStream_Read(stm, data, stat.cbSize.QuadPart, &count);
180     if (FAILED(hr) || count != stat.cbSize.QuadPart)
181     {
182         WARN("failed to read stream: %08x\n", hr);
183         goto done;
184     }
185
186     name = strdupW(MSI_RecordGetString(rec, 1));
187     if (!name)
188         goto done;
189
190     r = write_stream_data(sv->db->storage, name, data, count, FALSE);
191     if (r != ERROR_SUCCESS)
192     {
193         WARN("failed to write stream data: %d\n", r);
194         goto done;
195     }
196
197     stream = create_stream(sv, name, FALSE, NULL);
198     if (!stream)
199         goto done;
200
201     IStorage_OpenStream(sv->db->storage, name, 0,
202                         STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
203
204     if (!add_stream_to_table(sv, stream, sv->num_rows++))
205         goto done;
206
207 done:
208     msi_free(name);
209     msi_free(data);
210
211     IStream_Release(stm);
212
213     return r;
214 }
215
216 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
217 {
218     FIXME("(%p %d): stub!\n", view, row);
219     return ERROR_SUCCESS;
220 }
221
222 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
223 {
224     TRACE("(%p, %p)\n", view, record);
225     return ERROR_SUCCESS;
226 }
227
228 static UINT STREAMS_close(struct tagMSIVIEW *view)
229 {
230     TRACE("(%p)\n", view);
231     return ERROR_SUCCESS;
232 }
233
234 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
235 {
236     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
237
238     TRACE("(%p, %p, %p)\n", view, rows, cols);
239
240     if (cols) *cols = NUM_STREAMS_COLS;
241     if (rows) *rows = sv->num_rows;
242
243     return ERROR_SUCCESS;
244 }
245
246 static UINT STREAMS_get_column_info(struct tagMSIVIEW *view,
247                                     UINT n, LPWSTR *name, UINT *type)
248 {
249     LPCWSTR name_ptr = NULL;
250
251     static const WCHAR Name[] = {'N','a','m','e',0};
252     static const WCHAR Data[] = {'D','a','t','a',0};
253
254     TRACE("(%p, %d, %p, %p)\n", view, n, name, type);
255
256     if (n == 0 || n > NUM_STREAMS_COLS)
257         return ERROR_INVALID_PARAMETER;
258
259     switch (n)
260     {
261     case 1:
262         name_ptr = Name;
263         if (type) *type = MSITYPE_STRING | MAX_STREAM_NAME_LEN;
264         break;
265
266     case 2:
267         name_ptr = Data;
268         if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
269         break;
270     }
271
272     if (name)
273     {
274         *name = strdupW(name_ptr);
275         if (!*name) return ERROR_FUNCTION_FAILED;
276     }
277
278     return ERROR_SUCCESS;
279 }
280
281 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
282 {
283     UINT r;
284
285     TRACE("%p %d %p\n", view, eModifyMode, rec);
286
287     switch (eModifyMode)
288     {
289     case MSIMODIFY_INSERT:
290         r = STREAMS_insert_row(view, rec, FALSE);
291         break;
292
293     case MSIMODIFY_VALIDATE_NEW:
294     case MSIMODIFY_INSERT_TEMPORARY:
295     case MSIMODIFY_UPDATE:
296     case MSIMODIFY_REFRESH:
297     case MSIMODIFY_ASSIGN:
298     case MSIMODIFY_REPLACE:
299     case MSIMODIFY_MERGE:
300     case MSIMODIFY_DELETE:
301     case MSIMODIFY_VALIDATE:
302     case MSIMODIFY_VALIDATE_FIELD:
303     case MSIMODIFY_VALIDATE_DELETE:
304         FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
305         r = ERROR_CALL_NOT_IMPLEMENTED;
306         break;
307
308     default:
309         r = ERROR_INVALID_DATA;
310     }
311
312     return r;
313 }
314
315 static UINT STREAMS_delete(struct tagMSIVIEW *view)
316 {
317     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
318     int i;
319
320     TRACE("(%p)\n", view);
321
322     for (i = 0; i < sv->num_rows; i++)
323     {
324         if (sv->streams[i]->stream)
325             IStream_Release(sv->streams[i]->stream);
326         msi_free(sv->streams[i]);
327     }
328
329     msi_free(sv->streams);
330
331     return ERROR_SUCCESS;
332 }
333
334 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
335                                        UINT val, UINT *row, MSIITERHANDLE *handle)
336 {
337     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
338     UINT index = (UINT)*handle;
339
340     TRACE("(%d, %d): %d\n", *row, col, val);
341
342     if (col == 0 || col > NUM_STREAMS_COLS)
343         return ERROR_INVALID_PARAMETER;
344
345     while (index < sv->num_rows)
346     {
347         if (sv->streams[index]->str_index == val)
348         {
349             *row = index;
350             break;
351         }
352
353         index++;
354     }
355
356     *handle = (MSIITERHANDLE)++index;
357     if (index >= sv->num_rows)
358         return ERROR_NO_MORE_ITEMS;
359
360     return ERROR_SUCCESS;
361 }
362
363 static const MSIVIEWOPS streams_ops =
364 {
365     STREAMS_fetch_int,
366     STREAMS_fetch_stream,
367     STREAMS_get_row,
368     STREAMS_set_row,
369     STREAMS_insert_row,
370     STREAMS_delete_row,
371     STREAMS_execute,
372     STREAMS_close,
373     STREAMS_get_dimensions,
374     STREAMS_get_column_info,
375     STREAMS_modify,
376     STREAMS_delete,
377     STREAMS_find_matching_rows,
378     NULL,
379     NULL,
380     NULL,
381     NULL,
382 };
383
384 static UINT add_streams_to_table(MSISTREAMSVIEW *sv)
385 {
386     IEnumSTATSTG *stgenum = NULL;
387     STATSTG stat;
388     STREAM *stream = NULL;
389     HRESULT hr;
390     UINT count = 0, size;
391
392     hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
393     if (FAILED(hr))
394         return -1;
395
396     sv->max_streams = 1;
397     sv->streams = msi_alloc(sizeof(STREAM *));
398     if (!sv->streams)
399         return -1;
400
401     while (TRUE)
402     {
403         size = 0;
404         hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
405         if (FAILED(hr) || !size)
406             break;
407
408         /* table streams are not in the _Streams table */
409         if (*stat.pwcsName == 0x4840)
410             continue;
411
412         stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
413         if (!stream)
414         {
415             count = -1;
416             break;
417         }
418
419         IStorage_OpenStream(sv->db->storage, stat.pwcsName, 0,
420                             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
421
422         if (!add_stream_to_table(sv, stream, count++))
423         {
424             count = -1;
425             break;
426         }
427     }
428
429     IEnumSTATSTG_Release(stgenum);
430     return count;
431 }
432
433 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
434 {
435     MSISTREAMSVIEW *sv;
436
437     TRACE("(%p, %p)\n", db, view);
438
439     sv = msi_alloc(sizeof(MSISTREAMSVIEW));
440     if (!sv)
441         return ERROR_FUNCTION_FAILED;
442
443     sv->view.ops = &streams_ops;
444     sv->db = db;
445     sv->num_rows = add_streams_to_table(sv);
446
447     if (sv->num_rows < 0)
448         return ERROR_FUNCTION_FAILED;
449
450     *view = (MSIVIEW *)sv;
451
452     return ERROR_SUCCESS;
453 }