2 * SHLWAPI Compact 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(shell);
30 /* Compact list element (ordinals 17-22) */
31 typedef struct tagSHLWAPI_CLIST
33 ULONG ulSize; /* Size of this list element and its data */
34 ULONG ulId; /* If -1, The real element follows */
35 /* Item data (or a contained SHLWAPI_CLIST) follows... */
36 } SHLWAPI_CLIST, *LPSHLWAPI_CLIST;
38 typedef const SHLWAPI_CLIST* LPCSHLWAPI_CLIST;
40 /* ulId for contained SHLWAPI_CLIST items */
41 static const ULONG CLIST_ID_CONTAINER = -1u;
43 HRESULT WINAPI SHLWAPI_20(LPSHLWAPI_CLIST*,LPCSHLWAPI_CLIST);
45 /*************************************************************************
48 * Internal helper: move a clist pointer to the next item.
50 inline static LPSHLWAPI_CLIST NextItem(LPCSHLWAPI_CLIST lpList)
52 const char* address = (char*)lpList;
53 address += lpList->ulSize;
54 return (LPSHLWAPI_CLIST)address;
57 /*************************************************************************
60 * Write a compact list to a stream.
63 * lpStream [I] Stream to write the list to
64 * lpList [I] List of items to write
68 * Failure: An HRESULT error code
71 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
72 * list structure which may be stored and retrieved from a stream.
74 * The exposed API consists of:
75 * @17 Write a compact list to a stream
76 * @18 Read and create a list from a stream
78 * @20 Insert a new item into a list
79 * @21 Remove an item from a list
80 * @22 Find an item in a list
82 * The compact list is stored packed into a memory array. Each element has a
83 * size and an associated ID. Elements must be less than 64k if the list is
84 * to be subsequently read from a stream.
86 * Elements are aligned on DWORD boundaries. If an elements data size is not
87 * a DWORD size multiple, the element is wrapped by inserting a surrounding
88 * element with an Id of -1, and size sufficient to pad to a DWORD boundary.
90 * These functions are slow for large objects and long lists.
92 HRESULT WINAPI SHLWAPI_17(IStream* lpStream, LPSHLWAPI_CLIST lpList)
95 HRESULT hRet = E_FAIL;
97 TRACE("(%p,%p)\n", lpStream, lpList);
101 while (lpList->ulSize)
103 LPSHLWAPI_CLIST lpItem = lpList;
105 if(lpList->ulId == CLIST_ID_CONTAINER)
108 hRet = IStream_Write(lpStream,lpItem,lpItem->ulSize,&ulSize);
112 if(lpItem->ulSize != ulSize)
113 return STG_E_MEDIUMFULL;
115 lpList = NextItem(lpList);
124 /* Write a terminating list entry with zero size */
125 hRet = IStream_Write(lpStream, &ulSize,sizeof(ulSize),&ulDummy);
131 /*************************************************************************
134 * Read and create a compact list from a stream
137 * lpStream [I] Stream to read the list from
138 * lppList [0] Pointer to recieve the new List
142 * Failure: An HRESULT error code
145 * When read from a file, list objects are limited in size to 64k.
147 HRESULT WINAPI SHLWAPI_18(IStream* lpStream, LPSHLWAPI_CLIST* lppList)
149 SHLWAPI_CLIST bBuff[128]; /* Temporary storage for new list item */
150 ULONG ulBuffSize = sizeof(bBuff);
151 LPSHLWAPI_CLIST pItem = bBuff;
152 ULONG ulRead, ulSize;
155 TRACE("(%p,%p)\n", lpStream, lppList);
159 /* Free any existing list */
160 LocalFree((HLOCAL)*lppList);
166 /* Read the size of the next item */
167 hRet = IStream_Read(lpStream, &ulSize,sizeof(ulSize),&ulRead);
169 if(FAILED(hRet) || ulRead != sizeof(ulSize) || !ulSize)
170 break; /* Read failed or read zero size (the end of the list) */
174 LARGE_INTEGER liZero;
175 ULARGE_INTEGER ulPos;
179 /* Back the stream up; this object is too big for the list */
180 if(SUCCEEDED(IStream_Seek(lpStream, liZero, STREAM_SEEK_CUR, &ulPos)))
182 liZero.QuadPart = ulPos.QuadPart - sizeof(ULONG);
183 IStream_Seek(lpStream, liZero, STREAM_SEEK_SET, NULL);
187 else if (ulSize >= sizeof(SHLWAPI_CLIST))
189 /* Add this new item to the list */
190 if(ulSize > ulBuffSize)
192 /* We need more buffer space, allocate it */
193 LPSHLWAPI_CLIST lpTemp;
196 lpTemp = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT, ulSize);
198 lpTemp = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)pItem, ulSize,
199 LMEM_ZEROINIT|LMEM_MOVEABLE);
203 hRet = E_OUTOFMEMORY;
210 pItem->ulSize = ulSize;
211 ulSize -= sizeof(pItem->ulSize); /* already read this member */
213 /* Read the item Id and data */
214 hRet = IStream_Read(lpStream, &pItem->ulId, ulSize, &ulRead);
216 if(FAILED(hRet) || ulRead != ulSize)
219 SHLWAPI_20(lppList, pItem); /* Insert Item */
223 /* If we allocated space, free it */
225 LocalFree((HLOCAL)pItem);
230 /*************************************************************************
233 * Free a compact list.
236 * lpList [I] List to free
241 VOID WINAPI SHLWAPI_19(LPSHLWAPI_CLIST lpList)
243 TRACE("(%p)\n", lpList);
246 LocalFree((HLOCAL)lpList);
249 /*************************************************************************
252 * Insert a new item into a compact list.
255 * lppList [0] Pointer to the List
256 * lpNewItem [I] The new item to add to the list
259 * Success: The size of the inserted item.
260 * Failure: An HRESULT error code.
262 HRESULT WINAPI SHLWAPI_20(LPSHLWAPI_CLIST* lppList, LPCSHLWAPI_CLIST lpNewItem)
264 LPSHLWAPI_CLIST lpInsertAt = NULL;
267 TRACE("(%p,%p)\n", lppList, lpNewItem);
269 if(!lppList || !lpNewItem ||
270 lpNewItem->ulId == CLIST_ID_CONTAINER ||
271 lpNewItem->ulSize < sizeof(SHLWAPI_CLIST))
274 ulSize = lpNewItem->ulSize;
278 /* Tune size to a ULONG boundary, add space for container element */
279 ulSize = ((ulSize + 0x3) & 0xFFFFFFFC) + sizeof(SHLWAPI_CLIST);
280 TRACE("Creating container item, new size = %ld\n", ulSize);
285 /* An empty list. Allocate space for terminal ulSize also */
286 *lppList = (LPSHLWAPI_CLIST)LocalAlloc(LMEM_ZEROINIT,
287 ulSize + sizeof(ULONG));
288 lpInsertAt = *lppList;
292 /* Append to the end of the list */
293 ULONG ulTotalSize = 0;
294 LPSHLWAPI_CLIST lpIter = *lppList;
296 /* Iterate to the end of the list, calculating the total size */
297 while (lpIter->ulSize)
299 ulTotalSize += lpIter->ulSize;
300 lpIter = NextItem(lpIter);
303 /* Increase the size of the list */
304 lpIter = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList,
305 ulTotalSize + ulSize+sizeof(ULONG),
306 LMEM_ZEROINIT | LMEM_MOVEABLE);
310 lpInsertAt = (LPSHLWAPI_CLIST)((char*)lpIter + ulTotalSize); /* At end */
316 /* Copy in the new item */
317 LPSHLWAPI_CLIST lpDest = lpInsertAt;
319 if(ulSize != lpNewItem->ulSize)
321 lpInsertAt->ulSize = ulSize;
322 lpInsertAt->ulId = CLIST_ID_CONTAINER;
325 memcpy(lpDest, lpNewItem, lpNewItem->ulSize);
327 /* Terminate the list */
328 lpInsertAt = NextItem(lpInsertAt);
329 lpInsertAt->ulSize = 0;
331 return lpNewItem->ulSize;
336 /*************************************************************************
339 * Remove an item from a compact list.
342 * lppList [O] List to remove the item from
343 * ulId [I] Id of item to remove
347 * Failure: FALSE, If any parameters are invalid, or the item was not found.
349 BOOL WINAPI SHLWAPI_21(LPSHLWAPI_CLIST* lppList, ULONG ulId)
351 LPSHLWAPI_CLIST lpList = 0;
352 LPSHLWAPI_CLIST lpItem = NULL;
353 LPSHLWAPI_CLIST lpNext;
356 TRACE("(%p,%ld)\n", lppList, ulId);
358 if(lppList && (lpList = *lppList))
360 /* Search for item in list */
361 while (lpList->ulSize)
363 if(lpList->ulId == ulId ||
364 (lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId))
366 lpItem = lpList; /* Found */
369 lpList = NextItem(lpList);
376 lpList = lpNext = NextItem(lpItem);
378 /* Locate the end of the list */
379 while (lpList->ulSize)
380 lpList = NextItem(lpList);
382 /* Resize the list */
383 ulNewSize = LocalSize((HLOCAL)*lppList) - lpItem->ulSize;
385 /* Copy following elements over lpItem */
386 memmove(lpItem, lpNext, (char *)lpList - (char *)lpNext + sizeof(ULONG));
388 if(ulNewSize <= sizeof(ULONG))
390 LocalFree((HLOCAL)*lppList);
391 *lppList = NULL; /* Removed the last element */
395 lpList = (LPSHLWAPI_CLIST)LocalReAlloc((HLOCAL)*lppList, ulNewSize,
396 LMEM_ZEROINIT|LMEM_MOVEABLE);
403 /*************************************************************************
406 * Find an item in a compact list.
409 * lpList [I] List to search
410 * ulId [I] ID of item to find
413 * Success: A pointer to the list item found
416 LPSHLWAPI_CLIST WINAPI SHLWAPI_22(LPSHLWAPI_CLIST lpList, ULONG ulId)
418 TRACE("(%p,%ld)\n", lpList, ulId);
422 while(lpList->ulSize)
424 if(lpList->ulId == ulId)
425 return lpList; /* Matched */
426 else if(lpList->ulId == CLIST_ID_CONTAINER && lpList[1].ulId == ulId)
427 return lpList + 1; /* Contained item matches */
429 lpList = NextItem(lpList);