msi: Get rid of the refcount parameter to msi_addstringW.
[wine.git] / dlls / msi / streams.c
blobd795c8ad023d807eca01a911f6ff1b4f202f3ef7
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2007 James Hawkins
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.
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.
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
21 #include <stdarg.h>
23 #define COBJMACROS
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"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
39 #define NUM_STREAMS_COLS 2
41 typedef struct tabSTREAM
43 UINT str_index;
44 IStream *stream;
45 } STREAM;
47 typedef struct tagMSISTREAMSVIEW
49 MSIVIEW view;
50 MSIDATABASE *db;
51 STREAM **streams;
52 UINT max_streams;
53 UINT num_rows;
54 UINT row_size;
55 } MSISTREAMSVIEW;
57 static BOOL streams_set_table_size(MSISTREAMSVIEW *sv, UINT size)
59 if (size >= sv->max_streams)
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;
67 return TRUE;
70 static STREAM *create_stream(MSISTREAMSVIEW *sv, LPCWSTR name, BOOL encoded, IStream *stm)
72 STREAM *stream;
73 WCHAR decoded[MAX_STREAM_NAME_LEN + 1];
75 stream = msi_alloc(sizeof(STREAM));
76 if (!stream)
77 return NULL;
79 if (encoded)
81 decode_streamname(name, decoded);
82 TRACE("stream -> %s %s\n", debugstr_w(name), debugstr_w(decoded));
83 name = decoded;
86 stream->str_index = msi_add_string(sv->db->strings, name, -1, StringNonPersistent);
87 stream->stream = stm;
88 return stream;
91 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
93 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
95 TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
97 if (col != 1)
98 return ERROR_INVALID_PARAMETER;
100 if (row >= sv->num_rows)
101 return ERROR_NO_MORE_ITEMS;
103 *val = sv->streams[row]->str_index;
105 return ERROR_SUCCESS;
108 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
110 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
112 TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
114 if (row >= sv->num_rows)
115 return ERROR_FUNCTION_FAILED;
117 IStream_AddRef(sv->streams[row]->stream);
118 *stm = sv->streams[row]->stream;
120 return ERROR_SUCCESS;
123 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
125 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
127 TRACE("%p %d %p\n", sv, row, rec);
129 return msi_view_get_row( sv->db, view, row, rec );
132 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
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;
144 TRACE("(%p, %d, %p, %08x)\n", view, row, rec, mask);
146 if (row > sv->num_rows)
147 return ERROR_FUNCTION_FAILED;
149 r = MSI_RecordGetIStream(rec, 2, &stm);
150 if (r != ERROR_SUCCESS)
151 return r;
153 hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
154 if (FAILED(hr))
156 WARN("failed to stat stream: %08x\n", hr);
157 goto done;
160 if (stat.cbSize.QuadPart >> 32)
162 WARN("stream too large\n");
163 goto done;
166 data = msi_alloc(stat.cbSize.QuadPart);
167 if (!data)
168 goto done;
170 hr = IStream_Read(stm, data, stat.cbSize.QuadPart, &count);
171 if (FAILED(hr) || count != stat.cbSize.QuadPart)
173 WARN("failed to read stream: %08x\n", hr);
174 goto done;
177 name = strdupW(MSI_RecordGetString(rec, 1));
178 if (!name)
180 WARN("failed to retrieve stream name\n");
181 goto done;
184 r = write_stream_data(sv->db->storage, name, data, count, FALSE);
185 if (r != ERROR_SUCCESS)
187 WARN("failed to write stream data: %d\n", r);
188 goto done;
191 stream = create_stream(sv, name, FALSE, NULL);
192 if (!stream)
193 goto done;
195 encname = encode_streamname(FALSE, name);
196 hr = IStorage_OpenStream(sv->db->storage, encname, 0,
197 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
198 if (FAILED(hr))
200 WARN("failed to open stream: %08x\n", hr);
201 msi_free(stream);
202 goto done;
205 sv->streams[row] = stream;
207 done:
208 msi_free(name);
209 msi_free(data);
210 msi_free(encname);
212 IStream_Release(stm);
214 return r;
217 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
219 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
220 UINT i;
222 TRACE("(%p, %p, %d, %d)\n", view, rec, row, temporary);
224 if (!streams_set_table_size(sv, ++sv->num_rows))
225 return ERROR_FUNCTION_FAILED;
227 if (row == -1)
228 row = sv->num_rows - 1;
230 /* shift the rows to make room for the new row */
231 for (i = sv->num_rows - 1; i > row; i--)
233 sv->streams[i] = sv->streams[i - 1];
236 return STREAMS_set_row(view, row, rec, 0);
239 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
241 FIXME("(%p %d): stub!\n", view, row);
242 return ERROR_SUCCESS;
245 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
247 TRACE("(%p, %p)\n", view, record);
248 return ERROR_SUCCESS;
251 static UINT STREAMS_close(struct tagMSIVIEW *view)
253 TRACE("(%p)\n", view);
254 return ERROR_SUCCESS;
257 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
259 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
261 TRACE("(%p, %p, %p)\n", view, rows, cols);
263 if (cols) *cols = NUM_STREAMS_COLS;
264 if (rows) *rows = sv->num_rows;
266 return ERROR_SUCCESS;
269 static UINT STREAMS_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
270 UINT *type, BOOL *temporary, LPCWSTR *table_name )
272 TRACE("(%p, %d, %p, %p, %p, %p)\n", view, n, name, type, temporary,
273 table_name);
275 if (n == 0 || n > NUM_STREAMS_COLS)
276 return ERROR_INVALID_PARAMETER;
278 switch (n)
280 case 1:
281 if (name) *name = szName;
282 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MAX_STREAM_NAME_LEN;
283 break;
285 case 2:
286 if (name) *name = szData;
287 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
288 break;
290 if (table_name) *table_name = szStreams;
291 if (temporary) *temporary = FALSE;
292 return ERROR_SUCCESS;
295 static UINT streams_find_row(MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row)
297 LPCWSTR str;
298 UINT r, i, id, data;
300 str = MSI_RecordGetString(rec, 1);
301 r = msi_string2id(sv->db->strings, str, -1, &id);
302 if (r != ERROR_SUCCESS)
303 return r;
305 for (i = 0; i < sv->num_rows; i++)
307 STREAMS_fetch_int(&sv->view, i, 1, &data);
309 if (data == id)
311 *row = i;
312 return ERROR_SUCCESS;
316 return ERROR_FUNCTION_FAILED;
319 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
321 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
322 UINT r, row;
324 r = streams_find_row(sv, rec, &row);
325 if (r != ERROR_SUCCESS)
326 return ERROR_FUNCTION_FAILED;
328 return STREAMS_set_row(view, row, rec, 0);
331 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
333 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
334 UINT r, row;
336 r = streams_find_row(sv, rec, &row);
337 if (r == ERROR_SUCCESS)
338 return streams_modify_update(view, rec);
340 return STREAMS_insert_row(view, rec, -1, FALSE);
343 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
345 UINT r;
347 TRACE("%p %d %p\n", view, eModifyMode, rec);
349 switch (eModifyMode)
351 case MSIMODIFY_ASSIGN:
352 r = streams_modify_assign(view, rec);
353 break;
355 case MSIMODIFY_INSERT:
356 r = STREAMS_insert_row(view, rec, -1, FALSE);
357 break;
359 case MSIMODIFY_UPDATE:
360 r = streams_modify_update(view, rec);
361 break;
363 case MSIMODIFY_VALIDATE_NEW:
364 case MSIMODIFY_INSERT_TEMPORARY:
365 case MSIMODIFY_REFRESH:
366 case MSIMODIFY_REPLACE:
367 case MSIMODIFY_MERGE:
368 case MSIMODIFY_DELETE:
369 case MSIMODIFY_VALIDATE:
370 case MSIMODIFY_VALIDATE_FIELD:
371 case MSIMODIFY_VALIDATE_DELETE:
372 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
373 r = ERROR_CALL_NOT_IMPLEMENTED;
374 break;
376 default:
377 r = ERROR_INVALID_DATA;
380 return r;
383 static UINT STREAMS_delete(struct tagMSIVIEW *view)
385 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
386 UINT i;
388 TRACE("(%p)\n", view);
390 for (i = 0; i < sv->num_rows; i++)
392 if (sv->streams[i])
394 if (sv->streams[i]->stream)
395 IStream_Release(sv->streams[i]->stream);
396 msi_free(sv->streams[i]);
400 msi_free(sv->streams);
401 msi_free(sv);
403 return ERROR_SUCCESS;
406 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
407 UINT val, UINT *row, MSIITERHANDLE *handle)
409 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
410 UINT index = PtrToUlong(*handle);
412 TRACE("(%p, %d, %d, %p, %p)\n", view, col, val, row, handle);
414 if (col == 0 || col > NUM_STREAMS_COLS)
415 return ERROR_INVALID_PARAMETER;
417 while (index < sv->num_rows)
419 if (sv->streams[index]->str_index == val)
421 *row = index;
422 break;
425 index++;
428 *handle = UlongToPtr(++index);
430 if (index > sv->num_rows)
431 return ERROR_NO_MORE_ITEMS;
433 return ERROR_SUCCESS;
436 static const MSIVIEWOPS streams_ops =
438 STREAMS_fetch_int,
439 STREAMS_fetch_stream,
440 STREAMS_get_row,
441 STREAMS_set_row,
442 STREAMS_insert_row,
443 STREAMS_delete_row,
444 STREAMS_execute,
445 STREAMS_close,
446 STREAMS_get_dimensions,
447 STREAMS_get_column_info,
448 STREAMS_modify,
449 STREAMS_delete,
450 STREAMS_find_matching_rows,
451 NULL,
452 NULL,
453 NULL,
454 NULL,
455 NULL,
456 NULL,
459 static INT add_streams_to_table(MSISTREAMSVIEW *sv)
461 IEnumSTATSTG *stgenum = NULL;
462 STATSTG stat;
463 STREAM *stream = NULL;
464 HRESULT hr;
465 UINT r, count = 0, size;
466 LPWSTR encname;
468 hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
469 if (FAILED(hr))
470 return -1;
472 sv->max_streams = 1;
473 sv->streams = msi_alloc_zero(sizeof(STREAM *));
474 if (!sv->streams)
475 return -1;
477 while (TRUE)
479 size = 0;
480 hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
481 if (FAILED(hr) || !size)
482 break;
484 if (stat.type != STGTY_STREAM)
486 CoTaskMemFree(stat.pwcsName);
487 continue;
490 /* table streams are not in the _Streams table */
491 if (*stat.pwcsName == 0x4840)
493 CoTaskMemFree(stat.pwcsName);
494 continue;
497 stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
498 if (!stream)
500 count = -1;
501 CoTaskMemFree(stat.pwcsName);
502 break;
505 /* these streams appear to be unencoded */
506 if (*stat.pwcsName == 0x0005)
508 r = msi_get_raw_stream(sv->db, stat.pwcsName, &stream->stream);
510 else
512 encname = encode_streamname(FALSE, stat.pwcsName);
513 r = msi_get_raw_stream(sv->db, encname, &stream->stream);
514 msi_free(encname);
516 CoTaskMemFree(stat.pwcsName);
518 if (r != ERROR_SUCCESS)
520 WARN("unable to get stream %u\n", r);
521 count = -1;
522 break;
525 if (!streams_set_table_size(sv, ++count))
527 count = -1;
528 break;
531 sv->streams[count - 1] = stream;
534 IEnumSTATSTG_Release(stgenum);
535 return count;
538 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
540 MSISTREAMSVIEW *sv;
541 INT rows;
543 TRACE("(%p, %p)\n", db, view);
545 sv = msi_alloc_zero( sizeof(MSISTREAMSVIEW) );
546 if (!sv)
547 return ERROR_FUNCTION_FAILED;
549 sv->view.ops = &streams_ops;
550 sv->db = db;
551 rows = add_streams_to_table(sv);
552 if (rows < 0)
554 msi_free( sv );
555 return ERROR_FUNCTION_FAILED;
557 sv->num_rows = rows;
559 *view = (MSIVIEW *)sv;
561 return ERROR_SUCCESS;