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