/* Copy upto length bytes from cache entry to buffer starting at offset bytes into the cache entry. If there's not length bytes then copy the number of bytes available. In all cases return the number of bytes copied.  */
int squashfs_copy_data(void *buffer, struct squashfs_cache_entry *entry, int offset, int length)
/* Copy upto length bytes from cache entry to buffer starting at offset bytes into the cache entry. If there's not length bytes then copy the number of bytes available. In all cases return the number of bytes copied.  */
int squashfs_copy_data(void *buffer, struct squashfs_cache_entry *entry, int offset, int length)
{
	int remaining = length;
	if (length == 0)
		return 0;
	else if (buffer == NULL)
		return min(length, entry->length - offset);
	while (offset < entry->length) {
		void *buff = entry->data[offset / PAGE_CACHE_SIZE]
				+ (offset % PAGE_CACHE_SIZE);
		int bytes = min_t(int, entry->length - offset,
				PAGE_CACHE_SIZE - (offset % PAGE_CACHE_SIZE));
		if (bytes >= remaining) {
			memcpy(buffer, buff, remaining);
			remaining = 0;
			break;
		}
		memcpy(buffer, buff, bytes);
		buffer += bytes;
		remaining -= bytes;
		offset += bytes;
	}
	return length - remaining;
}