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.
57 HRESULT WINAPI
SHAddDataBlock(LPDBLIST
* lppList
, const DATABLOCK_HEADER
*lpNewItem
)
59 LPDATABLOCK_HEADER lpInsertAt
= NULL
;
62 TRACE("(%p,%p)\n", lppList
, lpNewItem
);
64 if(!lppList
|| !lpNewItem
)
67 if (lpNewItem
->cbSize
< sizeof(DATABLOCK_HEADER
) ||
68 lpNewItem
->dwSignature
== CLIST_ID_CONTAINER
)
71 ulSize
= lpNewItem
->cbSize
;
75 /* Tune size to a ULONG boundary, add space for container element */
76 ulSize
= ((ulSize
+ 0x3) & 0xFFFFFFFC) + sizeof(DATABLOCK_HEADER
);
77 TRACE("Creating container item, new size = %d\n", ulSize
);
82 /* An empty list. Allocate space for terminal ulSize also */
83 *lppList
= LocalAlloc(LMEM_ZEROINIT
, ulSize
+ sizeof(ULONG
));
84 lpInsertAt
= *lppList
;
88 /* Append to the end of the list */
89 ULONG ulTotalSize
= 0;
90 LPDATABLOCK_HEADER lpIter
= *lppList
;
92 /* Iterate to the end of the list, calculating the total size */
93 while (lpIter
->cbSize
)
95 ulTotalSize
+= lpIter
->cbSize
;
96 lpIter
= NextItem(lpIter
);
99 /* Increase the size of the list */
100 lpIter
= LocalReAlloc(*lppList
, ulTotalSize
+ ulSize
+sizeof(ULONG
),
101 LMEM_ZEROINIT
| LMEM_MOVEABLE
);
105 lpInsertAt
= (LPDATABLOCK_HEADER
)((char*)lpIter
+ ulTotalSize
); /* At end */
111 /* Copy in the new item */
112 LPDATABLOCK_HEADER lpDest
= lpInsertAt
;
114 if(ulSize
!= lpNewItem
->cbSize
)
116 lpInsertAt
->cbSize
= ulSize
;
117 lpInsertAt
->dwSignature
= CLIST_ID_CONTAINER
;
120 memcpy(lpDest
, lpNewItem
, lpNewItem
->cbSize
);
122 /* Terminate the list */
123 lpInsertAt
= NextItem(lpInsertAt
);
124 lpInsertAt
->cbSize
= 0;
126 return lpNewItem
->cbSize
;
131 /*************************************************************************
134 * Write a DataBlock list to an IStream object.
137 * lpStream [I] IStream object to write the list to
138 * lpList [I] List of items to write
141 * Success: S_OK. The object is written to the stream.
142 * Failure: An HRESULT error code
145 * Ordinals 17,18,19,20,21 and 22 are related and together provide a compact
146 * list structure (a "DataBlock List"), which may be stored and retrieved from
149 * The exposed API consists of:
151 * - SHWriteDataBlockList() - Write a DataBlock list to a stream,
152 * - SHReadDataBlockList() - Read and create a list from a stream,
153 * - SHFreeDataBlockList() - Free a list,
154 * - SHAddDataBlock() - Insert a new item into a list,
155 * - SHRemoveDataBlock() - Remove an item from a list,
156 * - SHFindDataBlock() - Find an item in a list.
158 * The DataBlock list is stored packed into a memory array. Each element has a
159 * size and an associated ID. Elements must be less than 64k if the list is
160 * to be subsequently read from a stream.
162 * Elements are aligned on DWORD boundaries. If an elements data size is not
163 * a DWORD size multiple, the element is wrapped by inserting a surrounding
164 * element with an Id of 0xFFFFFFFF, and size sufficient to pad to a DWORD boundary.
166 * These functions are slow for large objects and long lists.
168 HRESULT WINAPI
SHWriteDataBlockList(IStream
* lpStream
, LPDBLIST lpList
)
173 TRACE("(%p,%p)\n", lpStream
, lpList
);
177 while (lpList
->cbSize
)
179 LPDATABLOCK_HEADER lpItem
= lpList
;
181 if(lpList
->dwSignature
== CLIST_ID_CONTAINER
)
184 hRet
= IStream_Write(lpStream
,lpItem
,lpItem
->cbSize
,&ulSize
);
188 if(lpItem
->cbSize
!= ulSize
)
189 return STG_E_MEDIUMFULL
;
191 lpList
= NextItem(lpList
);
200 /* Write a terminating list entry with zero size */
201 hRet
= IStream_Write(lpStream
, &ulSize
,sizeof(ulSize
),&ulDummy
);
207 /*************************************************************************
210 * Read and create a DataBlock list from an IStream object.
213 * lpStream [I] Stream to read the list from
214 * lppList [0] Pointer to receive the new List
218 * Failure: An HRESULT error code
221 * When read from a file, list objects are limited in size to 64k.
222 * See SHWriteDataBlockList.
224 HRESULT WINAPI
SHReadDataBlockList(IStream
* lpStream
, LPDBLIST
* lppList
)
226 DATABLOCK_HEADER bBuff
[128]; /* Temporary storage for new list item */
227 ULONG ulBuffSize
= sizeof(bBuff
);
228 LPDATABLOCK_HEADER pItem
= bBuff
;
229 ULONG ulRead
, ulSize
;
232 TRACE("(%p,%p)\n", lpStream
, lppList
);
236 /* Free any existing list */
243 /* Read the size of the next item */
244 hRet
= IStream_Read(lpStream
, &ulSize
,sizeof(ulSize
),&ulRead
);
246 if(FAILED(hRet
) || ulRead
!= sizeof(ulSize
) || !ulSize
)
247 break; /* Read failed or read zero size (the end of the list) */
251 LARGE_INTEGER liZero
;
252 ULARGE_INTEGER ulPos
;
256 /* Back the stream up; this object is too big for the list */
257 if(SUCCEEDED(IStream_Seek(lpStream
, liZero
, STREAM_SEEK_CUR
, &ulPos
)))
259 liZero
.QuadPart
= ulPos
.QuadPart
- sizeof(ULONG
);
260 IStream_Seek(lpStream
, liZero
, STREAM_SEEK_SET
, NULL
);
264 else if (ulSize
>= sizeof(DATABLOCK_HEADER
))
266 /* Add this new item to the list */
267 if(ulSize
> ulBuffSize
)
269 /* We need more buffer space, allocate it */
270 LPDATABLOCK_HEADER lpTemp
;
273 lpTemp
= LocalAlloc(LMEM_ZEROINIT
, ulSize
);
275 lpTemp
= LocalReAlloc(pItem
, ulSize
, LMEM_ZEROINIT
|LMEM_MOVEABLE
);
279 hRet
= E_OUTOFMEMORY
;
286 pItem
->cbSize
= ulSize
;
287 ulSize
-= sizeof(pItem
->cbSize
); /* already read this member */
289 /* Read the item Id and data */
290 hRet
= IStream_Read(lpStream
, &pItem
->dwSignature
, ulSize
, &ulRead
);
292 if(FAILED(hRet
) || ulRead
!= ulSize
)
295 SHAddDataBlock(lppList
, pItem
); /* Insert Item */
299 /* If we allocated space, free it */
306 /*************************************************************************
309 * Free a DataBlock list.
312 * lpList [I] List to free
318 * See SHWriteDataBlockList.
320 VOID WINAPI
SHFreeDataBlockList(LPDBLIST lpList
)
322 TRACE("(%p)\n", lpList
);
328 /*************************************************************************
331 * Remove an item from a DataBlock list.
334 * lppList [O] List to remove the item from
335 * dwSignature [I] Id of item to remove
339 * Failure: FALSE, If any parameters are invalid, or the item was not found.
342 * See SHWriteDataBlockList.
344 BOOL WINAPI
SHRemoveDataBlock(LPDBLIST
* lppList
, DWORD dwSignature
)
346 LPDATABLOCK_HEADER lpList
= 0;
347 LPDATABLOCK_HEADER lpItem
= NULL
;
348 LPDATABLOCK_HEADER lpNext
;
351 TRACE("(%p,%d)\n", lppList
, dwSignature
);
353 if(lppList
&& (lpList
= *lppList
))
355 /* Search for item in list */
356 while (lpList
->cbSize
)
358 if(lpList
->dwSignature
== dwSignature
||
359 (lpList
->dwSignature
== CLIST_ID_CONTAINER
&& lpList
[1].dwSignature
== dwSignature
))
361 lpItem
= lpList
; /* Found */
364 lpList
= NextItem(lpList
);
371 lpList
= lpNext
= NextItem(lpItem
);
373 /* Locate the end of the list */
374 while (lpList
->cbSize
)
375 lpList
= NextItem(lpList
);
377 /* Resize the list */
378 ulNewSize
= LocalSize(*lppList
) - lpItem
->cbSize
;
380 /* Copy following elements over lpItem */
381 memmove(lpItem
, lpNext
, (char *)lpList
- (char *)lpNext
+ sizeof(ULONG
));
383 if(ulNewSize
<= sizeof(ULONG
))
386 *lppList
= NULL
; /* Removed the last element */
390 lpList
= LocalReAlloc(*lppList
, ulNewSize
, LMEM_ZEROINIT
|LMEM_MOVEABLE
);
397 /*************************************************************************
400 * Find an item in a DataBlock list.
403 * lpList [I] List to search
404 * dwSignature [I] Id of item to find
407 * Success: A pointer to the list item found
411 * See SHWriteDataBlockList.
413 DATABLOCK_HEADER
* WINAPI
SHFindDataBlock(LPDBLIST lpList
, DWORD dwSignature
)
415 TRACE("(%p,%d)\n", lpList
, dwSignature
);
419 while(lpList
->cbSize
)
421 if(lpList
->dwSignature
== dwSignature
)
422 return lpList
; /* Matched */
423 else if(lpList
->dwSignature
== CLIST_ID_CONTAINER
&& lpList
[1].dwSignature
== dwSignature
)
424 return lpList
+ 1; /* Contained item matches */
426 lpList
= NextItem(lpList
);