summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openbios/fs/iso9660/iso9660_read.c
blob: 22cc463f04a8744ea0c5d87832d39d4be7d0093f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 *
 * (c) 2005-2009 Laurent Vivier <Laurent@vivier.eu>
 *
 * This file has been copied from EMILE, http://emile.sf.net
 *
 */

#include "libiso9660.h"
#include "libopenbios/bindings.h"
#include "libc/diskio.h"

size_t iso9660_read(iso9660_FILE *_file, char *buf, size_t count)
{
	iso9660_FILE *file = (iso9660_FILE*)_file;
	size_t read = 0;

	if ( count > (file->size  - file->offset) )
		count = file->size  - file->offset;

	while (count > 0)
	{
		size_t part;
		int offset_extent;
		int offset_index;

		offset_extent = file->base +
				    (file->offset / ISOFS_BLOCK_SIZE);
		offset_index = file->offset % ISOFS_BLOCK_SIZE;

		if (file->current != offset_extent)
		{
			if ( (offset_index == 0) &&
			     (count >= ISOFS_BLOCK_SIZE) )
			{
				/* direct i/o */

				int extents_nb;

				extents_nb = count / ISOFS_BLOCK_SIZE;

				part = extents_nb * ISOFS_BLOCK_SIZE;

				seek_io(file->volume->fd,
					offset_extent * ISOFS_BLOCK_SIZE);
				read_io(file->volume->fd, buf + read, part);

				file->offset += part;
				count -= part;
				read += part;

				continue;
			}

			file->current = offset_extent;
			seek_io(file->volume->fd,
				offset_extent * ISOFS_BLOCK_SIZE);
			read_io(file->volume->fd, file->buffer,
				ISOFS_BLOCK_SIZE);
		}

		part = ISOFS_BLOCK_SIZE - offset_index;
		if (count < part)
			part = count;

		memcpy(buf + read, file->buffer + offset_index, part);

		file->offset += part;
		count -= part;
		read += part;
	}

	return read;
}