bca1c0125a01346f01c5acc75d6239982df31fcd
[reactos.git] / reactos / dll / win32 / msi / streams.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2007 James Hawkins
5 * Copyright 2015 Hans Leidekker for CodeWeavers
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "msipriv.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
25
26 #define NUM_STREAMS_COLS 2
27
28 typedef struct tagMSISTREAMSVIEW
29 {
30 MSIVIEW view;
31 MSIDATABASE *db;
32 UINT num_cols;
33 } MSISTREAMSVIEW;
34
35 static BOOL streams_resize_table( MSIDATABASE *db, UINT size )
36 {
37 if (!db->num_streams_allocated)
38 {
39 if (!(db->streams = msi_alloc_zero( size * sizeof(MSISTREAM) ))) return FALSE;
40 db->num_streams_allocated = size;
41 return TRUE;
42 }
43 while (size >= db->num_streams_allocated)
44 {
45 MSISTREAM *tmp;
46 UINT new_size = db->num_streams_allocated * 2;
47 if (!(tmp = msi_realloc_zero( db->streams, new_size * sizeof(MSISTREAM) ))) return FALSE;
48 db->streams = tmp;
49 db->num_streams_allocated = new_size;
50 }
51 return TRUE;
52 }
53
54 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
55 {
56 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
57
58 TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
59
60 if (col != 1)
61 return ERROR_INVALID_PARAMETER;
62
63 if (row >= sv->db->num_streams)
64 return ERROR_NO_MORE_ITEMS;
65
66 *val = sv->db->streams[row].str_index;
67
68 return ERROR_SUCCESS;
69 }
70
71 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
72 {
73 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
74 LARGE_INTEGER pos;
75 HRESULT hr;
76
77 TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
78
79 if (row >= sv->db->num_streams)
80 return ERROR_FUNCTION_FAILED;
81
82 pos.QuadPart = 0;
83 hr = IStream_Seek( sv->db->streams[row].stream, pos, STREAM_SEEK_SET, NULL );
84 if (FAILED( hr ))
85 return ERROR_FUNCTION_FAILED;
86
87 *stm = sv->db->streams[row].stream;
88 IStream_AddRef( *stm );
89
90 return ERROR_SUCCESS;
91 }
92
93 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
94 {
95 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
96
97 TRACE("%p %d %p\n", sv, row, rec);
98
99 return msi_view_get_row( sv->db, view, row, rec );
100 }
101
102 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
103 {
104 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
105
106 TRACE("(%p, %d, %p, %08x)\n", view, row, rec, mask);
107
108 if (row > sv->db->num_streams || mask >= (1 << sv->num_cols))
109 return ERROR_INVALID_PARAMETER;
110
111 if (mask & 1)
112 {
113 const WCHAR *name = MSI_RecordGetString( rec, 1 );
114
115 if (!name) return ERROR_INVALID_PARAMETER;
116 sv->db->streams[row].str_index = msi_add_string( sv->db->strings, name, -1, StringNonPersistent );
117 }
118 if (mask & 2)
119 {
120 IStream *old, *new;
121 HRESULT hr;
122 UINT r;
123
124 r = MSI_RecordGetIStream( rec, 2, &new );
125 if (r != ERROR_SUCCESS)
126 return r;
127
128 old = sv->db->streams[row].stream;
129 hr = IStream_QueryInterface( new, &IID_IStream, (void **)&sv->db->streams[row].stream );
130 if (FAILED( hr ))
131 {
132 IStream_Release( new );
133 return ERROR_FUNCTION_FAILED;
134 }
135 if (old) IStream_Release( old );
136 }
137
138 return ERROR_SUCCESS;
139 }
140
141 static UINT streams_find_row( MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row )
142 {
143 const WCHAR *str;
144 UINT r, i, id, val;
145
146 str = MSI_RecordGetString( rec, 1 );
147 r = msi_string2id( sv->db->strings, str, -1, &id );
148 if (r != ERROR_SUCCESS)
149 return r;
150
151 for (i = 0; i < sv->db->num_streams; i++)
152 {
153 STREAMS_fetch_int( &sv->view, i, 1, &val );
154
155 if (val == id)
156 {
157 if (row) *row = i;
158 return ERROR_SUCCESS;
159 }
160 }
161
162 return ERROR_FUNCTION_FAILED;
163 }
164
165 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
166 {
167 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
168 UINT i, r, num_rows = sv->db->num_streams + 1;
169
170 TRACE("(%p, %p, %d, %d)\n", view, rec, row, temporary);
171
172 r = streams_find_row( sv, rec, NULL );
173 if (r == ERROR_SUCCESS)
174 return ERROR_FUNCTION_FAILED;
175
176 if (!streams_resize_table( sv->db, num_rows ))
177 return ERROR_FUNCTION_FAILED;
178
179 if (row == -1)
180 row = num_rows - 1;
181
182 /* shift the rows to make room for the new row */
183 for (i = num_rows - 1; i > row; i--)
184 {
185 sv->db->streams[i] = sv->db->streams[i - 1];
186 }
187
188 r = STREAMS_set_row( view, row, rec, (1 << sv->num_cols) - 1 );
189 if (r == ERROR_SUCCESS)
190 sv->db->num_streams = num_rows;
191
192 return r;
193 }
194
195 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
196 {
197 FIXME("(%p %d): stub!\n", view, row);
198 return ERROR_SUCCESS;
199 }
200
201 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
202 {
203 TRACE("(%p, %p)\n", view, record);
204 return ERROR_SUCCESS;
205 }
206
207 static UINT STREAMS_close(struct tagMSIVIEW *view)
208 {
209 TRACE("(%p)\n", view);
210 return ERROR_SUCCESS;
211 }
212
213 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
214 {
215 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
216
217 TRACE("(%p, %p, %p)\n", view, rows, cols);
218
219 if (cols) *cols = sv->num_cols;
220 if (rows) *rows = sv->db->num_streams;
221
222 return ERROR_SUCCESS;
223 }
224
225 static UINT STREAMS_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
226 UINT *type, BOOL *temporary, LPCWSTR *table_name )
227 {
228 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
229
230 TRACE("(%p, %d, %p, %p, %p, %p)\n", view, n, name, type, temporary, table_name);
231
232 if (!n || n > sv->num_cols)
233 return ERROR_INVALID_PARAMETER;
234
235 switch (n)
236 {
237 case 1:
238 if (name) *name = szName;
239 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MAX_STREAM_NAME_LEN;
240 break;
241
242 case 2:
243 if (name) *name = szData;
244 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
245 break;
246 }
247 if (table_name) *table_name = szStreams;
248 if (temporary) *temporary = FALSE;
249 return ERROR_SUCCESS;
250 }
251
252 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
253 {
254 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
255 UINT r, row;
256
257 r = streams_find_row(sv, rec, &row);
258 if (r != ERROR_SUCCESS)
259 return ERROR_FUNCTION_FAILED;
260
261 return STREAMS_set_row( view, row, rec, (1 << sv->num_cols) - 1 );
262 }
263
264 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
265 {
266 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
267 UINT r;
268
269 r = streams_find_row( sv, rec, NULL );
270 if (r == ERROR_SUCCESS)
271 return streams_modify_update(view, rec);
272
273 return STREAMS_insert_row(view, rec, -1, FALSE);
274 }
275
276 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
277 {
278 UINT r;
279
280 TRACE("%p %d %p\n", view, eModifyMode, rec);
281
282 switch (eModifyMode)
283 {
284 case MSIMODIFY_ASSIGN:
285 r = streams_modify_assign(view, rec);
286 break;
287
288 case MSIMODIFY_INSERT:
289 r = STREAMS_insert_row(view, rec, -1, FALSE);
290 break;
291
292 case MSIMODIFY_UPDATE:
293 r = streams_modify_update(view, rec);
294 break;
295
296 case MSIMODIFY_VALIDATE_NEW:
297 case MSIMODIFY_INSERT_TEMPORARY:
298 case MSIMODIFY_REFRESH:
299 case MSIMODIFY_REPLACE:
300 case MSIMODIFY_MERGE:
301 case MSIMODIFY_DELETE:
302 case MSIMODIFY_VALIDATE:
303 case MSIMODIFY_VALIDATE_FIELD:
304 case MSIMODIFY_VALIDATE_DELETE:
305 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
306 r = ERROR_CALL_NOT_IMPLEMENTED;
307 break;
308
309 default:
310 r = ERROR_INVALID_DATA;
311 }
312
313 return r;
314 }
315
316 static UINT STREAMS_delete(struct tagMSIVIEW *view)
317 {
318 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
319
320 TRACE("(%p)\n", view);
321
322 msi_free(sv);
323 return ERROR_SUCCESS;
324 }
325
326 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
327 UINT val, UINT *row, MSIITERHANDLE *handle)
328 {
329 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
330 UINT index = PtrToUlong(*handle);
331
332 TRACE("(%p, %d, %d, %p, %p)\n", view, col, val, row, handle);
333
334 if (!col || col > sv->num_cols)
335 return ERROR_INVALID_PARAMETER;
336
337 while (index < sv->db->num_streams)
338 {
339 if (sv->db->streams[index].str_index == val)
340 {
341 *row = index;
342 break;
343 }
344 index++;
345 }
346
347 *handle = UlongToPtr(++index);
348
349 if (index > sv->db->num_streams)
350 return ERROR_NO_MORE_ITEMS;
351
352 return ERROR_SUCCESS;
353 }
354
355 static const MSIVIEWOPS streams_ops =
356 {
357 STREAMS_fetch_int,
358 STREAMS_fetch_stream,
359 STREAMS_get_row,
360 STREAMS_set_row,
361 STREAMS_insert_row,
362 STREAMS_delete_row,
363 STREAMS_execute,
364 STREAMS_close,
365 STREAMS_get_dimensions,
366 STREAMS_get_column_info,
367 STREAMS_modify,
368 STREAMS_delete,
369 STREAMS_find_matching_rows,
370 NULL,
371 NULL,
372 NULL,
373 NULL,
374 NULL,
375 NULL,
376 };
377
378 static HRESULT open_stream( MSIDATABASE *db, const WCHAR *name, IStream **stream )
379 {
380 HRESULT hr;
381
382 hr = IStorage_OpenStream( db->storage, name, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, stream );
383 if (FAILED( hr ))
384 {
385 MSITRANSFORM *transform;
386
387 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
388 {
389 hr = IStorage_OpenStream( transform->stg, name, NULL, STGM_READ|STGM_SHARE_EXCLUSIVE, 0, stream );
390 if (SUCCEEDED( hr ))
391 break;
392 }
393 }
394 return hr;
395 }
396
397 static MSISTREAM *find_stream( MSIDATABASE *db, const WCHAR *name )
398 {
399 UINT r, id, i;
400
401 r = msi_string2id( db->strings, name, -1, &id );
402 if (r != ERROR_SUCCESS)
403 return NULL;
404
405 for (i = 0; i < db->num_streams; i++)
406 {
407 if (db->streams[i].str_index == id) return &db->streams[i];
408 }
409 return NULL;
410 }
411
412 static UINT append_stream( MSIDATABASE *db, const WCHAR *name, IStream *stream )
413 {
414 WCHAR decoded[MAX_STREAM_NAME_LEN + 1];
415 UINT i = db->num_streams;
416
417 if (!streams_resize_table( db, db->num_streams + 1 ))
418 return ERROR_OUTOFMEMORY;
419
420 decode_streamname( name, decoded );
421 db->streams[i].str_index = msi_add_string( db->strings, decoded, -1, StringNonPersistent );
422 db->streams[i].stream = stream;
423 db->num_streams++;
424
425 TRACE("added %s\n", debugstr_w( decoded ));
426 return ERROR_SUCCESS;
427 }
428
429 static UINT load_streams( MSIDATABASE *db )
430 {
431 IEnumSTATSTG *stgenum;
432 STATSTG stat;
433 HRESULT hr;
434 UINT count, r = ERROR_SUCCESS;
435 IStream *stream;
436
437 hr = IStorage_EnumElements( db->storage, 0, NULL, 0, &stgenum );
438 if (FAILED( hr ))
439 return ERROR_FUNCTION_FAILED;
440
441 for (;;)
442 {
443 count = 0;
444 hr = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
445 if (FAILED( hr ) || !count)
446 break;
447
448 /* table streams are not in the _Streams table */
449 if (stat.type != STGTY_STREAM || *stat.pwcsName == 0x4840 ||
450 find_stream( db, stat.pwcsName ))
451 {
452 CoTaskMemFree( stat.pwcsName );
453 continue;
454 }
455 TRACE("found new stream %s\n", debugstr_w( stat.pwcsName ));
456
457 hr = open_stream( db, stat.pwcsName, &stream );
458 if (FAILED( hr ))
459 {
460 ERR("unable to open stream %08x\n", hr);
461 CoTaskMemFree( stat.pwcsName );
462 r = ERROR_FUNCTION_FAILED;
463 break;
464 }
465
466 r = append_stream( db, stat.pwcsName, stream );
467 CoTaskMemFree( stat.pwcsName );
468 if (r != ERROR_SUCCESS)
469 break;
470 }
471
472 TRACE("loaded %u streams\n", db->num_streams);
473 IEnumSTATSTG_Release( stgenum );
474 return r;
475 }
476
477 UINT msi_get_stream( MSIDATABASE *db, const WCHAR *name, IStream **ret )
478 {
479 MSISTREAM *stream;
480 WCHAR *encname;
481 HRESULT hr;
482 UINT r;
483
484 if ((stream = find_stream( db, name )))
485 {
486 LARGE_INTEGER pos;
487
488 pos.QuadPart = 0;
489 hr = IStream_Seek( stream->stream, pos, STREAM_SEEK_SET, NULL );
490 if (FAILED( hr ))
491 return ERROR_FUNCTION_FAILED;
492
493 *ret = stream->stream;
494 IStream_AddRef( *ret );
495 return ERROR_SUCCESS;
496 }
497
498 if (!(encname = encode_streamname( FALSE, name )))
499 return ERROR_OUTOFMEMORY;
500
501 hr = open_stream( db, encname, ret );
502 msi_free( encname );
503 if (FAILED( hr ))
504 return ERROR_FUNCTION_FAILED;
505
506 r = append_stream( db, name, *ret );
507 if (r != ERROR_SUCCESS)
508 {
509 IStream_Release( *ret );
510 return r;
511 }
512
513 IStream_AddRef( *ret );
514 return ERROR_SUCCESS;
515 }
516
517 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
518 {
519 MSISTREAMSVIEW *sv;
520 UINT r;
521
522 TRACE("(%p, %p)\n", db, view);
523
524 r = load_streams( db );
525 if (r != ERROR_SUCCESS)
526 return r;
527
528 if (!(sv = msi_alloc_zero( sizeof(MSISTREAMSVIEW) )))
529 return ERROR_OUTOFMEMORY;
530
531 sv->view.ops = &streams_ops;
532 sv->num_cols = NUM_STREAMS_COLS;
533 sv->db = db;
534
535 *view = (MSIVIEW *)sv;
536
537 return ERROR_SUCCESS;
538 }
539
540 static HRESULT write_stream( IStream *dst, IStream *src )
541 {
542 HRESULT hr;
543 char buf[4096];
544 STATSTG stat;
545 LARGE_INTEGER pos;
546 UINT count, size;
547
548 hr = IStream_Stat( src, &stat, STATFLAG_NONAME );
549 if (FAILED( hr )) return hr;
550
551 hr = IStream_SetSize( dst, stat.cbSize );
552 if (FAILED( hr )) return hr;
553
554 pos.QuadPart = 0;
555 hr = IStream_Seek( dst, pos, STREAM_SEEK_SET, NULL );
556 if (FAILED( hr )) return hr;
557
558 for (;;)
559 {
560 size = min( sizeof(buf), stat.cbSize.QuadPart );
561 hr = IStream_Read( src, buf, size, &count );
562 if (FAILED( hr ) || count != size)
563 {
564 WARN("failed to read stream: %08x\n", hr);
565 return E_INVALIDARG;
566 }
567 stat.cbSize.QuadPart -= count;
568 if (count)
569 {
570 size = count;
571 hr = IStream_Write( dst, buf, size, &count );
572 if (FAILED( hr ) || count != size)
573 {
574 WARN("failed to write stream: %08x\n", hr);
575 return E_INVALIDARG;
576 }
577 }
578 if (!stat.cbSize.QuadPart) break;
579 }
580
581 return S_OK;
582 }
583
584 UINT msi_commit_streams( MSIDATABASE *db )
585 {
586 UINT i;
587 const WCHAR *name;
588 WCHAR *encname;
589 IStream *stream;
590 HRESULT hr;
591
592 TRACE("got %u streams\n", db->num_streams);
593
594 for (i = 0; i < db->num_streams; i++)
595 {
596 name = msi_string_lookup( db->strings, db->streams[i].str_index, NULL );
597 if (!(encname = encode_streamname( FALSE, name ))) return ERROR_OUTOFMEMORY;
598
599 hr = open_stream( db, encname, &stream );
600 if (FAILED( hr )) /* new stream */
601 {
602 hr = IStorage_CreateStream( db->storage, encname, STGM_WRITE|STGM_SHARE_EXCLUSIVE, 0, 0, &stream );
603 if (FAILED( hr ))
604 {
605 ERR("failed to create stream %s (hr = %08x)\n", debugstr_w(encname), hr);
606 msi_free( encname );
607 return ERROR_FUNCTION_FAILED;
608 }
609 hr = write_stream( stream, db->streams[i].stream );
610 if (FAILED( hr ))
611 {
612 ERR("failed to write stream %s (hr = %08x)\n", debugstr_w(encname), hr);
613 msi_free( encname );
614 IStream_Release( stream );
615 return ERROR_FUNCTION_FAILED;
616 }
617 }
618 hr = IStream_Commit( stream, 0 );
619 IStream_Release( stream );
620 if (FAILED( hr ))
621 {
622 WARN("failed to commit stream %s (hr = %08x)\n", debugstr_w(encname), hr);
623 msi_free( encname );
624 return ERROR_FUNCTION_FAILED;
625 }
626 msi_free( encname );
627 }
628
629 return ERROR_SUCCESS;
630 }