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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/*
* deblock_media.c -
*
* Written by Eryk Vershen
*/
/*
* Copyright 1997,1998 by Apple Computer, Inc.
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appears in all copies and
* that both the copyright notice and this permission notice appear in
* supporting documentation.
*
* APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
* NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
// for malloc() & free()
#include <stdlib.h>
// for memcpy()
#include <string.h>
#include "deblock_media.h"
/*
* Defines
*/
/*
* Types
*/
typedef struct deblock_media *DEBLOCK_MEDIA;
struct deblock_media {
struct media m;
long need_filtering;
MEDIA next_media;
unsigned long next_block_size;
unsigned char *buffer;
};
struct deblock_globals {
long exists;
long kind;
};
/*
* Global Constants
*/
/*
* Global Variables
*/
static long deblock_inited = 0;
static struct deblock_globals deblock_info;
/*
* Forward declarations
*/
void deblock_init(void);
DEBLOCK_MEDIA new_deblock_media(void);
long read_deblock_media(MEDIA m, long long offset, unsigned long count, void *address);
long write_deblock_media(MEDIA m, long long offset, unsigned long count, void *address);
long close_deblock_media(MEDIA m);
long os_reload_deblock_media(MEDIA m);
/*
* Routines
*/
void
deblock_init(void)
{
if (deblock_inited != 0) {
return;
}
deblock_inited = 1;
deblock_info.kind = allocate_media_kind();
}
DEBLOCK_MEDIA
new_deblock_media(void)
{
return (DEBLOCK_MEDIA) new_media(sizeof(struct deblock_media));
}
MEDIA
open_deblock_media(long new_block_size, MEDIA m)
{
DEBLOCK_MEDIA a;
unsigned long block_size;
if (deblock_inited == 0) {
deblock_init();
}
a = 0;
if (m != 0) {
block_size = media_granularity(m);
if (new_block_size == block_size) {
return m;
} else if (new_block_size > block_size) {
if ((new_block_size % block_size) == 0) {
/* no filtering necessary */
a = new_deblock_media();
if (a != 0) {
a->need_filtering = 0;
a->next_block_size = block_size;
a->buffer = 0;
}
} else {
/* too hard to bother with */
}
} else /* new_block_size < block_size */ {
if ((block_size % new_block_size) == 0) {
/* block & unblock */
a = new_deblock_media();
if (a != 0) {
a->need_filtering = 1;
a->next_block_size = block_size;
a->buffer = malloc(block_size);
}
} else {
/* too hard to bother with */
}
}
if (a != 0) {
a->m.kind = deblock_info.kind;
a->m.grain = new_block_size;
a->m.size_in_bytes = media_total_size(m);
a->m.do_read = read_deblock_media;
a->m.do_write = write_deblock_media;
a->m.do_close = close_deblock_media;
a->m.do_os_reload = os_reload_deblock_media;
a->next_media = m;
}
}
return (MEDIA) a;
}
long
read_deblock_media(MEDIA m, long long offset, unsigned long count, void *address)
{
DEBLOCK_MEDIA a;
long rtn_value;
unsigned long next_size;
unsigned long partial_offset;
unsigned long partial_count;
long long cur_offset;
unsigned long remainder;
unsigned char *addr;
a = (DEBLOCK_MEDIA) m;
rtn_value = 0;
if (a == 0) {
/* no media */
} else if (a->m.kind != deblock_info.kind) {
/* wrong kind - XXX need to error here - this is an internal problem */
} else if (count <= 0 || count % a->m.grain != 0) {
/* can't handle size */
} else if (offset < 0 || offset % a->m.grain != 0) {
/* can't handle offset */
} else if (a->need_filtering == 0) {
rtn_value = read_media(a->next_media, offset, count, address);
} else {
next_size = a->next_block_size;
addr = address;
cur_offset = offset;
remainder = count;
rtn_value = 1;
/* read partial */
partial_offset = cur_offset % next_size;
if (partial_offset != 0) {
partial_count = next_size - partial_offset;
if (partial_count > remainder) {
partial_count = remainder;
}
rtn_value = read_media(a->next_media, cur_offset - partial_offset, next_size, a->buffer);
if (rtn_value != 0) {
memcpy (addr, a->buffer + partial_offset, partial_count);
addr += partial_count;
cur_offset += partial_count;
remainder -= partial_count;
}
}
/* read fulls as long as needed */
if (rtn_value != 0 && remainder > next_size) {
partial_count = remainder - (remainder % next_size);
rtn_value = read_media(a->next_media, cur_offset, partial_count, addr);
addr += partial_count;
cur_offset += partial_count;
remainder -= partial_count;
}
/* read partial */
if (rtn_value != 0 && remainder > 0) {
partial_count = remainder;
rtn_value = read_media(a->next_media, cur_offset, next_size, a->buffer);
if (rtn_value != 0) {
memcpy (addr, a->buffer, partial_count);
}
}
}
return rtn_value;
}
long
write_deblock_media(MEDIA m, long long offset, unsigned long count, void *address)
{
DEBLOCK_MEDIA a;
long rtn_value;
unsigned long next_size;
unsigned long partial_offset;
unsigned long partial_count;
long long cur_offset;
unsigned long remainder;
unsigned char *addr;
a = (DEBLOCK_MEDIA) m;
rtn_value = 0;
if (a == 0) {
/* no media */
} else if (a->m.kind != deblock_info.kind) {
/* wrong kind - XXX need to error here - this is an internal problem */
} else if (count <= 0 || count % a->m.grain != 0) {
/* can't handle size */
} else if (offset < 0 || offset % a->m.grain != 0) {
/* can't handle offset */
} else if (a->need_filtering == 0) {
rtn_value = write_media(a->next_media, offset, count, address);
} else {
next_size = a->next_block_size;
addr = address;
cur_offset = offset;
remainder = count;
rtn_value = 1;
/* write partial */
partial_offset = cur_offset % next_size;
if (partial_offset != 0) {
partial_count = next_size - partial_offset;
if (partial_count > remainder) {
partial_count = remainder;
}
rtn_value = read_media(a->next_media, cur_offset - partial_offset, next_size, a->buffer);
if (rtn_value != 0) {
memcpy (a->buffer + partial_offset, addr, partial_count);
rtn_value = write_media(a->next_media, cur_offset - partial_offset, next_size, a->buffer);
addr += partial_count;
cur_offset += partial_count;
remainder -= partial_count;
}
}
/* write fulls as long as needed */
if (rtn_value != 0 && remainder > next_size) {
partial_count = remainder - (remainder % next_size);
rtn_value = write_media(a->next_media, cur_offset, partial_count, addr);
addr += partial_count;
cur_offset += partial_count;
remainder -= partial_count;
}
/* write partial */
if (rtn_value != 0 && remainder > 0) {
partial_count = remainder;
rtn_value = read_media(a->next_media, cur_offset, next_size, a->buffer);
if (rtn_value != 0) {
memcpy (a->buffer, addr, partial_count);
rtn_value = write_media(a->next_media, cur_offset, next_size, a->buffer);
}
}
}
/* recompute size to handle file media */
a->m.size_in_bytes = media_total_size(a->next_media);
return rtn_value;
}
long
close_deblock_media(MEDIA m)
{
DEBLOCK_MEDIA a;
a = (DEBLOCK_MEDIA) m;
if (a == 0) {
return 0;
} else if (a->m.kind != deblock_info.kind) {
/* XXX need to error here - this is an internal problem */
return 0;
}
close_media(a->next_media);
free(a->buffer);
return 1;
}
long
os_reload_deblock_media(MEDIA m)
{
DEBLOCK_MEDIA a;
a = (DEBLOCK_MEDIA) m;
if (a == 0) {
return 0;
} else if (a->m.kind != deblock_info.kind) {
/* XXX need to error here - this is an internal problem */
return 0;
}
os_reload_media(a->next_media);
return 1;
}
|