Esempio n. 1
0
int main( int argc , char *argv[] ){
  FILE *fp;
  int r;
  //  unsigned char c ; 
  int c , tot0 = 0 , tot1 = 0  ; 
  fp=stdin;
  r = 0 ; 
  while( (c=getc(fp)) != EOF ) {
    //    printf("\n%d\n",c) ;
    if( c == '1' ) {
      print_encoded(r) ;
      r = 0 ; tot1++;
    }    else if (c=='0') {
      r++   ; tot0++;
      if(r==RMAX) {
	print_encoded(r) ;
	r=0;
      }
    } else {
      // carriage returns
    }
  }
  // clear the buffer of remaining stuff.
  if(r>0){
    fprintf(stderr,"The final run of %d 0s has been replaced by a run of %d\n",r,RMAX) ; 
    r=RMAX;
    print_encoded(r) ;
  }
  fprintf(stderr,"Shannon information content of the file is %d log(f) + %d log(1-f) = %8.3f\n",tot0,tot1, (tot0*log(0.99)+tot1*log(0.01))/log(0.5) ) ; 
  
}
Esempio n. 2
0
void deflate_dynamic(OutBuffer &buf, vector<Symb> &lz77Coded, vector<int> &litLens, vector<int> &dstLens) {
	short BTYPE = 2;
	buf.writebits(BTYPE, 2, true);

	int HLIT = Hcount(litLens, 257);
	int HDIST = Hcount(dstLens, 1);

	vector<Symb> HCcoded = HC_encode(HLIT, HDIST, litLens, dstLens);

	vector<int> hcProbs(19, 0);
	for (int i = 0; i < HCcoded.size(); i++)
		hcProbs[HCcoded[i].lit]++;

	vector<int> hcLens(19);
	Huffman_builder(hcProbs, hcLens);
	vector<int> hcCodes = get_codes(hcLens, 8);

	int HCLEN = 4;
	for (int i = 4; i < hcLens.size(); i++)
		if (hcLens[hcOrder[i]]) HCLEN = i + 1;

	buf.writebits(HLIT - 257, 5, true);
	buf.writebits(HDIST - 1, 5, true);
	buf.writebits(HCLEN - 4, 4, true);

	print_hc_encoded(buf, HCLEN, hcCodes, hcLens, HCcoded);

	int maxLen = 20;
	vector<int> litCodes = get_codes(litLens, maxLen);
	vector<int> dstCodes = get_codes(dstLens, maxLen);
	print_encoded(buf, lz77Coded, litCodes, litLens, dstCodes, dstLens);
}
Esempio n. 3
0
void deflate_fixed(OutBuffer &buf, vector<Symb> &lz77Coded, vector<int> litLens, vector<int> dstLens) {
	short BTYPE = 1;
	buf.writebits(BTYPE, 2, true);

	int maxLen = 20;
	vector<int> litCodes = get_codes(litLens, maxLen);
	vector<int> dstCodes = get_codes(dstLens, maxLen);
	print_encoded(buf, lz77Coded, litCodes, litLens, dstCodes, dstLens);
}
Esempio n. 4
0
static inline void print_stdin_encoded(encoder f, unsigned flags)
{
	char buffer[4096];
	ssize_t l;

	/* zero indicates EOF */
	while ((l = read(STDIN_FILENO, buffer, sizeof(buffer))) != 0) {
		if (l > 0) {
			print_encoded(buffer, l, f, flags);
		} else if (errno == EAGAIN || errno == EINTR) {
			continue;
		} else {
			perror("read");
			_exit(errno);
		}
	}
}
Esempio n. 5
0
int main(int argc, char **argv)
{
	int opt;
	encoder f = printf_encode;
	unsigned flags = 0;

	while ((opt = getopt(argc, argv, "?Vpeun")) != -1) {
		switch (opt) {
		case 'V':
			fputs("printable v" VERSION " <" HOME ">\n"
			      DESCRIPTION "\n"
			      "Copyright (c) 2010, Alejandro Mery <*****@*****.**>\n",
			      stderr);
			return 0;
		case 'p': f = printf_encode; break;
		case 'e': f = echo_encode; break;
		case 'u': f = url_encode; break;
		case 'n': flags ^= SKIP_NL; break;
		default:
			fprintf(stderr,
				"printable v" VERSION "\n" DESCRIPTION "\n\n"
				 "Usage: %s [-V] [-peu] [<strings> ...]\n\n"
				 "  -V   print version and exit\n"
				 "  -p   encode for printf()\n"
				 "  -e   encode for `echo`\n"
				 "  -u   encode for the querystring of a url\n"
				 "  -n   don't encode new lines\n"
				 "\n(stdin is used if no string is given)\n",
				 argv[0]);
			 return(1);
		}
	}

	if (optind < argc) { /* by argument */
		for(int i = optind; i < argc; i++) {
			print_encoded(argv[i], strlen(argv[i]), f, flags);
			stdout_all("\n",1);
		}
	} else {
		print_stdin_encoded(f, flags);
	}
	return 0;
}