msi: If the UserData product key exists, but the user product key doesn't, the produc...
[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_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
133 {
134     FIXME("(%p, %d, %p, %d): stub!\n", view, row, rec, mask);
135     return ERROR_SUCCESS;
136 }
137
138 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary)
139 {
140     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
141     STREAM *stream;
142     IStream *stm;
143     STATSTG stat;
144     LPWSTR name = NULL;
145     USHORT *data = NULL;
146     HRESULT hr;
147     ULONG count;
148     UINT r = ERROR_FUNCTION_FAILED;
149
150     TRACE("(%p, %p, %d)\n", view, rec, temporary);
151
152     r = MSI_RecordGetIStream(rec, 2, &stm);
153     if (r != ERROR_SUCCESS)
154         return r;
155
156     hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
157     if (FAILED(hr))
158     {
159         WARN("failed to stat stream: %08x\n", hr);
160         goto done;
161     }
162
163     if (stat.cbSize.QuadPart >> 32)
164         goto done;
165
166     data = msi_alloc(stat.cbSize.QuadPart);
167     if (!data)
168         goto done;
169
170     hr = IStream_Read(stm, data, stat.cbSize.QuadPart, &count);
171     if (FAILED(hr) || count != stat.cbSize.QuadPart)
172     {
173         WARN("failed to read stream: %08x\n", hr);
174         goto done;
175     }
176
177     name = strdupW(MSI_RecordGetString(rec, 1));
178     if (!name)
179         goto done;
180
181     r = write_stream_data(sv->db->storage, name, data, count, FALSE);
182     if (r != ERROR_SUCCESS)
183     {
184         WARN("failed to write stream data: %d\n", r);
185         goto done;
186     }
187
188     stream = create_stream(sv, name, FALSE, NULL);
189     if (!stream)
190         goto done;
191
192     IStorage_OpenStream(sv->db->storage, name, 0,
193                         STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
194
195     if (!add_stream_to_table(sv, stream, sv->num_rows++))
196         goto done;
197
198 done:
199     msi_free(name);
200     msi_free(data);
201
202     IStream_Release(stm);
203
204     return r;
205 }
206
207 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
208 {
209     FIXME("(%p %d): stub!\n", view, row);
210     return ERROR_SUCCESS;
211 }
212
213 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
214 {
215     TRACE("(%p, %p)\n", view, record);
216     return ERROR_SUCCESS;
217 }
218
219 static UINT STREAMS_close(struct tagMSIVIEW *view)
220 {
221     TRACE("(%p)\n", view);
222     return ERROR_SUCCESS;
223 }
224
225 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
226 {
227     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
228
229     TRACE("(%p, %p, %p)\n", view, rows, cols);
230
231     if (cols) *cols = NUM_STREAMS_COLS;
232     if (rows) *rows = sv->num_rows;
233
234     return ERROR_SUCCESS;
235 }
236
237 static UINT STREAMS_get_column_info(struct tagMSIVIEW *view,
238                                     UINT n, LPWSTR *name, UINT *type)
239 {
240     LPCWSTR name_ptr = NULL;
241
242     static const WCHAR Name[] = {'N','a','m','e',0};
243     static const WCHAR Data[] = {'D','a','t','a',0};
244
245     TRACE("(%p, %d, %p, %p)\n", view, n, name, type);
246
247     if (n == 0 || n > NUM_STREAMS_COLS)
248         return ERROR_INVALID_PARAMETER;
249
250     switch (n)
251     {
252     case 1:
253         name_ptr = Name;
254         if (type) *type = MSITYPE_STRING | MAX_STREAM_NAME_LEN;
255         break;
256
257     case 2:
258         name_ptr = Data;
259         if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
260         break;
261     }
262
263     if (name)
264     {
265         *name = strdupW(name_ptr);
266         if (!*name) return ERROR_FUNCTION_FAILED;
267     }
268
269     return ERROR_SUCCESS;
270 }
271
272 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec)
273 {
274     FIXME("(%p, %d, %p): stub!\n", view, eModifyMode, rec);
275     return ERROR_SUCCESS;
276 }
277
278 static UINT STREAMS_delete(struct tagMSIVIEW *view)
279 {
280     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
281     int i;
282
283     TRACE("(%p)\n", view);
284
285     for (i = 0; i < sv->num_rows; i++)
286     {
287         if (sv->streams[i]->stream)
288             IStream_Release(sv->streams[i]->stream);
289         msi_free(sv->streams[i]);
290     }
291
292     msi_free(sv->streams);
293
294     return ERROR_SUCCESS;
295 }
296
297 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
298                                        UINT val, UINT *row, MSIITERHANDLE *handle)
299 {
300     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
301     UINT index = (UINT)*handle;
302
303     TRACE("(%d, %d): %d\n", *row, col, val);
304
305     if (col == 0 || col > NUM_STREAMS_COLS)
306         return ERROR_INVALID_PARAMETER;
307
308     while (index < sv->num_rows)
309     {
310         if (sv->streams[index]->str_index == val)
311         {
312             *row = index;
313             break;
314         }
315
316         index++;
317     }
318
319     *handle = (MSIITERHANDLE)++index;
320     if (index >= sv->num_rows)
321         return ERROR_NO_MORE_ITEMS;
322
323     return ERROR_SUCCESS;
324 }
325
326 static const MSIVIEWOPS streams_ops =
327 {
328     STREAMS_fetch_int,
329     STREAMS_fetch_stream,
330     STREAMS_set_row,
331     STREAMS_insert_row,
332     STREAMS_delete_row,
333     STREAMS_execute,
334     STREAMS_close,
335     STREAMS_get_dimensions,
336     STREAMS_get_column_info,
337     STREAMS_modify,
338     STREAMS_delete,
339     STREAMS_find_matching_rows
340 };
341
342 static UINT add_streams_to_table(MSISTREAMSVIEW *sv)
343 {
344     IEnumSTATSTG *stgenum = NULL;
345     STATSTG stat;
346     STREAM *stream = NULL;
347     HRESULT hr;
348     UINT count = 0, size;
349
350     hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
351     if (FAILED(hr))
352         return -1;
353
354     sv->max_streams = 1;
355     sv->streams = msi_alloc(sizeof(STREAM *));
356     if (!sv->streams)
357         return -1;
358
359     while (TRUE)
360     {
361         size = 0;
362         hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
363         if (FAILED(hr) || !size)
364             break;
365
366         /* table streams are not in the _Streams table */
367         if (*stat.pwcsName == 0x4840)
368             continue;
369
370         stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
371         if (!stream)
372         {
373             count = -1;
374             break;
375         }
376
377         IStorage_OpenStream(sv->db->storage, stat.pwcsName, 0,
378                             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
379
380         if (!add_stream_to_table(sv, stream, count++))
381         {
382             count = -1;
383             break;
384         }
385     }
386
387     IEnumSTATSTG_Release(stgenum);
388     return count;
389 }
390
391 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
392 {
393     MSISTREAMSVIEW *sv;
394
395     TRACE("(%p, %p)\n", db, view);
396
397     sv = msi_alloc(sizeof(MSISTREAMSVIEW));
398     if (!sv)
399         return ERROR_FUNCTION_FAILED;
400
401     sv->view.ops = &streams_ops;
402     sv->db = db;
403     sv->num_rows = add_streams_to_table(sv);
404
405     if (sv->num_rows < 0)
406         return ERROR_FUNCTION_FAILED;
407
408     *view = (MSIVIEW *)sv;
409
410     return ERROR_SUCCESS;
411 }