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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/*
* stream.c -- Implementation of data streams for Atari bootstrapper
*
* Copyright (c) 1997 by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive
* for more details.
*
*
* The streams implemented in this file are intended to clearly organize the
* data sources and transformations used by bootstrap to read the kernel and
* ramdisk image. They also free the single modules from managing data
* buffers, minimizes the memory needed for buffers in the various stages and
* the amount of copying between them.
*
* Terminology: A stream consists of a stack of modules. All of those either
* "produce" data (e.g. read them from disk or receive them via TFTP), or do
* transformations on data (e.g. decompress them). Producing modules must be
* at the bottom of the stack, and they don't call modules below them. Their
* order in the stack represents a preference which method to use to get the
* data. Transforming modules are above the producing modules and need the
* modules below to get the data they work on. They can call sopen(), sread()
* and the other stream interface functions just the usual way.
*
* Interface functions:
*
* stream_init(): Initialize the stream by removing all modules.
*
* stream_push(mod): Pushes a new module MOD onto the stack. All modules
* must be ready before any other function is called.
*
* sopen(name): Open the stream, NAME is the name of a file or some other
* entity to access.
*
* sread(buf,cnt): Read data from the stream, just like the Unix read()
* function. Returns number of bytes written to BUF. This is lower than
* CNT only at EOF. -1 means some error.
*
* sseek(whence,offset): Seek to some other location in the byte stream,
* arguments are as with Unix lseek(). Seeking backwards is supported only
* to some unspecified border, but small steps back should work after
* reading a small amount of data. SEEK_END as WHENCE is not supported,
* since the size isn't always known. Return value is the new position in
* the stream, or < 0 for error.
*
* sclose(): Close and de-init the stream.
*
* Module interface:
*
* Each module has to supply a struct of type MODULE describing itself. The
* struct consists of a name, a maximum buffer size, and four module methods.
* The max. buffer size is the biggest number of bytes a call to fillbuf() can
* return. If this is actually unlimited for the module, use some reasonable
* value that doesn't make reading inefficient, but also doesn't waste memory.
* 32k seems ok.
*
* open(name): Open the file (or other entity) NAME. Transforming modules
* usually pass this request down, and may do additional internal
* initializations. Producing modules check whether they can supply data,
* and then grab the stream tail. Otherwise, they deregister (retval 1).
* Return value is 0 for OK, 1 for "remove me from the stream please, I
* can't do anything", and < 0 for some error. 1 for transforming modules
* means that the transformation isn't to be applied (e.g. the file isn't
* compressed). If goinf to return 1, the open method must call sopen()
* for the modules downstreams itself, and return 0 or -1 according to
* success of this. This allows modules to open the downstream channel,
* check it, and if the data seen are not applicable just return. If the
* upper layer would do the opening, it couldn't tell whether the stream
* below the current module is already open or not.
*
* fillbuf(buf): Fill the buffer BUF with data. This should not write more
* than maxbuf bytes, but it can write less. It returns the number of
* bytes returned, or < 0 for an error.
*
* skip(cnt): Skip CNT bytes of the stream. This method is optional and may
* be NULL if the module can't implement it reasonably. (E.g., on
* decompressing it's impossible to skip, the data in between have to
* decompressed anyway.) The new position in the stream is returned
* (this may be less than requested). A return value < 0 stands for error.
*
* close(): Close this module and do any deinitializations necessary. Return
* 0 for ok, < 0 for error.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "penguin_prototypes.h"
#include "stream.h"
#ifndef MAX
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#endif
/* definition of the dummy head module */
MODULE head_mod = {
"head", /* name */
0, /* maxbuf (unused) */
NULL, NULL, NULL, NULL, /* methods */
MOD_REST_INIT
};
static int stream_dont_display = 0;
static MODULE *currmod; /* currently active module */
/***************************** Prototypes *****************************/
#ifdef SHOW_PROGRESS
static void stream_show_progress( void );
#endif /* SHOW_PROGRESS */
/************************* End of Prototypes **************************/
/* ------------------------------------------------------------------------ */
/* Initialization */
/* initialize the module stack */
void stream_init( void )
{
currmod = &head_mod;
currmod->buf = NULL;
head_mod.maxbuf = 0;
head_mod.up = head_mod.down = NULL;
}
/* push a module onto the stream
*
* The new module is inserted after the head module, i.e. ontop of the other
* modules registered before.
*/
void stream_push( MODULE *mod )
{
mod->buf = NULL;
mod->down = head_mod.down;
mod->up = &head_mod;
head_mod.down = mod;
mod->down->up = mod;
}
/* ------------------------------------------------------------------------ */
/* Macros */
/* go up and down the stream */
#define DOWN_MOD() \
do { \
if (!(currmod = currmod->down)) { \
cprintf( "Internal error: bottom-most module %s calls " \
"downstreams!\n", currmod->name ); \
exit( 1 ); \
} \
} while(0)
#define UP_MOD() \
do { \
if (!(currmod = currmod->up)) { \
cprintf( "Internal error: topmost module %s calls " \
"upstreams!\n", currmod->name ); \
exit( 1 ); \
} \
} while(0)
/* macros for accessing the methods of current module */
#define MOD_OPEN(name) ((*currmod->open)( (name) ))
#define MOD_FILLBUF(buf) ((*currmod->fillbuf)( (buf) ))
#define MOD_SKIP(off) ((*currmod->skip)( (off) ))
#define MOD_CLOSE() ((*currmod->close)())
#define ADJUST_USERBUF(len) \
do { \
buf += (len); \
cnt -= (len); \
currmod->fpos += (len); \
} while(0)
#define ADJUST_MODBUF(len) \
do { \
currmod->bufp += (len); \
currmod->buf_cnt -= (len); \
} while(0)
#define TEST_ERR(e) do { if ((e)<0) { rv = (e); goto err_out; } } while(0)
#define TEST_EOF(e) do { if ((e)==0) { currmod->eof = 1; goto out; } } while(0)
/* ------------------------------------------------------------------------ */
/* Functions */
/* open the stream */
int sopen( const char *name )
{
int rv;
stream_dont_display++;
DOWN_MOD();
rv = MOD_OPEN( name );
if (rv > 0) {
/* remove module from the stream */
if (currmod->down) {
currmod->up->down = currmod->down;
currmod->down->up = currmod->up;
currmod = currmod->down;
}
else
/* Was the bottom-most module, i.e. no module feels responsible
* for producing data -> no data available :-( */
rv = -1;
}
else if (rv == 0) {
/* init buffering data */
currmod->fpos =
currmod->buf_cnt =
currmod->eof = 0;
currmod->last_shown = -1;
if (!(currmod->buf = malloc( currmod->maxbuf ))) {
cprintf( "Out of buffer memory for module %s\n",
currmod->name );
rv = -1;
}
currmod->bufp = currmod->buf;
}
UP_MOD();
stream_dont_display--;
return( rv );
}
long sread( char *buf, long cnt )
{
long len, rv;
char *bufstart = buf;
DOWN_MOD();
if (currmod->eof) {
return( 0 );
}
if (currmod->buf_cnt) {
/* take data from buffer as far as possible */
len = MIN( currmod->buf_cnt, cnt );
memcpy( buf, currmod->bufp, len );
ADJUST_USERBUF(len);
ADJUST_MODBUF(len);
#ifdef SHOW_PROGRESS
stream_show_progress();
#endif
}
while( cnt >= currmod->maxbuf ) {
/* while fillbuf chunks fit into user buffer, call fillbuf for there
* directly */
len = MOD_FILLBUF( buf );
TEST_ERR(len);
TEST_EOF(len);
ADJUST_USERBUF(len);
#ifdef SHOW_PROGRESS
stream_show_progress();
#endif
}
while( cnt ) {
/* rest of request must be buffered */
currmod->buf_cnt = MOD_FILLBUF( currmod->buf );
currmod->bufp = currmod->buf;
TEST_ERR( currmod->buf_cnt );
TEST_EOF( currmod->buf_cnt );
len = MIN( currmod->buf_cnt, cnt );
memcpy( buf, currmod->buf, len );
ADJUST_USERBUF(len);
ADJUST_MODBUF(len);
#ifdef SHOW_PROGRESS
stream_show_progress();
#endif
}
out:
rv = buf - bufstart;
err_out:
UP_MOD();
return( rv );
}
#define RETURN(v) do { rv = (v); goto err_out; } while(0)
int sseek( long offset, int whence )
{
int rv;
long newpos, len;
DOWN_MOD();
switch( whence ) {
case SEEK_SET:
newpos = offset;
break;
case SEEK_CUR:
newpos = currmod->fpos + offset;
break;
case SEEK_END:
default:
/* not supported */
cprintf( "Unsupported seek operation for module %s\n",
currmod->name );
RETURN( -1 );
}
// cprintf("sseek() to %ld from %ld\n", newpos, currmod->fpos);
if (newpos == currmod->fpos)
goto out;
if (newpos < currmod->fpos) {
/* backward seeks are only supported inside the current buffer */
long bufstartpos = currmod->fpos - (currmod->bufp - currmod->buf);
long back;
if (!currmod->buf_cnt || newpos < bufstartpos) {
cprintf( "Unsupported backward seek in module %s "
"(bufstart=%ld, dstpos=%ld)\n",
currmod->name,
currmod->buf_cnt ? bufstartpos : -1,
newpos );
RETURN( -1 );
}
back = currmod->fpos - newpos;
currmod->bufp -= back;
currmod->buf_cnt += back;
currmod->fpos = newpos;
goto out;
}
if (currmod->buf_cnt && newpos <= currmod->fpos + currmod->buf_cnt) {
/* seek is forward inside current buffer */
long fwd = newpos - currmod->fpos;
ADJUST_MODBUF( fwd );
currmod->fpos += fwd;
goto out;
}
/* otherwise: always need to advance buffer (if present) */
if (currmod->buf_cnt) {
currmod->fpos += currmod->buf_cnt;
currmod->buf_cnt = 0;
}
/* let the module skip, if it can */
if (currmod->skip) {
len = MOD_SKIP( newpos - currmod->fpos );
TEST_ERR( len );
currmod->fpos = len;
}
/* otherwise, read and junk the data */
while( currmod->fpos < newpos ) {
/* rest of request must be buffered */
currmod->buf_cnt = MOD_FILLBUF( currmod->buf );
currmod->bufp = currmod->buf;
TEST_ERR( currmod->buf_cnt );
TEST_EOF( currmod->buf_cnt );
len = MIN( currmod->buf_cnt, newpos-currmod->fpos );
ADJUST_MODBUF(len);
currmod->fpos += len;
}
#ifdef SHOW_PROGRESS
stream_show_progress();
#endif
out:
rv = currmod->fpos;
err_out:
UP_MOD();
return( rv );
}
int sclose( void )
{
int rv;
#ifdef SHOW_PROGRESS
stream_show_progress();
cprintf( "\n" );
#endif
stream_dont_display++;
DOWN_MOD();
rv = MOD_CLOSE();
if (currmod->buf)
free( currmod->buf );
currmod->buf = NULL;
UP_MOD();
stream_dont_display--;
return( rv );
}
#ifdef SHOW_PROGRESS
#define SHOW_SHIFT 13
static void stream_show_progress( void )
{
static char rotchar[4] = { '|', '/', '-', '\\' };
MODULE *p;
int notnull = 0;
long spos = currmod->fpos >> SHOW_SHIFT;
return;
if ( stream_dont_display || (spos == currmod->last_shown) )
return;
currmod->last_shown = spos;
cprintf( "\n" );
for( p = head_mod.down; p; p = p->down ) {
if (!p->fpos && notnull)
break;
if (p->fpos)
notnull = 1;
cprintf( " %c %7ld %-8s ",
rotchar[(p->fpos / MAX(p->maxbuf,1<<SHOW_SHIFT)) & 3],
p->fpos, p->name );
}
fflush( stdout );
}
#endif /* SHOW_PROGRESS */
/* Local Variables: */
/* tab-width: 4 */
/* End: */
|