Skip to content

Commit

Permalink
Add 'onerng' and 'xor'
Browse files Browse the repository at this point in the history
  • Loading branch information
za3k committed Apr 21, 2020
1 parent 00665a6 commit 003882c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ This allows for fast startup, by executing a tmux configuration file as a progra
---
Play randomly-generated noise through the speakers

onerng
---
Outputs the (raw) random bytes from an OneRNG hardware device

owns?
---
Checks which Arch Linux package owns a command
Expand Down Expand Up @@ -363,3 +367,11 @@ Counts the number of words in a file or stream.
xadom
---
Runs adom in a fixed-geometry window (because window geometry affects gameplay)

xor
---
Takes the bitwise xor of the given files (or infinite streams), extending all files to the longest length with zeros.

Usage:

xor FILE1 FILE2 [FILE3...]
22 changes: 15 additions & 7 deletions budget_summary.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!/usr/bin/python3
import datetime
import re
import datetime, re, sys

current_date=str(datetime.date.today())
with open("blog2.za3k.com/_posts/{}-weekly-review.md".format(current_date), "r") as f:
if len(sys.argv) > 1:
fp = sys.argv[1]
else:
current_date=str(datetime.date.today())
fp = "/home/zachary/blog2.za3k.com/_posts/{}-weekly-review.md".format(current_date)
with open(fp, "r") as f:
lines = list(line for line in f)

budget_start = re.compile("^\\| Date")
Expand All @@ -19,9 +22,14 @@
for line in budget[2:]:
date, place, amount, category, thing = [x.strip() for x in line.split("|")[1:]]
lines.append((float(amount), category))
print("{: <12} {}".format("Total:", sum(amount for (amount, category) in lines)))
print("{: <12} {}".format("Total (no rent):", sum(amount for (amount, category) in lines if category != "Rent")))
print("{: <12} {:.2f}".format("Total:", sum(amount for (amount, category) in lines)))
print("{: <12} {:.2f}".format("Total (no rent):", sum(amount for (amount, category) in lines if category != "Rent")))
categories = sorted(set(category for (amount, category) in lines))
print()
OTHER = ("Food", "Grocery", "Luxury")
for category in categories:
print("{: <12} {}".format(category+":", sum(amount for (amount, c) in lines if category == c)))
if category not in OTHER:
print("{: <12} {:.2f}".format(category+":", sum(amount for (amount, c) in lines if category == c)))
print("{: <12} {:.2f}".format("Other"+":", sum(amount for (amount, c) in lines if c in OTHER)))
for category in OTHER:
print(" {: <12} {:.2f}".format(category+":", sum(amount for (amount, c) in lines if category == c)))
16 changes: 16 additions & 0 deletions onerng
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
ONERNG=/dev/ttyACM0
t=onerng.out

stty -F $ONERNG raw -echo clocal -crtscts
echo "cmd0" >$ONERNG # standard noise
echo "cmdO" >$ONERNG # turn it on
stty -F $ONERNG raw -echo clocal -crtscts
gone() {
echo "cmdo" >$ONERNG # turn it off
echo "cmd4" >$ONERNG # turn off noise gathering
echo "cmdw" >$ONERNG # flush entropy pool
}
trap gone EXIT SIGINT SIGTERM SIGHUP
dd if=$ONERNG iflag=fullblock bs=1k "$@"
exit 0
20 changes: 20 additions & 0 deletions random_book
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
TMPFILE=`mktemp`
BOOK=$(ssh germinate "/home/zachary/bin/random_book" epub pdf txt html mobi)
scp -q germinate:$BOOK "$TMPFILE"
echo "$BOOK"
case $BOOK in
*.pdf)
evince "$TMPFILE" >/dev/null 2>/dev/null
;;
*.html)
w3m -T text/html "$TMPFILE"
;;
*.txt)
less "$TMPFILE"
;;
*.epub|*.mobi)
cr3 "$TMPFILE" >/dev/null 2>/dev/null
;;
esac
rm "$TMPFILE"
Binary file added xor
Binary file not shown.
55 changes: 55 additions & 0 deletions xor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 'xor'
// by zachary vance, released into the public domain (see CC0 1.0 license)
// Given two streams (exactly) as filenames on the command line, print to stdout the 'xor' of the two files. Pads the shorter file with zeros, so the length of the output is always the length of the longer file
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define MAX_FILES 10
#define BUFFER 1024

int main(int argc, char *argv[]){
FILE *fp[MAX_FILES];
char inbytes[BUFFER], outbytes[BUFFER];
int bytes_read, max_bytes_read;
int files, files_done;
int f,b;

files = argc-1;
if (files < 2) {
printf("usage: xor FILE1 FILE2\n");
return 2;
} else if (files > MAX_FILES) {
printf("Too many files. Please recompile with a faster number of files maximum\n");
return 2;
}

for (f=0;f<files;f++){
fp[f]=fopen(argv[f+1],"rb");
if(fp[f] == NULL){
printf("File %d not found: %s\n", f, argv[f+1]);
return 1;
}
}

while(1) {
files_done=0;
for(f=0;f<files;f++)
if(feof(fp[f])) files_done++;
if(files == files_done) break;

memset(outbytes, 0, BUFFER);
max_bytes_read=0;
for(f=0;f<files;f++) {
bytes_read = fread(&inbytes, 1, BUFFER, fp[f]);
for(b=0;b<bytes_read;b++) outbytes[b] ^= inbytes[b];
if(bytes_read > max_bytes_read) max_bytes_read=bytes_read;
}

write(1, outbytes, max_bytes_read);
}

for(f=0;f<files;f++) fclose(fp[f]);

return 0;
}

0 comments on commit 003882c

Please sign in to comment.