From e0309d7bb89f864e826e8f7233a279262d7872a4 Mon Sep 17 00:00:00 2001 From: ketmar Date: Thu, 12 Dec 2013 19:07:18 +0200 Subject: [PATCH] tests: added signature and size to packed files --- src/main.c | 36 ++++++++++++++++++++++++++---------- src/main_unp.c | 28 +++++++++++++++++----------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/main.c b/src/main.c index 6ab0960..1aeda76 100644 --- a/src/main.c +++ b/src/main.c @@ -38,7 +38,6 @@ static int fdo = -1; #define INBUF_SIZE (1024*1024) static uint8_t rdbuf[INBUF_SIZE]; static int rdpos = 0, rdmax = 0; -static uint64_t rdcur = 0, rdtotal = 0; #define OUTBUF_SIZE (1024*1024) static uint8_t wrbuf[OUTBUF_SIZE]; @@ -46,23 +45,34 @@ static int wrpos = 0; static int dot_count = -1; +static uint64_t bytes_done = 0, bytes_total = 0; +static int packing; + static int get_byte (void *udata) { if (rdpos >= rdmax) { - if (pbar_dot_count(rdcur, rdtotal) != dot_count) { - dot_count = pbar_dot_count(rdcur, rdtotal); - pbar_draw(rdcur, rdtotal); + if (packing) { + if (pbar_dot_count(bytes_done, bytes_total) != dot_count) { + dot_count = pbar_dot_count(bytes_done, bytes_total); + pbar_draw(bytes_done, bytes_total); + } } rdmax = read(fdi, rdbuf, sizeof(rdbuf)); if (rdmax <= 0) return LIBHA_FEOF; rdpos = 0; - rdcur += rdmax; + bytes_done += rdmax; } return rdbuf[rdpos++]; } static int put_byte (int c, void *udata) { + if (!packing) { + if (pbar_dot_count(bytes_done, bytes_total) != dot_count) { + dot_count = pbar_dot_count(bytes_done, bytes_total); + pbar_draw(bytes_done, bytes_total); + } + } if (wrpos >= sizeof(wrbuf)) { if (write(fdo, wrbuf, wrpos) != wrpos) return LIBHA_FERR; wrpos = 0; @@ -73,7 +83,6 @@ static int put_byte (int c, void *udata) { static int flush (void *udata) { - pbar_clear(); if (wrpos > 0) { if (write(fdo, wrbuf, wrpos) != wrpos) return LIBHA_FERR; wrpos = 0; @@ -111,18 +120,25 @@ 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); + bytes_total = lseek(fdi, 0, SEEK_END); lseek(fdi, 0, SEEK_SET); asc = asc_alloc(&haio, NULL); if (dopack) { - res = asc_pack(asc); + // hey, write signature + if (write(fdo, "LBHZ", 4) != 4) res = -1; + else if (write(fdo, &bytes_total, 8) != 8) res = -1; + else res = asc_pack(asc); } else { - res = asc_unpack(asc); + char sign[4]; + if (read(fdi, sign, 4) != 4) res = -1; + else if (memcmp(sign, "LBHZ", 4) != 0) res = -1; + else if (read(fdi, &bytes_total, 8) != 8) res = -1; + else res = asc_unpack(asc); } asc_free(asc); close(fdi); close(fdo); - if (res < 0) pbar_clear(); + if (res == 0) pbar_clear(); switch (res) { case 0: break; case LIBHA_ERR_READ: fprintf(stderr, "\nREADING ERROR!\n"); break; diff --git a/src/main_unp.c b/src/main_unp.c index d46c8f5..fe30396 100644 --- a/src/main_unp.c +++ b/src/main_unp.c @@ -33,7 +33,7 @@ static int fdi = -1; static int fdo = -1; -static uint64_t rdcur = 0, rdtotal = 0; +static uint64_t bytes_done = 0, bytes_total = 0; #ifdef NDEBUG # define OUTBUF_SIZE (1024*1024) @@ -48,13 +48,7 @@ static int dot_count = -1; static int bread (void *buf, int buf_len, void *udata) { int res = read(fdi, buf, buf_len); - if (res >= 0) { - if (pbar_dot_count(rdcur, rdtotal) != dot_count) { - dot_count = pbar_dot_count(rdcur, rdtotal); - pbar_draw(rdcur, rdtotal); - } - rdcur += res; - } + if (res > 0) bytes_done += res; return res; } @@ -62,6 +56,7 @@ static int bread (void *buf, int buf_len, void *udata) { int main (int argc, char *argv[]) { haunp_t hup; int res = 0; + char sign[4]; #ifndef NDEBUG if (argc != 3) { argc = 3; @@ -78,17 +73,27 @@ int main (int argc, char *argv[]) { fprintf(stderr, "FATAL: can't open file: '%s'\n", argv[1]); return 1; } + if (read(fdi, sign, 4) != 4) res = -1; + else if (memcmp(sign, "LBHZ", 4) != 0) res = -1; + else if (read(fdi, &bytes_total, 8) != 8) res = -1; + if (res == -1) { + close(fdi); + fprintf(stderr, "FATAL: not libha file!\n"); + return 1; + } fdo = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0640); if (fdo < 0) { fprintf(stderr, "FATAL: can't create file: '%s'\n", argv[2]); return 1; } - rdtotal = lseek(fdi, 0, SEEK_END); - lseek(fdi, 0, SEEK_SET); hup = haunp_open_io(bread, NULL); wrbuf = malloc(OUTBUF_SIZE); - printf("output buffer size: %d\n", OUTBUF_SIZE); + //printf("output buffer size: %d\n", OUTBUF_SIZE); for (;;) { + if (pbar_dot_count(bytes_done, bytes_total) != dot_count) { + dot_count = pbar_dot_count(bytes_done, bytes_total); + pbar_draw(bytes_done, bytes_total); + } int rd = haunp_read(hup, wrbuf, OUTBUF_SIZE); if (rd <= 0) { pbar_clear(); @@ -102,6 +107,7 @@ int main (int argc, char *argv[]) { res = -1; break; } + bytes_total += rd; } free(wrbuf); pbar_clear(); -- 2.11.4.GIT