shell32: Have SHELL_ArgifyW respect the length of the buffer passed in and report...
[wine.git] / dlls / shlwapi / clist.c
blob274dafa30f36c261e8a3a493b3c9595b9808285c
1 /*
2 * SHLWAPI DataBlock List functions
4 * Copyright 2002 Jon Griffiths
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
20 #include <stdarg.h>
21 #include <string.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
32 /* DataBlock list element (ordinals 17-22) */
33 typedef struct tagSHLWAPI_CLIST
35 ULONG ulSize; /* Size of this list element and its data */
36 ULONG ulId; /* If 0xFFFFFFFF, The real element follows */
37 /* Item data (or a contained SHLWAPI_CLIST) follows... */
38 } SHLWAPI_CLIST, *LPSHLWAPI_CLIST;
40 typedef const SHLWAPI_CLIST* LPCSHLWAPI_CLIST;
42 /* ulId for contained SHLWAPI_CLIST items */
43 #define CLIST_ID_CONTAINER (~0U)
45 HRESULT WINAPI SHAddDataBlock(LPSHLWAPI_CLIST*,LPCSHLWAPI_CLIST);
47 /*************************************************************************
48 * NextItem
50 * Internal helper: move a DataBlock pointer to the next item.
52 inline static LPSHLWAPI_CLIST NextItem(LPCSHLWAPI_CLIST lpList)
54 const char* address = (const char*)lpList;
55 address += lpList->ulSize;
56 return (LPSHLWAPI_CLIST)address;
59 /*************************************************************************
60 * @ [SHLWAPI.17]
62 * Write a DataBlock list to an IStream object.
64 * PARAMS
65 * lpStream [I] IStream object to write the list to
66 * lpList [I] List of items to write
68 * RETURNS
69 * Success: S_OK. The object is written to the stream.
70 * Failure: An HRESULT error code
72 * NOTES
73 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
74 * list structure (a "DataBlock List"), which may be stored and retrieved from
75 * an IStream object.
77 * The exposed API consists of:
79 * - SHWriteDataBlockList() - Write a DataBlock list to a stream,
80 * - SHReadDataBlockList() - Read and create a list from a stream,
81 * - SHFreeDataBlockList() - Free a list,
82 * - SHAddDataBlock() - Insert a new item into a list,
83 * - SHRemoveDataBlock() - Remove an item from a list,
84 * - SHFindDataBlock() - Find an item in a list.
86 * The DataBlock list is stored packed into a memory array. Each element has a
87 * size and an associated ID. Elements must be less than 64k if the list is
88 * to be subsequently read from a stream.
90 * Elements are aligned on DWORD boundaries. If an elements data size is not
91 * a DWORD size multiple, the element is wrapped by inserting a surrounding
92 * element with an Id of 0xFFFFFFFF, and size sufficient to pad to a DWORD boundary.
94 * These functions are slow for large objects and long lists.
96 HRESULT WINAPI SHWriteDataBlockList(IStream* lpStream, LPSHLWAPI_CLIST lpList)
98 ULONG ulSize;
99 HRESULT hRet = E_FAIL;
101 TRACE("(%p,%p)\n", lpStream, lpList);
103 if(lpList)
105 while (lpList->ulSize)
107 LPSHLWAPI_CLIST lpItem = lpList;
109 if(lpList->ulId == CLIST_ID_CONTAINER)
110 lpItem++;
112 hRet = IStream_Write(lpStream,lpItem,lpItem->ulSize,&ulSize);
113 if (FAILED(hRet))
114 return hRet;
116 if(lpItem->ulSize != ulSize)
117 return STG_E_MEDIUMFULL;
119 lpList = NextItem(lpList);
123 if(SUCCEEDED(hRet))
125 ULONG ulDummy;
126 ulSize = 0;
128 /* Write a terminating list entry with zero size */
129 hRet = IStream_Write(lpStream, &ulSize,sizeof(ulSize),&ulDummy);
132 return hRet;
135 /*************************************************************************
136 * @ [SHLWAPI.18]
138 * Read and create a DataBlock list from an IStream object.
140 * PARAMS
141 * lpStream [I] Stream to read the list from
142 * lppList [0] Pointer to receive the new List
144 * RETURNS
145 * Success: S_OK
146 * Failure: An HRESULT error code
148 * NOTES
149 * When read from a file, list objects are limited in size to 64k.
150 * See SHWriteDataBlockList.
152 HRESULT WINAPI SHReadDataBlockList(IStream* lpStream, LPSHLWAPI_CLIST* lppList)
154 SHLWAPI_CLIST bBuff[128]; /* Temporary storage for new list item */
155 ULONG ulBuffSize = sizeof(bBuff);
156 LPSHLWAPI_CLIST pItem = bBuff;
157 ULONG ulRead, ulSize;
158 HRESULT hRet = S_OK;
160 TRACE("(%p,%p)\n", lpStream, lppList);
162 if(*lppList)
164 /* Free any existing list */
165 LocalFree((HLOCAL)*lppList);
166 *lppList = NULL;
171 /* Read the size of the next item */
172 hRet = IStream_Read(lpStream, &ulSize,sizeof(ulSize),&ulRead);
174 if(FAILED(hRet) || ulRead != sizeof(ulSize) || !ulSize)
175 break; /* Read failed or read zero size (the end of the list) */
177 if(ulSize > 0xFFFF)
179 LARGE_INTEGER liZero;
180 ULARGE_INTEGER ulPos;
182 liZero.QuadPart = 0;
184 /* Back the stream up; this object is too big for the list */
185 if(SUCCEEDED(IStream_Seek(lpStream, liZero, STREAM_SEEK_CUR, &ulPos)))
187 liZero.QuadPart = ulPos.QuadPart - sizeof(ULONG);
188 IStream_Seek(lpStream, liZero, STREAM_SEEK_SET, NULL);
190 break;
192 else if (ulSize >= sizeof(SHLWAPI_CLIST))
194 /* Add this new item to the list */
195 if(ulSize > ulBuffSize)
197 /* We need more buffer space, allocate it */
198 LPSHLWAPI_CLIST lpTemp;
200 if (pItem == bBuff)
201 lpTemp = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT, ulSize);
202 else
203 lpTemp = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)pItem, ulSize,
204 LMEM_ZEROINIT|LMEM_MOVEABLE);
206 if(!lpTemp)
208 hRet = E_OUTOFMEMORY;
209 break;
211 ulBuffSize = ulSize;
212 pItem = lpTemp;
215 pItem->ulSize = ulSize;
216 ulSize -= sizeof(pItem->ulSize); /* already read this member */
218 /* Read the item Id and data */
219 hRet = IStream_Read(lpStream, &pItem->ulId, ulSize, &ulRead);
221 if(FAILED(hRet) || ulRead != ulSize)
222 break;
224 SHAddDataBlock(lppList, pItem); /* Insert Item */
226 } while(1);
228 /* If we allocated space, free it */
229 if(pItem != bBuff)
230 LocalFree((HLOCAL)pItem);
232 return hRet;
235 /*************************************************************************
236 * @ [SHLWAPI.19]
238 * Free a DataBlock list.
240 * PARAMS
241 * lpList [I] List to free
243 * RETURNS
244 * Nothing.
246 * NOTES
247 * See SHWriteDataBlockList.
249 VOID WINAPI SHFreeDataBlockList(LPSHLWAPI_CLIST lpList)
251 TRACE("(%p)\n", lpList);
253 if (lpList)
254 LocalFree((HLOCAL)lpList);
257 /*************************************************************************
258 * @ [SHLWAPI.20]
260 * Insert a new item into a DataBlock list.
262 * PARAMS
263 * lppList [0] Pointer to the List
264 * lpNewItem [I] The new item to add to the list
266 * RETURNS
267 * Success: S_OK. The item is added to the list.
268 * Failure: An HRESULT error code.
270 * NOTES
271 * If the size of the element to be inserted is less than the size of a
272 * SHLWAPI_CLIST node, or the Id for the item is CLIST_ID_CONTAINER,
273 * the call returns S_OK but does not actually add the element.
274 * See SHWriteDataBlockList.
276 HRESULT WINAPI SHAddDataBlock(LPSHLWAPI_CLIST* lppList, LPCSHLWAPI_CLIST lpNewItem)
278 LPSHLWAPI_CLIST lpInsertAt = NULL;
279 ULONG ulSize;
281 TRACE("(%p,%p)\n", lppList, lpNewItem);
283 if(!lppList || !lpNewItem )
284 return E_INVALIDARG;
286 if (lpNewItem->ulSize < sizeof(SHLWAPI_CLIST) ||
287 lpNewItem->ulId == CLIST_ID_CONTAINER)
288 return S_OK;
290 ulSize = lpNewItem->ulSize;
292 if(ulSize & 0x3)
294 /* Tune size to a ULONG boundary, add space for container element */
295 ulSize = ((ulSize + 0x3) & 0xFFFFFFFC) + sizeof(SHLWAPI_CLIST);
296 TRACE("Creating container item, new size = %d\n", ulSize);
299 if(!*lppList)
301 /* An empty list. Allocate space for terminal ulSize also */
302 *lppList = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT,
303 ulSize + sizeof(ULONG));
304 lpInsertAt = *lppList;
306 else
308 /* Append to the end of the list */
309 ULONG ulTotalSize = 0;
310 LPSHLWAPI_CLIST lpIter = *lppList;
312 /* Iterate to the end of the list, calculating the total size */
313 while (lpIter->ulSize)
315 ulTotalSize += lpIter->ulSize;
316 lpIter = NextItem(lpIter);
319 /* Increase the size of the list */
320 lpIter = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList,
321 ulTotalSize + ulSize+sizeof(ULONG),
322 LMEM_ZEROINIT | LMEM_MOVEABLE);
323 if(lpIter)
325 *lppList = lpIter;
326 lpInsertAt = (LPSHLWAPI_CLIST)((char*)lpIter + ulTotalSize); /* At end */
330 if(lpInsertAt)
332 /* Copy in the new item */
333 LPSHLWAPI_CLIST lpDest = lpInsertAt;
335 if(ulSize != lpNewItem->ulSize)
337 lpInsertAt->ulSize = ulSize;
338 lpInsertAt->ulId = CLIST_ID_CONTAINER;
339 lpDest++;
341 memcpy(lpDest, lpNewItem, lpNewItem->ulSize);
343 /* Terminate the list */
344 lpInsertAt = NextItem(lpInsertAt);
345 lpInsertAt->ulSize = 0;
347 return lpNewItem->ulSize;
349 return S_OK;
352 /*************************************************************************
353 * @ [SHLWAPI.21]
355 * Remove an item from a DataBlock list.
357 * PARAMS
358 * lppList [O] List to remove the item from
359 * ulId [I] Id of item to remove
361 * RETURNS
362 * Success: TRUE.
363 * Failure: FALSE, If any parameters are invalid, or the item was not found.
365 * NOTES
366 * See SHWriteDataBlockList.
368 BOOL WINAPI SHRemoveDataBlock(LPSHLWAPI_CLIST* lppList, ULONG ulId)
370 LPSHLWAPI_CLIST lpList = 0;
371 LPSHLWAPI_CLIST lpItem = NULL;
372 LPSHLWAPI_CLIST lpNext;
373 ULONG ulNewSize;
375 TRACE("(%p,%d)\n", lppList, ulId);
377 if(lppList && (lpList = *lppList))
379 /* Search for item in list */
380 while (lpList->ulSize)
382 if(lpList->ulId == ulId ||
383 (lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId))
385 lpItem = lpList; /* Found */
386 break;
388 lpList = NextItem(lpList);
392 if(!lpItem)
393 return FALSE;
395 lpList = lpNext = NextItem(lpItem);
397 /* Locate the end of the list */
398 while (lpList->ulSize)
399 lpList = NextItem(lpList);
401 /* Resize the list */
402 ulNewSize = LocalSize((HLOCAL)*lppList) - lpItem->ulSize;
404 /* Copy following elements over lpItem */
405 memmove(lpItem, lpNext, (char *)lpList - (char *)lpNext + sizeof(ULONG));
407 if(ulNewSize <= sizeof(ULONG))
409 LocalFree((HLOCAL)*lppList);
410 *lppList = NULL; /* Removed the last element */
412 else
414 lpList = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList, ulNewSize,
415 LMEM_ZEROINIT|LMEM_MOVEABLE);
416 if(lpList)
417 *lppList = lpList;
419 return TRUE;
422 /*************************************************************************
423 * @ [SHLWAPI.22]
425 * Find an item in a DataBlock list.
427 * PARAMS
428 * lpList [I] List to search
429 * ulId [I] Id of item to find
431 * RETURNS
432 * Success: A pointer to the list item found
433 * Failure: NULL
435 * NOTES
436 * See SHWriteDataBlockList.
438 LPSHLWAPI_CLIST WINAPI SHFindDataBlock(LPSHLWAPI_CLIST lpList, ULONG ulId)
440 TRACE("(%p,%d)\n", lpList, ulId);
442 if(lpList)
444 while(lpList->ulSize)
446 if(lpList->ulId == ulId)
447 return lpList; /* Matched */
448 else if(lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId)
449 return lpList + 1; /* Contained item matches */
451 lpList = NextItem(lpList);
454 return NULL;