From 0224ad402d353e9584b7355d54dee240500ee2ac Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Lopes Date: Wed, 3 Oct 2007 16:32:09 +0000 Subject: [PATCH] Some fixes in burn_source implementation --- libisofs/ecma119.c | 52 +++++++++++++++++++++++++++++++++++----------- libisofs/ecma119.h | 11 ++++------ 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/libisofs/ecma119.c b/libisofs/ecma119.c index dcbc698..1e9c07e 100644 --- a/libisofs/ecma119.c +++ b/libisofs/ecma119.c @@ -479,6 +479,7 @@ ecma119_target_new(struct iso_volset *volset, /* prepare for writing */ t->curblock = 0; t->state = ECMA119_WRITE_SYSTEM_AREA; + t->bytes_read = 2048; return t; } @@ -863,30 +864,57 @@ write_data_chunk(struct ecma119_write_target *t, uint8_t *buf) } static int -bs_read(struct burn_source *bs, unsigned char *buf, int size) +bs_read_block(struct burn_source *bs) { struct ecma119_write_target *t = (struct ecma119_write_target*)bs->data; - if (size != t->block_size) { - printf("[ERROR] You can only read in %d chunks\n", t->block_size); - return -1; - } - if (t->curblock >= t->vol_space_size) { /* total_size could be setted by libburn */ - if ( t->curblock >= (t->total_size / (off_t) t->block_size) ) { + if ( t->curblock < (t->total_size / (off_t) t->block_size) ) { /* we pad the image */ - memset(buf, 0, size); - return size; + memset(t->buffer, 0, t->block_size); + t->curblock++; + return t->block_size; } return -1; } if (t->state_data_valid) - write_data_chunk(t, buf); + write_data_chunk(t, t->buffer); else - writers[t->state](t, buf); + writers[t->state](t, t->buffer); t->curblock++; - return size; + return t->block_size; +} + +static int +bs_read(struct burn_source *bs, unsigned char *buf, int size) +{ + int ret = 0,summed_ret = 0; + struct ecma119_write_target *t = (struct ecma119_write_target*)bs->data; + + /* make safe against partial buffer returns */ + while (1) { + if (t->bytes_read == 2048) { + /* we need to read next block */ + t->bytes_read = 0; + ret = bs_read_block(bs); + if (ret < 0) + return -1; + } + + int bytes_to_read = 2048 - t->bytes_read; + if (summed_ret + bytes_to_read > size) { + bytes_to_read = size - summed_ret; + } + + memcpy(buf + summed_ret, t->buffer, bytes_to_read); + + t->bytes_read += bytes_to_read; + summed_ret += bytes_to_read; + if (summed_ret >= size) + break; + } + return summed_ret; } static off_t diff --git a/libisofs/ecma119.h b/libisofs/ecma119.h index bdc67a4..0743bcd 100644 --- a/libisofs/ecma119.h +++ b/libisofs/ecma119.h @@ -174,17 +174,14 @@ struct ecma119_write_target /* for writing out files */ struct state_files { -// off_t pos; /* The number of bytes we have written -// * so far in the current file. -// */ -// off_t data_len;/* The number of bytes in the currently -// * open file. -// */ -// FILE *fd; /* The currently open file. */ struct iso_file_src *src; /* source for reading from the file */ int file; /* The index in filelist that we are * currently writing (or about to write). */ } state_files; + + /* temp buffer for read functions */ + uint8_t buffer[2048]; + int bytes_read; }; #define BP(a,b) [(b) - (a) + 1]