Re: [Mndiff-dev] [PATCH] mndiff (er, mnzip): alternative stream usage patch
Michael Niedermayer wrote:
Hi
On Mon, Jun 18, 2007 at 01:21:48AM -0700, Corey Hickey wrote:
This patch supersedes my previous two patches with regard to argument checking and file open checking.
The version attached allows input and output to be files, stdin/stdout, or a combination. I've tested it pretty thoroughly, but, if I messed it up, blame it on the fact that I should have been in bed long ago.
i dont think this will apply anymore after ive applied the last 3
Yeah, I meant it to be applied instead, but I guess I wasn't quick enough. ...and, of course, I meant mnzip in the subject of my previous message. I had a few minutes to re-make the patch; I also added some usage examples to help(). -Corey Index: mnzip.c =================================================================== --- mnzip.c (revision 36) +++ mnzip.c (working copy) @@ -458,7 +458,7 @@ 6,5 ( 3) -> 689 */ -static int ibwt(uint8_t *in, unsigned int len, unsigned int start, unsigned int histogram[256], int low_mem, int pretype){ +static int ibwt(uint8_t *in, FILE *fo, unsigned int len, unsigned int start, unsigned int histogram[256], int low_mem, int pretype){ int shift= low_mem*3; int shift2= shift + 8; int mask= (1<<shift)-1; @@ -539,7 +539,7 @@ if(pretype) inv_pretransform(buffer, pret_buf, len2, pretype); - fwrite(buffer, len2, 1, stdout); + fwrite(buffer, len2, 1, fo); } free(idx); @@ -766,7 +766,7 @@ return out_size+16; } -static int decompress(FILE *fi, int low_mem){ +static int decompress(FILE *fi, FILE *fo, int low_mem){ uint8_t *in; unsigned int in_len; unsigned int out_len,start,i, j, pretype; @@ -834,36 +834,42 @@ fprintf(stderr, "error (%d)\n", c.bytestream - c.bytestream_end); return -1; } - ibwt(tmp, out_len, start, histogram, low_mem, pretype); + ibwt(tmp, fo, out_len, start, histogram, low_mem, pretype); return out_len; } static void help(void){ fprintf(stderr, - "Usage: mnzip [OPTIONS] [INPUT-FILE]\n" + "Usage: mnzip [OPTIONS] [INPUT_FILE|-] [OUTPUT_FILE|-]\n" "options:\n" " c compress\n" " d decompress\n" " l slow/low memory mode\n" " 0-9 block size in 2^x megabyte (4mb is default)\n" + "\n" + "examples:\n" + " Compress one file to another file:\n" + " mnzip c input_file output_file\n" + " Compress from stdin to stdout:\n" + " mnzip c\n" + " mnzip c - -\n" + " Decompress from stdin to a file:\n" + " mnzip d - output_file\n" ); exit(1); } int main(int argc, char **argv){ - FILE *fi; + FILE *fi, *fo; uint8_t *out, *in; - int comp= !strcmp(argv[0], "mnzip"); + int comp= !strcmp(argv[0], "mnzib"); int low_mem= 0; int block_size= 1<<22; int outsize, i; static const uint8_t header[7]={'M','N','Z','I','P',0,0}; - if (argc != 3) - help(); - - for(i=0; argv[1][i]; i++){ + for(i=0; argv[1] && argv[1][i]; i++){ switch(argv[1][i]){ case 'c' : comp=1 ; break; case 'd' : comp=0 ; break; @@ -874,25 +880,34 @@ } } - if (! (fi = fopen(argv[2], "rb"))) { + if (argc < 3 || !strcmp(argv[2], "-")) + fi = stdin; + else if (! (fi = fopen(argv[2], "rb"))) { perror("Error opening input file"); exit(1); } + if (argc < 4 || !strcmp(argv[3], "-")) + fo = stdout; + else if (! (fo = fopen(argv[3], "w+b"))) { + perror("Error opening output file"); + exit(1); + } + init_prob(prob); if(comp){ in = malloc(block_size+RADIX_PASSES); out= malloc(2*block_size + 5000);//FIXME int and buffer overfl - fwrite(header, 7, 1, stdout); + fwrite(header, 7, 1, fo); while(!feof(fi)){ block_size= fread(in, 1, block_size, fi); fprintf(stderr, "bwt in %p %p %d\n", out, in, block_size); outsize= compress(out, in, block_size); fprintf(stderr, "bwt out %d\n", outsize); - fwrite(out, outsize, 1, stdout); + fwrite(out, outsize, 1, fo); } }else{ uint8_t tmp[256]; @@ -903,7 +918,7 @@ exit(1); } while(!feof(fi)){ - if(decompress(fi, low_mem)<0) + if(decompress(fi, fo, low_mem)<0) exit(1); } }
Hi On Mon, Jun 18, 2007 at 05:35:15PM -0700, Corey Hickey wrote:
Michael Niedermayer wrote:
Hi
On Mon, Jun 18, 2007 at 01:21:48AM -0700, Corey Hickey wrote:
This patch supersedes my previous two patches with regard to argument checking and file open checking.
The version attached allows input and output to be files, stdin/stdout, or a combination. I've tested it pretty thoroughly, but, if I messed it up, blame it on the fact that I should have been in bed long ago.
i dont think this will apply anymore after ive applied the last 3
Yeah, I meant it to be applied instead, but I guess I wasn't quick enough. ...and, of course, I meant mnzip in the subject of my previous message.
I had a few minutes to re-make the patch; I also added some usage examples to help(). [...] - int comp= !strcmp(argv[0], "mnzip"); + int comp= !strcmp(argv[0], "mnzib");
hmm
int low_mem= 0; int block_size= 1<<22; int outsize, i; static const uint8_t header[7]={'M','N','Z','I','P',0,0};
- if (argc != 3) - help(); - - for(i=0; argv[1][i]; i++){ + for(i=0; argv[1] && argv[1][i]; i++){
shouldnt we rather check argc instead of argv[1]? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB No human being will ever know the Truth, for even if they happen to say it by chance, they would not even known they had done so. -- Xenophanes
Michael Niedermayer wrote:
On Mon, Jun 18, 2007 at 01:21:48AM -0700, Corey Hickey wrote:
This patch supersedes my previous two patches with regard to argument checking and file open checking.
The version attached allows input and output to be files, stdin/stdout, or a combination. I've tested it pretty thoroughly, but, if I messed it up, blame it on the fact that I should have been in bed long ago. i dont think this will apply anymore after ive applied the last 3 Yeah, I meant it to be applied instead, but I guess I wasn't quick enough. ...and, of course, I meant mnzip in the subject of my previous message.
I had a few minutes to re-make the patch; I also added some usage examples to help(). [...] - int comp= !strcmp(argv[0], "mnzip"); + int comp= !strcmp(argv[0], "mnzib");
hmm
Gah... a relic from before my earlier patch got applied; I missed it when re-making the patch.
int low_mem= 0; int block_size= 1<<22; int outsize, i; static const uint8_t header[7]={'M','N','Z','I','P',0,0};
- if (argc != 3) - help(); - - for(i=0; argv[1][i]; i++){ + for(i=0; argv[1] && argv[1][i]; i++){
shouldnt we rather check argc instead of argv[1]?
If you prefer; that's just how it was before. If you like this patch, I'll send a cosmetic patch afterward. -Corey Index: mnzip.c =================================================================== --- mnzip.c (revision 36) +++ mnzip.c (working copy) @@ -458,7 +458,7 @@ 6,5 ( 3) -> 689 */ -static int ibwt(uint8_t *in, unsigned int len, unsigned int start, unsigned int histogram[256], int low_mem, int pretype){ +static int ibwt(uint8_t *in, FILE *fo, unsigned int len, unsigned int start, unsigned int histogram[256], int low_mem, int pretype){ int shift= low_mem*3; int shift2= shift + 8; int mask= (1<<shift)-1; @@ -539,7 +539,7 @@ if(pretype) inv_pretransform(buffer, pret_buf, len2, pretype); - fwrite(buffer, len2, 1, stdout); + fwrite(buffer, len2, 1, fo); } free(idx); @@ -766,7 +766,7 @@ return out_size+16; } -static int decompress(FILE *fi, int low_mem){ +static int decompress(FILE *fi, FILE *fo, int low_mem){ uint8_t *in; unsigned int in_len; unsigned int out_len,start,i, j, pretype; @@ -834,25 +834,34 @@ fprintf(stderr, "error (%d)\n", c.bytestream - c.bytestream_end); return -1; } - ibwt(tmp, out_len, start, histogram, low_mem, pretype); + ibwt(tmp, fo, out_len, start, histogram, low_mem, pretype); return out_len; } static void help(void){ fprintf(stderr, - "Usage: mnzip [OPTIONS] [INPUT-FILE]\n" + "Usage: mnzip [OPTIONS] [INPUT_FILE|-] [OUTPUT_FILE|-]\n" "options:\n" " c compress\n" " d decompress\n" " l slow/low memory mode\n" " 0-9 block size in 2^x megabyte (4mb is default)\n" + "\n" + "examples:\n" + " Compress one file to another file:\n" + " mnzip c input_file output_file\n" + " Compress from stdin to stdout:\n" + " mnzip c\n" + " mnzip c - -\n" + " Decompress from stdin to a file:\n" + " mnzip d - output_file\n" ); exit(1); } int main(int argc, char **argv){ - FILE *fi; + FILE *fi, *fo; uint8_t *out, *in; int comp= !strcmp(argv[0], "mnzip"); int low_mem= 0; @@ -860,9 +869,7 @@ int outsize, i; static const uint8_t header[7]={'M','N','Z','I','P',0,0}; - if (argc != 3) - help(); - + if (argc > 1) { for(i=0; argv[1][i]; i++){ switch(argv[1][i]){ case 'c' : comp=1 ; break; @@ -873,26 +880,36 @@ default : help() ; } } + } - if (! (fi = fopen(argv[2], "rb"))) { + if (argc < 3 || !strcmp(argv[2], "-")) + fi = stdin; + else if (! (fi = fopen(argv[2], "rb"))) { perror("Error opening input file"); exit(1); } + if (argc < 4 || !strcmp(argv[3], "-")) + fo = stdout; + else if (! (fo = fopen(argv[3], "w+b"))) { + perror("Error opening output file"); + exit(1); + } + init_prob(prob); if(comp){ in = malloc(block_size+RADIX_PASSES); out= malloc(2*block_size + 5000);//FIXME int and buffer overfl - fwrite(header, 7, 1, stdout); + fwrite(header, 7, 1, fo); while(!feof(fi)){ block_size= fread(in, 1, block_size, fi); fprintf(stderr, "bwt in %p %p %d\n", out, in, block_size); outsize= compress(out, in, block_size); fprintf(stderr, "bwt out %d\n", outsize); - fwrite(out, outsize, 1, stdout); + fwrite(out, outsize, 1, fo); } }else{ uint8_t tmp[256]; @@ -903,7 +920,7 @@ exit(1); } while(!feof(fi)){ - if(decompress(fi, low_mem)<0) + if(decompress(fi, fo, low_mem)<0) exit(1); } }
Hi On Tue, Jun 19, 2007 at 05:38:21PM -0700, Corey Hickey wrote: [...]
int low_mem= 0; int block_size= 1<<22; int outsize, i; static const uint8_t header[7]={'M','N','Z','I','P',0,0};
- if (argc != 3) - help(); - - for(i=0; argv[1][i]; i++){ + for(i=0; argv[1] && argv[1][i]; i++){
shouldnt we rather check argc instead of argv[1]?
If you prefer; that's just how it was before.
If you like this patch, I'll send a cosmetic patch afterward.
applied [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Opposition brings concord. Out of discord comes the fairest harmony. -- Heraclitus
Michael Niedermayer wrote:
Hi
On Tue, Jun 19, 2007 at 05:38:21PM -0700, Corey Hickey wrote: [...]
int low_mem= 0; int block_size= 1<<22; int outsize, i; static const uint8_t header[7]={'M','N','Z','I','P',0,0};
- if (argc != 3) - help(); - - for(i=0; argv[1][i]; i++){ + for(i=0; argv[1] && argv[1][i]; i++){
shouldnt we rather check argc instead of argv[1]? If you prefer; that's just how it was before.
If you like this patch, I'll send a cosmetic patch afterward.
applied
Thanks. -Corey
participants (2)
-
Corey Hickey -
Michael Niedermayer