From efb25f556a3635f3e4978246f6cca540cd9f9b11 Mon Sep 17 00:00:00 2001 From: ketmar Date: Wed, 11 Dec 2013 10:08:44 +0200 Subject: [PATCH] test: using buffers and show progress; library: check and signal division by zero by error code --- src/libha.c | 7 +++++-- src/main.c | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/libha.c b/src/libha.c index 2417977..d15ef63 100644 --- a/src/libha.c +++ b/src/libha.c @@ -295,6 +295,7 @@ typedef struct { ***********************************************************************/ static void ac_out (ari_t *ari, uint16_t low, uint16_t high, uint16_t tot) { uint32_t r; + if (tot == 0) longjmp(ari->io->errJP, LIBHA_ERR_OTHER); r = (uint32_t)(ari->h-ari->l)+1; ari->h = (uint16_t)(r*high/tot-1)+ari->l; ari->l += (uint16_t)(r*low/tot); @@ -352,6 +353,7 @@ static void ac_end_encode (ari_t *ari) { ***********************************************************************/ static void ac_in (ari_t *ari, uint16_t low, uint16_t high, uint16_t tot) { uint32_t r; + if (tot == 0) longjmp(ari->io->errJP, LIBHA_ERR_OTHER); r = (uint32_t)(ari->h-ari->l)+1; ari->h = (uint16_t)(r*high/tot-1)+ari->l; ari->l += (uint16_t)(r*low/tot); @@ -375,8 +377,9 @@ static void ac_in (ari_t *ari, uint16_t low, uint16_t high, uint16_t tot) { static uint16_t ac_threshold_val (ari_t *ari, uint16_t tot) { - uint32_t r = (uint32_t) (ari->h-ari->l)+1; - return (uint16_t)((((uint32_t) (ari->v-ari->l)+1)*tot-1)/r); + uint32_t r = (uint32_t)(ari->h-ari->l)+1; + if (r == 0) longjmp(ari->io->errJP, LIBHA_ERR_OTHER); + return (uint16_t)((((uint32_t)(ari->v-ari->l)+1)*tot-1)/r); } diff --git a/src/main.c b/src/main.c index e630960..1e6fa20 100644 --- a/src/main.c +++ b/src/main.c @@ -15,22 +15,42 @@ static asc_t asc = NULL; static int fdi = -1; static int fdo = -1; +static uint8_t rdbuf[1024*1024]; +static int rdpos = 0, rdmax = 0; +static int rdcur = 0, rdtotal = 0; + +static uint8_t wrbuf[1024*1024]; +static int wrpos = 0; + static int get_byte (void *udata) { - uint8_t b; - if (read(fdi, &b, 1) == 1) return b; - return LIBHA_FEOF; + if (rdpos >= rdmax) { + fprintf(stdout, "\r[%d/%d] %3d%%", rdcur, rdtotal, (int)((uint64_t)100*rdcur/rdtotal)); fflush(stdout); + rdmax = read(fdi, rdbuf, sizeof(rdbuf)); + if (rdmax <= 0) return LIBHA_FEOF; + rdpos = 0; + rdcur += rdmax; + } + return rdbuf[rdpos++]; } static int put_byte (int c, void *udata) { - uint8_t b = c&0xff; - write(fdo, &b, 1); + if (wrpos >= sizeof(wrbuf)) { + if (write(fdo, wrbuf, wrpos) != wrpos) return LIBHA_FERR; + wrpos = 0; + } + wrbuf[wrpos++] = c&0xff; return LIBHA_FOK; } static int flush (void *udata) { + fprintf(stdout, "\r[%d/%d] %3d%%\n", rdtotal, rdtotal, 100); fflush(stdout); + if (wrpos > 0) { + if (write(fdo, wrbuf, wrpos) != wrpos) return LIBHA_FERR; + wrpos = 0; + } return LIBHA_FOK; } @@ -64,6 +84,8 @@ int main (int argc, char *argv[]) { fprintf(stderr, "FATAL: can't create file: '%s'\n", argv[3]); return 1; } + rdtotal = lseek(fdi, 0, SEEK_END); + lseek(fdi, 0, SEEK_SET); asc = asc_alloc(&haio, NULL); if (dopack) { res = asc_pack(asc); @@ -75,10 +97,10 @@ int main (int argc, char *argv[]) { close(fdo); switch (res) { case 0: break; - case LIBHA_ERR_READ: fprintf(stderr, "READING ERROR!\n"); break; - case LIBHA_ERR_WRITE: fprintf(stderr, "WRITING ERROR!\n"); break; - case LIBHA_ERR_MEMORY: fprintf(stderr, "MEMORY ERROR!\n"); break; - default: fprintf(stderr, "UNKNOWN ERROR!\n"); break; + case LIBHA_ERR_READ: fprintf(stderr, "\nREADING ERROR!\n"); break; + case LIBHA_ERR_WRITE: fprintf(stderr, "\nWRITING ERROR!\n"); break; + case LIBHA_ERR_MEMORY: fprintf(stderr, "\nMEMORY ERROR!\n"); break; + default: fprintf(stderr, "\nOTHER ERROR!\n"); break; } if (res != 0) unlink(argv[3]); return res; -- 2.11.4.GIT