includes: Fix alignment for 64-bits
[wine/wine64.git] / dlls / msi / streams.c
blobe263105798e8b6d82d2375015c44637260d24c0d
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 LPWSTR name;
45 IStream *stream;
46 } STREAM;
48 typedef struct tagMSISTREAMSVIEW
50 MSIVIEW view;
51 MSIDATABASE *db;
52 STREAM **streams;
53 UINT max_streams;
54 UINT num_rows;
55 UINT row_size;
56 } MSISTREAMSVIEW;
58 static BOOL streams_set_table_size(MSISTREAMSVIEW *sv, UINT size)
60 if (size >= sv->max_streams)
62 sv->max_streams *= 2;
63 sv->streams = msi_realloc(sv->streams, sv->max_streams * sizeof(STREAM *));
64 if (!sv->streams)
65 return FALSE;
68 return TRUE;
71 static STREAM *create_stream(MSISTREAMSVIEW *sv, LPWSTR name, BOOL encoded, IStream *stm)
73 STREAM *stream;
74 WCHAR decoded[MAX_STREAM_NAME_LEN];
75 LPWSTR ptr = name;
77 stream = msi_alloc(sizeof(STREAM));
78 if (!stream)
79 return NULL;
81 if (encoded)
83 decode_streamname(name, decoded);
84 ptr = decoded;
85 TRACE("stream -> %s %s\n", debugstr_w(name), debugstr_w(decoded));
88 stream->name = strdupW(ptr);
89 if (!stream->name)
91 msi_free(stream);
92 return NULL;
95 stream->str_index = msi_addstringW(sv->db->strings, 0, stream->name, -1, 1, StringNonPersistent);
96 stream->stream = stm;
97 return stream;
100 static UINT STREAMS_fetch_int(struct tagMSIVIEW *view, UINT row, UINT col, UINT *val)
102 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
104 TRACE("(%p, %d, %d, %p)\n", view, row, col, val);
106 if (col != 1)
107 return ERROR_INVALID_PARAMETER;
109 if (row >= sv->num_rows)
110 return ERROR_NO_MORE_ITEMS;
112 *val = sv->streams[row]->str_index;
114 return ERROR_SUCCESS;
117 static UINT STREAMS_fetch_stream(struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
119 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
121 TRACE("(%p, %d, %d, %p)\n", view, row, col, stm);
123 if (row >= sv->num_rows)
124 return ERROR_FUNCTION_FAILED;
126 IStream_AddRef(sv->streams[row]->stream);
127 *stm = sv->streams[row]->stream;
129 return ERROR_SUCCESS;
132 static UINT STREAMS_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
134 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
136 FIXME("%p %d %p\n", sv, row, rec);
138 return ERROR_CALL_NOT_IMPLEMENTED;
141 static UINT STREAMS_set_row(struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask)
143 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
144 STREAM *stream;
145 IStream *stm;
146 STATSTG stat;
147 LPWSTR name = NULL;
148 USHORT *data = NULL;
149 HRESULT hr;
150 ULONG count;
151 UINT r = ERROR_FUNCTION_FAILED;
153 TRACE("(%p, %p)\n", view, rec);
155 if (row > sv->num_rows)
156 return ERROR_FUNCTION_FAILED;
158 r = MSI_RecordGetIStream(rec, 2, &stm);
159 if (r != ERROR_SUCCESS)
160 return r;
162 hr = IStream_Stat(stm, &stat, STATFLAG_NONAME);
163 if (FAILED(hr))
165 WARN("failed to stat stream: %08x\n", hr);
166 goto done;
169 if (stat.cbSize.QuadPart >> 32)
170 goto done;
172 data = msi_alloc(stat.cbSize.QuadPart);
173 if (!data)
174 goto done;
176 hr = IStream_Read(stm, data, stat.cbSize.QuadPart, &count);
177 if (FAILED(hr) || count != stat.cbSize.QuadPart)
179 WARN("failed to read stream: %08x\n", hr);
180 goto done;
183 name = strdupW(MSI_RecordGetString(rec, 1));
184 if (!name)
185 goto done;
187 r = write_stream_data(sv->db->storage, name, data, count, FALSE);
188 if (r != ERROR_SUCCESS)
190 WARN("failed to write stream data: %d\n", r);
191 goto done;
194 stream = create_stream(sv, name, FALSE, NULL);
195 if (!stream)
196 goto done;
198 IStorage_OpenStream(sv->db->storage, name, 0,
199 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
201 sv->streams[row] = stream;
203 done:
204 msi_free(name);
205 msi_free(data);
207 IStream_Release(stm);
209 return r;
212 static UINT STREAMS_insert_row(struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary)
214 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
216 if (!streams_set_table_size(sv, ++sv->num_rows))
217 return ERROR_FUNCTION_FAILED;
219 return STREAMS_set_row(view, sv->num_rows - 1, rec, 0);
222 static UINT STREAMS_delete_row(struct tagMSIVIEW *view, UINT row)
224 FIXME("(%p %d): stub!\n", view, row);
225 return ERROR_SUCCESS;
228 static UINT STREAMS_execute(struct tagMSIVIEW *view, MSIRECORD *record)
230 TRACE("(%p, %p)\n", view, record);
231 return ERROR_SUCCESS;
234 static UINT STREAMS_close(struct tagMSIVIEW *view)
236 TRACE("(%p)\n", view);
237 return ERROR_SUCCESS;
240 static UINT STREAMS_get_dimensions(struct tagMSIVIEW *view, UINT *rows, UINT *cols)
242 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
244 TRACE("(%p, %p, %p)\n", view, rows, cols);
246 if (cols) *cols = NUM_STREAMS_COLS;
247 if (rows) *rows = sv->num_rows;
249 return ERROR_SUCCESS;
252 static UINT STREAMS_get_column_info(struct tagMSIVIEW *view,
253 UINT n, LPWSTR *name, UINT *type)
255 LPCWSTR name_ptr = NULL;
257 static const WCHAR Name[] = {'N','a','m','e',0};
258 static const WCHAR Data[] = {'D','a','t','a',0};
260 TRACE("(%p, %d, %p, %p)\n", view, n, name, type);
262 if (n == 0 || n > NUM_STREAMS_COLS)
263 return ERROR_INVALID_PARAMETER;
265 switch (n)
267 case 1:
268 name_ptr = Name;
269 if (type) *type = MSITYPE_STRING | MAX_STREAM_NAME_LEN;
270 break;
272 case 2:
273 name_ptr = Data;
274 if (type) *type = MSITYPE_STRING | MSITYPE_VALID | MSITYPE_NULLABLE;
275 break;
278 if (name)
280 *name = strdupW(name_ptr);
281 if (!*name) return ERROR_FUNCTION_FAILED;
284 return ERROR_SUCCESS;
287 static UINT streams_find_row(MSISTREAMSVIEW *sv, MSIRECORD *rec, UINT *row)
289 LPCWSTR str;
290 UINT i, id, data;
292 str = MSI_RecordGetString(rec, 1);
293 msi_string2idW(sv->db->strings, str, &id);
295 for (i = 0; i < sv->num_rows; i++)
297 STREAMS_fetch_int(&sv->view, i, 1, &data);
299 if (data == id)
301 *row = i;
302 return ERROR_SUCCESS;
306 return ERROR_FUNCTION_FAILED;
309 static UINT streams_modify_update(struct tagMSIVIEW *view, MSIRECORD *rec)
311 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
312 UINT r, row;
314 r = streams_find_row(sv, rec, &row);
315 if (r != ERROR_SUCCESS)
316 return ERROR_FUNCTION_FAILED;
318 return STREAMS_set_row(view, row, rec, 0);
321 static UINT streams_modify_assign(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 streams_modify_update(view, rec);
330 return STREAMS_insert_row(view, rec, FALSE);
333 static UINT STREAMS_modify(struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIRECORD *rec, UINT row)
335 UINT r;
337 TRACE("%p %d %p\n", view, eModifyMode, rec);
339 switch (eModifyMode)
341 case MSIMODIFY_ASSIGN:
342 r = streams_modify_assign(view, rec);
343 break;
345 case MSIMODIFY_INSERT:
346 r = STREAMS_insert_row(view, rec, FALSE);
347 break;
349 case MSIMODIFY_UPDATE:
350 r = streams_modify_update(view, rec);
351 break;
353 case MSIMODIFY_VALIDATE_NEW:
354 case MSIMODIFY_INSERT_TEMPORARY:
355 case MSIMODIFY_REFRESH:
356 case MSIMODIFY_REPLACE:
357 case MSIMODIFY_MERGE:
358 case MSIMODIFY_DELETE:
359 case MSIMODIFY_VALIDATE:
360 case MSIMODIFY_VALIDATE_FIELD:
361 case MSIMODIFY_VALIDATE_DELETE:
362 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
363 r = ERROR_CALL_NOT_IMPLEMENTED;
364 break;
366 default:
367 r = ERROR_INVALID_DATA;
370 return r;
373 static UINT STREAMS_delete(struct tagMSIVIEW *view)
375 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
376 UINT i;
378 TRACE("(%p)\n", view);
380 for (i = 0; i < sv->num_rows; i++)
382 if (sv->streams[i]->stream)
383 IStream_Release(sv->streams[i]->stream);
384 msi_free(sv->streams[i]);
387 msi_free(sv->streams);
389 return ERROR_SUCCESS;
392 static UINT STREAMS_find_matching_rows(struct tagMSIVIEW *view, UINT col,
393 UINT val, UINT *row, MSIITERHANDLE *handle)
395 MSISTREAMSVIEW *sv = (MSISTREAMSVIEW *)view;
396 UINT index = (UINT)*handle;
398 TRACE("(%d, %d): %d\n", *row, col, val);
400 if (col == 0 || col > NUM_STREAMS_COLS)
401 return ERROR_INVALID_PARAMETER;
403 while (index < sv->num_rows)
405 if (sv->streams[index]->str_index == val)
407 *row = index;
408 break;
411 index++;
414 *handle = (MSIITERHANDLE)++index;
415 if (index >= sv->num_rows)
416 return ERROR_NO_MORE_ITEMS;
418 return ERROR_SUCCESS;
421 static const MSIVIEWOPS streams_ops =
423 STREAMS_fetch_int,
424 STREAMS_fetch_stream,
425 STREAMS_get_row,
426 STREAMS_set_row,
427 STREAMS_insert_row,
428 STREAMS_delete_row,
429 STREAMS_execute,
430 STREAMS_close,
431 STREAMS_get_dimensions,
432 STREAMS_get_column_info,
433 STREAMS_modify,
434 STREAMS_delete,
435 STREAMS_find_matching_rows,
436 NULL,
437 NULL,
438 NULL,
439 NULL,
440 NULL,
441 NULL,
444 static INT add_streams_to_table(MSISTREAMSVIEW *sv)
446 IEnumSTATSTG *stgenum = NULL;
447 STATSTG stat;
448 STREAM *stream = NULL;
449 HRESULT hr;
450 UINT count = 0, size;
452 hr = IStorage_EnumElements(sv->db->storage, 0, NULL, 0, &stgenum);
453 if (FAILED(hr))
454 return -1;
456 sv->max_streams = 1;
457 sv->streams = msi_alloc(sizeof(STREAM *));
458 if (!sv->streams)
459 return -1;
461 while (TRUE)
463 size = 0;
464 hr = IEnumSTATSTG_Next(stgenum, 1, &stat, &size);
465 if (FAILED(hr) || !size)
466 break;
468 if (stat.type != STGTY_STREAM)
469 continue;
471 /* table streams are not in the _Streams table */
472 if (*stat.pwcsName == 0x4840)
474 CoTaskMemFree(stat.pwcsName);
475 continue;
478 stream = create_stream(sv, stat.pwcsName, TRUE, NULL);
479 if (!stream)
481 count = -1;
482 CoTaskMemFree(stat.pwcsName);
483 break;
486 IStorage_OpenStream(sv->db->storage, stat.pwcsName, 0,
487 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream->stream);
488 CoTaskMemFree(stat.pwcsName);
490 if (!streams_set_table_size(sv, ++count))
492 count = -1;
493 break;
496 sv->streams[count - 1] = stream;
499 IEnumSTATSTG_Release(stgenum);
500 return count;
503 UINT STREAMS_CreateView(MSIDATABASE *db, MSIVIEW **view)
505 MSISTREAMSVIEW *sv;
506 INT rows;
508 TRACE("(%p, %p)\n", db, view);
510 sv = msi_alloc(sizeof(MSISTREAMSVIEW));
511 if (!sv)
512 return ERROR_FUNCTION_FAILED;
514 sv->view.ops = &streams_ops;
515 sv->db = db;
516 rows = add_streams_to_table(sv);
517 if (rows < 0)
518 return ERROR_FUNCTION_FAILED;
519 sv->num_rows = rows;
521 *view = (MSIVIEW *)sv;
523 return ERROR_SUCCESS;