File: file.c

package info (click to toggle)
delo 0.8-2.5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 476 kB
  • ctags: 1,272
  • sloc: ansic: 4,027; makefile: 200; sh: 141; asm: 55
file content (217 lines) | stat: -rw-r--r-- 4,306 bytes parent folder | download | duplicates (4)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * This file is licensed under the terms of the GNU General Public License,
 * version 2. See the file COPYING in the main directory for details.
 * 
 *  Copyright (C) 2002,2003  Thiemo Seufer <seufer@csv.ica.uni-stuttgart.de>
 */

#include "delo.h"
#include "stringops.h"

/*
 * A list of extents.
 */

static struct extentlist *extentlist_head = NULL;
static struct extentlist *extentlist_last;

void extentlist_clear(void)
{
	while (extentlist_head) {
		struct extentlist *tmp = extentlist_head;
		extentlist_head = tmp->next;
		free(tmp);
	}
}

int extentlist_add_tail(unsigned int extent, unsigned int count)
{
	struct extentlist *tmp;

	tmp = (struct extentlist *)malloc(sizeof(struct extentlist));
	if (!tmp)
		return 1;

	tmp->next = NULL;
	tmp->extent = extent;
	tmp->count = count;

	if (extentlist_head)
		extentlist_last->next = tmp;
	else
		extentlist_head = tmp;

	extentlist_last = tmp;
	
	return 0;
}

unsigned int extentlist_size(void)
{
	struct extentlist *tmp = extentlist_head;
	int count = 0;

	while (tmp) {
		count += tmp->count;
		tmp = tmp->next;
	}
	return count * SECTOR_SIZE;
}

void extentlist_dump(void)
{
	struct extentlist *tmp = extentlist_head;

	printf("Extents: ");
	while (tmp) {
		printf("%d/%d ", tmp->extent, tmp->count);
		tmp = tmp->next;
	}
	printf("\n");
}

/*
 * File I/O.
 */

int delo_open(char *partition, char *file)
{
	/* Check for iso9660 fs, which has no partitions. */
	if (!readisoblocks(file))
		return 0;

	dprintf("%s not in iso9660 filesystem\n", file);

	/* Check for ext2/ext3 fs, this checks also the partition table. */
	if(!readext2blocks(partition, file))
		return 0;

	dprintf("%s not in ext2 filesystem on partition %s\n", file,
		partition);

	/* Error */
	return 1;
}

int delo_read(void *dest, unsigned int offset, unsigned int len, int progress)
{
	struct extentlist *list = extentlist_head;
	int l;
	int rl;
	int bl;

	dprintf("read %x-%x->%p-%p: ", offset, offset + len, dest,
		dest + len);

	/*
	 * The REX firmware only pretends to read bytes, in fact it clobbers
	 * some bytes beyond the end, too. Thus we read always full blocks.
	 */

	if (list && offset) {
		unsigned int count = offset / SECTOR_SIZE;
		unsigned int rest = offset % SECTOR_SIZE;

		/* Find first block. */
		while (list) {
			if (list->count > count)
				break;
			count -= list->count;
			list = list->next;
		}

		/* Read first partial block. */
		l = MIN(SECTOR_SIZE, rest + len);
		if (rest && l) {
			char block[SECTOR_SIZE];

			dprintf("\n[%d]+%x-%x->%p-%p ", list->extent + count,
				rest, l - rest, dest, dest + l - rest);

			if (bootread(list->extent + count, block, l) != l)
				return 1;
			memcpy(dest, block + rest, l - rest);
			dest += l - rest;
			len -= l - rest;
			count++;
		}

		/* Read rest of first extent. */
		l = MIN((list->count - count) * SECTOR_SIZE, len);
		rl = l % SECTOR_SIZE;
		bl = l - rl;

		if (l)
			dprintf("\n[%d]-%x->%p-%p ", list->extent + count, l,
				dest, dest + l);

		if (bl && (bootread(list->extent + count, dest, bl) != bl))
			return 1;
		if (rl) {
			char block[SECTOR_SIZE];
			unsigned int re = list->extent + count + bl / SECTOR_SIZE;

			if ((bootread(re, block, rl) != rl))
				return 1;
			memcpy(dest + bl, block, rl);
		}
		dest += l;
		len -= l;
		list = list->next;
		if (progress)
			printf(".");
	}

	/* Read subsequent extents. */
	while (list && len) {
		l = MIN(list->count * SECTOR_SIZE, len);
		rl = l % SECTOR_SIZE;
		bl = l - rl;

		dprintf("\n[%d]-%x->%p-%p ", list->extent, l, dest, dest + l);

		if (bl && (bootread(list->extent, dest, bl) != bl))
			return 1;
		if (rl) {
			char block[SECTOR_SIZE];
			unsigned int re = list->extent + bl / SECTOR_SIZE;

			if ((bootread(re, block, rl) != rl))
				return 1;
			memcpy(dest + bl, block, rl);
		}
		dest += l;
		len -= l;
		list = list->next;
		if (progress)
			printf(".");
	}

	dprintf("\n");

	return len ? 1 : 0;
}

int readfile(void **dest, char *partition, char *file, size_t align)
{
	unsigned int size;

	/* Begin of progress indicator. */
	printf("Loading %s ", file);

	if (delo_open(partition, file))
		return 0;

	size = extentlist_size();

	if (!(*dest = malloc_aligned(size, align)))
		return 0;

	if (delo_read(*dest, 0, size, 1))
		return 0;

	/* End of progress indicator. */
	printf(" ok\n");

	return size;
}