msi: Remove unused and redundant name field from STREAM structure.
[wine/multimedia.git] / dlls / msi / streams.c
blob21357cd21485b328af2f3d074b2b9530204271e6
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"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38 #define NUM_STREAMS_COLS 2
39 #define MAX_STREAM_NAME_LEN 62
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, LPWSTR name, BOOL encoded, IStream *stm)
72 STREAM *stream;
73 WCHAR decoded[MAX_STREAM_NAME_LEN];
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_addstringW(sv->db->strings, 0, name, -1, 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 FIXME("%p %d %p\n", sv, row, rec);
129 return ERROR_CALL_NOT_IMPLEMENTED;
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 name = NULL;
139 USHORT *data = NULL;
140 HRESULT hr;
141 ULONG count;
142 UINT r = ERROR_FUNCTION_FAILED;
144 TRACE("(%p, %p)\n", view, rec);
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)
161 goto done;
163 data = msi_alloc(stat.cbSize.QuadPart);
164 if (!data)
165 goto done;
167 hr = IStream_Read(stm, data, stat.cbSize.QuadPart, &count);
168 if (FAILED(hr) || count != stat.cbSize.QuadPart)
170 WARN("failed to read stream: %08x\n", hr);
171 goto done;
174 name = strdupW(MSI_RecordGetString(rec, 1));
175 if (!name)
176 goto done;
178 r = write_stream_data(sv->db->storage, name, data, count, FALSE);
179 if (r != ERROR_SUCCESS)
181 WARN("failed to write stream data: %d\n", r);
182 goto done;
185 stream = create_stream(sv, name, FALSE, NULL);
186 if (!stream)
187 goto done;
189 IStorage_OpenStream(sv->db->storage, name, 0,
190 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
192 sv->streams[row] = stream;
194 done:
195 msi_free(name);
196 msi_free(data);
198 IStream_Release(stm);
200 return r;
203 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary)
205 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
207 if (!streams_set_table_size(sv, ++sv->num_rows))
208 return ERROR_FUNCTION_FAILED;
210 if (row == -1)
211 row = sv->num_rows - 1;
213 /* FIXME have to readjust rows */
215 return STREAMS_set_row(view, row, rec, 0);
218 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
220 FIXME("(%p %d): stub!\n", view, row);
221 return ERROR_SUCCESS;
224 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
226 TRACE("(%p, %p)\n", view, record);
227 return ERROR_SUCCESS;
230 static UINT STREAMS_close(struct tagMSIVIEW *view)
232 TRACE("(%p)\n", view);
233 return ERROR_SUCCESS;
236 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
238 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
240 TRACE("(%p, %p, %p)\n", view, rows, cols);
242 if (cols) *cols = NUM_STREAMS_COLS;
243 if (rows) *rows = sv->num_rows;
245 return ERROR_SUCCESS;
248 static UINT STREAMS_get_column_info(struct tagMSIVIEW *view, UINT n,
249 LPWSTR *name, UINT *type, BOOL *temporary,
250 LPWSTR *table_name)
252 LPCWSTR name_ptr = NULL;
254 static const WCHAR Name[] = {'N','a','m','e',0};
255 static const WCHAR Data[] = {'D','a','t','a',0};
256 static const WCHAR _Streams[] = {'_','S','t','r','e','a','m','s',0};
258 TRACE("(%p, %d, %p, %p, %p, %p)\n", view, n, name, type, temporary,
259 table_name);
261 if (n == 0 || n > NUM_STREAMS_COLS)
262 return ERROR_INVALID_PARAMETER;
264 switch (n)
266 case 1:
267 name_ptr = Name;
268 if (type) *type = MSITYPE_STRING | MAX_STREAM_NAME_LEN;
269 break;
271 case 2:
272 name_ptr = Data;
273 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
274 break;
277 if (name)
279 *name = strdupW(name_ptr);
280 if (!*name) return ERROR_FUNCTION_FAILED;
283 if (table_name)
285 *table_name = strdupW(_Streams);
286 if (!*table_name)
288 msi_free(name);
289 return ERROR_FUNCTION_FAILED;
293 if (temporary)
294 *temporary = FALSE;
296 return ERROR_SUCCESS;
299 static UINT streams_find_row(MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row)
301 LPCWSTR str;
302 UINT i, id, data;
304 str = MSI_RecordGetString(rec, 1);
305 msi_string2idW(sv->db->strings, str, &id);
307 for (i = 0; i < sv->num_rows; i++)
309 STREAMS_fetch_int(&sv->view, i, 1, &data);
311 if (data == id)
313 *row = i;
314 return ERROR_SUCCESS;
318 return ERROR_FUNCTION_FAILED;
321 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
323 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
324 UINT r, row;
326 r = streams_find_row(sv, rec, &row);
327 if (r != ERROR_SUCCESS)
328 return ERROR_FUNCTION_FAILED;
330 return STREAMS_set_row(view, row, rec, 0);
333 static UINT streams_modify_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
335 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
336 UINT r, row;
338 r = streams_find_row(sv, rec, &row);
339 if (r == ERROR_SUCCESS)
340 return streams_modify_update(view, rec);
342 return STREAMS_insert_row(view, rec, -1, FALSE);
345 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
347 UINT r;
349 TRACE("%p %d %p\n", view, eModifyMode, rec);
351 switch (eModifyMode)
353 case MSIMODIFY_ASSIGN:
354 r = streams_modify_assign(view, rec);
355 break;
357 case MSIMODIFY_INSERT:
358 r = STREAMS_insert_row(view, rec, -1, FALSE);
359 break;
361 case MSIMODIFY_UPDATE:
362 r = streams_modify_update(view, rec);
363 break;
365 case MSIMODIFY_VALIDATE_NEW:
366 case MSIMODIFY_INSERT_TEMPORARY:
367 case MSIMODIFY_REFRESH:
368 case MSIMODIFY_REPLACE:
369 case MSIMODIFY_MERGE:
370 case MSIMODIFY_DELETE:
371 case MSIMODIFY_VALIDATE:
372 case MSIMODIFY_VALIDATE_FIELD:
373 case MSIMODIFY_VALIDATE_DELETE:
374 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
375 r = ERROR_CALL_NOT_IMPLEMENTED;
376 break;
378 default:
379 r = ERROR_INVALID_DATA;
382 return r;
385 static UINT STREAMS_delete(struct tagMSIVIEW *view)
387 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
388 UINT i;
390 TRACE("(%p)\n", view);
392 for (i = 0; i < sv->num_rows; i++)
394 if (sv->streams[i])
396 if (sv->streams[i]->stream)
397 IStream_Release(sv->streams[i]->stream);
398 msi_free(sv->streams[i]);
402 msi_free(sv->streams);
403 msi_free(sv);
405 return ERROR_SUCCESS;
408 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
409 UINT val, UINT *row, MSIITERHANDLE *handle)
411 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
412 UINT index = PtrToUlong(*handle);
414 TRACE("(%d, %d): %d\n", *row, col, val);
416 if (col == 0 || col > NUM_STREAMS_COLS)
417 return ERROR_INVALID_PARAMETER;
419 while (index < sv->num_rows)
421 if (sv->streams[index]->str_index == val)
423 *row = index;
424 break;
427 index++;
430 *handle = UlongToPtr(++index);
431 if (index >= sv->num_rows)
432 return ERROR_NO_MORE_ITEMS;
434 return ERROR_SUCCESS;
437 static const MSIVIEWOPS streams_ops =
439 STREAMS_fetch_int,
440 STREAMS_fetch_stream,
441 STREAMS_get_row,
442 STREAMS_set_row,
443 STREAMS_insert_row,
444 STREAMS_delete_row,
445 STREAMS_execute,
446 STREAMS_close,
447 STREAMS_get_dimensions,
448 STREAMS_get_column_info,
449 STREAMS_modify,
450 STREAMS_delete,
451 STREAMS_find_matching_rows,
452 NULL,
453 NULL,
454 NULL,
455 NULL,
456 NULL,
457 NULL,
460 static INT add_streams_to_table(MSISTREAMSVIEW *sv)
462 IEnumSTATSTG *stgenum = NULL;
463 STATSTG stat;
464 STREAM *stream = NULL;
465 HRESULT hr;
466 UINT count = 0, size;
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)
485 continue;
487 /* table streams are not in the _Streams table */
488 if (*stat.pwcsName == 0x4840)
490 CoTaskMemFree(stat.pwcsName);
491 continue;
494 stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
495 if (!stream)
497 count = -1;
498 CoTaskMemFree(stat.pwcsName);
499 break;
502 IStorage_OpenStream(sv->db->storage, stat.pwcsName, 0,
503 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
504 CoTaskMemFree(stat.pwcsName);
506 if (!streams_set_table_size(sv, ++count))
508 count = -1;
509 break;
512 sv->streams[count - 1] = stream;
515 IEnumSTATSTG_Release(stgenum);
516 return count;
519 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
521 MSISTREAMSVIEW *sv;
522 INT rows;
524 TRACE("(%p, %p)\n", db, view);
526 sv = msi_alloc(sizeof(MSISTREAMSVIEW));
527 if (!sv)
528 return ERROR_FUNCTION_FAILED;
530 sv->view.ops = &streams_ops;
531 sv->db = db;
532 rows = add_streams_to_table(sv);
533 if (rows < 0)
535 msi_free( sv );
536 return ERROR_FUNCTION_FAILED;
538 sv->num_rows = rows;
540 *view = (MSIVIEW *)sv;
542 return ERROR_SUCCESS;