/* This very simple text-compression algorithm works by stripping away the high-order 0 from the bytes of ASCII characters and transmitting the low-order 7 bits, thus compressing the file to 7/8 its original size. Usage: simple_compress < sourcefile > destinationfile */ #include #include main() { int ch; unsigned char inbyte,outbyte,mask; int outshiftcount=0; while((ch=getchar())!=EOF) { inbyte = (unsigned char) ch; mask=64; while(mask!=0) { outbyte=(outbyte<<1)|(((inbyte&mask)!=0)?1:0); outshiftcount++; if(outshiftcount==8) { putc(outbyte,stdout); outshiftcount=0; } mask=mask>>1; } } if(outshiftcount !=0) { outbyte=outbyte<<8-outshiftcount;/*if untransmitted bits remain, shift them left*/ putc(outbyte,stdout); } }