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
23 /* dwSignature for contained DATABLOCK_HEADER items */
24 #define CLIST_ID_CONTAINER (~0U)
26 /*************************************************************************
29 * Internal helper: move a DataBlock pointer to the next item.
31 static inline LPDATABLOCK_HEADER
NextItem(LPDBLIST lpList
)
33 char* address
= (char*)lpList
;
34 address
+= lpList
->cbSize
;
35 return (LPDATABLOCK_HEADER
)address
;
38 /*************************************************************************
41 * Insert a new item into a DataBlock list.
44 * lppList [0] Pointer to the List
45 * lpNewItem [I] The new item to add to the list
48 * Success: S_OK. The item is added to the list.
49 * Failure: An HRESULT error code.
52 * If the size of the element to be inserted is less than the size of a
53 * DATABLOCK_HEADER node, or the Id for the item is CLIST_ID_CONTAINER,
54 * the call returns S_OK but does not actually add the element.
55 * See SHWriteDataBlockList.
62 WINAPI
SHAddDataBlock(LPDBLIST
* lppList
, const DATABLOCK_HEADER
*lpNewItem
)
64 LPDATABLOCK_HEADER lpInsertAt
= NULL
;
67 TRACE("(%p,%p)\n", lppList
, lpNewItem
);
69 if(!lppList
|| !lpNewItem
)
76 if (lpNewItem
->cbSize
< sizeof(DATABLOCK_HEADER
) ||
77 lpNewItem
->dwSignature
== CLIST_ID_CONTAINER
)
84 ulSize
= lpNewItem
->cbSize
;
88 /* Tune size to a ULONG boundary, add space for container element */
89 ulSize
= ((ulSize
+ 0x3) & 0xFFFFFFFC) + sizeof(DATABLOCK_HEADER
);
90 TRACE("Creating container item, new size = %d\n", ulSize
);
95 /* An empty list. Allocate space for terminal ulSize also */
96 *lppList
= LocalAlloc(LMEM_ZEROINIT
, ulSize
+ sizeof(ULONG
));
97 lpInsertAt
= *lppList
;
101 /* Append to the end of the list */
102 ULONG ulTotalSize
= 0;
103 LPDATABLOCK_HEADER lpIter
= *lppList
;
105 /* Iterate to the end of the list, calculating the total size */
106 while (lpIter
->cbSize
)
108 ulTotalSize
+= lpIter
->cbSize
;
109 lpIter
= NextItem(lpIter
);
112 /* Increase the size of the list */
113 lpIter
= LocalReAlloc(*lppList
, ulTotalSize
+ ulSize
+sizeof(ULONG
),
114 LMEM_ZEROINIT
| LMEM_MOVEABLE
);
118 lpInsertAt
= (LPDATABLOCK_HEADER
)((char*)lpIter
+ ulTotalSize
); /* At end */
124 /* Copy in the new item */
125 LPDATABLOCK_HEADER lpDest
= lpInsertAt
;
127 if(ulSize
!= lpNewItem
->cbSize
)
129 lpInsertAt
->cbSize
= ulSize
;
130 lpInsertAt
->dwSignature
= CLIST_ID_CONTAINER
;
133 memcpy(lpDest
, lpNewItem
, lpNewItem
->cbSize
);
135 /* Terminate the list */
136 lpInsertAt
= NextItem(lpInsertAt
);
137 lpInsertAt
->cbSize
= 0;
150 /*************************************************************************
153 * Write a DataBlock list to an IStream object.
156 * lpStream [I] IStream object to write the list to
157 * lpList [I] List of items to write
160 * Success: S_OK. The object is written to the stream.
161 * Failure: An HRESULT error code
164 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
165 * list structure (a "DataBlock List"), which may be stored and retrieved from
168 * The exposed API consists of:
170 * - SHWriteDataBlockList() - Write a DataBlock list to a stream,
171 * - SHReadDataBlockList() - Read and create a list from a stream,
172 * - SHFreeDataBlockList() - Free a list,
173 * - SHAddDataBlock() - Insert a new item into a list,
174 * - SHRemoveDataBlock() - Remove an item from a list,
175 * - SHFindDataBlock() - Find an item in a list.
177 * The DataBlock list is stored packed into a memory array. Each element has a
178 * size and an associated ID. Elements must be less than 64k if the list is
179 * to be subsequently read from a stream.
181 * Elements are aligned on DWORD boundaries. If an elements data size is not
182 * a DWORD size multiple, the element is wrapped by inserting a surrounding
183 * element with an Id of 0xFFFFFFFF, and size sufficient to pad to a DWORD boundary.
185 * These functions are slow for large objects and long lists.
187 HRESULT WINAPI
SHWriteDataBlockList(IStream
* lpStream
, LPDBLIST lpList
)
192 TRACE("(%p,%p)\n", lpStream
, lpList
);
196 while (lpList
->cbSize
)
198 LPDATABLOCK_HEADER lpItem
= lpList
;
200 if(lpList
->dwSignature
== CLIST_ID_CONTAINER
)
203 hRet
= IStream_Write(lpStream
,lpItem
,lpItem
->cbSize
,&ulSize
);
207 if(lpItem
->cbSize
!= ulSize
)
208 return STG_E_MEDIUMFULL
;
210 lpList
= NextItem(lpList
);
219 /* Write a terminating list entry with zero size */
220 hRet
= IStream_Write(lpStream
, &ulSize
,sizeof(ulSize
),&ulDummy
);
226 /*************************************************************************
229 * Read and create a DataBlock list from an IStream object.
232 * lpStream [I] Stream to read the list from
233 * lppList [0] Pointer to receive the new List
237 * Failure: An HRESULT error code
240 * When read from a file, list objects are limited in size to 64k.
241 * See SHWriteDataBlockList.
243 HRESULT WINAPI
SHReadDataBlockList(IStream
* lpStream
, LPDBLIST
* lppList
)
245 DATABLOCK_HEADER bBuff
[128]; /* Temporary storage for new list item */
246 ULONG ulBuffSize
= sizeof(bBuff
);
247 LPDATABLOCK_HEADER pItem
= bBuff
;
248 ULONG ulRead
, ulSize
;
251 TRACE("(%p,%p)\n", lpStream
, lppList
);
255 /* Free any existing list */
262 /* Read the size of the next item */
263 hRet
= IStream_Read(lpStream
, &ulSize
,sizeof(ulSize
),&ulRead
);
265 if(FAILED(hRet
) || ulRead
!= sizeof(ulSize
) || !ulSize
)
266 break; /* Read failed or read zero size (the end of the list) */
270 LARGE_INTEGER liZero
;
271 ULARGE_INTEGER ulPos
;
275 /* Back the stream up; this object is too big for the list */
276 if(SUCCEEDED(IStream_Seek(lpStream
, liZero
, STREAM_SEEK_CUR
, &ulPos
)))
278 liZero
.QuadPart
= ulPos
.QuadPart
- sizeof(ULONG
);
279 IStream_Seek(lpStream
, liZero
, STREAM_SEEK_SET
, NULL
);
283 else if (ulSize
>= sizeof(DATABLOCK_HEADER
))
285 /* Add this new item to the list */
286 if(ulSize
> ulBuffSize
)
288 /* We need more buffer space, allocate it */
289 LPDATABLOCK_HEADER lpTemp
;
292 lpTemp
= LocalAlloc(LMEM_ZEROINIT
, ulSize
);
294 lpTemp
= LocalReAlloc(pItem
, ulSize
, LMEM_ZEROINIT
|LMEM_MOVEABLE
);
298 hRet
= E_OUTOFMEMORY
;
305 pItem
->cbSize
= ulSize
;
306 ulSize
-= sizeof(pItem
->cbSize
); /* already read this member */
308 /* Read the item Id and data */
309 hRet
= IStream_Read(lpStream
, &pItem
->dwSignature
, ulSize
, &ulRead
);
311 if(FAILED(hRet
) || ulRead
!= ulSize
)
314 SHAddDataBlock(lppList
, pItem
); /* Insert Item */
318 /* If we allocated space, free it */
325 /*************************************************************************
328 * Free a DataBlock list.
331 * lpList [I] List to free
337 * See SHWriteDataBlockList.
339 VOID WINAPI
SHFreeDataBlockList(LPDBLIST lpList
)
341 TRACE("(%p)\n", lpList
);
347 /*************************************************************************
350 * Remove an item from a DataBlock list.
353 * lppList [O] List to remove the item from
354 * dwSignature [I] Id of item to remove
358 * Failure: FALSE, If any parameters are invalid, or the item was not found.
361 * See SHWriteDataBlockList.
363 BOOL WINAPI
SHRemoveDataBlock(LPDBLIST
* lppList
, DWORD dwSignature
)
366 LPDATABLOCK_HEADER lpList
= 0;
368 LPDATABLOCK_HEADER lpList
= NULL
;
370 LPDATABLOCK_HEADER lpItem
= NULL
;
371 LPDATABLOCK_HEADER lpNext
;
374 TRACE("(%p,%d)\n", lppList
, dwSignature
);
376 if(lppList
&& (lpList
= *lppList
))
378 /* Search for item in list */
379 while (lpList
->cbSize
)
381 if(lpList
->dwSignature
== dwSignature
||
382 (lpList
->dwSignature
== CLIST_ID_CONTAINER
&& lpList
[1].dwSignature
== dwSignature
))
384 lpItem
= lpList
; /* Found */
387 lpList
= NextItem(lpList
);
394 lpList
= lpNext
= NextItem(lpItem
);
396 /* Locate the end of the list */
397 while (lpList
->cbSize
)
398 lpList
= NextItem(lpList
);
400 /* Resize the list */
401 ulNewSize
= LocalSize(*lppList
) - lpItem
->cbSize
;
403 /* Copy following elements over lpItem */
404 memmove(lpItem
, lpNext
, (char *)lpList
- (char *)lpNext
+ sizeof(ULONG
));
406 if(ulNewSize
<= sizeof(ULONG
))
409 *lppList
= NULL
; /* Removed the last element */
413 lpList
= LocalReAlloc(*lppList
, ulNewSize
, LMEM_ZEROINIT
|LMEM_MOVEABLE
);
420 /*************************************************************************
423 * Find an item in a DataBlock list.
426 * lpList [I] List to search
427 * dwSignature [I] Id of item to find
430 * Success: A pointer to the list item found
434 * See SHWriteDataBlockList.
436 DATABLOCK_HEADER
* WINAPI
SHFindDataBlock(LPDBLIST lpList
, DWORD dwSignature
)
438 TRACE("(%p,%d)\n", lpList
, dwSignature
);
442 while(lpList
->cbSize
)
444 if(lpList
->dwSignature
== dwSignature
)
445 return lpList
; /* Matched */
446 else if(lpList
->dwSignature
== CLIST_ID_CONTAINER
&& lpList
[1].dwSignature
== dwSignature
)
447 return lpList
+ 1; /* Contained item matches */
449 lpList
= NextItem(lpList
);