X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=ao-tools%2Fao-chaosread%2Fao-chaosread.c;h=7808f6c91d5cdad2689219951ac117ff96bc6392;hp=7b138f6cccdb0ef63d69b1047a9ef05b28c4933e;hb=c62816a00b9aa40879a2ea98da2d10667e09b704;hpb=1085ec5d57e0ed5d132f2bbdac1a0b6a32c0ab4a diff --git a/ao-tools/ao-chaosread/ao-chaosread.c b/ao-tools/ao-chaosread/ao-chaosread.c index 7b138f6c..7808f6c9 100644 --- a/ao-tools/ao-chaosread/ao-chaosread.c +++ b/ao-tools/ao-chaosread/ao-chaosread.c @@ -91,7 +91,6 @@ chaoskey_match(libusb_device *dev, char *match_serial) return handle; out: - free(device_serial); if (handle) libusb_close(handle); return 0; @@ -183,15 +182,16 @@ chaoskey_transfer_callback(struct libusb_transfer *transfer) #define ENDPOINT 0x86 int -chaoskey_read(struct chaoskey *ck, uint8_t *buffer, int len) +chaoskey_read(struct chaoskey *ck, void *buffer, int len) { + uint8_t *buf = buffer; int total = 0; while (len) { int ret; int transferred; - ret = libusb_bulk_transfer(ck->handle, ENDPOINT, buffer, len, &transferred, 10000); + ret = libusb_bulk_transfer(ck->handle, ENDPOINT, buf, len, &transferred, 10000); if (ret) { if (total) return total; @@ -201,19 +201,21 @@ chaoskey_read(struct chaoskey *ck, uint8_t *buffer, int len) } } len -= transferred; - buffer += transferred; + buf += transferred; } } static const struct option options[] = { { .name = "serial", .has_arg = 1, .val = 's' }, { .name = "length", .has_arg = 1, .val = 'l' }, + { .name = "infinite", .has_arg = 0, .val = 'i' }, + { .name = "bytes", .has_arg = 0, .val = 'b' }, { 0, 0, 0, 0}, }; static void usage(char *program) { - fprintf(stderr, "usage: %s [--serial=] [--length=[kMG]]\n", program); + fprintf(stderr, "usage: %s [--serial=] [--length=[kMG]] [--infinite] [--bytes]\n", program); exit(1); } @@ -221,16 +223,18 @@ int main (int argc, char **argv) { struct chaoskey *ck; - char buf[1024]; + uint16_t buf[512]; int got; int c; char *serial = NULL; char *length_string; char *length_end; unsigned long length = sizeof(buf); - int this_time; + int this_time; + int infinite = 0; + int bytes = 0; - while ((c = getopt_long(argc, argv, "s:l:", options, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "s:l:ib", options, NULL)) != -1) { switch (c) { case 's': serial = optarg; @@ -247,6 +251,12 @@ main (int argc, char **argv) else if (strlen(length_end)) usage(argv[0]); break; + case 'i': + infinite = 1; + break; + case 'b': + bytes = 1; + break; default: usage(argv[0]); break; @@ -257,16 +267,24 @@ main (int argc, char **argv) if (!ck) exit(1); - while (length) { + if (bytes) + length *= 2; + + while (length || infinite) { this_time = sizeof(buf); - if (length < sizeof(buf)) + if (!infinite && length < sizeof(buf)) this_time = (int) length; got = chaoskey_read(ck, buf, this_time); if (got < 0) { perror("read"); exit(1); } - write(1, buf, got); + if (bytes) { + int i; + for (i = 0; i < got / 2; i++) + putchar((buf[i] >> 1 & 0xff)); + } else + write(1, buf, got); length -= got; } exit(0);