You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.9 KiB
68 lines
1.9 KiB
/* bits - pipe the bits of each byte from stdin to stdout
|
|
*
|
|
* usage: bits [<mask>]
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define noreturn __attribute__((noreturn))
|
|
#define noinline __attribute__((noinline))
|
|
#define BUFFER_SIZE 4096
|
|
#define IGNORE(param) do { (void)(param); } while(0)
|
|
#define LIKELY(ex) __builtin_expect((ex), 1)
|
|
#define UNLIKELY(ex) __builtin_expect((ex), 0)
|
|
|
|
#define LIF(ex) if(LIKELY(!!(ex)))
|
|
#define UIF(ex) if(UNLIKELY(!!(ex)))
|
|
|
|
static inline int sfread(void* out, size_t *restrict size, FILE* f)
|
|
{
|
|
register ssize_t r = fread(out, 1, *size, f);
|
|
return r < 0 ? (perror("failed to read from stdin"), 0) : !!(*size = (size_t)r);
|
|
}
|
|
|
|
static int fwrite_all(const void* _buf, size_t sz, size_t num, FILE* out)
|
|
{
|
|
register size_t w, done=0;
|
|
register const unsigned char* buf = _buf;
|
|
while( (w = fwrite(buf+(sz*done), sz, num-done, out)) > 0 && done < num) done+=w;
|
|
if (done!=num) return 0;
|
|
else return 1;
|
|
}
|
|
|
|
static inline void pbits(char out[restrict 8], unsigned char byte, const char bit[static 2])
|
|
{
|
|
for(register int i=8; i --> 0; byte >>= 1) *out++=bit[byte & 1];
|
|
}
|
|
|
|
static noinline noreturn void die(const char* msg)
|
|
{
|
|
fprintf(stderr, "error: %s\n", msg);
|
|
exit(1);
|
|
}
|
|
|
|
// Light assertion (likely to succeed)
|
|
//TODO: Where do we have the __builtin_expect here? On the inside or the outside of the expression? Or both? For now, both.
|
|
#define Lcheck(value, message) (LIKELY(LIKELY(!!(value)) ? 1 : (die(message), 0)))
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
IGNORE(argc);
|
|
|
|
const char* mask = argv[1] && Lcheck(argv[1][0] && argv[1][1], "expected 2 character mask") ? argv[1] : "01";
|
|
unsigned char buf[BUFFER_SIZE];
|
|
char tbuf[BUFFER_SIZE * 8];
|
|
size_t bsz = BUFFER_SIZE;
|
|
|
|
while(sfread(buf, &bsz, stdin))
|
|
{
|
|
register size_t size = bsz;
|
|
while(size --> 0) pbits(tbuf+(8*size), buf[size], mask);
|
|
if(!fwrite_all(tbuf, 8, bsz, stdout)) { perror("Failed to write"); return 1; }
|
|
}
|
|
|
|
return 0;
|
|
}
|