msi: Fix an off-by-one error in STREAMS_find_matching_rows.
[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 #include "query.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37
38 #define NUM_STREAMS_COLS    2
39 #define MAX_STREAM_NAME_LEN 62
40
41 typedef struct tabSTREAM
42 {
43     UINT str_index;
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 streams_set_table_size(MSISTREAMSVIEW *sv, UINT size)
58 {
59     if (size >= sv->max_streams)
60     {
61         sv->max_streams *= 2;
62         sv->streams = msi_realloc_zero(sv->streams, sv->max_streams * sizeof(STREAM *));
63         if (!sv->streams)
64             return FALSE;
65     }
66
67     return TRUE;
68 }
69
70 static STREAM *create_stream(MSISTREAMSVIEW *sv, LPWSTR name, BOOL encoded, IStream *stm)
71 {
72     STREAM *stream;
73     WCHAR decoded[MAX_STREAM_NAME_LEN];
74
75     stream = msi_alloc(sizeof(STREAM));
76     if (!stream)
77         return NULL;
78
79     if (encoded)
80     {
81         decode_streamname(name, decoded);
82         TRACE("stream -> %s %s\n", debugstr_w(name), debugstr_w(decoded));
83         name = decoded;
84     }
85
86     stream->str_index = msi_addstringW(sv->db->strings, 0, name, -1, 1, StringNonPersistent);
87     stream->stream = stm;
88     return stream;
89 }
90
91 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
92 {
93     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
94
95     TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
96
97     if (col != 1)
98         return ERROR_INVALID_PARAMETER;
99
100     if (row >= sv->num_rows)
101         return ERROR_NO_MORE_ITEMS;
102
103     *val = sv->streams[row]->str_index;
104
105     return ERROR_SUCCESS;
106 }
107
108 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
109 {
110     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
111
112     TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
113
114     if (row >= sv->num_rows)
115         return ERROR_FUNCTION_FAILED;
116
117     IStream_AddRef(sv->streams[row]->stream);
118     *stm = sv->streams[row]->stream;
119
120     return ERROR_SUCCESS;
121 }
122
123 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
124 {
125     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
126
127     TRACE("%p %d %p\n", sv, row, rec);
128
129     return msi_view_get_row( sv->db, view, row, rec );
130 }
131
132 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
133 {
134     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
135     STREAM *stream;
136     IStream *stm;
137     STATSTG stat;
138     LPWSTR encname = NULL, name = NULL;
139     USHORT *data = NULL;
140     HRESULT hr;
141     ULONG count;
142     UINT r = ERROR_FUNCTION_FAILED;
143
144     TRACE("(%p, %d, %p, %08x)\n", view, row, rec, mask);
145
146     if (row > sv->num_rows)
147         return ERROR_FUNCTION_FAILED;
148
149     r = MSI_RecordGetIStream(rec, 2, &stm);
150     if (r != ERROR_SUCCESS)
151         return r;
152
153     hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
154     if (FAILED(hr))
155     {
156         WARN("failed to stat stream: %08x\n", hr);
157         goto done;
158     }
159
160     if (stat.cbSize.QuadPart >> 32)
161     {
162         WARN("stream too large\n");
163         goto done;
164     }
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     {
180         WARN("failed to retrieve stream name\n");
181         goto done;
182     }
183
184     encname = encode_streamname(FALSE, name);
185     IStorage_DestroyElement(sv->db->storage, encname);
186
187     r = write_stream_data(sv->db->storage, name, data, count, FALSE);
188     if (r != ERROR_SUCCESS)
189     {
190         WARN("failed to write stream data: %d\n", r);
191         goto done;
192     }
193
194     stream = create_stream(sv, name, FALSE, NULL);
195     if (!stream)
196         goto done;
197
198     hr = IStorage_OpenStream(sv->db->storage, encname, 0,
199                              STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
200     if (FAILED(hr))
201     {
202         WARN("failed to open stream: %08x\n", hr);
203         goto done;
204     }
205
206     sv->streams[row] = stream;
207
208 done:
209     msi_free(name);
210     msi_free(data);
211     msi_free(encname);
212
213     IStream_Release(stm);
214
215     return r;
216 }
217
218 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
219 {
220     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
221     UINT i;
222
223     TRACE("(%p, %p, %d, %d)\n", view, rec, row, temporary);
224
225     if (!streams_set_table_size(sv, ++sv->num_rows))
226         return ERROR_FUNCTION_FAILED;
227
228     if (row == -1)
229         row = sv->num_rows - 1;
230
231     /* shift the rows to make room for the new row */
232     for (i = sv->num_rows - 1; i > row; i--)
233     {
234         sv->streams[i] = sv->streams[i - 1];
235     }
236
237     return STREAMS_set_row(view, row, rec, 0);
238 }
239
240 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
241 {
242     FIXME("(%p %d): stub!\n", view, row);
243     return ERROR_SUCCESS;
244 }
245
246 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
247 {
248     TRACE("(%p, %p)\n", view, record);
249     return ERROR_SUCCESS;
250 }
251
252 static UINT STREAMS_close(struct tagMSIVIEW *view)
253 {
254     TRACE("(%p)\n", view);
255     return ERROR_SUCCESS;
256 }
257
258 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
259 {
260     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
261
262     TRACE("(%p, %p, %p)\n", view, rows, cols);
263
264     if (cols) *cols = NUM_STREAMS_COLS;
265     if (rows) *rows = sv->num_rows;
266
267     return ERROR_SUCCESS;
268 }
269
270 static UINT STREAMS_get_column_info(struct tagMSIVIEW *view, UINT n,
271                                     LPWSTR *name, UINT *type, BOOL *temporary,
272                                     LPWSTR *table_name)
273 {
274     LPCWSTR name_ptr = NULL;
275
276     static const WCHAR Name[] = {'N','a','m','e',0};
277     static const WCHAR Data[] = {'D','a','t','a',0};
278     static const WCHAR _Streams[] = {'_','S','t','r','e','a','m','s',0};
279
280     TRACE("(%p, %d, %p, %p, %p, %p)\n", view, n, name, type, temporary,
281           table_name);
282
283     if (n == 0 || n > NUM_STREAMS_COLS)
284         return ERROR_INVALID_PARAMETER;
285
286     switch (n)
287     {
288     case 1:
289         name_ptr = Name;
290         if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MAX_STREAM_NAME_LEN;
291         break;
292
293     case 2:
294         name_ptr = Data;
295         if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
296         break;
297     }
298
299     if (name)
300     {
301         *name = strdupW(name_ptr);
302         if (!*name) return ERROR_FUNCTION_FAILED;
303     }
304
305     if (table_name)
306     {
307         *table_name = strdupW(_Streams);
308         if (!*table_name)
309         {
310             msi_free(name);
311             return ERROR_FUNCTION_FAILED;
312         }
313     }
314
315     if (temporary)
316         *temporary = FALSE;
317
318     return ERROR_SUCCESS;
319 }
320
321 static UINT streams_find_row(MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row)
322 {
323     LPCWSTR str;
324     UINT i, id, data;
325
326     str = MSI_RecordGetString(rec, 1);
327     msi_string2idW(sv->db->strings, str, &id);
328
329     for (i = 0; i < sv->num_rows; i++)
330     {
331         STREAMS_fetch_int(&sv->view, i, 1, &data);
332
333         if (data == id)
334         {
335             *row = i;
336             return ERROR_SUCCESS;
337         }
338     }
339
340     return ERROR_FUNCTION_FAILED;
341 }
342
343 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
344 {
345     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
346     UINT r, row;
347
348     r = streams_find_row(sv, rec, &row);
349     if (r != ERROR_SUCCESS)
350         return ERROR_FUNCTION_FAILED;
351
352     return STREAMS_set_row(view, row, rec, 0);
353 }
354
355 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
356 {
357     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
358     UINT r, row;
359
360     r = streams_find_row(sv, rec, &row);
361     if (r == ERROR_SUCCESS)
362         return streams_modify_update(view, rec);
363
364     return STREAMS_insert_row(view, rec, -1, FALSE);
365 }
366
367 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
368 {
369     UINT r;
370
371     TRACE("%p %d %p\n", view, eModifyMode, rec);
372
373     switch (eModifyMode)
374     {
375     case MSIMODIFY_ASSIGN:
376         r = streams_modify_assign(view, rec);
377         break;
378
379     case MSIMODIFY_INSERT:
380         r = STREAMS_insert_row(view, rec, -1, FALSE);
381         break;
382
383     case MSIMODIFY_UPDATE:
384         r = streams_modify_update(view, rec);
385         break;
386
387     case MSIMODIFY_VALIDATE_NEW:
388     case MSIMODIFY_INSERT_TEMPORARY:
389     case MSIMODIFY_REFRESH:
390     case MSIMODIFY_REPLACE:
391     case MSIMODIFY_MERGE:
392     case MSIMODIFY_DELETE:
393     case MSIMODIFY_VALIDATE:
394     case MSIMODIFY_VALIDATE_FIELD:
395     case MSIMODIFY_VALIDATE_DELETE:
396         FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
397         r = ERROR_CALL_NOT_IMPLEMENTED;
398         break;
399
400     default:
401         r = ERROR_INVALID_DATA;
402     }
403
404     return r;
405 }
406
407 static UINT STREAMS_delete(struct tagMSIVIEW *view)
408 {
409     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
410     UINT i;
411
412     TRACE("(%p)\n", view);
413
414     for (i = 0; i < sv->num_rows; i++)
415     {
416         if (sv->streams[i])
417         {
418             if (sv->streams[i]->stream)
419                 IStream_Release(sv->streams[i]->stream);
420             msi_free(sv->streams[i]);
421         }
422     }
423
424     msi_free(sv->streams);
425     msi_free(sv);
426
427     return ERROR_SUCCESS;
428 }
429
430 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
431                                        UINT val, UINT *row, MSIITERHANDLE *handle)
432 {
433     MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
434     UINT index = PtrToUlong(*handle);
435
436     TRACE("(%p, %d, %d, %p, %p)\n", view, col, val, row, handle);
437
438     if (col == 0 || col > NUM_STREAMS_COLS)
439         return ERROR_INVALID_PARAMETER;
440
441     while (index < sv->num_rows)
442     {
443         if (sv->streams[index]->str_index == val)
444         {
445             *row = index;
446             break;
447         }
448
449         index++;
450     }
451
452     *handle = UlongToPtr(++index);
453
454     if (index > sv->num_rows)
455         return ERROR_NO_MORE_ITEMS;
456
457     return ERROR_SUCCESS;
458 }
459
460 static const MSIVIEWOPS streams_ops =
461 {
462     STREAMS_fetch_int,
463     STREAMS_fetch_stream,
464     STREAMS_get_row,
465     STREAMS_set_row,
466     STREAMS_insert_row,
467     STREAMS_delete_row,
468     STREAMS_execute,
469     STREAMS_close,
470     STREAMS_get_dimensions,
471     STREAMS_get_column_info,
472     STREAMS_modify,
473     STREAMS_delete,
474     STREAMS_find_matching_rows,
475     NULL,
476     NULL,
477     NULL,
478     NULL,
479     NULL,
480     NULL,
481 };
482
483 static INT add_streams_to_table(MSISTREAMSVIEW *sv)
484 {
485     IEnumSTATSTG *stgenum = NULL;
486     STATSTG stat;
487     STREAM *stream = NULL;
488     HRESULT hr;
489     UINT count = 0, size;
490
491     hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
492     if (FAILED(hr))
493         return -1;
494
495     sv->max_streams = 1;
496     sv->streams = msi_alloc_zero(sizeof(STREAM *));
497     if (!sv->streams)
498         return -1;
499
500     while (TRUE)
501     {
502         size = 0;
503         hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
504         if (FAILED(hr) || !size)
505             break;
506
507         if (stat.type != STGTY_STREAM)
508             continue;
509
510         /* table streams are not in the _Streams table */
511         if (*stat.pwcsName == 0x4840)
512         {
513             CoTaskMemFree(stat.pwcsName);
514             continue;
515         }
516
517         stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
518         if (!stream)
519         {
520             count = -1;
521             CoTaskMemFree(stat.pwcsName);
522             break;
523         }
524
525         hr = IStorage_OpenStream(sv->db->storage, stat.pwcsName, 0,
526                                  STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
527         CoTaskMemFree(stat.pwcsName);
528
529         if (FAILED(hr))
530         {
531             WARN("failed to open stream: %08x\n", hr);
532             count = -1;
533             break;
534         }
535
536         if (!streams_set_table_size(sv, ++count))
537         {
538             count = -1;
539             break;
540         }
541
542         sv->streams[count - 1] = stream;
543     }
544
545     IEnumSTATSTG_Release(stgenum);
546     return count;
547 }
548
549 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
550 {
551     MSISTREAMSVIEW *sv;
552     INT rows;
553
554     TRACE("(%p, %p)\n", db, view);
555
556     sv = msi_alloc(sizeof(MSISTREAMSVIEW));
557     if (!sv)
558         return ERROR_FUNCTION_FAILED;
559
560     sv->view.ops = &streams_ops;
561     sv->db = db;
562     rows = add_streams_to_table(sv);
563     if (rows < 0)
564     {
565         msi_free( sv );
566         return ERROR_FUNCTION_FAILED;
567     }
568     sv->num_rows = rows;
569
570     *view = (MSIVIEW *)sv;
571
572     return ERROR_SUCCESS;
573 }