commit e6a1f14c205782f9ee8580575185cf6e74428bdc
parent 0278dc6c7c605e7ef87e0d1127f7bc67cee4f66a
Author: phoebos <ben@bvnf.space>
Date: Sun, 11 Jul 2021 23:46:46 +0100
base64: wrap output
Diffstat:
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/base64.c b/base64.c
@@ -36,7 +36,11 @@
#include <sys/stat.h>
#include <fcntl.h>
-#define BUF_SIZE 999
+/* 54 == 76 * 3/4
+ * so reading the file 54 bytes at a time will cause
+ * the base64 string to be printed out wrapped at 76 chars.
+ */
+#define BUF_SIZE 54
const char tbl_base64[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
@@ -55,6 +59,7 @@ static void usage(const char *name) {
}
char *base64(char *i, ssize_t length, unsigned dflg){
+ /* TODO: support Unicode */
if (dflg) return NULL;
char *out = malloc(length/3 * 4 + 100);
int o = 0; /* index of position in out */
@@ -85,6 +90,20 @@ char *base64(char *i, ssize_t length, unsigned dflg){
return out;
}
+void print_wrapped(char *s, int line_length){
+ /* printf a string wrapped to line_length chars */
+ int c = 0;
+ while (1){
+ if (*s == '\0') break;
+ printf("%c", *s++);
+ c++;
+ if (c == line_length){
+ c = 0;
+ printf("\n");
+ }
+ }
+}
+
int main(int argc, char **argv) {
int c;
unsigned dflg = 0;
@@ -130,7 +149,9 @@ int main(int argc, char **argv) {
char *output = base64(buf, bytes, dflg);
printf("%s\n", output);
+ //print_wrapped(output, 76);
}
+ printf("\n");
if (close(fd) != 0) {
perror("close");