/* compress or decompress from stdin to stdout */ int main(int argc, char **argv) { int ret; /* avoid end-of-line conversions */ SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); /* do compression if no arguments */ if (argc == 1) { ret = def(stdin, stdout, Z_BEST_COMPRESSION); if (ret != Z_OK) zerr(ret); return ret; } /* do decompression if -d specified */ else if (argc == 2 && strcmp(argv[1], "-d") == 0) { ret = inf(stdin, stdout); if (ret != Z_OK) zerr(ret); return ret; } /* otherwise, report usage */ else { fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr); return 1; } }
int main(int argc, char **argv) { uint8_t buffer[1024]; size_t count; pb_istream_t stream; /* Whether to expect the optional values or the default values. */ int mode = (argc > 1) ? atoi(argv[1]) : 0; /* Read the data into buffer */ SET_BINARY_MODE(stdin); count = fread(buffer, 1, sizeof(buffer), stdin); /* Construct a pb_istream_t for reading from the buffer */ stream = pb_istream_from_buffer(buffer, count); /* Decode and print out the stuff */ if (!check_alltypes(&stream, mode)) { printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); return 1; } else { return 0; } }
void RDG_genOut(unsigned long long size, double matchProba, double litProba, unsigned seed) { BYTE buff[RDG_DICTSIZE + RDG_BLOCKSIZE]; U64 total = 0; size_t genBlockSize = RDG_BLOCKSIZE; void* ldctx; /* init */ if (litProba==0.0) litProba = matchProba / 4.5; ldctx = RDG_createLiteralDistrib(litProba); SET_BINARY_MODE(stdout); /* Generate dict */ RDG_genBlock(buff, RDG_DICTSIZE, 0, matchProba, ldctx, &seed); /* Generate compressible data */ while (total < size) { RDG_genBlock(buff, RDG_DICTSIZE+RDG_BLOCKSIZE, RDG_DICTSIZE, matchProba, ldctx, &seed); if (size-total < RDG_BLOCKSIZE) genBlockSize = (size_t)(size-total); total += genBlockSize; fwrite(buff, 1, genBlockSize, stdout); /* update dict */ memcpy(buff, buff + RDG_BLOCKSIZE, RDG_DICTSIZE); } free(ldctx); }
int main() { uint8_t buffer[Person_size]; pb_istream_t stream; size_t count; /* Read the data into buffer */ SET_BINARY_MODE(stdin); count = fread(buffer, 1, sizeof(buffer), stdin); if (!feof(stdin)) { printf("Message does not fit in buffer\n"); return 1; } /* Construct a pb_istream_t for reading from the buffer */ stream = pb_istream_from_buffer(buffer, count); /* Decode and print out the stuff */ if (!print_person(&stream)) { printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); return 1; } else { return 0; } }
int main(int argc, char **argv) { uint8_t buffer[AnonymousOneOfMessage_size]; size_t count; int option; if (argc != 2) { fprintf(stderr, "Usage: decode_oneof [number]\n"); return 1; } option = atoi(argv[1]); SET_BINARY_MODE(stdin); count = fread(buffer, 1, sizeof(buffer), stdin); if (!feof(stdin)) { printf("Message does not fit in buffer\n"); return 1; } { int status = 0; pb_istream_t stream; stream = pb_istream_from_buffer(buffer, count); status = test_oneof_1(&stream, option); if (status != 0) return status; } return 0; }
void RDG_genStdout(unsigned long long size, double matchProba, double litProba, unsigned seed) { size_t const stdBlockSize = 128 KB; size_t const stdDictSize = 32 KB; BYTE* const buff = (BYTE*)malloc(stdDictSize + stdBlockSize); U64 total = 0; BYTE ldt[LTSIZE]; /* literals distribution table */ /* init */ if (buff==NULL) { fprintf(stderr, "datagen: error: %s \n", strerror(errno)); exit(1); } if (litProba<=0.0) litProba = matchProba / 4.5; memset(ldt, '0', sizeof(ldt)); RDG_fillLiteralDistrib(ldt, litProba); SET_BINARY_MODE(stdout); /* Generate initial dict */ RDG_genBlock(buff, stdDictSize, 0, matchProba, ldt, &seed); /* Generate compressible data */ while (total < size) { size_t const genBlockSize = (size_t) (MIN (stdBlockSize, size-total)); RDG_genBlock(buff, stdDictSize+stdBlockSize, stdDictSize, matchProba, ldt, &seed); total += genBlockSize; { size_t const unused = fwrite(buff, 1, genBlockSize, stdout); (void)unused; } /* update dict */ memcpy(buff, buff + stdBlockSize, stdDictSize); } /* cleanup */ free(buff); }
/* ** write binary file */ unsigned long write_file_bin(char * file_name, unsigned char *data, unsigned long size) { FILE *file; unsigned long x; if (!data) { printf("(FILE/write) ERROR: no data\n"); return(0); } if (!file_name) { printf("(FILE/write) ERROR: invalid filename\n"); return(0); } if ((file=fopen(file_name, "wb"))) { SET_BINARY_MODE(file); x=fwrite(data, 1, size, file); fclose(file); } else { printf("(FILE/write) ERROR: unable to open file '%s'\n", file_name); } return(x); }
static void get_fileHandle(const char* input_filename, const char* output_filename, FILE** pfinput, FILE** pfoutput) { if (!strcmp (input_filename, stdinmark)) { DISPLAYLEVEL(4,"Using stdin for input\n"); *pfinput = stdin; SET_BINARY_MODE(stdin); } else { *pfinput = fopen(input_filename, "rb"); } if (!strcmp (output_filename, stdoutmark)) { DISPLAYLEVEL(4,"Using stdout for output\n"); *pfoutput = stdout; SET_BINARY_MODE(stdout); } else { /* Check if destination file already exists */ *pfoutput=0; if (strcmp(output_filename,nulmark)) *pfoutput = fopen( output_filename, "rb" ); if (*pfoutput!=0) { fclose(*pfoutput); if (!g_overwrite) { char ch; if (g_displayLevel <= 1) /* No interaction possible */ EXM_THROW(11, "Operation aborted : %s already exists", output_filename); DISPLAYLEVEL(2, "Warning : %s already exists\n", output_filename); DISPLAYLEVEL(2, "Overwrite ? (Y/N) : "); ch = (char)getchar(); if ((ch!='Y') && (ch!='y')) EXM_THROW(11, "Operation aborted : %s already exists", output_filename); } } *pfoutput = fopen( output_filename, "wb" ); } if ( *pfinput==0 ) EXM_THROW(12, "Pb opening %s", input_filename); if ( *pfoutput==0) EXM_THROW(13, "Pb opening %s", output_filename); }
int CZlib::Decompress(CString strFileName, CString strOutput) { // Open the files FILE * in = fopen(strFileName.Get(), "r"); FILE * out = fopen(strOutput.Get(), "w+"); // Enable binary mode SET_BINARY_MODE(in); SET_BINARY_MODE(out); // Deflate the file int iReturn = Inflate(in, out); // Close the files fclose(in); fclose(out); return iReturn; }
int main() { pb_istream_t stream = {&callback, NULL, SIZE_MAX}; stream.state = stdin; SET_BINARY_MODE(stdin); if (!print_person(&stream)) { printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); return 1; } else { return 0; } }
/* ** read binary file */ unsigned char * read_file_bin(char * file_name, unsigned long *size) { FILE *file; unsigned char *data; unsigned long x; if (!file_name) return(0); *size=get_filesize(file_name); data=malloc(*size); file=fopen(file_name, "rb"); SET_BINARY_MODE(file); x=fread(data, 1, *size, file); fclose(file); if (x != *size) { printf("error reading from file: '%s'\n",file_name); return(NULL); } return(data); }
int main(int argc,char**argv){ int counter =0; SET_BINARY_MODE(stdout); gzFile file = gzdopen(fileno(stdout), "wb6"); while(fgets(buf,MAXLEN,stdin)){ //fprintf(stderr,"buf:%s",buf); if(counter==0){ assert(buf[0]=='@'); gzwrite(file,buf,strlen(buf)); }else if(counter==1){ if(strlen(buf)>101){ gzwrite(file,buf,100); gzwrite(file,newline,1); }else gzwrite(file,buf,strlen(buf)); }else if(counter==2){ assert(buf[0]=='+'); gzwrite(file,buf,strlen(buf)); }else if(counter==3){ if(strlen(buf)>101){ gzwrite(file,buf,100); gzwrite(file,newline,1); }else gzwrite(file,buf,strlen(buf)); } counter++; if(counter==4){ counter = 0; // break; } } if(counter!=0) fprintf(stderr,"\t-> fastqfile looks truncated\n"); if (gzclose(file) != Z_OK) fprintf(stderr,"failed gzclose"); return 0; }
/* compress or decompress from stdin to stdout */ int main(int argc, char **argv) { const int klevel = 5; int ret; /* avoid end-of-line conversions */ SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); /* do compression if no arguments */ if (argc == 1) { FILE* fin = fopen( argv[0], "r" ); if ( NULL == fin ) { printf( "cannot open file %s\n", argv[0] ); return 0; } FILE* fout = fopen( "out.gz", "w+" ); ret = def( fin, fout, klevel ); if (ret != Z_OK) { zerr(ret); fclose( fin ); fclose( fout ); } fclose( fout ); fout = NULL; //test deflate and compress { fseek(fin, 0, SEEK_END); long origdata_len = ftell( fin ); char* origdata = (char*)malloc( origdata_len ); fseek(fin, 0, SEEK_SET); fread( origdata, 1, origdata_len, fin); fclose( fin ); unsigned long compresseddata_len = compressBound(origdata_len); Bytef* compresseddata = (Bytef*)malloc( compresseddata_len ); int retv = compress2( (Bytef*)compresseddata, &compresseddata_len, (Bytef*)origdata, origdata_len, klevel ); if ( retv != Z_OK ) return 0; FILE* fout = fopen( "out.gz", "r" ); fseek( fout, 0, SEEK_END ); long outdata_len = ftell( fout ); Bytef* outdata = (Bytef*)malloc( outdata_len ); memset(outdata, '1', outdata_len); fseek( fout, 0, SEEK_SET ); fread( outdata, 1, outdata_len, fout ); fclose( fout ); printf( "compresseddata_len=%d, outdata_len=%d\n", compresseddata_len, outdata_len ); unsigned long i = 0 ; for ( i = 0; i < compresseddata_len; ++i ) { if ( compresseddata[i] != outdata[i] ) { printf( "diff!!!! i=%d, a=%d, b=%d\n", i, compresseddata[i], outdata[i] ); break; } } } //test inflate and uncompress { FILE* fout = fopen( "out.gz", "r" ); fseek( fout, 0, SEEK_END ); long outdata_len = ftell( fout ); printf( "outdata_len=%d\n", outdata_len ); Bytef* outdata = (Bytef*)malloc( outdata_len ); fseek( fout, 0, SEEK_SET ); fread( outdata, 1, outdata_len, fout ); fclose( fout ); FILE* fout_inf = fopen( "out.inf", "w+" ); ret = inf( fout, fout_inf ); if ( ret != Z_OK ) { printf( "inf() failed. ret=%d\n", ret ); } fflush( fout_inf ); fclose( fout_inf ); fout_inf = fopen( "out.inf", "r" ); fseek( fout_inf, 0, SEEK_END ); long out_inf_data_len = ftell( fout_inf ); printf( "out_inf_data_len=%d\n", out_inf_data_len ); Bytef* out_inf_data = (Bytef*)malloc( out_inf_data_len ); fseek( fout_inf, 0, SEEK_SET ); fread( out_inf_data, 1, out_inf_data_len, fout_inf ); fclose( fout_inf ); unsigned long uncompressdata_len = out_inf_data_len * 10; Bytef* uncompressdata = (Bytef*)malloc( uncompressdata_len ); int retv = uncompress( uncompressdata, &uncompressdata_len, outdata, outdata_len ); if ( retv != Z_OK ) { zerr(retv); printf("uncompress failed, retv=%d\n", retv); return; } unsigned long i = 0; printf( "uncompressdata_len=%d, out_inf_data_len=%d\n", uncompressdata_len, out_inf_data_len ); for ( i = 0; i < uncompressdata_len; ++i ) { if ( uncompressdata[i] != out_inf_data[i] ) { printf( "diff!!! i=%d\n", i ); break; } } } return ret; /*ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION); if (ret != Z_OK) zerr(ret); return ret;*/ } /* do decompression if -d specified */ else if (argc == 2 && strcmp(argv[1], "-d") == 0) { ret = inf(stdin, stdout); if (ret != Z_OK) zerr(ret); return ret; } /* otherwise, report usage */ else { fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr); return 1; } }
/* Basic fields, nested submessages, extensions */ static bool test_TestMessage() { uint8_t buffer[256]; size_t msgsize; /* Construct a message with various fields filled in */ { TestMessage msg = TestMessage_init_zero; pb_ostream_t stream; fill_TestMessage(&msg); stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); if (!pb_encode(&stream, TestMessage_fields, &msg)) { fprintf(stderr, "Encode failed: %s\n", PB_GET_ERROR(&stream)); return false; } msgsize = stream.bytes_written; } /* Output encoded message for debug */ SET_BINARY_MODE(stdout); fwrite(buffer, 1, msgsize, stdout); /* Decode memory using dynamic allocation */ { TestMessage msg = TestMessage_init_zero; pb_istream_t stream; SubMessage ext2_dest; msg.extensions = &ext1; ext1.type = &dynamic_ext; ext1.dest = NULL; ext1.next = &ext2; ext2.type = &static_ext; ext2.dest = &ext2_dest; ext2.next = NULL; stream = pb_istream_from_buffer(buffer, msgsize); if (!pb_decode(&stream, TestMessage_fields, &msg)) { fprintf(stderr, "Decode failed: %s\n", PB_GET_ERROR(&stream)); return false; } /* Make sure it encodes back to same data */ { uint8_t buffer2[256]; pb_ostream_t ostream = pb_ostream_from_buffer(buffer2, sizeof(buffer2)); TEST(pb_encode(&ostream, TestMessage_fields, &msg)); TEST(ostream.bytes_written == msgsize); TEST(memcmp(buffer, buffer2, msgsize) == 0); } /* Make sure that malloc counters work */ TEST(get_alloc_count() > 0); /* Make sure that pb_release releases everything */ pb_release(TestMessage_fields, &msg); TEST(get_alloc_count() == 0); /* Check that double-free is a no-op */ pb_release(TestMessage_fields, &msg); TEST(get_alloc_count() == 0); } return true; }
/* compress or decompress from stdin to stdout */ int main(int argc, char **argv) { int ret; unsigned char *filename; unsigned char out_filename[255]; FILE *in,*devnull,*out; struct stat s; unsigned long f_size; unsigned long offset=0; // check args if (argc!=2) { printf("invalid arguments\n"); printf(" ./ruf_extract <filename.RUF>\n\n"); return(-1); } else { filename=argv[1]; } header(); /* check size of firmware file */ if (stat(filename, &s) == -1) { printf(" error opening file: %s\n\n",filename); return(-1); } f_size=s.st_size; printf(" Firmware size: %ld bytes\n\n",f_size); /* open firmware file */ in=fopen(filename, "r"); /* open /dev/null file */ devnull=fopen("/dev/null", "w"); /* avoid end-of-line conversions */ SET_BINARY_MODE(in); SET_BINARY_MODE(stdout); printf(" Scanning for inflated data ...\n\n"); for (offset=0; offset<f_size; offset++) { fseek(in, offset, SEEK_SET); ret = inf(in, devnull); if (ret == Z_OK) { printf(" Found data at offset: %X\n",offset); /* now do it once more, but this time we redirect it to a real file */ sprintf(out_filename,"chunk_%X.dat",offset); out=fopen(out_filename,"wb"); if (!out) { printf("\n Error: unable to open %s for writing\n\n",out_filename); } else { printf(" > inflating chunk to: %s ",out_filename); fseek(in, offset, SEEK_SET); ret=inf(in, out); if (ret == Z_OK) printf(" [OK]\n"); else printf(" [ERR %d]\n",ret); fclose(out); } } } fclose(devnull); fclose(in); printf("\n"); }
sox_format_t * sox_open_read( char const * path, sox_signalinfo_t const * signal, sox_encodinginfo_t const * encoding, char const * filetype) { sox_format_t * ft = lsx_calloc(1, sizeof(*ft)); sox_format_handler_t const * handler; char const * const io_types[] = {"file", "pipe", "file URL"}; char const * type = ""; size_t input_bufsiz = sox_globals.input_bufsiz? sox_globals.input_bufsiz : sox_globals.bufsiz; if (filetype) { if (!(handler = sox_find_format(filetype, sox_false))) { lsx_fail("no handler for given file type `%s'", filetype); goto error; } ft->handler = *handler; } if (!(ft->handler.flags & SOX_FILE_NOSTDIO)) { if (!strcmp(path, "-")) { /* Use stdin if the filename is "-" */ if (sox_globals.stdin_in_use_by) { lsx_fail("`-' (stdin) already in use by `%s'", sox_globals.stdin_in_use_by); goto error; } sox_globals.stdin_in_use_by = "audio input"; SET_BINARY_MODE(stdin); ft->fp = stdin; } else { ft->fp = xfopen(path, "rb", &ft->io_type); type = io_types[ft->io_type]; if (ft->fp == NULL) { lsx_fail("can't open input %s `%s': %s", type, path, strerror(errno)); goto error; } } if (setvbuf (ft->fp, NULL, _IOFBF, sizeof(char) * input_bufsiz)) { lsx_fail("Can't set read buffer"); goto error; } ft->seekable = is_seekable(ft); } if (!filetype) { if (ft->seekable) { filetype = auto_detect_format(ft, lsx_find_file_extension(path)); lsx_rewind(ft); } #ifndef NO_REWIND_PIPE else if (!(ft->handler.flags & SOX_FILE_NOSTDIO) && input_bufsiz >= AUTO_DETECT_SIZE) { filetype = auto_detect_format(ft, lsx_find_file_extension(path)); rewind_pipe(ft->fp); ft->tell_off = 0; } #endif if (filetype) { lsx_report("detected file format type `%s'", filetype); if (!(handler = sox_find_format(filetype, sox_false))) { lsx_fail("no handler for detected file type `%s'", filetype); goto error; } } else { if (ft->io_type == lsx_io_pipe) { filetype = "sox"; /* With successful pipe rewind, this isn't useful */ lsx_report("assuming input pipe `%s' has file-type `sox'", path); } else if (!(filetype = lsx_find_file_extension(path))) { lsx_fail("can't determine type of %s `%s'", type, path); goto error; } if (!(handler = sox_find_format(filetype, sox_true))) { lsx_fail("no handler for file extension `%s'", filetype); goto error; } } ft->handler = *handler; if (ft->handler.flags & SOX_FILE_NOSTDIO) { xfclose(ft->fp, ft->io_type); ft->fp = NULL; } } if (!ft->handler.startread && !ft->handler.read) { lsx_fail("file type `%s' isn't readable", filetype); goto error; } ft->mode = 'r'; ft->filetype = lsx_strdup(filetype); ft->filename = lsx_strdup(path); if (signal) ft->signal = *signal; if (encoding) ft->encoding = *encoding; else sox_init_encodinginfo(&ft->encoding); set_endiannesses(ft); if ((ft->handler.flags & SOX_FILE_DEVICE) && !(ft->handler.flags & SOX_FILE_PHONY)) lsx_set_signal_defaults(ft); ft->priv = lsx_calloc(1, ft->handler.priv_size); /* Read and write starters can change their formats. */ if (ft->handler.startread && (*ft->handler.startread)(ft) != SOX_SUCCESS) { lsx_fail("can't open input %s `%s': %s", type, ft->filename, ft->sox_errstr); goto error; } /* Fill in some defaults: */ if (sox_precision(ft->encoding.encoding, ft->encoding.bits_per_sample)) ft->signal.precision = sox_precision(ft->encoding.encoding, ft->encoding.bits_per_sample); if (!(ft->handler.flags & SOX_FILE_PHONY) && !ft->signal.channels) ft->signal.channels = 1; if (sox_checkformat(ft) != SOX_SUCCESS) { lsx_fail("bad input format for %s `%s': %s", type, ft->filename, ft->sox_errstr); goto error; } if (signal) { if (signal->rate && signal->rate != ft->signal.rate) lsx_warn("can't set sample rate %g; using %g", signal->rate, ft->signal.rate); if (signal->channels && signal->channels != ft->signal.channels) lsx_warn("can't set %u channels; using %u", signal->channels, ft->signal.channels); } return ft; error: if (ft->fp && ft->fp != stdin) xfclose(ft->fp, ft->io_type); free(ft->priv); free(ft->filename); free(ft->filetype); free(ft); return NULL; }
int main( int argc, char *argv[]) { int copyout = 0; int uncompr = 0; gzFile file; char *bname, outmode[20]; strcpy(outmode, "wb6 "); prog = argv[0]; bname = strrchr(argv[0], '/'); if (bname) bname++; else bname = argv[0]; argc--, argv++; if (!strcmp(bname, "gunzip")) uncompr = 1; else if (!strcmp(bname, "zcat")) copyout = uncompr = 1; while (argc > 0) { if (strcmp(*argv, "-c") == 0) copyout = 1; else if (strcmp(*argv, "-d") == 0) uncompr = 1; else if (strcmp(*argv, "-f") == 0) outmode[3] = 'f'; else if (strcmp(*argv, "-h") == 0) outmode[3] = 'h'; else if (strcmp(*argv, "-r") == 0) outmode[3] = 'R'; else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' && (*argv)[2] == 0) outmode[2] = (*argv)[1]; else break; argc--, argv++; } if (outmode[3] == ' ') outmode[3] = 0; if (argc == 0) { SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); if (uncompr) { file = gzdopen(fileno(stdin), "rb"); if (file == NULL) error("can't gzdopen stdin"); gz_uncompress(file, stdout); } else { file = gzdopen(fileno(stdout), outmode); if (file == NULL) error("can't gzdopen stdout"); gz_compress(stdin, file); } } else { if (copyout) { SET_BINARY_MODE(stdout); } do { if (uncompr) { if (copyout) { file = gzopen(*argv, "rb"); if (file == NULL) fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv); else gz_uncompress(file, stdout); } else { file_uncompress(*argv); } } else { if (copyout) { FILE * in = fopen(*argv, "rb"); if (in == NULL) { perror(*argv); } else { file = gzdopen(fileno(stdout), outmode); if (file == NULL) error("can't gzdopen stdout"); gz_compress(in, file); } } else { file_compress(*argv, outmode); } } } while (argv++, --argc); } return 0; }
int main(int argc, char* argv[]) { SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); k = 1; uint32_t n; /* number of kmers: 4^k */ uint32_t* cs; /* counts */ FILE* fin; int opt; int opt_idx; static struct option long_options[] = { {"size", no_argument, 0, 0}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {0, 0, 0, 0} }; while (1) { opt = getopt_long(argc, argv, "k:hV", long_options, &opt_idx); if( opt == -1 ) break; switch (opt) { case 0: if (long_options[opt_idx].flag != 0) break; if (optarg) { if( opt_idx == 1 ) { k = atoi(optarg); } } break; case 'k': k = atoi(optarg); break; case 'h': print_help(); return 0; case 'V': print_version(stdout, prog_name); return 0; case '?': return 1; default: abort(); } } if (k < 1) { fprintf(stderr, "Kmer size must be at least 1."); return 1; } if (k > 16) { fprintf(stderr, "Kmer size must be at most 16."); return 1; } n = 1 << (2*k); /* i.e. 4^k */ cs = malloc(n * sizeof(uint32_t)); memset(cs, 0, n * sizeof(uint32_t)); if (cs == NULL) { fprintf(stderr, "Insufficient memory to tally kmers of size %d\n", k ); return 1; } if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) { count_fastq_kmers(stdin, cs); } else { for (; optind < argc; optind++) { fin = fopen(argv[optind], "rb"); if (fin == NULL) { fprintf(stderr, "No such file '%s'.\n", argv[optind]); continue; } count_fastq_kmers(fin, cs); } } print_kmer_freqs( stdout, cs ); free(cs); return 0; }
int main(int argc, char* argv[]) { SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); const char* pat; pcre* re; const char* pat_error; int pat_error_offset; FILE* fin; invert_flag = 0; count_flag = 0; id_flag = 0; trim_before_flag = 0; trim_after_flag = 0; trim_match_flag = 0; int opt; int opt_idx; FILE* mismatch_file = NULL; static struct option long_options[] = { {"id", no_argument, &id_flag, 1}, {"invert-match", no_argument, &invert_flag, 1}, {"mismatches", required_argument, NULL, 'm'}, {"count", no_argument, &count_flag, 1}, {"trim-before", no_argument, &trim_before_flag, 1}, {"trim-after", no_argument, &trim_after_flag, 1}, {"trim-match", no_argument, &trim_match_flag, 1}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {0, 0, 0, 0} }; while (1) { opt = getopt_long(argc, argv, "ivmcabthV", long_options, &opt_idx); if (opt == -1) break; switch (opt) { case 0: if (long_options[opt_idx].flag != 0) break; if (optarg) { } break; case 'i': id_flag = 1; break; case 'v': invert_flag = 1; break; case 'a': trim_after_flag = 1; break; case 'b': trim_before_flag = 1; break; case 't': trim_match_flag = 1; break; case 'm': mismatch_file = fopen(optarg, "w"); if (mismatch_file == NULL) { fprintf(stderr, "No such file '%s'.\n", optarg); return 1; } break; case 'c': count_flag = 1; break; case 'h': print_help(); return 0; case 'V': print_version(stdout, prog_name); return 0; case '?': return 1; default: abort(); } } if (trim_before_flag && trim_after_flag) { fprintf(stderr, "Specify -b or -a, not both.\n"); return 1; } int trim = trim_before_flag || trim_after_flag; if (trim && id_flag) { fprintf(stderr, "Makes no sense to trim IDs.\n"); return 1; } if (optind >= argc) { fprintf(stderr, "A pattern must be specified.\n"); return 1; } pat = argv[optind++]; re = pcre_compile( pat, PCRE_CASELESS, &pat_error, &pat_error_offset, NULL ); if (re == NULL) { fprintf(stderr, "Syntax error in PCRE pattern at offset: %d: %s\n", pat_error_offset, pat_error ); return 1; } if (optind >= argc || (argc - optind == 1 && strcmp(argv[optind],"-") == 0)) { fastq_grep(stdin, stdout, mismatch_file, re); } else { for (; optind < argc; optind++) { fin = fopen(argv[optind], "rb"); if (fin == NULL) { fprintf(stderr, "No such file '%s'.\n", argv[optind]); continue; } fastq_grep(fin, stdout, mismatch_file, re); fclose(fin); } } pcre_free(re); if (mismatch_file) fclose(mismatch_file); return 0; }
int main(int argc, char **argv) { char *arg, *pargv[MAX_PARGS]; char **eargv = argv; FILE *fid; int pargc = 0, eargc = 0, mon = 0; if ( (emess_dat.Prog_name = strrchr(*argv,DIR_CHAR)) != nullptr) ++emess_dat.Prog_name; else emess_dat.Prog_name = *argv; inverse = ! strncmp(emess_dat.Prog_name, "inv", 3); if (argc <= 1 ) { (void)fprintf(stderr, usage, pj_get_release(), emess_dat.Prog_name); exit (0); } /* process run line arguments */ while (--argc > 0) { /* collect run line arguments */ if(**++argv == '-') for(arg = *argv;;) { switch(*++arg) { case '\0': /* position of "stdin" */ if (arg[-1] == '-') eargv[eargc++] = const_cast<char*>("-"); break; case 'b': /* binary I/O */ bin_in = bin_out = 1; continue; case 'v': /* monitor dump of initialization */ mon = 1; continue; case 'i': /* input binary */ bin_in = 1; continue; case 'o': /* output binary */ bin_out = 1; continue; case 'I': /* alt. method to spec inverse */ inverse = 1; continue; case 'E': /* echo ascii input to ascii output */ echoin = 1; continue; case 'V': /* very verbose processing mode */ very_verby = 1; mon = 1; continue; case 'S': /* compute scale factors */ dofactors = 1; continue; case 't': /* set col. one char */ if (arg[1]) tag = *++arg; else emess(1,"missing -t col. 1 tag"); continue; case 'l': /* list projections, ellipses or units */ if (!arg[1] || arg[1] == 'p' || arg[1] == 'P') { /* list projections */ const struct PJ_LIST *lp; int do_long = arg[1] == 'P', c; const char *str; for (lp = proj_list_operations() ; lp->id ; ++lp) { if( strcmp(lp->id,"latlong") == 0 || strcmp(lp->id,"longlat") == 0 || strcmp(lp->id,"geocent") == 0 ) continue; (void)printf("%s : ", lp->id); if (do_long) /* possibly multiline description */ (void)puts(*lp->descr); else { /* first line, only */ str = *lp->descr; while ((c = *str++) && c != '\n') putchar(c); putchar('\n'); } } } else if (arg[1] == '=') { /* list projection 'descr' */ const struct PJ_LIST *lp; arg += 2; for (lp = proj_list_operations(); lp->id ; ++lp) if (!strcmp(lp->id, arg)) { (void)printf("%9s : %s\n", lp->id, *lp->descr); break; } } else if (arg[1] == 'e') { /* list ellipses */ const struct PJ_ELLPS *le; for (le = proj_list_ellps(); le->id ; ++le) (void)printf("%9s %-16s %-16s %s\n", le->id, le->major, le->ell, le->name); } else if (arg[1] == 'u') { /* list units */ const struct PJ_UNITS *lu; for (lu = proj_list_units(); lu->id ; ++lu) (void)printf("%12s %-20s %s\n", lu->id, lu->to_meter, lu->name); } else if (arg[1] == 'd') { /* list datums */ const struct PJ_DATUMS *ld; printf("__datum_id__ __ellipse___ __definition/comments______________________________\n" ); for (ld = pj_get_datums_ref(); ld->id ; ++ld) { printf("%12s %-12s %-30s\n", ld->id, ld->ellipse_id, ld->defn); if( ld->comments != nullptr && strlen(ld->comments) > 0 ) printf( "%25s %s\n", " ", ld->comments ); } } else emess(1,"invalid list option: l%c",arg[1]); exit(0); /* cppcheck-suppress duplicateBreak */ continue; /* artificial */ case 'e': /* error line alternative */ if (--argc <= 0) noargument: emess(1,"missing argument for -%c",*arg); oterr = *++argv; continue; case 'm': /* cartesian multiplier */ if (--argc <= 0) goto noargument; postscale = 1; if (!strncmp("1/",*++argv,2) || !strncmp("1:",*argv,2)) { if((fscale = atof((*argv)+2)) == 0.) goto badscale; fscale = 1. / fscale; } else if ((fscale = atof(*argv)) == 0.) { badscale: emess(1,"invalid scale argument"); } continue; case 'W': /* specify seconds precision */ case 'w': /* -W for constant field width */ { int c = arg[1]; if (c != 0 && isdigit(c)) { set_rtodms(c - '0', *arg == 'W'); ++arg; } else emess(1,"-W argument missing or non-digit"); continue; } case 'f': /* alternate output format degrees or xy */ if (--argc <= 0) goto noargument; oform = *++argv; continue; case 'd': if (--argc <= 0) goto noargument; sprintf(oform_buffer, "%%.%df", atoi(*++argv)); oform = oform_buffer; break; case 'r': /* reverse input */ reversein = 1; continue; case 's': /* reverse output */ reverseout = 1; continue; default: emess(1, "invalid option: -%c",*arg); break; } break; } else if (**argv == '+') { /* + argument */ if (pargc < MAX_PARGS) pargv[pargc++] = *argv + 1; else emess(1,"overflowed + argument table"); } else /* assumed to be input file name(s) */ eargv[eargc++] = *argv; } if (eargc == 0) /* if no specific files force sysin */ eargv[eargc++] = const_cast<char*>("-"); /* done with parameter and control input */ if (inverse && postscale) { prescale = 1; postscale = 0; fscale = 1./fscale; } if (!(Proj = pj_init(pargc, pargv))) emess(3,"projection initialization failure\ncause: %s", pj_strerrno(pj_errno)); if (!proj_angular_input(Proj, PJ_FWD)) { emess(3, "can't initialize operations that take non-angular input coordinates"); exit(0); } if (proj_angular_output(Proj, PJ_FWD)) { emess(3, "can't initialize operations that produce angular output coordinates"); exit(0); } if (inverse) { if (!Proj->inv) emess(3,"inverse projection not available"); proj.inv = pj_inv; } else proj.fwd = pj_fwd; /* set input formatting control */ if (mon) { pj_pr_list(Proj); if (very_verby) { (void)printf("#Final Earth figure: "); if (Proj->es != 0.0) { (void)printf("ellipsoid\n# Major axis (a): "); (void)printf(oform ? oform : "%.3f", Proj->a); (void)printf("\n# 1/flattening: %.6f\n", 1./(1. - sqrt(1. - Proj->es))); (void)printf("# squared eccentricity: %.12f\n", Proj->es); } else { (void)printf("sphere\n# Radius: "); (void)printf(oform ? oform : "%.3f", Proj->a); (void)putchar('\n'); } } } if (inverse) informat = strtod; else { informat = proj_dmstor; if (!oform) oform = "%.2f"; } if (bin_out) { SET_BINARY_MODE(stdout); } /* process input file list */ for ( ; eargc-- ; ++eargv) { if (**eargv == '-') { fid = stdin; emess_dat.File_name = const_cast<char*>("<stdin>"); if (bin_in) { SET_BINARY_MODE(stdin); } } else { if ((fid = fopen(*eargv, "rb")) == nullptr) { emess(-2, *eargv, "input file"); continue; } emess_dat.File_name = *eargv; } emess_dat.File_line = 0; if (very_verby) vprocess(fid); else process(fid); (void)fclose(fid); emess_dat.File_name = nullptr; } if( Proj ) pj_free(Proj); exit(0); /* normal completion */ }
/* compress or decompress from stdin to stdout */ int main(int argc, char **argv) { int rc = Z_OK; bool compress = true; int list_contents = 0; bool force = false; bool quiet __attribute__((unused)) = false; int window_bits = 31; /* GZIP */ int level = Z_DEFAULT_COMPRESSION; char *prog = basename(argv[0]); const char *in_f = NULL; char out_f[PATH_MAX]; FILE *i_fp = stdin; FILE *o_fp = NULL; const char *suffix = "gz"; int force_software = 0; int cpu = -1; unsigned char *in = NULL; unsigned char *out = NULL; z_stream strm; const char *name = NULL; char *comment = NULL; const char *extra_fname = NULL; uint8_t *extra = NULL; int extra_len = 0; struct stat s; const char *accel = "GENWQE"; const char *accel_env = getenv("ZLIB_ACCELERATOR"); int card_no = 0; const char *card_no_env = getenv("ZLIB_CARD"); /* Use environment variables as defaults. Command line options can than overrule this. */ if (accel_env != NULL) accel = accel_env; if (card_no_env != NULL) card_no = atoi(card_no_env); /* avoid end-of-line conversions */ SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); if (strstr(prog, "gunzip") != 0) compress = false; while (1) { int ch; int option_index = 0; static struct option long_options[] = { { "stdout", no_argument, NULL, 'c' }, { "decompress", no_argument, NULL, 'd' }, { "force", no_argument, NULL, 'f' }, { "help", no_argument, NULL, 'h' }, /* list */ { "list", no_argument, NULL, 'l' }, { "license", no_argument, NULL, 'L' }, { "suffix", required_argument, NULL, 'S' }, { "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' }, { "fast", no_argument, NULL, '1' }, { "best", no_argument, NULL, '9' }, /* our own options */ { "cpu", required_argument, NULL, 'X' }, { "accelerator-type", required_argument, NULL, 'A' }, { "card_no", required_argument, NULL, 'B' }, { "software", no_argument, NULL, 's' }, { "extra", required_argument, NULL, 'E' }, { "name", required_argument, NULL, 'N' }, { "comment", required_argument, NULL, 'C' }, { "i_bufsize", required_argument, NULL, 'i' }, { "o_bufsize", required_argument, NULL, 'o' }, { 0, no_argument, NULL, 0 }, }; ch = getopt_long(argc, argv, "E:N:C:cdfqhlLsS:vV123456789?i:o:X:A:B:", long_options, &option_index); if (ch == -1) /* all params processed ? */ break; switch (ch) { case 'X': cpu = strtoul(optarg, NULL, 0); break; case 'A': accel = optarg; break; case 'B': card_no = strtol(optarg, (char **)NULL, 0); break; case 'E': extra_fname = optarg; break; case 'N': name = optarg; break; case 'C': comment = optarg; break; case 'd': compress = false; break; case 'f': force = true; break; case 'q': /* Currently does nothing, zless needs it */ quiet = true; break; case 'c': o_fp = stdout; break; case 'S': suffix = optarg; break; case 's': force_software = true; break; case 'l': list_contents++; break; case '1': level = Z_BEST_SPEED; break; case '2': level = 2; break; case '3': level = 3; break; case '4': level = 4; break; case '5': level = 5; break; case '6': level = 6; break; case '7': level = 7; break; case '8': level = 8; break; case '9': level = Z_BEST_COMPRESSION; break; case 'v': verbose++; break; case 'V': fprintf(stdout, "%s\n", version); exit(EXIT_SUCCESS); break; case 'i': CHUNK_i = str_to_num(optarg); break; case 'o': CHUNK_o = str_to_num(optarg); break; case 'L': userinfo(stdout, prog, version); exit(EXIT_SUCCESS); break; case 'h': case '?': usage(stdout, prog, argc, argv); exit(EXIT_SUCCESS); break; } } if (cpu != -1) pin_to_cpu(cpu); if (force_software) { zlib_set_inflate_impl(ZLIB_SW_IMPL); zlib_set_deflate_impl(ZLIB_SW_IMPL); } else { zlib_set_accelerator(accel, card_no); zlib_set_inflate_impl(ZLIB_HW_IMPL); zlib_set_deflate_impl(ZLIB_HW_IMPL); } /* FIXME loop over this ... */ if (optind < argc) { /* input file */ in_f = argv[optind++]; i_fp = fopen(in_f, "r"); if (!i_fp) { pr_err("%s\n", strerror(errno)); print_args(stderr, argc, argv); exit(EX_ERRNO); } rc = lstat(in_f, &s); if ((rc == 0) && S_ISLNK(s.st_mode)) { pr_err("%s: Too many levels of symbolic links\n", in_f); exit(EXIT_FAILURE); } if (list_contents) { rc = strip_ending(out_f, in_f, PATH_MAX, suffix); if (rc < 0) { pr_err("No .%s file!\n", suffix); print_args(stderr, argc, argv); exit(EXIT_FAILURE); } rc = do_list_contents(i_fp, out_f, list_contents); if (rc != 0) { pr_err("Unable to list contents.\n"); print_args(stderr, argc, argv); exit(EXIT_FAILURE); } fclose(i_fp); exit(EXIT_SUCCESS); } } if (in_f == NULL) o_fp = stdout; /* should not be a terminal! */ if (o_fp == NULL) { if (compress) snprintf(out_f, PATH_MAX, "%s.%s", in_f, suffix); else { rc = strip_ending(out_f, in_f, PATH_MAX, suffix); if (rc < 0) { pr_err("No .%s file!\n", suffix); print_args(stderr, argc, argv); exit(EXIT_FAILURE); } } rc = stat(out_f, &s); if (!force && (rc == 0)) { pr_err("File %s already exists!\n", out_f); print_args(stderr, argc, argv); exit(EX_ERRNO); } o_fp = fopen(out_f, "w+"); if (!o_fp) { pr_err("Cannot open output file %s: %s\n", out_f, strerror(errno)); print_args(stderr, argc, argv); exit(EX_ERRNO); } /* get mode settings for existing file and ... */ rc = fstat(fileno(i_fp), &s); if (rc == 0) { rc = fchmod(fileno(o_fp), s.st_mode); if (rc != 0) { pr_err("Cannot set mode %s: %s\n", out_f, strerror(errno)); exit(EX_ERRNO); } } else /* else ignore ... */ pr_err("Cannot set mode %s: %s\n", out_f, strerror(errno)); /* If output does not go to stdout and a filename is given, set it */ if (name == NULL) name = in_f; } if (isatty(fileno(o_fp))) { pr_err("Output must not be a terminal!\n"); print_args(stderr, argc, argv); exit(EXIT_FAILURE); } if (optind != argc) { /* now it must fit */ usage(stderr, prog, argc, argv); exit(EXIT_FAILURE); } in = malloc(CHUNK_i); /* This is the bigger Buffer by default */ if (NULL == in) { pr_err("%s\n", strerror(errno)); print_args(stderr, argc, argv); exit(EXIT_FAILURE); } out = malloc(CHUNK_o); /* This is the smaller Buffer by default */ if (NULL == out) { pr_err("%s\n", strerror(errno)); print_args(stderr, argc, argv); exit(EXIT_FAILURE); } /* allocate inflate state */ memset(&strm, 0, sizeof(strm)); strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; if (compress) { gz_header head; struct timeval tv; if (extra_fname) { extra_len = file_size(extra_fname); if (extra_len <= 0) { rc = extra_len; goto err_out; } extra = malloc(extra_len); if (extra == NULL) { rc = -ENOMEM; goto err_out; } rc = file_read(extra_fname, extra, extra_len); if (rc != 1) { fprintf(stderr, "err: Unable to read extra " "data rc=%d\n", rc); free(extra); goto err_out; } hexdump(stderr, extra, extra_len); } /* --------------- DEFALTE ----------------- */ rc = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY); if (Z_OK != rc) goto err_out; memset(&head, 0, sizeof(head)); gettimeofday(&tv, NULL); head.time = tv.tv_sec; head.os = 0x03; if (extra != NULL) { head.extra = extra; head.extra_len = extra_len; head.extra_max = extra_len; } if (comment != NULL) { head.comment = (Bytef *)comment; head.comm_max = strlen(comment) + 1; } if (name != NULL) { head.name = (Bytef *)name; head.name_max = strlen(name) + 1; } rc = deflateSetHeader(&strm, &head); if (Z_OK != rc) { fprintf(stderr, "err: Cannot set gz header! rc=%d\n", rc); deflateEnd(&strm); goto err_out; } /* do compression if no arguments */ rc = def(i_fp, o_fp, &strm, in, out); if (Z_OK != rc) zerr(rc); if (extra != NULL) free(extra); deflateEnd(&strm); } else { /* --------------- INFALTE ----------------- */ strm.avail_in = 0; strm.next_in = Z_NULL; rc = inflateInit2(&strm, window_bits); if (Z_OK != rc) goto err_out; do { rc = inf(i_fp, o_fp, &strm, in, out); if (Z_STREAM_END != rc) { zerr(rc); break; } } while (!feof(i_fp) && !ferror(i_fp)); inflateEnd(&strm); } err_out: /* Delete the input file, only if input is not stdin and if output is not stdout */ if ((rc == EXIT_SUCCESS) && (i_fp != stdin) && (o_fp != stdout)) { rc = unlink(in_f); if (rc != 0) { pr_err("%s\n", strerror(errno)); print_args(stderr, argc, argv); exit(EXIT_FAILURE); } } fclose(i_fp); fclose(o_fp); free(in); free(out); exit(rc); }
int main(int argc, char **argv) { int ret=0; myArgs myargs; myFilter myfilter; header fhp; char * outputfile=0; FILE *sourceFile; FILE *destinationFile; if(argc == 1){ progUsage(argv[0]); return UNSUCCESS; } init_Args(&myargs); ret=parseArgs(&myargs,argc,argv); if (ret == false) { progUsage(argv[0]); return UNSUCCESS; } if (myargs.destination == NULL){ ret=newOutputFile(&myargs,outputfile); if (ret == false){ return UNSUCCESS; } myargs.destParsed=true; } /** * Recolher dados para a operacao de filtragem **/ myfilter.sourceName=myargs.source; myfilter.type=myargs.filter; if (myargs.action == compress_action){ myfilter.nbrChars=processFileHeader(myfilter.sourceName,&fhp); myfilter.bpp=(fhp.magicValue>3 ? 3:1); myfilter.lineSize=fhp.imageWidth*myfilter.bpp; myfilter.firstLine=true; } /** * Processamento do ficheiro **/ sourceFile=fopen(myargs.source,"rb"); destinationFile=fopen(myargs.destination,"wr"); SET_BINARY_MODE(sourceFile); SET_BINARY_MODE(destinationFile); printf("Processing file: %s into %s ...", myargs.source,myargs.destination); if (myargs.action == compress_action){ ret = my_compress(sourceFile,destinationFile, myargs.compressLevel, myfilter); }else{ ret = my_decompress(sourceFile, destinationFile, myfilter); } if (ret != Z_OK){ printf("--- ::: ERROR. File not processed!\n"); my_errors(ret); if(myargs.destParsed){ cleanNewFileName(outputfile); } return UNSUCCESS; } printf(" ::: DONE File successufly processed!\n"); if(myargs.destParsed){ cleanNewFileName(outputfile); } return ret; }
int main(int argc, char **argv) { int mode = (argc > 1) ? atoi(argv[1]) : 0; /* Values for use from callbacks through pointers. */ uint32_t req_fixed32 = 1008; int32_t req_sfixed32 = -1009; float req_float = 1010.0f; uint64_t req_fixed64 = 1011; int64_t req_sfixed64 = -1012; double req_double = 1013.0; SubMessage req_submsg = {"1016", 1016}; uint32_t rep_fixed32 = 2008; int32_t rep_sfixed32 = -2009; float rep_float = 2010.0f; uint64_t rep_fixed64 = 2011; int64_t rep_sfixed64 = -2012; double rep_double = 2013.0; SubMessage rep_submsg = {"2016", 2016, true, 2016}; uint32_t opt_fixed32 = 3048; int32_t opt_sfixed32 = 3049; float opt_float = 3050.0f; uint64_t opt_fixed64 = 3051; int64_t opt_sfixed64 = 3052; double opt_double = 3053.0f; SubMessage opt_submsg = {"3056", 3056}; SubMessage oneof_msg1 = {"4059", 4059}; /* Bind callbacks for required fields */ AllTypes alltypes = {{{0}}}; alltypes.req_int32.funcs.encode = &write_varint; alltypes.req_int32.arg = (void*)-1001; alltypes.req_int64.funcs.encode = &write_varint; alltypes.req_int64.arg = (void*)-1002; alltypes.req_uint32.funcs.encode = &write_varint; alltypes.req_uint32.arg = (void*)1003; alltypes.req_uint32.funcs.encode = &write_varint; alltypes.req_uint32.arg = (void*)1003; alltypes.req_uint64.funcs.encode = &write_varint; alltypes.req_uint64.arg = (void*)1004; alltypes.req_sint32.funcs.encode = &write_svarint; alltypes.req_sint32.arg = (void*)-1005; alltypes.req_sint64.funcs.encode = &write_svarint; alltypes.req_sint64.arg = (void*)-1006; alltypes.req_bool.funcs.encode = &write_varint; alltypes.req_bool.arg = (void*)true; alltypes.req_fixed32.funcs.encode = &write_fixed32; alltypes.req_fixed32.arg = &req_fixed32; alltypes.req_sfixed32.funcs.encode = &write_fixed32; alltypes.req_sfixed32.arg = &req_sfixed32; alltypes.req_float.funcs.encode = &write_fixed32; alltypes.req_float.arg = &req_float; alltypes.req_fixed64.funcs.encode = &write_fixed64; alltypes.req_fixed64.arg = &req_fixed64; alltypes.req_sfixed64.funcs.encode = &write_fixed64; alltypes.req_sfixed64.arg = &req_sfixed64; alltypes.req_double.funcs.encode = &write_fixed64; alltypes.req_double.arg = &req_double; alltypes.req_string.funcs.encode = &write_string; alltypes.req_string.arg = "1014"; alltypes.req_bytes.funcs.encode = &write_string; alltypes.req_bytes.arg = "1015"; alltypes.req_submsg.funcs.encode = &write_submsg; alltypes.req_submsg.arg = &req_submsg; alltypes.req_enum.funcs.encode = &write_varint; alltypes.req_enum.arg = (void*)MyEnum_Truth; alltypes.req_emptymsg.funcs.encode = &write_emptymsg; /* Bind callbacks for repeated fields */ alltypes.rep_int32.funcs.encode = &write_repeated_varint; alltypes.rep_int32.arg = (void*)-2001; alltypes.rep_int64.funcs.encode = &write_repeated_varint; alltypes.rep_int64.arg = (void*)-2002; alltypes.rep_uint32.funcs.encode = &write_repeated_varint; alltypes.rep_uint32.arg = (void*)2003; alltypes.rep_uint64.funcs.encode = &write_repeated_varint; alltypes.rep_uint64.arg = (void*)2004; alltypes.rep_sint32.funcs.encode = &write_repeated_svarint; alltypes.rep_sint32.arg = (void*)-2005; alltypes.rep_sint64.funcs.encode = &write_repeated_svarint; alltypes.rep_sint64.arg = (void*)-2006; alltypes.rep_bool.funcs.encode = &write_repeated_varint; alltypes.rep_bool.arg = (void*)true; alltypes.rep_fixed32.funcs.encode = &write_repeated_fixed32; alltypes.rep_fixed32.arg = &rep_fixed32; alltypes.rep_sfixed32.funcs.encode = &write_repeated_fixed32; alltypes.rep_sfixed32.arg = &rep_sfixed32; alltypes.rep_float.funcs.encode = &write_repeated_fixed32; alltypes.rep_float.arg = &rep_float; alltypes.rep_fixed64.funcs.encode = &write_repeated_fixed64; alltypes.rep_fixed64.arg = &rep_fixed64; alltypes.rep_sfixed64.funcs.encode = &write_repeated_fixed64; alltypes.rep_sfixed64.arg = &rep_sfixed64; alltypes.rep_double.funcs.encode = &write_repeated_fixed64; alltypes.rep_double.arg = &rep_double; alltypes.rep_string.funcs.encode = &write_repeated_string; alltypes.rep_string.arg = "2014"; alltypes.rep_bytes.funcs.encode = &write_repeated_string; alltypes.rep_bytes.arg = "2015"; alltypes.rep_submsg.funcs.encode = &write_repeated_submsg; alltypes.rep_submsg.arg = &rep_submsg; alltypes.rep_enum.funcs.encode = &write_repeated_varint; alltypes.rep_enum.arg = (void*)MyEnum_Truth; alltypes.rep_emptymsg.funcs.encode = &write_repeated_emptymsg; alltypes.req_limits.funcs.encode = &write_limits; /* Bind callbacks for optional fields */ if (mode != 0) { alltypes.opt_int32.funcs.encode = &write_varint; alltypes.opt_int32.arg = (void*)3041; alltypes.opt_int64.funcs.encode = &write_varint; alltypes.opt_int64.arg = (void*)3042; alltypes.opt_uint32.funcs.encode = &write_varint; alltypes.opt_uint32.arg = (void*)3043; alltypes.opt_uint64.funcs.encode = &write_varint; alltypes.opt_uint64.arg = (void*)3044; alltypes.opt_sint32.funcs.encode = &write_svarint; alltypes.opt_sint32.arg = (void*)3045; alltypes.opt_sint64.funcs.encode = &write_svarint; alltypes.opt_sint64.arg = (void*)3046; alltypes.opt_bool.funcs.encode = &write_varint; alltypes.opt_bool.arg = (void*)true; alltypes.opt_fixed32.funcs.encode = &write_fixed32; alltypes.opt_fixed32.arg = &opt_fixed32; alltypes.opt_sfixed32.funcs.encode = &write_fixed32; alltypes.opt_sfixed32.arg = &opt_sfixed32; alltypes.opt_float.funcs.encode = &write_fixed32; alltypes.opt_float.arg = &opt_float; alltypes.opt_fixed64.funcs.encode = &write_fixed64; alltypes.opt_fixed64.arg = &opt_fixed64; alltypes.opt_sfixed64.funcs.encode = &write_fixed64; alltypes.opt_sfixed64.arg = &opt_sfixed64; alltypes.opt_double.funcs.encode = &write_fixed64; alltypes.opt_double.arg = &opt_double; alltypes.opt_string.funcs.encode = &write_string; alltypes.opt_string.arg = "3054"; alltypes.opt_bytes.funcs.encode = &write_string; alltypes.opt_bytes.arg = "3055"; alltypes.opt_submsg.funcs.encode = &write_submsg; alltypes.opt_submsg.arg = &opt_submsg; alltypes.opt_enum.funcs.encode = &write_varint; alltypes.opt_enum.arg = (void*)MyEnum_Truth; alltypes.opt_emptymsg.funcs.encode = &write_emptymsg; alltypes.oneof_msg1.funcs.encode = &write_submsg; alltypes.oneof_msg1.arg = &oneof_msg1; } alltypes.end.funcs.encode = &write_varint; alltypes.end.arg = (void*)1099; { uint8_t buffer[2048]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Now encode it and check if we succeeded. */ if (pb_encode(&stream, AllTypes_fields, &alltypes)) { SET_BINARY_MODE(stdout); fwrite(buffer, 1, stream.bytes_written, stdout); return 0; /* Success */ } else { fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream)); return 1; /* Failure */ } } }
int main(int argc, char **argv) { int mode = (argc > 1) ? atoi(argv[1]) : 0; /* Values for required fields */ int32_t req_int32 = -1001; int64_t req_int64 = -1002; uint32_t req_uint32 = 1003; uint64_t req_uint64 = 1004; int32_t req_sint32 = -1005; int64_t req_sint64 = -1006; bool req_bool = true; uint32_t req_fixed32 = 1008; int32_t req_sfixed32 = -1009; float req_float = 1010.0f; uint64_t req_fixed64 = 1011; int64_t req_sfixed64 = -1012; double req_double = 1013.0; char* req_string = "1014"; PB_BYTES_ARRAY_T(4) req_bytes = {4, {'1', '0', '1', '5'}}; static int32_t req_substuff = 1016; SubMessage req_submsg = {"1016", &req_substuff}; MyEnum req_enum = MyEnum_Truth; EmptyMessage req_emptymsg = {0}; int32_t end = 1099; /* Values for repeated fields */ int32_t rep_int32[5] = {0, 0, 0, 0, -2001}; int64_t rep_int64[5] = {0, 0, 0, 0, -2002}; uint32_t rep_uint32[5] = {0, 0, 0, 0, 2003}; uint64_t rep_uint64[5] = {0, 0, 0, 0, 2004}; int32_t rep_sint32[5] = {0, 0, 0, 0, -2005}; int64_t rep_sint64[5] = {0, 0, 0, 0, -2006}; bool rep_bool[5] = {false, false, false, false, true}; uint32_t rep_fixed32[5] = {0, 0, 0, 0, 2008}; int32_t rep_sfixed32[5] = {0, 0, 0, 0, -2009}; float rep_float[5] = {0, 0, 0, 0, 2010.0f}; uint64_t rep_fixed64[5] = {0, 0, 0, 0, 2011}; int64_t rep_sfixed64[5] = {0, 0, 0, 0, -2012}; double rep_double[5] = {0, 0, 0, 0, 2013.0f}; char* rep_string[5] = {"", "", "", "", "2014"}; static PB_BYTES_ARRAY_T(4) rep_bytes_4 = {4, {'2', '0', '1', '5'}}; pb_bytes_array_t *rep_bytes[5]= {NULL, NULL, NULL, NULL, (pb_bytes_array_t*)&rep_bytes_4}; static int32_t rep_sub2zero = 0; static int32_t rep_substuff2 = 2016; static uint32_t rep_substuff3 = 2016; SubMessage rep_submsg[5] = {{"", &rep_sub2zero}, {"", &rep_sub2zero}, {"", &rep_sub2zero}, {"", &rep_sub2zero}, {"2016", &rep_substuff2, &rep_substuff3}}; MyEnum rep_enum[5] = {0, 0, 0, 0, MyEnum_Truth}; EmptyMessage rep_emptymsg[5] = {{0}, {0}, {0}, {0}, {0}}; /* Values for optional fields */ int32_t opt_int32 = 3041; int64_t opt_int64 = 3042; uint32_t opt_uint32 = 3043; uint64_t opt_uint64 = 3044; int32_t opt_sint32 = 3045; int64_t opt_sint64 = 3046; bool opt_bool = true; uint32_t opt_fixed32 = 3048; int32_t opt_sfixed32 = 3049; float opt_float = 3050.0f; uint64_t opt_fixed64 = 3051; int64_t opt_sfixed64 = 3052; double opt_double = 3053.0; char* opt_string = "3054"; PB_BYTES_ARRAY_T(4) opt_bytes = {4, {'3', '0', '5', '5'}}; static int32_t opt_substuff = 3056; SubMessage opt_submsg = {"3056", &opt_substuff}; MyEnum opt_enum = MyEnum_Truth; EmptyMessage opt_emptymsg = {0}; /* Values for the Limits message. */ static int32_t int32_min = INT32_MIN; static int32_t int32_max = INT32_MAX; static uint32_t uint32_min = 0; static uint32_t uint32_max = UINT32_MAX; static int64_t int64_min = INT64_MIN; static int64_t int64_max = INT64_MAX; static uint64_t uint64_min = 0; static uint64_t uint64_max = UINT64_MAX; static HugeEnum enum_min = HugeEnum_Negative; static HugeEnum enum_max = HugeEnum_Positive; Limits req_limits = {&int32_min, &int32_max, &uint32_min, &uint32_max, &int64_min, &int64_max, &uint64_min, &uint64_max, &enum_min, &enum_max}; /* Initialize the message struct with pointers to the fields. */ AllTypes alltypes = {0}; alltypes.req_int32 = &req_int32; alltypes.req_int64 = &req_int64; alltypes.req_uint32 = &req_uint32; alltypes.req_uint64 = &req_uint64; alltypes.req_sint32 = &req_sint32; alltypes.req_sint64 = &req_sint64; alltypes.req_bool = &req_bool; alltypes.req_fixed32 = &req_fixed32; alltypes.req_sfixed32 = &req_sfixed32; alltypes.req_float = &req_float; alltypes.req_fixed64 = &req_fixed64; alltypes.req_sfixed64 = &req_sfixed64; alltypes.req_double = &req_double; alltypes.req_string = req_string; alltypes.req_bytes = (pb_bytes_array_t*)&req_bytes; alltypes.req_submsg = &req_submsg; alltypes.req_enum = &req_enum; alltypes.req_emptymsg = &req_emptymsg; alltypes.req_limits = &req_limits; alltypes.rep_int32_count = 5; alltypes.rep_int32 = rep_int32; alltypes.rep_int64_count = 5; alltypes.rep_int64 = rep_int64; alltypes.rep_uint32_count = 5; alltypes.rep_uint32 = rep_uint32; alltypes.rep_uint64_count = 5; alltypes.rep_uint64 = rep_uint64; alltypes.rep_sint32_count = 5; alltypes.rep_sint32 = rep_sint32; alltypes.rep_sint64_count = 5; alltypes.rep_sint64 = rep_sint64; alltypes.rep_bool_count = 5; alltypes.rep_bool = rep_bool; alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32 = rep_fixed32; alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32 = rep_sfixed32; alltypes.rep_float_count = 5; alltypes.rep_float = rep_float; alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64 = rep_fixed64; alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64 = rep_sfixed64; alltypes.rep_double_count = 5; alltypes.rep_double = rep_double; alltypes.rep_string_count = 5; alltypes.rep_string = rep_string; alltypes.rep_bytes_count = 5; alltypes.rep_bytes = rep_bytes; alltypes.rep_submsg_count = 5; alltypes.rep_submsg = rep_submsg; alltypes.rep_enum_count = 5; alltypes.rep_enum = rep_enum; alltypes.rep_emptymsg_count = 5; alltypes.rep_emptymsg = rep_emptymsg; if (mode != 0) { /* Fill in values for optional fields */ alltypes.opt_int32 = &opt_int32; alltypes.opt_int64 = &opt_int64; alltypes.opt_uint32 = &opt_uint32; alltypes.opt_uint64 = &opt_uint64; alltypes.opt_sint32 = &opt_sint32; alltypes.opt_sint64 = &opt_sint64; alltypes.opt_bool = &opt_bool; alltypes.opt_fixed32 = &opt_fixed32; alltypes.opt_sfixed32 = &opt_sfixed32; alltypes.opt_float = &opt_float; alltypes.opt_fixed64 = &opt_fixed64; alltypes.opt_sfixed64 = &opt_sfixed64; alltypes.opt_double = &opt_double; alltypes.opt_string = opt_string; alltypes.opt_bytes = (pb_bytes_array_t*)&opt_bytes; alltypes.opt_submsg = &opt_submsg; alltypes.opt_enum = &opt_enum; alltypes.opt_emptymsg = &opt_emptymsg; } alltypes.end = &end; { uint8_t buffer[4096]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Now encode it and check if we succeeded. */ if (pb_encode(&stream, AllTypes_fields, &alltypes)) { SET_BINARY_MODE(stdout); fwrite(buffer, 1, stream.bytes_written, stdout); return 0; /* Success */ } else { fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream)); return 1; /* Failure */ } } }
/* Decompress all of the files on the command line, or from stdin if no arguments. */ int main(int argc, char **argv) { FILE *in; int ret; unsigned char *source; void *dest; size_t len, got; #ifdef DEBUG /* process verbosity option */ if (argc > 1 && argv[1][0] == '-') { char *opt; --argc; opt = *++argv; while (*++opt) { if (*opt == 'v') yeast_verbosity++; else { fprintf(stderr, "deb: invalid option %s\n", opt); return 1; } } } #endif /* decompress each file on the remaining command line */ if (--argc) { for (;;) { in = fopen(*++argv, "rb"); if (in == NULL) { fprintf(stderr, "error opening %s\n", *argv); continue; } ret = load(in, &source, &len, 0); fclose(in); if (ret < 0) { fprintf(stderr, "error reading %s\n", *argv); continue; } if (ret > 0) { fputs("out of memory\n", stderr); return 1; } fputs(*argv, stderr); fputs(":\n", stderr); ret = yeast(&dest, &got, source, &len, 0); fprintf(stderr, "uncompressed length = %zu\n", got); if (ret) fprintf(stderr, "yeast() returned %d\n", ret); deliver(*argv, dest, got); free(dest); free(source); if (--argc == 0) break; putc('\n', stderr); } } /* or if no names on the remaining command line, decompress from stdin */ else { SET_BINARY_MODE(stdin); ret = load(stdin, &source, &len, 0); if (ret) { fputs(ret > 0 ? "out of memory\n" : "error reading stdin\n", stderr); return 1; } ret = yeast(&dest, &got, source, &len, 0); fprintf(stderr, "uncompressed length = %zu\n", got); if (ret) fprintf(stderr, "yeast() returned %d\n", ret); deliver("deb", dest, got); free(dest); free(source); } return 0; }
void BuildPackage() { bool foundCache = false; std::string currentFolder = folders.back(); strcpy_s(cachePath, currentFolder.c_str()); strcat_s(cachePath, "\\.fex_cache"); if (cache && !recache) { // TODO: handle several directories FILE* fp = fopen(cachePath, "r"); if (fp) { SET_BINARY_MODE(fp); fseek(fp, 0L, SEEK_END); int size = ftell(fp); fseek(fp, 0L, SEEK_SET); char* inBuffer = new char[size]; if (fread(inBuffer, 1, size, fp) != size && ferror(fp)) { printf("fex warning: couldn't read from file %s (%d)\n", cachePath, ferror(fp)); fclose(fp); delete inBuffer; goto fallback; } fclose(fp); z_stream stream; stream.zalloc = Z_NULL; stream.zfree = Z_NULL; stream.opaque = Z_NULL; stream.avail_in = size - sizeof(int); stream.next_in = (Bytef*)(inBuffer + sizeof(int)); int ret = inflateInit(&stream); if (ret != Z_OK) { printf("fex warning: couldn't init zlib for uncompression (%d)\n", ret); delete inBuffer; goto fallback; } globalBufferSize = *(int*)(inBuffer); globalBuffer = new char[globalBufferSize]; stream.avail_out = globalBufferSize; stream.next_out = (Bytef*)globalBuffer; ret = inflate(&stream, Z_FINISH); if (ret != Z_STREAM_END) { printf("fex warning: couldn't uncompress cache (%d)\n", ret); delete inBuffer; delete globalBuffer; megaBufferSize = 0; goto fallback; } // fixup pointers numBufferFiles = *(int*)globalBuffer; // first word is number of files in bufferFiles bufferFiles = (sBufferFile*)(globalBuffer + sizeof(int)); // next chunk is the bufferFiles buffer megaBuffer = (char*)(bufferFiles + numBufferFiles); // the rest is the mega buffer with the text megaBufferSize = globalBufferSize - (sizeof(int) + sizeof(sBufferFile) * numBufferFiles); inflateEnd(&stream); delete inBuffer; foundCache = true; } } if (foundCache) { return; } fallback: OSVERSIONINFO osInfo; osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osInfo); // large fetch and basic info optimization is only available on windows ver 6.1 and above (win server 2008 r2 and win 7 and above) int largeFetchFlag = 0; FINDEX_INFO_LEVELS infoTypeFlag = FindExInfoStandard; if (osInfo.dwMajorVersion > 6 || (osInfo.dwMajorVersion == 6 && osInfo.dwMinorVersion > 0)) { largeFetchFlag = FIND_FIRST_EX_LARGE_FETCH; infoTypeFlag = FindExInfoBasic; } // list all relevant files LARGE_INTEGER size; size.QuadPart = 0; int numFiles = 0; WIN32_FIND_DATA ffd; while (folders.size()) { std::string currentFolder = folders.back(); strcpy_s(searchPath, currentFolder.c_str()); strcat_s(searchPath, "\\*.*"); HANDLE hFind = FindFirstFileEx(searchPath, infoTypeFlag, &ffd, FindExSearchNameMatch, NULL, largeFetchFlag); folders.pop_back(); if (hFind == INVALID_HANDLE_VALUE) { printf("fex error: FindFirstFileEx failed with error %d - (%s)\n", GetLastError(), searchPath); continue; } do { if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && recursive) { if (ffd.cFileName[0] != '.') { strcpy_s(folderPath, currentFolder.c_str()); strcat_s(folderPath, "/"); strcat_s(folderPath, ffd.cFileName); folders.push_back(std::string(folderPath)); } } else { bool passed = false; const char* extension = GetExtension(ffd.cFileName); if (extension) { for (unsigned int i = 0; i < filters.size(); ++i) { if (!_stricmp(filters[i].c_str(), extension)) { passed = true; break; } } if (passed) { LARGE_INTEGER filesize; filesize.LowPart = ffd.nFileSizeLow; filesize.HighPart = ffd.nFileSizeHigh; strcpy_s(folderPath, currentFolder.c_str()); strcat_s(folderPath, "/"); strcat_s(folderPath, ffd.cFileName); sFile newFile; newFile.name = std::string(folderPath); newFile.size = (unsigned int)filesize.QuadPart; files.push_back(newFile); numFiles++; size.QuadPart += filesize.QuadPart; } } } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); } //printf("BuildPackage: found %d files...total of %.2f Mb\n", numFiles, size.QuadPart / 1024.f / 1024.f); if (numFiles == 0) { return; } CleanUp(); megaBufferSize = (unsigned int)size.QuadPart + (1024 * 1024); // an extra meg as safety net globalBufferSize = sizeof(sBufferFile) * numFiles; globalBufferSize += megaBufferSize; // allocate global buffer, store sizes and fixup pointers globalBuffer = new char[globalBufferSize]; *(int*)globalBuffer = numFiles; // first word is number of files in bufferFiles bufferFiles = (sBufferFile*)(globalBuffer + sizeof(int)); // next chunk is the bufferFiles buffer megaBuffer = (char*)(bufferFiles + numFiles); // the rest is the mega buffer with the text char* put = megaBuffer; if (!globalBuffer) { printf("fex error: out of memory!\n"); return; } // read all files into memory...yes unsigned int bfIdx = 0; for (unsigned int i = 0; i < files.size(); ++i) { const sFile& file = files[i]; FILE* fp = fopen(file.name.c_str(), "r"); if (fread(put, 1, file.size, fp) != file.size && ferror(fp)) { printf("fex warning: couldn't read from file %s (%d)\n", file.name.c_str(), ferror(fp)); fclose(fp); continue; } put += file.size; fclose(fp); sprintf_s(bufferFiles[bfIdx].name, "%s", file.name.c_str()); bufferFiles[bfIdx].ptrOffset = put - megaBuffer; bfIdx++; } numBufferFiles = bfIdx; if (cache || recache) { // TODO: handle several directories FILE* fp = fopen(cachePath, "w"); if (fp) { SET_BINARY_MODE(fp); z_stream stream; stream.zalloc = Z_NULL; stream.zfree = Z_NULL; stream.opaque = Z_NULL; int ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION); if (ret != Z_OK) { printf("fex warning: couldn't init zlib for compression (%d)\n", ret); return; } char* outBuffer = new char[globalBufferSize]; stream.avail_in = globalBufferSize; stream.next_in = (Bytef*)globalBuffer; stream.avail_out = globalBufferSize - sizeof(int); stream.next_out = (Bytef*)(outBuffer + sizeof(int)); ret = deflate(&stream, Z_FINISH); if (ret != Z_STREAM_END) { printf("fex warning: couldn't decompress cache (%d)\n", ret); delete outBuffer; return; } deflateEnd(&stream); *(int*)outBuffer = globalBufferSize; if (fwrite(outBuffer, 1, stream.total_out + sizeof(int), fp) != stream.total_out + sizeof(int) && ferror(fp)) { printf("fex warning: couldn't write to file %s (%d)\n", searchPath, ferror(fp)); delete outBuffer; } fclose(fp); } } }
int main() { uint8_t buffer[2048]; size_t msglen; AllTypes msg = AllTypes_init_zero; /* Get some base data to run the tests with */ SET_BINARY_MODE(stdin); msglen = fread(buffer, 1, sizeof(buffer), stdin); /* Test IO errors on decoding */ { bool status; pb_istream_t stream = {&read_callback, NULL, SIZE_MAX}; faulty_stream_t fs; size_t i; for (i = 0; i < msglen; i++) { stream.bytes_left = msglen; stream.state = &fs; fs.buffer = buffer; fs.fail_after = i; status = pb_decode(&stream, AllTypes_fields, &msg); if (status != false) { fprintf(stderr, "Unexpected success in decode\n"); return 2; } else if (strcmp(stream.errmsg, "simulated") != 0) { fprintf(stderr, "Wrong error in decode: %s\n", stream.errmsg); return 3; } } stream.bytes_left = msglen; stream.state = &fs; fs.buffer = buffer; fs.fail_after = msglen; status = pb_decode(&stream, AllTypes_fields, &msg); if (!status) { fprintf(stderr, "Decoding failed: %s\n", stream.errmsg); return 4; } } /* Test IO errors on encoding */ { bool status; pb_ostream_t stream = {&write_callback, NULL, SIZE_MAX, 0}; faulty_stream_t fs; size_t i; for (i = 0; i < msglen; i++) { stream.max_size = msglen; stream.bytes_written = 0; stream.state = &fs; fs.buffer = buffer; fs.fail_after = i; status = pb_encode(&stream, AllTypes_fields, &msg); if (status != false) { fprintf(stderr, "Unexpected success in encode\n"); return 5; } else if (strcmp(stream.errmsg, "simulated") != 0) { fprintf(stderr, "Wrong error in encode: %s\n", stream.errmsg); return 6; } } stream.max_size = msglen; stream.bytes_written = 0; stream.state = &fs; fs.buffer = buffer; fs.fail_after = msglen; status = pb_encode(&stream, AllTypes_fields, &msg); if (!status) { fprintf(stderr, "Encoding failed: %s\n", stream.errmsg); return 7; } } return 0; }