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