52c5f6107ffca90b55b489267cbef400338c3c59
[reactos.git] / dll / win32 / shlwapi / clist.c
1 /*
2 * SHLWAPI DataBlock List functions
3 *
4 * Copyright 2002 Jon Griffiths
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 "precomp.h"
22
23 /* dwSignature for contained DATABLOCK_HEADER items */
24 #define CLIST_ID_CONTAINER (~0U)
25
26 /*************************************************************************
27 * NextItem
28 *
29 * Internal helper: move a DataBlock pointer to the next item.
30 */
31 static inline LPDATABLOCK_HEADER NextItem(LPDBLIST lpList)
32 {
33 char* address = (char*)lpList;
34 address += lpList->cbSize;
35 return (LPDATABLOCK_HEADER)address;
36 }
37
38 /*************************************************************************
39 * @ [SHLWAPI.20]
40 *
41 * Insert a new item into a DataBlock list.
42 *
43 * PARAMS
44 * lppList [0] Pointer to the List
45 * lpNewItem [I] The new item to add to the list
46 *
47 * RETURNS
48 #ifndef __REACTOS__
49 * Success: S_OK. The item is added to the list.
50 * Failure: An HRESULT error code.
51 #else
52 * Success: TRUE. The item is added to the list.
53 * Failure: FALSE.
54 #endif
55 *
56 * NOTES
57 * If the size of the element to be inserted is less than the size of a
58 * DATABLOCK_HEADER node, or the Id for the item is CLIST_ID_CONTAINER,
59 * the call returns S_OK but does not actually add the element.
60 * See SHWriteDataBlockList.
61 */
62 #ifndef __REACTOS__
63 HRESULT
64 #else
65 BOOL
66 #endif
67 WINAPI SHAddDataBlock(LPDBLIST* lppList, const DATABLOCK_HEADER *lpNewItem)
68 {
69 LPDATABLOCK_HEADER lpInsertAt = NULL;
70 ULONG ulSize;
71
72 TRACE("(%p,%p)\n", lppList, lpNewItem);
73
74 if(!lppList || !lpNewItem )
75 #ifndef __REACTOS__
76 return E_INVALIDARG;
77 #else
78 return FALSE;
79 #endif
80
81 if (lpNewItem->cbSize < sizeof(DATABLOCK_HEADER) ||
82 lpNewItem->dwSignature == CLIST_ID_CONTAINER)
83 #ifndef __REACTOS__
84 return S_OK;
85 #else
86 return FALSE;
87 #endif
88
89 ulSize = lpNewItem->cbSize;
90
91 if(ulSize & 0x3)
92 {
93 /* Tune size to a ULONG boundary, add space for container element */
94 ulSize = ((ulSize + 0x3) & 0xFFFFFFFC) + sizeof(DATABLOCK_HEADER);
95 TRACE("Creating container item, new size = %d\n", ulSize);
96 }
97
98 if(!*lppList)
99 {
100 /* An empty list. Allocate space for terminal ulSize also */
101 *lppList = LocalAlloc(LMEM_ZEROINIT, ulSize + sizeof(ULONG));
102 lpInsertAt = *lppList;
103 }
104 else
105 {
106 /* Append to the end of the list */
107 ULONG ulTotalSize = 0;
108 LPDATABLOCK_HEADER lpIter = *lppList;
109
110 /* Iterate to the end of the list, calculating the total size */
111 while (lpIter->cbSize)
112 {
113 ulTotalSize += lpIter->cbSize;
114 lpIter = NextItem(lpIter);
115 }
116
117 /* Increase the size of the list */
118 lpIter = LocalReAlloc(*lppList, ulTotalSize + ulSize+sizeof(ULONG),
119 LMEM_ZEROINIT | LMEM_MOVEABLE);
120 if(lpIter)
121 {
122 *lppList = lpIter;
123 lpInsertAt = (LPDATABLOCK_HEADER)((char*)lpIter + ulTotalSize); /* At end */
124 }
125 }
126
127 if(lpInsertAt)
128 {
129 /* Copy in the new item */
130 LPDATABLOCK_HEADER lpDest = lpInsertAt;
131
132 if(ulSize != lpNewItem->cbSize)
133 {
134 lpInsertAt->cbSize = ulSize;
135 lpInsertAt->dwSignature = CLIST_ID_CONTAINER;
136 lpDest++;
137 }
138 memcpy(lpDest, lpNewItem, lpNewItem->cbSize);
139
140 /* Terminate the list */
141 lpInsertAt = NextItem(lpInsertAt);
142 lpInsertAt->cbSize = 0;
143
144 #ifndef __REACTOS__
145 return lpNewItem->cbSize;
146 #else
147 return TRUE;
148 #endif
149 }
150 #ifndef __REACTOS__
151 return S_OK;
152 #else
153 return FALSE;
154 #endif
155 }
156
157 /*************************************************************************
158 * @ [SHLWAPI.17]
159 *
160 * Write a DataBlock list to an IStream object.
161 *
162 * PARAMS
163 * lpStream [I] IStream object to write the list to
164 * lpList [I] List of items to write
165 *
166 * RETURNS
167 * Success: S_OK. The object is written to the stream.
168 * Failure: An HRESULT error code
169 *
170 * NOTES
171 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
172 * list structure (a "DataBlock List"), which may be stored and retrieved from
173 * an IStream object.
174 *
175 * The exposed API consists of:
176 *
177 * - SHWriteDataBlockList() - Write a DataBlock list to a stream,
178 * - SHReadDataBlockList() - Read and create a list from a stream,
179 * - SHFreeDataBlockList() - Free a list,
180 * - SHAddDataBlock() - Insert a new item into a list,
181 * - SHRemoveDataBlock() - Remove an item from a list,
182 * - SHFindDataBlock() - Find an item in a list.
183 *
184 * The DataBlock list is stored packed into a memory array. Each element has a
185 * size and an associated ID. Elements must be less than 64k if the list is
186 * to be subsequently read from a stream.
187 *
188 * Elements are aligned on DWORD boundaries. If an elements data size is not
189 * a DWORD size multiple, the element is wrapped by inserting a surrounding
190 * element with an Id of 0xFFFFFFFF, and size sufficient to pad to a DWORD boundary.
191 *
192 * These functions are slow for large objects and long lists.
193 */
194 HRESULT WINAPI SHWriteDataBlockList(IStream* lpStream, LPDBLIST lpList)
195 {
196 ULONG ulSize;
197 HRESULT hRet = S_OK;
198
199 TRACE("(%p,%p)\n", lpStream, lpList);
200
201 if(lpList)
202 {
203 while (lpList->cbSize)
204 {
205 LPDATABLOCK_HEADER lpItem = lpList;
206
207 if(lpList->dwSignature == CLIST_ID_CONTAINER)
208 lpItem++;
209
210 hRet = IStream_Write(lpStream,lpItem,lpItem->cbSize,&ulSize);
211 if (FAILED(hRet))
212 return hRet;
213
214 if(lpItem->cbSize != ulSize)
215 return STG_E_MEDIUMFULL;
216
217 lpList = NextItem(lpList);
218 }
219 }
220
221 if(SUCCEEDED(hRet))
222 {
223 ULONG ulDummy;
224 ulSize = 0;
225
226 /* Write a terminating list entry with zero size */
227 hRet = IStream_Write(lpStream, &ulSize,sizeof(ulSize),&ulDummy);
228 }
229
230 return hRet;
231 }
232
233 /*************************************************************************
234 * @ [SHLWAPI.18]
235 *
236 * Read and create a DataBlock list from an IStream object.
237 *
238 * PARAMS
239 * lpStream [I] Stream to read the list from
240 * lppList [0] Pointer to receive the new List
241 *
242 * RETURNS
243 * Success: S_OK
244 * Failure: An HRESULT error code
245 *
246 * NOTES
247 * When read from a file, list objects are limited in size to 64k.
248 * See SHWriteDataBlockList.
249 */
250 HRESULT WINAPI SHReadDataBlockList(IStream* lpStream, LPDBLIST* lppList)
251 {
252 DATABLOCK_HEADER bBuff[128]; /* Temporary storage for new list item */
253 ULONG ulBuffSize = sizeof(bBuff);
254 LPDATABLOCK_HEADER pItem = bBuff;
255 ULONG ulRead, ulSize;
256 HRESULT hRet = S_OK;
257
258 TRACE("(%p,%p)\n", lpStream, lppList);
259
260 if(*lppList)
261 {
262 /* Free any existing list */
263 LocalFree(*lppList);
264 *lppList = NULL;
265 }
266
267 do
268 {
269 /* Read the size of the next item */
270 hRet = IStream_Read(lpStream, &ulSize,sizeof(ulSize),&ulRead);
271
272 if(FAILED(hRet) || ulRead != sizeof(ulSize) || !ulSize)
273 break; /* Read failed or read zero size (the end of the list) */
274
275 if(ulSize > 0xFFFF)
276 {
277 LARGE_INTEGER liZero;
278 ULARGE_INTEGER ulPos;
279
280 liZero.QuadPart = 0;
281
282 /* Back the stream up; this object is too big for the list */
283 if(SUCCEEDED(IStream_Seek(lpStream, liZero, STREAM_SEEK_CUR, &ulPos)))
284 {
285 liZero.QuadPart = ulPos.QuadPart - sizeof(ULONG);
286 IStream_Seek(lpStream, liZero, STREAM_SEEK_SET, NULL);
287 }
288 break;
289 }
290 else if (ulSize >= sizeof(DATABLOCK_HEADER))
291 {
292 /* Add this new item to the list */
293 if(ulSize > ulBuffSize)
294 {
295 /* We need more buffer space, allocate it */
296 LPDATABLOCK_HEADER lpTemp;
297
298 if (pItem == bBuff)
299 lpTemp = LocalAlloc(LMEM_ZEROINIT, ulSize);
300 else
301 lpTemp = LocalReAlloc(pItem, ulSize, LMEM_ZEROINIT|LMEM_MOVEABLE);
302
303 if(!lpTemp)
304 {
305 hRet = E_OUTOFMEMORY;
306 break;
307 }
308 ulBuffSize = ulSize;
309 pItem = lpTemp;
310 }
311
312 pItem->cbSize = ulSize;
313 ulSize -= sizeof(pItem->cbSize); /* already read this member */
314
315 /* Read the item Id and data */
316 hRet = IStream_Read(lpStream, &pItem->dwSignature, ulSize, &ulRead);
317
318 if(FAILED(hRet) || ulRead != ulSize)
319 break;
320
321 SHAddDataBlock(lppList, pItem); /* Insert Item */
322 }
323 } while(1);
324
325 /* If we allocated space, free it */
326 if(pItem != bBuff)
327 LocalFree(pItem);
328
329 return hRet;
330 }
331
332 /*************************************************************************
333 * @ [SHLWAPI.19]
334 *
335 * Free a DataBlock list.
336 *
337 * PARAMS
338 * lpList [I] List to free
339 *
340 * RETURNS
341 * Nothing.
342 *
343 * NOTES
344 * See SHWriteDataBlockList.
345 */
346 VOID WINAPI SHFreeDataBlockList(LPDBLIST lpList)
347 {
348 TRACE("(%p)\n", lpList);
349
350 if (lpList)
351 LocalFree(lpList);
352 }
353
354 /*************************************************************************
355 * @ [SHLWAPI.21]
356 *
357 * Remove an item from a DataBlock list.
358 *
359 * PARAMS
360 * lppList [O] List to remove the item from
361 * dwSignature [I] Id of item to remove
362 *
363 * RETURNS
364 * Success: TRUE.
365 * Failure: FALSE, If any parameters are invalid, or the item was not found.
366 *
367 * NOTES
368 * See SHWriteDataBlockList.
369 */
370 BOOL WINAPI SHRemoveDataBlock(LPDBLIST* lppList, DWORD dwSignature)
371 {
372 #ifndef __REACTOS__
373 LPDATABLOCK_HEADER lpList = 0;
374 #else
375 LPDATABLOCK_HEADER lpList = NULL;
376 #endif
377 LPDATABLOCK_HEADER lpItem = NULL;
378 LPDATABLOCK_HEADER lpNext;
379 ULONG ulNewSize;
380
381 TRACE("(%p,%d)\n", lppList, dwSignature);
382
383 if(lppList && (lpList = *lppList))
384 {
385 /* Search for item in list */
386 while (lpList->cbSize)
387 {
388 if(lpList->dwSignature == dwSignature ||
389 (lpList->dwSignature == CLIST_ID_CONTAINER && lpList[1].dwSignature == dwSignature))
390 {
391 lpItem = lpList; /* Found */
392 break;
393 }
394 lpList = NextItem(lpList);
395 }
396 }
397
398 if(!lpItem)
399 return FALSE;
400
401 lpList = lpNext = NextItem(lpItem);
402
403 /* Locate the end of the list */
404 while (lpList->cbSize)
405 lpList = NextItem(lpList);
406
407 /* Resize the list */
408 ulNewSize = LocalSize(*lppList) - lpItem->cbSize;
409
410 /* Copy following elements over lpItem */
411 memmove(lpItem, lpNext, (char *)lpList - (char *)lpNext + sizeof(ULONG));
412
413 if(ulNewSize <= sizeof(ULONG))
414 {
415 LocalFree(*lppList);
416 *lppList = NULL; /* Removed the last element */
417 }
418 else
419 {
420 lpList = LocalReAlloc(*lppList, ulNewSize, LMEM_ZEROINIT|LMEM_MOVEABLE);
421 if(lpList)
422 *lppList = lpList;
423 }
424 return TRUE;
425 }
426
427 /*************************************************************************
428 * @ [SHLWAPI.22]
429 *
430 * Find an item in a DataBlock list.
431 *
432 * PARAMS
433 * lpList [I] List to search
434 * dwSignature [I] Id of item to find
435 *
436 * RETURNS
437 * Success: A pointer to the list item found
438 * Failure: NULL
439 *
440 * NOTES
441 * See SHWriteDataBlockList.
442 */
443 DATABLOCK_HEADER* WINAPI SHFindDataBlock(LPDBLIST lpList, DWORD dwSignature)
444 {
445 TRACE("(%p,%d)\n", lpList, dwSignature);
446
447 if(lpList)
448 {
449 while(lpList->cbSize)
450 {
451 if(lpList->dwSignature == dwSignature)
452 return lpList; /* Matched */
453 else if(lpList->dwSignature == CLIST_ID_CONTAINER && lpList[1].dwSignature == dwSignature)
454 return lpList + 1; /* Contained item matches */
455
456 lpList = NextItem(lpList);
457 }
458 }
459 return NULL;
460 }