coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/base64.c (122 lines, 3.2 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
// base64.c, version 1.0.1
// OMKOV coreutils base64
// Copyright (C) 2021, Jakob Wakeling
// MIT Licence

/*
	TODO Improve or replace fgetb64.
*/

#include "util/base64.h"
#include "util/error.h"
#include "util/optget.h"

#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#define VERSION "1.0.1"

static struct lop lops[] = {
	{ "help",    ARG_NUL, 256 },
	{ "version", ARG_NUL, 257 },
	{ NULL, 0, 0 }
};

static bool dflag;

static inline int base64(const char *file);
static void encodeBase64(FILE *fi);
static void decodeBase64(FILE *fi);

static void wwrite(const uint8_t *p, size_t l, size_t c, size_t *cl, FILE *fi);
static inline size_t fgetb64(uint8_t *buf, size_t len, FILE *fi);

static void hlp(void);
static void ver(void);

int main(int ac, char *av[]) { A0 = av[0];
	struct opt opt = OPTGET_INIT; opt.str = "d"; opt.lops = lops;
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
	case 'd': { dflag = true; break; }
	case 256: { hlp(); return 0; }
	case 257: { ver(); return 0; }
	default: { return 1; }
	}
	
	if (opt.ind == ac) { base64(NULL); }
	else if (opt.ind != ac - 1) { error(1, "extra operand"); }
	else if (base64(av[opt.ind])) { error(1, "%s: %s", av[opt.ind], serr()); }
	
	return 0;
}

/*
	Base64 encode or decode a file.
	If the file path given is null, then use stdin.
*/
static inline int base64(const char *file) {
	FILE *fi;
	
	if (file == NULL) { fi = stdin; }
	else if (!(fi = fopen(file, "r"))) { return 1; }
	
	if (dflag) { decodeBase64(fi); } else { encodeBase64(fi); }
	
	if (fi != stdin) { fclose(fi); } return 0;
}

/* Encode base64 using fixed size buffers */
static void encodeBase64(FILE *fi) {
	uint8_t ibuf[BUFSIZ * 12]; uint8_t obuf[BUFSIZ * 16]; size_t cl = 0;
	
	for (register size_t c; (c = fread(ibuf, 1, sizeof (ibuf), fi));) {
		c = b64encode(obuf, ibuf, c); wwrite(obuf, c, 76, &cl, stdout);
	} fputc('\n', stdout); return;
}

/* Decode base64 using fixed size buffers */
static void decodeBase64(FILE *fi) {
	uint8_t ibuf[BUFSIZ * 16]; uint8_t obuf[BUFSIZ * 12];
	
	for (register size_t c; (c = fgetb64(ibuf, sizeof (ibuf), fi));) {
		c = b64decode(obuf, ibuf, c); fwrite(obuf, 1, c, stdout);
	} return;
}

/* Write string with wrapping */
static void wwrite(const uint8_t *p, size_t l, size_t c, size_t *cl, FILE *fi) {
	for (size_t w = 0; w < l;) {
		size_t cr = c - *cl < l - w ? c - *cl : l - w;
		
		if (!cr) { fputc('\n', fi); *cl = 0; }
		else { fwrite(p + w, 1, cr, fi); w += cr; *cl += cr; }
	}
}

/* Read valid Base64 characters from a file */
static inline size_t fgetb64(uint8_t *buf, size_t len, FILE *fi) {
	size_t i = 0; for (int c; (c = fgetc(fi)) != EOF;) {
		if (isalnum(c) || c == '+' || c == '/') { buf[i++] = c; }
		if (i == len) { break; }
	} return i;
}

/* Print help information */
static void hlp(void) {
	puts("base64 - base64 encode or decode data\n");
	puts("usage: base64 [-d] [file]\n");
	puts("options:");
	puts("  -d         Decode data");
	puts("  --help     Display help information");
	puts("  --version  Display version information");
}

/* Print version information */
static void ver(void) {
	puts("OMKOV coreutils base64, version " VERSION);
	puts("Copyright (C) 2021, Jakob Wakeling");
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
}